Skip to content

Instantly share code, notes, and snippets.

@mojobojo
Last active April 25, 2018 00:23
Show Gist options
  • Save mojobojo/933f99ec45213cd63f83d3a4152c669f to your computer and use it in GitHub Desktop.
Save mojobojo/933f99ec45213cd63f83d3a4152c669f to your computer and use it in GitHub Desktop.
void Render_DrawTileMap(dv2 position, i32 *tile_data, i32 world_width, i32 world_height, TileMap tile_map) {
GLuint texture_id = tile_map.image.id;
GLint program_id = tile_draw_shader.id;
f64 tile_scale_x = 1.0 / (f64)tile_map.image.width * (f64)tile_map.width;
f64 tile_scale_y = 1.0 / (f64)tile_map.image.height * (f64)tile_map.height;
f64 size_x = 1.0 * meters_to_pixels;
f64 size_y = 1.0 * meters_to_pixels;
glUseProgram(program_id);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glBindBuffer(GL_ARRAY_BUFFER, rectangle_vertex_buffer_object);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(f32), (void *)0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 4 * sizeof(f32), (void *)(2 * sizeof(f32)));
GLint InputTextureUniform = glGetUniformLocation(program_id, "Texture");
GLint tile_position_uniform = glGetUniformLocation(program_id, "TilePosition");
GLint tile_scale_uniform = glGetUniformLocation(program_id, "TileScale");
GLint position_uniform = glGetUniformLocation(program_id, "Position");
GLint size_uniform = glGetUniformLocation(program_id, "Size");
GLint mvp_uniform = glGetUniformLocation(program_id, "MVP");
for (i32 y = 0; y < world_height; ++y) {
for (i32 x = 0; x < world_width; ++x) {
i32 tile = tile_data[x + (y * world_width)];
u16 t_x = (tile >> 16) & 0xFFFF;
u16 t_y = tile & 0xFFFF;
if (t_x != 0xFFFF && t_y != 0xFFFF) {
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture_id);
glUniform1i(InputTextureUniform, GL_TEXTURE0);
glUniform2f(tile_position_uniform, tile_scale_x * t_x, tile_scale_y * t_y);
glUniform2f(tile_scale_uniform, (GLfloat)tile_scale_x, (GLfloat)tile_scale_y);
f64 position_x = world_render_position.x + (x * meters_to_pixels) - size_x * 0.5;
f64 position_y = world_render_position.y + (y * meters_to_pixels) - size_y * 0.5;
glUniform2f(position_uniform, (GLfloat)position_x, (GLfloat)position_y);
glUniform2f(size_uniform, (GLfloat)size_y, (GLfloat)size_x);
glUniformMatrix4fv(mvp_uniform, 1, GL_FALSE, glm::value_ptr(mvp_matrix));
glDrawArrays(GL_TRIANGLES, 0, 6);
}
}
}
glDisableVertexAttribArray(0);
glDisableVertexAttribArray(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment