commit d86ed5d
uint
·
2026-08-04 20:49:48 +0000 UTC
parent bda779e
use a single ClientWorld for test and server worlds
1 files changed,
+39,
-66
+39,
-66
1@@ -1,6 +1,5 @@
2 #include <stdio.h>
3 #include <stdlib.h>
4-#include <string.h>
5
6 #include "config.h"
7 #include "mgmath.h"
8@@ -16,8 +15,6 @@
9 #include "world/block.h"
10 #include "world/chunk.h"
11
12-/* big TODO. most of this stuff belongs elsewhere !!! */
13-
14 #define TEST_WORLD_WIDTH 50
15 #define TEST_WORLD_DEPTH 50
16
17@@ -25,16 +22,16 @@ static MGContext ctx = {
18 NULL, { NULL }, "Magnolia", 0, 0, 1024, 768, { MGK_UNKNOWN },
19 };
20
21-static MGCamera cam;
22-static Chunk chunks[TEST_WORLD_WIDTH][TEST_WORLD_DEPTH];
23-
24-static ClientWorld server_world;
25+static MGCamera cam;
26+static ClientWorld world;
27 static i8 server_mode;
28
29 static void test_scene_create(Player* player, i8 server)
30 {
31- i32 x;
32- i32 z;
33+ Chunk* chunk;
34+ i32 chunk_index;
35+ i32 x;
36+ i32 z;
37
38 if (!block_textures_create("assets/resourcepacks/default"))
39 mg_die(MG_EC_TEXTURE_LOAD, "Failed to load block textures");
40@@ -44,7 +41,7 @@ static void test_scene_create(Player* player, i8 server)
41 cam.pitch = MG_AngleDeg(-25.0f);
42
43 server_mode = server;
44- world_init(&server_world);
45+ world_init(&world);
46 player->x = 0.0;
47 player->y = 0.0;
48 player->z = 0.0;
49@@ -56,15 +53,21 @@ static void test_scene_create(Player* player, i8 server)
50 cam.far = 512.0f;
51 }
52 else { /* test world */
53- for (z = 0; z < TEST_WORLD_DEPTH; z++) {
54- for (x = 0; x < TEST_WORLD_WIDTH; x++) {
55- chunk_clear(&chunks[x][z]);
56- chunk_section_create(&chunks[x][z].sections[0]);
57- chunks[x][z].section_mask = 1;
58-
59- if (!chunk_section_mesh_create(&chunks[x][z].sections[0]))
60- mg_die(MG_EC_MESH_CREATE, "Failed to create chunk mesh");
61- }
62+ for (chunk_index = 0; chunk_index < CLIENT_WORLD_MAX_CHUNKS; chunk_index++) {
63+ x = chunk_index % TEST_WORLD_WIDTH;
64+ z = chunk_index / TEST_WORLD_WIDTH;
65+ if (z >= TEST_WORLD_DEPTH)
66+ break;
67+
68+ chunk = &world.chunks[chunk_index];
69+ chunk->used = 1;
70+ chunk->x = x;
71+ chunk->z = z;
72+ chunk_section_create(&chunk->sections[0]);
73+ chunk->section_mask = 1;
74+
75+ if (!chunk_section_mesh_create(&chunk->sections[0]))
76+ mg_die(MG_EC_MESH_CREATE, "Failed to create chunk mesh");
77 }
78 }
79 }
80@@ -75,8 +78,6 @@ static void test_scene_draw(MGContext* ctx)
81 MGMat4 view;
82 MGMat4 mvp;
83 MGMat4 model;
84- i32 x;
85- i32 z;
86 i32 width;
87 i32 height;
88 float aspect;
89@@ -97,41 +98,24 @@ static void test_scene_draw(MGContext* ctx)
90 proj = camera_get_proj(&cam, aspect);
91 view = camera_get_view(&cam);
92
93- if (server_mode) {
94- for (i = 0; i < CLIENT_WORLD_MAX_CHUNKS; i++) {
95- column = &server_world.chunks[i];
96+ for (i = 0; i < CLIENT_WORLD_MAX_CHUNKS; i++) {
97+ column = &world.chunks[i];
98
99- if (!column->used)
100+ if (!column->used)
101+ continue;
102+
103+ for (section = 0; section < CHUNK_SECTION_COUNT; section++) {
104+ if (!(column->section_mask & (1U << section)))
105 continue;
106
107- for (section = 0; section < CHUNK_SECTION_COUNT; section++) {
108- if (!(column->section_mask & (1U << section)))
109- continue;
110-
111- section_chunk = &column->sections[section];
112- model = MG_Translate(
113- MG_V3(column->x * CHUNK_WIDTH, section * CHUNK_SECTION_HEIGHT, column->z * CHUNK_DEPTH));
114- mvp = MG_MulM4(proj, MG_MulM4(view, model));
115- for (texture = 0; texture < BLOCK_TEXTURE_COUNT; texture++) {
116- if (section_chunk->meshes[texture] != MGMESH_INVALID)
117- renderer_mesh_draw(section_chunk->meshes[texture],
118- block_texture_get(texture), &mvp);
119- }
120- }
121- }
122- }
123- else { /* test world */
124- for (z = 0; z < TEST_WORLD_DEPTH; z++) {
125- for (x = 0; x < TEST_WORLD_WIDTH; x++) {
126- model = MG_Translate(MG_V3(x * CHUNK_WIDTH, 0.0f, z * CHUNK_DEPTH));
127- mvp = MG_MulM4(proj, MG_MulM4(view, model));
128-
129- for (texture = 0; texture < BLOCK_TEXTURE_COUNT; texture++) {
130- if (chunks[x][z].sections[0].meshes[texture] == MGMESH_INVALID)
131- continue;
132- renderer_mesh_draw(chunks[x][z].sections[0].meshes[texture],
133- block_texture_get(texture), &mvp);
134- }
135+ section_chunk = &column->sections[section];
136+ model = MG_Translate(
137+ MG_V3(column->x * CHUNK_WIDTH, section * CHUNK_SECTION_HEIGHT, column->z * CHUNK_DEPTH));
138+ mvp = MG_MulM4(proj, MG_MulM4(view, model));
139+ for (texture = 0; texture < BLOCK_TEXTURE_COUNT; texture++) {
140+ if (section_chunk->meshes[texture] != MGMESH_INVALID)
141+ renderer_mesh_draw(section_chunk->meshes[texture], block_texture_get(texture),
142+ &mvp);
143 }
144 }
145 }
146@@ -139,18 +123,7 @@ static void test_scene_draw(MGContext* ctx)
147
148 static void test_scene_destroy(void)
149 {
150- i32 x;
151- i32 z;
152-
153- if (server_mode)
154- world_destroy(&server_world);
155- else {
156- for (z = 0; z < TEST_WORLD_DEPTH; z++) {
157- for (x = 0; x < TEST_WORLD_WIDTH; x++)
158- chunk_remove(&chunks[x][z]);
159- }
160- }
161-
162+ world_destroy(&world);
163 block_textures_destroy();
164 }
165
166@@ -283,7 +256,7 @@ int main(int argc, char** argv)
167 break;
168 case 0x21:
169 case 0x26:
170- chunk_packet(&server_world, &packet);
171+ chunk_packet(&world, &packet);
172 break;
173 }
174