Included supertux.h stuff into it.
[supertux.git] / src / menu.cpp
index 893962d..c70b699 100644 (file)
@@ -43,6 +43,7 @@ Menu* options_controls_menu   = 0;
 Menu* highscore_menu = 0;
 Menu* load_game_menu = 0;
 Menu* save_game_menu = 0;
+Menu* contrib_menu   = 0;
 
 Menu* current_menu = 0;
 
@@ -63,25 +64,30 @@ Menu::set_current(Menu* pmenu)
         if(tmp->last_menu != pmenu)
           current_menu->last_menu = tmp;
 
-      timer_start(&pmenu->effect, 500);
+      pmenu->effect.start(500);
     }
 }
 
 /* Return a pointer to a new menu item */
-menu_item_type* menu_item_create(MenuItemKind kind, char *text, int init_toggle, Menu* target_menu)
+MenuItem*
+MenuItem::create(MenuItemKind kind_, char *text_, int init_toggle_, Menu* target_menu_)
 {
-  menu_item_type *pnew_item = (menu_item_type*) malloc(sizeof(menu_item_type));
-  pnew_item->kind = kind;
-  pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text) + 1));
-  strcpy(pnew_item->text,text);
-  if(kind == MN_TOGGLE)
-    pnew_item->toggled = init_toggle;
+  MenuItem *pnew_item = new MenuItem;
+  
+  pnew_item->kind = kind_;
+  pnew_item->text = (char*) malloc(sizeof(char) * (strlen(text_) + 1));
+  strcpy(pnew_item->text, text_);
+
+  if(kind_ == MN_TOGGLE)
+    pnew_item->toggled = init_toggle_;
   else
     pnew_item->toggled = false;
-  pnew_item->target_menu = target_menu;
+
+  pnew_item->target_menu = target_menu_;
   pnew_item->input = (char*) malloc(sizeof(char));
   pnew_item->input[0] = '\0';
-  if(kind == MN_STRINGSELECT)
+
+  if(kind_ == MN_STRINGSELECT)
     {
       pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type));
       string_list_init(pnew_item->list);
@@ -91,37 +97,39 @@ menu_item_type* menu_item_create(MenuItemKind kind, char *text, int init_toggle,
   return pnew_item;
 }
 
-void menu_item_change_text(menu_item_type* pmenu_item,const  char *text)
+void
+MenuItem::change_text(const  char *text_)
 {
-  if(text)
+  if (text_)
     {
-      free(pmenu_item->text);
-      pmenu_item->text = (char*) malloc(sizeof(char )*(strlen(text)+1));
-      strcpy(pmenu_item->text,text);
+      free(text);
+      text = (char*) malloc(sizeof(char )*(strlen(text_)+1));
+      strcpy(text, text_);
     }
 }
-void menu_item_change_input(menu_item_type* pmenu_item,const  char *text)
+
+void
+MenuItem::change_input(const  char *text_)
 {
   if(text)
     {
-      free(pmenu_item->input);
-      pmenu_item->input = (char*) malloc(sizeof(char )*(strlen(text)+1));
-      strcpy(pmenu_item->input,text);
+      free(input);
+      input = (char*) malloc(sizeof(char )*(strlen(text_)+1));
+      strcpy(input, text_);
     }
 }
 
 /* Free a menu and all its items */
 Menu::~Menu()
 {
-  if(num_items != 0 && item != NULL)
+  if(item.size() != 0)
     {
-      for(int i = 0; i < num_items; ++i)
+      for(unsigned int i = 0; i < item.size(); ++i)
         {
           free(item[i].text);
           free(item[i].input);
           string_list_free(item[i].list);
         }
-      free(item);
     }
 }
 
@@ -131,40 +139,36 @@ Menu::Menu()
   pos_y        = screen->h/2;
   last_menu    = 0;
   arrange_left = 0;
-  num_items    = 0;
   active_item  = 0;
   last_menu    = 0;
-  item         = NULL;
-  timer_init(&effect,false);
+  effect.init(false);
 }
 
 void Menu::set_pos(int x, int y, float rw, float rh)
 {
- pos_x = x + (int)((float)width() * rw);
- pos_y = y + (int)((float)height() * rh);
 pos_x = x + (int)((float)width() * rw);
 pos_y = y + (int)((float)height() * rh);
 }
 
 void
-Menu::additem(MenuItemKind kind, char *text, int toggle, Menu* menu)
+Menu::additem(MenuItemKind kind_, char *text_, int toggle_, Menu* menu_)
 {
-  additem(menu_item_create(kind, text, toggle, menu));
+  additem(MenuItem::create(kind_, text_, toggle_, menu_));
 }
 
 /* Add an item to a menu */
 void
