a-game

2D platformer written from scratch.
git clone git://git.amin.space/a-game.git
Log | Files | Refs | README | LICENSE

commit cc6a022c5d380463917e0f8ec3c5abce347dc14a
parent be373a7d2000d67d16196cd02b27e577828c334a
Author: amin <dev@aminmesbah.com>
Date:   Fri,  1 Jun 2018 06:44:15 +0000

Scale, rotate, and tranlate a triangle

FossilOrigin-Name: afb153410f0f4feca9616758ab2deec598e17cb6514cd565423973c99b3a52b2
Diffstat:
Mshader/triangle_f.glsl | 4++--
Mshader/triangle_v.glsl | 4+++-
Msrc/game.c | 14+++++++++++---
Msrc/game.h | 6+++---
Msrc/platform_linux.c | 6+++---
5 files changed, 22 insertions(+), 12 deletions(-)

diff --git a/shader/triangle_f.glsl b/shader/triangle_f.glsl @@ -2,9 +2,9 @@ in vec4 vertex_color; -out vec4 color; +out vec4 frag_color; void main() { - color = vertex_color; + frag_color = vertex_color; } diff --git a/shader/triangle_v.glsl b/shader/triangle_v.glsl @@ -5,8 +5,10 @@ layout (location = 1) in vec3 color; out vec4 vertex_color; +uniform mat4 transform; + void main() { - gl_Position = vec4(position, 0.0f, 1.0f); + gl_Position = transform * vec4(position, 0.0f, 1.0f); vertex_color = vec4(color, 1.0f); } diff --git a/src/game.c b/src/game.c @@ -1,5 +1,7 @@ #include "game.h" +#include <math.h> + void game_init(struct GameState *game_state) { @@ -33,20 +35,26 @@ void game_init(struct GameState *game_state) } -void game_update_and_render(struct GameState *game_state) +void game_update_and_render(struct GameState *game_state, float dt) { glClearColor(0.1f, 0.1f, 0.1f, 1.0f); glClear(GL_COLOR_BUFFER_BIT); - glUseProgram(game_state->triangle_shader.program); + m4 trans = glmth_m4_init_id(); + trans = glmth_translate(trans, glmth_v3_init(0.5f * cosf(dt), 0.5f * sinf(dt), 0.0f)); + float scale_factor = fabs(1.0f * sinf(dt)); + trans = glmth_scale(trans, glmth_v3_init(scale_factor, scale_factor, scale_factor)); + trans = glmth_rotate_z(trans, -dt); + shader_use(&game_state->triangle_shader); + shader_setm4(&game_state->triangle_shader, "transform", &trans); glBindVertexArray(game_state->vao_id); glDrawArrays(GL_TRIANGLES, 0, 3); glBindVertexArray(0); } -void game_deinit(struct GameState *game_state) +void game_cleanup(struct GameState *game_state) { glDeleteVertexArrays(1, &game_state->vao_id); glDeleteBuffers(1, &game_state->vbo_id); diff --git a/src/game.h b/src/game.h @@ -11,10 +11,10 @@ struct GameState struct Shader triangle_shader; }; -typedef void (game_update_and_render_func)(struct GameState *game_state); +typedef void (game_update_and_render_func)(struct GameState *game_state, float dt); void game_init(struct GameState *game_state); -void game_update_and_render(struct GameState *game_state); -void game_deinit(struct GameState *game_state); +void game_update_and_render(struct GameState *game_state, float dt); +void game_cleanup(struct GameState *game_state); #endif diff --git a/src/platform_linux.c b/src/platform_linux.c @@ -85,16 +85,16 @@ int main(void) // TODO: fall back to backup? } } - game_code.game_update_and_render(&game_state); + game_code.game_update_and_render(&game_state, lag/PLATFORM_SECOND); #else - game_update_and_render(&game_state); + game_update_and_render(&game_state, lag/PLATFORM_SECOND); #endif // PLATFORM_HOTLOAD_GAME_CODE glfwSwapBuffers(window); glfwPollEvents(); } - game_deinit(&game_state); + game_cleanup(&game_state); glfwDestroyWindow(window); glfwTerminate();