commit 0b9986a
uint
·
2026-08-05 01:53:46 +0000 UTC
parent 5a4a912
reorder static functions alphabetically in block.c
2 files changed,
+101,
-100
M
TODO
+1,
-1
1@@ -1,5 +1,4 @@
2 uint:
3-[ ] Remove `renderer_backend`
4 [ ] Review `block`
5 [ ] Small3DLib inline?? check ANSI
6
7@@ -20,6 +19,7 @@ uint:
8
9 [ ] Check it works on Windows
10
11+[-] Remove `renderer_backend`
12 [X] mc_*, mg_random type cleanup (int->i8 on bool)
13 [X] Clean up test_scene.h
14 [X] Maus wayland
+100,
-99
1@@ -78,38 +78,6 @@ static void model_rotate(BlockModel* model, i32 first, cJSON* rotation);
2 static void model_texture(BlockModel* model, const char* name, const char* value);
3 static i32 texture_load(const char* name);
4
5-/* check whether face covers cube side
6- returns 1 if face covers side, 0 otherwise */
7-static i8 face_covers_cube(const ModelFace* face)
8-{
9- /* only faces spanning their entire boundary plane can occlude neighbours */
10- static const i32 face_axes[BLOCK_FACE_COUNT] = { 2, 2, 0, 0, 1, 1 };
11- float low[3] = { 1, 1, 1 };
12- float high[3] = { 0, 0, 0 };
13- float value[3];
14- i32 vertex;
15- i32 axis;
16- i32 plane;
17-
18- for (vertex = 0; vertex < 4; vertex++) {
19- value[0] = face->vertices[vertex].x;
20- value[1] = face->vertices[vertex].y;
21- value[2] = face->vertices[vertex].z;
22- for (axis = 0; axis < 3; axis++) {
23- if (value[axis] < low[axis])
24- low[axis] = value[axis];
25- if (value[axis] > high[axis])
26- high[axis] = value[axis];
27- }
28- }
29- plane = face_axes[face->cullface];
30- for (axis = 0; axis < 3; axis++) {
31- if (axis != plane && (low[axis] != 0 || high[axis] != 1))
32- return 0;
33- }
34- return face->cullface & 1 ? high[plane] == 1 : low[plane] == 0;
35-}
36-
37 /* load block model and textures */
38 static void block_load(BlockType block)
39 {
40@@ -170,6 +138,39 @@ static void block_load(BlockType block)
41 loaded->occludes = mask == (1 << BLOCK_FACE_COUNT) - 1;
42 }
43
44+/* check whether face covers cube side
45+ returns 1 if face covers side, 0 otherwise */
46+static i8 face_covers_cube(const ModelFace* face)
47+{
48+ /* only faces spanning their entire boundary plane can occlude neighbours */
49+ static const i32 face_axes[BLOCK_FACE_COUNT] = { 2, 2, 0, 0, 1, 1 };
50+ float low[3] = { 1, 1, 1 };
51+ float high[3] = { 0, 0, 0 };
52+ float value[3];
53+ i32 vertex;
54+ i32 axis;
55+ i32 plane;
56+
57+ for (vertex = 0; vertex < 4; vertex++) {
58+ value[0] = face->vertices[vertex].x;
59+ value[1] = face->vertices[vertex].y;
60+ value[2] = face->vertices[vertex].z;
61+ for (axis = 0; axis < 3; axis++) {
62+ if (value[axis] < low[axis])
63+ low[axis] = value[axis];
64+ if (value[axis] > high[axis])
65+ high[axis] = value[axis];
66+ }
67+ }
68+ plane = face_axes[face->cullface];
69+ for (axis = 0; axis < 3; axis++) {
70+ if (axis != plane && (low[axis] != 0 || high[axis] != 1))
71+ return 0;
72+ }
73+ return face->cullface & 1 ? high[plane] == 1 : low[plane] == 0;
74+}
75+
76+
77 /* load JSON file
78 returns parsed JSON, or NULL on failure */
79 static cJSON* json_load(const char* path)
80@@ -219,61 +220,6 @@ static void model_cube(BlockModel* model, const char* texture)
81 model->face_count = BLOCK_FACE_COUNT;
82 }
83
84-/* load inherited model data and apply overrides
85- returns 1 on success, 0 on failure */
86-static i8 model_load(const char* name, BlockModel* model, i32 depth)
87-{
88- char path[PATH_SIZE];
89- cJSON* json;
90- cJSON* item;
91- cJSON* element;
92- cJSON* from;
93- cJSON* to;
94- cJSON* faces;
95- i32 face;
96- i32 first;
97-
98- if (depth == 16)
99- return 0;
100- if (strchr(name, ':'))
101- name = strchr(name, ':') + 1;
102- snprintf(path, sizeof(path), "%s/models/%s%s.json", pack_path, strchr(name, '/') ? "" : "block/", name);
103- json = json_load(path);
104- if (!json)
105- return 0;
106- /* load the parent first... */
107- item = cJSON_GetObjectItemCaseSensitive(json, "parent");
108- if (cJSON_IsString(item) && !model_load(item->valuestring, model, depth + 1)) {
109- cJSON_Delete(json);
110- return 0;
111- }
112- item = cJSON_GetObjectItemCaseSensitive(json, "textures");
113- cJSON_ArrayForEach(faces, item)
114- {
115- if (cJSON_IsString(faces))
116- model_texture(model, faces->string, faces->valuestring);
117- }
118- item = cJSON_GetObjectItemCaseSensitive(json, "elements");
119- if (cJSON_IsArray(item)) {
120- /* ... then let the child override*/
121- model->face_count = 0;
122- /* get the from and to values and then turn them into verticies*/
123- /*TODO we dont rotate stairs*/
124- cJSON_ArrayForEach(element, item)
125- {
126- first = model->face_count;
127- from = cJSON_GetObjectItemCaseSensitive(element, "from");
128- to = cJSON_GetObjectItemCaseSensitive(element, "to");
129- faces = cJSON_GetObjectItemCaseSensitive(element, "faces");
130- for (face = 0; face < BLOCK_FACE_COUNT; face++)
131- model_face(model, from, to, faces, face);
132- model_rotate(model, first, cJSON_GetObjectItemCaseSensitive(element, "rotation"));
133- }
134- }
135- cJSON_Delete(json);
136- return 1;
137-}
138-
139 /* convert JSON face element into vertices */
140 static void model_face(BlockModel* model, cJSON* from, cJSON* to, cJSON* faces, i32 face)
141 {
142@@ -326,21 +272,59 @@ static void model_face(BlockModel* model, cJSON* from, cJSON* to, cJSON* faces,
143 }
144 }
145
146-/* add or override model texture */
147-static void model_texture(BlockModel* model, const char* name, const char* value)
148+/* load inherited model data and apply overrides
149+ returns 1 on success, 0 on failure */
150+static i8 model_load(const char* name, BlockModel* model, i32 depth)
151 {
152- i32 i;
153+ char path[PATH_SIZE];
154+ cJSON* json;
155+ cJSON* item;
156+ cJSON* element;
157+ cJSON* from;
158+ cJSON* to;
159+ cJSON* faces;
160+ i32 face;
161+ i32 first;
162
163- for (i = 0; i < model->variable_count; i++) {
164- if (strcmp(model->variables[i].name, name) == 0)
165- break;
166+ if (depth == 16)
167+ return 0;
168+ if (strchr(name, ':'))
169+ name = strchr(name, ':') + 1;
170+ snprintf(path, sizeof(path), "%s/models/%s%s.json", pack_path, strchr(name, '/') ? "" : "block/", name);
171+ json = json_load(path);
172+ if (!json)
173+ return 0;
174+ /* load the parent first... */
175+ item = cJSON_GetObjectItemCaseSensitive(json, "parent");
176+ if (cJSON_IsString(item) && !model_load(item->valuestring, model, depth + 1)) {
177+ cJSON_Delete(json);
178+ return 0;
179 }
180- if (i == VARIABLE_COUNT)
181- return;
182- if (i == model->variable_count)
183- model->variable_count++;
184- snprintf(model->variables[i].name, NAME_SIZE, "%s", name);
185- snprintf(model->variables[i].value, NAME_SIZE, "%s", value);
186+ item = cJSON_GetObjectItemCaseSensitive(json, "textures");
187+ cJSON_ArrayForEach(faces, item)
188+ {
189+ if (cJSON_IsString(faces))
190+ model_texture(model, faces->string, faces->valuestring);
191+ }
192+ item = cJSON_GetObjectItemCaseSensitive(json, "elements");
193+ if (cJSON_IsArray(item)) {
194+ /* ... then let the child override*/
195+ model->face_count = 0;
196+ /* get the from and to values and then turn them into verticies*/
197+ /*TODO we dont rotate stairs*/
198+ cJSON_ArrayForEach(element, item)
199+ {
200+ first = model->face_count;
201+ from = cJSON_GetObjectItemCaseSensitive(element, "from");
202+ to = cJSON_GetObjectItemCaseSensitive(element, "to");
203+ faces = cJSON_GetObjectItemCaseSensitive(element, "faces");
204+ for (face = 0; face < BLOCK_FACE_COUNT; face++)
205+ model_face(model, from, to, faces, face);
206+ model_rotate(model, first, cJSON_GetObjectItemCaseSensitive(element, "rotation"));
207+ }
208+ }
209+ cJSON_Delete(json);
210+ return 1;
211 }
212
213 /* resolve model texture variable
214@@ -420,6 +404,23 @@ static void model_rotate(BlockModel* model, i32 first, cJSON* rotation)
215 }
216 }
217
218+/* add or override model texture */
219+static void model_texture(BlockModel* model, const char* name, const char* value)
220+{
221+ i32 i;
222+
223+ for (i = 0; i < model->variable_count; i++) {
224+ if (strcmp(model->variables[i].name, name) == 0)
225+ break;
226+ }
227+ if (i == VARIABLE_COUNT)
228+ return;
229+ if (i == model->variable_count)
230+ model->variable_count++;
231+ snprintf(model->variables[i].name, NAME_SIZE, "%s", name);
232+ snprintf(model->variables[i].value, NAME_SIZE, "%s", value);
233+}
234+
235 /* load texture by name
236 returns texture index, or -1 on failure */
237 static i32 texture_load(const char* name)