From 303f5dc58dd8e8266df3c62fc84d9799db8047b9 Mon Sep 17 00:00:00 2001 From: 3gg <3gg@shellblade.net> Date: Tue, 27 Jun 2023 16:12:45 -0700 Subject: Initial renderer. --- gfx-iso/src/isogfx.c | 37 ++++++++++++++++++++++--------------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/gfx-iso/src/isogfx.c b/gfx-iso/src/isogfx.c index 27981f9..b38efe7 100644 --- a/gfx-iso/src/isogfx.c +++ b/gfx-iso/src/isogfx.c @@ -52,14 +52,22 @@ static inline ivec2 iso2cart(ivec2 iso, int s, int t, int w) { .x = (iso.x - iso.y) * (s / 2) + (w / 2), .y = (iso.x + iso.y) * (t / 2)}; } +// Method 1. +// static inline vec2 cart2iso(vec2 cart, int s, int t, int w) { +// const double x = cart.x - (double)(w / 2); +// const double xiso = (x * t + cart.y * s) / (double)(s * t); +// return (vec2){ +// .x = (int)(xiso), .y = (int)((2.0 / (double)t) * cart.y - xiso)}; +//} + +// Method 2. static inline vec2 cart2iso(vec2 cart, int s, int t, int w) { const double one_over_s = 1. / (double)s; const double one_over_t = 1. / (double)t; const double x = cart.x - (double)(w / 2); - return (vec2){ - .x = (int)(one_over_s * x + one_over_t * cart.y), - .y = (int)(-one_over_s * x + one_over_t * cart.y)}; + .x = (one_over_s * x + one_over_t * cart.y), + .y = (-one_over_s * x + one_over_t * cart.y)}; } Pixel* tile_xy_mut(const IsoGfx* iso, TileData* tile, int x, int y) { @@ -120,7 +128,7 @@ static inline Pixel* screen_xy_mut(IsoGfx* iso, int x, int y) { return &iso->screen[y * iso->screen_width + x]; } -static void draw_tile(IsoGfx* iso, ivec2 so, Tile tile) { +static void draw_tile(IsoGfx* iso, ivec2 origin, Tile tile) { assert(iso); const TileData* data = mempool_get_block(&iso->tiles, tile); @@ -129,8 +137,8 @@ static void draw_tile(IsoGfx* iso, ivec2 so, Tile tile) { for (int py = 0; py < iso->tile_height; ++py) { for (int px = 0; px < iso->tile_width; ++px) { const Pixel colour = tile_xy(iso, data, px, py); - const int sx = so.x + px; - const int sy = so.y + py; + const int sx = origin.x + px; + const int sy = origin.y + py; if ((sx >= 0) && (sy >= 0) && (sx < iso->screen_width) && (sy < iso->screen_height)) { const uint8_t mask = iso->tile_mask[py * iso->tile_width + px]; @@ -154,8 +162,9 @@ static void draw(IsoGfx* iso) { const ivec2 x = {.x = iso->tile_width / 2, .y = iso->tile_height / 2}; const ivec2 y = {.x = -iso->tile_width / 2, .y = iso->tile_height / 2}; - // TODO: Since the world will generally be larger than the screen, it - // would be best to walk in screen space and fetch the tile. + // TODO: Culling. + // Ex: map the screen corners to tile space to cull. + // Ex: walk in screen space and fetch the tile. // The tile-centric approach might be more cache-friendly, however, since the // screen-centric approach would juggle multiple tiles throughout the scan. for (int ty = 0; ty < iso->world_height; ++ty) { @@ -312,15 +321,13 @@ void isogfx_pick_tile( (vec2){.x = xcart, .y = ycart}, iso->tile_width, iso->tile_height, iso->screen_width); - const int x = (int)xy_iso.x; - const int y = (int)xy_iso.y; - - if ((0 <= x) && (x < iso->world_width) && (0 <= y) && - (y < iso->world_height)) { - *xiso = x; - *yiso = y; + if ((0 <= xy_iso.x) && (xy_iso.x < iso->world_width) && (0 <= xy_iso.y) && + (xy_iso.y < iso->world_height)) { + *xiso = (int)xy_iso.x; + *yiso = (int)xy_iso.y; } else { *xiso = -1; + *yiso = -1; } } -- cgit v1.2.3