diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2016-03-16 08:48:50 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2016-03-16 08:48:50 -0400 |
commit | 8dae5eeac2c6254bb8288c0479c193ab185a312f (patch) | |
tree | b9cac413e7da7b955c598ef39e61e6f2beecc124 | |
parent | 93d6035dad1eb60fb01648232809e55059dd1cfa (diff) |
string'd some stuff
-rw-r--r-- | Changelog | 9 | ||||
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | include/ui.h | 44 | ||||
-rw-r--r-- | include/world.h | 214 | ||||
-rw-r--r-- | main.cpp | 19 | ||||
-rw-r--r-- | src/ui.cpp | 473 | ||||
-rw-r--r-- | src/world.cpp | 73 |
7 files changed, 404 insertions, 429 deletions
@@ -770,3 +770,12 @@ - fixed player repositioning when switching worlds - XML'd tickCount setting - even more inventory stuffs (side panes) + +3/16/2016: +========== + + - cleaned up world and ui functions (std::string'ing) + - oh so more inventory stuffs (animations) + - planned ahead + + ~ set finish date for game engine: 3/31 @@ -38,6 +38,7 @@ cleandata: $(EXEC): $(CXXOUTDIR)/$(CXXOBJ) main.cpp @echo " CXX/LD main" @$(CXX) $(CXXFLAGS) $(CXXINC) $(CXXWARN) -o $(EXEC) main.cpp out/*.o $(LIBS) + @rm -rf xml/*.dat $(CXXOUTDIR)/%.o: $(CXXSRCDIR)/%.cpp @echo " CXX " $< diff --git a/include/ui.h b/include/ui.h index 9523269..072d418 100644 --- a/include/ui.h +++ b/include/ui.h @@ -99,12 +99,12 @@ namespace ui { /* * These flags are used elsewhere. */ - + extern unsigned int fontSize; - + extern bool debug; extern bool posFlag; - + extern unsigned char dialogOptChosen; extern bool dialogBoxExists; extern bool dialogImportant; @@ -117,54 +117,54 @@ namespace ui { */ void initFonts(void); - + void destroyFonts(void); - + /* * Sets the current font/font size. */ - + void setFontFace(const char *ttf); void setFontSize(unsigned int size); - + /* * Draw a centered string. */ - - float putStringCentered(const float x,const float y,const char *s); - + + float putStringCentered(const float x,const float y,std::string s); + /* * Draws a formatted string at the given coordinates. */ - + float putText(const float x,const float y,const char *str,...); - + /* * Creates a dialogBox text string (format: `name`: `text`). This function simply sets up * variables that are drawn in ui::draw(). When the dialog box exists player control is * limited until a right click is given, closing the box. */ - + void dialogBox(const char *name,const char *opt,bool passive,const char *text,...); void merchantBox(const char *name,Trade trade,const char *opt,bool passive,const char *text,...); void merchantBox(); void waitForDialog(void); - + void drawPage( std::string path ); - + /* * Draws a larger string in the center of the screen. Drawing is done inside this function. */ - + void importantText(const char *text,...); void passiveImportantText(int duration,const char *text,...); - + /* * Draw various UI elements (dialogBox, player health) */ - + void draw(void); - + /* * Draw various menu items @@ -177,17 +177,17 @@ namespace ui { * Handle keyboard/mouse events. */ void handleEvents(void); - + /* * Toggle the black overlay thing. */ - + void toggleBlack(void); void toggleBlackFast(void); void toggleWhite(void); void toggleWhiteFast(void); void waitForCover(void); - + } #endif // UI_H diff --git a/include/world.h b/include/world.h index 55c08f5..553cd8d 100644 --- a/include/world.h +++ b/include/world.h @@ -1,6 +1,6 @@ /** @file world.h * @brief The world system. - * + * * This file contains the classes and variables necessary to create an in-game * world. */ @@ -95,7 +95,7 @@ public: vec2 end; bool in; std::vector<Structures *> build; - + Village(const char *meme, World *w); ~Village(void){} }; @@ -106,112 +106,118 @@ public: class World { protected: + /** * The line array. + * * This array is created through 'new' in World::generate(), with an amount * of elements provided by the function. */ - + std::vector<WorldData> worldData; - + /** * Starting x coordinate. + * * This x value is the point at which line[0] should reside, can be used to * calculate the width of the world. */ - + int worldStart; - + /** * Handle physics for a single entity. + * * This function handles gravity and death for an entity. The public version * of this, World::detect(), handles all entities in the world as well as * the player. World::singleDetect() should never be used outside of * World::detect(), which is why it is declared private. */ - - void singleDetect(Entity *e); - + + void singleDetect( Entity *e ); + /** * Empties all entity vectors. + * * Each entity vector is iterated through, calling delete for each entry. * Once all specific vectors are cleared, the general entity vector is * emptied of the pointers to those other vectors. This function should only * be called in World's destructor, as there shouldn't be another reason to * call this function. */ - - void deleteEntities(void); - + + void deleteEntities( void ); + /** * Number of lines in the world. + * * While this number is helpful for knowing the world's width, it is kept * private for security reasons. To compensate for this, * World::getTheWidth() is provided (see below). */ - + unsigned int lineCount; - + /** * An array of star coordinates. */ - + std::vector<vec2> star; - + /** * The Texturec object that holds the background sprites for this world. */ - + Texturec *bgTex; /** * Defines the set of background images that should be used for this world. */ - + WorldBGType bgType; - + /** * The Mix_Music object that holds the background soundtrack for the world. */ - + Mix_Music *bgmObj; - + /** * The file path of the song wished to be loaded by bgmObj. */ - + std::string bgm; - - std::vector<std::string>bgFiles; - std::vector<std::string>bgFilesIndoors; - + + std::vector<std::string> bgFiles; + std::vector<std::string> bgFilesIndoors; + public: /** * The filename of the XML file for the world to the left; NULL if no world * is present. */ - - char *toLeft; - + + std::string toLeft; + /** * The filename of the XML file for the world to the right; NULL if no world * is present. */ - - char *toRight; - + + std::string toRight; + /** * Sets what XML file to use for loading the world to the left. */ - - char *setToLeft(const char *file); - + + std::string setToLeft( std::string file ); + /** * Sets what XML file to use for loading the world to the right. */ - - char *setToRight(const char *file); + + std::string setToRight( std::string file ); /** * A vector of pointers to every NPC, Structure, Mob, and Object in this @@ -226,38 +232,38 @@ public: std::vector<NPC *> npc; std::vector<Merchant *> merchant; - + /** * A vector of all Structures in this world. */ - + std::vector<Structures *> build; - + /** * A vector of all Mobs in this world. */ - + std::vector<Mob *> mob; - + /** * A vector of all Objects in this world. */ - + std::vector<Object *> object; - + /** * A vector of all particles in this world. */ - + std::vector<Particles> particles; - - + + std::vector<Village *> village; - + /** * A vector of all light elements in this world. */ - + std::vector<Light> light; /** @@ -265,175 +271,175 @@ public: */ std::vector<std::string> sTexLoc; - + /** * NULLifies pointers and allocates necessary memory. This should be * followed by some combination of setBackground(), setBGM(), or * generate(). */ - + World( void ); - + /** * Frees resources taken by the world. */ - + virtual ~World(void); - + /** * Adds a structure to the world, with the specified subtype and * coordinates. `inside` is a file name for the IndoorWorld XML file that * this structure will lead to; if NULL the player won't be able to enter * the structure. */ - + void addStructure(BUILD_SUB subtype,float x,float y, std::string tex, std::string inside); - + /** * Adds a Mob to the world with the specified type and coordinates. */ - + void addMob(int type,float x,float y); - + /** * Adds a Mob to the world with a handler function that can be called by * certain mobs to trigger events. */ - + void addMob(int t,float x,float y,void (*hey)(Mob *)); - + /** * Adds an NPC to the world with the specified coordinates. */ - + void addNPC(float x,float y); - + /** * Adds a Merchant to the world at the specified coordinates. */ - + void addMerchant(float x, float y); - + /** * Adds an object to the world with the specified item id and coordinates. * If `pickupDialog` is not NULL, that string will display in a dialog box * upon object interaction. */ - + void addObject( std::string in, std::string pickupDialog, float x, float y); - + /** * Adds a particle to the world with the specified coordinates, dimensions, * velocity, color and duration (time to live). */ - + void addParticle(float x, float y, float w, float h, float vx, float vy, Color color, int duration); - + /** * Adds a light to the world with the specified coordinates and color. */ - + void addLight(vec2 xy, Color color); - + /** * Updates the coordinates of everything in the world that has coordinates * and a velocity. The provided delta time is used for smoother updating. */ - - void update(Player *p,unsigned int delta); - + + void update( Player *p, unsigned int delta ); + /** * Generate a world of the provided width. Worlds are drawn centered on the * y-axis, so the reachable coordinates on the world would be from negative * half-width to positive half-width. */ - + virtual void generate(unsigned int width); - + /** * Sets the background theme, collecting the required textures into a * Texturec object. */ - + void setBackground(WorldBGType bgt); - + /** * Sets the background music for the world, required for the world to be * playable. */ - + void setBGM(std::string path); /** * Sets the worlds style folder */ - void setStyle(std::string pre); - + void setStyle(std::string pre); + /** * Plays/stops this world's BGM. If `prev` is not NULL, that world's BGM * will be faded out followed by the fading in of this world's BGM. */ - + void bgmPlay(World *prev) const; - + /** * Draw the world and entities based on the player's coordinates. */ virtual void draw(Player *p); - + /** * Handles collision between the entities and the world, as well as entity * death. */ - + void detect(Player *p); - + /** * Attempts to let the player enter the left-linked world specified by * `toLeft`. Returns the world to the left if the movement is possible, * otherwise returns this world. */ - + World *goWorldLeft(Player *p); - + /** * Attempts to let the player enter the right-linked world specified by * `toRight`. Returns the world to the right if the movement is possible, * otherwise returns this world. */ - + World *goWorldRight(Player *p); - + /** * This function looks for any structure the player is standing in front of * that also have an inside world. Returns the inside world if those * conditions are met, otherwise returns this world. */ - + World *goInsideStructure(Player *p); - + /** * Adds a hole between the specified y coordinates. If the player falls in * this hole the game will exit. */ - + void addHole(unsigned int start,unsigned int end); - + /** * Adds a hill to the world, given the peak's coordinates and how wide the * hill can be. */ - + void addHill( ivec2 peak, unsigned int width ); - + /** * Gets the world's width. */ - + int getTheWidth(void) const; - + void save(void); void load(void); }; @@ -441,19 +447,19 @@ public: /* * IndoorWorld - Indoor settings stored in a World class */ - + class IndoorWorld : public World { public: IndoorWorld(void); ~IndoorWorld(void); - + void generate(unsigned int width); // Generates a flat world of width 'width' void draw(Player *p); // Draws the world (ignores layers) }; /** * The arena class - creates an arena. - * + * * This world, when created, expects a pointer to a Mob. This mob will be * transported to a temporary world with the player, and the Mob will be * killed upon exiting the arena. @@ -476,18 +482,18 @@ public: */ Arena( World *leave, Player *p, Mob *m ); - + /** * Frees resources taken by the arena. */ - + ~Arena( void ); - + /** * Attempts to exit the world, returning the player to the world they were * last in. */ - + World *exitArena( Player *p ); }; @@ -415,9 +415,9 @@ int main(int argc, char *argv[]){ //currentWorld->mob.back()->followee = player; gameRunning = true; - while(gameRunning){ + + while ( gameRunning ) mainLoop(); - } /************************** **** CLOSE PROGRAM **** @@ -431,7 +431,7 @@ int main(int argc, char *argv[]){ Mix_CloseAudio(); destroyInventory(); - ui::destroyFonts(); + ui::destroyFonts(); Texture::freeTextures(); SDL_GL_DeleteContext(mainGLContext); @@ -478,7 +478,6 @@ void mainLoop(void){ prev = currentWorld; - //pool.Enqueue(ui::handleEvents); ui::handleEvents(); @@ -493,15 +492,7 @@ void mainLoop(void){ prevPrevTime = currentTime; } - /* - * Update player and entity coordinates. - */ - - /*pool.Enqueue([](){ - currentWorld->update(player,deltaTime); - });*/ - - currentWorld->update(player,deltaTime); + currentWorld->update( player, deltaTime ); /* * Update debug variables if necessary @@ -512,7 +503,7 @@ void mainLoop(void){ if ( deltaTime ) fps = 1000 / deltaTime; - if(!(debugDiv%10)) + if(!(debugDiv % 10)) debugY = player->loc.y; } MENU: @@ -6,7 +6,7 @@ #define SDL_KEY e.key.keysym.sym -extern std::vector<menuItem>optionsMenu; +extern std::vector<menuItem> optionsMenu; extern SDL_Window *window; @@ -102,13 +102,13 @@ void Menu::gotoChild(){ } namespace ui { - + /* * Mouse coordinates. */ - + vec2 mouse; - static vec2 premouse={0,0}; + static vec2 premouse={0,0}; /* * Variety of keydown bools @@ -118,7 +118,7 @@ namespace ui { /* * Debugging flags. */ - + bool debug=false; bool posFlag=false; bool dialogPassive = false; @@ -126,23 +126,23 @@ namespace ui { int dialogPassiveTime = 0; Trade merchTrade; - + /* * Dialog stuff that needs to be 'public'. */ - + bool dialogBoxExists = false; bool dialogImportant = false; unsigned char dialogOptChosen = 0; unsigned char merchOptChosen = 0; - + unsigned int textWrapLimit = 110; - + /* * Current font size. Changing this WILL NOT change the font size, see setFontSize() for * actual font size changing. */ - + unsigned int fontSize; /* @@ -164,20 +164,20 @@ namespace ui { sanic = Mix_LoadWAV("assets/sounds/sanic.wav"); //Mix_Volume(1,50); } - + void destroyFonts(void){ FT_Done_Face(ftf); FT_Done_FreeType(ftl); - + Mix_FreeChunk(dialogClick); Mix_FreeChunk(battleStart); Mix_FreeChunk(sanic); } - + /* * Sets a new font family to use (*.ttf). */ - + void setFontFace(const char *ttf){ if(FT_New_Face(ftl,ttf,0,&ftf)){ std::cout<<"Error! Couldn't open "<<ttf<<"."<<std::endl; @@ -187,55 +187,55 @@ namespace ui { DEBUG_printf("Using font %s\n",ttf); #endif // DEBUG } - + /* * Sets a new font size (default: 12). */ - + void setFontSize(unsigned int size){ unsigned int i,j; unsigned char *buf; - + fontSize=size; FT_Set_Pixel_Sizes(ftf,0,fontSize); - + /* * Pre-render 'all' the characters. */ - + glDeleteTextures(93,ftex); // delete[] any already-rendered textures glGenTextures(93,ftex); // Generate new texture name/locations? - + for(i=33;i<126;i++){ - + /* * Load the character from the font family file. */ - + if(FT_Load_Char(ftf,i,FT_LOAD_RENDER)){ std::cout<<"Error! Unsupported character "<<(char)i<<" ("<<i<<")."<<std::endl; abort(); } - + /* * Transfer the character's bitmap (?) to a texture for rendering. */ - + glBindTexture(GL_TEXTURE_2D,ftex[i-33]); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S ,GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T ,GL_CLAMP_TO_EDGE); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER ,GL_LINEAR ); glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER ,GL_LINEAR ); glPixelStorei(GL_UNPACK_ALIGNMENT,1); - + /* * The just-created texture will render red-on-black if we don't do anything to it, so * here we create a buffer 4 times the size and transform the texture into an RGBA array, * making it white-on-black. */ - + buf = new unsigned char[ftf->glyph->bitmap.width * ftf->glyph->bitmap.rows * 4]; - + for(j=0;j<ftf->glyph->bitmap.width*ftf->glyph->bitmap.rows;j++){ buf[j*4 ]=255;//fontColor[0]; buf[j*4+1]=255;//fontColor[1]; @@ -243,51 +243,51 @@ namespace ui { buf[j*4+3]=ftf->glyph->bitmap.buffer[j] ? 255 : 0; //buf[j*4+3]=ftf->glyph->bitmap.buffer[j]; } - + ftexwh[i-33].x=ftf->glyph->bitmap.width; ftexwh[i-33].y=ftf->glyph->bitmap.rows; ftexbl[i-33].x=ftf->glyph->bitmap_left; ftexbl[i-33].y=ftf->glyph->bitmap_top; ftexad[i-33].x=ftf->glyph->advance.x>>6; ftexad[i-33].y=ftf->glyph->advance.y>>6; - - glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,ftf->glyph->bitmap.width,ftf->glyph->bitmap.rows,0,GL_RGBA,GL_UNSIGNED_BYTE,buf); - + + glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,ftf->glyph->bitmap.width,ftf->glyph->bitmap.rows,0,GL_RGBA,GL_UNSIGNED_BYTE,buf); + delete[] buf; //free(buf); } } - + /* * Set a color for font rendering (default: white). */ - + void setFontColor(unsigned char r,unsigned char g,unsigned char b){ fontColor[0]=r; fontColor[1]=g; fontColor[2]=b; } - + /* * Draws a character at the specified coordinates, aborting if the character is unknown. */ - + vec2 putChar(float xx,float yy,char c){ vec2 c1,c2; int x = xx, y = yy; - + /* * Get the width and height of the rendered character. */ - + c1={(float)floor(x)+ftexbl[c-33].x, (float)floor(y)+ftexbl[c-33].y}; c2=ftexwh[c-33]; - + /* * Draw the character: */ - + glEnable(GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D,ftex[c-33]); glPushMatrix(); @@ -301,169 +301,168 @@ namespace ui { glEnd(); glPopMatrix(); glDisable(GL_TEXTURE_2D); - + /* * return the width. */ - + return ftexad[c-33];//(vec2){c2.x,ftexad[c-33].y}; } - + /* * Draw a string at the specified coordinates. */ - - float putString(const float x,const float y,const char *s){ + + float putString( const float x, const float y, std::string s ) { unsigned int i=0; - float xo=x,yo=y; - vec2 add; - //vec2 off = { (float)floor(x), (float)floor(y) }; - + vec2 add, o = {x, y}; + /* * Loop on each character: */ - - do{ + + do { if(i && ((i / 110.0) == (i / 110))){ - yo-=fontSize*1.05; - xo=x; + o.y -= fontSize * 1.05f; + o.x = x; if(s[i] == ' ') i++; } + if(i && (i / (float)textWrapLimit == i / textWrapLimit)){ - yo -= fontSize * 1.05; - xo = x; - + o.y -= fontSize * 1.05f; + o.x = x; + // skip a space if it's there since we just newline'd if(s[i] == ' ') i++; } - if(s[i] == '\n'){ - yo-=fontSize*1.05; - xo=x; - }else if(s[i] == '\r' || s[i] == '\t'){ - /*if(s[i] == '\n'){ - yo-=fontSize*1.05; - xo=x; - */}else if(s[i]==' '){ // Handle spaces - xo+=fontSize/2; - }else if(s[i]=='\b'){ // Handle backspaces? - xo-=add.x; - }else{ - add=putChar(floor(xo),floor(yo),s[i]); - xo+=add.x; - yo+=add.y; + + switch ( s[i] ) { + case '\n': + o.y -= fontSize * 1.05f; + o.x = x; + break; + case '\r': + break; + case '\t': + break; + case '\b': + o.x -= add.x; + break; + case ' ': + o.x += fontSize / 2; + break; + default: + add = putChar( floor(o.x), floor(o.y), s[i] ); + o.x += add.x; + o.y += add.y; + break; } + }while(s[++i]); - - return xo; // i.e. the string width + + return o.x; // i.e. the string width } - - float putStringCentered(const float x,const float y,const char *s){ + + float putStringCentered( const float x, const float y, std::string s ) { unsigned int i = 0; float width = 0; - - do{ - if(s[i]=='\n'){ // Handle newlines + + do { + switch ( s[i] ) { + case '\n': // TODO - }else if(s[i]==' '){ // Handle spaces - width+=fontSize/2; - }else if(s[i]=='\b'){ // Handle backspaces? - // Why? - // Cuz - }else{ - width+=ftexwh[i].x+fontSize*.1; + break; + case '\b': + break; + case ' ': + width += fontSize / 2; + break; + default: + width += ftexwh[i].x + fontSize * 0.1f; + break; } - }while(s[++i]); - + } while(s[++i]); + putString(floor(x-width/2),y,s); return width; } - + /* * Draw a string in a typewriter-esque fashion. Each letter is rendered as calls are made * to this function. Passing a different string to the function will reset the counters. */ - static char *ret = NULL; - char *typeOut(char *str){ + std::string ret; + std::string typeOut(char *str){ static unsigned int sinc, // Acts as a delayer for the space between each character. linc=0, // Contains the number of letters that should be drawn. size=0; // Contains the full size of the current string. - //static char *ret = NULL; - - /* - * Create a well-sized buffer if we haven't yet. - */ - - if(!ret){ - ret = new char[512]; //(char *)calloc(512,sizeof(char)); - memset(ret,0,512*sizeof(char)); - } - + /* * Reset values if a new string is being passed. */ - - if(strncmp(ret,str,linc-1)){ - memset(ret,0,512); // Zero the buffer + + if(strncmp(ret.c_str(),str,linc-1)){ + ret.clear(); // Zero the buffer size=strlen(str); // Set the new target string size linc=0; // Reset the incrementers sinc=1; typeOutDone = false; } - + /* * Draw the next letter if necessary. */ - + if(typeOutDone) return str; else if(++sinc==2){ sinc=0; - - strncpy(ret+linc,str+linc,1); // Get next character - + + ret.append( 1, *(str + linc) ); + if(linc<size) linc++; else typeOutDone = true; } - + return ret; // The buffered string. } - + /* * Draw a formatted string to the specified coordinates. */ - + float putText(const float x,const float y,const char *str,...){ va_list args; char *buf; float width; - + /* * 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); - + /* * Draw the string, free resources, return the width of the string. */ - + width=putString(x,y,buf); delete[] buf; //free(buf); - + return width; } void dialogBox(const char *name,const char *opt,bool passive,const char *text,...){ @@ -471,32 +470,32 @@ namespace ui { va_list dialogArgs; unsigned int len; char *sopt,*soptbuf; - + dialogPassive = passive; - + /* * Set up the text buffer. */ - + memset(dialogBoxText,0,512); - + /* * Get the text ready for rendering. */ - + len=strlen(name); strcpy(dialogBoxText ,name); strcpy(dialogBoxText+len,": "); len+=2; - + va_start(dialogArgs,text); vsnprintf(dialogBoxText+len,512-len,text,dialogArgs); va_end(dialogArgs); - + /* * Set up option text. */ - + while(dialogOptCount){ if(dialogOptText[dialogOptCount]){ delete[] dialogOptText[dialogOptCount]; //free(dialogOptText[dialogOptCount]); @@ -508,32 +507,31 @@ namespace ui { dialogOptCount = 0; dialogOptChosen = 0; memset(&dialogOptLoc,0,sizeof(float)*12); - + if(opt != NULL){ - + soptbuf = new char[strlen(opt)+1]; strcpy(soptbuf,opt); - + sopt=strtok(soptbuf,":"); while(sopt != NULL){ dialogOptText[dialogOptCount] = new char[strlen(sopt)+1]; //(char *)malloc(strlen(sopt)); strcpy(dialogOptText[dialogOptCount++],sopt); sopt=strtok(NULL,":"); } - + delete[] soptbuf; } - + /* - * Tell draw() that the box is ready. + * Tell draw() that the box is ready. */ - + dialogBoxExists = true; dialogImportant = false; - - if(ret) - ret[0] = '\0'; + + ret.clear(); } @@ -541,38 +539,38 @@ namespace ui { std::cout << "Buying and selling on the bi-weekly!" << std::endl; va_list dialogArgs; size_t len; - + dialogPassive = passive; std::cout << "Market Trading: " << trade.quantity[0] << " " << trade.item[0] << " for " << trade.quantity[1] << " " << trade.item[1] << std::endl; merchTrade = trade; - + // 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<char[]> soptbuf (new char[strlen(opt) + 1]); @@ -586,27 +584,25 @@ namespace ui { 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(ret) - *ret = '\0'; + + ret.clear(); } - + void merchantBox(){ textWrapLimit = 50; dialogMerchant = true; } - + /** * Wait for a dialog box to be dismissed. */ - + void waitForDialog(void){ do{ mainLoop(); @@ -664,10 +660,10 @@ namespace ui { void draw(void){ unsigned char i; float x,y,tmp; - char *rtext; - + std::string rtext; + if ( pageTex ) { - + glEnable( GL_TEXTURE_2D); glBindTexture( GL_TEXTURE_2D, pageTex ); glBegin( GL_QUADS ); @@ -677,11 +673,11 @@ namespace ui { glTexCoord2i( 0, 1 ); glVertex2i( offset.x - 300, SCREEN_HEIGHT - 600 ); glEnd(); glDisable( GL_TEXTURE_2D); - + } else if (dialogBoxExists){ - + rtext=typeOut(dialogBoxText); - + if(dialogImportant){ setFontColor(255,255,255); if(dialogPassive){ @@ -694,7 +690,7 @@ namespace ui { } if(fadeIntensity == 255 || dialogPassive){ setFontSize(24); - putStringCentered(offset.x,offset.y,rtext); + putStringCentered(offset.x,offset.y,rtext.c_str()); setFontSize(16); } }else if(dialogMerchant){ @@ -702,8 +698,8 @@ namespace ui { 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); @@ -712,10 +708,10 @@ namespace ui { glVertex2f(x-1,y-1-SCREEN_HEIGHT*.6); glVertex2f(x - 1,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))); @@ -759,8 +755,8 @@ namespace ui { merchAOptLoc[1][2] = offset.x + (SCREEN_WIDTH / 8.5); for(i = 0; i < 2; i++){ - if(((merchAOptLoc[i][0] < merchAOptLoc[i][2]) ? - (mouse.x > merchAOptLoc[i][0] && mouse.x < merchAOptLoc[i][2]) : + if(((merchAOptLoc[i][0] < merchAOptLoc[i][2]) ? + (mouse.x > merchAOptLoc[i][0] && mouse.x < merchAOptLoc[i][2]) : (mouse.x < merchAOptLoc[i][0] && mouse.x > merchAOptLoc[i][2])) && mouse.y > merchAOptLoc[i][1] - 8 && mouse.y < merchAOptLoc[i][1] + 8){ glColor3ub(255,255, 0); @@ -774,19 +770,19 @@ namespace ui { 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 ){ @@ -794,13 +790,13 @@ namespace ui { 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); @@ -811,14 +807,14 @@ namespace ui { glVertex2i(x-1 ,y-1-SCREEN_HEIGHT/4); glVertex2i( x - 1, y + 1 ); glEnd(); - + glColor3ub(0,0,0); glRectf(x,y,x+SCREEN_WIDTH-HLINE*16,y-SCREEN_HEIGHT/4); - + rtext=typeOut(dialogBoxText); - + putString(x+HLINE,y-fontSize-HLINE,rtext); - + for(i=0;i<dialogOptCount;i++){ setFontColor(255,255,255); tmp = putStringCentered(offset.x,dialogOptLoc[i][1],dialogOptText[i]); @@ -835,17 +831,16 @@ namespace ui { } setFontColor(255,255,255); } - - if(strcmp(rtext,dialogBoxText)){ + + if ( rtext != dialogBoxText ) Mix_PlayChannel(1,dialogClick,0); - } - + }if(!fadeIntensity){ vec2 hub = { (SCREEN_WIDTH/2+offset.x)-fontSize*10, (offset.y+SCREEN_HEIGHT/2)-fontSize }; - + putText(hub.x,hub.y,"Health: %u/%u",player->health>0?(unsigned)player->health:0, (unsigned)player->maxHealth ); @@ -862,21 +857,21 @@ namespace ui { hub.x+(player->health/player->maxHealth * 150), hub.y+12); } - + /* * Lists all of the quests the player is currently taking. */ - + if(player->inv->invOpen){ hub.y = player->loc.y + fontSize * 8; hub.x = player->loc.x;// + player->width / 2; - + putStringCentered(hub.x,hub.y,"Current Quests:"); - + for(auto &c : player->qh.current){ hub.y -= fontSize * 1.15; putStringCentered(hub.x,hub.y,c.title.c_str()); - } + } } } } @@ -889,7 +884,7 @@ namespace ui { updateConfig(); saveConfig(); } - + menuItem createButton(vec2 l, dim2 d, Color c, const char* t, menuFunc f){ menuItem temp; temp.member = 0; @@ -962,7 +957,7 @@ namespace ui { setFontSize(24); updateConfig(); SDL_Event e; - + mouse.x=premouse.x+offset.x-(SCREEN_WIDTH/2); mouse.y=(offset.y+SCREEN_HEIGHT/2)-premouse.y; @@ -998,15 +993,15 @@ namespace ui { //draw the button background glColor3f(m.button.color.red,m.button.color.green,m.button.color.blue); - glRectf(offset.x+m.button.loc.x, - offset.y+m.button.loc.y, - offset.x+m.button.loc.x + m.button.dim.x, + glRectf(offset.x+m.button.loc.x, + offset.y+m.button.loc.y, + offset.x+m.button.loc.x + m.button.dim.x, offset.y+m.button.loc.y + m.button.dim.y); //draw the button text putStringCentered(offset.x + m.button.loc.x + (m.button.dim.x/2), (offset.y + m.button.loc.y + (m.button.dim.y/2)) - ui::fontSize/2, m.button.text); - + //tests if the mouse is over the button if(mouse.x >= offset.x+m.button.loc.x && mouse.x <= offset.x+m.button.loc.x + m.button.dim.x){ if(mouse.y >= offset.y+m.button.loc.y && mouse.y <= offset.y+m.button.loc.y + m.button.dim.y){ @@ -1061,9 +1056,9 @@ namespace ui { } //draw the background of the slider glColor4f(m.slider.color.red,m.slider.color.green,m.slider.color.blue, .5f); - glRectf(offset.x+m.slider.loc.x, - offset.y+m.slider.loc.y, - offset.x+m.slider.loc.x + m.slider.dim.x, + glRectf(offset.x+m.slider.loc.x, + offset.y+m.slider.loc.y, + offset.x+m.slider.loc.x + m.slider.dim.x, offset.y+m.slider.loc.y + m.slider.dim.y); //draw the slider handle @@ -1084,7 +1079,7 @@ namespace ui { //draw the now combined slider text putStringCentered(offset.x + m.slider.loc.x + (m.slider.dim.x/2), (offset.y + m.slider.loc.y + (m.slider.dim.y/2)) - ui::fontSize/2, outSV); - } + } //test if mouse is inside of the slider's borders if(mouse.x >= offset.x+m.slider.loc.x && mouse.x <= offset.x+m.slider.loc.x + m.slider.dim.x){ if(mouse.y >= offset.y+m.slider.loc.y && mouse.y <= offset.y+m.slider.loc.y + m.slider.dim.y){ @@ -1123,18 +1118,18 @@ namespace ui { *m.slider.var = (((mouse.y-offset.y) - m.slider.loc.y)/m.slider.dim.y)*100; //draw a white box over the handle glColor3f(1.0f,1.0f,1.0f); - glRectf(offset.x+m.slider.loc.x, - offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05), - offset.x+m.slider.loc.x + sliderW, + glRectf(offset.x+m.slider.loc.x, + offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05), + offset.x+m.slider.loc.x + sliderW, offset.y+m.slider.loc.y + (m.slider.sliderLoc * 1.05) + sliderH); }else{ *m.slider.var = (((mouse.x-offset.x) - m.slider.loc.x)/m.slider.dim.x)*100; //draw a white box over the handle glColor3f(1.0f,1.0f,1.0f); - glRectf(offset.x+m.slider.loc.x + m.slider.sliderLoc, - offset.y+m.slider.loc.y, - offset.x+m.slider.loc.x + (m.slider.sliderLoc + sliderW), + glRectf(offset.x+m.slider.loc.x + m.slider.sliderLoc, + offset.y+m.slider.loc.y, + offset.x+m.slider.loc.x + (m.slider.sliderLoc + sliderW), offset.y+m.slider.loc.y + m.slider.dim.y); } } @@ -1151,7 +1146,7 @@ namespace ui { void takeScreenshot(GLubyte* pixels){ std::vector<GLubyte> bgr (SCREEN_WIDTH * SCREEN_HEIGHT * 3, 0); - + for(uint x = 0; x < SCREEN_WIDTH*SCREEN_HEIGHT*3; x+=3){ bgr[x] = pixels[x+2]; bgr[x+1] = pixels[x+1]; @@ -1211,19 +1206,19 @@ namespace ui { fwrite(&bgr, 1,3*SCREEN_WIDTH*SCREEN_HEIGHT,bmp); delete[] pixels; - + fclose(bmp); } void dialogAdvance(void){ unsigned char i; - + if ( pageTex ) { glDeleteTextures( 1, &pageTex ); pageTex = 0; return; } - + if(!typeOutDone){ typeOutDone = true; return; @@ -1239,7 +1234,7 @@ namespace ui { } } DONE: - + // handle important text if(dialogImportant){ dialogImportant = false; @@ -1256,25 +1251,25 @@ DONE: World *tmp; vec2 oldpos,tmppos; SDL_Event e; - + // update mouse coords mouse.x = premouse.x + offset.x - ( SCREEN_WIDTH / 2 ); mouse.y = ( offset.y + SCREEN_HEIGHT / 2 ) - premouse.y; - + while(SDL_PollEvent(&e)){ switch(e.type){ - + // escape - quit game case SDL_QUIT: gameRunning=false; break; - + // mouse movement - update mouse vector case SDL_MOUSEMOTION: premouse.x=e.motion.x; premouse.y=e.motion.y; break; - + // mouse clicks case SDL_MOUSEBUTTONDOWN: // right click advances dialog @@ -1284,10 +1279,10 @@ DONE: if ( ( e.button.button & SDL_BUTTON_LEFT ) && !dialogBoxExists ) player->inv->usingi = true; break; - + // key presses case SDL_KEYDOWN: - + // space - make player jump if ( SDL_KEY == SDLK_SPACE ) { if ( player->ground ) { @@ -1311,16 +1306,16 @@ DONE: player->right = false; left = true; right = false; - if(currentWorld->toLeft){ + if ( !currentWorld->toLeft.empty() ) { oldpos = player->loc; if((tmp = currentWorld->goWorldLeft(player)) != currentWorld){ tmppos = player->loc; player->loc = oldpos; - + toggleBlackFast(); waitForCover(); player->loc = tmppos; - + currentWorld = tmp; toggleBlackFast(); } @@ -1333,16 +1328,16 @@ DONE: player->left = false; left = false; right = true; - if(currentWorld->toRight){ + if ( !currentWorld->toRight.empty() ) { oldpos = player->loc; if((tmp = currentWorld->goWorldRight(player)) != currentWorld){ tmppos = player->loc; player->loc = oldpos; - + toggleBlackFast(); waitForCover(); player->loc = tmppos; - + currentWorld = tmp; toggleBlackFast(); } @@ -1351,37 +1346,17 @@ DONE: case SDLK_s: break; case SDLK_w: - if(inBattle){ + if ( inBattle ) { tmp = currentWorld; - currentWorld = ((Arena *)currentWorld)->exitArena(player); - if(tmp != currentWorld){ - //delete &tmp; + currentWorld = ((Arena *)currentWorld)->exitArena( player ); + if ( tmp != currentWorld ) toggleBlackFast(); - } - }else{ - if((tmp = currentWorld->goInsideStructure(player)) != currentWorld) + } else if( (tmp = currentWorld->goInsideStructure( player )) != currentWorld ) currentWorld = tmp; - } break; case SDLK_i: - /*currentWorld=currentWorld->goWorldBack(player); // Go back a layer if possible - if(tmp!=currentWorld){ - currentWorld->detect(player); - player->vel.y=.2; - player->loc.y+=HLINE*5; - player->ground=false; - }*/ player->health -= 5; break; - case SDLK_k: - /*currentWorld=currentWorld->goWorldFront(player); // Go forward a layer if possible - if(tmp!=currentWorld){ - currentWorld->behind->detect(player); - player->vel.y=.2; - player->loc.y+=HLINE*5; - player->ground=false; - }*/ - break; case SDLK_LSHIFT: if(debug){ Mix_PlayChannel(1,sanic,-1); @@ -1422,7 +1397,7 @@ DONE: /* * KEYUP */ - + case SDL_KEYUP: if(SDL_KEY == SDLK_ESCAPE){ //gameRunning = false; @@ -1501,16 +1476,16 @@ DONE: default: break; } - + if(!left&&!right) player->vel.x=0; - + break; default: break; } } - + // Flush preloaded AI functions if necessary if ( !dialogBoxExists && AIpreaddr.size() ) { while ( !AIpreaddr.empty() ) { @@ -1520,7 +1495,7 @@ DONE: } } } - + void toggleBlack(void){ fadeEnable ^= true; fadeWhite = false; @@ -1540,7 +1515,7 @@ DONE: fadeEnable ^= true; fadeWhite = true; fadeFast = true; - + Mix_PlayChannel( 1, battleStart, 0 ); } } diff --git a/src/world.cpp b/src/world.cpp index 097847f..dec9a64 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -151,9 +151,6 @@ World:: World( void ) { bgmObj = NULL; - - toLeft = NULL; - toRight = NULL; } /** @@ -222,9 +219,6 @@ World:: delete bgTex; - delete[] toLeft; - delete[] toRight; - deleteEntities(); } @@ -792,14 +786,15 @@ singleDetect( Entity *e ) * Handle gravity if the entity is above the line. */ - }else{ + } else { if(e->type == STRUCTURET && e->loc.y > 2000){ e->loc.y = worldData[i].groundHeight; e->vel.y = 0; e->ground = true; return; - }else if(e->vel.y > -2)e->vel.y-=.003 * deltaTime; + } else if ( e->vel.y > -2 ) + e->vel.y -= .003 * deltaTime; } @@ -974,30 +969,23 @@ addParticle( float x, float y, float w, float h, float vx, float vy, Color color void World::addLight(vec2 loc, Color color){ Light l; - if(light.size() < 64){ + if ( light.size() < 64 ) { l.loc = loc; l.color = color; light.push_back(l); } } -char *World::setToLeft(const char *file){ - if(toLeft) - delete[] toLeft; - if(!file) - return (toLeft = NULL); - - strcpy((toLeft = new char[strlen(file) + 1]),file); - return toLeft; +std::string World:: +setToLeft( std::string file ) +{ + return (toLeft = file); } -char *World::setToRight(const char *file){ - if(toRight) - delete[] toRight; - if(!file) - return (toRight = NULL); - - strcpy((toRight = new char[strlen(file) + 1]),file); - return toRight; + +std::string World:: +setToRight( std::string file ) +{ + return (toRight = file); } World *World:: @@ -1006,7 +994,7 @@ goWorldLeft( Player *p ) World *tmp; // check if player is at world edge - if( toLeft && p->loc.x < worldStart + HLINE * 15.0f ) { + if( !toLeft.empty() && p->loc.x < worldStart + HLINE * 15.0f ) { // load world (`toLeft` conditional confirms existance) tmp = loadWorldFromXML(toLeft); @@ -1026,7 +1014,7 @@ goWorldRight( Player *p ) { World *tmp; - if( toRight && p->loc.x + p->width > -worldStart - HLINE * 15 ) { + if( !toRight.empty() && p->loc.x + p->width > -worldStart - HLINE * 15 ) { tmp = loadWorldFromXML(toRight); p->loc.x = tmp->worldStart - HLINE * -15.0f; @@ -1042,14 +1030,19 @@ World *World:: goInsideStructure( Player *p ) { World *tmp; - char *current; - if(inside.empty()){ - for(auto &b : build){ - if(p->loc.x > b->loc.x && - p->loc.x + p->width < b->loc.x + b->width ){ - inside.push_back((std::string)(currentXML.c_str() + 4)); + std::string current; - tmp = loadWorldFromXML(b->inside.c_str()); + if ( inside.empty() ) { + for ( auto &b : build ) { + if ( p->loc.x > b->loc.x && + p->loc.x + p->width < b->loc.x + b->width ) { + + if ( b->inside.empty() ) + return this; + + inside.push_back(currentXML.c_str() + 4); + + tmp = loadWorldFromXML( b->inside ); ui::toggleBlackFast(); ui::waitForCover(); @@ -1058,11 +1051,11 @@ goInsideStructure( Player *p ) return tmp; } } - }else{ - strcpy((current = new char[strlen((const char *)(currentXML.c_str() + 4)) + 1]),(const char *)(currentXML.c_str() + 4)); - tmp = loadWorldFromXML(inside.back().c_str()); - for(auto &b : tmp->build){ - if(!strcmp(current,b->inside.c_str())){ + } else { + current = currentXML.c_str() + 4; + tmp = loadWorldFromXML( inside.back() ); + for ( auto &b : tmp->build ) { + if ( current == b->inside ) { inside.pop_back(); ui::toggleBlackFast(); @@ -1075,8 +1068,8 @@ goInsideStructure( Player *p ) return tmp; } } - delete[] current; } + return this; } |