Browse Source

Merge pull request #13 from WalrusRPG/text_engine

Dan Elkouby 9 years ago
parent
commit
f43f5f8daa

+ 14 - 14
art/rules.mk

@@ -5,20 +5,20 @@ art_SPR_DATA := $(addprefix $(OUT)/,$(art_SPRITES:%.png=%.cspr))
 art_SPR_SRC := $(OUT)/$(art_LOCAL_PATH)/sprites.c
 art_SPR_HDR := $(OUT)/$(art_LOCAL_PATH)/sprites.h
 
-BUILT_SRCS_C += $(art_SPR_SRC)
-INCLUDE += $(OUT)/$(art_LOCAL_PATH)
-BUILT_SRCS += $(art_SPR_HDR)
+# BUILT_SRCS_C += $(art_SPR_SRC)
+# INCLUDE += $(OUT)/$(art_LOCAL_PATH)
+# BUILT_SRCS += $(art_SPR_HDR)
 
-$(OUT)/%.cspr: %.png
-	@echo "SPRITE: $@"
-	@mkdir -p $(dir $@)
-	@ConvertImg --not-static --format n2dlib $< > $@
+# $(OUT)/%.cspr: %.png
+# 	@echo "SPRITE: $@"
+# 	@mkdir -p $(dir $@)
+# 	@ConvertImg --not-static --format n2dlib $< > $@
 
-$(art_SPR_SRC): $(art_SPR_DATA)
-	@echo "Catting sprites into sprites.c"
-	@rm -f $@
-	@echo "#include <stdint.h>" > $@
-	@cat $^ >> $@
+# $(art_SPR_SRC): $(art_SPR_DATA)
+# 	@echo "Catting sprites into sprites.c"
+# 	@rm -f $@
+# 	@echo "#include <stdint.h>" > $@
+# 	@cat $^ >> $@
 
-$(art_SPR_HDR): $(art_SPR_SRC)
-	@./$(art_LOCAL_PATH)/header.bash $< $@
+# $(art_SPR_HDR): $(art_SPR_SRC)
+# 	@./$(art_LOCAL_PATH)/header.bash $< $@

BIN
data/haeccity.wrf


BIN
data/out.wrf


+ 7 - 0
platform/nspire/Interrupts.cpp

@@ -17,8 +17,12 @@ uint32_t interrupt_pointer_bkp;
 // Interrupt source 21 is the LCD
 #define INTERRUPT_MASK (1 << 21)
 
