polyadvent

A game engine from scratch in C
git clone git://jb55.com/polyadvent
Log | Files | Refs | README

commit e740e04468efd16f37bd85007ea3b4b25966661e
parent 2f22b32f05071778a734e97087590d555546bb40
Author: William Casarin <jb55@jb55.com>
Date:   Wed,  3 Nov 2021 22:00:40 -0700

tileset wip

Diffstat:
M.envrc | 1+
Metc/shaders/rogue/grid.v.glsl | 6++++--
Metc/shaders/rogue/main.f.glsl | 4++--
Metc/shaders/rogue/uniforms.glsl | 2++
Metc/shaders/skybox.f.glsl | 1-
Mmain.c | 2++
Mrogue/rogue.c | 36++++++++++++++++++++++++++++++++++--
Mrogue/rogue.h | 1+
Msrc/grid.c | 6+++++-
9 files changed, 51 insertions(+), 8 deletions(-)

diff --git a/.envrc b/.envrc @@ -1,2 +1,3 @@ use nix export CC=gcc +export TODO_FILE=TODO diff --git a/etc/shaders/rogue/grid.v.glsl b/etc/shaders/rogue/grid.v.glsl @@ -2,6 +2,7 @@ #include profile in vec3 position; +in vec2 tex_coord; in vec3 normal; #include rogue/uniforms.glsl @@ -9,12 +10,13 @@ in vec3 normal; uniform int is_white; out shader_data { -#include shadervars.glsl +#include rogue/vars.glsl } data_out; void main() { - vec3 color = position; + vec3 color = 0.4 + position / 32.0; + data_out.tex_coord = tex_coord; #include standard_vtxos.glsl gl_Position = mvp * v4_pos; } diff --git a/etc/shaders/rogue/main.f.glsl b/etc/shaders/rogue/main.f.glsl @@ -5,13 +5,13 @@ out vec4 frag_color; #include rogue/uniforms.glsl in shader_data { -#include shadervars.glsl +#include rogue/vars.glsl } vertex; void main() { //vec4 v4_pos = vec4(vertex.position, 1.0); - frag_color = gl_FragCoord / 1024.0; + frag_color = texture(tileset, vertex.tex_coord); } diff --git a/etc/shaders/rogue/uniforms.glsl b/etc/shaders/rogue/uniforms.glsl @@ -4,3 +4,5 @@ precision highp int; uniform mat4 mvp; uniform mat4 model_view; uniform mat4 depth_mvp; + +uniform sampler2D tileset; diff --git a/etc/shaders/skybox.f.glsl b/etc/shaders/skybox.f.glsl @@ -1,7 +1,6 @@ #include profile out vec4 frag_color; - in vec3 tex_coords; uniform samplerCube skybox; diff --git a/main.c b/main.c @@ -66,8 +66,10 @@ int main(void) engine.frame++; process_events(&engine.input, engine.frame); if (engine.input.resized_height) { + /* printf("handling resize %d %d\n", engine.input.resized_width, engine.input.resized_height); + */ handle_resize(&engine, engine.input.resized_width, engine.input.resized_height); } diff --git a/rogue/rogue.c b/rogue/rogue.c @@ -5,6 +5,8 @@ #include "grid.h" #include "mat4.h" #include "lens.h" +#include "stb_image.h" +#include "file.h" enum shader_structs { COMMON_VARS @@ -73,15 +75,46 @@ static void render_grid(struct grid *grid, struct gpu *gpu, glUseProgram(program->handle); check_gl(); bind_uniforms(program, structures, 1); check_gl(); bind_geometry(&grid->geom, program); check_gl(); + + glActiveTexture(GL_TEXTURE0); render_geometry(&grid->geom, program); check_gl(); } +u32 make_items_texture() +{ + unsigned char *img_data, *data; + size_t data_len; + u32 tid; + int width, height, n_channels; + + glGenTextures(1, &tid); + glBindTexture(GL_TEXTURE_2D, tid); + check_gl(); + + data = file_contents(TEXTURE("roguelikeitems.png"), &data_len); + assert(data); + img_data = stbi_load_from_memory(data, data_len, &width, &height, + &n_channels, 0); + + if (img_data) { + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, + height, 0, GL_RGB, GL_UNSIGNED_BYTE, img_data); + check_gl(); + stbi_image_free(img_data); + free(data); + } + + return tid; +} + int init_rogue_game(struct engine *engine, struct rogue_game *game) { int cell_size = 1.0; init_ortho(game); + game->grid.items_tilemap = make_items_texture(); + engine->gpu.num_programs = NUM_ROGUE_PROGRAMS; if (!compile_shaders(&engine->gpu)) return 0; @@ -133,8 +166,7 @@ static void render(struct engine *engine, struct rogue_game *game, struct render mat4_translate(cvars->mvp, V3(-0.5, -0.5, 0.0), cvars->mvp); mat4_scale(cvars->mvp, V3(w, h, 1.0), cvars->mvp); - glLineWidth(1.0); - glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); + //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE ); render_grid(&game->grid, &engine->gpu, cvars); diff --git a/rogue/rogue.h b/rogue/rogue.h @@ -26,6 +26,7 @@ enum shader_programs { struct grid { struct geometry geom; + int items_tilemap; }; struct rogue_game diff --git a/src/grid.c b/src/grid.c @@ -32,6 +32,7 @@ static void make_grid(float *verts, float vert_capacity, u32 *indices, float index_capacity, float *normals, float normal_capacity, float *colors, float color_capacity, + float *coords, float coords_capacity, int width, int height, float cell_size) { int c = 0; @@ -94,15 +95,18 @@ void make_grid_geom(struct geometry *geom, int width, int height, float cell_siz indices, ind_size, normals, size, colors, size, + tex_coords, size, width, height, cell_size); mkgeom.indices = indices; mkgeom.vertices = verts; mkgeom.normals = normals; mkgeom.colors = colors; + mkgeom.tex_coords = quad_uvs; + mkgeom.num_indices = ARRAY_SIZE(indices); mkgeom.num_verts = ARRAY_SIZE(verts)/3; - mkgeom.num_uv_components = 0; + mkgeom.num_uv_components = 2; make_buffer_geometry(&mkgeom, geom); check_gl();