diff options
author | Clyne Sullivan <tullivan99@gmail.com> | 2017-09-22 08:47:14 -0400 |
---|---|---|
committer | Clyne Sullivan <tullivan99@gmail.com> | 2017-09-22 08:47:14 -0400 |
commit | 19b2b1dde25885ada99deefe79266c2e22376e85 (patch) | |
tree | a5bf9c56f468663b78ce109d89b853637236574c | |
parent | 8720f1f253b55fa5233626dd854671a5925d65de (diff) |
new font; dialog box word wrap... finally
-rw-r--r-- | include/font.hpp | 6 | ||||
-rw-r--r-- | include/ui.hpp | 2 | ||||
-rw-r--r-- | include/vector2.hpp | 17 | ||||
-rw-r--r-- | src/engine.cpp | 2 | ||||
-rw-r--r-- | src/font.cpp | 5 | ||||
-rw-r--r-- | src/render.cpp | 2 | ||||
-rw-r--r-- | src/ui.cpp | 57 | ||||
-rw-r--r-- | src/ui_menu.cpp | 6 | ||||
-rw-r--r-- | ttf/Janyk.ttf | bin | 0 -> 14032 bytes |
9 files changed, 63 insertions, 34 deletions
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 ); @@ -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 Binary files differnew file mode 100644 index 0000000..c327e16 --- /dev/null +++ b/ttf/Janyk.ttf |