From 19b2b1dde25885ada99deefe79266c2e22376e85 Mon Sep 17 00:00:00 2001
From: Clyne Sullivan <tullivan99@gmail.com>
Date: Fri, 22 Sep 2017 08:47:14 -0400
Subject: new font; dialog box word wrap... finally

---
 include/font.hpp    |   6 ++++++
 include/ui.hpp      |   2 +-
 include/vector2.hpp |  17 ++++++++++++++--
 src/engine.cpp      |   2 +-
 src/font.cpp        |   5 +++--
 src/render.cpp      |   2 +-
 src/ui.cpp          |  57 ++++++++++++++++++++++++++++++----------------------
 src/ui_menu.cpp     |   6 +++---
 ttf/Janyk.ttf       | Bin 0 -> 14032 bytes
 9 files changed, 63 insertions(+), 34 deletions(-)
 create mode 100644 ttf/Janyk.ttf

diff --git a/include/font.hpp b/include/font.hpp
index 0a6e38b..20d27be 100644
--- a/include/font.hpp
+++ b/include/font.hpp
@@ -51,6 +51,9 @@ public:
 		FT_Done_FreeType(ftLibrary);
 	}
 
+	constexpr static inline int SizeSmall = 16;
+	constexpr static inline int SizeLarge = 24;
+
 	static void init(const std::string& ttf);
 	static void setFontSize(int size);
 	static void setFontColor(float r, float g, float b);
@@ -65,6 +68,9 @@ public:
 
 	static inline auto& getFont(void)
 	{ return fontData.at(currentSize); }
+
+	static inline auto getCharWidth(char c)
+	{ return fontData.at(currentSize)[c - 33].wh.x; }
 };
 
 #endif // FONT_HPP_
diff --git a/include/ui.hpp b/include/ui.hpp
index 61c06cf..70f3b80 100644
--- a/include/ui.hpp
+++ b/include/ui.hpp
@@ -83,7 +83,7 @@ public:
 	 */
 
 	static void putText(const vec2& p, const std::string& s, ...);
-	static void putString(const vec2& p, const std::string& s, float wrap = 0.12345f);
+	static void putString(const vec2& p, const std::string& s, float wrap = 0);
 	static float putStringCentered(const vec2& p, const std::string& s, bool print = true);
 
 	static void dialogBox(const std::string& n, const std::string& s, ...);
diff --git a/include/vector2.hpp b/include/vector2.hpp
index 5c9f9e1..54b0809 100644
--- a/include/vector2.hpp
+++ b/include/vector2.hpp
@@ -4,6 +4,9 @@
 #include <string>
 #include <type_traits>
 
