From: Lukas Krickl Date: Tue, 24 Feb 2026 15:56:31 +0000 (+0100) Subject: assets: wip rendering tiles X-Git-Url: https://git.krickl.dev/?a=commitdiff_plain;h=d1d4cac1c31f3b5f2c688769c36ce90127fe5774;p=lrts%2F.git assets: wip rendering tiles --- diff --git a/makefile b/makefile index bf1d2c9..9fbcdf6 100644 --- a/makefile +++ b/makefile @@ -11,7 +11,7 @@ OBJS:= CCOBJ=$(CC) -c -o $@ $< $(CFLAGS) $(LDFLAGS) -all: bin test +all: bin btest release: make DBGCFLAGS="" DBGLDFLAGS="" @@ -28,9 +28,18 @@ test.o: src/test.c FORCE bin: main.o $(OBJS) $(CC) -o $(NAME) main.o $(OBJS) $(CFLAGS) $(LDFLAGS) -test: test.o $(OBJS) +btest: test.o $(OBJS) $(CC) -o $(TEST_NAME) test.o $(OBJS) $(CFLAGS) $(LDFLAGS) -fsanitize=address + +.PHONY: test +test: $(TEST_NAME) + ./$(TEST_NAME) + +.PHONY: run +run: $(NAME) + ./$(NAME) + .PHONY: clean clean: rm -f ./*.o diff --git a/src/lrts.c b/src/lrts.c index 193c2e0..2245bf0 100644 --- a/src/lrts.c +++ b/src/lrts.c @@ -28,8 +28,8 @@ void lrts_version(void) { /** * The default render size */ -#define U_RENDER_W 640 -#define U_RENDER_H 640 +#define U_RENDER_W 896 +#define U_RENDER_H 504 struct u_vec2 lrts_get_render_res(void) { struct lrts_config *cfg = lrts_cfg(); diff --git a/src/lrts.h b/src/lrts.h index 04f17af..ffb0f6d 100644 --- a/src/lrts.h +++ b/src/lrts.h @@ -55,6 +55,7 @@ #include "t_camera.c" #include "r_render.c" #include "r_color.c" +#include "r_assets.c" #endif diff --git a/src/r_assets.c b/src/r_assets.c new file mode 100644 index 0000000..7b36631 --- /dev/null +++ b/src/r_assets.c @@ -0,0 +1,30 @@ +#include "r_assets.h" +#include "r_render.h" + +#define R_SOLID_TILE_OUTLINE_COLOR 0xFF0000FF + +void r_draw_solid_isometric_tile(i32 x, i32 y, r_color color, r_color outline_color) { + u32 i = 0; + u32 j = 0; + + LRTS_UNUSED(outline_color); + LRTS_UNUSED(x); + LRTS_UNUSED(y); + + for (i = 0; i < R_TILE_W; i++) { + for (j = 1; j < R_TILE_H-1; j++) { + r_draw_pixel(&r_framebuffer, x+i, y+j, color); + } + } +} + +void r_draw_tile(struct t_tile *t, i32 x, i32 y) { + switch (t->type) { + case T_TILE_TYPE_NONE: + break; + case T_TILE_TYPE_GRASS: + r_draw_solid_isometric_tile(x, y, 0x44FF00FF, R_SOLID_TILE_OUTLINE_COLOR); + break; + } +} + diff --git a/src/r_assets.h b/src/r_assets.h new file mode 100644 index 0000000..2da488f --- /dev/null +++ b/src/r_assets.h @@ -0,0 +1,28 @@ +#ifndef R_ASSETS_H__ +#define R_ASSETS_H__ + +#define R_TILE_W 32 +#define R_TILE_H 16 + +/** + * This module contains asset descriptions + * for all in-game asset types + * It provides an interface to obtain e.g. tile gfx based on the tile type. + * + * Gfx assets: + * all gfx assets have a built-in representation that does not require loading any + * resources. They serve as a fallback in case an image cannot be loaded. + */ + +#include "t_tile.h" +#include "u_defs.h" + +/* + * Draws a tile to the frame buffer at x, y. + * Attempts to load the tile from the tile sheet + * if no tile sheet is availabel a fallback is drawn instead + */ +void r_draw_tile(struct t_tile *t, i32 x, i32 y); + + +#endif diff --git a/src/r_render.c b/src/r_render.c index d53c8b7..c0d5b75 100644 --- a/src/r_render.c +++ b/src/r_render.c @@ -1,7 +1,8 @@ #include "r_render.h" #include "r_color.h" #include "p_draw.h" - +#include "t_tile.h" +#include "r_assets.h" struct r_framebuffer r_framebuffer; @@ -21,12 +22,13 @@ void r_draw_pixel(struct r_framebuffer *fb, i32 x, i32 y, r_color color) { } void r_render_frame() { - p_draw_begin(); + struct t_tile t; - r_draw_pixel(&r_framebuffer, 0, 0, 0xFF0000FF); - r_draw_pixel(&r_framebuffer, 1, 1, 0xFF0000FF); - r_draw_pixel(&r_framebuffer, 2, 2, 0xFF0000FF); - r_draw_pixel(&r_framebuffer, r_framebuffer.width-1, r_framebuffer.height-1, 0xFF0000FF); + p_draw_begin(); + + /* draw test tile */ + t.type = T_TILE_TYPE_GRASS; + r_draw_tile(&t, 128, 128); p_draw_end(); p_draw_present(); diff --git a/src/t_tile.h b/src/t_tile.h index d5424a6..a67e363 100644 --- a/src/t_tile.h +++ b/src/t_tile.h @@ -4,7 +4,8 @@ #include "u_defs.h" enum t_tile_types { - T_TILE_TYPE_NONE = 0 + T_TILE_TYPE_NONE = 0, + T_TILE_TYPE_GRASS }; enum t_tile_flags { diff --git a/src/u_math.c b/src/u_math.c index ff65375..a9ceaa2 100644 --- a/src/u_math.c +++ b/src/u_math.c @@ -44,3 +44,12 @@ struct u_fp u_fp_mod(struct u_fp a, struct u_fp b) { lrts_todo(""); return c; } + +struct u_vec2 u_vec2_mul(i32 n, struct u_vec2 v) { + struct u_vec2 r; + + r.x = v.x * n; + r.y = v.y * n; + + return r; +} diff --git a/src/u_math.h b/src/u_math.h index 543c97b..2aa2374 100644 --- a/src/u_math.h +++ b/src/u_math.h @@ -19,8 +19,8 @@ struct u_fp { * A simple 2d vector */ struct u_vec2 { - u32 x; - u32 y; + i32 x; + i32 y; }; /* @@ -37,6 +37,11 @@ struct u_fp u_fp_mul(struct u_fp a, struct u_fp b); struct u_fp u_fp_div(struct u_fp a, struct u_fp b); struct u_fp u_fp_mod(struct u_fp a, struct u_fp b); +/* + * Multiplies an integer with a vec2 reutnrs the resulting vector + */ +struct u_vec2 u_vec2_mul(i32 n, struct u_vec2 v); + /* Clamps an integer value between min and max */ #define U_CLAMP(v, min, max) if (v < min) { v = min; } if (v > max) { v = max; }