Thread: SDL Window Closes...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    118

    SDL Window Closes...

    I figured some SDL guru's lurked around these areas...

    I'm writing a... well, I fear saying 'game engine', because when I start to make stuff official, it starts to fail, but for the purposes of argument, a game engine. I'm using SDL to give me a windowing context at the current moment. Here's what I have:

    main.h:
    Code:
    int SW;
    int SH;
    int BPP;
    
    class SGE
    {
    	public:
    		SGE();
    		~SGE();
    		void startWindow(int screenWidth, int screenHeight, int bitsPerPixel);
    		void stopWindow();
    		
    	private:
    		
    };
    
    SGE::SGE()
    {
    
    }
    
    SGE::~SGE()
    {
    
    }
    
    void SGE::startWindow(int screenWidth, int screenHeight, int bitsPerPixel)
    {
         screenWidth = SW;
         screenHeight = SH;
         bitsPerPixel = BPP;
         
    	// Initialize SDL
    	SDL_Init(SDL_INIT_EVERYTHING);
    	
    	// Set the SDL Video Mode
    	SDL_SetVideoMode(SW, SH, BPP, SDL_OPENGL);
    	
    	// Set Window Caption
    	SDL_WM_SetCaption("SGT Engine", NULL);
    	
    	glClear(GL_COLOR_BUFFER_BIT);
    	SDL_GL_SwapBuffers();
    }
    
    void SGE::stopWindow()
    {
         SDL_Quit();
    }
    main.cpp:
    Code:
    int main(int argc, char *argv[])
    {
        SGE   engineInstance;
        
        engineInstance.startWindow(20, 20, 32);
        
        engineInstance.stopWindow();
    }
    I've taken some other (nothing that could cause the problem) stuff from my posted code, but this is it for the window stuff...

    Thing is, when I run it, I just get a quick window that opens, and then it closes almost instantaneously. What is causing this sudden termination? Or, if worded wrong, what do I need to do to keep it open?

    Thanks so much!
    FlyingIsFun1217

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    The window closes because you are telling it to you might want to put a delay or a main loop between startWindow and stopWindow. Maybe check out 'event polling' in SDL so you can handle input and quit when the user wants to.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Quote Originally Posted by mike_g View Post
    The window closes because you are telling it to you might want to put a delay or a main loop between startWindow and stopWindow. Maybe check out 'event polling' in SDL so you can handle input and quit when the user wants to.
    So basically, it's closing because I'm not doing anything with the OpenGL context?

    FlyingIsFun1217

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by FlyingIsFun1217 View Post
    So basically, it's closing because I'm not doing anything with the OpenGL context?

    FlyingIsFun1217
    Or, perhaps better put, because you are not doing anything between creating (opening) the window and closing it. The window would stay open (longer) if you put a sleep or long for-loop in there, even if there's no OpenGL code involved [although a blank window is sort of meaningless, whether it stays up for a few microseconds or a longer period].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Quote Originally Posted by matsp View Post
    Or, perhaps better put, because you are not doing anything between creating (opening) the window and closing it. The window would stay open (longer) if you put a sleep or long for-loop in there, even if there's no OpenGL code involved [although a blank window is sort of meaningless, whether it stays up for a few microseconds or a longer period].

    --
    Mats
    I see what you mean. In the main.cpp file, I am explicitly opening a window, and suddenly closing it, so there is no reason to keep it open, on the computer's part. The thing I was confused on was why it would close so instantaneously (like it does in the posted code) even when I never declared SDL_Quit();. Is that because it automatically closes?

    As to why I am attempting to open a blank window: Since I am attempting to make a game engine (which should really just be called a template at this point), I want everything to be pluggable. So, right now I'm just checking to see that it opens a window that will eventually have 'stuff' in it.

    FlyingIsFun1217

  6. #6
    Sanity is for the weak! beene's Avatar
    Join Date
    Jul 2006
    Posts
    321
    Just comment out engineInstance.stopWindow() in main.cpp and then run it.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by FlyingIsFun1217 View Post
    I see what you mean. In the main.cpp file, I am explicitly opening a window, and suddenly closing it, so there is no reason to keep it open, on the computer's part. The thing I was confused on was why it would close so instantaneously (like it does in the posted code) even when I never declared SDL_Quit();. Is that because it automatically closes?
    Code:
    void SGE::stopWindow()
    {
         SDL_Quit();
    }
    Isn't this doing SDL_Quit()?


    [/quote]

    As to why I am attempting to open a blank window: Since I am attempting to make a game engine (which should really just be called a template at this point), I want everything to be pluggable. So, right now I'm just checking to see that it opens a window that will eventually have 'stuff' in it.

    FlyingIsFun1217[/QUOTE]

    Sure, I understand that - I was just being a bit sarcastic, which sometimes gets a lost across the internet.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Quote Originally Posted by matsp View Post
    Isn't this doing SDL_Quit()?
    Yeah, but I meant when I had not implemented engineInstance.stopWindow();. Basically, it did the same thing before as now, I believe. I should probably try again though, I might have been hallucinating... :P

    Quote Originally Posted by matsp View Post
    Sure, I understand that - I was just being a bit sarcastic, which sometimes gets a lost across the internet.
    Boy do I know that feeling!
    I take most stuff lightly, though, it's alright

    FlyingIsFun1217

  9. #9
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Quote Originally Posted by beene View Post
    Just comment out engineInstance.stopWindow() in main.cpp and then run it.
    Yep, still closes the window almost instantly... :/

    FlyingIsFun1217

    ----------EDIT----------

    After putting in a simple

    Code:
    int number = 0;
        
        while(number == 0)
        {
                     
        }
    It pauses the window as it is. Of course, the program also rightly freezes.

    I can now say I know what I need to do with the code to keep the window open. I really just need to implement an action catch and see if the user closed the window.

    Thanks!
    FlyingIsFun1217
    Last edited by FlyingIsFun1217; 11-20-2007 at 04:45 PM.

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Alright, I fixed it by adding the following class

    Code:
    class events
    {
    	public:
    		events();
    		~events();
    		bool closeRequest();
    		
    	private:
    		SDL_Event event;
    };
    
    events::events()
    {
                    
    }
    
    events::~events()
    {
                     
    }
    
    bool events::closeRequest()
    {
         bool quit = false;
         
         while(quit == false)
         {
             while(SDL_PollEvent(&event))
             {
                 if(event.type == SDL_QUIT)
                 {
                     quit = true;
                 }
             }
         }
         
         return quit;                                                 
    }
    ...and replaced engineInstance.stopWindow(); with

    Code:
    while(eventInstance.closeRequest() == true)
        {
            engineInstance.stopWindow();
        }
    It works as it's supposed to. Is there a different (proper) way of doing it?

    And my abiding question, last thing before I stop bugging you guys

    Is there any good place to get SDL-specific help? Any forums in particular? I know about and have visited places like devmaster.net and gamedev.net, but as to something pertaining specifically to SDL...?

    Thanks so much!
    FlyingIsFun1217

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I guess you called your class SGE for "SDL Graphics Engine" . . . unfortunately, there's already a library for the SDL called SGE (probably standing for the same thing): http://libsdl.org/libraries.php?orde...sge&perpage=50
    (It's a very good library, too.) So if you ever publish your engine, you'll probably have to invent a new name for it. Maybe FiFSGE.

    For your event code, you could use SDL_WaitEvent() instead of SDL_PollEvent(), if all you're looking for is an SDL_QUIT event. Of course, you'll probably extend that class later to handle all event types, so you'd likely have to change it back.

    Is there any good place to get SDL-specific help? Any forums in particular? I know about and have visited places like devmaster.net and gamedev.net, but as to something pertaining specifically to SDL...?
    You could try the SDL newsgroup. http://news.gmane.org/gmane.comp.lib.sdl

    You'll probably get reasonably quick responses here on CBoard, actually, but I imagine the newsgroup is the best place around.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Quote Originally Posted by dwks View Post
    I guess you called your class SGE for "SDL Graphics Engine"
    Actually, I decided to use SGE because I was going to call it Stupid Game Engine, which it really is, kinda.

    Thanks for the link to the newsgroup! Hopefully I will get more specific knowledge on SDL there (not that people here don't know their stuff )

    FlyingIsFun1217

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Just starting Windows Programming, School me!
    By Shamino in forum Windows Programming
    Replies: 17
    Last Post: 02-22-2008, 08:14 AM
  2. WM_CAPTION causing CreateWindowEx() to fail.
    By Necrofear in forum Windows Programming
    Replies: 8
    Last Post: 04-06-2007, 08:23 AM
  3. 6 measly errors
    By beene in forum Game Programming
    Replies: 11
    Last Post: 11-14-2006, 11:06 AM
  4. Linking OpenGL in Dev-C++
    By linkofazeroth in forum Game Programming
    Replies: 4
    Last Post: 09-13-2005, 10:17 AM
  5. OpenGL Window
    By Morgul in forum Game Programming
    Replies: 1
    Last Post: 05-15-2005, 12:34 PM