Skip to content

Commit 5fe93a9

Browse files
committed
Implement world_save_to_file.
1 parent 84d2c61 commit 5fe93a9

23 files changed

Lines changed: 584 additions & 202 deletions

CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ function(tiny_engine_configure_target TARGET_NAME)
3030
if(MSVC)
3131
target_compile_options(${TARGET_NAME} PUBLIC /W3 /WX /wd4996 /wd4090) # disable warnings: unsafe function, different const qualifiers
3232
else()
33-
target_compile_options(${TARGET_NAME} PUBLIC -Wall -Wextra -Werror -Wconversion -Wno-unused-but-set-parameter -Wno-incompatible-function-pointer-types -Wno-incompatible-pointer-types -Wno-unknown-warning-option)
33+
target_compile_options(${TARGET_NAME} PUBLIC -Wall -Wextra -Werror -Wconversion -Wno-unused-but-set-parameter -Wno-incompatible-function-pointer-types)
34+
if(CMAKE_SIZEOF_VOID_P EQUAL 4)
35+
target_compile_options(${TARGET_NAME} PUBLIC -Wno-incompatible-pointer-types -Wno-unknown-warning-option)
36+
endif()
3437
endif()
3538

3639
# Add `DEBUG` macro in debug builds.

docs/Manual.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,6 @@ void model_register_type(void) {
140140
type_info_add_vec3_variable(info, "position", model_set_position, model_get_position);
141141
type_info_add_string_variable(info, "texture", model_set_texture, model_get_texture);
142142
type_info_add_vec2_variable(info, "texture_tiling", model_set_texture_tiling, model_get_texture_tiling);
143-
type_info_add_bool_variable(info, "enable_transparency", model_enable_transparency, model_is_transparency_enabled);
144143

145144
type_database_register_type(info);
146145
}

