summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--core.c28
-rw-r--r--include/core.h2
-rw-r--r--scenes.c8
3 files changed, 26 insertions, 12 deletions
diff --git a/core.c b/core.c
index 0a3f73d..4e2b89e 100644
--- a/core.c
+++ b/core.c
@@ -7,6 +7,8 @@
#include"input.h"
#include"core.h"
+#define DELAY_FALL 0.2
+#define DELAY_BAKE 1.0
#define GRID_W 10
#define GRID_H 20
#define GRID_X 32
@@ -56,12 +58,10 @@ _ V tetro_rot(Tetro*t,S8 c){U8 i;vec2 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_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),
@@ -72,10 +72,22 @@ _ V tetro_draw_col(Tetro t,vec4 c){U8 i;vec2 a;vec2 z={CELL_W,CELL_H};
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 tetro_next(Tetro*t){_ Tetro bag[14];_ U8 i=14;U8 j,k;Tetro u;
- Q(i>13,i=0,memcpy(bag,tetros,SZ tetros),memcpy(bag+7,tetros,SZ tetros);
- for(j=0;j<13;++j)k=j+rand()/(RAND_MAX/(14-j)+1),u=bag[k],bag[k]=bag[j],bag[j]=u;)
- OR ++i;t->i=bag[i].i,memcpy(t->org,bag[i].org,SZ(vec2)),memcpy(t->v,bag[i].v,4*SZ(vec2));}
+
+_ Tetro bag[14];
+_ U8 bag_i=14;
+_ V tetro_next(Tetro*t){U8 i,j;Tetro u;
+ Q(bag_i>=13,bag_i=0,memcpy(bag,tetros,SZ tetros),memcpy(bag+7,tetros,SZ tetros);
+ for(i=0;i<13;++i)j=i+rand()/(RAND_MAX/(14-i)+1),u=bag[j],bag[j]=bag[i],bag[i]=u;)
+ OR ++bag_i;*t=bag[bag_i],t->pos[0]=GRID_X+CELL_W*4,t->pos[1]=GRID_Y+CELL_H*(GRID_H-1);}
+_ U8 tetro_in_floor(Tetro t){U8 i;U32 w,x;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){w=a[1]+t.v[i][1],x=a[0]+t.v[i][0];Q(w>=GRID_H||w<0,R 1)Q(-1!=grid[w][x],R 1)}
+ R 0;}
+_ V tetro_fall(Tetro*t,F64 dt){_ F64 et=0;U8 q;
+ t->pos[1]-=CELL_H,q=tetro_in_floor(*t),t->pos[1]+=CELL_H;
+ Q(q,Q(et>=DELAY_BAKE,tetro_bake(*t),tetro_next(t),et=0)OR et+=dt)
+ OR tetro_trans(t,(vec2){0,-32});}
_ 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),
@@ -100,11 +112,11 @@ V core_init(V){
tex_cell_soon=graph_tex_create("res/tex/cell_soon.png"),
memset(grid,-1,GRID_W*GRID_H);
tetro_next(&held);}
-
-V core_tick(InputPoint p,InputButtons b){
+V core_tick(InputPoint p,InputButtons b,F64 dt){_ F64 et=0;
glm_ortho(0,graph_win_w,0,graph_win_h,0,4,proj);
grid_draw();
tetro_draw(held);
+ Q((et+=dt)>=DELAY_FALL,tetro_fall(&held,et),et=0)
Q(IB_Z&b.p,tetro_rot(&held, 1))
Q(IB_X&b.p,tetro_rot(&held,-1))
Q(IB_C&b.p,tetro_next(&held))
diff --git a/include/core.h b/include/core.h
index cc9ef1c..5884f25 100644
--- a/include/core.h
+++ b/include/core.h
@@ -2,5 +2,5 @@
#define MINO_CORE_H
typedef struct{U8 i;vec2 pos,org,v[4];}Tetro;
X V core_init(V);
-X V core_tick(InputPoint,InputButtons);
+X V core_tick(InputPoint,InputButtons,F64);
#endif
diff --git a/scenes.c b/scenes.c
index bb4202e..418d777 100644
--- a/scenes.c
+++ b/scenes.c
@@ -1,3 +1,4 @@
+#include<SDL.h>
#include<cglm/affine.h>
#include<cglm/cglm.h>
#include"mino.h"
@@ -6,14 +7,15 @@
#include"input.h"
#include"core.h"
-_ V game_loop(V){/*prelude*/graph_before(),graph_events();InputPoint p;InputButtons b;
+_ V game_loop(F64 dt){/*prelude*/graph_before(),graph_events();InputPoint p;InputButtons b;
/*interlude*/
input_update(),p=input_point,b=input_buttons;
- core_tick(p,b);
+ core_tick(p,b,dt);
/*postlude*/
graph_after();}
-_ Scene*game_run(V){core_init();WH(1,game_loop());R 0;}
+_ Scene*game_run(V){U64 prev,now;core_init();prev=SDL_GetPerformanceCounter();
+ WH(1,now=SDL_GetPerformanceCounter(),game_loop((now-prev)/(F64)SDL_GetPerformanceFrequency()),prev=now);R 0;}
Scene scene_game={"Game",game_run};
_ Scene*title_run(V){R&scene_game;}