commit ecffa4be5c0b8ae0ef99e1a7e3e1b27bbc91f742
parent a7cf1d26328c53ea80a8d96b6c9b3e7909b178ff
Author: amin <dev@aminmesbah.com>
Date: Sat, 2 Mar 2019 21:19:09 +0000
Clean up passing of framebuffer dimensions
FossilOrigin-Name: 5f26e741a893ad6af848a95545924ba735a638fb6c00aaa80ee9dddd6323ea27
Diffstat:
6 files changed, 45 insertions(+), 15 deletions(-)
diff --git a/src/game.c b/src/game.c
@@ -41,8 +41,11 @@ float randf(float min, float max)
}
-void game_init(struct GameState *game_state, uint32_t screen_width, uint32_t screen_height)
+void game_init(struct GameState *game_state, v2u framebuffer)
{
+ uint32_t screen_width = framebuffer.width;
+ uint32_t screen_height = framebuffer.height;
+
// set up and load star vertex data
{
float max_speed = 0.3f;
@@ -160,8 +163,10 @@ void game_init(struct GameState *game_state, uint32_t screen_width, uint32_t scr
}
-void game_update_and_render(struct GameState *game_state, float dt, uint32_t screen_width, uint32_t screen_height)
+void game_update_and_render(struct GameState *game_state, float dt, v2u framebuffer)
{
+ uint32_t screen_width = framebuffer.width;
+ uint32_t screen_height = framebuffer.height;
// update stars
{
for (size_t i = 0; i < game_state->stars.num_stars; ++i)
@@ -218,7 +223,7 @@ void game_update_and_render(struct GameState *game_state, float dt, uint32_t scr
shader_setm4(&game_state->triangle_shader, "projection", &projection);
glBindVertexArray(game_state->triangle_vao_id);
- //glDrawArrays(GL_TRIANGLES, 0, 3);
+ glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
}
@@ -226,11 +231,7 @@ void game_update_and_render(struct GameState *game_state, float dt, uint32_t scr
{
m4 model = glmth_m4_init_id();
model = glmth_translate(model, glmth_v3_init(screen_width / 2.0f, screen_height / 2.0f, 0.0f));
- model = glmth_translate(model, glmth_v3_init(100.0f * cosf(dt), 100.0f * sinf(dt), 0.0f));
- model = glmth_rotate_z(model, -dt);
model = glmth_scale(model, glmth_v3_init(10.0f, 10.0f, 10.0f));
- float scale_factor = fabs(1.0f * sinf(dt));
- model = glmth_scale(model, glmth_v3_init(scale_factor, scale_factor, scale_factor));
shader_use(&game_state->star_shader);
shader_setm4(&game_state->star_shader, "model", &model);
diff --git a/src/game.h b/src/game.h
@@ -42,10 +42,10 @@ typedef void (game_load_opengl_symbols_func)(void);
void game_load_opengl_symbols(void);
#endif // PLATFORM_HOTLOAD_GAME_CODE
-typedef void (game_update_and_render_func)(struct GameState *game_state, float dt, uint32_t screen_width, uint32_t screen_height);
-void game_update_and_render(struct GameState *game_state, float dt, uint32_t screen_width, uint32_t screen_height);
+typedef void (game_update_and_render_func)(struct GameState *game_state, float dt, v2u framebuffer);
+void game_update_and_render(struct GameState *game_state, float dt, v2u framebuffer);
-void game_init(struct GameState *game_state, uint32_t screen_width, uint32_t screen_height);
+void game_init(struct GameState *game_state, v2u framebuffer);
void game_cleanup(struct GameState *game_state);
#endif
diff --git a/src/glmth.c b/src/glmth.c
@@ -92,6 +92,11 @@ v4 glmth_m4v4_m(m4 m, v4 v)
return r;
}
+v2u glmth_v2u_init(u32 x, u32 y)
+{
+ v2u v = { .x = x, .y = y };
+ return v;
+}
v3 glmth_v3_cross(v3 vec1, v3 vec2)
{
diff --git a/src/glmth.h b/src/glmth.h
@@ -27,6 +27,10 @@ typedef union
{
f32 x, y;
};
+ struct
+ {
+ f32 width, height;
+ };
f32 E[2];
} v2;
@@ -34,6 +38,19 @@ typedef union
{
struct
{
+ u32 x, y;
+ };
+ struct
+ {
+ u32 width, height;
+ };
+ u32 E[2];
+} v2u;
+
+typedef union
+{
+ struct
+ {
f32 x, y, z;
};
struct
@@ -86,6 +103,11 @@ typedef union
typedef struct
{
+ v2 min, max;
+} rect;
+
+typedef struct
+{
// row-major, so you probably want to transpose before passing to opengl,
// which uses column-major matrices
f32 E[4][4]; // E[row][column]
@@ -98,6 +120,7 @@ f32 *glmth_m4_valueptr(m4 m);
bool glmth_m4m4_eq(m4 mat1, m4 mat2);
m4 glmth_m4m4_m(m4 mat1, m4 mat2);
v4 glmth_m4v4_m(m4 m, v4 v);
+v2u glmth_v2u_init(u32 x, u32 y);
v3 glmth_v3_cross(v3 vec1, v3 vec2);
v3 glmth_v3_init(f32 x, f32 y, f32 z);
v3 glmth_v3_init_f(f32 f);
diff --git a/src/platform_linux.c b/src/platform_linux.c
@@ -11,6 +11,7 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
+#include "glmth.h"
void error_callback(int error, const char* description);
void framebuffer_size_callback(GLFWwindow* window, int width, int height);
@@ -69,7 +70,7 @@ int main(void)
#endif
struct GameState game_state = {0};
- game_init(&game_state, PLATFORM_SCR_WIDTH, PLATFORM_SCR_HEIGHT);
+ game_init(&game_state, glmth_v2u_init(PLATFORM_SCR_WIDTH, PLATFORM_SCR_HEIGHT));
#ifdef PLATFORM_HOTLOAD_GAME_CODE
struct GameCode game_code = load_game_code(PLATFORM_GAME_LIB_PATH);
@@ -104,9 +105,9 @@ int main(void)
// TODO: fall back to backup?
}
}
- game_code.game_update_and_render(&game_state, lag/PLATFORM_SECOND, framebuffer_width, framebuffer_height);
+ game_code.game_update_and_render(&game_state, lag/PLATFORM_SECOND, glmth_v2u_init(framebuffer_width, framebuffer_height));
#else
- game_update_and_render(&game_state, lag/PLATFORM_SECOND, framebuffer_width, framebuffer_height);
+ game_update_and_render(&game_state, lag/PLATFORM_SECOND, glmth_v2u_init(framebuffer_width, framebuffer_height));
#endif // PLATFORM_HOTLOAD_GAME_CODE
glfwSwapBuffers(window);
diff --git a/src/platform_linux.h b/src/platform_linux.h
@@ -6,8 +6,8 @@
#include "game.h"
-#define PLATFORM_SCR_WIDTH 600
-#define PLATFORM_SCR_HEIGHT 600
+#define PLATFORM_SCR_WIDTH 600u
+#define PLATFORM_SCR_HEIGHT 600u
#define PLATFORM_SECOND 1000.0f
#define PLATFORM_FPS 60