src/editor/src/editor.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ editor_create_game_world(te_editor* editor, te_game_manager* game_manager) {
9494
editor->game_world_stats_widget = text_widget_create();
9595
widget_set_relative_position(text_widget_get_widget(editor->game_world_stats_widget), (vec2){0.01f, 0.01f});
9696
widget_set_relative_size(text_widget_get_widget(editor->game_world_stats_widget), (vec2){0.4f, 0.2f});
97+
widget_set_is_serialization_allowed(text_widget_get_widget(editor->game_world_stats_widget), false);
9798
text_widget_set_is_multiline(editor->game_world_stats_widget, true);
9899
editor->time_since_stats_update_sec = 10.0f;
99100

src/editor/src/editor_camera.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ editor_camera_create() {
3737
glm_vec2_zero(editor_camera->gamepad_look);
3838
glm_vec3_zero(editor_camera->movement_input);
3939

40+
camera_set_is_serialization_allowed(editor_camera->camera, false);
41+
4042
return editor_camera;
4143
}
4244

src/engine_lib/include/game/camera.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include <stdbool.h>
34
#include "cglm/mat4.h"
45
#include "cglm/vec3.h"
56
#include "cglm/vec4.h"
@@ -43,6 +44,10 @@ void camera_get_forward(te_camera* camera, vec3 out);
4344
void camera_get_right(te_camera* camera, vec3 out);
4445
void camera_get_up(te_camera* camera, vec3 out);
4546

47+
// Allows disabling serialization of the camera (enabled by default).
48+
void camera_set_is_serialization_allowed(te_camera* camera, bool enable);
49+
bool camera_is_serialization_allowed(te_camera* camera);
50+
4651
// Returns camera's view projection matrix.
4752
// Do not free/destroy returned pointer, valid while the camera exists.
4853
mat4* camera_get_view_proj_mat(te_camera* camera);
@@ -53,6 +58,9 @@ struct te_world* camera_get_world(te_camera* camera);
5358
// Always valid pointer. Do not free/destroy returned pointer. Valid while the camera exists.
5459
struct te_frustum_shape* camera_get_frustum(te_camera* camera);
5560

61+
// Returns NULL if not attached to a model.
62+
struct te_model* camera_get_parent_model(te_camera* camera);
63+
5664
// Returns unique ID of this type in the type database.
5765
const char* camera_get_type_id(void);
5866
// Registers the type in the type database.

src/engine_lib/include/game/model.h

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,21 @@ void model_get_texture_tiling(te_model* model, vec2 tex_tiling);
5151
void model_set_uv_offset(te_model* model, vec2 uv_offset);
5252
void model_get_uv_offset(te_model* model, vec2 uv_offset);
5353

54-
// Child model's location/rotation/scale will then be treated as relative to the parents parameters.
54+
// Child model's location/rotation/scale will then be treated as relative to the parents location/rotation/scale.
5555
// If the child model is not spawned but the parent is spawned will make the child model spawned.
5656
// When parent is despawned/destroyed will also make the attached child despawn/destroy.
57+
// After attached do not attempt to despawn the child model using the world because the world only operates on "root" models.
58+
// In order to despawn such model first detach it from the parent to make it "root" model and then despawn using the world.
5759
// Specify NULL as parent to detach.
5860
void model_set_parent(te_model* model, te_model* new_parent);
61+
te_model* model_get_parent(te_model* model);
62+
te_model* model_get_child_model(te_model* model);
5963

6064
// Same as @ref model_set_parent but attaches a camera. Specify NULL to detach the camera.
65+
// After attached do not attempt to despawn this camera using the world because the world only operates on "root" cameras.
66+
// In order to despawn such camera first detach it from the parent to make it "root" camera and then despawn using the world.
6167
void model_attach_camera(te_model* model, struct te_camera* camera);
68+
struct te_camera* model_get_attached_camera(te_model* model);
6269

6370
// Sets custom vertex shader.
6471
//
@@ -80,6 +87,10 @@ const char* model_get_custom_frag_shader(te_model* model);
8087
void model_enable_transparency(te_model* model, bool enable);
8188
bool model_is_transparency_enabled(te_model* model);
8289

90+
// Allows disabling serialization of the model (enabled by default).
91+
void model_set_is_serialization_allowed(te_model* model, bool enable);
92+
bool model_is_serialization_allowed(te_model* model);
93+
8394
// Returns NULL if the model is not spawned.
8495
// Do not free/destroy returned pointer, valid while the model exists.
8596
struct te_world* model_get_world(te_model* model);

src/engine_lib/include/type_database.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include "cglm/vec4.h"
77
#include <wchar.h>
88

9+
struct te_config;
10+
911
enum te_variable_type {
1012
TE_VT_BOOL,
1113
TE_VT_UINT,
@@ -103,6 +105,10 @@ void type_info_add_vec4_variable(te_type_info* info, const char* name, te_vec4_s
103105
void type_info_add_string_variable(te_type_info* info, const char* name, te_string_setter setter, te_string_getter getter);
104106
void type_info_add_wstring_variable(te_type_info* info, const char* name, te_wstring_setter setter, te_wstring_getter getter);
105107

108+
// Creates a new section in the specified config (returns index of the created section) and saves all reflected variables
109+
// in this new section.
110+
unsigned int type_info_save_to_config(const te_type_info* type_info, struct te_config* config, void* obj);
111+
106112
// Registers the specified type. Ownership of the pointer is moved to the type database.
107113
void type_database_register_type(te_type_info* info);
108114

@@ -114,4 +120,4 @@ const te_type_info* type_database_get_type_info(const char* id);
114120
// ------------------------------------------------------------------------------------------------
115121

116122
void prv_type_database_init(void);
117-
void prv_type_database_deinit(void);
123+
void prv_type_database_deinit(void);

src/engine_lib/include/widget/widget.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ struct te_world;
1919
// "on after spawned" callback is called before any child widget is spawned.
2020
// "on before despawned" callback is called after all child widgets are despawned.
2121
te_widget* widget_create(
22-
void* owner, void (*on_pos_changed)(void* owner), void (*on_size_changed)(void* owner),
22+
void* owner, const char* (*get_type_id)(), void (*on_pos_changed)(void* owner), void (*on_size_changed)(void* owner),
2323
void (*on_before_base_destroyed)(void* owner), void (*on_parent_changed)(void* owner),
2424
void (*on_children_changed)(void* owner), void (*on_after_spawned)(void* owner), void (*on_before_despawned)(void* owner),
2525
void (*on_window_size_changed)(void* owner));
2626
void widget_destroy(te_widget* widget);
2727

28+
// Returns the actual widget object that owns this base widget.
29+
void* widget_get_owner(te_widget* widget);
30+
const char* widget_get_owner_type_id(te_widget* widget);
31+
2832
// Sets or changes the current parent of a widget. Specify NULL to remove parent.
2933
// If the specified parent is spawned in some world but this widget is not spawned the widget will
3034
// be spawned (added to world) and attached to the specified parent.

src/engine_lib/include/world.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,19 @@ void world_despawn_widget(te_world* world, struct te_widget* widget);
3838
// The camera must be previously spawned in this world.
3939
void world_set_active_camera(te_world* world, struct te_camera* camera);
4040

41+
// Serializes all spawned world entities into the specified file
42+
// (path relative to the `res` directory).
43+
void world_save_to_file(te_world* world, const char* relative_path);
44+
4145
// Returns NULL if the world has no active camera.
4246
// Do not free/destroy returned pointer, valid until the camera is not destroyed.
4347
struct te_camera* world_get_active_camera(te_world* world);
4448

49+
// Returns NULL if no camera is spawned, otherwise all spawned cameras.
50+
// Do not save/store returned pointer as it might become invalid after a camera is spawned/despawned.
51+
struct te_camera** world_get_cameras_tmp(te_world* world, unsigned int* count);
52+
struct te_model** world_get_models_tmp(te_world* world, unsigned int* count);
53+
4554
// Do not free/destroy returned pointer, valid while the world exists.
4655
struct te_model_renderer* world_get_opaque_model_renderer(te_world* world);
4756
struct te_model_renderer* world_get_transparent_model_renderer(te_world* world);
@@ -67,6 +76,15 @@ bool prv_world_is_being_destroyed(te_world* world);
6776
// Called to possibly notify widgets.
6877
void prv_world_on_window_size_changed(te_world* world);
6978

79+
// Adds/removes the specified item to/from the array of spawned root item
80+
// (does nothing if already added/removed). Does not notify the item.
81+
void prv_world_add_root_model_no_notify(te_world* world, struct te_model* model, bool check_if_already_added);
82+
void prv_world_remove_root_model_no_notify(te_world* world, struct te_model* model, bool must_exist_in_array);
83+
void prv_world_add_root_widget_no_notify(te_world* world, struct te_widget* widget, bool check_if_already_added);
84+
void prv_world_remove_root_widget_no_notify(te_world* world, struct te_widget* widget, bool must_exist_in_array);
85+
void prv_world_add_root_camera_no_notify(te_world* world, struct te_camera* camera, bool check_if_already_added);
86+
void prv_world_remove_root_camera_no_notify(te_world* world, struct te_camera* camera, bool must_exist_in_array);
87+
7088
// Called by spawned widgets that receive input (for example buttons).
7189
// Note: these functions are not called from the base te_widget type (base type does not implement such functionality).
7290
void prv_world_add_interactable_widget(te_world* world, struct te_widget* widget);

src/engine_lib/src/game/camera.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ struct te_camera {
6666

6767
// `true` if @ref proj_mat contains outdated value and needs to be recalculated.
6868
bool is_proj_mat_outdated;
69+
70+
bool is_serialization_allowed;
6971
};
7072

7173
te_camera*
@@ -92,6 +94,7 @@ camera_create() {
9294
camera->is_view_mat_outdated = true;
9395
camera->is_proj_mat_outdated = true;
9496
camera->is_directions_outdated = true;
97+
camera->is_serialization_allowed = true;
9598

9699
return camera;
97100
}
@@ -266,6 +269,16 @@ camera_get_up(te_camera* camera, vec3 out) {
266269
glm_vec3_copy(camera->up, out);
267270
}
268271

272+
void
273+
camera_set_is_serialization_allowed(te_camera* camera, bool enable) {
274+
camera->is_serialization_allowed = enable;
275+
}
276+
277+
bool
278+
camera_is_serialization_allowed(te_camera* camera) {
279+
return camera->is_serialization_allowed;
280+
}
281+
269282
void
270283
prv_camera_recalc_frustum(te_camera* camera) {
271284
#if defined(DEBUG)
@@ -352,6 +365,11 @@ camera_get_frustum(te_camera* camera) {
352365
return &camera->frustum;
353366
}
354367

368+
struct te_model*
369+
camera_get_parent_model(te_camera* camera) {
370+
return camera->parent_model;
371+
}
372+
355373
void
356374
prv_camera_set_render_target_size(te_camera* camera, unsigned int width, unsigned int height) {
357375
if ((width == camera->render_width) && (height == camera->render_height)) {

0 commit comments

Comments
 (0)