From ed7b3006da4a6ffda69802965fd26d4c286f8349 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Tobias=20Gl=C3=A4=C3=9Fer?= Date: Thu, 4 Mar 2004 23:15:44 +0000 Subject: [PATCH] leveleditor level-settings menu is working now. introduced string_list_type. SVN-Revision: 170 --- src/leveleditor.c | 179 +++++++++++++++++++++++++++++++++--------------------- src/menu.c | 53 ++++++++++------ src/menu.h | 4 +- src/setup.c | 87 ++++++++++++++++++++++---- src/setup.h | 4 +- src/title.c | 24 ++++---- src/type.c | 52 ++++++++++++++++ src/type.h | 15 +++++ 8 files changed, 305 insertions(+), 113 deletions(-) diff --git a/src/leveleditor.c b/src/leveleditor.c index 46eea5b2c..dacafa1ef 100644 --- a/src/leveleditor.c +++ b/src/leveleditor.c @@ -77,8 +77,7 @@ void le_highlight_selection(); void apply_level_settings_menu(); /* leveleditor internals */ -static char **level_subsets; -static int subsets_num; +static string_list_type level_subsets; static int le_level_changed; /* if changes, ask for saving, when quiting*/ static int pos_x, cursor_x, cursor_y, cursor_tile, fire; static int le_level; @@ -226,7 +225,7 @@ int leveleditor(int levelnb) default: if(i != -1) { - subset_load(&le_level_subset,level_subsets[i-2]); + subset_load(&le_level_subset,level_subsets.item[i-2]); leveleditor_menu.item[3].kind = MN_GOTO; menu_item_change_input(&subset_settings_menu.item[2],le_level_subset.title); menu_item_change_input(&subset_settings_menu.item[3],le_level_subset.description); @@ -331,8 +330,7 @@ int le_init() { int i; char str[80]; - level_subsets = NULL; - level_subsets = dsubdirs("/levels", "info", &subsets_num); + level_subsets = dsubdirs("/levels", "info"); le_show_grid = YES; @@ -397,9 +395,9 @@ int le_init() menu_init(&subset_load_menu); menu_additem(&subset_load_menu,menu_item_create(MN_LABEL,"Load Level Subset",0,0)); menu_additem(&subset_load_menu,menu_item_create(MN_HL,"",0,0)); - for(i = 0; i < subsets_num; ++i) + for(i = 0; i < level_subsets.num_items; ++i) { - menu_additem(&subset_load_menu,menu_item_create(MN_ACTION,level_subsets[i],0,0)); + menu_additem(&subset_load_menu,menu_item_create(MN_ACTION,level_subsets.item[i],0,0)); } menu_additem(&subset_load_menu,menu_item_create(MN_HL,"",0,0)); menu_additem(&subset_load_menu,menu_item_create(MN_BACK,"Back",0,0)); @@ -454,6 +452,11 @@ void update_level_settings_menu() menu_item_change_input(&level_settings_menu.item[2], le_current_level->name); sprintf(str,"%d",le_current_level->width); + + string_list_copy(level_settings_menu.item[3].list, dsubdirs("images/themes", "solid0.png")); + string_list_copy(level_settings_menu.item[4].list, dfiles("music/", NULL)); + string_list_copy(level_settings_menu.item[5].list, dfiles("images/background", NULL)); + menu_item_change_input(&level_settings_menu.item[6], str); sprintf(str,"%d",le_current_level->time_left); menu_item_change_input(&level_settings_menu.item[7], str); @@ -471,7 +474,18 @@ void apply_level_settings_menu() { int i,y,j; strcpy(le_current_level->name,level_settings_menu.item[2].input); - + + strcpy(le_current_level->bkgd_image,string_list_active(level_settings_menu.item[5].list)); + + if(strcmp(le_current_level->theme,string_list_active(level_settings_menu.item[3].list)) != 0) + { + strcpy(le_current_level->theme,string_list_active(level_settings_menu.item[3].list)); + level_free_gfx(); + level_load_gfx(le_current_level); + } + + strcpy(le_current_level->song_title,string_list_active(level_settings_menu.item[4].list)); + i = le_current_level->width; le_current_level->width = atoi(level_settings_menu.item[6].input); if(le_current_level->width < i) @@ -963,83 +977,108 @@ void le_checkevents() } + 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))) { - /* Check for button events */ - button_event(&le_test_level_bt,&event); - if(button_get_state(&le_test_level_bt) == BN_CLICKED) - le_testlevel(); - button_event(&le_save_level_bt,&event); - if(button_get_state(&le_save_level_bt) == BN_CLICKED) - level_save(le_current_level,le_level_subset.name,le_level); - button_event(&le_next_level_bt,&event); - if(button_get_state(&le_next_level_bt) == BN_CLICKED) - { - if(le_level < le_level_subset.levels) - le_goto_level(++le_level); - } - button_event(&le_previous_level_bt,&event); - if(button_get_state(&le_previous_level_bt) == BN_CLICKED) + if(show_menu == NO) { - if(le_level > 1) - le_goto_level(--le_level); - } - button_event(&le_rubber_bt,&event); - if(button_get_state(&le_rubber_bt) == BN_CLICKED) - le_current_tile = '.'; - button_event(&le_select_mode_one_bt,&event); - if(button_get_state(&le_select_mode_one_bt) == BN_CLICKED) - le_selection_mode = CURSOR; - button_event(&le_select_mode_two_bt,&event); - if(button_get_state(&le_select_mode_two_bt) == BN_CLICKED) - le_selection_mode = SQUARE; - button_event(&le_bad_bsod_bt,&event); - if(button_get_state(&le_bad_bsod_bt) == BN_CLICKED) - le_current_tile = '0'; - button_event(&le_settings_bt,&event); - if(button_get_state(&le_settings_bt) == BN_CLICKED) - { - if(show_menu == NO) + /* Check for button events */ + button_event(&le_test_level_bt,&event); + if(button_get_state(&le_test_level_bt) == BN_CLICKED) + le_testlevel(); + button_event(&le_save_level_bt,&event); + if(button_get_state(&le_save_level_bt) == BN_CLICKED) + level_save(le_current_level,le_level_subset.name,le_level); + button_event(&le_next_level_bt,&event); + if(button_get_state(&le_next_level_bt) == BN_CLICKED) { - update_level_settings_menu(); - menu_set_current(&level_settings_menu); - show_menu = YES; + if(le_level < le_level_subset.levels) + le_goto_level(++le_level); } - else + button_event(&le_previous_level_bt,&event); + if(button_get_state(&le_previous_level_bt) == BN_CLICKED) { - menu_set_current(&leveleditor_menu); - show_menu = NO; + if(le_level > 1) + le_goto_level(--le_level); + } + button_event(&le_rubber_bt,&event); + if(button_get_state(&le_rubber_bt) == BN_CLICKED) + le_current_tile = '.'; + button_event(&le_select_mode_one_bt,&event); + if(button_get_state(&le_select_mode_one_bt) == BN_CLICKED) + le_selection_mode = CURSOR; + button_event(&le_select_mode_two_bt,&event); + if(button_get_state(&le_select_mode_two_bt) == BN_CLICKED) + le_selection_mode = SQUARE; + button_event(&le_bad_bsod_bt,&event); + if(button_get_state(&le_bad_bsod_bt) == BN_CLICKED) + le_current_tile = '0'; + button_event(&le_settings_bt,&event); + if(button_get_state(&le_settings_bt) == BN_CLICKED) + { + if(show_menu == NO) + { + update_level_settings_menu(); + menu_set_current(&level_settings_menu); + show_menu = YES; + } + else + { + menu_set_current(&leveleditor_menu); + show_menu = NO; + } + } + } + else + { + button_event(&le_settings_bt,&event); + if(button_get_state(&le_settings_bt) == BN_CLICKED) + { + if(show_menu == NO) + { + update_level_settings_menu(); + menu_set_current(&level_settings_menu); + show_menu = YES; + } + else + { + menu_set_current(&leveleditor_menu); + show_menu = NO; + } } } } - } + if(show_menu == NO) + { + button_event(&le_move_left_bt,&event); + if(button_get_state(&le_move_left_bt) == BN_PRESSED) + { + pos_x -= 192; + } + else if(button_get_state(&le_move_left_bt) == BN_HOVER) + { + pos_x -= 96; + } + button_event(&le_move_right_bt,&event); + if(button_get_state(&le_move_right_bt) == BN_PRESSED) + { + pos_x += 192; + } + else if(button_get_state(&le_move_right_bt) == BN_HOVER) + { + pos_x += 96; + } - button_event(&le_move_left_bt,&event); - if(button_get_state(&le_move_left_bt) == BN_PRESSED) - { - pos_x -= 192; - } - else if(button_get_state(&le_move_left_bt) == BN_HOVER) - { - pos_x -= 96; - } - button_event(&le_move_right_bt,&event); - if(button_get_state(&le_move_right_bt) == BN_PRESSED) - { - pos_x += 192; - } - else if(button_get_state(&le_move_right_bt) == BN_HOVER) - { - pos_x += 96; + if(le_mouse_pressed) + { + le_change(cursor_x, cursor_y, le_current_tile); + } + } } - if(le_mouse_pressed) - { - le_change(cursor_x, cursor_y, le_current_tile); - } } diff --git a/src/menu.c b/src/menu.c index 191296d62..9e39efe0e 100644 --- a/src/menu.c +++ b/src/menu.c @@ -36,7 +36,6 @@ int menuaction; int show_menu; int menu_change; texture_type checkbox, checkbox_checked, back, arrow_left, arrow_right; -; menu_type main_menu, game_menu, options_menu, highscore_menu, load_game_menu, save_game_menu; menu_type* current_menu, * last_menu; @@ -71,6 +70,13 @@ menu_item_type* menu_item_create(int kind, char *text, int init_toggle, void* ta pnew_item->target_menu = target_menu; pnew_item->input = (char*) malloc(sizeof(char)); pnew_item->input[0] = '\0'; + if(kind == MN_STRINGSELECT) + { + pnew_item->list = (string_list_type*) malloc(sizeof(string_list_type)); + string_list_init(pnew_item->list); + } + else + pnew_item->list = NULL; return pnew_item; } @@ -103,6 +109,7 @@ void menu_free(menu_type* pmenu) { free(pmenu->item[i].text); free(pmenu->item[i].input); + string_list_free(pmenu->item[i].list); } free(pmenu->item); } @@ -149,11 +156,23 @@ void menu_action(menu_type* pmenu) pmenu->active_item = 0; break; case MN_LEFT: - if(pmenu->item[pmenu->active_item].kind == MN_STRINGSELECT) - {} + if(pmenu->item[pmenu->active_item].kind == MN_STRINGSELECT && pmenu->item[pmenu->active_item].list->num_items != 0) + { + if(pmenu->item[pmenu->active_item].list->active_item > 0) + --pmenu->item[pmenu->active_item].list->active_item; + else + pmenu->item[pmenu->active_item].list->active_item = pmenu->item[pmenu->active_item].list->num_items-1; + } + break; case MN_RIGHT: - if(pmenu->item[pmenu->active_item].kind == MN_STRINGSELECT) - {} + if(pmenu->item[pmenu->active_item].kind == MN_STRINGSELECT && pmenu->item[pmenu->active_item].list->num_items != 0) + { + if(pmenu->item[pmenu->active_item].list->active_item < pmenu->item[pmenu->active_item].list->num_items-1) + ++pmenu->item[pmenu->active_item].list->active_item; + else + pmenu->item[pmenu->active_item].list->active_item = 0; + } + break; case MN_HIT: if(pmenu->item[pmenu->active_item].kind == MN_GOTO && pmenu->item[pmenu->active_item].target_menu != NULL) menu_set_current((menu_type*)pmenu->item[pmenu->active_item].target_menu); @@ -250,7 +269,7 @@ void menu_draw(menu_type* pmenu) 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); + 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; @@ -281,7 +300,7 @@ void menu_draw(menu_type* pmenu) for(i = 0; i < pmenu->num_items; ++i) { if(pmenu->arrange_left == YES) - b = (a - ((strlen(pmenu->item[i].text)+strlen(pmenu->item[i].input)) * 16)) / 2; + 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; @@ -325,21 +344,21 @@ void menu_draw(menu_type* pmenu) } else if(pmenu->item[i].kind == MN_STRINGSELECT) { - /* Draw arrows */ - texture_draw(&arrow_left,-b +screen->w/2 - ((strlen(pmenu->item[i].input)*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(pmenu->item[i].input)*16)/2) + ((strlen(pmenu->item[i].text) + 1)*16)/2 - 1 + (strlen(pmenu->item[i].input)+1)*16,(i)*24 - menu_height/2 + 10 + screen->h / 2 -8 + f,NO_UPDATE); - /* Draw input background */ - 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); + /* 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(pmenu->item[i].input)+1) * 16)/2),(i)*24 - menu_height/2 + 10 + f,A_HMIDDLE, A_VMIDDLE,3,NO_UPDATE); + 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(pmenu->item[i].input)+1) * 16)/2),(i)*24 - menu_height/2 +10 + f,A_HMIDDLE, A_VMIDDLE,2,NO_UPDATE); + 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); } } else if(i == pmenu->active_item) diff --git a/src/menu.h b/src/menu.h index 5550b592c..0274e35c0 100644 --- a/src/menu.h +++ b/src/menu.h @@ -16,13 +16,15 @@ #include #include "texture.h" #include "timer.h" +#include "type.h" typedef struct menu_item_type { int kind; + int toggled; char *text; char *input; - int toggled; + string_list_type* list; void* target_menu; } menu_item_type; diff --git a/src/setup.c b/src/setup.c index 22fecc4a6..5ae1e0fd5 100644 --- a/src/setup.c +++ b/src/setup.c @@ -90,15 +90,16 @@ int fcreatedir(char* relative_dir) /* Get all names of sub-directories in a certain directory. */ /* Returns the number of sub-directories found. */ /* Note: The user has to free the allocated space. */ -char ** dsubdirs(char *rel_path, char* expected_file, int* num) +string_list_type dsubdirs(char *rel_path, char* expected_file) { DIR *dirStructP; struct dirent *direntp; int i = 0; - char ** sdirs= NULL; + string_list_type sdirs; char filename[1024]; char path[1024]; + string_list_init(&sdirs); sprintf(path,"%s/%s",st_dir,rel_path); if((dirStructP = opendir(path)) != NULL) { @@ -118,10 +119,7 @@ char ** dsubdirs(char *rel_path, char* expected_file, int* num) continue; } - sdirs = (char**) realloc(sdirs, sizeof(char*) * (i+1)); - sdirs[i] = (char*) malloc(sizeof(char) * strlen(direntp->d_name) + 1 ); - strcpy(sdirs[i],direntp->d_name); - ++i; + string_list_add_item(&sdirs,direntp->d_name); } } closedir(dirStructP); @@ -154,16 +152,83 @@ char ** dsubdirs(char *rel_path, char* expected_file, int* num) } } - sdirs = (char**) realloc(sdirs, sizeof(char*) * (i+1)); - sdirs[i] = (char*) malloc(sizeof(char) * strlen(direntp->d_name) + 1 ); - strcpy(sdirs[i],direntp->d_name); - ++i; + string_list_add_item(&sdirs,direntp->d_name); + } + } + closedir(dirStructP); + } + + return sdirs; +} + +string_list_type dfiles(char *rel_path, char* expected_file) +{ + DIR *dirStructP; + struct dirent *direntp; + int i = 0; + string_list_type sdirs; + char filename[1024]; + char path[1024]; + + string_list_init(&sdirs); + sprintf(path,"%s/%s",st_dir,rel_path); + if((dirStructP = opendir(path)) != NULL) + { + while((direntp = readdir(dirStructP)) != NULL) + { + char absolute_filename[1024]; + struct stat buf; + + sprintf(absolute_filename, "%s/%s", path, direntp->d_name); + + if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode)) + { + if(expected_file != NULL) + { + sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file); + if(!faccessible(filename)) + continue; + } + + string_list_add_item(&sdirs,direntp->d_name); + } + } + closedir(dirStructP); + } + + sprintf(path,"%s/%s",DATA_PREFIX,rel_path); + if((dirStructP = opendir(path)) != NULL) + { + while((direntp = readdir(dirStructP)) != NULL) + { + char absolute_filename[1024]; + struct stat buf; + + sprintf(absolute_filename, "%s/%s", path, direntp->d_name); + + if (stat(absolute_filename, &buf) == 0 && S_ISREG(buf.st_mode)) + { + if(expected_file != NULL) + { + sprintf(filename,"%s/%s/%s",path,direntp->d_name,expected_file); + if(!faccessible(filename)) + { + continue; + } + else + { + sprintf(filename,"%s/%s/%s/%s",st_dir,rel_path,direntp->d_name,expected_file); + if(faccessible(filename)) + continue; + } + } + + string_list_add_item(&sdirs,direntp->d_name); } } closedir(dirStructP); } - *num = i; return sdirs; } diff --git a/src/setup.h b/src/setup.h index df9efe51e..60470a231 100644 --- a/src/setup.h +++ b/src/setup.h @@ -15,11 +15,13 @@ #include "menu.h" #include "sound.h" +#include "type.h" int faccessible(char *filename); int fcreatedir(char* relative_dir); int fwriteable(char *filename); -char ** dsubdirs(char *rel_path, char* expected_file, int* num); +string_list_type dsubdirs(char *rel_path, char* expected_file); +string_list_type dfiles(char *rel_path, char* expected_file); void free_strings(char **strings, int num); void st_directory_setup(void); void st_general_setup(void); diff --git a/src/title.c b/src/title.c index aead01a6a..44510a642 100644 --- a/src/title.c +++ b/src/title.c @@ -46,10 +46,8 @@ int title(void) SDLKey key; int done, quit, frame, pict, i; char str[80]; - char **level_subsets; - int subsets_num; - level_subsets = NULL; - level_subsets = dsubdirs("/levels", "info", &subsets_num); + string_list_type level_subsets; + level_subsets = dsubdirs("/levels", "info"); st_subset subset; subset_init(&subset); @@ -157,20 +155,20 @@ int title(void) case 2: done = 0; i = 0; - if(level_subsets != NULL) + if(level_subsets.num_items != 0) { - subset_load(&subset,level_subsets[0]); + subset_load(&subset,level_subsets.item[0]); while(!done) { texture_draw(&img_choose_subset,(screen->w - img_choose_subset.w) / 2, 0, NO_UPDATE); - if(subsets_num != 0) + if(level_subsets.num_items != 0) { texture_draw(&subset.image,(screen->w - subset.image.w) / 2 + 25,78,NO_UPDATE); - if(subsets_num > 1) + if(level_subsets.num_items > 1) { if(i > 0) texture_draw(&arrow_left,(screen->w / 2) - ((strlen(subset.title)+2)*16)/2,20,NO_UPDATE); - if(i < subsets_num-1) + if(i < level_subsets.num_items-1) texture_draw(&arrow_right,(screen->w / 2) + ((strlen(subset.title))*16)/2,20,NO_UPDATE); } text_drawf(&gold_text, subset.title, 0, 20, A_HMIDDLE, A_TOP, 1, NO_UPDATE); @@ -196,16 +194,16 @@ int title(void) { --i; subset_free(&subset); - subset_load(&subset,level_subsets[i]); + subset_load(&subset,level_subsets.item[i]); } } else if(key == SDLK_RIGHT) { - if(i < subsets_num -1) + if(i < level_subsets.num_items -1) { ++i; subset_free(&subset); - subset_load(&subset,level_subsets[i]); + subset_load(&subset,level_subsets.item[i]); } } else if(key == SDLK_SPACE || key == SDLK_RETURN) @@ -259,7 +257,7 @@ int title(void) texture_free(&title); texture_free(&anim1); texture_free(&anim2); - free_strings(level_subsets,subsets_num); + string_list_free(&level_subsets); /* Return to main! */ diff --git a/src/type.c b/src/type.c index 69c61f36f..ee23c2712 100644 --- a/src/type.c +++ b/src/type.c @@ -11,6 +11,8 @@ // #include "SDL_image.h" +#include "string.h" +#include "stdlib.h" #include "setup.h" #include "globals.h" #include "screen.h" @@ -18,4 +20,54 @@ #include "type.h" #include "scene.h" +void string_list_init(string_list_type* pstring_list) +{ +pstring_list->num_items = 0; +pstring_list->active_item = -1; +pstring_list->item = NULL; +} +char* string_list_active(string_list_type* pstring_list) +{ +if(pstring_list == NULL) +return ""; + +if(pstring_list->active_item != -1) +return pstring_list->item[pstring_list->active_item]; +else +return ""; +} + +void string_list_add_item(string_list_type* pstring_list, char* str) +{ +char *pnew_string; +pnew_string = (char*) malloc(sizeof(char)*(strlen(str)+1)); +strcpy(pnew_string,str); +++pstring_list->num_items; +pstring_list->item = (char**) realloc(pstring_list->item,sizeof(char**)*pstring_list->num_items); +pstring_list->item[pstring_list->num_items-1] = pnew_string; +if(pstring_list->active_item == -1) +pstring_list->active_item = 0; +} + +void string_list_copy(string_list_type* pstring_list, string_list_type pstring_list_orig) +{ +int i; +string_list_free(pstring_list); +for(i = 0; i < pstring_list_orig.num_items; ++i) +string_list_add_item(pstring_list,pstring_list_orig.item[i]); +} + +void string_list_free(string_list_type* pstring_list) +{ +if(pstring_list != NULL) +{ + int i; + for(i=0; i < pstring_list->num_items; ++i) + free(pstring_list->item[i]); +free(pstring_list->item); +pstring_list->item = NULL; +pstring_list->num_items = 0; +pstring_list->active_item = -1; +} +} diff --git a/src/type.h b/src/type.h index 0eb6d4227..b02a35064 100644 --- a/src/type.h +++ b/src/type.h @@ -29,5 +29,20 @@ typedef struct base_type } base_type; + +typedef struct string_list_type +{ + int num_items; + int active_item; + char **item; + } +string_list_type; + +void string_list_init(string_list_type* pstring_list); +char* string_list_active(string_list_type* pstring_list); +void string_list_copy(string_list_type* pstring_list, string_list_type pstring_list_orig); +void string_list_add_item(string_list_type* pstring_list, char* str); +void string_list_free(string_list_type* pstring_list); + #endif /*SUPERTUX_TYPE_H*/ -- 2.11.0