a-game

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

commit a7cf1d26328c53ea80a8d96b6c9b3e7909b178ff
parent 9b48857eac063a406f73886d2fc5a6a1fb41213b
Author: amin <dev@aminmesbah.com>
Date:   Sat,  2 Mar 2019 20:25:52 +0000

Render a tile

FossilOrigin-Name: b0aa41e9492efe00dd9fceb42a8b99a790c1361a06c34b492b53eca9369ae14a
Diffstat:
Msrc/game.c | 67++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Msrc/game.h | 9+++++++++
Msrc/platform_linux.c | 2+-
3 files changed, 76 insertions(+), 2 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -112,6 +112,49 @@ void game_init(struct GameState *game_state, uint32_t screen_width, uint32_t scr game_state->triangle_vbo_id = triangle_vbo_id; } + // set up and load tiles + { + GLfloat square_vertices[] = { + 1.0f, 1.0f, + 1.0f, -1.0f, + -1.0f, -1.0f, + -1.0f, 1.0f, + }; + + GLuint square_elements[] = { 0, 1, 3, 1, 2, 3 }; + + GLuint vao; + GLuint vbo; + GLuint ebo; + glGenVertexArrays(1, &vao); + glGenBuffers(1, &vbo); + glGenBuffers(1, &ebo); + + glBindVertexArray(vao); + + glBindBuffer(GL_ARRAY_BUFFER, vbo); + glBufferData(GL_ARRAY_BUFFER, sizeof(square_vertices), square_vertices, GL_STATIC_DRAW); + glVertexAttribPointer( + 0, // Data location + 2, // Number of values + GL_FLOAT, // Data type + GL_FALSE, // Normalize data + 2 * sizeof(GLfloat), // Stride + (GLvoid*)0 // Position data offset (0) + ); + + glEnableVertexAttribArray(0); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ebo); + glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(square_elements), square_elements, GL_STATIC_DRAW); + + glBindVertexArray(0); + + game_state->tiles.vao = vao; + game_state->tiles.vbo = vbo; + game_state->tiles.ebo = vbo; + game_state->tiles.count = 1; + } + game_state->star_shader = shader_compile("shader/star_v.glsl", "shader/star_f.glsl"); game_state->triangle_shader = shader_compile("shader/triangle_v.glsl", "shader/triangle_f.glsl"); } @@ -175,7 +218,29 @@ 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); + } + + // render tiles + { + 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); + + m4 projection = glmth_m4_init_id(); + projection = glmth_projection_ortho(0.0f, screen_width, screen_height, 0.0f, -1.0f, 1.0f); + shader_setm4(&game_state->star_shader, "projection", &projection); + + glBindVertexArray(game_state->tiles.vao); + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); glBindVertexArray(0); } } diff --git a/src/game.h b/src/game.h @@ -14,8 +14,17 @@ struct Stars v2 positions[NUM_STARS]; }; +struct Tiles +{ + GLuint vao; + GLuint vbo; + GLuint ebo; + uint32_t count; +}; + struct GameState { + struct Tiles tiles; GLuint star_vao_id; GLuint star_vbo_id; GLuint triangle_vao_id; diff --git a/src/platform_linux.c b/src/platform_linux.c @@ -42,7 +42,7 @@ int main(void) glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - glfwWindowHint(GLFW_RESIZABLE, GL_FALSE); + glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); GLFWwindow* window = glfwCreateWindow(PLATFORM_SCR_WIDTH, PLATFORM_SCR_HEIGHT, "A Game", NULL, NULL); if (!window)