Search code examples
c++visual-studiosdl

creating a class for graphics in C++ using SDL


I'm trying to create a game window for a cave story clone in C++, so first, I create the header file below, after that, I went to create the class file. When I finish the class I kept receiving the error that the argument type is incomplete with a parameter of type sdl_window and sdl_render. If anyone could help me figure out what I'm doing wrong.

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);
}

Solution

  • The problem is that you are declaring your own types unrelated to SDL types. Rewrite class to use appropriate types:

    #include <SDL.h>
    
    class Graphics {
      public:
        Graphics();
        ~Graphics();
      private:
        SDL_Window *   window = nullptr;
        SDL_Renderer * render = nullptr;
    };