Selaa lähdekoodia

Better interface + printf-like functions

Eiyeron Fulmincendii 9 vuotta sitten
vanhempi
commit
10f572edd1
4 muutettua tiedostoa jossa 61 lisäystä ja 39 poistoa
  1. 11 22
      src/map/StateMap.cpp
  2. 1 1
      src/map/StateMap.h
  3. 38 9
      src/render/Font.cpp
  4. 11 7
      src/render/Font.h

+ 11 - 22
src/map/StateMap.cpp

@@ -11,17 +11,22 @@ using WalrusRPG::Utils::Rect;
 using WalrusRPG::PIAF::Archive;
 using WalrusRPG::PIAF::File;
 using WalrusRPG::Graphics::Texture;
+using WalrusRPG::Graphics::Font;
 
 namespace
 {
-    void print_debug_camera_data(const Camera &camera)
+    void print_debug_camera_data(const Camera &camera, const Font &fnt)
     {
-        Text::print_format(0, 8, "CAM : X : %d Y: %d", camera.get_x(), camera.get_y());
+        fnt.draw_format(240, 1, Black, "CAM : X : %d Y: %d", camera.get_x(),
+                        camera.get_y());
+        fnt.draw_format(240, 0, "CAM : X : %d Y: %d", camera.get_x(), camera.get_y());
     }
 
-    void print_debug_map_data(const Map &map)
+    void print_debug_map_data(const Map &map, const Font &fnt)
     {
-        Text::print_format(0, 16, "MAP : W: %d, H:%d", map.get_width(), map.get_height());
+        fnt.draw_format(240, 9, Black, "MAP : W: %d, H:%d", map.get_width(),
+                        map.get_height());
+        fnt.draw_format(240, 8, "MAP : W: %d, H:%d", map.get_width(), map.get_height());
     }
 }
 
@@ -42,22 +47,6 @@ void StateMap::render(unsigned dt)
     // fill(Black);
     map.render(camera, dt);
 
-    // print_debug_camera_data(camera);
-    // print_debug_map_data(map);
-    txt.draw("Hello world! :D", 0, 0);
-    txt.draw("This isn't actually a utility nor built-ins functions.", 0, 9);
-    txt.draw("This is a quick prototype to see if variable-wdith fonts works.", 0, 18);
-    txt.draw("Builting it myself allows me to make it work on calc too.", 0, 27, Yellow);
-    txt.draw(
-        "( I wonder if a conditionnal to tint the text slows down a lot the process. )",
-        0, 36, Gray);
-    txt.draw("Oh well, I hope I'll get to do a pretty textbox like in games !", 0, 45);
-
-    for (int i = 0; i < 16; ++i)
-    {
-        for (int j = 0; j < 16; ++j)
-        {
-            txt.draw(16 * j + i, 6 * i, 9 * j + 54, Pixel(i * 16, j * 16, 255));
-        }
-    }
+    print_debug_camera_data(camera, txt);
+    print_debug_map_data(map, txt);
 }

+ 1 - 1
src/map/StateMap.h

@@ -17,7 +17,7 @@ namespace WalrusRPG
             Map &map;
             WalrusRPG::PIAF::Archive data;
             WalrusRPG::Graphics::Texture tex_haeccity;
-            WalrusRPG::Font::Font txt;
+            WalrusRPG::Graphics::Font txt;
 
           public:
             StateMap(int x, int y, Map &map);

+ 38 - 9
src/render/Font.cpp

@@ -1,10 +1,12 @@
 #include <string.h>
 #include <zlib.h>
+#include <cstdio>
+#include <cstdarg>
 #include "Font.h"
 #include "utility/misc.h"
 
-using WalrusRPG::Font::Font;
-using WalrusRPG::Font::CharacterParameters;
+using WalrusRPG::Graphics::Font;
+using WalrusRPG::Graphics::CharacterParameters;
 using WalrusRPG::Graphics::Texture;
 using WalrusRPG::Graphics::Pixel;
 
@@ -30,7 +32,7 @@ Font::Font(Texture &font_tex, WalrusRPG::PIAF::File font_config)
     space_width = read_big_endian_value<uint32_t>(&ptr[20]);
 
     // Stupid thing to accelerate a biiiit the font loading, I think.
