diff options
Diffstat (limited to 'core.c')
-rw-r--r-- | core.c | 75 |
1 files changed, 75 insertions, 0 deletions
@@ -0,0 +1,75 @@ +#include<string.h> +#include<cglm/cglm.h> +#include"mino.h" +#include"graph.h" +#include"input.h" +#include"core.h" + +#define GRID_W 10 +#define GRID_H 16 +_ mat4 proj; +_ S8 grid[GRID_H][GRID_W]={0};/*indexes into tetros,-1 for nothing*/ +_ Tetro tetros[]={ + {0,{0,0},{1.5,1.5}, {{0,1},{1,1},{2,1},{3,1}}}, + {1,{0,0},{1,1}, {{0,0},{0,1},{1,1},{2,1}}}, + {2,{0,0},{1,1}, {{2,0},{0,1},{1,1},{2,1}}}, + {3,{0,0},{1,1}, {{1,0},{0,1},{1,1},{2,1}}}, + {4,{0,0},{1,1}, {{1,0},{2,0},{0,1},{1,1}}}, + {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}}; + +_ U32 shader_tetro; +_ IM C shader_tetro_vert[]=GRAPH_GLSL( + layout (location=0) in vec3 pos; + uniform mat4 proj; + uniform mat4 model; + void main(){gl_Position=proj*model*vec4(pos,1);}); +_ IM C shader_tetro_frag[]=GRAPH_GLSL( + out vec4 Frag; + uniform vec3 col; + void main(){Frag=vec4(col,1);}); +_ 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});} + +_ 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; + for(i=0;i<4;++i){ + glm_vec2_sub(t->org,t->v[i],a), + glm_vec2_rotate(a,GLM_PI_2*(2-c),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}; + graph_shader_use(shader_tetro), + graph_shader_setM4("proj",proj), + graph_shader_setV3("col",tetros_col[t.i]); + 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);}} + +_ Tetro held; +V core_init(V){ + glm_ortho(0,graph_win_w,0,graph_win_h,0,4,proj), + shader_tetro_init(), + 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); + tetro_draw(held); + Q(IB_Z&b.p,tetro_rot(&held, 1)) + Q(IB_X&b.p,tetro_rot(&held,-1)) + Q(IB_C&b.p,held=tetros[(held.i+1)%7]) + Q(IB_L&b.p,tetro_trans(&held,(vec2){-32, 0})) + 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})) +} |