-Menu::additem(menu_item_type* pmenu_item)
+Menu::additem(MenuItem* pmenu_item)
 {
-  ++num_items;
-  item = (menu_item_type*)realloc(item, sizeof(menu_item_type) * num_items);
-  memcpy(&item[num_items-1],pmenu_item,sizeof(menu_item_type));
-  free(pmenu_item);
+  item.push_back(*pmenu_item);
+  delete pmenu_item;
 }
 
 /* Process actions done on the menu */
 void
 Menu::action()
 {
-  if(num_items != 0 && item != NULL)
+  if(item.size() != 0)
     {
       switch(menuaction)
         {
@@ -172,11 +176,11 @@ Menu::action()
           if (active_item > 0)
             --active_item;
           else
-            active_item = num_items-1;
+            active_item = int(item.size())-1;
           break;
 
         case MENU_ACTION_DOWN:
-          if(active_item < num_items-1)
+          if(active_item < int(item.size())-1)
             ++active_item;
           else
             active_item = 0;
@@ -279,7 +283,7 @@ Menu::action()
         }
     }
 
-  menu_item_type& new_item = item[active_item];
+  MenuItem& new_item = item[active_item];
   if(new_item.kind == MN_DEACTIVE
       || new_item.kind == MN_LABEL
       || new_item.kind == MN_HL)
@@ -288,16 +292,15 @@ Menu::action()
       if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN)
         menuaction = MENU_ACTION_DOWN;
 
-      if(num_items > 1)
+      if(item.size() > 1)
         action();
     }
 }
 
-/* Check, if the value of the active menu item has changed. */
 int
 Menu::check()
 {
-  if(num_items != 0 && item != NULL)
+  if (item.size() != 0)
     {
       if((item[active_item].kind == MN_ACTION
           || item[active_item].kind == MN_TEXTFIELD
@@ -324,15 +327,14 @@ Menu::draw_item(int index, // Position of the current item in the menu
                 int menu_width,
                 int menu_height)
 {
-  int font_width  = 16;
-
-  const menu_item_type& pitem =  item[index];
+  const MenuItem& pitem = item[index];
 
+  int font_width  = 16;
   int effect_offset = 0;
   {
     int effect_time = 0;
-    if(timer_check(&effect))
-      effect_time = timer_get_left(&effect) / 4;
+    if(effect.check())
+      effect_time = effect.get_left() / 4;
 
     effect_offset = (index % 2) ? effect_time : -effect_time;
   }
@@ -470,7 +472,7 @@ int Menu::width()
   /* The width of the menu has to be more than the width of the text
      with the most characters */
   int menu_width = 0;
-  for(int i = 0; i < num_items; ++i)
+  for(unsigned int i = 0; i < item.size(); ++i)
     {
       int w = strlen(item[i].text) + (item[i].input ? strlen(item[i].input) + 1 : 0) + strlen(string_list_active(item[i].list));
       if( w > menu_width )
@@ -481,12 +483,12 @@ int Menu::width()
         }
     }
 
-  return (menu_width * 16 + 48);
+  return (menu_width * 16 + 24);
 }
 
 int Menu::height()
 {
-  return ((num_items) * 24);
+  return item.size() * 24;
 }
 
 /* Draw the current menu. */
@@ -498,11 +500,11 @@ Menu::draw()
 
   /* Draw a transparent background */
   fillrect(pos_x - menu_width/2,
-           pos_y - 24*num_items/2,
-           menu_width,menu_height,
-           150,180,200,100);
+           pos_y - 24*item.size()/2 - 10,
+           menu_width,menu_height + 20,
+           150,180,200,125);
 
-  for(int i = 0; i < num_items; ++i)
+  for(unsigned int i = 0; i < item.size(); ++i)
     {
       draw_item(i, menu_width, menu_height);
     }
@@ -536,7 +538,8 @@ void menu_process_current(void)
 }
 
 /* Check for menu event */
-void menu_event(SDL_Event& event)
+void
+Menu::event(SDL_Event& event)
 {
   SDLKey key;
   switch(event.type)
@@ -579,7 +582,7 @@ void menu_event(SDL_Event& event)
           menu_change = true;
           break;
         case SDLK_SPACE:
-          if(current_menu->item[current_menu->active_item].kind == MN_TEXTFIELD)
+          if(item[active_item].kind == MN_TEXTFIELD)
             {
               menuaction = MENU_ACTION_INPUT;
               menu_change = true;
@@ -625,10 +628,10 @@ void menu_event(SDL_Event& event)
     case SDL_MOUSEBUTTONDOWN:
       x = event.motion.x;
       y = event.motion.y;
-      if(x > current_menu->pos_x - current_menu->width()/2 &&
-          x < current_menu->pos_x + current_menu->width()/2 &&
-          y > current_menu->pos_y - current_menu->height()/2 &&
-          y < current_menu->pos_y + current_menu->height()/2)
+      if(x > pos_x - width()/2 &&
+          x < pos_x + width()/2 &&
+          y > pos_y - height()/2 &&
+          y < pos_y + height()/2)
         {
           menuaction = MENU_ACTION_HIT;
         }
@@ -636,12 +639,12 @@ void menu_event(SDL_Event& event)
     case SDL_MOUSEMOTION:
       x = event.motion.x;
       y = event.motion.y;
-      if(x > current_menu->pos_x - current_menu->width()/2 &&
-          x < current_menu->pos_x + current_menu->width()/2 &&
-          y > current_menu->pos_y - current_menu->height()/2 &&
-          y < current_menu->pos_y + current_menu->height()/2)
+      if(x > pos_x - width()/2 &&
+          x < pos_x + width()/2 &&
+          y > pos_y - height()/2 &&
+          y < pos_y + height()/2)
         {
-          current_menu->active_item = (y - (current_menu->pos_y - current_menu->height()/2)) / 24;
+          active_item = (y - (pos_y - height()/2)) / 24;
           menu_change = true;
          mouse_cursor->set_state(MC_LINK);
         }