Need Help With Loop Freezing.....


deadlychicken22

Man is a reasoning rather than a reasonable animal
Joined
Mar 18, 2004
Messages
1,501
Location
MN, USA
Website
Visit site
The code for my menu loop and a couple other loops of mine seems to have some problems with them. When I test them on my gp2x and I don't press any button for awhile, it will freeze up on me and not react to any buttons. However, my input loop in my game loop seems to work fine (no freezing). I have looked through both of them many times and can't find the problem. The code for my main menu is below, please help me find the source of my problems.
Code:
    int option=1;
    int selectOption=0;
    //variables for button input
    int exit_menu = 0;
    //MENU LOOP HERE
    while(exit_menu == 0)
    { 
    SDL_Event event;
 
    while(SDL_PollEvent(&event))
    {
     switch(event.type)
     {
      case SDL_JOYBUTTONDOWN:
      switch(event.jbutton.button)
      {
       case GP2X_BUTTON_UP:
                 option -= 1;
                 break;                              
       case GP2X_BUTTON_DOWN:
                 option += 1;
                 break;
                 break;
       case GP2X_BUTTON_X:
                 selectOption = 1;
                 break;
       }
       break;
 
      }
    }
    //act on input
    if(selectOption == 1)
    {
     selectOption = 0;
     //if the user selects story mode
     if(option == 1) exit_menu = 1;
     //if the user selects arcade mode
     else if(option == 2) exit_menu = 1;
     else if(option == 3)
     {
       //If the user exits
       chdir("/usr/gp2x");
       execl("/usr/gp2x/gp2xmenu", "/usr/gp2x/gp2xmenu", NULL);
       return 0;
       }
     }
    if(option < 1) option=3;
    if(option > 3) option=1;
    //-----Draw menu
    menu = load_image("menu_background.bmp");
    //Draw menu options
    if(option == 1) options_screen = load_image("option1.bmp");
    if(option == 2) options_screen = load_image("option2.bmp");
    if(option == 3) options_screen = load_image("option3.bmp");
 
    DrawIMG(menu, screen, 0, 0);
    DrawIMG(options_screen, screen, 132, 142);    
    //Blit image to actual screen
    SDL_Flip(screen);
 
    }
    //End Menu Loop

Thanks,
-ben
 
Whilst I cant help you with your problem, I would suggest that you stop using 'magic numbers' that don't mean anything on their own.

eg:
Code:
if(selectOption == 1) // What is 1?

So use #defines, const globals or enums
eg:
Code:
#define MENU_EXIT_TO_GP2X (1)
....
if(selectOption == MENU_EXIT_TO_GP2X) // Much more readable.

edit: The only thing that I can think of is that you don't have a "case: default" in your switch and you are constantly loading images everyframe. Do you have to free the images up in SDL manually?
 
Thanks for the advice. I am new to SDL and C++ so much of this is new to me.

Yes, I'm pretty sure you have to free the images up in SDL manually (SDL_FreeSurface, i believe). I will look into that.....
 
In that case I be putting money on that you have a memory leak through the constant image loading which causes the GP2X to crash.
 
yaustar posted on Feb 11 2006 at 04:30 AM said:
In that case I be putting money on that you have a memory leak through the constant image loading which causes the GP2X to crash.
Ok, I'm looking into that right now.....

EDIT: well, it looks like you were right, thanks for the help. I don't know why i didn't think of that, now i feel really stupid... (i was thinking it had to do with an endless loop or something like that....) I just moved all image loading before the loop and then only had to draw the images in the loop.
 
Last edited by a moderator:
yaustar posted on Feb 11 2006 at 05:16 AM said:
Whilst I cant help you with your problem, I would suggest that you stop using 'magic numbers' that don't mean anything on their own.

eg:
Code:
if(selectOption == 1) // What is 1?

So use #defines, const globals or enums
eg:
Code:
#define MENU_EXIT_TO_GP2X (1)
....
if(selectOption == MENU_EXIT_TO_GP2X) // Much more readable.

edit: The only thing that I can think of is that you don't have a "case: default" in your switch and you are constantly loading images everyframe. Do you have to free the images up in SDL manually?
Nice tip.
 
Last edited by a moderator:
Thanks for the encouragment and help. My game is now up and running well, although there is still a lot of work that needs to be done on it. I hope to have it ready by the 20th so I can enter it into the competition.
 
Back
Top