a-game

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

commit 0932526a570609afc69439f7ffb61bf805a88a7d
parent 642c0708354d8b1cf8b2fd9d790a7a959f8e40da
Author: amin <dev@aminmesbah.com>
Date:   Mon,  4 Mar 2019 00:31:31 +0000

Render an obnoxious 'tile map'

FossilOrigin-Name: 015ad4077dabc3ce4a4ea7b7606eb4cdd3c1e242d44d6d64c481c19c9ee036cf
Diffstat:
Mshader/star_v.glsl | 3++-
Msrc/game.c | 65++++++++++++++++++++++++++++++++++++++++++++++++++---------------
Msrc/shader.c | 8++++++++
3 files changed, 60 insertions(+), 16 deletions(-)

diff --git a/shader/star_v.glsl b/shader/star_v.glsl @@ -6,9 +6,10 @@ out vec4 vertex_color; uniform mat4 model; uniform mat4 projection; +uniform vec3 color; void main() { gl_Position = projection * model * vec4(position, 0.0f, 1.0f); - vertex_color = vec4(1.0f, 1.0f, 1.0f, 1.0f); + vertex_color = vec4(color, 1.0f); } diff --git a/src/game.c b/src/game.c @@ -116,10 +116,10 @@ void game_init(struct GameState *game_state, v2u framebuffer) // set up and load tiles { GLfloat square_vertices[] = { - 1.0f, 1.0f, - 1.0f, -1.0f, - -1.0f, -1.0f, - -1.0f, 1.0f, + 0.5f, 0.5f, + 0.5f, -0.5f, + -0.5f, -0.5f, + -0.5f, 0.5f, }; GLuint square_elements[] = { 0, 1, 3, 1, 2, 3 }; @@ -197,6 +197,8 @@ void game_update_and_render(struct GameState *game_state, float dt, v2u framebuf 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); + v3 color = glmth_v3_init(1.0f, 1.0f, 1.0f); + shader_setv3(&game_state->star_shader, "color", &color); glBindVertexArray(game_state->star_vao_id); glDrawArrays(GL_POINTS, 0, game_state->stars.num_stars - 1); @@ -227,19 +229,52 @@ void game_update_and_render(struct GameState *game_state, float dt, v2u framebuf // 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_scale(model, glmth_v3_init(10.0f, 10.0f, 10.0f)); - + glBindVertexArray(game_state->tiles.vao); 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); + uint32_t tile_map[9][16] = { + { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, }, + { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, }, + { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, }, + + { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, }, + { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, }, + { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, }, + + { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, }, + { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, }, + { 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, }, + }; - glBindVertexArray(game_state->tiles.vao); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + uint32_t start = 25; + float size = fabsf(100 * sinf(dt)); + for (size_t y = 0; y < 9; y++) + { + for (size_t x = 0; x < 16; x++) + { + uint32_t tile_id = tile_map[y][x]; + m4 model = glmth_m4_init_id(); + model = glmth_translate(model, glmth_v3_init(start + (size * x), start + (size * y), 0.0f)); + model = glmth_scale(model, glmth_v3_init(size, size, 1.0f)); + + shader_setm4(&game_state->star_shader, "model", &model); + v3 color; + if (tile_id > 0) + { + color = glmth_v3_init(0.0f, 1.0f, 0.0f); + } + else + { + color = glmth_v3_init(1.0f, 0.0f, 0.0f); + } + shader_setv3(&game_state->star_shader, "color", &color); + + 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); + + glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); + } + } glBindVertexArray(0); } } diff --git a/src/shader.c b/src/shader.c @@ -27,6 +27,8 @@ char *read_file(char *file_path) else { printf("Error: Couldn't open file at path: %s", file_path); + // TODO: handle errors here in a better way + exit(1); } return buffer; @@ -49,6 +51,8 @@ struct Shader shader_compile(GLchar *vertex_path, GLchar *fragment_path) { glGetShaderInfoLog(vertex_shader, 512, NULL, info_log); printf("ERROR::SHADER::VERTEX::COMPILATION_FAILED\n %s\n", info_log); + // TODO: handle errors here in a better way + exit(1); } GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER); @@ -59,6 +63,8 @@ struct Shader shader_compile(GLchar *vertex_path, GLchar *fragment_path) { glGetShaderInfoLog(fragment_shader, 512, NULL, info_log); printf("ERROR::SHADER::FRAGMENT::COMPILATION_FAILED\n %s\n", info_log); + // TODO: handle errors here in a better way + exit(1); } struct Shader s; @@ -71,6 +77,8 @@ struct Shader shader_compile(GLchar *vertex_path, GLchar *fragment_path) { glGetShaderInfoLog(s.program, 512, NULL, info_log); printf("ERROR::SHADER::LINKING_FAILED\n %s\n", info_log); + // TODO: handle errors here in a better way + exit(1); } glDeleteShader(fragment_shader);