+#include <sstream>
+#include <iomanip>
+
 template<typename T>
 struct vector2 {
 	static_assert(std::is_arithmetic<T>::value, "vector2 members must be an arithmetic type (i.e. numbers)");
@@ -123,8 +126,18 @@ struct vector2 {
 	}
 
 	// other functions
-	std::string toString(void) const {
-		return "(" + std::to_string(x) + ", " + std::to_string(y) + ")";
+	std::string toString(int precision = -1) const {
+		std::stringstream ss;
+		std::string sx, sy;
+		ss << std::fixed;
+		if (precision > -1)
+			ss << std::setprecision(precision);
+		ss << x;
+		sx = ss.str();
+		ss.str(std::string());
+		ss << y;
+		sy = ss.str();
+		return "(" + sx + ", " + sy + ")";
 	}
 };
 
diff --git a/src/engine.cpp b/src/engine.cpp
index b596c6e..bf0bca9 100644
--- a/src/engine.cpp
+++ b/src/engine.cpp
@@ -47,7 +47,7 @@ void Engine::init(void) {
 	// init ui
 
 	FontSystem::init(game::config::fontFamily);
-	FontSystem::setFontSize(16);
+	FontSystem::setFontSize(FontSystem::SizeSmall);
 	FontSystem::setFontColor(1, 1, 1);
 	FontSystem::setFontZ(-6.0f);
 
diff --git a/src/font.cpp b/src/font.cpp
index af49d2a..0b425f7 100644
--- a/src/font.cpp
+++ b/src/font.cpp
@@ -41,9 +41,10 @@ void FontSystem::setFontSize(int size)
 
 			// convert red-on-black to RGBA
 			auto& g = ftFace->glyph;
-			std::vector<uint32_t> buf (g->bitmap.width * g->bitmap.rows, 0xFFFFFFFF);
+			std::vector<uint32_t> buf (g->bitmap.width * g->bitmap.rows, 0x00FFFFFF);
 			for (auto j = buf.size(); j--;)
-				buf[j] ^= !g->bitmap.buffer[j] ? buf[j] : 0;
+				buf[j] |= g->bitmap.buffer[j] << 24;
+//				buf[j] ^= !g->bitmap.buffer[j] ? buf[j] : 0;
 
 			d.wh.x = g->bitmap.width;
 			d.wh.y = g->bitmap.rows;
diff --git a/src/render.cpp b/src/render.cpp
index baa08ee..b9a9770 100644
--- a/src/render.cpp
+++ b/src/render.cpp
@@ -187,7 +187,7 @@ void render(const int& fps)
 		auto pos = PlayerSystem::getPosition();
 		UISystem::putText(vec2(offset.x - game::SCREEN_WIDTH / 2, (offset.y + game::SCREEN_HEIGHT / 2) - FontSystem::getSize()),
 		    "loc: %s\noffset: %s\nfps: %d\nticks: %d\npcount: %d\nxml: %s\nmem: %llukb (%d)",
-			pos.toString().c_str(), offset.toString().c_str(), fps,
+			pos.toString(2).c_str(), offset.toString(2).c_str(), fps,
 			game::time::getTickCount(), ParticleSystem::getCount(),
 			WorldSystem::getXMLFile().c_str(), getUsedMem() / 1024, balance
 			);
diff --git a/src/ui.cpp b/src/ui.cpp
index e8fd78a..dea2f8e 100644
--- a/src/ui.cpp
+++ b/src/ui.cpp
@@ -20,6 +20,7 @@
 #include <events.hpp>
 #include <window.hpp>
 #include <player.hpp>
+#include <tokens.hpp>
 
 #include <chrono>
 
@@ -779,34 +780,42 @@ void UISystem::putString(const vec2& p, const std::string& s, float wrap)
 {
 	vec2 offset = p, add;
 
-	for (auto c : s) {
-		switch (c) {
-		case '\n':
+	int sum;
+	for (auto word : tokens(s, ' ')) {
+		sum = 0;
+		for (auto c : word)
+			sum += FontSystem::getCharWidth(c);
+		if (wrap > 0 && offset.x + sum > wrap) {
 			offset.y -= FontSystem::getSize() * 1.05f;
 			offset.x = p.x;
-			break;
-		case '\b':
-			offset.x -= add.x;
-			break;
-		case '\r':
-		case '\t':
-			break;
-		case ' ':
-			offset.x += FontSystem::getSize() / 2.0f;
-			break;
-		default:
-			add = FontSystem::putChar(floor(offset.x), floor(offset.y), c);
-			offset += add;
-			break;
 		}
+		
+		for (auto c : word) {
+			switch (c) {
+			case '\n':
+				//offset.y -= FontSystem::getSize() * 1.05f;
+				//offset.x = p.x;
+				break;
+			case '\b':
+				//offset.x -= add.x;
+				break;
+			case '\r':
+			case '\t':
+				break;
+			default:
+				add = FontSystem::putChar(floor(offset.x), floor(offset.y), c);
+				offset.x += add.x;
+				break;
+			}
 
-		if (wrap != 0.12345f && offset.x >= (wrap - 10)) {
-			offset.y -= FontSystem::getSize() * 1.05f;
-			offset.x = p.x;
+			/*if (wrap != 0.12345f && offset.x >= (wrap - 10)) {
+				offset.y -= FontSystem::getSize() * 1.05f;
+				offset.x = p.x;
+			}*/
 		}
-	}
 
-	//return offset.x;
+		offset.x += FontSystem::getSize() / 2.0f;
+	}
 }
 
 float UISystem::putStringCentered(const vec2& p, const std::string& s, bool print)
@@ -972,10 +981,10 @@ void UISystem::render(void)
 	}
 
 	if (!importantText.empty()) {
-		FontSystem::setFontSize(24);
+		FontSystem::setFontSize(FontSystem::SizeLarge);
 		FontSystem::setFontZ(-9.0f);
 		putStringCentered(vec2(offset.x, 400), ui::typeOut(importantText));
 		FontSystem::setFontZ(-6.0f);
-		FontSystem::setFontSize(16);
+		FontSystem::setFontSize(FontSystem::SizeSmall);
 	}
 }
diff --git a/src/ui_menu.cpp b/src/ui_menu.cpp
index fd316ce..3afe0dc 100644
--- a/src/ui_menu.cpp
+++ b/src/ui_menu.cpp
@@ -55,7 +55,7 @@ void Menu::gotoParent(void)
 {
 	if (parent == nullptr) {
 		game::config::update();
-		FontSystem::setFontSize(16);
+		FontSystem::setFontSize(FontSystem::SizeSmall);
 	}
 	
 	currentMenu = parent;
@@ -253,7 +253,7 @@ namespace ui {
 			Render::useShader(&Render::textShader);
 
             game::config::update();
-            FontSystem::setFontSize(24);
+            FontSystem::setFontSize(FontSystem::SizeLarge);
 			FontSystem::setFontZ(-9.0);
 	
             mouse.x = ui::premouse.x+offset.x-(SCREEN_WIDTH/2);
@@ -370,7 +370,7 @@ namespace ui {
             }
 
 			SDLReceiver::clicked = false;
-            FontSystem::setFontSize(16);
+            FontSystem::setFontSize(FontSystem::SizeSmall);
 			FontSystem::setFontZ(-8.0);
         }
 
diff --git a/ttf/Janyk.ttf b/ttf/Janyk.ttf
new file mode 100644
index 0000000..c327e16
Binary files /dev/null and b/ttf/Janyk.ttf differ
-- 
cgit v1.2.3