使用 SDL 在 C++ 中为图形创建 class
creating a class for graphics in C++ using SDL
我正在尝试为 C++ 中的洞穴故事克隆创建一个游戏 window,因此首先,我创建了下面的头文件,之后,我创建了 class 文件.当我完成 class 时,我一直收到错误消息,即参数类型不完整,参数类型为 sdl_window 和 sdl_render。如果有人能帮我弄清楚我做错了什么。
Graphics.h
#ifndef GRAPHICS.h
#define GRAPHICS.h
struct SDL_window;
struct SDL_render;
class Graphics {
public:
Graphics();
~Graphics();
private:
SDL_window* window = NULL;
SDL_render* render = NULL;
};
#endif
Graphics.cpp
#include <SDL.h>
#include "graphics.h"
/* Graphics class
* Holds all information dealing with graphics for the game
*/
Graphics::Graphics() {
SDL_CreateWindowAndRenderer(640, 480, 0, &window, &render);
SDL_SetWindowTitle(window, "Cavestory");
}
Graphics::~Graphics() {
SDL_DestroyWindow(window);
}
问题是您正在声明与 SDL 类型无关的您自己的类型。重写 class 以使用适当的类型:
#include <SDL.h>
class Graphics {
public:
Graphics();
~Graphics();
private:
SDL_Window * window = nullptr;
SDL_Renderer * render = nullptr;
};
我正在尝试为 C++ 中的洞穴故事克隆创建一个游戏 window,因此首先,我创建了下面的头文件,之后,我创建了 class 文件.当我完成 class 时,我一直收到错误消息,即参数类型不完整,参数类型为 sdl_window 和 sdl_render。如果有人能帮我弄清楚我做错了什么。
Graphics.h
#ifndef GRAPHICS.h
#define GRAPHICS.h
struct SDL_window;
struct SDL_render;
class Graphics {
public:
Graphics();
~Graphics();
private:
SDL_window* window = NULL;
SDL_render* render = NULL;
};
#endif
Graphics.cpp
#include <SDL.h>
#include "graphics.h"
/* Graphics class
* Holds all information dealing with graphics for the game
*/
Graphics::Graphics() {
SDL_CreateWindowAndRenderer(640, 480, 0, &window, &render);
SDL_SetWindowTitle(window, "Cavestory");
}
Graphics::~Graphics() {
SDL_DestroyWindow(window);
}
问题是您正在声明与 SDL 类型无关的您自己的类型。重写 class 以使用适当的类型:
#include <SDL.h>
class Graphics {
public:
Graphics();
~Graphics();
private:
SDL_Window * window = nullptr;
SDL_Renderer * render = nullptr;
};