[Super-tux-commit] supertux/src menu.cpp,1.30,1.31 menu.h,1.29,1.30
Brought to you by:
wkendrick
From: Ingo R. <gr...@us...> - 2004-04-11 18:01:53
|
Update of /cvsroot/super-tux/supertux/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24629 Modified Files: menu.cpp menu.h Log Message: - turned some pointer-foo into a std::vector Index: menu.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/menu.cpp,v retrieving revision 1.30 retrieving revision 1.31 diff -u -d -r1.30 -r1.31 --- menu.cpp 11 Apr 2004 17:39:47 -0000 1.30 +++ menu.cpp 11 Apr 2004 17:48:12 -0000 1.31 @@ -122,15 +122,14 @@ /* 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); } } @@ -140,10 +139,8 @@ pos_y = screen->h/2; last_menu = 0; arrange_left = 0; - num_items = 0; active_item = 0; last_menu = 0; - item = NULL; effect.init(false); } @@ -163,17 +160,15 @@ void Menu::additem(MenuItem* pmenu_item) { - ++num_items; - item = (MenuItem*)realloc(item, sizeof(MenuItem) * num_items); - memcpy(&item[num_items-1], pmenu_item, sizeof(MenuItem)); - 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) { @@ -181,11 +176,11 @@ 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; @@ -297,7 +292,7 @@ if(menuaction != MENU_ACTION_UP && menuaction != MENU_ACTION_DOWN) menuaction = MENU_ACTION_DOWN; - if(num_items > 1) + if(item.size() > 1) action(); } } @@ -305,7 +300,7 @@ 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 @@ -477,7 +472,7 @@ /* 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 ) @@ -493,7 +488,7 @@ int Menu::height() { - return ((num_items) * 24); + return item.size() * 24; } /* Draw the current menu. */ @@ -505,11 +500,11 @@ /* Draw a transparent background */ fillrect(pos_x - menu_width/2, - pos_y - 24*num_items/2 - 10, + 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); } Index: menu.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/menu.h,v retrieving revision 1.29 retrieving revision 1.30 diff -u -d -r1.29 -r1.30 --- menu.h 11 Apr 2004 17:39:47 -0000 1.29 +++ menu.h 11 Apr 2004 17:48:12 -0000 1.30 @@ -14,6 +14,7 @@ #define SUPERTUX_MENU_H #include <SDL.h> +#include <vector> #include "texture.h" #include "timer.h" #include "type.h" @@ -59,7 +60,6 @@ int pos_x; int pos_y; - int num_items; Menu* last_menu; int width(); int height(); @@ -68,7 +68,7 @@ Timer effect; int arrange_left; int active_item; - MenuItem* item; + std::vector<MenuItem> item; static void set_current(Menu* pmenu); |