From 5ead22e91fe1165f3a9208c0d1c965b3edd104be Mon Sep 17 00:00:00 2001 From: zlago Date: Sun, 29 Sep 2024 22:45:59 +0200 Subject: player entity --- src/tilemap.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 61 insertions(+), 4 deletions(-) (limited to 'src/tilemap.c') diff --git a/src/tilemap.c b/src/tilemap.c index af2c163..7dbc4dd 100644 --- a/src/tilemap.c +++ b/src/tilemap.c @@ -33,6 +33,56 @@ struct PACKED map { uint8_t tiles[]; }; +collision_T tilemap_tile_raw(struct tilemap *tilemap, int x, int y) { + return tilemap->collision[x + y * tilemap->width]; +} + +collision_T tilemap_tile(struct tilemap *tilemap, int x, int y) { + x /= 8; + y /= 8; + if (x < 0) { + x = 0; + } else if ((unsigned) x >= tilemap->width) { + x = tilemap->width - 1; + } + if (y < 0) { + y = 0; + } else if ((unsigned) y >= tilemap->height) { + y = tilemap->height - 1; + } + return tilemap_tile_raw(tilemap, x, y); +} + +collision_T tilemap_area_raw(struct tilemap *tilemap, int x1, int y1, int x2, int y2) { + collision_T value = 0; + for (int x = x1; x <= x2; x++) { + for (int y = y1; y <= y2; y++) { + value |= tilemap->collision[x + y * tilemap->width]; + } + } + return value; +} + +collision_T tilemap_area(struct tilemap *tilemap, int x1, int y1, int x2, int y2) { + x1 /= 8; + y1 /= 8; + x2 = (x2 - 1) / 8; + y2 = (y2 - 1) / 8; + if (x1 < 0) { + x1 = 0; + } + if (x2 >= tilemap->width) { + x2 = tilemap->width - 1; + } + if (y1 < 0) { + y1 = 0; + } + if (y2 >= tilemap->height) { + y2 = tilemap->height - 1; + } + return tilemap_area_raw(tilemap, x1, y1, x2, y2); +} + void tilemap_background(struct tilemap *tilemap, int x, int y, int w, int h) { SDL_SetRenderDrawColor(renderer, tilemap->backdrop.r, tilemap->backdrop.g, tilemap->backdrop.b, tilemap->backdrop.a); SDL_RenderFillRect(renderer, &(SDL_Rect) {0, 0, w, h}); @@ -103,13 +153,20 @@ struct tilemap *tilemap_load(void *data, size_t size) { struct blob col = res_get_collision(str); SDL_RenderCopy(renderer, tex, &(SDL_Rect) {0, 0, 128, height}, &(SDL_Rect) {0, y, 128, height}); - if (col.size + y * 2 > 0xf0) { - fprintf(stderr, "warn: '%s' overflows tile properties\n", str); - col.size = 0xf0 - y * 2; + if (col.data != NULL) { // silence -fsanitize=undefined + if (col.size + y * 2 > 0xf0) { + fprintf(stderr, "warn: '%s' overflows tile properties\n", str); + col.size = 0xf0 - y * 2; + } + memcpy(collision + y * 2, col.data, col.size * sizeof (collision_T)); } - memcpy(collision + y * 2, col.data, col.size * sizeof (collision_T)); y += height; + if (y + height > 0xf0) { + fputs("error: too many tiles\n", stderr); + tilemap_free(tilemap); + return NULL; + } str += len + 1; size -= len + 4 + 1; } -- cgit 1.4.1-2-gfad0