diff options
-rw-r--r-- | .gitmodules | 3 | ||||
-rw-r--r-- | Makefile | 11 | ||||
-rw-r--r-- | core.c | 75 | ||||
m--------- | dep/cglm | 0 | ||||
-rw-r--r-- | graph.c | 22 | ||||
-rw-r--r-- | include/core.h | 6 | ||||
-rw-r--r-- | include/graph.h | 1 | ||||
-rw-r--r-- | include/input.h | 11 | ||||
-rw-r--r-- | input.c | 42 | ||||
-rw-r--r-- | mino.c | 10 | ||||
-rw-r--r-- | scene-title.c | 51 | ||||
-rw-r--r-- | scenes.c | 25 |
12 files changed, 168 insertions, 89 deletions
diff --git a/.gitmodules b/.gitmodules index 1bbb38a..8ac074e 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,6 @@ [submodule "include/stb"] path = include/stb url = https://github.com/nothings/stb +[submodule "dep/cglm"] + path = dep/cglm + url = https://github.com/recp/cglm @@ -1,16 +1,17 @@ .POSIX: CC := clang -CFLAGS := -std=c11 -g -O0 -Wall -Wextra -Wshadow -Iinclude $(shell sdl2-config --cflags) $(shell pkg-config --cflags cglm) +CFLAGS := -std=c11 -g -O0 -Wall -Wextra -Wshadow -Iinclude -Idep/cglm/include $(shell sdl2-config --cflags) LDFLAGS := -LDLIBS := -ldl -lm $(shell sdl2-config --libs) $(shell pkg-config --libs cglm) +LDLIBS := -ldl -lm $(shell sdl2-config --libs) all: mino -mino: glad.o tex.o graph.o input.o scene.o scene-title.o mino.o +mino: glad.o tex.o graph.o input.o scene.o scenes.o core.o mino.o mino.o: mino.c include/mino.h +core.o: core.c include/core.h scene.o: scene.c include/scene.h -scene-title.o: scene-title.c +scenes.o: scenes.c input.o: input.c include/input.h graph.o: graph.c include/graph.h tex.o: tex.c include/tex.h glad.o: glad.c include/glad/glad.h clean: - rm -rf mino mino.o scene-title.o scene.o input.o graph.o tex.o glad.o + rm -rf mino mino.o core.o scenes.o scene.o input.o graph.o tex.o glad.o @@ -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})) +} diff --git a/dep/cglm b/dep/cglm new file mode 160000 +Subproject 13036036c4e5caa5c2236dee2fb1f518d9a534b @@ -15,7 +15,7 @@ _ SDL_Window*win; _ SDL_GLContext ctx; _ V on_err(IM C*d){die("Could not %s: %s",d,SDL_GetError());} -_ V q_init(V); +_ V q_init(V);_ V q_deinit(V); V graph_init(V){SDL_version sdl_vh,sdl_vl; Q(SDL_Init(SDL_INIT_VIDEO),on_err("init sdl2")) SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION,3), @@ -46,7 +46,7 @@ V graph_events(V){SDL_Event ev;U32 t;WH(SDL_PollEvent(&ev),t=ev.type; ||SDL_WINDOWEVENT_SIZE_CHANGED==ew.event,graph_win_w=ew.data1,graph_win_h=ew.data2, glViewport(0,0,ew.data1,ew.data2))))} -V graph_deinit(V){SDL_GL_DeleteContext(ctx),SDL_DestroyWindow(win),SDL_Quit();} +V graph_deinit(V){q_deinit(),SDL_GL_DeleteContext(ctx),SDL_DestroyWindow(win),SDL_Quit();} V graph_before(V){glClearColor(1.0,0.75,0.8,1.0),glClear(GL_COLOR_BUFFER_BIT);} V graph_after(V){SDL_GL_SwapWindow(win);} @@ -54,12 +54,10 @@ U32 graph_ticks(V){R SDL_GetTicks();} _ U32 q_vao,q_vbo,q_ebo; _ V q_init(V){ - _ IM F32 verts[]={ - /*positions*/ /*colours*/ /*texture*/ - 0.5, 0.5,0.0, 1.0,0.0,0.0, 1.0,1.0, - 0.5,-0.5,0.0, 0.0,1.0,0.0, 1.0,0.0, - -0.5,-0.5,0.0, 1.0,0.0,1.0, 0.0,0.0, - -0.5, 0.5,0.0, 0.0,0.0,0.0, 0.0,1.0}; + _ IM F32 verts[]={1,1,0, 1,1, + 1,0,0, 1,0, + 0,0,0, 0,0, + 0,1,0, 0,1}; _ IM U32 indxs[]={0,1,3,1,2,3}; glGenVertexArrays(1,&q_vao),glGenBuffers(1,&q_vbo),glGenBuffers(1,&q_ebo); glBindVertexArray(q_vao), @@ -67,13 +65,12 @@ _ V q_init(V){ glBufferData(GL_ARRAY_BUFFER,SZ verts,verts,GL_STATIC_DRAW), glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,q_ebo), glBufferData(GL_ELEMENT_ARRAY_BUFFER,SZ indxs,indxs,GL_STATIC_DRAW); - glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,8*SZ(F32),(V*)(0*SZ(F32))), + glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,5*SZ(F32),(V*)(0*SZ(F32))), glEnableVertexAttribArray(0), - glVertexAttribPointer(1,3,GL_FLOAT,GL_FALSE,8*SZ(F32),(V*)(3*SZ(F32))), + glVertexAttribPointer(1,2,GL_FLOAT,GL_FALSE,5*SZ(F32),(V*)(3*SZ(F32))), glEnableVertexAttribArray(1), - glVertexAttribPointer(2,2,GL_FLOAT,GL_FALSE,8*SZ(F32),(V*)(6*SZ(F32))), - glEnableVertexAttribArray(2), glBindVertexArray(0);} +_ V q_deinit(V){glDeleteBuffers(1,&q_vao),glDeleteBuffers(1,&q_vbo),glDeleteBuffers(1,&q_ebo);} V graph_quad_xywh(F32 x,F32 y,F32 w,F32 h){graph_quad((vec2){x,y},(vec2){w,h});} V graph_quad(vec2 p,vec2 z){mat4 trans; glm_mat4_identity(trans), @@ -96,6 +93,7 @@ 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_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/core.h b/include/core.h new file mode 100644 index 0000000..cc9ef1c --- /dev/null +++ b/include/core.h @@ -0,0 +1,6 @@ +#ifndef MINO_CORE_H +#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); +#endif diff --git a/include/graph.h b/include/graph.h index f04bfb5..ff2ab8a 100644 --- a/include/graph.h +++ b/include/graph.h @@ -17,6 +17,7 @@ X U32 graph_shader_create(IM C*,IM C*); 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_setM4(IM C*,mat4); X U32 graph_tex_create(IM C*); diff --git a/include/input.h b/include/input.h index 0a54d19..b2a0ea1 100644 --- a/include/input.h +++ b/include/input.h @@ -2,8 +2,11 @@ #define MINO_INPUT_H #include"mino.h" typedef struct{U32 x,y;}InputPoint; -typedef struct{U32 f;}InputButtons; -enum{IB_1=1<<0,IB_2=1<<1,IB_3=1<<2,IB_4=1<<3}; -X InputPoint input_point(V); -X InputButtons input_buttons(V); +enum{IB_1=1<<0, IB_2=1<<1, IB_3=1<<2, IB_4=1<<3, + IB_L=1<<4, IB_R=1<<5, IB_U=1<<6, IB_D=1<<7, + IB_S=1<<8, IB_Z=1<<9, IB_X=1<<10,IB_C=1<<11}; +typedef struct{U32 p,h;}InputButtons; +X InputPoint input_point; +X InputButtons input_buttons; +X V input_update(V); #endif @@ -4,10 +4,38 @@ #include"graph.h" #include"input.h" #define SDLBUT(x)SDL_BUTTON(SDL_BUTTON_##x) -InputPoint input_point(V){InputPoint ip;SDL_GetMouseState((S32*)&ip.x,(S32*)&ip.y);ip.y=graph_win_h-ip.y;R ip;} -InputButtons input_button(V){InputButtons ib={0};U32 mb;mb=SDL_GetMouseState(0,0); - Q(SDLBUT(LEFT)&mb, ib.f|=IB_1) - Q(SDLBUT(MIDDLE)&mb, ib.f|=IB_2) - Q(SDLBUT(RIGHT)&mb, ib.f|=IB_3) - Q(SDLBUT(X1)&mb, ib.f|=IB_4) - R ib;} +InputPoint input_point; +InputButtons input_buttons; + +_ U32 map_mouse[][2]={ + {SDLBUT(LEFT), IB_1}, + {SDLBUT(RIGHT), IB_2}, + {SDLBUT(MIDDLE),IB_3}, + {SDLBUT(X1), IB_4}}; + +_ U32 map_keeb[][2]={ + {SDL_SCANCODE_LEFT, IB_L}, + {SDL_SCANCODE_RIGHT, IB_R}, + {SDL_SCANCODE_UP, IB_U}, + {SDL_SCANCODE_DOWN, IB_D}, + {SDL_SCANCODE_SPACE, IB_S}, + {SDL_SCANCODE_Z, IB_Z}, + {SDL_SCANCODE_X, IB_X}, + {SDL_SCANCODE_C, IB_C}}; + +#define ip input_point +#define ib input_buttons +#define mm map_mouse +#define mk map_keeb +V input_update(V){U32 mb,mm_len,mk_len,i,kb_len;IM U8*kb;InputButtons nb={0,0}; + mb=SDL_GetMouseState((S32*)&ip.x,(S32*)&ip.y),ip.y=graph_win_h-ip.y, + kb=SDL_GetKeyboardState((S32*)&kb_len), + mm_len=(SZ mm)/(SZ mm[0]), + mk_len=(SZ mk)/(SZ mk[0]), + i=0;WH(i<mm_len,Q(mm[i][0]&mb, Q(mm[i][1]&(ib.p|ib.h),nb.h|=mm[i][1])OR nb.p|=mm[i][1]);++i) + i=0;WH(i<mk_len,Q(kb[mk[i][0]],Q(mk[i][1]&(ib.p|ib.h),nb.h|=mk[i][1])OR nb.p|=mk[i][1]);++i) + input_buttons=nb;} +#undef mk +#undef mm +#undef ib +#undef ip @@ -1,18 +1,8 @@ #include<stdio.h> #include<stdlib.h> -#include<cglm/affine.h> -#include<cglm/cglm.h> #include"mino.h" -#include"graph.h" -#include"scene.h" NR V die(IM C*f,...)VA(f,vfprintf(stderr,f,ap),fputc('\n',stderr),exit(1)) V*make(Uz z){V*p;N(p=malloc(z), die("oom"))R p;} V*remk(V*p,Uz z){N(p=realloc(p,z),die("oom"))R p;} V del(V*p){free(p);} - -X Scene scene_title; -I main(I ac,C**av){(V)ac,(V)av; - graph_init(),atexit(graph_deinit); - scene_run(&scene_title); - R 0;} diff --git a/scene-title.c b/scene-title.c deleted file mode 100644 index 527c686..0000000 --- a/scene-title.c +++ /dev/null @@ -1,51 +0,0 @@ -/*2d title screen*/ - -#include<cglm/affine.h> -#include<cglm/cglm.h> -#include"mino.h" -#include"graph.h" -#include"scene.h" -#include"input.h" - -_ mat4 win_proj; - -_ U32 shader_tex; -_ IM C shader_tex_vert[]=GRAPH_GLSL( - layout (location=0) in vec3 pos; - layout (location=1) in vec3 col; - layout (location=2) in vec2 tex; - out vec3 Col; - out vec2 Tex; - uniform mat4 proj; - uniform mat4 model; - void main(){gl_Position=proj*model*vec4(pos,1.0);Col=col;Tex=tex;}); -_ IM C shader_tex_frag[]=GRAPH_GLSL( - out vec4 Frag; - in vec3 Col; - in vec2 Tex; - uniform sampler2D buf; - void main(){Frag=texture(buf,Tex);}); -_ V shader_tex_init(V){ - shader_tex=graph_shader_create(shader_tex_vert,shader_tex_frag), - graph_shader_use(shader_tex), - graph_shader_setM4("proj",win_proj), - graph_shader_setI("buf",0);} - -_ U32 tex_cat; -_ V title_init(V){ - glm_ortho(0,graph_win_w,0,graph_win_h,0,4,win_proj), - shader_tex_init(); - tex_cat=graph_tex_create("res/tex/cat.jpg");} - -_ V title_loop(V){/*prelude*/graph_before(),graph_events();InputPoint p; - glm_ortho(0,graph_win_w,0,graph_win_h,0,4,win_proj), - /*interlude*/ - p=input_point(), - graph_shader_use(shader_tex),graph_shader_setM4("proj",win_proj), - graph_tex_use(tex_cat,0), - graph_quad_xywh(p.x,p.y,256,256), - /*postlude*/ - graph_after();} - -_ Scene*title_run(V){title_init();WH(1,title_loop());R 0;} -Scene scene_title={"title",title_run}; diff --git a/scenes.c b/scenes.c new file mode 100644 index 0000000..bb4202e --- /dev/null +++ b/scenes.c @@ -0,0 +1,25 @@ +#include<cglm/affine.h> +#include<cglm/cglm.h> +#include"mino.h" +#include"graph.h" +#include"scene.h" +#include"input.h" +#include"core.h" + +_ V game_loop(V){/*prelude*/graph_before(),graph_events();InputPoint p;InputButtons b; + /*interlude*/ + input_update(),p=input_point,b=input_buttons; + core_tick(p,b); + /*postlude*/ + graph_after();} + +_ Scene*game_run(V){core_init();WH(1,game_loop());R 0;} +Scene scene_game={"Game",game_run}; + +_ Scene*title_run(V){R&scene_game;} +Scene scene_title={"Title",title_run}; + +I main(I ac,C**av){(V)ac,(V)av; + graph_init(),atexit(graph_deinit); + scene_run(&scene_title); + R 0;} |