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