diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2015-09-18 17:26:28 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2015-09-18 17:26:28 -0400 |
commit | 6c2fa9e1b372f71d311b376e689961dad6aaa036 (patch) | |
tree | 39e8b2f07b6e04d3fb2e584ff4738150f2aa95de /src/ui.cpp | |
parent | a94372de8746229705e38eea66ce42f0a1ad3dba (diff) |
forgot stuff
Diffstat (limited to 'src/ui.cpp')
-rw-r--r-- | src/ui.cpp | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/src/ui.cpp b/src/ui.cpp new file mode 100644 index 0000000..d7aabc0 --- /dev/null +++ b/src/ui.cpp @@ -0,0 +1,103 @@ +#include <ui.h> +#include <ft2build.h> +#include FT_FREETYPE_H + +#define SDL_KEY e.key.keysym.sym + +extern Player *player; + +static FT_Library ftl; +static FT_Face ftf; +static GLuint ftex; +static unsigned int fontSize; + +namespace ui { + void initFonts(void){ + if(FT_Init_FreeType(&ftl)){ + std::cout<<"Error! Couldn't initialize freetype."<<std::endl; + abort(); + } + } + void setFontFace(const char *ttf){ + if(FT_New_Face(ftl,ttf,0,&ftf)){ + std::cout<<"Error! Couldn't open "<<ttf<<"."<<std::endl; + abort(); + } + } + void setFontSize(unsigned int size){ + fontSize=size; + FT_Set_Pixel_Sizes(ftf,0,fontSize); + } + void putString(const float x,const float y,const char *s){ + unsigned int i=0,j; + float xo=x,yo=y,w,h; + char *buf; + do{ + if(FT_Load_Char(ftf,s[i],FT_LOAD_RENDER)){ + std::cout<<"Error! Unsupported character "<<s[i]<<" ("<<(int)s[i]<<")."<<std::endl; + return; + } + glActiveTexture(GL_TEXTURE0); + glGenTextures(1,&ftex); + glBindTexture(GL_TEXTURE_2D,ftex); + 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); + buf=(char *)malloc(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; + buf[j*4+1]=255; + buf[j*4+2]=255; + buf[j*4+3]=ftf->glyph->bitmap.buffer[j]?255:0; + } + glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,ftf->glyph->bitmap.width,ftf->glyph->bitmap.rows,0,GL_RGBA,GL_UNSIGNED_BYTE,buf); + w=ftf->glyph->bitmap.width; + h=ftf->glyph->bitmap.rows; + glEnable(GL_TEXTURE_2D); + glBindTexture(GL_TEXTURE_2D,ftex); + glBegin(GL_QUADS); + glColor3ub(255,255,255); + glTexCoord2f(0,1);glVertex2f(xo,yo); + glTexCoord2f(1,1);glVertex2f(xo+w,yo); + glTexCoord2f(1,0);glVertex2f(xo+w,yo+h); + glTexCoord2f(0,0);glVertex2f(xo,yo+h); + glEnd(); + glDisable(GL_TEXTURE_2D); + xo+=w; + free(buf); + }while(s[i++]); + } + void putText(const float x,const float y,const char *str,...){ + va_list args; + char *buf; + buf=(char *)calloc(128,sizeof(char)); + va_start(args,str); + vsnprintf(buf,128,str,args); + va_end(args); + putString(x,y,buf); + free(buf); + } + void handleEvents(void){ + SDL_Event e; + while(SDL_PollEvent(&e)){ + switch(e.type){ + case SDL_QUIT: + gameRunning=false; + break; + case SDL_KEYDOWN: + if(SDL_KEY==SDLK_ESCAPE)gameRunning=false; + if(SDL_KEY==SDLK_a)player->vel.x=-2; + else if(SDL_KEY==SDLK_d)player->vel.x=2; + break; + case SDL_KEYUP: + if(SDL_KEY==SDLK_a)player->vel.x=0; + else if(SDL_KEY==SDLK_d)player->vel.x=0; + break; + default: + break; + } + } + } +} |