diff options
Diffstat (limited to 'app/src/app.c')
-rw-r--r-- | app/src/app.c | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/app/src/app.c b/app/src/app.c index e1edf88..a7bb9c9 100644 --- a/app/src/app.c +++ b/app/src/app.c | |||
@@ -24,7 +24,7 @@ static GfxApp g_gfx_app; | |||
24 | 24 | ||
25 | /// Called by GLFW when the window is resized. | 25 | /// Called by GLFW when the window is resized. |
26 | static void on_resize(GLFWwindow* window, int width, int height) { | 26 | static void on_resize(GLFWwindow* window, int width, int height) { |
27 | (*g_gfx_app.callbacks.resize)(g_gfx_app.app_state, width, height); | 27 | (*g_gfx_app.callbacks.resize)(&g_gfx_app, g_gfx_app.app_state, width, height); |
28 | } | 28 | } |
29 | 29 | ||
30 | /// Run the application's main loop. | 30 | /// Run the application's main loop. |
@@ -41,12 +41,13 @@ static void loop(GfxApp* app) { | |||
41 | 41 | ||
42 | // Warm up the update to initialize the application's state. | 42 | // Warm up the update to initialize the application's state. |
43 | (*app->callbacks.update)( | 43 | (*app->callbacks.update)( |
44 | app->app_state, time_delta_to_sec(time), time_delta_to_sec(update_dt)); | 44 | app, app->app_state, time_delta_to_sec(time), |
45 | time_delta_to_sec(update_dt)); | ||
45 | 46 | ||
46 | // Warm up the rendering before entering the main loop. A renderer can | 47 | // Warm up the rendering before entering the main loop. A renderer can |
47 | // compile shaders and do other initialization the first time it renders a | 48 | // compile shaders and do other initialization the first time it renders a |
48 | // scene. | 49 | // scene. |
49 | (*app->callbacks.render)(app->app_state); | 50 | (*app->callbacks.render)(app, app->app_state); |
50 | glfwSwapBuffers(app->window); | 51 | glfwSwapBuffers(app->window); |
51 | 52 | ||
52 | timer_start(&timer); | 53 | timer_start(&timer); |
@@ -56,14 +57,14 @@ static void loop(GfxApp* app) { | |||
56 | 57 | ||
57 | while (time_budget >= update_dt) { | 58 | while (time_budget >= update_dt) { |
58 | (*app->callbacks.update)( | 59 | (*app->callbacks.update)( |
59 | app->app_state, time_delta_to_sec(time), | 60 | app, app->app_state, time_delta_to_sec(time), |
60 | time_delta_to_sec(update_dt)); | 61 | time_delta_to_sec(update_dt)); |
61 | 62 | ||
62 | time += update_dt; | 63 | time += update_dt; |
63 | time_budget -= update_dt; | 64 | time_budget -= update_dt; |
64 | } | 65 | } |
65 | 66 | ||
66 | (*app->callbacks.render)(app->app_state); | 67 | (*app->callbacks.render)(app, app->app_state); |
67 | glfwSwapBuffers(app->window); | 68 | glfwSwapBuffers(app->window); |
68 | glfwPollEvents(); | 69 | glfwPollEvents(); |
69 | 70 | ||
@@ -85,7 +86,7 @@ bool gfx_app_run(const GfxAppDesc* desc, const GfxAppCallbacks* callbacks) { | |||
85 | g_gfx_app.callbacks = *callbacks; | 86 | g_gfx_app.callbacks = *callbacks; |
86 | g_gfx_app.max_fps = desc->max_fps; | 87 | g_gfx_app.max_fps = desc->max_fps; |
87 | g_gfx_app.update_delta_time = desc->update_delta_time; | 88 | g_gfx_app.update_delta_time = desc->update_delta_time; |
88 | g_gfx_app.window = 0; | 89 | g_gfx_app.window = nullptr; |
89 | 90 | ||
90 | if (!glfwInit()) { | 91 | if (!glfwInit()) { |
91 | LOGE("glfwInit() failed"); | 92 | LOGE("glfwInit() failed"); |
@@ -124,20 +125,21 @@ bool gfx_app_run(const GfxAppDesc* desc, const GfxAppCallbacks* callbacks) { | |||
124 | 125 | ||
125 | // Initialize the application's state before setting any callbacks. | 126 | // Initialize the application's state before setting any callbacks. |
126 | if (!(*g_gfx_app.callbacks.init)( | 127 | if (!(*g_gfx_app.callbacks.init)( |
127 | g_gfx_app.app_state, desc->argc, desc->argv)) { | 128 | &g_gfx_app, g_gfx_app.app_state, desc->argc, desc->argv)) { |
128 | LOGE("Failed to initialize application"); | 129 | LOGE("Failed to initialize application"); |
129 | goto cleanup; | 130 | goto cleanup; |
130 | } | 131 | } |
131 | 132 | ||
132 | // Trigger an initial resize for convenience. | 133 | // Trigger an initial resize for convenience. |
133 | (*g_gfx_app.callbacks.resize)(g_gfx_app.app_state, desc->width, desc->height); | 134 | (*g_gfx_app.callbacks.resize)( |
135 | &g_gfx_app, g_gfx_app.app_state, desc->width, desc->height); | ||
134 | 136 | ||
135 | // Set GLFW callbacks now that the application has been initialized. | 137 | // Set GLFW callbacks now that the application has been initialized. |
136 | glfwSetWindowSizeCallback(g_gfx_app.window, on_resize); | 138 | glfwSetWindowSizeCallback(g_gfx_app.window, on_resize); |
137 | 139 | ||
138 | loop(&g_gfx_app); | 140 | loop(&g_gfx_app); |
139 | 141 | ||
140 | (*g_gfx_app.callbacks.shutdown)(g_gfx_app.app_state); | 142 | (*g_gfx_app.callbacks.shutdown)(&g_gfx_app, g_gfx_app.app_state); |
141 | 143 | ||
142 | success = true; | 144 | success = true; |
143 | 145 | ||
@@ -149,21 +151,21 @@ cleanup: | |||
149 | return success; | 151 | return success; |
150 | } | 152 | } |
151 | 153 | ||
152 | void gfx_app_get_mouse_position(double* x, double* y) { | 154 | void gfx_app_get_mouse_position(GfxApp* app, double* x, double* y) { |
153 | glfwGetCursorPos(g_gfx_app.window, x, y); | 155 | glfwGetCursorPos(app->window, x, y); |
154 | } | 156 | } |
155 | 157 | ||
156 | static int to_glfw_mouse_button(MouseButton button); | 158 | static int to_glfw_mouse_button(MouseButton button); |
157 | 159 | ||
158 | bool gfx_app_is_mouse_button_pressed(MouseButton button) { | 160 | bool gfx_app_is_mouse_button_pressed(GfxApp* app, MouseButton button) { |
159 | return glfwGetMouseButton(g_gfx_app.window, to_glfw_mouse_button(button)) == | 161 | return glfwGetMouseButton(app->window, to_glfw_mouse_button(button)) == |
160 | GLFW_PRESS; | 162 | GLFW_PRESS; |
161 | } | 163 | } |
162 | 164 | ||
163 | static int to_glfw_key(Key key); | 165 | static int to_glfw_key(Key key); |
164 | 166 | ||
165 | bool gfx_app_is_key_pressed(Key key) { | 167 | bool gfx_app_is_key_pressed(GfxApp* app, Key key) { |
166 | return glfwGetKey(g_gfx_app.window, to_glfw_key(key)) == GLFW_PRESS; | 168 | return glfwGetKey(app->window, to_glfw_key(key)) == GLFW_PRESS; |
167 | } | 169 | } |
168 | 170 | ||
169 | static int to_glfw_mouse_button(MouseButton button) { | 171 | static int to_glfw_mouse_button(MouseButton button) { |