Jump to content
  • Advertisement
Sign in to follow this  
ukdeveloper

Switch statement problem? (Not 100% sure)

This topic is 4868 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

Hi, I'm building a main menu for a game of Tic Tac Toe in SDL. I can change menu states fine by pressing up and down on the keyboard, and the Quit command works perfectly. I'm using nested switch statements. But for some reason, the switch statement I'm using to handle what the menu state was when return/enter was pressed either isn't updating a global variable, or the switch statement isn't returning. Here's the code for the main gameloop (I'm just working on the menu at the moment - that's why there are Message Boxes in place of the actual game for most of it):

// WManager.MainWindow is the main game window - a standard SDL surface

int GameState=1; // The Game state - 1 is the main menu

int MenuState=1; // Determines which menu item is selected

void gameloop()
{  
    
    switch(GameState) // What state is the game in?
    {
                      
                      case 1: // Main Menu
                      
                      Text.Write(WManager.MainWindow,"TicTacToe",190,50,84,200,150,150,0,0,0);
                      
                      switch(MenuState)
                      {
                      
                      case 1: // Single player is selected - default
                                
                      Text.Write(WManager.MainWindow,"Single Player",275,210,35,255,0,0,0,0,0);
                      
                      Text.Write(WManager.MainWindow,"Two Player",290,290,35,255,255,255,0,0,0);
                      
                      Text.Write(WManager.MainWindow,"Instructions",290,370,35,255,255,255,0,0,0);
                      
                      Text.Write(WManager.MainWindow,"Quit",350,450,35,255,255,255,0,0,0);
                      
                      HandleEvents(1); // GameState 1 is passed in as a parameter so that the event handler knows which set of switches to use
                      
                      break;
                      
                      case 2: // Two player is selected
                           
                      Text.Write(WManager.MainWindow,"Single Player",275,210,35,255,255,255,0,0,0);
                      
                      Text.Write(WManager.MainWindow,"Two Player",290,290,35,255,0,0,0,0,0);
                      
                      Text.Write(WManager.MainWindow,"Instructions",290,370,35,255,255,255,0,0,0);
                      
                      Text.Write(WManager.MainWindow,"Quit",350,450,35,255,255,255,0,0,0);
                      
                      HandleEvents(1);
                      
                      break;
                      
                      case 3: // Instructions selected
                      
                      Text.Write(WManager.MainWindow,"Single Player",275,210,35,255,255,255,0,0,0);
                      
                      Text.Write(WManager.MainWindow,"Two Player",290,290,35,255,255,255,0,0,0);
                      
                      Text.Write(WManager.MainWindow,"Instructions",290,370,35,255,0,0,0,0,0);
                      
                      Text.Write(WManager.MainWindow,"Quit",350,450,35,255,255,255,0,0,0);
                      
                      HandleEvents(1);
                      
                      break;
                      
                      case 4: // Quit is selected
                      
                      Text.Write(WManager.MainWindow,"Single Player",275,210,35,255,255,255,0,0,0);
                      
                      Text.Write(WManager.MainWindow,"Two Player",290,290,35,255,255,255,0,0,0);
                      
                      Text.Write(WManager.MainWindow,"Instructions",290,370,35,255,255,255,0,0,0);
                      
                      Text.Write(WManager.MainWindow,"Quit",350,450,35,255,0,0,0,0,0);
                      
                      HandleEvents(1);
                      
                      break;
                      
                      
                      }
                      
                      break;
                      
                      case 2:
                           
                           SDL_FillRect(WManager.MainWindow,NULL,0);
                           
                           Board.DrawSprite(45,15);
                           
                           break;   
                           
                      case 3:
                           
                           MessageBox(NULL,"Two Player","Two Player",MB_OK|MB_ICONSTOP);
                           
                           break;
                                
                      case 4:
                              
                            MessageBox(NULL,"Instructions","Instructions",MB_OK|MB_ICONSTOP);
                              
                            break;
                              
    }
    
    SDL_Delay(10);
    
    SDL_Flip(WManager.MainWindow);
    
}


