Jump to content
  • Advertisement
Sign in to follow this  
Mr_Fhqwhgads

SDL Help

This topic is 4588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement
Yep, it does that


#include <iostream>
#include <cstdlib>
#include "RBreakout.h"

int main(int argc, char *argv[])
{
RBreakout game;
game.GameLoop();
return 0;
}

Share this post


Link to post
Share on other sites
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");

Share this post


Link to post
Share on other sites
Besides the above, you probably need to flip the backbuffer every frame.

See: SDL_Flip()

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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;
}



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!