From 6bc81dd151c94ad72a9560761a9964e066cc53e6 Mon Sep 17 00:00:00 2001
From: Clyne Sullivan <tullivan99@gmail.com>
Date: Sun, 13 Sep 2015 14:02:55 -0400
Subject: added text using freetype2

---
 Makefile          |  2 +-
 include/UIClass.h |  3 +++
 src/UIClass.cpp   | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/main.cpp      |  5 +++++
 4 files changed, 68 insertions(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index be0e49e..057e720 100644
--- a/Makefile
+++ b/Makefile
@@ -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   ****
 		**************************/
-- 
cgit v1.2.3