And here's HandleEvents():
/* HandleEvents - Gamestate passed as a parameter - means the game can handle all inputs using the one function */

void HandleEvents(int GameState)
{
     
     SDL_Event game_event;
     
     switch(GameState)
     {
      
                      case 1: // The Main Menu is active
                      
                           while(SDL_PollEvent(&game_event))
                           {
                                                            
                                                            switch(game_event.type)
                                                            {
                                                                                   
                                                                                   case SDL_KEYDOWN:
                                                                                        
                                                                                        switch(game_event.key.keysym.sym)
                                                                                        {
                                                                                                                         case SDLK_UP:
                                                                                                                              
                                                                                                                              if(MenuState!=1)
                                                                                                                              {
                                                                                                                                             
                                                                                                                                             MenuState--;
                                                                                                                                             
                                                                                                                              }
                                                                                                                                    
                                                                                                                              break;
                                                                                                                              
                                                                                                                              case SDLK_DOWN:
                                                                                                                                   
                                                                                                                                   if(MenuState<4)
                                                                                                                                   {
                                                                                                                                                  
                                                                                                                                                  MenuState++;
                                                                                                                                   
                                                                                                                                   }
                                                                                                                                   
                                                                                                                                   break;
                                                                                                                                   
                                                                                                                              case SDLK_RETURN:
                                                                                                                                   
                                                                                                                                   switch(MenuState)
                                                                                                                                   {
                                                                                                                                                    case 1:
                                                                                                                                                         
                                                                                                                                                         GameState=2; // Single player was chosen
                                                                                                                                                         
                                                                                                                                                         break;
                                                                                                                                                         
                                                                                                                                                    case 2: 
                                                                                                                                                    
                                                                                                                                                         GameState=3; // Two player was chosen
                                                                                                                                                    
                                                                                                                                                         break;
                                                                                                                                                         
                                                                                                                                                    case 3:
                                                                                                                                                         
                                                                                                                                                         GameState=4; // Instructions were chosen
                                                                                                                                                         
                                                                                                                                                         break;
                                                                                                                                                         
                                                                                                                                                    case 4:
                                                                                                                                                         
                                                                                                                                                         Running=false;
                                                                                                                                                         
                                                                                                                                                         CloseDown();
                                                                                                                                                         
                                                                                                                                                         break;
                                                                                                                                                         
                                                                                                                                   }
                                                                                                                                   
                                                                                                                                   break;
                                                                                        }
                                                                    
                                                                    break;                    
                                                                                        
                                                                                                                              
                                                            }
                      //break;                                                                
                           }
                           
                      break;
       
                      case 2: // The single player game is active
                      
                      break;
                      
                      case 3: // The two player game is active
                      
                      break;
                      
                      case 4: // The instructions are active 
                      
                      break;
                      
      }
      
}                
                      
/* End HandleEvents */

The main problem is when you press enter. The GameState should change, the switch statement break, and return to the main gameloop function. This should mean the switch statement there selects case 2, which clears the screen and draws the Tic Tac Toe board to the screen. However, what happens is... nothing. The menu still functions as normal, and quit works, but none of the upper 3 options actually do anything - they're programmed, as you can see, to give some kind of feedback to show they've been called, but they don't. You can still move the menu "selector" up and down to highlight each line in turn. If I hardcode GameState manually to be 2, when I load the game it immediately blits the game board to the screen. The same effect should be observed when I select "Single Player" on the main menu and press enter, but it's not working. The problem, I think, lies somewhere in one of the switch statements. It's weird, I can't really see any possible reason for it. A fresh pair of eyes to look over this would be greatly appreciated. Thanks for your time, ukdeveloper.

Share this post


Link to post
Share on other sites
Advertisement
Inside HandleEvents, GameState is a local variable, created as it is a formal parameter. Changing it there will have absolutely no effect on the global that is also called GameState, and therefore will have no effect on your game loop.

If you want to access the global instead, simply remove the relevant parameter from the definition of HandleEvents.

PS: What is going on with your indentation there?

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!