Skip to content

Commit f277a8c

Browse files
committed
Implement editor camera.
1 parent 2030e93 commit f277a8c

16 files changed

Lines changed: 566 additions & 60 deletions

File tree

res/engine/shader/model.frag.glsl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ void main(void) {
88
// Normals may be unnormalized after the rasterization (when they are interpolated).
99
vec3 frag_normal_unit = normalize(frag_normal);
1010

11-
vec4 out_color = color;
11+
vec4 out_color = color;
1212

1313
// TODO: dummy usage
14-
if (frag_normal.x < -5.0 || frag_uv.x < 0.5) {
15-
out_color.r += 0.5;
14+
if (frag_normal.x < -5.0 || frag_uv.x < -0.5) {
15+
out_color.r += 0.5;
1616
}
1717

1818
gl_FragColor = out_color;

src/editor/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
set(PROJECT_SOURCES
88
src/main.c
99
src/editor.c
10+
src/editor_camera.c
1011
include/editor.h
12+
include/editor_camera.h
1113
)
1214

1315
add_executable(${PROJECT_NAME} ${PROJECT_SOURCES})

src/editor/include/editor.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,30 @@
66

77
struct te_game_manager;
88

9+
typedef struct te_editor te_editor;
10+
11+
/**
12+
* Creates a new editor instance.
13+
*
14+
* @return Editor.
15+
*/
16+
te_editor* editor_create();
17+
18+
/**
19+
* Destroys editor instance.
20+
*
21+
* @param editor Editor.
22+
*/
23+
void editor_destroy(te_editor* editor);
24+
25+
/**
26+
* Creates a new game world.
27+
*
28+
* @param editor Editor.
29+
* @param game_manager Game manager.
30+
*/
31+
void editor_create_game_world(te_editor* editor, struct te_game_manager* game_manager);
32+
933
/// @cond UNDOCUMENTED
1034
// window callbacks -------------------------------------------------------------------------------
1135

src/editor/include/editor_camera.h

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#pragma once
2+
3+
#include <stdbool.h>
4+
#include "input/gamepad_button.h"
5+
#include "input/keyboard_button.h"
6+
7+
/** AKA viewport camera (holds an actual camera inside of it). */
8+
typedef struct te_editor_camera te_editor_camera;
9+
10+
struct te_world;
11+
12+
/**
13+
* Creates a new viewport camera.
14+
*
15+
* @return Viewport camera.
16+
*/
17+
te_editor_camera* editor_camera_create();
18+
19+
/**
20+
* Destroys the viewport camera.
21+
*
22+
* @param editor_camera Viewport camera.
23+
*/
24+
void editor_camera_destroy(te_editor_camera* editor_camera);
25+
26+
/**
27+
* Spawns the viewport camera in the specified world and makes the camera active.
28+
*
29+
* @remark @ref editor_camera_despawn must be called before the world is destroyed.
30+
*
31+
* @param editor_camera Viewport camera.
32+
* @param world World.
33+
*/
34+
void editor_camera_spawn(te_editor_camera* editor_camera, struct te_world* world);
35+
36+
/**
37+
* Despawns the viewport camera from the specified world.
38+
*
39+
* @param editor_camera Viewport camera.
40+
* @param world World.
41+
*/
42+
void editor_camera_despawn(te_editor_camera* editor_camera, struct te_world* world);
43+
44+
/**
45+
* Sets whether the camera should react to the input or not.
46+
*
47+
* @param editor_camera Camera.
48+
* @param enable `true` to enable.
49+
*/
50+
void editor_camera_enable_input(te_editor_camera* editor_camera, bool enable);
51+
52+
/// @cond UNDOCUMENTED
53+
// callbacks -------------------------------------------------------------------------------
54+
55+
void editor_camera_on_mouse_moved(te_editor_camera* editor_camera, float x_offset, float y_offset);
56+
57+
void editor_camera_on_gamepad_axis_moved(te_editor_camera* editor_camera, enum te_gamepad_axis axis,
58+
float new_pos);
59+
60+
void editor_camera_on_keyboard_button_pressed(te_editor_camera* editor_camera,
61+
enum te_keyboard_button button);
62+
63+
void editor_camera_on_keyboard_button_released(te_editor_camera* editor_camera,
64+
enum te_keyboard_button button);
65+
66+
void editor_camera_on_game_tick(te_editor_camera* editor_camera, float delta_time_sec);
67+
68+
void editor_camera_on_gamepad_connected(te_editor_camera* editor_camera);
69+
void editor_camera_on_gamepad_disconnected(te_editor_camera* editor_camera);
70+
71+
// @endcond ------------------------------------------------------------------------------------------------

src/editor/src/editor.c

Lines changed: 111 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,91 @@
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+
827
void
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

2463
void
2564
editor_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

3171
void
3272
editor_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

4081
void
4182
editor_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

4991
void
@@ -65,35 +107,55 @@ editor_on_gamepad_button_released(void* game_instance, struct te_game_manager* g
65107
void
66108
editor_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

74116
void
75117
editor_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

82134
void
83135
editor_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

90152
void
91153
editor_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

99161
void
@@ -106,15 +168,19 @@ editor_on_mouse_scroll_moved(void* game_instance, struct te_game_manager* game_m
106168
void
107169
editor_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

114178
void
115179
editor_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

120186
void
@@ -133,12 +199,25 @@ editor_on_window_received_focus(void* game_instance, struct te_game_manager* gam
133199

134200
void
135201
editor_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

140216
void
141217
editor_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

Comments
 (0)