]> code.bitgloo.com Git - clyne/gamedev.git/commitdiff
new font; dialog box word wrap... finally
authorClyne Sullivan <tullivan99@gmail.com>
Fri, 22 Sep 2017 12:47:14 +0000 (08:47 -0400)
committerClyne Sullivan <tullivan99@gmail.com>
Fri, 22 Sep 2017 12:47:14 +0000 (08:47 -0400)
include/font.hpp
include/ui.hpp
include/vector2.hpp
src/engine.cpp
src/font.cpp
src/render.cpp
src/ui.cpp
src/ui_menu.cpp
ttf/Janyk.ttf [new file with mode: 0644]

index 0a6e38b0ff7f518d96336f1972c93c96de3db42b..20d27be6a696273faeb9946273e559d3f8152b1e 100644 (file)
@@ -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_
index 61c06cfdafbe96947cd1c714e9f2759ffa2ce610..70f3b8008ae23e38c39ccdc07e5737e25bd10fba 100644 (file)
@@ -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, ...);
index 5c9f9e11c183f2332d16c51e8e8b7529cedce080..54b08093d707916de7ef7d0d107e4a1b529da90a 100644 (file)
@@ -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 + ")";
        }
 };
 
index b596c6e7323796e94c90a86065740431e0aa91b0..bf0bca9879c10d47b7b3c8ef19da3000482a51a1 100644 (file)
@@ -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);
 
index af49d2ad8afb9db191a54154e7221e36d6ed145c..0b425f743fe1e141efafab4468b8cd3b90bd2581 100644 (file)
@@ -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;
index baa08ee0c1f7dcac7ad6c7c079965eddbc055c5b..b9a97702e8e18672faf7b25e15ccde5683c81b8a 100644 (file)
@@ -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
                        );
index e8fd78af71055b27a2165ef42416cf5a8c70e137..dea2f8e14ee6abfa1f2765d6076f89d864d06d1c 100644 (file)
@@ -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);
        }
 }
index fd316ce0865c039f9af95fbf54c5ccb41c22852a..3afe0dc7460a5fe9f1be38c1b1eb71611fb72a03 100644 (file)
@@ -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 (file)
index 0000000..c327e16
Binary files /dev/null and b/ttf/Janyk.ttf differ