summaryrefslogtreecommitdiff
path: root/core.c
diff options
context:
space:
mode:
Diffstat (limited to 'core.c')
-rw-r--r--core.c51
1 files changed, 42 insertions, 9 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))
}