2
0
Panagiotis Christopoulos Charitos 14 жил өмнө
parent
commit
025c27303b

+ 2 - 2
src/Main.cpp

@@ -124,7 +124,8 @@ void init()
 
 	srand(unsigned(time(NULL)));
 
-	painter = new Ui::Painter(AppSingleton::getInstance().getWindowWidth(), AppSingleton::getInstance().getWindowHeight());
+	painter = new Ui::Painter(Vec2(AppSingleton::getInstance().getWindowWidth(),
+	                               AppSingleton::getInstance().getWindowHeight()));
 	painter->setFont("engine-rsrc/ModernAntiqua.ttf", 25, 25);
 
 	// camera
@@ -371,7 +372,6 @@ void mainLoop()
 		MainRendererSingleton::getInstance().render(*AppSingleton::getInstance().getActiveCam());
 
 		painter->setPosition(Vec2(0.0, 0.5));
-		painter->setFontSize(Vec2(0.03, 0.03 * MainRendererSingleton::getInstance().getAspectRatio()));
 		painter->setColor(Vec4(1.0));
 
 		//painter->drawText("A");

+ 3 - 1
src/Ui/UiFont.cpp

@@ -18,7 +18,9 @@ void Font::create(const char* fontFilename, uint nominalWidth, uint NominalHeigh
 	FT_Vector ftSize = {nominalWidth, NominalHeight};
 	FtFontLoader ft(fontFilename, ftSize);
 
-	ft.saveImage("/tmp/test.tga");
+	lineHeight = ft.getLineHeight();
+
+	//ft.saveImage("/tmp/test.tga");
 
 	// - Create glyphs
 	// - Get metrics

+ 2 - 0
src/Ui/UiFont.h

@@ -29,6 +29,7 @@ class Font
 		int getGlyphBearingX(char c) const {return glyphs[c - ' '].horizBearingX;}
 		int getGlyphBearingY(char c) const {return glyphs[c - ' '].horizBearingY;}
 		const Texture& getMap() const {return *map;}
+		GETTER_R_BY_VAL(uint, lineHeight, getLineHeight)
 		/// @}
 
 	private:
@@ -46,6 +47,7 @@ class Font
 
 		std::auto_ptr<Texture> map; ///< The texture map that contains all the glyphs
 		boost::ptr_vector<Glyph> glyphs; ///< A set of glyphs from ' ' to ' ' + 128
+		uint lineHeight;
 
 		/// @param[in] fontFilename The filename of the font to load
 		/// @param[in] nominalWidth The nominal glyph width in pixels

+ 22 - 26
src/Ui/UiPainter.cpp

@@ -12,9 +12,8 @@ namespace Ui {
 //======================================================================================================================
 // Constructor                                                                                                         =
 //======================================================================================================================
-Painter::Painter(uint deviceWidth, uint deviceHeight):
-	deviceWidth(deviceWidth),
-	deviceHeight(deviceHeight)
+Painter::Painter(const Vec2& deviceSize_):
+	deviceSize(deviceSize_)
 {
 	init();
 }
@@ -34,10 +33,12 @@ void Painter::setFont(const char* fontFilename, uint nominalWidth, uint nominalH
 //======================================================================================================================
 void Painter::init()
 {
-	// Map
-	fontMap.loadRsrc("engine-rsrc/fontmap.png");
-	columns = 16;
-	rows = 8;
+	// Default font
+	float dfltFontWidth = 0.2 * deviceSize.x();
+	font.reset(new Font("engine-rsrc/ModernAntiqua.ttf", dfltFontWidth,
+	                    deviceSize.x() / deviceSize.y() * dfltFontWidth));
+
+	// Misc
 	tabSize = 4;
 
 	// SProg
@@ -80,53 +81,48 @@ void Painter::drawText(const char* text)
 	// Iterate
 	Vec2 p; // in NDC
 	p.x() = 2.0 * pos.x() - 1.0;
-	p.y() = 2.0 * -pos.y() - 1.0;
+	p.y() = 2.0 * (1.0 - pos.y()) - 1.0;
 	const char* c = text;
 	while(*c != '\0')
 	{
 		char cc = *c;
-		// Check if special
+
 		if(cc == ' ')
 		{
-			// do nothing
+			p.x() += 2.0 * font->getGlyphWidth(cc) / deviceSize.x();
 		}
 		else if(cc == '\n')
 		{
-			/*p.y() += verticalMoving;
-			p.x() = pos.x();
-			++c;
-			continue;*/
+			p.x() = 2.0 * pos.x() - 1.0;
+			p.y() += 2.0 * font->getLineHeight() / deviceSize.y();
 		}
 		else if(cc == '\t') // tab
 		{
-			p.x() +=  font->getGlyphWidth(' ') * (tabSize - 1);
-			cc = ' ';
+			p.x() +=  font->getGlyphWidth(' ') * tabSize;
 		}
 		else if(cc < ' ' || cc > '~') // out of range
 		{
-			cc = '~' + 1;
+			ERROR("Char out of range (" << cc << "). Ignoring");
 		}
 		else
 		{
 			Mat3 trfM = Mat3::getIdentity();
-			trfM(0, 0) = 2.0 * float(font->getGlyphWidth(cc)) / deviceWidth;
-			trfM(1, 1) = 2.0 * float(font->getGlyphHeight(cc)) / deviceHeight;
-			trfM(0, 2) = p.x() /*+ (font->getGlyphBearingX(cc) / float(deviceWidth))*/;
-
-			trfM(1, 2) = 2 * (font->getGlyphBearingY(cc) - int(font->getGlyphHeight(cc))) / float(deviceHeight);
+			trfM(0, 0) = 2.0 * float(font->getGlyphWidth(cc)) / deviceSize.x();
+			trfM(1, 1) = 2.0 * float(font->getGlyphHeight(cc)) / deviceSize.y();
+			trfM(0, 2) = p.x();
+			trfM(1, 2) = p.y() + 2.0 * (font->getGlyphBearingY(cc) - int(font->getGlyphHeight(cc))) / deviceSize.y();
 
 			sProg->findUniVar("transformation")->set(&trfM);
 			sProg->findUniVar("textureTranformation")->set(&font->getGlyphTextureMatrix(cc));
 
-
 			// Render
 			glDrawElements(GL_TRIANGLES, 2 * 3, GL_UNSIGNED_SHORT, 0);
+
+			// Inc
+			p.x() += 2.0 * font->getGlyphWidth(cc) / deviceSize.x();
 		}
 
-		// Inc
 		++c;
-		//p.x() += 2 * font->getGlyphAdvance(cc) / float(deviceWidth);
-		p.x() += 2 * font->getGlyphWidth(cc) / float(deviceWidth);
 	}
 }
 

+ 2 - 10
src/Ui/UiPainter.h

@@ -18,14 +18,12 @@ class Font;
 class Painter
 {
 	public:
-		Painter(uint deviceWidth = 0, uint deviceHeight = 0);
+		Painter(const Vec2& deviceSize);
 
 		/// @name Accessors
 		/// @{
 		GETTER_SETTER(Vec2, pos, getPosition, setPosition)
-		GETTER_SETTER(Vec2, fontSize, getFontSize, setFontSize)
 		GETTER_SETTER(Vec4, col, getColor, setColor)
-		//GETTER_SETTER(boost::array<uint, 2>, deviceSize, getDeviceSize, setDeviceSize)
 		void setFont(const char* fontFilename, uint nominalWidth, uint nominalHeight);
 		const Font& getFont() const {return *font;}
 		/// @}
@@ -37,14 +35,9 @@ class Painter
 		/// @name Data
 		/// @{
 		std::auto_ptr<Font> font;
-
-		RsrcPtr<Texture> fontMap;
-		uint columns;
-		uint rows;
 		RsrcPtr<ShaderProg> sProg;
 
 		Vec2 pos;
-		Vec2 fontSize;
 		Vec4 col;
 		uint tabSize;
 
@@ -52,8 +45,7 @@ class Painter
 		Vbo qIndecesVbo;
 		Vao qVao;
 
-		uint deviceWidth; ///< The size of the device in pixels
-		uint deviceHeight;
+		Vec2 deviceSize; ///< The size of the device in pixels
 		/// @}
 
 		void init();

+ 11 - 0
unit-tests/Ui/UiFtFontLoader.ut.cpp

@@ -1,7 +1,18 @@
 #include <gtest/gtest.h>
+#include <malloc.h>
 #include "UiFtFontLoader.h"
 
 
 TEST(UiTests, UiFtFontLoader)
 {
+	struct mallinfo pre = mallinfo();
+	
+	{
+		FT_Vector ftSize = {20, 20};
+		Ui::FtFontLoader ft("engine-rsrc/ModernAntiqua.ttf", ftSize);
+	}
+	
+	struct mallinfo post = mallinfo();
+	
+	EXPECT_EQ(post.uordblks - pre.uordblks, 0);
 }

Файлын зөрүү хэтэрхий том тул дарагдсан байна
+ 0 - 1
unit-tests/build/Makefile


Энэ ялгаанд хэт олон файл өөрчлөгдсөн тул зарим файлыг харуулаагүй болно