From 5e9d27a6fedfe964ea68fa5bd6cc84ba244a01fb Mon Sep 17 00:00:00 2001 From: kitty piapiac Date: Sat, 22 Apr 2023 22:54:17 -0700 Subject: add grid + baking held piece into grid --- core.c | 51 +++++++++++++++++++++++++++++++++++++++++--------- graph.c | 5 ++++- include/graph.h | 1 + res/tex/cell_grid.png | Bin 0 -> 111 bytes res/tex/cell_mino.png | Bin 0 -> 124 bytes res/tex/cell_soon.png | Bin 0 -> 119 bytes 6 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 res/tex/cell_grid.png create mode 100644 res/tex/cell_mino.png create mode 100644 res/tex/cell_soon.png diff --git a/core.c b/core.c index 2a90b16..9ddd354 100644 --- a/core.c +++ b/core.c @@ -6,7 +6,11 @@ #include"core.h" #define GRID_W 10 -#define GRID_H 16 +#define GRID_H 20 +#define GRID_X 32 +#define GRID_Y 32 +#define CELL_W 32 +#define CELL_H 32 _ mat4 proj; _ S8 grid[GRID_H][GRID_W]={0};/*indexes into tetros,-1 for nothing*/ _ Tetro tetros[]={ @@ -18,25 +22,30 @@ _ Tetro tetros[]={ {5,{0,0},{1,1}, {{0,0},{1,0},{1,1},{2,1}}}, {6,{0,0},{0.5,0.5}, {{0,0},{1,0},{0,1},{1,1}}}}; _ C tetros_c[]="IJLTSZO"; -_ vec3 tetros_col[]={{0,1,1},{0,0,1},{1,0.5,0},{1,0,1},{1,0,0},{0,1,0},{1,1,0}}; +_ vec4 tetros_col[]={{0,1,1,1},{0,0,1,1},{1,0.5,0,1},{1,0,1,1},{1,0,0,1},{0,1,0,1},{1,1,0,1}}; +_ U32 tex_cell_grid,tex_cell_mino,tex_cell_soon; _ U32 shader_tetro; _ IM C shader_tetro_vert[]=GRAPH_GLSL( layout (location=0) in vec3 pos; + layout (location=1) in vec2 tex; + out vec2 Tex; uniform mat4 proj; uniform mat4 model; - void main(){gl_Position=proj*model*vec4(pos,1);}); + void main(){gl_Position=proj*model*vec4(pos,1);Tex=tex;}); _ IM C shader_tetro_frag[]=GRAPH_GLSL( out vec4 Frag; - uniform vec3 col; - void main(){Frag=vec4(col,1);}); + in vec2 Tex; + uniform vec4 col; + uniform sampler2D buf; + void main(){Frag=texture(buf,Tex)*col;}); _ V shader_tetro_init(V){ shader_tetro=graph_shader_create(shader_tetro_vert,shader_tetro_frag), graph_shader_use(shader_tetro), graph_shader_setM4("proj",proj), - graph_shader_setV3("col",(vec3){1,1,1});} + graph_shader_setV4("col",(vec4){1,1,1,1}), + graph_shader_setI("buf",0);} -_ V tetro_copy(Tetro*d,Tetro*s){*d=*s;} _ V tetro_trans(Tetro*t,vec2 m){ glm_vec2_add(t->pos,m,t->pos);} _ V tetro_rot(Tetro*t,S8 c){U8 i;vec2 a; @@ -46,24 +55,47 @@ _ V tetro_rot(Tetro*t,S8 c){U8 i;vec2 a; glm_vec2_add(a,t->org,a), glm_vec2_copy(a,t->v[i]);}} -_ V tetro_draw(Tetro t){U8 i;vec2 a;vec2 z={32,32}; +_ V tetro_bake(Tetro t){U8 i;vec2 a; + glm_vec2_sub(t.pos,(vec2){GRID_X,GRID_Y},a), + glm_vec2_div(a,(vec2){CELL_W,CELL_H},a); + for(i=0;i<4;++i)grid[(U32)(a[1]+t.v[i][1])][(U32)(a[0]+t.v[i][0])]=t.i;} + +_ V tetro_draw_col(Tetro t,vec4 c){U8 i;vec2 a;vec2 z={CELL_W,CELL_H}; graph_shader_use(shader_tetro), + graph_tex_use(tex_cell_mino,0), graph_shader_setM4("proj",proj), - graph_shader_setV3("col",tetros_col[t.i]); + graph_shader_setV4("col",c); for(i=0;i<4;++i){ glm_vec2_mul(t.v[i],z,a), glm_vec2_add(t.pos,a,a), graph_quad(a,z);}} +_ V tetro_draw(Tetro t){tetro_draw_col(t,tetros_col[t.i]);} +_ V grid_draw(V){U8 i,j;S8 c;vec2 z={CELL_W,CELL_H},pos={GRID_X,GRID_Y}; + graph_shader_use(shader_tetro), + graph_shader_setM4("proj",proj); + for(i=0;i