Sfoglia il codice sorgente

Finish up texture abstraction

Streetwalrus Einstein 10 anni fa
parent
commit
ed8414b553

+ 2 - 1
platform/include/Graphics.h

@@ -9,6 +9,7 @@
 #include <cstdint>
 #include "render/Pixel.h"
 #include "utility/Rect.h"
+#include "Texture.h"
 
 namespace WalrusRPG
 {
@@ -35,7 +36,7 @@ namespace WalrusRPG
          * (not sure the GC supports drawing parts of a texture, but it
          * may be worth trying
          */
-        void put_sprite(const uint16_t *sheet, int x, int y,
+        void put_sprite(const Texture &sheet, int x, int y,
                         const WalrusRPG::Utils::Rect &window);
 
         /*

+ 2 - 2
platform/nspire/Graphics.cpp

@@ -25,10 +25,10 @@ void GRAPHICS::frame_end()
     CXfb::buffer_swap_render();
 }
 
-void GRAPHICS::put_sprite(const uint16_t *sheet, int x, int y,
+void GRAPHICS::put_sprite(const Texture &sheet, int x, int y,
                           const WalrusRPG::Utils::Rect &window)
 {
-    CXfb::draw_sprite_sheet(sheet, x, y, window);
+    CXfb::draw_sprite_sheet(sheet.data, x, y, window);
 }
 
 void GRAPHICS::fill(const WalrusRPG::Graphics::Pixel &color)

+ 5 - 3
platform/nspire/Texure.cpp

@@ -1,7 +1,7 @@
 #include "Texture.h"
 #include "utility/Rect.h"
 #include "render/Pixel.h"
-#include <cstdint>
+#include "utility/misc.h"
 
 using WalrusRPG::Graphics::Black;
 using WalrusRPG::Graphics::Pixel;
@@ -13,17 +13,19 @@ namespace
     texture_data_t loadPNG(char *data)
     {
         // TODO : stuff
+        UNUSED(data);
         return nullptr;
     }
 }
 
-Texture::Texture(char *data) : data(loadPNG(data))
+Texture::Texture(char *data) : data((texture_data_t) data)
 {
 }
 
 Texture::~Texture()
 {
-    delete (data);
+    // Don't deallocate for now since we still hardcode the data
+    // delete (data);
 }
 
 Rect Texture::get_dimensions()

+ 3 - 1
src/map/Map.cpp

@@ -9,9 +9,11 @@
 using namespace WalrusRPG;
 using namespace WalrusRPG::Utils;
 
+Graphics::Texture tex_overworld((char *) overworld);
+
 MAP::Map(int width, int height, uint16_t *layer0, uint16_t *layer1) : anim()
 {
-    this->renderer = new TileRenderer(overworld, 16, 16);
+    this->renderer = new TileRenderer(tex_overworld, 16, 16);
     this->width = width;
     this->height = height;
     this->layer0 = layer0;

+ 2 - 1
src/render/SpriteRenderer.cpp

@@ -6,7 +6,8 @@
 using namespace WalrusRPG;
 using namespace WalrusRPG::Utils;
 
-SPRITERENDERER::SpriteRenderer(unsigned short *_tilesheet) : tilesheet(_tilesheet)
+SPRITERENDERER::SpriteRenderer(WalrusRPG::Graphics::Texture _tilesheet)
+    : tilesheet(_tilesheet)
 {
 }
 

+ 3 - 2
src/render/SpriteRenderer.h

@@ -4,17 +4,18 @@
 #include <TINYSTL/unordered_map.h>
 #include "utility/Rect.h"
 #include "render/Renderer.h"
+#include "Texture.h"
 
 namespace WalrusRPG
 {
     class SpriteRenderer : public Renderer
     {
       protected:
-        unsigned short *tilesheet;
+        WalrusRPG::Graphics::Texture tilesheet;
         tinystl::unordered_map<unsigned, WalrusRPG::Utils::Rect> sprites;
 
       public:
-        SpriteRenderer(unsigned short *tilesheet);
+        SpriteRenderer(WalrusRPG::Graphics::Texture tilesheet);
         void add_sprite(unsigned id, WalrusRPG::Utils::Rect rect);
         virtual void render(const unsigned id, const WalrusRPG::Utils::Rect &rect);
     };

+ 4 - 2
src/render/Text.cpp

@@ -8,9 +8,11 @@
 using namespace WalrusRPG::Graphics;
 using namespace WalrusRPG::Utils;
 
+Texture tex_font((char *) font);
+
 void TEXT::print_char(char c, unsigned x, unsigned y)
 {
-    put_sprite(font, x, y, Rect((c % 16) * 8, (c / 16) * 8, 8, 8));
+    put_sprite(tex_font, x, y, Rect((c % 16) * 8, (c / 16) * 8, 8, 8));
 }
 
 void TEXT::print_string(const char *str, unsigned x, unsigned y)
@@ -23,7 +25,7 @@ void TEXT::print_string(const char *str, unsigned x, unsigned y)
         char c = str[index];
         rect.x = (c % 16) * 8;
         rect.y = (c / 16) * 8;
-        put_sprite(font, x, y, rect);
+        put_sprite(tex_font, x, y, rect);
         x += 8;
     }
 }

+ 2 - 2
src/render/TileRenderer.cpp

@@ -5,7 +5,7 @@
 using namespace WalrusRPG;
 using namespace WalrusRPG::Utils;
 
-TILERENDERER::TileRenderer(unsigned short *_tilesheet, unsigned tile_width,
+TILERENDERER::TileRenderer(WalrusRPG::Graphics::Texture _tilesheet, unsigned tile_width,
                            unsigned tile_height)
     : tilesheet(_tilesheet), tile_width(tile_width), tile_height(tile_height)
 {
@@ -13,7 +13,7 @@ TILERENDERER::TileRenderer(unsigned short *_tilesheet, unsigned tile_width,
 
 void TILERENDERER::render(const unsigned id, const Rect &rect)
 {
-    unsigned num_tiles_x = tilesheet[0] / tile_width;
+    unsigned num_tiles_x = tilesheet.get_dimensions().width / tile_width;
     // unsigned num_tiles_y = sheet_height / tile_height;
     Graphics::put_sprite(tilesheet, rect.x, rect.y,
                          Rect(tile_width * (id % num_tiles_x),

+ 3 - 2
src/render/TileRenderer.h

@@ -3,18 +3,19 @@
 
 #include "utility/Rect.h"
 #include "Renderer.h"
+#include "Texture.h"
 
 namespace WalrusRPG
 {
     class TileRenderer : public Renderer
     {
       protected:
-        unsigned short *tilesheet;
+        WalrusRPG::Graphics::Texture tilesheet;
         unsigned tile_width;
         unsigned tile_height;
 
       public:
-        TileRenderer(unsigned short *tilesheet, unsigned tile_width,
+        TileRenderer(WalrusRPG::Graphics::Texture tilesheet, unsigned tile_width,
                      unsigned tile_height);
         void render(const unsigned id, const WalrusRPG::Utils::Rect &rect);