commit 801c2331cd47046204d5845ed9c4c3bffe5100c2
parent f1d93cae98cedbfea5f5f788fa63327f5f233f28
Author: amin <dev@aminmesbah.com>
Date: Mon, 28 May 2018 22:02:40 +0000
Pull game setup out of platform layer
FossilOrigin-Name: 24d809d044427b95fb67627ea2a0cb438397c162c11afd8cec532c0e6b2fb063
Diffstat:
3 files changed, 58 insertions(+), 37 deletions(-)
diff --git a/src/game.c b/src/game.c
@@ -1,14 +1,53 @@
#include "game.h"
-#include <glad/glad.h>
-void game_update_and_render(GLuint vao_id, struct Shader *triangle_shader)
+
+void game_init(struct GameState *game_state)
+{
+ GLfloat triangle_vertices[] = {
+ // positions // colors
+ -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
+ 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
+ 0.0f, 0.5f, 0.0f, 0.0f, 1.0f,
+ };
+
+ GLuint vbo_id;
+ GLuint vao_id;
+ glGenVertexArrays(1, &vao_id);
+ glGenBuffers(1, &vbo_id);
+
+ glBindVertexArray(vao_id);
+ glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
+ glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_vertices), triangle_vertices, GL_STATIC_DRAW);
+
+ // positions
+ glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(*triangle_vertices), (GLvoid*)0);
+ glEnableVertexAttribArray(0);
+
+ // colors
+ glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(*triangle_vertices), (GLvoid*)(2 * sizeof(*triangle_vertices)));
+ glEnableVertexAttribArray(1);
+ glBindVertexArray(0);
+
+ game_state->vao_id = vao_id;
+ game_state->triangle_shader = shader_compile("shader/triangle_v.glsl", "shader/triangle_f.glsl");
+}
+
+
+void game_update_and_render(struct GameState *game_state)
{
glClearColor(0.1f, 0.1f, 0.1f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT);
- glUseProgram(triangle_shader->program);
+ glUseProgram(game_state->triangle_shader.program);
- glBindVertexArray(vao_id);
+ glBindVertexArray(game_state->vao_id);
glDrawArrays(GL_TRIANGLES, 0, 3);
glBindVertexArray(0);
}
+
+
+void game_deinit(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
@@ -1,8 +1,18 @@
#ifndef GAME_H
#define GAME_H
+#include "glad/glad.h"
#include "shader.h"
-void game_update_and_render(GLuint vao_id, struct Shader *triangle_shader);
+struct GameState
+{
+ GLuint vao_id;
+ GLuint vbo_id;
+ struct Shader triangle_shader;
+};
+
+void game_init(struct GameState *game_state);
+void game_update_and_render(struct GameState *game_state);
+void game_deinit(struct GameState *game_state);
#endif
diff --git a/src/platform_linux.c b/src/platform_linux.c
@@ -3,8 +3,6 @@
#include <glad/glad.h>
#include <GLFW/glfw3.h>
-#include "shader.h"
-#include "glmth.h"
#include "game.h"
@@ -52,43 +50,17 @@ int main(void)
return -1;
}
- struct Shader triangle_shader = shader_compile("shader/triangle_v.glsl", "shader/triangle_f.glsl");
-
- GLfloat triangle_vertices[] = {
- // positions // colors
- -0.5f, -0.5f, 1.0f, 0.0f, 0.0f,
- 0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
- 0.0f, 0.5f, 0.0f, 0.0f, 1.0f,
- };
-
- GLuint vbo_id;
- GLuint vao_id;
- glGenVertexArrays(1, &vao_id);
- glGenBuffers(1, &vbo_id);
-
- glBindVertexArray(vao_id);
- glBindBuffer(GL_ARRAY_BUFFER, vbo_id);
- glBufferData(GL_ARRAY_BUFFER, sizeof(triangle_vertices), triangle_vertices, GL_STATIC_DRAW);
-
- // positions
- // TODO: make sizeof dereference pointer
- glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)0);
- glEnableVertexAttribArray(0);
-
- // colors
- glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 5 * sizeof(GLfloat), (GLvoid*)(2 * sizeof(GLfloat)));
- glEnableVertexAttribArray(1);
- glBindVertexArray(0);
+ struct GameState game_state = {0};
+ game_init(&game_state);
while (!glfwWindowShouldClose(window))
{
- game_update_and_render(vao_id, &triangle_shader);
+ game_update_and_render(&game_state);
glfwSwapBuffers(window);
glfwPollEvents();
}
- glDeleteVertexArrays(1, &vao_id);
- glDeleteBuffers(1, &vbo_id);
+ game_deinit(&game_state);
glfwDestroyWindow(window);
glfwTerminate();