   SDL main loop help
  this is my main loop and it is freezing for some reason...please help?int main( int argc, char* args[] ){    //Initialize    if( init() == false ){return 1;}    //Load the files    if( load_files() == false ){return 1;}while(event.type != SDL_QUIT){if(scrn==0){apply_surface(0,0,titlebackground,screen);if( event.type == SDL_MOUSEBUTTONDOWN ){ if(event.button.button == SDL_BUTTON_LEFT)scrn=1;}event.type=NULL;}if(scrn==1){//apply_surface( 0, 0, menubackground, screen );Button myButton( 170, 120, 320, 240 );//Handle button events   myButton.handle_events();//show buttonmyButton.show();if( event.type == SDL_MOUSEBUTTONDOWN ){ if(event.button.button == SDL_BUTTON_LEFT)scrn=2;}event.type=NULL;}if(scrn==2){apply_surface( 0, 0, gamebackground, screen );drawMario();    apply_surface( 340, 190, bowser, screen );message = TTF_RenderText_Solid( font, "MARIO VS BOWSER!", textColor );if( message == NULL ) { return 1; } apply_surface( SCREEN_WIDTH/2, 100, message, screen ); }if( event.type == SDL_KEYDOWN ){ switch( event.key.keysym.sym ){ case SDLK_UP: jump(); break; case SDLK_DOWN: messageKeys = downMessage; break; case SDLK_LEFT: marioX--;marioCondition="standingleft"; break; case SDLK_RIGHT: marioX++;marioCondition="standingright"; break; } } else if(event.type == SDL_KEYUP){messageKeys = NULL;}else if( event.type == SDL_QUIT ){clean_up();return 0;} //Update the screen if( SDL_Flip( screen ) == -1 ) { return 1; }}return 1;}
  The first thing that comes up is that SDL events require that you poll for new events by calling SDL_PollEvent(&event) and pass it a pointer to an instance of the SDL_Event structure with each iteration of the loop. It returns 1 as long as there are events in it's queue and 0 when that queue is empty.SDL Doc Wiki article on SDL_PollEvent()[Edited by - prh99 on May 12, 2009 11:39:35 PM];  Thanks!   ;  ok now im getting an error: 'apply_surface': identifier not foundhere:void Button::show(){    //Show the button    apply_surface( box.x, box.y, buttonSheet, screen, buttonclip );}heres the definitions:SDL_Surface *buttonSheet = NULL;SDL_Surface *screen = NULL;SDL_Rect buttonclips[ 4 ];and heres the button class:class Button{    //The attributes of the button    SDL_Rect box;    //The part of the button sprite sheet that will be shown    SDL_Rect* buttonclip;    public:    //Initialize the variables    Button( int x, int y, int w, int h );    //Handles events and set the button's sprite region    void handle_events();    //Shows the button on the screen    void show();};Button::Button( int x, int y, int w, int h ){    //Set the button's attributes    box.x = x;    box.y = y;    box.w = w;    box.h = h;    //Set the default sprite    buttonclip = &buttonclips[ CLIP_MOUSEOUT ];}void Button::handle_events(){    //The mouse offsets    int x = 0, y = 0;    //If the mouse moved    if( event.type == SDL_MOUSEMOTION )    {   //Get the mouse offsets   x = event.motion.x;   y = event.motion.y;   //If the mouse is over the button   if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )   {  //Set the button sprite  buttonclip = &buttonclips[ CLIP_MOUSEOVER ];   }   //If not   else   {  //Set the button sprite  buttonclip = &buttonclips[ CLIP_MOUSEOUT ];   }    }    //If a mouse button was pressed    if( event.type == SDL_MOUSEBUTTONDOWN )    {   //If the left mouse button was pressed   if( event.button.button == SDL_BUTTON_LEFT )   {  //Get the mouse offsets  x = event.button.x;  y = event.button.y;  //If the mouse is over the button  if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )  { //Set the button sprite buttonclip = &buttonclips[ CLIP_MOUSEDOWN ];  }   }    }    //If a mouse button was released    if( event.type == SDL_MOUSEBUTTONUP )    {   //If the left mouse button was released   if( event.button.button == SDL_BUTTON_LEFT )   {  //Get the mouse offsets  x = event.button.x;  y = event.button.y;  //If the mouse is over the button  if( ( x > box.x ) && ( x < box.x + box.w ) && ( y > box.y ) && ( y < box.y + box.h ) )  { //Set the button sprite buttonclip = &buttonclips[ CLIP_MOUSEUP ];  }   }    }}and heres the main segment:if(scrn==1){apply_surface( 0, 0, menubackground, screen );Button myButton( 170, 120, 320, 240 );//Handle button events   myButton.handle_events();//show buttonmyButton.show();if( event.type == SDL_MOUSEBUTTONDOWN ){ if(event.button.button == SDL_BUTTON_LEFT)scrn=2;}event.type=NULL;};  Falling off the end of main is usually not an error. So the convention is to return 0 in this case.Other than that, I think you are confused about event handling. Firstly, the value NULL only makes sense when you are talking about pointers. It is bad style to do something like "event.type = NULL".I would write something like this:struct Button{Button( int, int, int, int );// change: the button handles individual events// each event is passed as a parameter.// instead of a "handle_events()" function.void handle_event(const SDL_Event &event);void show();};// All the game modes.enum Mode{TITLE,BUTTON,BATTLE};int main( int argc, char* args[] ){// note how I combine the two conditionsif( !( init() && load_files() ) ){// Here, we simply return 1 when there is an error.// Not very informative though; "which error" is important.// Did init() fail: if so, how?// Did a file fail to load: which file?return 1;}bool running = true;Mode mode = TITLE;Button button(170, 120, 320, 240);while(running){switch(mode){case TITLE:apply_surface(0,0,titlebackground,screen);break;case BUTTON:button.show();break;case BATTLE:{apply_surface( 0, 0, gamebackground, screen );drawMario();apply_surface( 340, 190, bowser, screen );// This should be a local variable.// Also, you allocate this surface in a loop but never free it.// This will cause you to leak memory.message = TTF_RenderText_Solid( font, "MARIO VS BOWSER!", textColor );// As mentioned above, you should probably implement// more useful error handling.if( message == NULL ){return 1;} apply_surface( SCREEN_WIDTH/2, 100, message, screen ); // You should free "message" here.} break;} // end switch(mode)const Uint8 *keys = SDL_GetKeyState(NULL);if(keys[SDLK_UP]){jump();}if(keys[SDLK_DOWN]){messageKeys = downMessage;}else{// What type is messageKeys?// Remember: NULL is for pointers.messageKeys = NULL;}if(keys[SDLK_LEFT]){marioX--;marioCondition = "standingleft";}else if(keys[SDLK_RIGHT]){marioX++;marioCondition = "standingright";}if( SDL_Flip( screen ) == -1 ){return 1; }// Handle events here.// Remember, events are transient things, you should not store themSDL_Event event;while(SDL_PollEvent(&event)){if(event.type == SDL_QUIT){running = false;}else if(event.type == SDL_MOUSEBUTTONDOWN){switch(mode){case TITLE:mode = BUTTON;break;case BUTTON:mode = BATTLE;break;}}else if(mode == BATTLE){button.handle_event(event);}}}clean_up();return 0;}I have changed how you react to events, because your code did not look like it was event based. I put most of it in a PollEvent() loop. The non-event based code, which is the keyboard stuff, I changed to just query the current keyboard state instead of using events. Some of this may need to change to use the event model, for example the jump() function.Notice now that the button is created only once. In addition, the particular event that gets passed to the button is the local one in main(), not a global event. Also, all the input logic is in one place.   ;  As for your new problem, we would need to understand the file organisation to help you. But I would advise you read this carefully, it will show you how to lay out your files and get rid of errors like that one.   