From: Ingo Ruhnke Date: Sun, 21 Mar 2004 20:31:47 +0000 (+0000) Subject: - added default argument instead of NO_UPDATE X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=f5e0142fd278bfdaaacde96568cc13134dfb6051;p=supertux.git - added default argument instead of NO_UPDATE - cleaned menucode a little bit SVN-Revision: 303 --- diff --git a/src/menu.cpp b/src/menu.cpp index 7e91b9d99..ce2b3b8d0 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -259,128 +259,180 @@ int menu_check(menu_type* pmenu) return -1; } -/* Draw the current menu. */ -void menu_draw(menu_type* pmenu) +void menu_draw_item(menu_type* pmenu, const menu_item_type& pitem, + int offset, // Position of the current item in the menu + int menu_width, + int menu_height) { - int i, y, a, b, e, f, menu_height, menu_width; + int a; + int b; + int e; + int f; - /* The width of the menu has to be more than the width of the text with the most characters */ - menu_width = 0; - for(i = 0; i < pmenu->num_items; ++i) - { - y = strlen(pmenu->item[i].text) + (pmenu->item[i].input ? strlen(pmenu->item[i].input) + 1 : 0) + strlen(string_list_active(pmenu->item[i].list)); - if( y > menu_width ) - { - menu_width = y; - if( pmenu->item[i].kind == MN_TOGGLE) - menu_width += 2; - } - } if(pmenu->arrange_left == YES) a = menu_width * 16; else a = 0; + + if(pmenu->arrange_left == YES) + b = (a - ((strlen(pitem.text)+strlen(pitem.input)+ strlen(string_list_active(pitem.list))) * 16)) / 2; + else + b = 0; - menu_width = menu_width * 16 + 48; - menu_height = (pmenu->num_items) * 24; + if(timer_check(&pmenu->effect)) + e = timer_get_left(&pmenu->effect) / 4; + else + e = 0; - /* Draw a transparent background */ - fillrect(screen->w/2 - menu_width/2,screen->h/2-(((pmenu->num_items)*24)/2),menu_width,menu_height,150,150,150,100); + if(e != 0) + f = (offset % 2) ? e : -e; + else + f = 0; - if(timer_check(&pmenu->effect)) + int x_pos = screen->w/2 - menu_width/2; + int y_pos = offset*24 - menu_height/2; + + int center_x = screen->w/2; + int center_y = screen->h /2; + int start_x = center_x - b; + + if(pitem.kind == MN_DEACTIVE) { - e = timer_get_left(&pmenu->effect) / 4; + text_drawf(&black_text, pitem.text, + -b, y_pos + 10 + f, + A_HMIDDLE, A_VMIDDLE, 2, NO_UPDATE); } - else + else if(pitem.kind == MN_HL) { - e = 0; + /* Draw a horizontal line with a little 3d effect */ + fillrect(x_pos, y_pos + 6 + center_y, + menu_width, 4, + 210,50,50,225); + fillrect(x_pos, y_pos + 10 + center_y, + menu_width, 2, + 0,0,0,255); } - - for(i = 0; i < pmenu->num_items; ++i) + else if(pitem.kind == MN_LABEL) { - if(pmenu->arrange_left == YES) - b = (a - ((strlen(pmenu->item[i].text)+strlen(pmenu->item[i].input)+ strlen(string_list_active(pmenu->item[i].list))) * 16)) / 2; - else - b = 0; - - if(e != 0) + text_drawf(&white_big_text, pitem.text, + -b, y_pos + 10, + A_HMIDDLE, A_VMIDDLE, 2); + } + else if(pitem.kind == MN_TEXTFIELD || pitem.kind == MN_NUMFIELD) + { + int input_pos = ((strlen(pitem.input)*16)/2); + int text_pos = ((strlen(pitem.text) + 1)*16)/2; + fillrect(-b +center_x - input_pos + text_pos - 1,y_pos + 10 + center_y - 10 + f, + (strlen(pitem.input)+1)*16 + 2, 20, + 255,255,255,255); + fillrect(-b +center_x - input_pos + text_pos, y_pos + 10 + center_y - 9 + f, + (strlen(pitem.input)+1)*16, 18, + 0,0,0,128); + text_drawf(&gold_text, pitem.input, + -b + text_pos, y_pos + 10 + f, + A_HMIDDLE, A_VMIDDLE, 2); + + if(offset == pmenu->active_item) { - if(i % 2) - f = e; - else - f = -e; + text_drawf(&blue_text,pitem.text, -b -(((strlen(pitem.input)+1) * 16)/2),y_pos + 10 + f,A_HMIDDLE, A_VMIDDLE,3); } else - f = 0; - - if(pmenu->item[i].kind == MN_DEACTIVE) - { - text_drawf(&black_text,pmenu->item[i].text, - b,(i)*24 - menu_height/2 + 10 + f,A_HMIDDLE, A_VMIDDLE,2,NO_UPDATE); - } - else if(pmenu->item[i].kind == MN_HL) - { - /* Draw a horizontal line with a little 3d effect */ - fillrect(screen->w/2 - menu_width/2,(i)*24 - menu_height/2 + 6 + screen->h /2,menu_width,4,210,50,50,225); - fillrect(screen->w/2 - menu_width/2,(i)*24 - menu_height/2 + 10 + screen->h /2,menu_width,2,0,0,0,255); - } - else if(pmenu->item[i].kind == MN_LABEL) - { - text_drawf(&white_big_text,pmenu->item[i].text, - b,(i)*24 - menu_height/2 + 10,A_HMIDDLE, A_VMIDDLE,2,NO_UPDATE); - } - else if(pmenu->item[i].kind == MN_TEXTFIELD || pmenu->item[i].kind == MN_NUMFIELD) - { - fillrect(-b +screen->w/2 - ((strlen(pmenu->item[i].input)*16)/2) + ((strlen(pmenu->item[i].text) + 1)*16)/2 - 1,(i)*24 - menu_height/2 + 10 + screen->h /2 - 10 + f,(strlen(pmenu->item[i].input)+1)*16 + 2,20,255,255,255,255); - fillrect(- b +screen->w/2 - ((strlen(pmenu->item[i].input)*16)/2) + ((strlen(pmenu->item[i].text) + 1)*16)/2,(i)*24 - menu_height/2 + 10 + screen->h /2 - 9 + f,(strlen(pmenu->item[i].input)+1)*16,18,0,0,0,128); - text_drawf(&gold_text,pmenu->item[i].input, - b + ((strlen(pmenu->item[i].text)+1) * 16)/2,(i)*24 - menu_height/2 + 10 + f,A_HMIDDLE, A_VMIDDLE,2,NO_UPDATE); - if(i == pmenu->active_item) - { - text_drawf(&blue_text,pmenu->item[i].text, - b -(((strlen(pmenu->item[i].input)+1) * 16)/2),(i)*24 - menu_height/2 + 10 + f,A_HMIDDLE, A_VMIDDLE,3,NO_UPDATE); - } - else - { - text_drawf(&white_text,pmenu->item[i].text, - b -(((strlen(pmenu->item[i].input)+1) * 16)/2),(i)*24 - menu_height/2 +10 + f,A_HMIDDLE, A_VMIDDLE,2,NO_UPDATE); - } - } - else if(pmenu->item[i].kind == MN_STRINGSELECT) { - /* Draw arrows */ - texture_draw(&arrow_left,-b +screen->w/2 - ((strlen(string_list_active(pmenu->item[i].list))*16)/2) + ((strlen(pmenu->item[i].text) + 1)*16)/2 - 17,(i)*24 - menu_height/2 + 10 + screen->h / 2 -8 + f,NO_UPDATE); - texture_draw(&arrow_right,-b +screen->w/2 - ((strlen(string_list_active(pmenu->item[i].list))*16)/2) + ((strlen(pmenu->item[i].text) + 1)*16)/2 - 1 + (strlen(string_list_active(pmenu->item[i].list))+1)*16,(i)*24 - menu_height/2 + 10 + screen->h / 2 -8 + f,NO_UPDATE); - /* Draw input background */ - fillrect(-b +screen->w/2 - ((strlen(string_list_active(pmenu->item[i].list))*16)/2) + ((strlen(pmenu->item[i].text) + 1)*16)/2 - 1,(i)*24 - menu_height/2 + 10 + screen->h /2 - 10 + f,(strlen(string_list_active(pmenu->item[i].list))+1)*16 + 2,20,255,255,255,255); - fillrect(- b +screen->w/2 - ((strlen(string_list_active(pmenu->item[i].list))*16)/2) + ((strlen(pmenu->item[i].text) + 1)*16)/2,(i)*24 - menu_height/2 + 10 + screen->h /2 - 9 + f,(strlen(string_list_active(pmenu->item[i].list))+1)*16,18,0,0,0,128); - - text_drawf(&gold_text,string_list_active(pmenu->item[i].list), - b + ((strlen(pmenu->item[i].text)+1) * 16)/2,(i)*24 - menu_height/2 + 10 + f,A_HMIDDLE, A_VMIDDLE,2,NO_UPDATE); - if(i == pmenu->active_item) - { - text_drawf(&blue_text,pmenu->item[i].text, - b -(((strlen(string_list_active(pmenu->item[i].list))+1) * 16)/2),(i)*24 - menu_height/2 + 10 + f,A_HMIDDLE, A_VMIDDLE,3,NO_UPDATE); - } - else - { - text_drawf(&white_text,pmenu->item[i].text, - b -(((strlen(string_list_active(pmenu->item[i].list))+1) * 16)/2),(i)*24 - menu_height/2 +10 + f,A_HMIDDLE, A_VMIDDLE,2,NO_UPDATE); - } + text_drawf(&white_text,pitem.text, -b -(((strlen(pitem.input)+1) * 16)/2),y_pos +10 + f,A_HMIDDLE, A_VMIDDLE,2); } - else if(i == pmenu->active_item) + } + else if(pitem.kind == MN_STRINGSELECT) + { + int list_pos = ((strlen(string_list_active(pitem.list))*16)/2); + int list_pos_2 = (strlen(string_list_active(pitem.list))+1)*16; + int text_pos = ((strlen(pitem.text) + 1)*16)/2; + + /* Draw arrows */ + texture_draw(&arrow_left, start_x - list_pos + text_pos - 17, y_pos + 10 + center_y -8 + f); + texture_draw(&arrow_right, start_x - list_pos + text_pos - 1 + list_pos_2, y_pos + 10 + center_y -8 + f); + + /* Draw input background */ + fillrect(start_x - list_pos + text_pos - 1, y_pos + 10 + center_y - 10 + f, + list_pos_2 + 2, 20, + 255,255,255,255); + fillrect(start_x - list_pos + text_pos, y_pos + 10 + center_y - 9 + f, + list_pos_2, 18, + 0,0,0,128); + + text_drawf(&gold_text, string_list_active(pitem.list), + -b + text_pos, y_pos + 10 + f, + A_HMIDDLE, A_VMIDDLE,2); + + if(offset == pmenu->active_item) { - text_drawf(&blue_text,pmenu->item[i].text, - b,(i)*24 - menu_height/2 + 10 + f ,A_HMIDDLE, A_VMIDDLE,3,NO_UPDATE); + text_drawf(&blue_text,pitem.text, + -b - list_pos_2/2, y_pos + 10 + f, + A_HMIDDLE, A_VMIDDLE,3); } else { - text_drawf(&white_text,pmenu->item[i].text, - b,(i)*24 - menu_height/2 + 10 + f ,A_HMIDDLE, A_VMIDDLE,2,NO_UPDATE); + text_drawf(&white_text,pitem.text, + -b - list_pos_2/2, y_pos +10 + f, + A_HMIDDLE, A_VMIDDLE,2); } - if(pmenu->item[i].kind == MN_TOGGLE) - { + } + else if(offset == pmenu->active_item) + { + text_drawf(&blue_text, pitem.text, + -b,y_pos + 10 + f, + A_HMIDDLE, A_VMIDDLE, 3); + } + else + { + text_drawf(&white_text, pitem.text, + -b, y_pos + 10 + f, + A_HMIDDLE, A_VMIDDLE, 2); + } - if(pmenu->item[i].toggled == YES) - texture_draw(&checkbox_checked, - b + screen->w / 2 + (strlen(pmenu->item[i].text) * 16)/2 + 16,(i)*24 - menu_height/2 + 10 + screen->h / 2 -8 + f,NO_UPDATE); - else - texture_draw(&checkbox, - b + screen->w / 2 + (strlen(pmenu->item[i].text) * 16)/2 + 16,(i)*24 - menu_height/2 + 10 + screen->h / 2 - 8 + f,NO_UPDATE); - } - else if(pmenu->item[i].kind == MN_BACK) + if(pitem.kind == MN_TOGGLE) + { + if(pitem.toggled == YES) + texture_draw(&checkbox_checked, -b + screen->w / 2 + (strlen(pitem.text) * 16)/2 + 16,y_pos + 10 + center_y -8 + f); + else + texture_draw(&checkbox, -b + screen->w / 2 + (strlen(pitem.text) * 16)/2 + 16,y_pos + 10 + center_y - 8 + f); + } + else if(pitem.kind == MN_BACK) + { + texture_draw(&back, -b + screen->w / 2 + (strlen(pitem.text) * 16)/2 + 16,y_pos + 10 + center_y -8 + f); + } +} + +/* Draw the current menu. */ +void menu_draw(menu_type* pmenu) +{ + int y; + int menu_height; + int menu_width; + + /* The width of the menu has to be more than the width of the text with the most characters */ + menu_width = 0; + for(int i = 0; i < pmenu->num_items; ++i) + { + y = strlen(pmenu->item[i].text) + (pmenu->item[i].input ? strlen(pmenu->item[i].input) + 1 : 0) + strlen(string_list_active(pmenu->item[i].list)); + if( y > menu_width ) { - texture_draw(&back, - b + screen->w / 2 + (strlen(pmenu->item[i].text) * 16)/2 + 16,(i)*24 - menu_height/2 + 10 + screen->h / 2 -8 + f,NO_UPDATE); + menu_width = y; + if( pmenu->item[i].kind == MN_TOGGLE) + menu_width += 2; } } + + menu_width = menu_width * 16 + 48; + menu_height = (pmenu->num_items) * 24; + + int center_x = screen->w/2; + /* Draw a transparent background */ + fillrect(center_x - menu_width/2,screen->h/2-(((pmenu->num_items)*24)/2),menu_width,menu_height,150,150,150,100); + + for(int i = 0; i < pmenu->num_items; ++i) + { + menu_draw_item(pmenu, pmenu->item[i], i, menu_width, menu_height); + } } /* Reset/Set global defaults */ diff --git a/src/screen.h b/src/screen.h index b32bd8ff5..7ff7a22ba 100644 --- a/src/screen.h +++ b/src/screen.h @@ -19,8 +19,8 @@ #endif #include "texture.h" -#define NO_UPDATE 0 -#define UPDATE 1 +#define NO_UPDATE false +#define UPDATE true #define USE_ALPHA 0 #define IGNORE_ALPHA 1 diff --git a/src/text.h b/src/text.h index 383e25f95..e0d6a8ad4 100644 --- a/src/text.h +++ b/src/text.h @@ -45,7 +45,7 @@ enum { void text_load(text_type* ptext, const std::string& file, int kind, int w, int h); void text_draw(text_type* ptext, const char* text, int x, int y, int shadowsize, int update); void text_draw_chars(text_type* ptext, texture_type* pchars, const char* text, int x, int y, int update); -void text_drawf(text_type* ptext, const char* text, int x, int y, int halign, int valign, int shadowsize, int update); +void text_drawf(text_type* ptext, const char* text, int x, int y, int halign, int valign, int shadowsize, int update = NO_UPDATE); void text_free(text_type* ptext); void erasetext(text_type* ptext, const char * text, int x, int y, texture_type * surf, int update, int shadowsize); void erasecenteredtext(text_type* ptext, const char * text, int y, texture_type * surf, int update, int shadowsize); diff --git a/src/texture.cpp b/src/texture.cpp index e318207bf..bd0031e20 100644 --- a/src/texture.cpp +++ b/src/texture.cpp @@ -20,9 +20,9 @@ void (*texture_load) (texture_type* ptexture, const std::string& file, int use_alpha); void (*texture_load_part)(texture_type* ptexture, const std::string& file, int x, int y, int w, int h, int use_alpha); void (*texture_free) (texture_type* ptexture); -void (*texture_draw) (texture_type* ptexture, float x, float y, int update); -void (*texture_draw_bg) (texture_type* ptexture, int update); -void (*texture_draw_part)(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, int update); +void (*texture_draw) (texture_type* ptexture, float x, float y, bool update); +void (*texture_draw_bg) (texture_type* ptexture, bool update); +void (*texture_draw_part)(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, bool update); void texture_setup(void) @@ -135,7 +135,7 @@ void texture_free_gl(texture_type* ptexture) glDeleteTextures(1, &ptexture->gl_texture); } -void texture_draw_gl(texture_type* ptexture, float x, float y, int update) +void texture_draw_gl(texture_type* ptexture, float x, float y, bool update) { float pw = power_of_two(ptexture->w); float ph = power_of_two(ptexture->h); @@ -162,7 +162,7 @@ float ph = power_of_two(ptexture->h); glDisable(GL_BLEND); } -void texture_draw_bg_gl(texture_type* ptexture, int update) +void texture_draw_bg_gl(texture_type* ptexture, bool update) { float pw = power_of_two(ptexture->w); float ph = power_of_two(ptexture->h); @@ -186,7 +186,7 @@ float ph = power_of_two(ptexture->h); glDisable(GL_TEXTURE_2D); } -void texture_draw_part_gl(texture_type* ptexture,float sx, float sy, float x, float y, float w, float h, int update) +void texture_draw_part_gl(texture_type* ptexture,float sx, float sy, float x, float y, float w, float h, bool update) { float pw = power_of_two(ptexture->w); float ph = power_of_two(ptexture->h); @@ -342,7 +342,7 @@ void texture_from_sdl_surface(texture_type* ptexture, SDL_Surface* sdl_surf, int #endif } -void texture_draw_sdl(texture_type* ptexture, float x, float y, int update) +void texture_draw_sdl(texture_type* ptexture, float x, float y, bool update) { SDL_Rect dest; @@ -358,7 +358,7 @@ void texture_draw_sdl(texture_type* ptexture, float x, float y, int update) } -void texture_draw_bg_sdl(texture_type* ptexture, int update) +void texture_draw_bg_sdl(texture_type* ptexture, bool update) { SDL_Rect dest; @@ -373,7 +373,7 @@ void texture_draw_bg_sdl(texture_type* ptexture, int update) SDL_UpdateRect(screen, dest.x, dest.y, dest.w, dest.h); } -void texture_draw_part_sdl(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, int update) +void texture_draw_part_sdl(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, bool update) { SDL_Rect src, dest; diff --git a/src/texture.h b/src/texture.h index 68fa4bccb..329ff781c 100644 --- a/src/texture.h +++ b/src/texture.h @@ -20,6 +20,8 @@ #include "SDL_opengl.h" #endif +#include "screen.h" + /* Texture type */ typedef struct texture_type { @@ -33,23 +35,23 @@ void texture_setup(void); extern void (*texture_load) (texture_type* ptexture, const std::string& file, int use_alpha); extern void (*texture_load_part) (texture_type* ptexture, const std::string& file, int x, int y, int w, int h, int use_alpha); extern void (*texture_free) (texture_type* ptexture); -extern void (*texture_draw) (texture_type* ptexture, float x, float y, int update); -extern void (*texture_draw_bg) (texture_type* ptexture, int update); -extern void (*texture_draw_part) (texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, int update); +extern void (*texture_draw) (texture_type* ptexture, float x, float y, bool update = false); +extern void (*texture_draw_bg) (texture_type* ptexture, bool update = false); +extern void (*texture_draw_part) (texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, bool update = false); void texture_load_sdl(texture_type* ptexture, const std::string&, int use_alpha); void texture_load_part_sdl(texture_type* ptexture, const std::string& file, int x, int y, int w, int h, int use_alpha); void texture_free_sdl(texture_type* ptexture); -void texture_draw_sdl(texture_type* ptexture, float x, float y, int update); -void texture_draw_bg_sdl(texture_type* ptexture, int update); -void texture_draw_part_sdl(texture_type* ptexture,float sx, float sy, float x, float y, float w, float h, int update); +void texture_draw_sdl(texture_type* ptexture, float x, float y, bool update); +void texture_draw_bg_sdl(texture_type* ptexture, bool update); +void texture_draw_part_sdl(texture_type* ptexture,float sx, float sy, float x, float y, float w, float h, bool update); void texture_from_sdl_surface(texture_type* ptexture, SDL_Surface * sdl_surf, int use_alpha); #ifndef NOOPENGL void texture_load_gl(texture_type* ptexture, const std::string& file, int use_alpha); void texture_load_part_gl(texture_type* ptexture, const std::string& file, int x, int y, int w, int h, int use_alpha); void texture_free_gl(texture_type* ptexture); -void texture_draw_gl(texture_type* ptexture, float x, float y, int update); -void texture_draw_bg_gl(texture_type* ptexture, int update); -void texture_draw_part_gl(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, int update); +void texture_draw_gl(texture_type* ptexture, float x, float y, bool update); +void texture_draw_bg_gl(texture_type* ptexture, bool update); +void texture_draw_part_gl(texture_type* ptexture, float sx, float sy, float x, float y, float w, float h, bool update); void texture_create_gl(SDL_Surface * surf, GLuint * tex); #endif