]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
added text using freetype2
authorClyne Sullivan <tullivan99@gmail.com>
Sun, 13 Sep 2015 18:02:55 +0000 (14:02 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Sun, 13 Sep 2015 18:02:55 +0000 (14:02 -0400)
Makefile
include/UIClass.h
src/UIClass.cpp
src/main.cpp

index be0e49ee9f40d487aab74aba37516fe12b89d9a7..057e720432d5ca8cc168ceb7ca3e1a6bdb4cdff5 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-FLAGS_LINUX = -lGL                 -lSDL2_image\r
+FLAGS_LINUX = -lGL                 -lSDL2_image -I/usr/include/freetype2/ -lfreetype\r
 FLAGS_WIN32 = -lopengl32 -lmingw32 -lSDL2_Image\r
 FLAGS = -m32 -std=c++11 -Iinclude -Wall -Werror -lSDL2main -lSDL2\r
 \r
index f12cc1e67a56644f3d196804d827cc95d69fd404..e61bf14117c35da47f04c03cbf9b6de2b98f18b8 100644 (file)
@@ -5,6 +5,9 @@
 \r
 class UIClass {\r
        public:\r
+               void init(const char *ttf);\r
+               void setFontSize(unsigned int fs);\r
+               void putText(float x,float y,const char *s);\r
                void handleEvents();\r
 };\r
 \r
index 1376d104a9e9ed171c8e30d5e91fd849b75f3e1e..b0e020609d8ac519278c30203b008cfce937c078 100644 (file)
@@ -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;
index 88c08efd7ad3efecfa9ab4df889a09a97f64ad80..6c53a75435a0fec3e01f7f0b3f4d223ea8dc21bd 100644 (file)
@@ -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   ****
                **************************/