Przeglądaj źródła

Externalised Rect to its own class.

Eiyeron Fulmincendii 10 lat temu
rodzic
commit
77599f8bb8
6 zmienionych plików z 42 dodań i 34 usunięć
  1. 2 4
      include/Entity.h
  2. 3 8
      include/Graphics.h
  3. 22 0
      include/Rect.h
  4. 5 5
      src/Graphics.cpp
  5. 6 10
      src/Text.cpp
  6. 4 7
      src/Tileset.cpp

+ 2 - 4
include/Entity.h

@@ -1,6 +1,7 @@
 #ifndef INCLUDE_ENTITY_H
 #define INCLUDE_ENTITY_H
 
+#include "Rect.h"
 #include "Camera.h"
 
 namespace WalrusRPG
@@ -13,10 +14,7 @@ namespace WalrusRPG
     class Entity
     {
       protected:
-        unsigned x;
-        unsigned y;
-        unsigned width;
-        unsigned height;
+        WalrusRPG::Utils::Rect coords;
 
       public:
         Entity();

+ 3 - 8
include/Graphics.h

@@ -1,17 +1,12 @@
 #ifndef INCLUDE_GRAPHICS_H
 #define INCLUDE_GRAPHICS_H
 
+#include "Rect.h"
+
 namespace WalrusRPG
 {
     namespace Graphics
     {
-        typedef struct Rect Rect_t;
-        struct Rect
-        {
-            int x, y;
-            int w, h;
-        };
-
         /*
 	 * Buffer management
 	 */
@@ -34,7 +29,7 @@ namespace WalrusRPG
 	 */
 
         void draw_pixel(unsigned x, unsigned y, unsigned short color);
-        void draw_sprite_sheet(const unsigned short *sheet, int x, int y, const Rect_t *window);
+        void draw_sprite_sheet(const unsigned short *sheet, int x, int y, const WalrusRPG::Utils::Rect &window);
 
 
         /*

+ 22 - 0
include/Rect.h

@@ -0,0 +1,22 @@
+#ifndef INCLUDE_RECT_H
+#define INCLUDE_RECT_H
+namespace WalrusRPG
+{
+    namespace Utils
+    {
+        class Rect
+        {
+          public:
+            signed x, y;
+            unsigned width, height;
+            // We don't need a source file for two inline functions.
+            Rect(unsigned x_, unsigned y_, signed width_, signed height_)
+                : x(x_), y(y_), width(width_), height(height_){};
+            Rect(unsigned x_, unsigned y_)
+                : Rect(x_, y_, 0, 0){};
+            Rect()
+                : Rect(0, 0, 0, 0){};
+        };
+    }
+}
+#endif

+ 5 - 5
src/Graphics.cpp

@@ -88,15 +88,15 @@ void GRAPHICS::draw_pixel(unsigned x, unsigned y, unsigned short color)
     buffer_back[x + (y * 320)] = color;
 }
 
-void GRAPHICS::draw_sprite_sheet(const unsigned short *sheet, int x, int y, const GRAPHICS::Rect_t *window)
+void GRAPHICS::draw_sprite_sheet(const unsigned short *sheet, int x, int y, const WalrusRPG::Utils::Rect &window)
 {
     unsigned short color;
-    int w = min(window->w + x, 320);
-    int h = min(window->h + y, 240);
+    int w = min(window.width + x, 320);
+    int h = min(window.height + y, 240);
 
-    for (int j = max(y, 0), l = window->y - min(y, 0); j < h; j++, l++)
+    for (int j = max(y, 0), l = window.y - min(y, 0); j < h; j++, l++)
     {
-        for (int i = max(x, 0), k = window->x - min(x, 0); i < w; i++, k++)
+        for (int i = max(x, 0), k = window.x - min(x, 0); i < w; i++, k++)
         {
             color = sprite_pixel_get(sheet, k, l);
             if (color != sheet[2])

+ 6 - 10
src/Text.cpp

@@ -7,28 +7,24 @@
 
 #define TEXT WalrusRPG::Graphics::Text
 #define GRAPHICS WalrusRPG::Graphics
+#define UTILS WalrusRPG::Utils
 
 void TEXT::print_char(char c, unsigned x, unsigned y)
 {
-    GRAPHICS::Rect_t rect;
-    rect.w = 8;
-    rect.h = 8;
-    rect.x = (c % 16) * 8;
-    rect.y = (c / 16) * 8;
-    draw_sprite_sheet(font, x, y, &rect);
+    draw_sprite_sheet(font, x, y, UTILS::Rect((c % 16) * 8, (c / 16) * 8, 8, 8));
 }
 
 void TEXT::print_string(const char *str, unsigned x, unsigned y)
 {
-    GRAPHICS::Rect_t rect;
-    rect.w = 8;
-    rect.h = 8;
+    UTILS::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;
-        draw_sprite_sheet(font, x, y, &rect);
+        draw_sprite_sheet(font, x, y, rect);
         x += 8;
     }
 }

+ 4 - 7
src/Tileset.cpp

@@ -1,8 +1,11 @@
 #include "Tileset.h"
 #include "Graphics.h"
+#include "Rect.h"
 
+#define GRAPHICS WalrusRPG::Graphics
 #define TILESET WalrusRPG::Tileset
 #define FRAME WalrusRPG::Frame
+#define UTILS WalrusRPG::Utils
 
 namespace
 {
@@ -32,13 +35,7 @@ void TILESET::render_tile(unsigned int index, unsigned x, unsigned y) const
 {
     unsigned num_tiles_x = sheet_width / tile_width;
     //unsigned num_tiles_y = sheet_height / tile_height;
-
-    WalrusRPG::Graphics::Rect_t sprite;
-    sprite.w = tile_width;
-    sprite.h = tile_height;
-    sprite.x = tile_width * (index % num_tiles_x);
-    sprite.y = tile_height * (index / num_tiles_x);
-    draw_sprite_sheet(sheet, x, y, &sprite);
+    GRAPHICS::draw_sprite_sheet(sheet, x, y, UTILS::Rect(tile_width * (index % num_tiles_x), tile_height * (index / num_tiles_x), tile_width, tile_height));
 }
 
 void TILESET::render_tile(unsigned int index, unsigned x, unsigned y, unsigned time)