diff options
author | kdx <kikoodx@paranoici.org> | 2024-01-11 09:51:55 +0100 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2024-01-11 09:51:55 +0100 |
commit | e6c2cd08a7b3b4d3bf7d5ddfa6ef3b669d66b0b8 (patch) | |
tree | 095253a4006a186154424f3361011367d27cac17 | |
parent | 07feddebba8f59a343181abb36dd655a84168db6 (diff) | |
download | 008-e6c2cd08a7b3b4d3bf7d5ddfa6ef3b669d66b0b8.tar.gz |
cell rendering
-rw-r--r-- | inc/cell.h | 2 | ||||
-rw-r--r-- | inc/mesh.h | 1 | ||||
-rw-r--r-- | inc/world.h | 2 | ||||
-rw-r--r-- | res/dot.mesh | 3 | ||||
-rw-r--r-- | res/tset.bmp | bin | 24714 -> 2186 bytes | |||
-rw-r--r-- | res/world/0.csv | 32 | ||||
-rw-r--r-- | res/world/world.csv | 2 | ||||
-rw-r--r-- | src/cell.c | 59 | ||||
-rw-r--r-- | src/main.c | 26 | ||||
-rw-r--r-- | src/mesh.c | 30 | ||||
-rw-r--r-- | src/world.c | 4 |
11 files changed, 90 insertions, 71 deletions
@@ -15,5 +15,5 @@ extern float g_shake; Cell *cell_load(const char *pattern, int id, int x, int y); void cell_destroy(Cell *this, bool recurse); -void cell_draw(Cell *this, int x, int y); +void cell_draw(Cell *this, f64 x, f64 y); int2 cell_find(Cell *this, int tile); @@ -1,6 +1,7 @@ #pragma once typedef enum : u8 { + MESH_POINTS, MESH_LINES, MESH_TRIANGLES, MESH_QUADS diff --git a/inc/world.h b/inc/world.h index e7af998..c0ab36f 100644 --- a/inc/world.h +++ b/inc/world.h @@ -15,7 +15,7 @@ extern World g_world; void world_init(const char *path, const char *cell_pattern); void world_deinit(void); -void world_draw(void); +void world_draw(f64 x, f64 y); int world_get(int x, int y); int2 world_find_cell(int tile); int2 world_find(int tile); diff --git a/res/dot.mesh b/res/dot.mesh new file mode 100644 index 0000000..d2842ac --- /dev/null +++ b/res/dot.mesh @@ -0,0 +1,3 @@ +POINTS + +0 0 0 diff --git a/res/tset.bmp b/res/tset.bmp Binary files differindex 8c8c620..7d0b431 100644 --- a/res/tset.bmp +++ b/res/tset.bmp diff --git a/res/world/0.csv b/res/world/0.csv index d981590..8226164 100644 --- a/res/world/0.csv +++ b/res/world/0.csv @@ -1,17 +1,17 @@ 16,16 -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
\ No newline at end of file +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 +1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1 +1,0,0,0,0,0,0,0,0,0,0,0,2,2,2,1 +1,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1 +1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1 +1,0,2,0,0,0,0,0,0,0,0,0,0,0,0,1 +1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,1 +1,1,1,0,0,0,0,2,2,0,0,0,0,0,0,1 +1,0,0,0,0,0,1,1,1,1,0,0,0,0,0,1 +1,0,0,0,0,0,0,0,0,0,0,0,0,0,2,1 +1,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1 +1,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1 +1,0,0,0,2,0,0,0,0,0,0,0,0,1,1,1 +1,0,0,0,1,1,1,0,0,0,0,0,0,0,0,1 +1,0,0,0,1,1,1,2,2,2,0,0,0,2,2,1 +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
\ No newline at end of file diff --git a/res/world/world.csv b/res/world/world.csv index 92d5ec0..573796d 100644 --- a/res/world/world.csv +++ b/res/world/world.csv @@ -1,3 +1,3 @@ -2,1,0 +2,0,0 0,0,0 1,1,0 @@ -65,28 +65,47 @@ cell_destroy(Cell *this, bool recurse) } void -cell_draw(Cell *this, int x, int y) +cell_draw(Cell *this, f64 x, f64 y) { - const TZR_Uint tset = TZR_RES("res/tset.bmp"); - if (tset == 0) - return; + extern Mesh *g_cube; + extern Mesh *g_spike; + extern Mesh *g_dot; + + Mat4 m = mat4_identity(); + m = mat4_dot(m, mat4_scaling(1, 1, 48)); + m = mat4_dot(m, mat4_translating(-7.5 - x, -7.5 - y, -256)); + + Mat4 m_spike = mat4_identity(); + m_spike = mat4_dot(m_spike, mat4_scaling(1, 1, 16)); + m_spike = mat4_dot(m_spike, mat4_translating(-7.5 - x, -7.5 - y, -256)); - const int tset_width = TZR_GetImageWidth(tset) / cfg.tile_width; - rfor (ty, 0, this->height) { - rfor (tx, 0, this->width) { - const int tile = this->data[tx + ty * this->width]; - if (tile == 0) - continue; - int dy = y + ty * cfg.tile_height; - int dx = x + tx * cfg.tile_width; - const int ix = (tile-1) % tset_width * cfg.tile_width; - int iy = (tile-1) / tset_width * cfg.tile_height; - const int r = rand(); - TZR_DrawImage(tset, - dx, dy, - ix, iy, - cfg.tile_width, cfg.tile_height, - .flip_x=r&1, .flip_y=(r&2)!=0); + repeat (ty, this->height) { + repeat (tx, this->width) { + switch (this->data[tx + ty * this->width]) { + case 0: { + Mat4 lm = mat4_dot(m, + mat4_translating(tx, ty, 0)); + TZR_DrawSetColor(0, 0, 1); + mesh_draw(g_dot, lm); + break; + } + case 1: { + Mat4 lm = mat4_dot(m, + mat4_translating(tx, ty, 0)); + TZR_DrawSetColor(0, 0, 1); + mesh_draw(g_cube, lm); + break; + } + case 2: { + Mat4 lm = mat4_dot(m_spike, + mat4_translating(tx, ty, 0)); + TZR_DrawSetColor(1, 0, 0); + mesh_draw(g_spike, lm); + break; + } + default: + break; + } } } } @@ -6,6 +6,7 @@ static int _main_loop(void *udata); Mesh *g_cube; Mesh *g_spike; +Mesh *g_dot; Mat4 g_projection; int @@ -17,6 +18,7 @@ main([[maybe_unused]] int argc, [[maybe_unused]] char **argv) g_projection = mat4_glm(proj); g_cube = mesh_load("res/cube.mesh"); g_spike = mesh_load("res/spike.mesh"); + g_dot = mesh_load("res/dot.mesh"); defer(wdeinit); assert(TZR_Init(.width=cfg.display_width, .height=cfg.display_height, @@ -70,29 +72,7 @@ _main_loop([[maybe_unused]] void *udata) TZR_DrawClear(); TZR_BlendMode(SDL_BLENDMODE_ADD); - Mat4 m = mat4_identity(); - m = mat4_dot(m, mat4_scaling(1, 1, 48)); - m = mat4_dot(m, mat4_translating(-7.5 - x, -7.5 - y, -256)); - - Mat4 m_spike = mat4_identity(); - m_spike = mat4_dot(m_spike, mat4_scaling(1, 1, 16)); - m_spike = mat4_dot(m_spike, mat4_translating(-7.5 - x, -7.5 - y, -256)); - - repeat (y, 16) { - repeat (x, 16) { - if (y == 14 && x != 0 && x != 15) { - Mat4 lm = mat4_dot(m_spike, - mat4_translating(x, y, 0)); - TZR_DrawSetColor(1, 0, 0); - mesh_draw(g_spike, lm); - } - if (x != 0 && x != 15 && y != 0 && y != 15) - continue; - Mat4 lm = mat4_dot(m, mat4_translating(x, y, 0)); - TZR_DrawSetColor(0, 0, 1); - mesh_draw(g_cube, lm); - } - } + world_draw(x, y); assert(TZR_DrawEnd() == 0); return 0; @@ -25,7 +25,10 @@ mesh_load(const char *path) size_t tupsize; MeshType type; - if (strcasecmp(line, "LINES") == 0) { + if (strcasecmp(line, "POINTS") == 0) { + tupsize = 1; + type = MESH_POINTS; + } else if (strcasecmp(line, "LINES") == 0) { tupsize = 2; type = MESH_LINES; } else if (strcasecmp(line, "TRIANGLES") == 0) { @@ -61,9 +64,12 @@ mesh_load(const char *path) } assert(i == 3); mesh_push_vertex(this, vertex); + + if (tupsize == 1 && line[strlen(line) - 1] != '\n') + break; } - while (fgets(line, sizeof(line) - 1, fp) != nullptr) { + while (tupsize > 1 && fgets(line, sizeof(line) - 1, fp) != nullptr) { size_t i = 0; auto tok = strtok(line, delim); while (tok != nullptr) { @@ -129,6 +135,14 @@ _draw_line(Mesh *this, u8 idx1, u8 idx2) } static void +_mesh_draw_points(Mesh *this) +{ + for (size_t i = 0; i < this->transformed->size; i += 1) + TZR_DrawPoint(this->transformed->p[i].x, + this->transformed->p[i].y); +} + +static void _mesh_draw_lines(Mesh *this) { assert(this->indices->size % 2 == 0); @@ -175,11 +189,11 @@ _mesh_draw_quads(Mesh *this) } } -static f64 -randf(void) -{ - return 2 * rand() / (f64)RAND_MAX - 1; -} +//static f64 +//randf(void) +//{ +// return 2 * rand() / (f64)RAND_MAX - 1; +//} void mesh_draw(Mesh *this, Mat4 transform) @@ -203,6 +217,8 @@ mesh_draw(Mesh *this, Mat4 transform) } switch (this->type) { + case MESH_POINTS: + return _mesh_draw_points(this); case MESH_LINES: return _mesh_draw_lines(this); case MESH_TRIANGLES: diff --git a/src/world.c b/src/world.c index ffa3f26..a48df6f 100644 --- a/src/world.c +++ b/src/world.c @@ -76,10 +76,10 @@ world_deinit(void) } void -world_draw(void) +world_draw(f64 x, f64 y) { with (cell, g_world.cells[g_world.x + g_world.y * g_world.width]) - cell_draw(cell, 0, 0); + cell_draw(cell, x, y); } int |