SDL Help

Started by
7 comments, last by Scet 18 years ago
Well, I decided to try out SDL because the way Irrlicht is designed it didn't work out to well for my game. So I have SDL setup with Dev-Cpp, and everything compiles fine but when I run the program it doesn't stay open. Any ideas?

/*  RBreakout.h
    This is the actual game class
*/
#include <SDL/SDL.h>

class RBreakout
{
      public:
             RBreakout();
             int GameLoop();
      private:
             SDL_Surface *screen; 

};

// Constructor
RBreakout::RBreakout()
{
   // Initiate SDL
   if(SDL_Init(SDL_INIT_VIDEO) < 0)
   {
      printf("Unable to start SDL");
      exit(1);
   }
   
   // Register EXIT so things get cleaned up correctly
   atexit(SDL_Quit);
   
   // Attempt to create a window
   screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
   // Else fail
   if(screen = NULL)
      printf("Opps, you bwoke it. Cannot create the window");
   
}

int RBreakout::GameLoop()
{
    while(1)
    {
       
       
       // Event Handeling
       SDL_Event event;  
       while(SDL_PollEvent(&event))
       {                          
                                           
            // Handle Events
            switch(event.type)
            {                            
                              
               case SDL_KEYUP:
               {
                    if(event.key.keysym.sym == SDLK_ESCAPE)
                       return 0;
               }
               case SDL_QUIT:
               {
                    return 0;
               }
            }
       }
     }
     return 0;
       
}



PS: I HATE it when you figure out the problem right after you post about it. It is late and I am tierd so this might be the case.
Blupix Games
Advertisement
What does your main look like?

inside your main you have to be sure to create a new RBreakout object and call it's GameLoop function.
Yep, it does that

#include <iostream>#include <cstdlib>#include "RBreakout.h"int main(int argc, char *argv[]){    RBreakout game;    game.GameLoop();    return 0;}
Blupix Games
I compiled the code you posted and it runs just fine. But there is one line containing an error which might lead to some headache in the future:
   if(screen = NULL) // should be if(screen == NULL)      printf("Opps, you bwoke it. Cannot create the window");
baumep
Besides the above, you probably need to flip the backbuffer every frame.

See: SDL_Flip()
Thanks. I fixed both of your suggestions and it still flashes on for a second them closes. :-\
Blupix Games
You need breaks after your cases like so:
case SDL_KEYUP:   if(event.key.keysym.sym == SDLK_ESCAPE)       return 0;   break;case SDL_QUIT:   return 0;   break;


You don't need the braces either. It could be that a key up occurs, but is not escape, but because there is no break, it falls through to the quit case and exits.
SDL_Delay(5000) // @param - milliseconds. So, 5 seconds it pauses

Also, get in the habit of adding break; to any of your case statements or you'll likely run into headaches. ;)

case foo:
blah;
break;
case bar:
blah;
break;

It may also be better practice to create a boolean, set it to false and make your gameloop do a while(!done) { }

So, get rid of your while inside of the gameloop, go back to your main and wrap it with

bool done = false;

while(!done) {
game.GameLoop();
}

and make sure inside your gameloop you set bool to true when your keypress is active.

-Dave
Quote:Original post by ildave1
bool done = false;

while(!done) {
game.GameLoop();
}


or another method would be to have GameLoop() return false when the user exits.

bool RBreakout::GameLoop(){    // Event Handeling    SDL_Event event;      while(SDL_PollEvent(&event))    {                                                                              // Handle Events         switch(event.type)         {                                                                      case SDL_KEYUP:                 if(event.key.keysym.sym == SDLK_ESCAPE)                 {                    return false;                 }                 break;            case SDL_QUIT:                 return false;                 break;         }     }     return true;      }int main(int argc, char *argv[]){    RBreakout game;    while( game.GameLoop() ) {}    return 0;}

This topic is closed to new replies.

Advertisement