summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkitty piapiac <kcp@bsd.computer>2023-04-22 22:54:17 -0700
committerkitty piapiac <kcp@bsd.computer>2023-04-22 22:54:17 -0700
commit5e9d27a6fedfe964ea68fa5bd6cc84ba244a01fb (patch)
tree29073bfc65b711aeb4a9ae27677d4fbcfadfa232
parent8f38659932677a2b0e00b7f4abda54ac76a8ecd9 (diff)
add grid + baking held piece into grid
-rw-r--r--core.c51
-rw-r--r--graph.c5
-rw-r--r--include/graph.h1
-rw-r--r--res/tex/cell_grid.pngbin0 -> 111 bytes
-rw-r--r--res/tex/cell_mino.pngbin0 -> 124 bytes
-rw-r--r--res/tex/cell_soon.pngbin0 -> 119 bytes
6 files changed, 47 insertions, 10 deletions
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<GRID_H;++i,pos[1]+=CELL_H,pos[0]=CELL_W)
+ for(j=0;j<GRID_W;++j,pos[0]+=CELL_W){
+ if(-1==(c=grid[i][j]))
+ graph_tex_use(tex_cell_soon,0),
+ graph_shader_setV4("col",(vec4){0.5,0.5,0.5,1});
+ OR
+ graph_tex_use(tex_cell_mino,0),
+ graph_shader_setV4("col",tetros_col[c]);
+ graph_quad(pos,z);}}
_ Tetro held;
V core_init(V){
glm_ortho(0,graph_win_w,0,graph_win_h,0,4,proj),
shader_tetro_init(),
+ tex_cell_grid=graph_tex_create("res/tex/cell_grid.png"),
+ tex_cell_mino=graph_tex_create("res/tex/cell_mino.png"),
+ tex_cell_soon=graph_tex_create("res/tex/cell_soon.png"),
memset(grid,-1,GRID_W*GRID_H);
held=tetros[3];}
V core_tick(InputPoint p,InputButtons b){
glm_ortho(0,graph_win_w,0,graph_win_h,0,4,proj);
+ grid_draw();
tetro_draw(held);
Q(IB_Z&b.p,tetro_rot(&held, 1))
Q(IB_X&b.p,tetro_rot(&held,-1))
@@ -72,4 +104,5 @@ V core_tick(InputPoint p,InputButtons b){
Q(IB_R&b.p,tetro_trans(&held,(vec2){ 32, 0}))
Q(IB_U&b.p,tetro_trans(&held,(vec2){ 0, 32}))
Q(IB_D&b.p,tetro_trans(&held,(vec2){ 0,-32}))
+ Q(IB_1&b.p,tetro_bake(held))
}
diff --git a/graph.c b/graph.c
index 05c22bf..52898cb 100644
--- a/graph.c
+++ b/graph.c
@@ -26,6 +26,8 @@ V graph_init(V){SDL_version sdl_vh,sdl_vl;
N(ctx=SDL_GL_CreateContext(win),on_err("create OpenGL context"))
N(gladLoadGLLoader(SDL_GL_GetProcAddress),on_err("load GL"))
SDL_GL_SetSwapInterval(1),
+ glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA),
+ glEnable(GL_BLEND),
q_init();
SDL_VERSION(&sdl_vh)SDL_GetVersion(&sdl_vl);
fprintf(stderr,
@@ -93,7 +95,8 @@ U32 graph_shader_create(IM C*vert,IM C*frag){I ok;C info[512];U32 v,f,p;
V graph_shader_use(U32 s){glUseProgram(s);}
V graph_shader_setI(IM C*s,I i) {I p;glGetIntegerv(GL_CURRENT_PROGRAM,&p),glUniform1i(glGetUniformLocation(p,s),i);}
-V graph_shader_setV3(IM C*s,vec3 v3){I p;glGetIntegerv(GL_CURRENT_PROGRAM,&p),glUniform3f(glGetUniformLocation(p,s),v3[0],v3[1],v3[2]);}
+V graph_shader_setV3(IM C*s,vec3 v3){I p;glGetIntegerv(GL_CURRENT_PROGRAM,&p),glUniform3fv(glGetUniformLocation(p,s),1,v3);}
+V graph_shader_setV4(IM C*s,vec4 v4){I p;glGetIntegerv(GL_CURRENT_PROGRAM,&p),glUniform4fv(glGetUniformLocation(p,s),1,v4);}
V graph_shader_setM4(IM C*s,mat4 m4){I p;glGetIntegerv(GL_CURRENT_PROGRAM,&p),glUniformMatrix4fv(glGetUniformLocation(p,s),1,GL_FALSE,&m4[0][0]);}
U32 graph_tex_create(IM C*f){Tex tex;U32 t;
diff --git a/include/graph.h b/include/graph.h
index ff2ab8a..7ac9f1e 100644
--- a/include/graph.h
+++ b/include/graph.h
@@ -18,6 +18,7 @@ X U32 graph_shader_tex;
X V graph_shader_use(U32);
X V graph_shader_setI(IM C*,I);
X V graph_shader_setV3(IM C*,vec3);
+X V graph_shader_setV4(IM C*,vec4);
X V graph_shader_setM4(IM C*,mat4);
X U32 graph_tex_create(IM C*);
diff --git a/res/tex/cell_grid.png b/res/tex/cell_grid.png
new file mode 100644
index 0000000..1aaef39
--- /dev/null
+++ b/res/tex/cell_grid.png
Binary files differ
diff --git a/res/tex/cell_mino.png b/res/tex/cell_mino.png
new file mode 100644
index 0000000..2fdbde6
--- /dev/null
+++ b/res/tex/cell_mino.png
Binary files differ
diff --git a/res/tex/cell_soon.png b/res/tex/cell_soon.png
new file mode 100644
index 0000000..0aff94c
--- /dev/null
+++ b/res/tex/cell_soon.png
Binary files differ