【问题标题】:creating a class for graphics in C++ using SDL使用 SDL 在 C++ 中创建图形类
【发布时间】:2018-01-13 20:48:31
【问题描述】:

我正在尝试用 C++ 为洞穴故事克隆创建一个游戏窗口,所以首先,我在下面创建头文件,然后,我去创建类文件。当我完成课程时,我不断收到参数类型不完整的错误,参数类型为 sdl_window 和 sdl_render。如果有人可以帮助我弄清楚我做错了什么。

图形.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

图形.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);
}

【问题讨论】:

  • @JohnnyMopp 这是不正确的。他可以转发声明结构并引用没有完整定义的指针。
  • 你确定你不是指struct SDL_Window吗?前向声明中的大小写与实际的 SDL 结构不匹配。
  • @AustinBrunkhorst 好的。错过了。
  • 附带说明,为什么不使用SFML?它提供与 SDL 基本相同的功能,但使用 C++ 而不是 C 编写。
  • @TimStraubinger 实际上我不知道 SFML,我的朋友告诉我 SDL,所以我试了一下。我会调查一下,谢谢。

标签: c++ visual-studio sdl


【解决方案1】:

问题在于您声明自己的类型与 SDL 类型无关。重写类以使用适当的类型:

#include <SDL.h>

class Graphics {
  public:
    Graphics();
    ~Graphics();
  private:
    SDL_Window *   window = nullptr;
    SDL_Renderer * render = nullptr;
};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-08
    • 2015-10-17
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 2018-11-25
    相关资源
    最近更新 更多