Skip to content

Commit b66fd0c

Browse files
committed
Refactoring.
1 parent 02a07b6 commit b66fd0c

14 files changed

Lines changed: 634 additions & 56 deletions

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
[submodule "ext/SDL"]
22
path = ext/SDL
33
url = https://github.com/libsdl-org/SDL.git
4+
[submodule "ext/cglm"]
5+
path = ext/cglm
6+
url = https://github.com/recp/cglm.git

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function(tiny_engine_configure_target TARGET_NAME)
4848
if(MSVC)
4949
target_compile_options(${TARGET_NAME} PUBLIC /W3 /WX)
5050
else()
51-
target_compile_options(${TARGET_NAME} PUBLIC -Wall -Wextra -Werror -Wconversion)
51+
target_compile_options(${TARGET_NAME} PUBLIC -Wall -Wextra -Werror -Wconversion -Wno-unused-but-set-parameter)
5252
endif()
5353

5454
# Add includes (relative to the target's directory).

ext/cglm

Submodule cglm added at 1952042

src/engine_lib/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ set(PROJECT_SOURCES
1616
src/renderer.c
1717
src/world.c
1818
src/ecs/ecs.c
19+
src/ecs/transform_component.c
20+
src/ecs/camera_component.c
21+
src/ecs/camera_system.c
1922
include/window.h
2023
include/io/log.h
2124
include/io/paths.h
@@ -26,6 +29,9 @@ set(PROJECT_SOURCES
2629
include/renderer.h
2730
include/world.h
2831
include/ecs/ecs.h
32+
include/ecs/transform_component.h
33+
include/ecs/camera_component.h
34+
include/ecs/camera_system.h
2935
)
3036

3137
# Define targets (one without any specific macros and another for compilation with editor).
@@ -79,6 +85,16 @@ target_include_directories(${GLAD_TARGET_NAME} SYSTEM PUBLIC ${RELATIVE_EXT_PATH
7985
target_link_libraries(${ENGINE_LIB_DEPS_TARGET} INTERFACE ${GLAD_TARGET_NAME})
8086
add_dependencies(${ENGINE_LIB_DEPS_TARGET} ${GLAD_TARGET_NAME})
8187

88+
# Add cglm.
89+
message(STATUS "${PROJECT_NAME}: adding dependency \"cglm\"...")
90+
set(CGLM_SHARED OFF CACHE BOOL "" FORCE)
91+
set(CGLM_STATIC ON CACHE BOOL "" FORCE)
92+
set(CGLM_USE_C99 ON CACHE BOOL "" FORCE)
93+
set(CGLM_USE_TEST OFF CACHE BOOL "" FORCE)
94+
add_subdirectory(${RELATIVE_EXT_PATH}/cglm ${DEPENDENCY_BUILD_DIR_NAME}/cglm EXCLUDE_FROM_ALL)
95+
target_link_libraries(${ENGINE_LIB_DEPS_TARGET} INTERFACE cglm_headers)
96+
target_compile_definitions(cglm_headers INTERFACE CGLM_ALL_UNALIGNED)
97+
8298
# Link engine library to dependencies.
8399
target_link_libraries(${ENGINE_LIB_TARGET} PUBLIC ${ENGINE_LIB_DEPS_TARGET})
84100
target_link_libraries(${ENGINE_LIB_TARGET_FOR_EDITOR} PUBLIC ${ENGINE_LIB_DEPS_TARGET})
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
#pragma once
2+
3+
#include <stdbool.h>
4+
#include "cglm/cglm.h"
5+
6+
/** Manages camera properties. */
7+
struct te_camera_component;
8+
9+
/**
10+
* Sets camera's near clip plane distance.
11+
*
12+
* @param comp Component.
13+
* @param z Non-negative value.
14+
*/
15+
void camera_set_near_clip(struct te_camera_component* comp, float z);
16+
17+
/**
18+
* Sets camera's far clip plane distance.
19+
*
20+
* @param comp Component.
21+
* @param z Non-negative value.
22+
*/
23+
void camera_set_far_clip(struct te_camera_component* comp, float z);
24+
25+
/**
26+
* Sets camera's vertical field of view.
27+
*
28+
* @param comp Component.
29+
* @param fov Vertical FOV.
30+
*/
31+
void camera_set_vertical_fov(struct te_camera_component* comp, unsigned char fov);
32+
33+
/**
34+
* Returns camera's near clip plane distance.
35+
*
36+
* @param comp Component.
37+
*
38+
* @return Near clip.
39+
*/
40+
float camera_get_near_clip(struct te_camera_component* comp);
41+
42+
/**
43+
* Returns camera's far clip plane distance.
44+
*
45+
* @param comp Component.
46+
*
47+
* @return Far clip.
48+
*/
49+
float camera_get_far_clip(struct te_camera_component* comp);
50+
51+
/**
52+
* Returns camera's vertical field of view.
53+
*
54+
* @return Vertical FOV.
55+
*/
56+
unsigned char camera_get_vertical_fov(struct te_camera_component* comp);
57+
58+
// ------------------------------------------------------------------------------------------------
59+
// PRIVATE API
60+
// ------------------------------------------------------------------------------------------------
61+
62+
/**
63+
* Returns sizeof(te_camera_component).
64+
*
65+
* @return Size in bytes.
66+
*/
67+
unsigned int prv_camera_component_get_sizeof(void);
68+
69+
/**
70+
* Initializes allocated memory.
71+
*
72+
* @param data Allocated component data.
73+
*/
74+
void prv_camera_component_init(void* data);
75+
76+
/**
77+
* Returns `true` if some properties of the camera changed
78+
* and view and projection matrices need to be recalculated.
79+
*
80+
* @remark This function is used by the camera system.
81+
*
82+
* @param comp Component.
83+
*
84+
* @return `true` if changed.
85+
*/
86+
bool prv_camera_is_properties_changed(struct te_camera_component* comp);
87+
88+
/**
89+
* Sets `false` to @ref prv_camera_is_properties_changed.
90+
*
91+
* @remark This function is used by the camera system.
92+
*
93+
* @param comp Component.
94+
*/
95+
void prv_camera_reset_properties_changed_flag(struct te_camera_component* comp);
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#pragma once
2+
3+
#include <stdbool.h>
4+
#include "cglm/cglm.h"
5+
6+
/** Manages camera components. */
7+
struct te_camera_system;
8+
9+
/**
10+
* Sets ID of an entity (with a camera component) that should be the active camera
11+
* (the camera used to render the world).
12+
*
13+
* @param system Camera system.
14+
* @param entity_id ID of an entity to be the active camera.
15+
*/
16+
void camera_system_set_active_camera(struct te_camera_system* system, unsigned int entity_id);
17+
18+
/**
19+
* Returns `true` if there is an active camera to render the world.
20+
*
21+
* @param system Camera system.
22+
*
23+
* @return `true` if have active camera.
24+
*/
25+
bool camera_system_is_camera_set(struct te_camera_system* system);
26+
27+
/**
28+
* Returns active camera's view matrix.
29+
*
30+
* @param system Camera system.
31+
* @param out Output.
32+
*/
33+
void camera_system_get_camera_view(struct te_camera_system* system, mat4 out);
34+
35+
/**
36+
* Returns active camera's projection matrix.
37+
*
38+
* @param system Camera system.
39+
* @param out Output.
40+
*/
41+
void camera_system_get_camera_proj(struct te_camera_system* system, mat4 out);
42+
43+
// ------------------------------------------------------------------------------------------------
44+
// PRIVATE API
45+
// ------------------------------------------------------------------------------------------------
46+
47+
struct te_ecs;
48+
49+
/**
50+
* Creates a new camera system to manage entities with camera components.
51+
*
52+
* @param ecs ECS manager. The pointer must be valid while the system exists.
53+
*
54+
* @return Camera system.
55+
*/
56+
struct te_camera_system* prv_camera_system_create(struct te_ecs* ecs);
57+
58+
/**
59+
* Destroys a camera system.
60+
*
61+
* @param system System.
62+
*/
63+
void prv_camera_system_destroy(struct te_camera_system* system);

src/engine_lib/include/ecs/ecs.h

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ typedef struct te_ecs te_ecs;
88

99
/** Engine component type IDs. */
1010
enum te_ecs_engine_component_type_id : unsigned int {
11-
ECS_COMPONENT_TYPE_TRANSFORM = 0,
11+
TE_ECS_COMPONENT_TYPE_TRANSFORM = 0,
12+
TE_ECS_COMPONENT_TYPE_CAMERA,
1213
// ... new engine component types go here ...
1314

14-
ECS_COMPONENT_TYPE_FIRST_GAME_COMPONENT, // <- first component type ID for games
15+
TE_ECS_COMPONENT_TYPE_FIRST_GAME_COMPONENT, // <- first component type ID for games
1516
};
1617

1718
/**
@@ -95,13 +96,10 @@ void ecs_unregister_system(te_ecs* ecs, void* user_system);
9596
* @param component_type_ids Non-NULL pointer to type IDs of the components that the entity has
9697
* (component types must be previously registered using @ref ecs_register_component_type).
9798
*
98-
* @param opt_parent_entity_id Specify TE_ECS_ENTITY_ID_INVALID if the newly entity is not a child of some other entity,
99-
* otherwise specify a valid ID of an existing entity.
100-
*
10199
* @return Unique ID of the created entity.
102100
*/
103101
unsigned int ecs_create_entity(te_ecs* ecs, const char* name, unsigned int component_count,
104-
unsigned int* component_type_ids, unsigned int opt_parent_entity_id);
102+
unsigned int* component_type_ids);
105103

106104
/**
107105
* Destroys an entity that was previously created using @ref ecs_create_entity.
@@ -171,18 +169,6 @@ void* ecs_get_entity_component(te_ecs* ecs, unsigned int entity_id, unsigned int
171169
*/
172170
const char* ecs_get_entity_name(te_ecs* ecs, unsigned int entity_id);
173171

174-
/**
175-
* Returns TE_ECS_ENTITY_ID_INVALID if the entity does not have a parent entity, otherwise
176-
* returns entity ID of the parent entity.
177-
*
178-
* @param ecs ECS manager.
179-
*
180-
* @param entity_id ID of the entity.
181-
*
182-
* @return TE_ECS_ENTITY_ID_INVALID if no parent.
183-
*/
184-
unsigned int ecs_get_entity_parent(te_ecs* ecs, unsigned int entity_id);
185-
186172
// ------------------------------------------------------------------------------------------------
187173
// PRIVATE API
188174
// ------------------------------------------------------------------------------------------------
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
#pragma once
2+
3+
#include "cglm/cglm.h"
4+
5+
/** Allows entities to have a specific location, rotation and size in the world. */
6+
struct te_transform_component;
7+
8+
/**
9+
* Sets location.
10+
*
11+
* @param comp Component data.
12+
* @param in Value to set.
13+
*/
14+
void transform_set_location(struct te_transform_component* comp, vec3 in);
15+
16+
/**
17+
* Sets rotation in degrees.
18+
*
19+
* @param comp Component data.
20+
* @param in Value to set.
21+
*/
22+
void transform_set_rotation(struct te_transform_component* comp, vec3 in);
23+
24+
/**
25+
* Sets scale.
26+
*
27+
* @param comp Component data.
28+
* @param in Value to set.
29+
*/
30+
void transform_set_scale(struct te_transform_component* comp, vec3 in);
31+
32+
/**
33+
* Returns location.
34+
*
35+
* @param comp Component data.
36+
* @param out Value to write the result to.
37+
*/
38+
void transform_get_location(struct te_transform_component* comp, vec3 out);
39+
40+
/**
41+
* Returns rotation in radians.
42+
*
43+
* @param comp Component data.
44+
* @param out Value to write the result to.
45+
*/
46+
void transform_get_rotation(struct te_transform_component* comp, vec3 out);
47+
48+
/**
49+
* Returns scale.
50+
*
51+
* @param comp Component data.
52+
* @param out Value to write the result to.
53+
*/
54+
void transform_get_scale(struct te_transform_component* comp, vec3 out);
55+
56+
/**
57+
* Returns normalized forward direction.
58+
*
59+
* @param comp Component data.
60+
* @param out Value to write the result to. 4th component is zero.
61+
*/
62+
void transform_get_forward(struct te_transform_component* comp, vec4 out);
63+
64+
// ------------------------------------------------------------------------------------------------
65+
// PRIVATE API
66+
// ------------------------------------------------------------------------------------------------
67+
68+
/**
69+
* Returns sizeof(te_transform_component).
70+
*
71+
* @return Size in bytes.
72+
*/
73+
unsigned int prv_transform_component_get_sizeof(void);
74+
75+
/**
76+
* Initializes allocated memory.
77+
*
78+
* @param data Allocated component data.
79+
*/
80+
void prv_transform_component_init(void* data);

src/engine_lib/include/misc/globals.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,24 @@
66
* @return Application name.
77
*/
88
const char* globals_get_app_name(void);
9+
10+
/**
11+
* Returns a normalized vector that points in the world's forward direction.
12+
*
13+
* @param out Value to write the result to. 4th component is zero.
14+
*/
15+
void globals_get_world_forward(float out[4]);
16+
17+
/**
18+
* Returns a normalized vector that points in the world's right direction.
19+
*
20+
* @param out Value to write the result to. 4th component is zero.
21+
*/
22+
void globals_get_world_right(float out[4]);
23+
24+
/**
25+
* Returns a normalized vector that points in the world's up direction.
26+
*
27+
* @param out Value to write the result to. 4th component is zero.
28+
*/
29+
void globals_get_world_up(float out[4]);

0 commit comments

Comments
 (0)