diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2015-09-13 14:02:55 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2015-09-13 14:02:55 -0400 |
commit | 6bc81dd151c94ad72a9560761a9964e066cc53e6 (patch) | |
tree | 0c01b338ad13c94d4cea77853f7d98d12ab9bc73 | |
parent | 8a88ef32f41a69166280dae8f59fa6477fd3609a (diff) |
added text using freetype2
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | include/UIClass.h | 3 | ||||
-rw-r--r-- | src/UIClass.cpp | 59 | ||||
-rw-r--r-- | src/main.cpp | 5 |
4 files changed, 68 insertions, 1 deletions
@@ -1,4 +1,4 @@ -FLAGS_LINUX = -lGL -lSDL2_image
+FLAGS_LINUX = -lGL -lSDL2_image -I/usr/include/freetype2/ -lfreetype
FLAGS_WIN32 = -lopengl32 -lmingw32 -lSDL2_Image
FLAGS = -m32 -std=c++11 -Iinclude -Wall -Werror -lSDL2main -lSDL2
diff --git a/include/UIClass.h b/include/UIClass.h index f12cc1e..e61bf14 100644 --- a/include/UIClass.h +++ b/include/UIClass.h @@ -5,6 +5,9 @@ class UIClass {
public:
+ void init(const char *ttf);
+ void setFontSize(unsigned int fs);
+ void putText(float x,float y,const char *s);
void handleEvents();
};
diff --git a/src/UIClass.cpp b/src/UIClass.cpp index 1376d10..b0e0206 100644 --- a/src/UIClass.cpp +++ b/src/UIClass.cpp @@ -1,8 +1,67 @@ #include <UIClass.h> +#include <ft2build.h> +#include FT_FREETYPE_H + extern Player player; extern World *currentWorld; +static FT_Library ftl; +static FT_Face ftf; +static GLuint ftex; + +void UIClass::init(const char *ttf){ + if(FT_Init_FreeType(&ftl)){ + std::cout<<"Error! Couldn't initialize freetype."<<std::endl; + abort(); + } + if(FT_New_Face(ftl,ttf,0,&ftf)){ + std::cout<<"Error! Couldn't open "<<ttf<<"."<<std::endl; + abort(); + } + +} +void UIClass::setFontSize(unsigned int fs){ + FT_Set_Pixel_Sizes(ftf,0,fs); +} +void UIClass::putText(float x,float y,const char *s){ + unsigned int i=0,j; + float xo=x,yo=y,w,h; + char *buf; + do{ + FT_Load_Char(ftf,s[i],FT_LOAD_RENDER); + 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*(2.0/SCREEN_WIDTH); + h=ftf->glyph->bitmap.rows *(2.0/SCREEN_HEIGHT); + 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+.01; + free(buf); + }while(s[i++]); +} + void UIClass::handleEvents(){ static bool space=false; float thing; diff --git a/src/main.cpp b/src/main.cpp index 88c08ef..6c53a75 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -67,6 +67,9 @@ int main(int argc,char **argv){ **** GAMELOOP **** **************************/ + ui.init("ttf/VCR_OSD_MONO_1.001.ttf"); + ui.setFontSize(100); + irand(time(NULL)); entPlay = &player; entPlay->spawn(0, 0); @@ -176,6 +179,8 @@ void render(){ glRectf(build.loc.x, build.loc.y, build.loc.x + build.width, build.loc.y + build.height); ///BWAHHHHHHHHHHHH + ui.putText(0,0,"Hello"); + /************************** **** CLOSE THE LOOP **** **************************/ |