5

My code:

  1. window.cpp

    Window::Window(int w, int h, const char *title, const char *icon)
    {
        height = h;
        width = w;
    
        if(SDL_Init( SDL_INIT_EVERYTHING ) == 0)
        {
            SDL_WM_SetCaption(title, NULL);
            SDL_WM_SetIcon(SDL_LoadBMP(icon),NULL);
    
            screen = SDL_SetVideoMode(width, height, 32,
                         SDL_SWSURFACE | SDL_RESIZABLE | SDL_DOUBLEBUF);
            if(screen == NULL)
            {
                running = false;
                return;
            }
            fullscreen = false;
        }
        else
            running = false;
            return;
    }
    
    Window::Window()
    {
        const SDL_VideoInfo* info = SDL_GetVideoInfo();
        screenWidth = info->current_w;
        screenHeight = info->current_h;
        Window(640, 480, "Flatgu game", "rsc/img/icon.bmp");
    }
    
  2. window.h

    class Window
    {
    public:
        Window();
        ~Window();
    
        int getWidth() {return width;}
        int getHeight() {return height;}
        bool isFullscreen() {return fullscreen;}
    
        void toggleFullscreen();
    
    private:
        Window(int w, int h, const char *title, const char *icon);
    
        bool fullscreen, running;
        int height, width, screenWidth, screenHeight;
        SDL_Surface *screen;
    };
    

It compiles fine, but then, after compiling, I'm getting this ugly error: Some address problem

What's the reason of my problem? Why do I get so weird numbers?

My aim is to store original screen resolution for further use (like toggling to fullscreen), and I have to do this before calling SDL_SetVideoMode(). That's why it is in the constructor.

sjaustirni
  • 3,056
  • 7
  • 32
  • 50

3 Answers3

6

You have a problem with calling SDL Video Functions before actually initializing SDL.

SDL_Init( SDL_INIT_EVERYTHING )

has to be called before

SDL_GetVideoInfo(); 

In your case you call SDL_GetVideoInfo(); first

const SDL_VideoInfo* info = SDL_GetVideoInfo();   //<-- calls SDL_GetVideoInfo();   
screenWidth = info->current_w;
screenHeight = info->current_h;
Window(640, 480, "Flatgu game", "rsc/img/icon.bmp");    //<-- initializes SDL

So the solution is simple; make the call SDL_Init( SDL_INIT_EVERYTHING ) immediately at the start of your program, then you can call SDL_GetVideoInfo(); as much as you like. You will have to restructure your class Window slightly.

  • That no longer works with SDL2. See [a newer answer](https://stackoverflow.com/a/33393689/18920490) – Sisyffe Oct 01 '22 at 08:48
3

To get the best video mode call SDL_GetVideoInfo before setting up the video (before calling SDL_SetVideoMode).

But you still have to initialize the video subsystem before calling it (SDL_Init(SDL_INIT_VIDEO)).

talles
  • 14,356
  • 8
  • 45
  • 58
3

I know this is old, but there's a big mistake in the code.

Window(640, 480, "Flatgu game", "rsc/img/icon.bmp");

creates a nameless instance of a Window, so the instance that calls it will still have uninitialized variables. It looks like you were trying to use delegating constructors, but in that case the call to the other constructor must be in the member initializer list. See this page.

Community
  • 1
  • 1
jhoffman0x
  • 795
  • 5
  • 9