+static bool is_on = false;
+
 void INTERRUPTS::init()
 {
+    is_on = true;
+
     interrupt_select_bkp = *interrupt_select;
     *interrupt_select = 0; // All IRQ for now
 
@@ -41,6 +45,9 @@ void INTERRUPTS::init()
 
 void INTERRUPTS::off()
 {
+    if (!is_on)
+        return;
+    is_on = false;
     // Disable IRQ in the CPU
     asm(
         "mrs r1, cpsr \n\t"

+ 1 - 1
src/engine/StateMachine.cpp

@@ -101,7 +101,7 @@ void StateMachine::run()
             stack.back()->render(100 * frame_time / TIMER_FREQ);
             last_frame = frame_stamp;
 
-            // Text::print_format(0, 0, "WalrusRPG test build %s", git_version);
+            Text::print_format(0, 0, "WRPG build %s", git_version);
             if (frame_time != 0 && update_time != 0)
             {
                 Text::print_format(0, 240 - 8, "%ufps, %uups", TIMER_FREQ / frame_time,

+ 3 - 1
src/engine/main.cpp

@@ -1,16 +1,17 @@
 #include "StateMachine.h"
 #include "Timing.h"
+#include "render/Text.h"
 #include "Graphics.h"
 #include "Quirks.h"
 #include "map/Map.h"
 #include "map/StateMap.h"
 #include "piaf/Archive.h"
 #include "utility/misc.h"
-#include "sprites.h"
 
 using namespace WalrusRPG;
 using WalrusRPG::PIAF::Archive;
 using WalrusRPG::Graphics::Texture;
+using namespace WalrusRPG::Graphics;
 
 int main(int argc, char *argv[])
 {
@@ -19,6 +20,7 @@ int main(int argc, char *argv[])
     Graphics::init();
     Timing::init();
     Quirks::init(argv[0]);
+    Text::init();
 
     Archive arc("data/out.wrf");
     Texture tex(arc.get("ov.png"));

+ 0 - 1
src/map/Map.cpp

@@ -1,7 +1,6 @@
 #include "Map.h"
 #include "render/TileRenderer.h"
 #include "Graphics.h"
-#include "sprites.h"
 #include "utility/Rect.h"
 #include "utility/misc.h"
 

+ 22 - 7
src/map/StateMap.cpp

@@ -1,25 +1,39 @@
 #include "StateMap.h"
 #include "Graphics.h"
 #include "render/Text.h"
+#include "piaf/Archive.h"
+
 
 using WalrusRPG::States::StateMap;
 using namespace WalrusRPG;
 using namespace WalrusRPG::Graphics;
+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());
     }
 }
 
-StateMap::StateMap(int x, int y, Map &map) : camera(x, y), map(map)
+// TODO : We definitely need a Resource Manager
+StateMap::StateMap(int x, int y, Map &map)
+    : camera(x, y), map(map), data("data/out.wrf"), tex_haeccity(data.get("t_haeccity")),
+      txt(tex_haeccity, data.get("f_haeccity"))
 {
     camera.set_x(0);
 }
@@ -31,8 +45,9 @@ void StateMap::update(unsigned dt)
 
 void StateMap::render(unsigned dt)
 {
+    // fill(Black);
     map.render(camera, dt);
 
-    print_debug_camera_data(camera);
-    print_debug_map_data(map);
+    print_debug_camera_data(camera, txt);
+    print_debug_map_data(map, txt);
 }

+ 10 - 0
src/map/StateMap.h

@@ -2,7 +2,9 @@
 #define INCLUDE_STATEMAP_H
 
 #include "engine/State.h"
+#include "piaf/Archive.h"
 #include "Map.h"
+#include "render/Font.h"
 
 namespace WalrusRPG
 {
@@ -13,11 +15,19 @@ namespace WalrusRPG
           protected:
             Camera camera;
             Map &map;
+            WalrusRPG::PIAF::Archive data;
+            WalrusRPG::Graphics::Texture tex_haeccity;
+            WalrusRPG::Graphics::Font txt;
 
           public:
             StateMap(int x, int y, Map &map);
             void render(unsigned dt);
             void update(unsigned dt);
+            void putchar_haeccity(unsigned char c, unsigned x, unsigned y);
+            void putchar_haeccity_tint(unsigned char c, unsigned x, unsigned y,
+                                       const WalrusRPG::Graphics::Pixel &col);
+            void print_haeccity(const char *str, unsigned x, unsigned y,
+                                const WalrusRPG::Graphics::Pixel &col);
         };
     }
 }

+ 0 - 1
src/piaf/Archive.h

@@ -39,7 +39,6 @@ namespace WalrusRPG
             FileType file_type;
             CompressionType compression_type;
             uint32_t file_size;
-            std::size_t size;
 
             File(uint8_t *data);
             File();

+ 121 - 0
src/render/Font.cpp

@@ -0,0 +1,121 @@
+#include <string.h>
+#include <zlib.h>
+#include <cstdio>
+#include <cstdarg>
+#include "Font.h"
+#include "utility/misc.h"
+
+using WalrusRPG::Graphics::Font;
+using WalrusRPG::Graphics::CharacterParameters;
+using WalrusRPG::Graphics::Texture;
+using WalrusRPG::Graphics::Pixel;
+
+Font::Font(Texture &font_tex, WalrusRPG::PIAF::File font_config)
+    : baseline(0), space_width(1), font_tex(font_tex)
+{
+    const uint8_t *ptr = font_config.get();
+    // TODO : parse file
+    // TODO : forgot to put the version and the font type
+    if (strncmp((const char *) ptr, "WFONT", 4) != 0)
+    {
+        // TODO : wrong header
+    }
+    uint32_t expected_checksum = read_big_endian_value<uint32_t>(&ptr[4]);
+    uint32_t calculated_checksum =
+        crc32(0L, (const unsigned char *) (&ptr[8]), font_config.file_size - 8);
+    if (expected_checksum != calculated_checksum)
+    {
+        // printf("Bad checksum : %x != %x\n", expected_checksum, calculated_checksum);
+    }
+
+    baseline = read_big_endian_value<uint32_t>(&ptr[12]);
+    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;
+    for (int i = 0; i < 256; ++i)
+    {
+        chars[i].dimensions.x = read_big_endian_value<int16_t>(current_char);
+        chars[i].dimensions.y = read_big_endian_value<int16_t>(current_char + 2);
+        chars[i].dimensions.width = read_big_endian_value<uint16_t>(current_char + 4);
+        chars[i].dimensions.height = read_big_endian_value<uint16_t>(current_char + 6);
+        chars[i].x_offset = read_big_endian_value<int16_t>(current_char + 8);
+        chars[i].y_offset = read_big_endian_value<int16_t>(current_char + 10);
+        current_char += (6 * sizeof(uint16_t));
+    }
+}
+
+Font::~Font()
+{
+}
+
+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(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(uint16_t x, uint16_t y, const char *str) const
+{
+    for (unsigned i = 0; str[i] && x < 320; i++)
+    {
+        unsigned char c = str[i];
+        if (c == 32)
+        {
+            x += space_width;
+            continue;
+        }
+        draw(x, y, c);
+        x += chars[c].dimensions.width + 1;
+    }
+}
+
+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++)
+    {
+        unsigned char c = str[i];
+        if (c == 32)
+        {
+            x += space_width;
+            continue;
+        }
+        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 = 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 = vsnprintf(buffer, 512, format, args);
+    va_end(args);
+    if (size < 0)
+        return;
+    draw(x, y, buffer, col);
+}

+ 47 - 0
src/render/Font.h

@@ -0,0 +1,47 @@
+#ifndef INCLUDE_FONT_H
+#define INCLUDE_FONT_H
+
+#include "Graphics.h"
+#include "Texture.h"
+#include "piaf/Archive.h"
+#include "utility/Rect.h"
+
+namespace WalrusRPG
+{
+    namespace Graphics
+    {
+        struct CharacterParameters
+        {
+            // Sprite clip
+            WalrusRPG::Utils::Rect dimensions;
+            // Character rendering offset
+            int16_t x_offset, y_offset;
+        };
+
+        class Font
+        {
+          public:
+            uint8_t baseline;
+            uint8_t space_width;
+            CharacterParameters chars[256];
+            WalrusRPG::Graphics::Texture &font_tex;
+
+            Font(WalrusRPG::Graphics::Texture &font_tex,
+                 WalrusRPG::PIAF::File font_config);
+            ~Font();
+
+            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;
+        };
+    }
+}
+
+#endif

+ 27 - 16
src/render/Text.cpp

@@ -1,36 +1,47 @@
 #include <cstdio>
 #include <cstdarg>
+#include <cstring>
 #include "Text.h"
-#include "sprites.h"
 #include "Graphics.h"
+#include "render/Font.h"
+#include "piaf/Archive.h"
 
 using namespace WalrusRPG::Graphics; /*Text*/
 using namespace WalrusRPG::Graphics;
 using namespace WalrusRPG::Utils;
+using namespace WalrusRPG::PIAF;
 
-Texture tex_font((char *) font);
+Font *fnt;
+Texture *tex;
+
+void Text::init()
+{
+    Archive arc("data/out.wrf");
+    tex = new Texture(arc.get("t_dbgfnt"));
+    fnt = new Font(*tex, arc.get("f_dbgfnt"));
+}
+
+void Text::deinit()
+{
+    delete fnt;
+    delete tex;
+}
 
 void Text::print_char(char c, unsigned x, unsigned y)
 {
-    put_sprite(tex_font, x, y, Rect((c % 16) * 8, (c / 16) * 8, 8, 8));
+    put_rectangle({static_cast<int>(x), static_cast<int>(y), 8, 8}, Black);
+    fnt->draw(x, y, c);
 }
 
 void Text::print_string(const char *str, unsigned x, unsigned y)
 {
-    Rect rect;
-    rect.width = 8;
-    rect.height = 8;
-    for (unsigned index = 0; str[index]; index++)
-    {
-        char c = str[index];
-        rect.x = (c % 16) * 8;
-        rect.y = (c / 16) * 8;
-        put_sprite(tex_font, x, y, rect);
-        x += 8;
-    }
+    put_rectangle({static_cast<int>(x), static_cast<int>(y),
+                   static_cast<unsigned>(8 * strlen(str)), 8},
+                  Black);
+    fnt->draw(x, y, str);
 }
 
-void Text::print_string(const std::string &str, unsigned x, unsigned y)
+inline void Text::print_string(const std::string &str, unsigned x, unsigned y)
 {
     Text::print_string(str.c_str(), x, y);
 }
@@ -44,7 +55,7 @@ void Text::print_format(unsigned x, unsigned y, const char *format, ...)
     print_string(buffer, x, y);
 }
 
-void Text::print_format(unsigned x, unsigned y, const std::string &format, ...)
+inline void Text::print_format(unsigned x, unsigned y, const std::string &format, ...)
 {
     Text::print_format(x, y, format.c_str());
 }

+ 3 - 0
src/render/Text.h

@@ -9,6 +9,9 @@ namespace WalrusRPG
     {
         namespace Text
         {
+            void init();
+            void deinit();
+
             void print_char(char c, unsigned x, unsigned y);
 
             void print_char(char c, unsigned x, unsigned y);