2
\$\begingroup\$

I need to check if the game's SDL Window is open before I can start rendering graphics, normally I would know how to this, but the problem is the window is created in the main class, and I need to check if the window is open in the menu class. How do I check if the window is open in an entirely different class?

\$\endgroup\$
  • 1
    \$\begingroup\$ Hi from StackOverflow! Just a heads-up about crossposting to multiple stacks: it is quite strongly discouraged. About your question, the simplest approach is storing your window pointer inside a global variable :) \$\endgroup\$ – Quentin Oct 24 '16 at 8:26
  • 1
    \$\begingroup\$ Voting to close this; it doesn't seem specific to game development (despite SDL) but is rather a case of sharing state between classes, which is a generic C++ problem. \$\endgroup\$ – Maximus Minimus Oct 24 '16 at 8:49
  • 2
    \$\begingroup\$ @LeComte a generic Object oriented problem, if you want to be more specific \$\endgroup\$ – Bálint Oct 24 '16 at 8:50
  • \$\begingroup\$ How would you do it in the main class? In which class is the menu instance created? \$\endgroup\$ – Thomas Nov 24 '16 at 13:52
2
\$\begingroup\$

You dont specify if you are using SDL 1.2, or SDL 2. I will explain it with SDL 1.2.

When yo do a

SDL_Surface* = SDL_SetVideoMode(Params...);

to create the window, you set the pointer of the window created in your SDL_Surface* SDL variable.

The function has two returns.

If has been executed successfully, it will return the pointer of the window. If has NOT been executed successfully, it will return 'NULL';

So, yo need to check if the windows was created checking that pointer. I will copy a segment of my code in SDL:

    Window = SDL_SetVideoMode(WindowWidth, WindowHeigth,Bpp, SDL_HWSURFACE | SDL_RESIZABLE);    /// Asigno al Window el puntero asignado de la pantalla creada

    if( Window == NULL )    /// Window inicializado con error
    {
        if(Is_Open) { Log("Inicialize Window error"); } if(Is_Open) { Log(SDL_GetError()); }
        if(Is_Open) { Log("Window", Window); }
        SDL_Quit();
        exit(-1);
    }

Try to read the documentation for each function. Its not hard. Docs for SDL 1.2: https://www.libsdl.org/release/SDL-1.2.15/docs/ for SDL 2: https://wiki.libsdl.org/

And if you are using different functions you have 2 choices, make the variable of the window Global, or, passing by parameter to all your functions.

PD: Sorry for my english!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.