From a157c5358b28a8640d19bdd3da9ea2dd3427e90b Mon Sep 17 00:00:00 2001 From: Celtic Minstrel Date: Mon, 14 Apr 2014 17:06:50 -0400 Subject: [PATCH] Fix crash when talking to someone - Also make a lot of talk stuff use std::string instead of char[] --- osx/boe.dlgutil.cpp | 83 +++++++++++++++++++++----------------------- osx/boe.fileio.cpp | 4 +-- osx/boe.newgraph.cpp | 28 +++++++-------- osx/boe.newgraph.h | 4 +-- 4 files changed, 58 insertions(+), 61 deletions(-) diff --git a/osx/boe.dlgutil.cpp b/osx/boe.dlgutil.cpp index 1b3ce763..58b18422 100644 --- a/osx/boe.dlgutil.cpp +++ b/osx/boe.dlgutil.cpp @@ -74,10 +74,10 @@ eGameMode store_pre_talk_mode; short store_personality,store_personality_graphic,shop_identify_cost; sf::RenderTexture talk_gworld; bool talk_end_forced; -char old_str1[256],old_str2[255],one_back1[255],one_back2[255]; +std::string old_str1, old_str2, one_back1, one_back2; extern word_rect_type preset_words[9]; RECT talk_area_rect = {5,5,420,284}, word_place_rect = {44,7,372,257},talk_help_rect = {5,254,21,272}; -char title_string[50]; +std::string title_string; m_num_t store_monst_type; short store_m_num; RECT dummy_rect = {0,0,0,0}; @@ -166,13 +166,13 @@ void end_shop_mode() shop_sbar->hide(); if (store_pre_shop_mode == 20) { - strcpy((char *)old_str1,"You conclude your business."); - strcpy((char *)old_str2,""); - strcpy((char *)one_back1,"You conclude your business."); - strcpy((char *)one_back2,""); + old_str1 = "You conclude your business."; + old_str2 = ""; + one_back1 = "You conclude your business."; + one_back2 = ""; strnum1 = strnum2 = oldstrnum1 = oldstrnum2 = 0; - place_talk_str((char *)old_str1,"",0,dummy_rect); + place_talk_str(old_str1, "", 0, dummy_rect); } overall_mode = store_pre_shop_mode; @@ -511,8 +511,7 @@ void set_up_shop_array() void start_talk_mode(short m_num,short personality,m_num_t monst_type,short store_face_pic)//// { RECT area_rect; - char place_string1[256] = ""; - char place_string2[256] = ""; + std::string place_string1; store_personality = personality; @@ -523,15 +522,13 @@ void start_talk_mode(short m_num,short personality,m_num_t monst_type,short stor talk_gworld.create(area_rect.width(), area_rect.height()); // first make sure relevant talk strs are loaded in - if (personality / 10 != univ.town.cur_talk_loaded){ - load_town(personality / 10,univ.town.cur_talk); - } + load_town(personality / 10,univ.town.cur_talk); // load all possible responses store_responses(); // Dredge up critter's name - sprintf((char *) title_string,"%s:",univ.town.cur_talk->talk_strs[personality % 10]); + title_string = std::string(univ.town.cur_talk->talk_strs[personality % 10]) + ":"; store_pre_talk_mode = overall_mode; overall_mode = MODE_TALKING; @@ -539,15 +536,15 @@ void start_talk_mode(short m_num,short personality,m_num_t monst_type,short stor stat_screen_mode = 1; // Bring up and place first strings. - sprintf((char *) place_string1,"%s",univ.town.cur_talk->talk_strs[personality % 10 + 10]); + place_string1 = univ.town.cur_talk->talk_strs[personality % 10 + 10]; strnum1 = personality % 10 + 10; strnum2 = 0; - strcpy((char *) old_str1,(char *) place_string1); - strcpy((char *) old_str2,(char *) place_string2); - strcpy((char *) one_back1,(char *) place_string1); - strcpy((char *) one_back2,(char *) place_string2); - place_talk_str((char *) place_string1,(char *) place_string2,0,dummy_rect); + old_str1 = place_string1; + old_str2 = ""; + one_back1 = place_string1; + one_back2 = ""; + place_talk_str(place_string1, "", 0, dummy_rect); put_item_screen(stat_window,0); give_help(5,6); @@ -589,7 +586,7 @@ void handle_talk_event(location p) for (i = 0; i < 9; i++) if ((p.in(preset_words[i].word_rect)) && ((talk_end_forced == false) || (i == 6) || (i == 5))) { - click_talk_rect((char *) old_str1,(char *) old_str2,preset_words[i].word_rect); + click_talk_rect(old_str1,old_str2,preset_words[i].word_rect); switch (i) { case 0: case 1: case 2: case 7: case 8: force_special = i + 1; @@ -635,7 +632,7 @@ void handle_talk_event(location p) if (i < 100) { for (i = 0; i < 50; i++) if ((p.in(store_words[i].word_rect)) && (talk_end_forced == false)) { - click_talk_rect((char *) old_str1,(char *) old_str2,store_words[i].word_rect); + click_talk_rect(old_str1,old_str2,store_words[i].word_rect); for (j = 0; j < 4; j++) asked[j] = store_words[i].word[j]; @@ -677,11 +674,11 @@ void handle_talk_event(location p) oldstrnum1 = strnum1; oldstrnum2 = strnum2; strnum1 = store_personality % 10 + 10 * force_special; strnum2 = 0; - strcpy((char *) one_back1,(char *) old_str1); - strcpy((char *) one_back2,(char *) old_str2); - strcpy((char *) old_str1,place_string1.c_str()); - strcpy((char *) old_str2,place_string2.c_str()); - place_talk_str(place_string1.c_str(),place_string2.c_str(),0,dummy_rect); + one_back1 = old_str1; + one_back2 = old_str2; + old_str1 = place_string1; + old_str2 = place_string2; + place_talk_str(place_string1,place_string2,0,dummy_rect); return; break; case 4: // buy button @@ -710,11 +707,11 @@ void handle_talk_event(location p) strnum1 = oldstrnum1; strnum2 = oldstrnum2; place_string1 = one_back1; place_string2 = one_back2; - strcpy((char *) one_back1,(char *) old_str1); - strcpy((char *) one_back2,(char *) old_str2); - strcpy((char *) old_str1,place_string1.c_str()); - strcpy((char *) old_str2,place_string2.c_str()); - place_talk_str(place_string1.c_str(),place_string2.c_str(),0,dummy_rect); + one_back1 = old_str1; + one_back2 = old_str2; + old_str1 = place_string1; + old_str2 = place_string2; + place_talk_str(place_string1,place_string2,0,dummy_rect); return; break; } @@ -722,13 +719,13 @@ void handle_talk_event(location p) which_talk_entry = scan_for_response(asked); if ((which_talk_entry < 0) || (which_talk_entry > 59)) { - strcpy((char *) one_back1,(char *) old_str1); - strcpy((char *) one_back2,(char *) old_str2); - strcpy((char *) old_str2,""); - sprintf((char *) old_str1,"%s",univ.town.cur_talk->talk_strs[store_personality % 10 + 160]); - if (strlen((char *) old_str1) < 2) - strcpy((char *) old_str1,"You get no response."); - place_talk_str((char *) old_str1,(char *) old_str2,0,dummy_rect); + one_back1 = old_str1; + one_back2 = old_str2; + old_str2 = ""; + old_str1 = univ.town.cur_talk->talk_strs[store_personality % 10 + 160]; + if(old_str1.length() < 2) + old_str1 = "You get no response."; + place_talk_str(old_str1,old_str2,0,dummy_rect); strnum1 = -1; return; } @@ -1032,11 +1029,11 @@ void handle_talk_event(location p) } - strcpy((char *) one_back1,(char *) old_str1); - strcpy((char *) one_back2,(char *) old_str2); - strcpy((char *) old_str1,place_string1.c_str()); - strcpy((char *) old_str2,place_string2.c_str()); - place_talk_str((char *) old_str1,(char *) old_str2,0,dummy_rect); + one_back1 = old_str1; + one_back2 = old_str2; + old_str1 = place_string1; + old_str2 = place_string2; + place_talk_str(old_str1,old_str2,0,dummy_rect); } diff --git a/osx/boe.fileio.cpp b/osx/boe.fileio.cpp index ef956f01..dba43e89 100644 --- a/osx/boe.fileio.cpp +++ b/osx/boe.fileio.cpp @@ -177,7 +177,7 @@ void finish_load_party(){ else { load_town_str(univ.town.num,univ.town.record); load_town(univ.town.num,univ.town.record); - univ.town.cur_talk_loaded = univ.town.num; + univ.town.cur_talk_loaded = -1; for (int i = 0; i < univ.town->max_monst(); i++){ univ.town.monst[i].targ_loc.x = 0; @@ -542,7 +542,7 @@ void init_town(){ // formerly part of load_town } } -// univ.town.cur_talk_loaded = univ.town.town_num; + univ.town.cur_talk_loaded = -1; // } } diff --git a/osx/boe.newgraph.cpp b/osx/boe.newgraph.cpp index 9e1185f3..d92507a1 100644 --- a/osx/boe.newgraph.cpp +++ b/osx/boe.newgraph.cpp @@ -86,10 +86,10 @@ extern eGameMode store_pre_talk_mode; extern short store_personality,store_personality_graphic,current_pc; extern sf::RenderTexture talk_gworld; extern bool talk_end_forced; -extern char old_str1[256],old_str2[256],one_back1[256],one_back2[256]; +extern std::string old_str1,old_str2,one_back1,one_back2; extern word_rect_type preset_words[9]; extern RECT talk_area_rect, word_place_rect,talk_help_rect; -extern char title_string[50]; +extern std::string title_string; extern m_num_t store_monst_type; //extern hold_responses store_resp[83]; @@ -950,7 +950,7 @@ void refresh_shopping() } } -void click_talk_rect(char *str_to_place,char *str_to_place2,RECT c_rect) +void click_talk_rect(std::string str_to_place,std::string str_to_place2,RECT c_rect) { place_talk_str(str_to_place,str_to_place2,1,c_rect); @@ -1071,7 +1071,7 @@ void get_item_interesting_string(cItemRec item,char *message) } -void place_talk_str(const char *str_to_place,const char *str_to_place2,short color,RECT c_rect) +void place_talk_str(std::string str_to_place,std::string str_to_place2,short color,RECT c_rect) // color 0 - regular 1 - darker { RECT area_rect; @@ -1079,7 +1079,7 @@ void place_talk_str(const char *str_to_place,const char *str_to_place2,short col RECT face_rect = {6,6,38,38}; RECT title_rect = {19,48,42,260}; RECT dest_rect,help_from = {85,36,101,54}; - char str[356]; + std::string str; sf::Text str_to_draw; static const char fn2[] = "Dungeon Bold"; static const char fn3[] = "Palatino"; @@ -1151,10 +1151,10 @@ void place_talk_str(const char *str_to_place,const char *str_to_place2,short col TEXT.colour = c[3]; dest_rect = title_rect; dest_rect.offset(1,1); - win_draw_string(talk_gworld,dest_rect,title_string,2,18); + win_draw_string(talk_gworld,dest_rect,title_string.c_str(),2,18); dest_rect.offset(-1,-1); TEXT.colour = c[4]; - win_draw_string(talk_gworld,dest_rect,title_string,2,18); + win_draw_string(talk_gworld,dest_rect,title_string.c_str(),2,18); // Place buttons at bottom. if (color == 0) @@ -1172,11 +1172,11 @@ void place_talk_str(const char *str_to_place,const char *str_to_place2,short col for (i = 0; i < 50; i++) store_words[i].word_rect.left = store_words[i].word_rect.right = 0; - str_len = (short) strlen((char *)str_to_place); + str_len = str_to_place.length(); if (str_len == 0) { - sprintf((char *) str_to_place,"."); + str_to_place = "."; } - strcpy((char *) str,(char *) str_to_place); + str = str_to_place; for (i = 0; i < 257; i++) { text_len[i]= 0; char c = str[i]; @@ -1248,7 +1248,7 @@ void place_talk_str(const char *str_to_place,const char *str_to_place2,short col int end = last_word_break - last_line_break; char c = str[end]; str[end] = 0; - str_to_draw.setString(str + last_line_break); + str_to_draw.setString(str.substr(last_line_break)); str_to_draw.setPosition(moveTo); mainPtr.draw(str_to_draw); str[end] = c; @@ -1274,12 +1274,12 @@ void place_talk_str(const char *str_to_place,const char *str_to_place2,short col } // Now for string 2 - str_len = (short) strlen((char *)str_to_place2); + str_len = str_to_place2.length(); start_of_last_kept_word = -1; if (str_len > 0) { - strcpy((char *) str,str_to_place2); + str = str_to_place2; for (i = 0; i < 257; i++) { text_len[i]= 0; char c = str[i]; @@ -1343,7 +1343,7 @@ void place_talk_str(const char *str_to_place,const char *str_to_place2,short col int end = last_word_break - last_line_break; char c = str[end]; str[end] = 0; - str_to_draw.setString(str + last_line_break); + str_to_draw.setString(str.substr(last_line_break)); str_to_draw.setPosition(moveTo); mainPtr.draw(str_to_draw); str[end] = c; diff --git a/osx/boe.newgraph.h b/osx/boe.newgraph.h index aef1ed90..d5f3e6fc 100644 --- a/osx/boe.newgraph.h +++ b/osx/boe.newgraph.h @@ -43,8 +43,8 @@ cItemRec store_mage_spells(short which_s) ; cItemRec store_priest_spells(short which_s); cItemRec store_alchemy(short which_s); void get_item_interesting_string(cItemRec item,char *message); -void click_talk_rect(char *str_to_place,char *str_to_place2,RECT c_rect); -void place_talk_str(const char *str_to_place,const char *str_to_place2,short color,RECT c_rect); +void click_talk_rect(std::string str_to_place,std::string str_to_place2,RECT c_rect); +void place_talk_str(std::string str_to_place,std::string str_to_place2,short color,RECT c_rect); short scan_for_response(char *str); void refresh_talking(); void draw_dialog_graphic(sf::RenderTarget& target, RECT rect, short which_g, short type_g, bool do_frame);