From: Clyne Sullivan Date: Tue, 1 Mar 2016 13:43:56 +0000 (-0500) Subject: merge with remake X-Git-Url: https://code.bitgloo.com/?a=commitdiff_plain;h=47f8aa5b312a5ef671e83322bcbe201a034f84c0;p=clyne%2Fgamedev.git merge with remake --- 47f8aa5b312a5ef671e83322bcbe201a034f84c0 diff --cc Changelog index 9a9b927,b85c195..0384181 --- a/Changelog +++ b/Changelog @@@ -697,12 -697,3 +697,18 @@@ - C++-ified loaded texture handlers - documented more stuff - made merchant menu + +2/29/2016: +========== + + - renewed branch 'remake': re-wrote world.cpp and pushed + - fixed world linkage errors + - considered more formal coding? (as in documentation and indentation) + - fixed dialog boxes and options + - broke screenshots ++ ++3/1/2016: ++========= ++ ++ - merged 'remake' with 'master', fixed font issues and world stuffs ++ - continued work on merchant dialog diff --cc main.cpp index fd02cc1,fa833fb..abc4f3c --- a/main.cpp +++ b/main.cpp @@@ -457,14 -496,16 +483,17 @@@ void mainLoop(void) */ prev = currentWorld; - pool.Enqueue(ui::handleEvents); - //ui::handleEvents(); ++ ++ //pool.Enqueue(ui::handleEvents); + ui::handleEvents(); - + if(prev != currentWorld){ currentWorld->bgmPlay(prev); ui::dialogBoxExists = false; } - + if(prevPrevTime + MSEC_PER_TICK <= currentTime){ + //pool.Enqueue(logic); logic(); prevPrevTime = currentTime; } @@@ -472,9 -513,12 +501,13 @@@ /* * Update player and entity coordinates. */ - + + /*pool.Enqueue([](){ + currentWorld->update(player,deltaTime); + });*/ - currentWorld->update(player,deltaTime); + + currentWorld->update(player,deltaTime); + /* * Update debug variables if necessary */ diff --cc src/ui.cpp index 47110cc,89e4805..7029cd3 --- a/src/ui.cpp +++ b/src/ui.cpp @@@ -46,17 -45,25 +46,22 @@@ static unsigned char fontColor[3] = {25 /* * Variables for dialog boxes / options. - */ +*/ - static char dialogBoxText[512]; - static char *dialogOptText[4]; - static float dialogOptLoc[4][3]; + static char dialogBoxText[512]; + static char *dialogOptText[4]; + static float merchAOptLoc[2][3]; + static float dialogOptLoc[4][3]; static unsigned char dialogOptCount = 0; - static bool typeOutDone = true; + static bool typeOutDone = true; + + /* + * Menu-related objects + */ -extern Menu *currentMenu; +extern Menu* currentMenu; extern Menu pauseMenu; -/** - * The sound made when displaying characters with dialogBox or importantText. - */ static Mix_Chunk *dialogClick; @@@ -86,51 -108,74 +91,53 @@@ void Menu::gotoParent() } void Menu::gotoChild(){ - if(!child) + if(child == NULL){ currentMenu = NULL; - else + }else{ currentMenu = child; + } } -static vec2 premouse={0,0}; - namespace ui { - /** - * The current position of the mouse. - */ + /* + * Mouse coordinates. + */ vec2 mouse; + static vec2 premouse={0,0}; - /** - * If true, debug information will be drawn to the screen. - */ - - bool debug = false; - - /** - * If true, lines should be drawn to the player when the debug menu is open. - */ + /* + * Variety of keydown bools + */ + bool edown; + + /* + * Debugging flags. + */ + bool debug=false; bool posFlag=false; - - /** - * If true, the player will be able to move when the current dialog box is - * displayed. - */ - bool dialogPassive = false; + bool dialogMerchant = false; + std::vector *minv; int dialogPassiveTime = 0; - /** - * When set to true the dialog box will attempt to display. - */ - - bool dialogBoxExists = false; - /** - * When set to true the text will display as 'important' text. - */ + /* + * Dialog stuff that needs to be 'public'. + */ + bool dialogBoxExists = false; bool dialogImportant = false; - - /** - * Contains the last chosen dialog option. - */ - unsigned char dialogOptChosen = 0; - /** - * Determines how many characters can be displayed in a dialog box before - * a new line is required. - */ - unsigned int textWrapLimit = 110; - /** - * The current font size. - * - * DO NOT change this directly, use setFontSize() instead. - */ + /* + * Current font size. Changing this WILL NOT change the font size, see setFontSize() for + * actual font size changing. + */ unsigned int fontSize; @@@ -419,37 -534,29 +426,38 @@@ float putText(const float x,const float y,const char *str,...){ va_list args; - std::unique_ptr buf (new char[512]); + char *buf; + float width; - // create the formatted string - va_start(args, str); - vsnprintf(buf.get(), 512, str, args); + /* + * Create a wimpy buffer. + */ + + buf = new char[512]; //(char *)calloc(128,sizeof(char)); + memset(buf,0,512*sizeof(char)); + + /* + * Handle the formatted string, printing it to the buffer. + */ + + va_start(args,str); + vsnprintf(buf,512,str,args); va_end(args); - return putString(x, y, buf.get()); + /* + * Draw the string, free resources, return the width of the string. + */ + + width=putString(x,y,buf); + delete[] buf; //free(buf); + + return width; } - - /** - * Prints a character dialog box. - * - * This function sets up the variables necessary to draw a dialog box. If - * `opt` contains a valid string, options will be printed with the dialog - * box. If the box is passive, the player will be allowed to move while it - * is being displayed. - */ - void dialogBox(const char *name,const char *opt,bool passive,const char *text,...){ + textWrapLimit = 110; va_list dialogArgs; - size_t len; + unsigned int len; + char *sopt,*soptbuf; dialogPassive = passive; @@@ -511,9 -607,78 +519,77 @@@ dialogBoxExists = true; dialogImportant = false; - // kill the string created by typeOut if it contains something - if(typeOutStr) - *typeOutStr = '\0'; + if(ret) + ret[0] = '\0'; } + + void merchantBox(const char *name,std::vector *bsinv,const char *opt,bool passive,const char *text,...){ + std::cout << "Buying and selling on the bi-weekly!" << std::endl; + va_list dialogArgs; + size_t len; + + minv = bsinv; + dialogPassive = passive; + + // clear the buffer + memset(dialogBoxText, '\0', 512); + + // create the string + strcpy(dialogBoxText, name); + strcat(dialogBoxText, ": "); + + len=strlen(dialogBoxText); + va_start(dialogArgs,text); + vsnprintf(dialogBoxText + len, 512 - len, text, dialogArgs); + va_end(dialogArgs); + + // free old option text + while(dialogOptCount){ + if(dialogOptText[dialogOptCount]){ + delete[] dialogOptText[dialogOptCount]; + dialogOptText[dialogOptCount] = NULL; + } + + dialogOptCount--; + }; + + dialogOptChosen = 0; + memset(&dialogOptLoc, 0, sizeof(float) * 12); + + // handle options if desired + if(opt){ + //std::unique_ptr soptbuf (new char[strlen(opt) + 1]); + char soptbuf[255]; + strcpy(soptbuf, opt); + char *sopt = strtok(soptbuf, ":"); + + // cycle through options + while(sopt){ + strcpy( (dialogOptText[dialogOptCount++] = new char[strlen(sopt) + 1]), sopt); + sopt = strtok(NULL,":"); + } + } + + // allow box to be displayed + dialogBoxExists = true; + dialogImportant = false; + dialogMerchant = true; + textWrapLimit = 50; + + // kill the string created by typeOut if it contains something - if(typeOutStr) - *typeOutStr = '\0'; ++ if(ret) ++ *ret = '\0'; + } + + void merchantBox(){ + textWrapLimit = 50; + dialogMerchant = true; + } + + /** + * Wait for a dialog box to be dismissed. + */ + void waitForDialog(void){ do{ mainLoop(); @@@ -569,50 -751,123 +645,117 @@@ float x,y,tmp; char *rtext; - // handle dialog box / important text if(dialogBoxExists){ - rtext = typeOut(dialogBoxText); + rtext=typeOut(dialogBoxText); if(dialogImportant){ - setFontColor(255, 255, 255); - - // handle timeout - if(dialogPassive && (dialogPassiveTime -= deltaTime) <= 0){ - dialogPassive = false; - dialogImportant = false; - dialogBoxExists = false; + setFontColor(255,255,255); + if(dialogPassive){ + dialogPassiveTime -= deltaTime; + if(dialogPassiveTime < 0){ + dialogPassive = false; + dialogImportant = false; + dialogBoxExists = false; + } } - - // draw text if(fadeIntensity == 255 || dialogPassive){ setFontSize(24); - putStringCentered(offset.x, offset.y, rtext); + putStringCentered(offset.x,offset.y,rtext); setFontSize(16); } - }else{ - - x=offset.x-SCREEN_WIDTH/2+HLINE*8; + }else if(dialogMerchant){ + x=offset.x-SCREEN_WIDTH/6; y=(offset.y+SCREEN_HEIGHT/2)-HLINE*8; glColor3ub(255,255,255); + glBegin(GL_LINE_STRIP); + glVertex2f(x-1 ,y+1); + glVertex2f(x+1+(SCREEN_WIDTH/3),y+1); + glVertex2f(x+1+(SCREEN_WIDTH/3),y-1-SCREEN_HEIGHT*.6); + glVertex2f(x-1,y-1-SCREEN_HEIGHT*.6); + glVertex2f(x,y+1); + glEnd(); + + glColor3ub(0,0,0); + glRectf(x,y,x+SCREEN_WIDTH/3,y-SCREEN_HEIGHT*.6); + + // draw typeOut'd text + putString(x + HLINE, y - fontSize - HLINE, (rtext = typeOut(dialogBoxText))); + merchAOptLoc[0][0] = offset.x - (SCREEN_WIDTH / 6.5) - 16; + merchAOptLoc[0][1] = offset.x + (SCREEN_WIDTH / 6.5); + merchAOptLoc[1][0] = offset.y + (SCREEN_HEIGHT *.25); + merchAOptLoc[1][1] = offset.y + (SCREEN_HEIGHT *.25); + merchAOptLoc[2][0] = offset.x - (SCREEN_WIDTH / 6.5); + merchAOptLoc[2][1] = offset.x + (SCREEN_WIDTH / 6.5) + 16; + + for(i = 0; i < 2; i++){ + if(mouse.x > merchAOptLoc[0][i] && mouse.x < merchAOptLoc[2][i] && + mouse.y > merchAOptLoc[1][i] - 8 && mouse.y < merchAOptLoc[1][i] + 8){ + glColor3ub(255, 255, 0); + }else{ + glColor3ub(255,255,255); + } + } + + glBegin(GL_TRIANGLES); + glVertex2f(merchAOptLoc[0][0],merchAOptLoc[1][0]); + glVertex2f(merchAOptLoc[2][0],merchAOptLoc[1][0]-8); + glVertex2f(merchAOptLoc[2][0],merchAOptLoc[1][0]+8); + + glVertex2f(merchAOptLoc[2][1],merchAOptLoc[1][1]); + glVertex2f(merchAOptLoc[0][1],merchAOptLoc[1][1]-8); + glVertex2f(merchAOptLoc[0][1],merchAOptLoc[1][1]+8); + glEnd(); + + // draw / handle dialog options if they exist + for(i = 0; i < dialogOptCount; i++){ + setFontColor(255, 255, 255); + + // draw option + tmp = putStringCentered(offset.x, dialogOptLoc[i][1], dialogOptText[i]); + + // get coordinate information on option + dialogOptLoc[i][2] = offset.x + tmp; + dialogOptLoc[i][0] = offset.x - tmp; + dialogOptLoc[i][1] = y - SCREEN_HEIGHT / 2 - (fontSize + HLINE) * (i + 1); + + // make text yellow if the mouse hovers over the text + if(mouse.x > dialogOptLoc[i][0] && mouse.x < dialogOptLoc[i][2] && + mouse.y > dialogOptLoc[i][1] && mouse.y < dialogOptLoc[i][1] + 16 ){ + setFontColor(255, 255, 0); + putStringCentered(offset.x, dialogOptLoc[i][1], dialogOptText[i]); + } + } + + setFontColor(255, 255, 255); + }else{ //normal dialog box + + x=offset.x-SCREEN_WIDTH/2+HLINE*8; + y=(offset.y+SCREEN_HEIGHT/2)-HLINE*8; + + // draw white border + glColor3ub(255, 255, 255); ++ glBegin(GL_LINE_STRIP); - glVertex2f(x - 1 , y + 1 ); - glVertex2f(x + 1 + SCREEN_WIDTH - HLINE * 16, y + 1 ); - glVertex2f(x + 1 + SCREEN_WIDTH - HLINE * 16, y - 1 - SCREEN_HEIGHT / 4 ); - glVertex2f(x - 1 , y - 1 - SCREEN_HEIGHT / 4 ); - glVertex2f(x , y + 1 ); + glVertex2f(x-1 ,y+1); + glVertex2f(x+1+SCREEN_WIDTH-HLINE*16,y+1); + glVertex2f(x+1+SCREEN_WIDTH-HLINE*16,y-1-SCREEN_HEIGHT/4); + glVertex2f(x-1 ,y-1-SCREEN_HEIGHT/4); + glVertex2f(x ,y+1); glEnd(); - // draw black box - glColor3ub(0, 0, 0); - glRectf(x, y, x + SCREEN_WIDTH - HLINE * 16, y - SCREEN_HEIGHT / 4); + glColor3ub(0,0,0); + glRectf(x,y,x+SCREEN_WIDTH-HLINE*16,y-SCREEN_HEIGHT/4); - // draw typeOut'd text - putString(x + HLINE, y - fontSize - HLINE, (rtext = typeOut(dialogBoxText))); + rtext=typeOut(dialogBoxText); - // draw / handle dialog options if they exist - for(i = 0; i < dialogOptCount; i++){ - setFontColor(255, 255, 255); - - // draw option - tmp = putStringCentered(offset.x, dialogOptLoc[i][1], dialogOptText[i]); - - // get coordinate information on option + putString(x+HLINE,y-fontSize-HLINE,rtext); + + for(i=0;i dialogOptLoc[i][0] && mouse.x < dialogOptLoc[i][2] && - mouse.y > dialogOptLoc[i][1] && mouse.y < dialogOptLoc[i][1] + 16 ){ + for(i=0;i dialogOptLoc[i][0] && + mouse.x < dialogOptLoc[i][2] && + mouse.y > dialogOptLoc[i][1] && + mouse.y < dialogOptLoc[i][1] + 16 ){ // fontSize dialogOptChosen = i + 1; - break; + goto DONE; } } +DONE: + - if(dialogMerchant){ - for(i = 0; i < 2; i++){ - } - } + + // handle important text if(dialogImportant){ dialogImportant = false; setFontSize(16); diff --cc src/world.cpp index 4fa71d8,9e891fb..80ddc5a --- a/src/world.cpp +++ b/src/world.cpp @@@ -148,9 -118,10 +148,11 @@@ deleteEntities( void delete mob.back(); mob.pop_back(); } - - // free npcs - while ( !npc.empty() ) { ++ + while(!merchant.empty()){ + merchant.pop_back(); + } + while(!npc.empty()){ delete npc.back(); npc.pop_back(); } @@@ -187,28 -147,19 +189,27 @@@ delete village.back(); village.pop_back(); } - } -World::~World(void){ - /*if(behind != NULL) - delete behind;*/ - +/** + * The world destructor. + * + * This will free objects used by the world itself, then free the vectors of + * entity-related objects. + */ + +World:: +~World( void ) +{ + // sdl2_mixer's object if(bgmObj) Mix_FreeMusic(bgmObj); + + // bgm path if(bgm) delete[] bgm; + delete bgTex; - delete[] star; - delete[] line; delete[] toLeft; delete[] toRight; @@@ -810,22 -945,28 +811,21 @@@ singleDetect( Entity *e } } -void World::detect(Player *p){ - - /* - * Handle the player. - */ - - //auto pl = std::async(&World::singleDetect,this,p); - //std::thread(&World::singleDetect,this, p).detach(); - singleDetect(p); +void World:: +detect( Player *p ) +{ - + // handle the player + std::thread( &World::singleDetect, this, p).detach(); - /* - * Handle all remaining entities in this world. - */ - -//LOOOOP: - for(auto &e : entity) - //std::thread(&World::singleDetect,this,e).detach(); - singleDetect(e); - for(auto &part : particles){ + // handle other entities + for ( auto &e : entity ) + std::thread(&World::singleDetect,this,e).detach(); + + // handle particles + for ( auto &part : particles ) { int l; unsigned int i; - l=(part->loc.x + part->width / 2 - x_start) / HLINE; + l=(part->loc.x + part->width / 2 - worldStart) / HLINE; if(l < 0) l=0; i = l; if(i > lineCount-1) i=lineCount-1; @@@ -1337,12 -1537,18 +1345,12 @@@ World *loadWorldFromXMLNoSave(const cha bool dialog,Indoor; const char *ptr,*name; - - unsigned int size = 5 + strlen(path); + + currentXML = (std::string)"xml/" + path; - if(currentXML) - delete[] currentXML; - memset((currentXML = new char[size]),0,size); - strcpy(currentXML,"xml/"); - strcat(currentXML,path); - - xml.LoadFile(currentXML); + xml.LoadFile(currentXML.c_str()); wxml = xml.FirstChildElement("World"); - + if(wxml){ wxml = wxml->FirstChildElement(); vil = xml.FirstChildElement("World")->FirstChildElement("village");