From cb9b2c2fe7b77b0b8a3826ad2846655133683dfe Mon Sep 17 00:00:00 2001
From: 3gg <3gg@shellblade.net>
Date: Thu, 29 Aug 2024 22:09:29 -0700
Subject: Fix offseting. Add function to get screen size.

---
 gfx-iso/include/isogfx/isogfx.h |  3 +++
 gfx-iso/src/isogfx.c            | 22 +++++++++++++---------
 2 files changed, 16 insertions(+), 9 deletions(-)

(limited to 'gfx-iso')

diff --git a/gfx-iso/include/isogfx/isogfx.h b/gfx-iso/include/isogfx/isogfx.h
index e96606c..3421a7b 100644
--- a/gfx-iso/include/isogfx/isogfx.h
+++ b/gfx-iso/include/isogfx/isogfx.h
@@ -123,6 +123,9 @@ void isogfx_draw_tile(IsoGfx*, int x, int y, Tile);
 /// Resize the virtual screen's dimensions.
 bool isogfx_resize(IsoGfx*, int screen_width, int screen_height);
 
+/// Get the virtual screen's dimensions.
+void isogfx_get_screen_size(const IsoGfx*, int* width, int* height);
+
 /// Return a pointer to the virtual screen's colour buffer.
 ///
 /// Call after each call to isogfx_render() to retrieve the render output.
diff --git a/gfx-iso/src/isogfx.c b/gfx-iso/src/isogfx.c
index f7d0fa9..52c4ae2 100644
--- a/gfx-iso/src/isogfx.c
+++ b/gfx-iso/src/isogfx.c
@@ -761,8 +761,8 @@ static Pixel alpha_blend(Pixel src, Pixel dst) {
 
 /// Draw a rectangle (tile or sprite).
 ///
-/// The rectangle's bottom-left corner is mapped to the given origin. The
-/// rectangle then extends to the right and top of the origin.
+/// The rectangle's top-left corner is mapped to the screen space position given
+/// by 'top_left'.
 ///
 /// The rectangle's pixels are assumed to be arranged in a linear, row-major
 /// fashion.
@@ -775,23 +775,19 @@ static void draw_rect(
     const Pixel* pixels, const uint8_t* indices) {
   assert(iso);
 
-#define rect_pixel(x, y)                           \
-  (indices ? pixels[indices[py * rect_width + px]] \
-           : pixels[py * rect_width + px])
+#define rect_pixel(X, Y) \
+  (indices ? pixels[indices[Y * rect_width + X]] : pixels[Y * rect_width + X])
 
   // Rect origin can be outside screen bounds, so we must offset accordingly to
   // draw only the visible portion.
 #define max(a, b) (a > b ? a : b)
   const int px_offset = max(0, -top_left.x);
   const int py_offset = max(0, -top_left.y);
-  // Adjust top-left to be inside bounds.
-  top_left.x = max(0, top_left.x);
-  top_left.y = max(0, top_left.y);
 
   // Rect can exceed screen bounds, so clip along Y and X as we draw.
   for (int py = py_offset;
        (py < rect_height) && (top_left.y + py < iso->screen_height); ++py) {
-    const int sy = top_left.y + py - py_offset;
+    const int sy = top_left.y + py;
     for (int px = px_offset;
          (px < rect_width) && (top_left.x + px < iso->screen_width); ++px) {
       const Pixel colour = rect_pixel(px, py);
@@ -922,6 +918,14 @@ bool isogfx_resize(IsoGfx* iso, int screen_width, int screen_height) {
   return true;
 }
 
+void isogfx_get_screen_size(const IsoGfx* iso, int* width, int* height) {
+  assert(iso);
+  assert(width);
+  assert(height);
+  *width  = iso->screen_width;
+  *height = iso->screen_height;
+}
+
 const Pixel* isogfx_get_screen_buffer(const IsoGfx* iso) {
   assert(iso);
   return iso->screen;
-- 
cgit v1.2.3