11#include "editor.h"
22
3- #include "game/camera .h"
3+ #include "editor_camera .h"
44#include "game/model.h"
55#include "game_manager.h"
6+ #include "window.h"
67#include "world.h"
78
9+ /** Main editor object. */
10+ struct te_editor {
11+ /** Always valid pointer. Must be destroyed during the editor's destruction. */
12+ te_editor_camera * editor_camera ;
13+
14+ /** Not NULL if game world exists. */
15+ te_world * game_world ;
16+ };
17+
18+ te_editor *
19+ editor_create () {
20+ te_editor * editor = malloc (sizeof (te_editor ));
21+ editor -> editor_camera = editor_camera_create ();
22+ editor -> game_world = NULL ;
23+
24+ return editor ;
25+ }
26+
827void
9- editor_on_game_started ( void * game_instance , struct te_game_manager * game_manager ) {
10- ( void ) game_instance ;
28+ editor_destroy ( te_editor * editor ) {
29+ editor_camera_destroy ( editor -> editor_camera ) ;
1130
12- te_world * world = game_manager_create_world (game_manager , "game" );
31+ free (editor );
32+ }
1333
14- te_camera * camera = camera_create ();
15- camera_set_location (camera , (vec3 ){0.0f , 0.0f , 3.0f });
34+ void
35+ editor_on_game_started (void * game_instance , te_game_manager * game_manager ) {
36+ te_editor * editor = game_instance ;
37+ editor_create_game_world (editor , game_manager );
38+ }
39+
40+ void
41+ editor_destroy_game_world (te_editor * editor , te_game_manager * game_manager ) {
42+ // Despawn editor camera because we manage its destruction manually.
43+ editor_camera_despawn (editor -> editor_camera , editor -> game_world );
1644
17- world_spawn_camera (world , camera );
18- world_set_active_camera (world , camera );
45+ // Destroy world.
46+ game_manager_destroy_world (game_manager , editor -> game_world );
47+ editor -> game_world = NULL ;
48+ }
49+
50+ void
51+ editor_create_game_world (te_editor * editor , te_game_manager * game_manager ) {
52+ if (editor -> game_world != NULL ) {
53+ editor_destroy_game_world (editor , game_manager );
54+ }
55+
56+ editor -> game_world = game_manager_create_world (game_manager , "game" );
57+ editor_camera_spawn (editor -> editor_camera , editor -> game_world );
1958
2059 te_model * model = model_create (NULL );
21- world_spawn_model (world , model );
60+ world_spawn_model (editor -> game_world , model );
2261}
2362
2463void
2564editor_on_game_tick (void * game_instance , struct te_game_manager * game_manager , float delta_time_sec ) {
26- (void )game_instance ;
2765 (void )game_manager ;
28- (void )delta_time_sec ;
66+
67+ te_editor * editor = game_instance ;
68+ editor_camera_on_game_tick (editor -> editor_camera , delta_time_sec );
2969}
3070
3171void
3272editor_on_keyboard_button_pressed (void * game_instance , struct te_game_manager * game_manager ,
3373 enum te_keyboard_button button , te_keyboard_modifiers modifiers ) {
34- (void )game_instance ;
3574 (void )game_manager ;
36- (void )button ;
3775 (void )modifiers ;
76+
77+ te_editor * editor = game_instance ;
78+ editor_camera_on_keyboard_button_pressed (editor -> editor_camera , button );
3879}
3980
4081void
4182editor_on_keyboard_button_released (void * game_instance , struct te_game_manager * game_manager ,
4283 enum te_keyboard_button button , te_keyboard_modifiers modifiers ) {
43- (void )game_instance ;
4484 (void )game_manager ;
45- (void )button ;
4685 (void )modifiers ;
86+
87+ te_editor * editor = game_instance ;
88+ editor_camera_on_keyboard_button_released (editor -> editor_camera , button );
4789}
4890
4991void
@@ -65,35 +107,55 @@ editor_on_gamepad_button_released(void* game_instance, struct te_game_manager* g
65107void
66108editor_on_gamepad_axis_moved (void * game_instance , struct te_game_manager * game_manager ,
67109 enum te_gamepad_axis axis , float new_pos ) {
68- (void )game_instance ;
69110 (void )game_manager ;
70- (void )axis ;
71- (void )new_pos ;
111+
112+ te_editor * editor = game_instance ;
113+ editor_camera_on_gamepad_axis_moved (editor -> editor_camera , axis , new_pos );
72114}
73115
74116void
75117editor_on_mouse_button_pressed (void * game_instance , struct te_game_manager * game_manager ,
76118 enum te_mouse_button button ) {
77- (void )game_instance ;
78- (void )game_manager ;
79- (void )button ;
119+ te_editor * editor = game_instance ;
120+
121+ if (button == TE_MB_RIGHT ) {
122+ if (editor -> game_world == NULL ) {
123+ // No point in doing something.
124+ return ;
125+ }
126+
127+ te_window * window = game_manager_get_window (game_manager );
128+ window_capture_mouse_cursor (window , true);
129+
130+ editor_camera_enable_input (editor -> editor_camera , true);
131+ }
80132}
81133
82134void
83135editor_on_mouse_button_released (void * game_instance , struct te_game_manager * game_manager ,
84136 enum te_mouse_button button ) {
85- (void )game_instance ;
86- (void )game_manager ;
87- (void )button ;
137+ te_editor * editor = game_instance ;
138+
139+ if (button == TE_MB_RIGHT ) {
140+ if (editor -> game_world == NULL ) {
141+ // No point in doing something.
142+ return ;
143+ }
144+
145+ te_window * window = game_manager_get_window (game_manager );
146+ window_capture_mouse_cursor (window , false);
147+
148+ editor_camera_enable_input (editor -> editor_camera , false);
149+ }
88150}
89151
90152void
91153editor_on_mouse_moved (void * game_instance , struct te_game_manager * game_manager , float x_offset ,
92154 float y_offset ) {
93- (void )game_instance ;
94155 (void )game_manager ;
95- (void )x_offset ;
96- (void )y_offset ;
156+
157+ te_editor * editor = game_instance ;
158+ editor_camera_on_mouse_moved (editor -> editor_camera , x_offset , y_offset );
97159}
98160
99161void
@@ -106,15 +168,19 @@ editor_on_mouse_scroll_moved(void* game_instance, struct te_game_manager* game_m
106168void
107169editor_on_gamepad_connected (void * game_instance , struct te_game_manager * game_manager ,
108170 const char * gamepad_name ) {
109- (void )game_instance ;
110171 (void )game_manager ;
111172 (void )gamepad_name ;
173+
174+ te_editor * editor = game_instance ;
175+ editor_camera_on_gamepad_connected (editor -> editor_camera );
112176}
113177
114178void
115179editor_on_gamepad_disconnected (void * game_instance , struct te_game_manager * game_manager ) {
116- (void )game_instance ;
117180 (void )game_manager ;
181+
182+ te_editor * editor = game_instance ;
183+ editor_camera_on_gamepad_disconnected (editor -> editor_camera );
118184}
119185
120186void
@@ -133,12 +199,25 @@ editor_on_window_received_focus(void* game_instance, struct te_game_manager* gam
133199
134200void
135201editor_on_window_lost_focus (void * game_instance , struct te_game_manager * game_manager ) {
136- (void )game_instance ;
137202 (void )game_manager ;
203+
204+ te_editor * editor = game_instance ;
205+ if (editor -> game_world == NULL ) {
206+ // No point in doing something.
207+ return ;
208+ }
209+
210+ te_window * window = game_manager_get_window (game_manager );
211+ window_capture_mouse_cursor (window , false);
212+
213+ editor_camera_enable_input (editor -> editor_camera , false);
138214}
139215
140216void
141217editor_on_window_close (void * game_instance , struct te_game_manager * game_manager ) {
142- (void )game_instance ;
143- (void )game_manager ;
218+ te_editor * editor = game_instance ;
219+
220+ if (editor -> game_world != NULL ) {
221+ editor_destroy_game_world (editor , game_manager );
222+ }
144223}
0 commit comments