- removed a few menu variables
authorIngo Ruhnke <grumbel@gmx.de>
Mon, 19 Apr 2004 19:06:45 +0000 (19:06 +0000)
committerIngo Ruhnke <grumbel@gmx.de>
Mon, 19 Apr 2004 19:06:45 +0000 (19:06 +0000)
SVN-Revision: 578

src/defines.h
src/gameloop.cpp
src/gameloop.h
src/high_scores.cpp
src/leveleditor.cpp
src/menu.cpp
src/menu.h
src/setup.cpp
src/title.cpp
src/worldmap.cpp
src/worldmap.h

index 2b053c3..b218e33 100644 (file)
@@ -29,6 +29,7 @@
 
 #define JOY_A 0
 #define JOY_B 1
+#define JOY_START 9
 
 #define JOY_X 0
 #define JOY_Y 1
index 5e86022..4b8f577 100644 (file)
@@ -129,6 +129,28 @@ GameSession::start_timers()
 }
 
 void
+GameSession::on_escape_press()
+{
+  if(!game_pause)
+    {
+      if(st_gl_mode == ST_GL_TEST)
+        {
+          exit_status = LEVEL_ABORT;
+        }
+      else if (!Menu::current())
+        {
+          Menu::set_current(game_menu);
+          st_pause_ticks_stop();
+        }
+      else
+        {
+          Menu::set_current(NULL);
+          st_pause_ticks_start();
+        }
+    }
+}
+
+void
 GameSession::process_events()
 {
   Player& tux = *world->get_tux();
@@ -137,7 +159,8 @@ GameSession::process_events()
   while (SDL_PollEvent(&event))
     {
       /* Check for menu-events, if the menu is shown */
-      current_menu->event(event);
+      if (Menu::current())
+        Menu::current()->event(event);
 
       switch(event.type)
         {
@@ -155,25 +178,7 @@ GameSession::process_events()
             switch(key)
               {
               case SDLK_ESCAPE:    /* Escape: Open/Close the menu: */
-                if(!game_pause)
-                  {
-                    if(st_gl_mode == ST_GL_TEST)
-                      {
-                        exit_status = LEVEL_ABORT;
-                      }
-                    else if(!show_menu)
-                      {
-                        Menu::set_current(game_menu);
-                        show_menu = 0;
-                        st_pause_ticks_stop();
-                      }
-                    else
-                      {
-                        Menu::set_current(game_menu);
-                        show_menu = 1;
-                        st_pause_ticks_start();
-                      }
-                  }
+                on_escape_press();
                 break;
               default:
                 break;
@@ -190,7 +195,7 @@ GameSession::process_events()
             switch(key)
               {
               case SDLK_p:
-                if(!show_menu)
+                if(!Menu::current())
                   {
                     if(game_pause)
                       {
@@ -285,13 +290,14 @@ GameSession::process_events()
             tux.input.up = DOWN;
           else if (event.jbutton.button == JOY_B)
             tux.input.fire = DOWN;
+          else if (event.jbutton.button == JOY_START)
+            on_escape_press();
           break;
         case SDL_JOYBUTTONUP:
           if (event.jbutton.button == JOY_A)
             tux.input.up = UP;
           else if (event.jbutton.button == JOY_B)
             tux.input.fire = UP;
-           
           break;
 
         default:
@@ -373,7 +379,7 @@ GameSession::draw()
       blue_text->drawf("PAUSE - Press 'P' To Play", 0, 230, A_HMIDDLE, A_TOP, 1);
     }
 
-  if(show_menu)
+  if(Menu::current())
     {
       menu_process_current();
       mouse_cursor->draw();
@@ -430,9 +436,9 @@ GameSession::run()
 
       process_events();
 
-      if(show_menu)
+      if(Menu::current())
         {
-          if(current_menu == game_menu)
+          if(Menu::current() == game_menu)
             {
               switch (game_menu->check())
                 {
@@ -445,18 +451,18 @@ GameSession::run()
                   break;
                 }
             }
-          else if(current_menu == options_menu)
+          else if(Menu::current() == options_menu)
             {
               process_options_menu();
             }
-          else if(current_menu == load_game_menu )
+          else if(Menu::current() == load_game_menu )
             {
               process_load_game_menu();
             }
         }
       
       // Handle actions:
-      if(!game_pause && !show_menu)
+      if(!game_pause && !Menu::current())
         {
           frame_ratio *= game_speed;
           frame_ratio += overlap;
@@ -490,7 +496,7 @@ GameSession::run()
       draw();
 
       /* Time stops in pause mode */
-      if(game_pause || show_menu )
+      if(game_pause || Menu::current())
         {
           continue;
         }
index 89c38e1..1978de2 100644 (file)
@@ -85,6 +85,9 @@ class GameSession
   void drawstatus();
   void drawendscreen();
   void drawresultscreen(void);
+
+ private:
+  void on_escape_press();
 };
 
 std::string slotinfo(int slot);
index 1849ff2..d50aea0 100644 (file)
@@ -77,7 +77,6 @@ void save_hs(int score)
 
   hs_score = score;
 
-  menu_reset();
   Menu::set_current(highscore_menu);
 
   if(!highscore_menu->item[0].input)
@@ -86,8 +85,7 @@ void save_hs(int score)
   strcpy(highscore_menu->item[0].input,hs_name.c_str());
 
   /* ask for player's name */
-  show_menu = 1;
-  while(show_menu)
+  while(Menu::current())
     {
       bkgd->draw_bg();
 
@@ -101,7 +99,7 @@ void save_hs(int score)
 
       while(SDL_PollEvent(&event))
         if(event.type == SDL_KEYDOWN)
-          current_menu->event(event);
+          Menu::current()->event(event);
 
       switch (highscore_menu->check())
         {
index db70458..ab50b2e 100644 (file)
@@ -171,7 +171,7 @@ int leveleditor(int levelnb)
   updatescreen();
 
   while (SDL_PollEvent(&event))
-  {}
+    {}
 
   while(true)
     {
@@ -180,7 +180,7 @@ int leveleditor(int levelnb)
 
       le_checkevents();
 
-      if(current_menu == select_tilegroup_menu)
+      if(Menu::current() == select_tilegroup_menu)
         {
           if(select_tilegroup_menu_effect.check())
             {
@@ -208,15 +208,15 @@ int leveleditor(int levelnb)
       /* draw editor interface */
       le_drawinterface();
 
-      if(show_menu)
+      if(Menu::current())
         {
           menu_process_current();
-          if(current_menu == leveleditor_menu)
+          if(Menu::current() == leveleditor_menu)
             {
               switch (leveleditor_menu->check())
                 {
                 case 2:
-                  show_menu = false;
+                  Menu::set_current(0);
                   break;
                 case 3:
                   update_subset_settings_menu();
@@ -226,7 +226,7 @@ int leveleditor(int levelnb)
                   break;
                 }
             }
-          else if(current_menu == level_settings_menu)
+          else if(Menu::current() == level_settings_menu)
             {
               switch (level_settings_menu->check())
                 {
@@ -234,28 +234,29 @@ int leveleditor(int levelnb)
                   apply_level_settings_menu();
                   Menu::set_current(leveleditor_menu);
                   break;
+                  
                 default:
-                  show_menu = true;
+                  //show_menu = true;
                   break;
                 }
             }
-          else if(current_menu == select_tilegroup_menu)
+          else if(Menu::current() == select_tilegroup_menu)
             {
-           int it = -1;
+              int it = -1;
               switch (it = select_tilegroup_menu->check())
                 {
                 default:
                  if(it != -1)
-                 {
-                 if(select_tilegroup_menu->item[it].kind == MN_ACTION)
-                 cur_tilegroup = select_tilegroup_menu->item[it].text;
+                    {
+                      if(select_tilegroup_menu->item[it].kind == MN_ACTION)
+                        cur_tilegroup = select_tilegroup_menu->item[it].text;
                  
-                  show_menu = false;
-                 }
+                      Menu::set_current(0);
+                    }
                   break;
                 }
             }
-          else if(current_menu == subset_load_menu)
+          else if(Menu::current() == subset_load_menu)
             {
               switch (i = subset_load_menu->check())
                 {
@@ -277,12 +278,14 @@ int leveleditor(int levelnb)
                       le_set_defaults();
                       le_current_level->load_gfx();
                      le_world.activate_bad_guys();
-                      show_menu = true;
+
+                      // FIXME:?
+                      Menu::set_current(leveleditor_menu);
                     }
                   break;
                 }
             }
-          else if(current_menu == subset_new_menu)
+          else if(Menu::current() == subset_new_menu)
             {
               if(subset_new_menu->item[2].input[0] == '\0')
                 subset_new_menu->item[3].kind = MN_DEACTIVE;
@@ -308,12 +311,13 @@ int leveleditor(int levelnb)
                       le_current_level->load_gfx();
                      le_world.activate_bad_guys();
                       subset_new_menu->item[2].change_input("");
-                      show_menu = true;
+                      // FIXME:? show_menu = true;
+                      Menu::set_current(leveleditor_menu);
                       break;
                     }
                 }
             }
-          else if(current_menu == subset_settings_menu)
+          else if(Menu::current() == subset_settings_menu)
             {
               if(le_level_subset.title.compare(subset_settings_menu->item[2].input) == 0 && le_level_subset.description.compare(subset_settings_menu->item[3].input) == 0  )
                 subset_settings_menu->item[5].kind = MN_DEACTIVE;
@@ -324,7 +328,8 @@ int leveleditor(int levelnb)
                 {
                 case 5:
                   save_subset_settings_menu();
-                  show_menu = true;
+                  //FIXME:show_menu = true;
+                  Menu::set_current(leveleditor_menu);
                   break;
                 }
             }
@@ -414,10 +419,8 @@ int le_init()
   leveleditor_menu->additem(MN_HL,"",0,0);
   leveleditor_menu->additem(MN_ACTION,"Quit Level Editor",0,0);
 
-  menu_reset();
   Menu::set_current(leveleditor_menu);
-  show_menu = true;
-
+  
   subset_load_menu->additem(MN_LABEL, "Load Level Subset", 0, 0);
   subset_load_menu->additem(MN_HL, "", 0, 0);
 
@@ -699,7 +702,7 @@ void le_drawinterface()
     }
   else
     {
-      if(show_menu == false)
+      if(!Menu::current())
         white_small_text->draw("No Level Subset loaded - Press ESC and choose one in the menu", 10, 430, 1);
       else
         white_small_text->draw("No Level Subset loaded", 10, 430, 1);
@@ -786,149 +789,157 @@ void le_checkevents()
 
   while(SDL_PollEvent(&event))
     {
-      current_menu->event(event);
-      if(!show_menu)
-        mouse_cursor->set_state(MC_NORMAL);
-
-      /* testing SDL_KEYDOWN, SDL_KEYUP and SDL_QUIT events*/
-      if(event.type == SDL_KEYDOWN || ((event.type == SDL_MOUSEBUTTONDOWN || SDL_MOUSEMOTION) && (event.motion.x > 0 && event.motion.x < screen->w - 64 &&
-                                       event.motion.y > 0 && event.motion.y < screen->h)))
+      if (Menu::current())
+        {
+          Menu::current()->event(event);
+        }
+      else
         {
-          switch(event.type)
+          mouse_cursor->set_state(MC_NORMAL);
+
+          /* testing SDL_KEYDOWN, SDL_KEYUP and SDL_QUIT events*/
+          if(event.type == SDL_KEYDOWN 
+             || ((event.type == SDL_MOUSEBUTTONDOWN || SDL_MOUSEMOTION)
+                 && (event.motion.x > 0 
+                     && event.motion.x < screen->w - 64 &&
+                     event.motion.y > 0 && event.motion.y < screen->h)))
             {
-            case SDL_QUIT:     // window closed
-printf("window closed\n");
-              done = -1;
-              break;
-            case SDL_KEYDOWN:  // key pressed
-              key = event.key.keysym.sym;
-              switch(key)
+              switch(event.type)
                 {
-                case SDLK_LEFT:
-                  if(fire == DOWN)
-                    cursor_x -= KEY_CURSOR_SPEED;
-                  else
-                    cursor_x -= KEY_CURSOR_FASTSPEED;
+                case SDL_KEYDOWN:      // key pressed
+                  key = event.key.keysym.sym;
+                  switch(key)
+                    {
+                    case SDLK_LEFT:
+                      if(fire == DOWN)
+                        cursor_x -= KEY_CURSOR_SPEED;
+                      else
+                        cursor_x -= KEY_CURSOR_FASTSPEED;
 
-                  if(cursor_x < pos_x + MOUSE_LEFT_MARGIN)
-                    pos_x = cursor_x - MOUSE_LEFT_MARGIN;
+                      if(cursor_x < pos_x + MOUSE_LEFT_MARGIN)
+                        pos_x = cursor_x - MOUSE_LEFT_MARGIN;
 
-                  break;
-                case SDLK_RIGHT:
-                  if(fire == DOWN)
-                    cursor_x += KEY_CURSOR_SPEED;
-                  else
-                    cursor_x += KEY_CURSOR_FASTSPEED;
+                      break;
+                    case SDLK_RIGHT:
+                      if(fire == DOWN)
+                        cursor_x += KEY_CURSOR_SPEED;
+                      else
+                        cursor_x += KEY_CURSOR_FASTSPEED;
 
-                  if(cursor_x > pos_x + MOUSE_RIGHT_MARGIN-32)
-                    pos_x = cursor_x - MOUSE_RIGHT_MARGIN+32;
+                      if(cursor_x > pos_x + MOUSE_RIGHT_MARGIN-32)
+                        pos_x = cursor_x - MOUSE_RIGHT_MARGIN+32;
 
+                      break;
+                    case SDLK_UP:
+                      if(fire == DOWN)
+                        cursor_y -= KEY_CURSOR_SPEED;
+                      else
+                        cursor_y -= KEY_CURSOR_FASTSPEED;
+
+                      if(cursor_y < 0)
+                        cursor_y = 0;
+                      break;
+                    case SDLK_DOWN:
+                      if(fire == DOWN)
+                        cursor_y += KEY_CURSOR_SPEED;
+                      else
+                        cursor_y += KEY_CURSOR_FASTSPEED;
+
+                      if(cursor_y > screen->h-32)
+                        cursor_y = screen->h-32;
+                      break;
+                    case SDLK_LCTRL:
+                      fire =UP;
+                      break;
+                    case SDLK_F1:
+                      le_showhelp();
+                      break;
+                    case SDLK_HOME:
+                      cursor_x = 0;
+                      pos_x = cursor_x;
+                      break;
+                    case SDLK_END:
+                      cursor_x = (le_current_level->width * 32) - 32;
+                      pos_x = cursor_x;
+                      break;
+                    case SDLK_F9:
+                      le_show_grid = !le_show_grid;
+                      break;
+                    default:
+                      break;
+                    }
                   break;
-                case SDLK_UP:
-                  if(fire == DOWN)
-                    cursor_y -= KEY_CURSOR_SPEED;
-                  else
-                    cursor_y -= KEY_CURSOR_FASTSPEED;
-
-                  if(cursor_y < 0)
-                    cursor_y = 0;
-                  break;
-                case SDLK_DOWN:
-                  if(fire == DOWN)
-                    cursor_y += KEY_CURSOR_SPEED;
-                  else
-                    cursor_y += KEY_CURSOR_FASTSPEED;
-
-                  if(cursor_y > screen->h-32)
-                    cursor_y = screen->h-32;
-                  break;
-                case SDLK_LCTRL:
-                  fire =UP;
-                  break;
-                case SDLK_F1:
-                  le_showhelp();
-                  break;
-                case SDLK_HOME:
-                  cursor_x = 0;
-                  pos_x = cursor_x;
-                  break;
-                case SDLK_END:
-                  cursor_x = (le_current_level->width * 32) - 32;
-                  pos_x = cursor_x;
-                  break;
-                case SDLK_F9:
-                  le_show_grid = !le_show_grid;
-                  break;
-                default:
+                case SDL_KEYUP:        /* key released */
+                  switch(event.key.keysym.sym)
+                    {
+                    case SDLK_LCTRL:
+                      fire = DOWN;
+                      break;
+                    default:
+                      break;
+                    }
                   break;
-                }
-              break;
-            case SDL_KEYUP:    /* key released */
-              switch(event.key.keysym.sym)
-                {
-                case SDLK_LCTRL:
-                  fire = DOWN;
+                case SDL_MOUSEBUTTONDOWN:
+                  if(event.button.button == SDL_BUTTON_LEFT)
+                    {
+                      le_mouse_pressed[LEFT] = true;
+
+                      selection.x1 = event.motion.x + pos_x;
+                      selection.y1 = event.motion.y;
+                      selection.x2 = event.motion.x + pos_x;
+                      selection.y2 = event.motion.y;
+                    }
+                  else if(event.button.button == SDL_BUTTON_RIGHT)
+                    {
+                      le_mouse_pressed[RIGHT] = true;
+                    }
                   break;
-                default:
+                case SDL_MOUSEBUTTONUP:
+                  if(event.button.button == SDL_BUTTON_LEFT)
+                    le_mouse_pressed[LEFT] = false;
+                  else if(event.button.button == SDL_BUTTON_RIGHT)
+                    le_mouse_pressed[RIGHT] = false;
                   break;
-                }
-              break;
-            case SDL_MOUSEBUTTONDOWN:
-              if(event.button.button == SDL_BUTTON_LEFT)
-                {
-                  le_mouse_pressed[LEFT] = true;
+                case SDL_MOUSEMOTION:
 
-                  selection.x1 = event.motion.x + pos_x;
-                  selection.y1 = event.motion.y;
-                  selection.x2 = event.motion.x + pos_x;
-                  selection.y2 = event.motion.y;
-                }
-              else if(event.button.button == SDL_BUTTON_RIGHT)
-               {
-                le_mouse_pressed[RIGHT] = true;
-               }
-              break;
-            case SDL_MOUSEBUTTONUP:
-              if(event.button.button == SDL_BUTTON_LEFT)
-                le_mouse_pressed[LEFT] = false;
-              else if(event.button.button == SDL_BUTTON_RIGHT)
-                le_mouse_pressed[RIGHT] = false;
-              break;
-            case SDL_MOUSEMOTION:
-              if(!show_menu)
-                {
-                  x = event.motion.x;
-                  y = event.motion.y;
+                  if(!Menu::current())
+                    {
+                      x = event.motion.x;
+                      y = event.motion.y;
 
-                  cursor_x = ((int)(pos_x + x) / 32) * 32;
-                  cursor_y = ((int) y / 32) * 32;
+                      cursor_x = ((int)(pos_x + x) / 32) * 32;
+                      cursor_y = ((int) y / 32) * 32;
 
-                  if(le_mouse_pressed[LEFT])
-                    {
-                      selection.x2 = x + pos_x;
-                      selection.y2 = y;
-                    }
+                      if(le_mouse_pressed[LEFT])
+                        {
+                          selection.x2 = x + pos_x;
+                          selection.y2 = y;
+                        }
 
-                  if(le_mouse_pressed[RIGHT])
-                    {
-                      pos_x += -1 * event.motion.xrel;
+                      if(le_mouse_pressed[RIGHT])
+                        {
+                          pos_x += -1 * event.motion.xrel;
+                        }
                     }
+                  break;
+                case SDL_QUIT: // window closed
+                  done = 1;
+                  break;
+                default:
+                  break;
                 }
-              break;
-            default:
-              break;
             }
         }
 
       if(le_current_level != NULL)
         {
           if(event.type == SDL_KEYDOWN || event.type == SDL_KEYUP || ((event.type == SDL_MOUSEBUTTONDOWN || SDL_MOUSEMOTION) && (event.motion.x > screen->w-64 && event.motion.x < screen->w &&
-              event.motion.y > 0 && event.motion.y < screen->h)))
+                                                                                                                                 event.motion.y > 0 && event.motion.y < screen->h)))
             {
               le_mouse_pressed[LEFT] = false;
               le_mouse_pressed[RIGHT] = false;
 
-              if(show_menu == false)
+              if(!Menu::current())
                 {
                   /* Check for button events */
                   le_test_level_bt->event(event);
@@ -939,10 +950,9 @@ printf("window closed\n");
                     le_current_level->save(le_level_subset.name.c_str(),le_level);
                   le_exit_bt->event(event);
                   if(le_exit_bt->get_state() == BUTTON_CLICKED)
-                 {
-                   Menu::set_current(leveleditor_menu);
-                   show_menu = true;
-                   }
+                    {
+                      Menu::set_current(leveleditor_menu);
+                    }
                   le_next_level_bt->event(event);
                   if(le_next_level_bt->get_state() == BUTTON_CLICKED)
                     {
@@ -1011,7 +1021,6 @@ printf("window closed\n");
                       Menu::set_current(select_tilegroup_menu);
                       select_tilegroup_menu_effect.start(200);
                       select_tilegroup_menu->set_pos(screen->w - 64,100,-0.5,0.5);
-                      show_menu = true;
                     }
 
                   le_settings_bt->event(event);
@@ -1019,41 +1028,39 @@ printf("window closed\n");
                     {
                       update_level_settings_menu();
                       Menu::set_current(level_settings_menu);
-                      show_menu = true;
                     }
                  if(!cur_tilegroup.empty())
-                 if((pbutton = tilegroups_map[cur_tilegroup]->event(event)) != NULL)
-                 {
-                   if(pbutton->get_state() == BUTTON_CLICKED)
-                     {
-                     le_current_tile = pbutton->get_tag();
-                     }
-                 }
+                    if((pbutton = tilegroups_map[cur_tilegroup]->event(event)) != NULL)
+                      {
+                        if(pbutton->get_state() == BUTTON_CLICKED)
+                          {
+                            le_current_tile = pbutton->get_tag();
+                          }
+                      }
                  if((pbutton = le_tilemap_panel->event(event)) != NULL)
-                 {
-                   if(pbutton->get_state() == BUTTON_CLICKED)
-                     {
-                     active_tm = static_cast<TileMapType>(pbutton->get_tag());
-                     }
-                 }
+                    {
+                      if(pbutton->get_state() == BUTTON_CLICKED)
+                        {
+                          active_tm = static_cast<TileMapType>(pbutton->get_tag());
+                        }
+                    }
                 }
               else
                 {
                   le_settings_bt->event(event);
                   if(le_settings_bt->get_state() == BUTTON_CLICKED)
                     {
-                      Menu::set_current(leveleditor_menu);
-                      show_menu = false;
+                      Menu::set_current(0);
                     }
                   le_tilegroup_bt->event(event);
                   if(le_tilegroup_bt->get_state() == BUTTON_CLICKED)
                     {
-                      Menu::set_current(leveleditor_menu);
-                      show_menu = false;
+                      Menu::set_current(0);
                     }
                 }
             }
-          if(show_menu == false)
+          
+          if(!Menu::current())
             {
               le_move_left_bt->event(event);
               le_move_right_bt->event(event);
@@ -1065,7 +1072,7 @@ printf("window closed\n");
             }
         }
     }
-  if(show_menu == false)
+  if(!Menu::current())
     {
       if(le_move_left_bt->get_state() == BUTTON_PRESSED)
         {
index 896591b..d05f510 100644 (file)
 #include "timer.h"
 #include "high_scores.h"
 
-/* (global) menu variables */
-MenuAction menuaction = MENU_ACTION_NONE;
-bool show_menu;
-bool menu_change;
-
 Surface* checkbox;
 Surface* checkbox_checked;
 Surface* back;
@@ -52,27 +47,18 @@ Menu* save_game_menu = 0;
 Menu* contrib_menu   = 0;
 Menu* contrib_subset_menu   = 0;
 
-Menu* current_menu = 0;
-
-/* input implementation variables */
-int delete_character;
-char mn_input_char;
+Menu* Menu::current_ = 0;
 
-/* Set the current menu */
 void
-Menu::set_current(Menu* pmenu)
+Menu::set_current(Menu* menu)
 {
-  if(pmenu != current_menu)
+  if (menu)
     {
-      menu_change  = true;
-      Menu* tmp = current_menu;
-      current_menu = pmenu;
-      if(tmp)
-        if(tmp->last_menu != pmenu)
-          current_menu->last_menu = tmp;
-
-      pmenu->effect.start(500);
+      menu->last_menu = current_;
+      menu->effect.start(500);
     }
+  
+  current_ = menu;
 }
 
 /* Return a pointer to a new menu item */
@@ -140,8 +126,13 @@ Menu::~Menu()
     }
 }
 
+
 Menu::Menu()
 {
+  menuaction = MENU_ACTION_NONE;
+  delete_character = 0;
+  mn_input_char = '\0';
+  
   pos_x        = screen->w/2;
   pos_y        = screen->h/2;
   has_backitem = false;
@@ -241,7 +232,6 @@ Menu::action()
 
               case MN_TOGGLE:
                 item[active_item].toggled = !item[active_item].toggled;
-                menu_change = true;
                 break;
 
               case MN_ACTION:
@@ -309,12 +299,14 @@ Menu::action()
       || new_item.kind == MN_HL)
     {
       // Skip the horzontal line item
-      if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
+      if (menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
         menuaction = MENU_ACTION_DOWN;
 
-      if(item.size() > 1)
+      if (item.size() > 1)
         action();
     }
+
+  menuaction = MENU_ACTION_NONE;
 }
 
 int
@@ -328,7 +320,7 @@ Menu::check()
           && item[active_item].toggled)
         {
           item[active_item].toggled = false;
-          show_menu = 0;
+          Menu::set_current(0);
           return active_item;
         }
       else if(item[active_item].kind == MN_TOGGLE || item[active_item].kind == MN_GOTO)
@@ -531,43 +523,21 @@ Menu::draw()
     }
 }
 
-/* Reset/Set global defaults */
-void menu_reset(void)
-{
-  menu_change  = false;
-  show_menu    = false;
-  menuaction   = MENU_ACTION_NONE;
-  current_menu = NULL;
-
-  delete_character = 0;
-  mn_input_char    = '\0';
-}
-
 /* --- MENU --- */
 /* Draw the current menu and execute the (menu)events */
-void menu_process_current(void)
+void menu_process_current()
 {
-  if(!show_menu)
-    return;
-
-  menu_change = false;
-
-  if(current_menu != NULL)
+  if(Menu::current())
     {
-      current_menu->action();
-      current_menu->draw();
+      Menu::current()->action();
+      Menu::current()->draw();
     }
-
-  menuaction = MENU_ACTION_NONE;
 }
 
 /* Check for menu event */
 void
 Menu::event(SDL_Event& event)
 {
-  if(show_menu == false && event.key.keysym.sym != SDLK_ESCAPE)
-    return;
-
   SDLKey key;
   switch(event.type)
     {
@@ -594,50 +564,43 @@ Menu::event(SDL_Event& event)
         {
         case SDLK_UP:          /* Menu Up */
           menuaction = MENU_ACTION_UP;
-          menu_change = true;
           break;
         case SDLK_DOWN:                /* Menu Down */
           menuaction = MENU_ACTION_DOWN;
-          menu_change = true;
           break;
         case SDLK_LEFT:                /* Menu Up */
           menuaction = MENU_ACTION_LEFT;
-          menu_change = true;
           break;
         case SDLK_RIGHT:               /* Menu Down */
           menuaction = MENU_ACTION_RIGHT;
-          menu_change = true;
           break;
         case SDLK_SPACE:
           if(item[active_item].kind == MN_TEXTFIELD)
             {
               menuaction = MENU_ACTION_INPUT;
-              menu_change = true;
               mn_input_char = ' ';
               break;
             }
         case SDLK_RETURN: /* Menu Hit */
           menuaction = MENU_ACTION_HIT;
-          menu_change = true;
           break;
         case SDLK_DELETE:
         case SDLK_BACKSPACE:
           menuaction = MENU_ACTION_REMOVE;
-          menu_change = true;
           delete_character++;
           break;
         case SDLK_ESCAPE:
-          if(show_menu && has_backitem == true && last_menu != NULL)
-            Menu::set_current(last_menu);
-          else if(show_menu)
-            show_menu = false;
-          else
-            show_menu = true;
+          if(Menu::current())
+            {
+              if (has_backitem == true && last_menu != NULL)
+                Menu::set_current(last_menu);
+              else
+                Menu::set_current(0);
+            }
         default:
           if( (key >= SDLK_0 && key <= SDLK_9) || (key >= SDLK_a && key <= SDLK_z) || (key >= SDLK_SPACE && key <= SDLK_SLASH))
             {
               menuaction = MENU_ACTION_INPUT;
-              menu_change = true;
               mn_input_char = *ch;
             }
           else
@@ -663,9 +626,9 @@ Menu::event(SDL_Event& event)
       x = event.motion.x;
       y = event.motion.y;
       if(x > pos_x - width()/2 &&
-          x < pos_x + width()/2 &&
-          y > pos_y - height()/2 &&
-          y < pos_y + height()/2)
+         x < pos_x + width()/2 &&
+         y > pos_y - height()/2 &&
+         y < pos_y + height()/2)
         {
           menuaction = MENU_ACTION_HIT;
         }
@@ -674,15 +637,14 @@ Menu::event(SDL_Event& event)
       x = event.motion.x;
       y = event.motion.y;
       if(x > pos_x - width()/2 &&
-          x < pos_x + width()/2 &&
-          y > pos_y - height()/2 &&
-          y < pos_y + height()/2)
+         x < pos_x + width()/2 &&
+         y > pos_y - height()/2 &&
+         y < pos_y + height()/2)
         {
           active_item = (y - (pos_y - height()/2)) / 24;
-          menu_change = true;
-         mouse_cursor->set_state(MC_LINK);
+          mouse_cursor->set_state(MC_LINK);
         }
-       else
+      else
        {
          mouse_cursor->set_state(MC_NORMAL);
        }
index 969b30d..92b1ae1 100644 (file)
@@ -56,22 +56,44 @@ public:
 class Menu
 {
 private:
+  /* Action done on the menu */
+  enum MenuAction {
+    MENU_ACTION_NONE = -1,
+    MENU_ACTION_UP,
+    MENU_ACTION_DOWN,
+    MENU_ACTION_LEFT,
+    MENU_ACTION_RIGHT,
+    MENU_ACTION_HIT,
+    MENU_ACTION_INPUT,
+    MENU_ACTION_REMOVE
+  };
+
   // position of the menu (ie. center of the menu, not top/left)
   int pos_x;
   int pos_y;
   bool has_backitem;
+
+  /** input event for the menu */
+  MenuAction menuaction;
+
+  /* input implementation variables */
+  int delete_character;
+  char mn_input_char;
   
   Menu* last_menu;
   int width();
   int height();
   
+  static Menu* current_;
 public:
   Timer effect;
   int arrange_left;
   int active_item;
   std::vector<MenuItem> item;
 
+  /** Set the current menu, if pmenu is NULL, hide the current menu */
   static void set_current(Menu* pmenu);
+  static Menu* current() { return current_; }
 
   Menu();
   ~Menu();
@@ -89,28 +111,10 @@ public:
   void draw_item(int index, int menu_width, int menu_height);
   void set_pos(int x, int y, float rw = 0, float rh = 0);
 
-  /* Check for a menu event */
+  /** translate a SDL_Event into a menu_action */
   void event(SDL_Event& event);
 };
 
-
-/* Action done on the menu */
-enum MenuAction {
-  MENU_ACTION_NONE = -1,
-  MENU_ACTION_UP,
-  MENU_ACTION_DOWN,
-  MENU_ACTION_LEFT,
-  MENU_ACTION_RIGHT,
-  MENU_ACTION_HIT,
-  MENU_ACTION_INPUT,
-  MENU_ACTION_REMOVE
-};
-
-/* (global) menu variables */
-extern MenuAction menuaction;
-extern bool show_menu;
-extern bool menu_change;
-
 extern Surface* checkbox;
 extern Surface* checkbox_checked;
 extern Surface* back;
@@ -127,14 +131,6 @@ extern Menu* options_controls_menu;
 extern Menu* highscore_menu;
 extern Menu* load_game_menu;
 extern Menu* save_game_menu;
-extern Menu* current_menu;
-
-/* input implementation variables */
-extern int delete_character;
-extern char mn_input_char;
-
-/* Reset the global menu variables */
-void menu_reset(void);
 
 /* "Calculate" and draw the menu */
 void menu_process_current(void);
index 24397e8..5389142 100644 (file)
@@ -468,7 +468,6 @@ bool process_load_game_menu()
 
       worldmap.display();
       
-      show_menu = true;
       Menu::set_current(main_menu);
 
       st_pause_ticks_stop();
index b729bef..138b9ee 100644 (file)
@@ -139,9 +139,7 @@ void check_contrib_subset_menu()
       std::cout << "Sarting level: " << index << std::endl;
       GameSession session(current_contrib_subset, index, ST_GL_PLAY);
       session.run();
-      menu_reset();
       Menu::set_current(main_menu);
-      show_menu = 1;
     }  
 }
 
@@ -207,8 +205,6 @@ void draw_demo(GameSession* session, double frame_ratio)
       scroll_x = tux->base.x - 320;
     }
 
-
-
   float last_tux_x_pos = tux->base.x;
   tux->action(frame_ratio);
 
@@ -232,12 +228,6 @@ bool title(void)
 
   GameSession session(datadir + "/levels/misc/menu.stl", 0, ST_GL_DEMO_GAME);
 
-  //FIXME:activate_particle_systems();
-
-  /* Reset menu variables */
-  menu_reset();
-  Menu::set_current(main_menu);
-
   clearscreen(0, 0, 0);
   updatescreen();
 
@@ -249,7 +239,6 @@ bool title(void)
 
   /* --- Main title loop: --- */
   bool done = 0;
-  show_menu = 1;
   frame = 0;
 
   /* Draw the title background: */
@@ -259,9 +248,10 @@ bool title(void)
   update_time = st_get_ticks();
   random_timer.start(rand() % 2000 + 2000);
 
+  Menu::set_current(main_menu);
   while (!done)
     {
-      /* Calculate the movement-factor */
+      // Calculate the movement-factor
       double frame_ratio = ((double)(update_time-last_update_time))/((double)FRAME_RATE);
       if(frame_ratio > 1.5) /* Quick hack to correct the unprecise CPU clocks a little bit. */
         frame_ratio = 1.5 + (frame_ratio - 1.5) * 0.85;
@@ -272,7 +262,6 @@ bool title(void)
 
       while (SDL_PollEvent(&event))
         {
-          current_menu->event(event);
           if (event.type == SDL_QUIT)
             {
               done = true;
@@ -282,10 +271,12 @@ bool title(void)
               /* Keypress... */
               key = event.key.keysym.sym;
 
-              /* Check for menu events */
-              //menu_event(event);
+              if (Menu::current())
+                {
+                  Menu::current()->event(event);
+                }
 
-              if (!show_menu)
+              if (!Menu::current())
                 {
                   /* Escape: Quit: */
                   done = true;
@@ -297,115 +288,24 @@ bool title(void)
       draw_background();
       draw_demo(&session, frame_ratio);
       
-      if (current_menu == main_menu)
+      if (Menu::current() == main_menu)
         logo->draw( 160, 30);
 
-      white_small_text->draw(
-                " SuperTux " VERSION "\n"
-                "Copyright (c) 2003 SuperTux Devel Team\n"
-                "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
-                "are welcome to redistribute it under certain conditions; see the file COPYING\n"
-                "for details.\n",
-                0, 420, 0);
-
-      /* Draw the high score: */
-      /*
-        sprintf(str, "High score: %d", hs_score);
-        text_drawf(&gold_text, str, 0, -40, A_HMIDDLE, A_BOTTOM, 1);
-        sprintf(str, "by %s", hs_name);
-        text_drawf(&gold_text, str, 0, -20, A_HMIDDLE, A_BOTTOM, 1);
-      */
+      white_small_text->draw(" SuperTux " VERSION "\n"
+                             "Copyright (c) 2003 SuperTux Devel Team\n"
+                             "This game comes with ABSOLUTELY NO WARRANTY. This is free software, and you\n"
+                             "are welcome to redistribute it under certain conditions; see the file COPYING\n"
+                             "for details.\n",
+                             0, 420, 0);
 
       /* Don't draw menu, if quit is true */
-      if(show_menu && !done)
+      if(!done)
         menu_process_current();
 
-      if(current_menu == main_menu)
+      if(Menu::current() == main_menu)
         {
           switch (main_menu->check())
             {
-#if 0
-            case 0:
-              string_list_type level_subsets;
-              level_subsets = dsubdirs("/levels", "info");
-
-              // Quick Play
-              // FIXME: obsolete
-              done = 0;
-              i = 0;
-              if(level_subsets.num_items != 0)
-                {
-                  subset.load(level_subsets.item[0]);
-                  while(!done)
-                    {
-                      img_choose_subset->draw((screen->w - img_choose_subset.w) / 2, 0);
-                      if(level_subsets.num_items != 0)
-                        {
-                          subset.image->draw((screen->w - subset.image.w) / 2 + 25,78);
-                          if(level_subsets.num_items > 1)
-                            {
-                              if(i > 0)
-                                arrow_left->draw((screen->w / 2) - ((subset.title.length()+2)*16)/2,20);
-                              if(i < level_subsets.num_items-1)
-                                arrow_right->draw((screen->w / 2) + ((subset.description.length())*16)/2,20);
-                            }
-                          text_drawf(&gold_text, subset.title.c_str(), 0, 20, A_HMIDDLE, A_TOP, 1);
-                          text_drawf(&gold_text, subset.description.c_str(), 20, -20, A_HMIDDLE, A_BOTTOM, 1);
-                        }
-                      updatescreen();
-                      SDL_Delay(50);
-                      while(SDL_PollEvent(&event) && !done)
-                        {
-                          switch(event.type)
-                            {
-                            case SDL_QUIT:
-                              done = true;
-                              break;
-                            case SDL_KEYDOWN:          // key pressed
-                              // Keypress...
-                              key = event.key.keysym.sym;
-
-                              if(key == SDLK_LEFT)
-                                {
-                                  if(i > 0)
-                                    {
-                                      --i;
-                                      subset.free();
-                                      subset.load(level_subsets.item[i]);
-                                    }
-                                }
-                              else if(key == SDLK_RIGHT)
-                                {
-                                  if(i < level_subsets.num_items -1)
-                                    {
-                                      ++i;
-                                      subset.free();
-                                      subset.load(level_subsets.item[i]);
-                                    }
-                                }
-                              else if(key == SDLK_SPACE || key == SDLK_RETURN)
-                                {
-                                  done = true;
-                                  quit = gameloop(subset.name.c_str(),1,ST_GL_PLAY);
-                                  subset.free();
-                                }
-                              else if(key == SDLK_ESCAPE)
-                                {
-                                  done = true;
-                                }
-                              break;
-                            default:
-                              break;
-                            }
-                        }
-                    }
-                }
-              // reset tux
-              scroll_x = 0;
-              titletux.level_begin();
-              update_time = st_get_ticks();
-              break;
-#endif
             case 0:
               // Start Game, ie. goto the slots menu
               update_load_save_game_menu(load_game_menu);
@@ -417,8 +317,6 @@ bool title(void)
             case 3:
               done = true;
               done = leveleditor(1);
-              menu_reset();
-              show_menu = 1;
               Menu::set_current(main_menu);
               break;
             case 4:
@@ -429,11 +327,11 @@ bool title(void)
               break;
             }
         }
-      else if(current_menu == options_menu)
+      else if(Menu::current() == options_menu)
         {
           process_options_menu();
         }
-      else if(current_menu == load_game_menu)
+      else if(Menu::current() == load_game_menu)
         {
           if (process_load_game_menu())
             {
@@ -444,11 +342,11 @@ bool title(void)
               update_time = st_get_ticks();
             }
         }
-      else if(current_menu == contrib_menu)
+      else if(Menu::current() == contrib_menu)
         {
           check_contrib_menu();
         }
-      else if (current_menu == contrib_subset_menu)
+      else if (Menu::current() == contrib_subset_menu)
         {
           check_contrib_subset_menu();
         }
@@ -605,6 +503,5 @@ void display_credits()
   string_list_free(&names);
 
   SDL_EnableKeyRepeat(0, 0);    // disables key repeating
-  show_menu = 1;
   Menu::set_current(main_menu);
 }
index ebd052c..e263912 100644 (file)
@@ -362,6 +362,17 @@ WorldMap::load_map()
 }
 
 void
+WorldMap::on_escape_press()
+{
+  std::cout << "on escape press" << std::endl;
+
+  if(!Menu::current())
+    Menu::set_current(worldmap_menu); 
+  else
+    Menu::set_current(0); 
+}
+
+void
 WorldMap::get_input()
 {
   enter_level = false;
@@ -370,11 +381,11 @@ WorldMap::get_input()
   SDL_Event event;
   while (SDL_PollEvent(&event))
     {
-      if(!show_menu && event.key.keysym.sym == SDLK_ESCAPE)
-        Menu::set_current(worldmap_menu);
-
-      current_menu->event(event);
-      if(!show_menu)
+      if (Menu::current())
+        {
+          Menu::current()->event(event);
+        }
+      else
         {
           switch(event.type)
             {
@@ -385,6 +396,9 @@ WorldMap::get_input()
             case SDL_KEYDOWN:
               switch(event.key.keysym.sym)
                 {
+                case SDLK_ESCAPE:
+                  on_escape_press();
+                  break;
                 case SDLK_LCTRL:
                 case SDLK_RETURN:
                   enter_level = true;
@@ -415,6 +429,8 @@ WorldMap::get_input()
             case SDL_JOYBUTTONDOWN:
               if (event.jbutton.button == JOY_B)
                 enter_level = true;
+              else if (event.jbutton.button == JOY_START)
+                on_escape_press();
               break;
 
             default:
@@ -423,7 +439,7 @@ WorldMap::get_input()
         }
     }
 
-  if (!show_menu)
+  if (!Menu::current())
     {
       Uint8 *keystate = SDL_GetKeyState(NULL);
   
@@ -534,8 +550,7 @@ WorldMap::update()
                 }
 
               play_music(song, 1);
-              show_menu = 0;
-              menu_reset();
+              Menu::set_current(0);
               if (!savegame_file.empty())
                 savegame(savegame_file);
               return;
@@ -553,14 +568,13 @@ WorldMap::update()
       tux->update(0.33f);
     }
   
-  if(show_menu)
+  if(Menu::current())
     {
-      if(current_menu == worldmap_menu)
+      if(Menu::current() == worldmap_menu)
         {
           switch (worldmap_menu->check())
             {
             case 2: // Return to game
-              menu_reset();
               break;
             case 3:
               if (!savegame_file.empty())
@@ -667,7 +681,7 @@ WorldMap::draw_status()
 void
 WorldMap::display()
 {
-  show_menu = false;
+  Menu::set_current(0);
 
   quit = false;
 
@@ -692,7 +706,7 @@ WorldMap::display()
     get_input();
     update();
 
-  if(show_menu)
+  if(Menu::current())
     {
       menu_process_current();
       mouse_cursor->draw();
index 343ef61..46d37d1 100644 (file)
@@ -188,6 +188,8 @@ public:
 
   void savegame(const std::string& filename);
   void loadgame(const std::string& filename);
+private:
+  void on_escape_press();
 };
 
 } // namespace WorldMapNS