-    uint8_t *current_char = (uint8_t*) ptr + 24;
+    uint8_t *current_char = (uint8_t *) ptr + 24;
     for (int i = 0; i < 256; ++i)
     {
         chars[i].dimensions.x = read_big_endian_value<int16_t>(current_char);
@@ -47,21 +49,21 @@ Font::~Font()
 {
 }
 
-void Font::draw(const char c, uint16_t x, uint16_t y)
+void Font::draw(uint16_t x, uint16_t y, const char c) const
 {
     uint8_t c2 = (uint8_t) c;
     put_sprite(font_tex, x + chars[c2].x_offset, y + chars[c2].y_offset,
                chars[c2].dimensions);
 }
 
-void Font::draw(const char c, uint16_t x, uint16_t y, const Pixel &col)
+void Font::draw(uint16_t x, uint16_t y, const char c, const Pixel &col) const
 {
     uint8_t c2 = (uint8_t) c;
     put_sprite_tint(font_tex, x + chars[c2].x_offset, y + chars[c2].y_offset,
                     chars[c2].dimensions, col);
 }
 
-void Font::draw(const char *str, uint16_t x, uint16_t y)
+void Font::draw(uint16_t x, uint16_t y, const char *str) const
 {
     for (unsigned i = 0; str[i] && x < 320; i++)
     {
@@ -71,12 +73,12 @@ void Font::draw(const char *str, uint16_t x, uint16_t y)
             x += space_width;
             continue;
         }
-        draw(c, x, y);
+        draw(x, y, c);
         x += chars[c].dimensions.width + 1;
     }
 }
 
-void Font::draw(const char *str, uint16_t x, uint16_t y, const Pixel &col)
+void Font::draw(uint16_t x, uint16_t y, const char *str, const Pixel &col) const
 {
     for (unsigned i = 0; str[i] && x < 320; i++)
     {
@@ -86,7 +88,34 @@ void Font::draw(const char *str, uint16_t x, uint16_t y, const Pixel &col)
             x += space_width;
             continue;
         }
-        draw(c, x, y, col);
+        draw(x, y, c, col);
         x += chars[c].dimensions.width + 1;
     }
 }
+
+void Font::draw_format(uint16_t x, uint16_t y, const char *format, ...) const
+{
+    char buffer[513] = {0};
+
+    va_list args;
+    va_start(args, format);
+    int size = std::vsnprintf(buffer, 512, format, args);
+    va_end(args);
+    if (size < 0)
+        return;
+    draw(x, y, buffer);
+}
+
+void Font::draw_format(uint16_t x, uint16_t y, const Pixel &col, const char *format,
+                       ...) const
+{
+    char buffer[513] = {0};
+
+    va_list args;
+    va_start(args, format);
+    int size = std::vsnprintf(buffer, 512, format, args);
+    va_end(args);
+    if (size < 0)
+        return;
+    draw(x, y, buffer, col);
+}

+ 11 - 7
src/render/Font.h

@@ -8,7 +8,7 @@
 
 namespace WalrusRPG
 {
-    namespace Font
+    namespace Graphics
     {
         struct CharacterParameters
         {
@@ -30,12 +30,16 @@ namespace WalrusRPG
                  WalrusRPG::PIAF::File font_config);
             ~Font();
 
-            void draw(const char c, uint16_t x, uint16_t y);
-            void draw(const char c, uint16_t x, uint16_t y,
-                      const WalrusRPG::Graphics::Pixel &col);
-            void draw(const char *str, uint16_t x, uint16_t y);
-            void draw(const char *str, uint16_t x, uint16_t y,
-                      const WalrusRPG::Graphics::Pixel &col);
+            void draw(uint16_t x, uint16_t y, const char c) const;
+            void draw(uint16_t x, uint16_t y, const char c,
+                      const WalrusRPG::Graphics::Pixel &col) const;
+            void draw(uint16_t x, uint16_t y, const char *str) const;
+            void draw(uint16_t x, uint16_t y, const char *str,
+                      const WalrusRPG::Graphics::Pixel &col) const;
+            void draw_format(uint16_t x, uint16_t y, const char *format, ...) const;
+            void draw_format(uint16_t x, uint16_t y,
+                             const WalrusRPG::Graphics::Pixel &col, const char *format,
+                             ...) const;
         };
     }
 }