diff options
author | drumsetmonkey <abelleisle@roadrunner.com> | 2016-03-01 08:21:12 -0500 |
---|---|---|
committer | drumsetmonkey <abelleisle@roadrunner.com> | 2016-03-01 08:21:12 -0500 |
commit | 883b348abac73d6c2b1d4ea8b65caccf0767e5a8 (patch) | |
tree | 83f1024e971bbff8e0b6b8c1bece267835a9a9fb /src | |
parent | 97c701f329bf3da154c23b0529f194d4d8823287 (diff) | |
parent | cd0236b94d5ecd2378518876820be201b9c635be (diff) |
Work on merchants and yer mum
Diffstat (limited to 'src')
-rw-r--r-- | src/Texture.cpp | 79 | ||||
-rw-r--r-- | src/gameplay.cpp | 1 | ||||
-rw-r--r-- | src/ui.cpp | 204 |
3 files changed, 171 insertions, 113 deletions
diff --git a/src/Texture.cpp b/src/Texture.cpp index 1487dcd..1aaadf6 100644 --- a/src/Texture.cpp +++ b/src/Texture.cpp @@ -1,45 +1,62 @@ #include <Texture.h> #include <string.h> +/** + * A structure for keeping track of loaded textures. + */ + struct texture_t { - char *name; - GLuint tex; - dim2 dim; -} __attribute__ ((packed)); + std::string name; /**< The file path of the texture. */ + GLuint tex; /**< The GLuint for the loaded texture. */ + dim2 dim; /**< The dimensions of the texture. */ +}; -struct index_t{ +struct index_t { Color color; int indexx; int indexy; }; -struct texture_t *LoadedTexture[256]; -unsigned int LoadedTextureCounter = 0; +/** + * A vector of all loaded textures. + * + * Should a texture be asked to be loaded twice, loadTexture() can reference + * this array and reuse GLuint's to save memory. + */ + +static std::vector<struct texture_t> LoadedTexture; namespace Texture{ Color pixels[8][4]; - GLuint loadTexture(const char *fileName){ + + GLuint loadTexture(std::string fileName){ SDL_Surface *image; GLuint object = 0; - for(unsigned int i=0;i<LoadedTextureCounter;i++){ - if(!strcmp(LoadedTexture[i]->name,fileName)){ + // check if texture is already loaded + for(auto &t : LoadedTexture){ + if(t.name == fileName){ + #ifdef DEBUG - DEBUG_printf("Reusing loaded texture for %s\n",fileName); + DEBUG_printf("Reusing loaded texture for %s\n", fileName.c_str()); #endif // DEBUG - return LoadedTexture[i]->tex; + + return t.tex; } } - if(!fileName) - return 0; - - if(!(image = IMG_Load(fileName))) + // load SDL_surface of texture + if(!(image = IMG_Load(fileName.c_str()))) return 0; + #ifdef DEBUG - DEBUG_printf("Loaded image file: %s\n", fileName); + DEBUG_printf("Loaded image file: %s\n", fileName.c_str()); #endif // DEBUG + /* + * Load texture through OpenGL. + */ + glGenTextures(1,&object); // Turns "object" into a texture glBindTexture(GL_TEXTURE_2D,object); // Binds "object" to the top of the stack glPixelStoref(GL_UNPACK_ALIGNMENT,1); @@ -61,33 +78,27 @@ namespace Texture{ image->pixels ); - LoadedTexture[LoadedTextureCounter] = new struct texture_t; //(struct texture_t *)malloc(sizeof(struct texture_t)); - LoadedTexture[LoadedTextureCounter]->name = new char[strlen(fileName)+1]; //(char *)malloc(safe_strlen(fileName)); - LoadedTexture[LoadedTextureCounter]->tex = object; - strcpy(LoadedTexture[LoadedTextureCounter]->name,fileName); - LoadedTexture[LoadedTextureCounter]->dim.x = image->w; - LoadedTexture[LoadedTextureCounter]->dim.y = image->h; - LoadedTextureCounter++; + // add texture to LoadedTexture + LoadedTexture.push_back((struct texture_t){fileName,object,{image->w,image->h}}); - SDL_FreeSurface(image); // Free the surface + // free the SDL_Surface + SDL_FreeSurface(image); return object; } - dim2 imageDim(const char *fileName){ - for(unsigned int i=0;i<LoadedTextureCounter;i++){ - if(!strcmp(LoadedTexture[i]->name,fileName)){ - return LoadedTexture[i]->dim; - } + dim2 imageDim(std::string fileName){ + for(auto &t : LoadedTexture){ + if(t.name == fileName) + return t.dim; } return {0,0}; } void freeTextures(void){ - for(unsigned int i=0;i<LoadedTextureCounter;i++){ - glDeleteTextures(1,&LoadedTexture[i]->tex); - delete[] LoadedTexture[i]->name; - delete LoadedTexture[i]; + while(!LoadedTexture.empty()){ + glDeleteTextures(1, &LoadedTexture.back().tex); + LoadedTexture.pop_back(); } } diff --git a/src/gameplay.cpp b/src/gameplay.cpp index 1c77309..e16e892 100644 --- a/src/gameplay.cpp +++ b/src/gameplay.cpp @@ -320,6 +320,7 @@ extern std::vector<NPC *> AIpreaddr; void destroyEverything(void){ currentWorld->save(); delete currentWorld; + delete[] currentXML; while(!AIpreload.empty()) AIpreload.pop_back(); @@ -35,7 +35,9 @@ typedef struct { ivec2 ad; /**< Number of pixels to advance cursor. */ } FT_Tex; -static FT_Tex ftmap[FT_CHAR_COUNT]; +static FT_Tex ftmap16[FT_CHAR_COUNT], + ftmap24[FT_CHAR_COUNT], + *ftmapptr; static FT_Library ftl; static FT_Face ftf; @@ -189,8 +191,13 @@ namespace ui { abort(); } - fontSize = 0; - memset(&ftmap, 0, FT_CHAR_COUNT * sizeof(FT_Tex)); + fontSize = 16; + + //ftmap16 = new FT_Tex[FT_CHAR_COUNT]; + //ftmap24 = new FT_Tex[FT_CHAR_COUNT]; + + memset(&ftmap16, 0, FT_CHAR_COUNT * sizeof(FT_Tex)); + memset(&ftmap24, 0, FT_CHAR_COUNT * sizeof(FT_Tex)); #ifdef DEBUG DEBUG_printf("Initialized FreeType2.\n",NULL); @@ -210,6 +217,9 @@ namespace ui { */ void destroyFonts(void){ + //delete[] ftmap16; + //delete[] ftmap24; + FT_Done_Face(ftf); FT_Done_FreeType(ftl); @@ -223,8 +233,13 @@ namespace ui { */ void setFontFace(const char *ttf){ - if(FT_New_Face(ftl,ttf,0,&ftf)){ - std::cout<<"Error! Couldn't open "<<ttf<<"."<<std::endl; + std::unique_ptr<uint8_t[]> rgbaBuf; + size_t rgbaBufSize, ftsize; + unsigned int i,j; + FT_Error fte; + + if((fte = FT_New_Face(ftl,ttf,0,&ftf))){ + std::cout<<"Error! Couldn't open "<<ttf<<" (Error "<<fte<<")."<<std::endl; abort(); } @@ -232,6 +247,77 @@ namespace ui { DEBUG_printf("Using font %s\n",ttf); #endif // DEBUG + /* + * Load the font in the two sizes we use, 16px and 24px. + */ + + ftmapptr = ftmap16; + ftsize = 16; + + do{ + FT_Set_Pixel_Sizes(ftf, 0, ftsize); + + // allocate texture space + for(i=0; i < FT_CHAR_COUNT; i++) + glGenTextures(1, &ftmapptr[i].tex); + + // Load all characters we expect to use + for(i=33; i<126; i++){ + + // Load the bitmap for the current character. + if(FT_Load_Char(ftf,i,FT_LOAD_RENDER)){ + std::cout<<"Error! Unsupported character "<<(char)i<<" ("<<i<<")."<<std::endl; + abort(); + } + + /* + * Set up the OpenGL texture thing. + */ + + glBindTexture(GL_TEXTURE_2D, ftmapptr[i-33].tex); + + 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); + + /* + * Convert the bitmap font given to us from FreeType into an RGBA + * format, for ease of drawing. + */ + + rgbaBuf.reset(new uint8_t [(rgbaBufSize = ftf->glyph->bitmap.width * ftf->glyph->bitmap.rows * 4)]); + rgbaBufSize /= 4; + + // populate the buffer + for(j=0; j < rgbaBufSize; j++){ + rgbaBuf[j * 4] = rgbaBuf[j * 4 + 1] = rgbaBuf[j * 4 + 2] = 255; + rgbaBuf[j * 4 + 3] = ftf->glyph->bitmap.buffer[j] ? 255 : 0; + } + + // save important character information + ftmapptr[i-33].wh = { (int)ftf->glyph->bitmap.width, (int)ftf->glyph->bitmap.rows }; + ftmapptr[i-33].bl = { ftf->glyph->bitmap_left, ftf->glyph->bitmap_top }; + ftmapptr[i-33].ad = { ftf->glyph->advance.x >> 6, ftf->glyph->advance.y >> 6 }; + + // do the thing + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ftf->glyph->bitmap.width, ftf->glyph->bitmap.rows, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgbaBuf.get()); + rgbaBuf.reset(); + } + + // repeat for 24px, or exit + if(ftmapptr == ftmap16){ + ftmapptr = ftmap24; + ftsize = 24; + }else{ + break; + } + + }while(1); + + setFontSize(16); } /** @@ -241,66 +327,14 @@ namespace ui { void setFontSize(unsigned int size){ mtx.lock(); - unsigned int i,j; - - std::unique_ptr<uint8_t[]> rgbaBuf; - size_t rgbaBufSize = 0; - - FT_Set_Pixel_Sizes(ftf,0,(fontSize = size)); - - // delete old characters, make space for new ones - for(i=0; i < FT_CHAR_COUNT; i++){ - glDeleteTextures(1, &ftmap[i].tex); - glGenTextures(1, &ftmap[i].tex); - } - - // Load all characters we expect to use - for(i=33;i<126;i++){ - - // Load the bitmap for the current character. - if(FT_Load_Char(ftf,i,FT_LOAD_RENDER)){ - std::cout<<"Error! Unsupported character "<<(char)i<<" ("<<i<<")."<<std::endl; - abort(); - } - - /* - * Set up the OpenGL texture thing. - */ - - glBindTexture(GL_TEXTURE_2D, ftmap[i-33].tex); - - 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); - - /* - * Convert the bitmap font given to us from FreeType into an RGBA - * format, for ease of drawing. - */ - - rgbaBuf.reset(new uint8_t [(rgbaBufSize = ftf->glyph->bitmap.width * ftf->glyph->bitmap.rows * 4)]); - rgbaBufSize /= 4; - - // populate the buffer - for(j=0; j < rgbaBufSize; j++){ - rgbaBuf[j * 4 ] = - rgbaBuf[j * 4 + 1] = - rgbaBuf[j * 4 + 2] = 255; - rgbaBuf[j * 4 + 3] = ftf->glyph->bitmap.buffer[j] ? 255 : 0; - } - - // save important character information - ftmap[i-33].wh = { (int)ftf->glyph->bitmap.width, (int)ftf->glyph->bitmap.rows }; - ftmap[i-33].bl = { ftf->glyph->bitmap_left, ftf->glyph->bitmap_top }; - ftmap[i-33].ad = { ftf->glyph->advance.x >> 6, ftf->glyph->advance.y >> 6 }; - - // do the thing - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ftf->glyph->bitmap.width, ftf->glyph->bitmap.rows, 0, GL_RGBA, GL_UNSIGNED_BYTE, rgbaBuf.get()); - - rgbaBuf.release(); + switch((fontSize = size)){ + case 24: + ftmapptr = ftmap24; + break; + default: + case 16: + ftmapptr = ftmap16; + break; } mtx.unlock(); } @@ -323,16 +357,16 @@ namespace ui { ivec2 c1,c2; // calculate coordinates - c1 = { (int)floor(x) + ftmap[c-33].bl.x, - (int)floor(y) + ftmap[c-33].bl.y }; - c2 = ftmap[c-33].wh; + c1 = { (int)floor(x) + ftmapptr[c-33].bl.x, + (int)floor(y) + ftmapptr[c-33].bl.y }; + c2 = ftmapptr[c-33].wh; /* * Draw the character */ glEnable(GL_TEXTURE_2D); - glBindTexture(GL_TEXTURE_2D, ftmap[c-33].tex); + glBindTexture(GL_TEXTURE_2D, ftmapptr[c-33].tex); glPushMatrix(); glTranslatef(0,-c2.y,0); @@ -351,7 +385,7 @@ namespace ui { glDisable(GL_TEXTURE_2D); // return the number of pixels the cursor should move - return ftmap[c-33].ad; + return ftmapptr[c-33].ad; } /** @@ -432,7 +466,7 @@ namespace ui { // handle everything else }else - width += ftmap[i].wh.x + fontSize * .1; + width += ftmapptr[i].wh.x + fontSize * .1; }while(s[++i]); @@ -757,18 +791,30 @@ namespace ui { // draw typeOut'd text putString(x + HLINE, y - fontSize - HLINE, (rtext = typeOut(dialogBoxText))); - merchAOptLoc[0][0] = offset.x - (SCREEN_WIDTH / 5.5); - merchAOptLoc[0][1] = offset.x + (SCREEN_WIDTH / 5.5); + 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 / 5.5) - 16; - merchAOptLoc[2][1] = offset.x + (SCREEN_WIDTH / 5.5) + 16; + 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); + } + } - glColor3ub(255,255,255); glBegin(GL_TRIANGLES); - glVertex2f(merchAOptLoc[2][0],merchAOptLoc[1][0]); - glVertex2f(merchAOptLoc[0][0],merchAOptLoc[1][0]-8); - glVertex2f(merchAOptLoc[0][0],merchAOptLoc[1][0]+8); + 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 |