playable with a gamepad
[vrtris] / src / gamescr.c
1 #include <stdlib.h>
2 #include <time.h>
3 #include <assert.h>
4 #include <imago2.h>
5 #include <goatvr.h>
6 #include "opengl.h"
7 #include "game.h"
8 #include "screen.h"
9 #include "cmesh.h"
10 #include "blocks.h"
11 #include "logger.h"
12 #include "gameinp.h"
13
14 int init_starfield(void);
15 void draw_starfield(void);
16
17 static int init(void);
18 static void cleanup(void);
19 static void start(void);
20 static void stop(void);
21 static void update(float dt);
22 static void draw(void);
23 static void draw_block(int block, const int *pos, int rot);
24 static void drawpf(void);
25 static void reshape(int x, int y);
26 static void keyboard(int key, int pressed);
27 static void mouse(int bn, int pressed, int x, int y);
28 static void motion(int x, int y);
29 static void wheel(int dir);
30
31 static void update_cur_block(void);
32 static void addscore(int nlines);
33 static int spawn(void);
34 static int collision(int block, const int *pos);
35 static void stick(int block, const int *pos);
36 static void erase_completed(void);
37
38 struct game_screen game_screen = {
39         "game",
40         1,      /* opaque */
41         0,      /* next */
42         init,
43         cleanup,
44         start,
45         stop,
46         update,
47         draw,
48         reshape,
49         keyboard,
50         mouse,
51         motion,
52         wheel
53 };
54
55 static struct cmesh *blkmesh, *wellmesh;
56 static unsigned int tex_well;
57
58 static float cam_theta, cam_phi, cam_dist = 30;
59 static int bnstate[16];
60 static int prev_mx, prev_my;
61
62 static long tick_interval;
63
64 /* dimensions of the playfield */
65 #define PF_ROWS         18
66 #define PF_COLS         10
67
68 #define PF_FULL         0x100
69 #define PF_VIS          0x200
70 #define PF_VIS_SHIFT    9
71
72 static unsigned int pfield[PF_ROWS * PF_COLS];
73
74 static int pos[2], next_pos[2];
75 static int cur_block, next_block, prev_block;
76 static int cur_rot, prev_rot;
77 static int complines[4];
78 static int num_complines;
79 static int gameover;
80 static int pause;
81 static int score, level, lines;
82 static int just_spawned;
83
84 #define NUM_LEVELS      21
85 static const long level_speed[NUM_LEVELS] = {
86         887, 820, 753, 686, 619, 552, 469, 368, 285, 184,
87         167, 151, 134, 117, 107, 98, 88, 79, 69, 60, 50
88 };
89
90 static const float blkcolor[][4] = {
91         {1.0, 0.65, 0.0, 1},
92         {0.16, 1.0, 0.4, 1},
93         {0.65, 0.65, 1.0, 1},
94         {1.0, 0.9, 0.1, 1},
95         {0.0, 1.0, 1.0, 1},
96         {1.0, 0.5, 1.0, 1},
97         {1.0, 0.35, 0.2, 1},
98         {0.5, 0.5, 0.5, 1}
99 };
100
101 #define GAMEOVER_FILL_RATE      50
102
103
104 static int init(void)
105 {
106         if(init_starfield() == -1) {
107                 return -1;
108         }
109
110         if(!(blkmesh = cmesh_alloc()) || cmesh_load(blkmesh, "data/noisecube.obj") == -1) {
111                 error_log("failed to load block mesh\n");
112                 return -1;
113         }
114
115         if(!(wellmesh = cmesh_alloc()) || cmesh_load(wellmesh, "data/well.obj") == -1) {
116                 error_log("failed to load well mesh\n");
117                 return -1;
118         }
119
120         if(!(tex_well = img_gltexture_load("data/grid.png"))) {
121                 error_log("failed to load well texture\n");
122                 return -1;
123         }
124
125         return 0;
126 }
127
128 static void cleanup(void)
129 {
130         cmesh_free(blkmesh);
131         cmesh_free(wellmesh);
132         glDeleteTextures(1, &tex_well);
133 }
134
135 static void start(void)
136 {
137         srand(time(0));
138
139         pause = 0;
140         gameover = 0;
141         num_complines = 0;
142         score = level = lines = 0;
143         tick_interval = level_speed[0];
144         cur_block = -1;
145         prev_block = 0;
146         next_block = rand() % NUM_BLOCKS;
147
148         memset(pfield, 0, PF_COLS * PF_ROWS * sizeof *pfield);
149
150         ginp_repeat(500, 75, GINP_LEFT | GINP_RIGHT | GINP_DOWN);
151 }
152
153 static void stop(void)
154 {
155 }
156
157 #define JTHRES  0.6
158
159 #define CHECK_BUTTON(idx, gbn) \
160         if(joy_bnstate & (1 << idx)) { \
161                 ginp_bnstate |= gbn; \
162         }
163
164 static void update_input(float dtsec)
165 {
166         int num_vr_sticks;
167
168         if((num_vr_sticks = goatvr_num_sticks()) > 0) {
169                 float p[2];
170
171                 goatvr_stick_pos(0, p);
172
173                 if(fabs(p[0]) > fabs(joy_axis[GPAD_LSTICK_X])) {
174                         joy_axis[GPAD_LSTICK_X] = p[0];
175                 }
176                 if(fabs(p[1]) > fabs(joy_axis[GPAD_LSTICK_Y])) {
177                         joy_axis[GPAD_LSTICK_Y] = p[1];
178                 }
179         }
180
181         ginp_bnstate = 0;
182
183         /* joystick axis */
184         if(joy_axis[GPAD_LSTICK_X] >= JTHRES) {
185                 ginp_bnstate |= GINP_RIGHT;
186         } else if(joy_axis[GPAD_LSTICK_X] <= -JTHRES) {
187                 ginp_bnstate |= GINP_LEFT;
188         }
189
190         if(joy_axis[GPAD_LSTICK_Y] >= JTHRES) {
191                 ginp_bnstate |= GINP_DOWN;
192         } else if(joy_axis[GPAD_LSTICK_Y] <= -JTHRES) {
193                 ginp_bnstate |= GINP_UP;
194         }
195
196         CHECK_BUTTON(GPAD_LEFT, GINP_LEFT);
197         CHECK_BUTTON(GPAD_RIGHT, GINP_RIGHT);
198         CHECK_BUTTON(GPAD_UP, GINP_UP);
199         CHECK_BUTTON(GPAD_DOWN, GINP_DOWN);
200         CHECK_BUTTON(GPAD_A, GINP_ROTATE);
201         CHECK_BUTTON(GPAD_START, GINP_PAUSE);
202
203         update_ginp();
204
205         if(GINP_PRESS(GINP_LEFT)) {
206                 game_keyboard('a', 1);
207         }
208         if(GINP_PRESS(GINP_RIGHT)) {
209                 game_keyboard('d', 1);
210         }
211         if(GINP_PRESS(GINP_DOWN)) {
212                 game_keyboard('s', 1);
213         }
214         if(GINP_PRESS(GINP_UP)) {
215                 game_keyboard('\t', 1);
216         }
217         if(GINP_PRESS(GINP_ROTATE)) {
218                 game_keyboard('w', 1);
219         }
220         if(GINP_PRESS(GINP_PAUSE)) {
221                 game_keyboard('p', 1);
222         }
223 }
224
225 static void update(float dtsec)
226 {
227         static long prev_tick;
228         long dt;
229
230         update_input(dtsec);
231
232         if(pause) {
233                 prev_tick = time_msec;
234                 return;
235         }
236         dt = time_msec - prev_tick;
237
238         if(gameover) {
239                 int i, row = PF_ROWS - gameover;
240                 unsigned int *ptr;
241
242                 if(dt < GAMEOVER_FILL_RATE) {
243                         return;
244                 }
245
246                 if(row >= 0) {
247                         ptr = pfield + row * PF_COLS;
248                         for(i=0; i<PF_COLS; i++) {
249                                 *ptr++ = PF_VIS | PF_FULL | 7;
250                         }
251
252                         gameover++;
253                         prev_tick = time_msec;
254                 }
255                 return;
256         }
257
258         if(num_complines) {
259                 /* lines where completed, we're in blinking mode */
260                 int i, j, blink = dt >> 8;
261
262                 if(blink > 6) {
263                         erase_completed();
264                         num_complines = 0;
265                         return;
266                 }
267
268                 for(i=0; i<num_complines; i++) {
269                         unsigned int *ptr = pfield + complines[i] * PF_COLS;
270                         for(j=0; j<PF_COLS; j++) {
271                                 *ptr = (*ptr & ~PF_VIS) | ((blink & 1) << PF_VIS_SHIFT);
272                                 ptr++;
273                         }
274                 }
275                 return;
276         }
277
278         /* fall */
279         while(dt >= tick_interval) {
280                 if(cur_block >= 0) {
281                         just_spawned = 0;
282                         next_pos[0] = pos[0] + 1;
283                         if(collision(cur_block, next_pos)) {
284                                 next_pos[0] = pos[0];
285                                 stick(cur_block, next_pos);
286                                 return;
287                         }
288                 } else {
289                         /* respawn */
290                         if(spawn() == -1) {
291                                 gameover = 1;
292                                 return;
293                         }
294                 }
295
296                 dt -= tick_interval;
297                 prev_tick = time_msec;
298         }
299
300         update_cur_block();
301 }
302
303 static void draw(void)
304 {
305         const float lpos[] = {-1, 1, 6, 1};
306
307         glTranslatef(0, 0, -cam_dist);
308         glRotatef(cam_phi, 1, 0, 0);
309         glRotatef(cam_theta, 0, 1, 0);
310
311         glLightfv(GL_LIGHT0, GL_POSITION, lpos);
312
313         draw_starfield();
314
315         glPushAttrib(GL_ENABLE_BIT);
316         glBindTexture(GL_TEXTURE_2D, tex_well);
317         glEnable(GL_TEXTURE_2D);
318         glDisable(GL_LIGHTING);
319         glColor3f(1, 1, 1);
320         cmesh_draw(wellmesh);
321         glPopAttrib();
322
323         /* center playfield */
324         glPushMatrix();
325         glTranslatef(-PF_COLS / 2 + 0.5, PF_ROWS / 2 - 0.5, 0);
326
327         drawpf();
328         if(cur_block >= 0) {
329                 draw_block(cur_block, pos, cur_rot);
330         }
331
332         glPopMatrix();
333 }
334
335 static const float blkspec[] = {0.85, 0.85, 0.85, 1};
336
337 static void draw_block(int block, const int *pos, int rot)
338 {
339         int i;
340         unsigned char *p = blocks[block][rot];
341
342         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blkcolor[block]);
343         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blkspec);
344         glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f);
345
346         for(i=0; i<4; i++) {
347                 int x = pos[1] + BLKX(*p);
348                 int y = pos[0] + BLKY(*p);
349                 p++;
350
351                 if(y < 0) continue;
352
353                 glPushMatrix();
354                 glTranslatef(x, -y, 0);
355                 cmesh_draw(blkmesh);
356                 glPopMatrix();
357         }
358 }
359
360 static void drawpf(void)
361 {
362         int i, j;
363         unsigned int *sptr = pfield;
364
365         for(i=0; i<PF_ROWS; i++) {
366                 for(j=0; j<PF_COLS; j++) {
367                         unsigned int val = *sptr++;
368
369                         glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blkcolor[val & 7]);
370                         glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blkspec);
371                         glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f);
372
373                         if((val & (PF_FULL | PF_VIS)) == (PF_FULL | PF_VIS)) {
374                                 glPushMatrix();
375                                 glTranslatef(j, -i, 0);
376                                 cmesh_draw(blkmesh);
377                                 glPopMatrix();
378                         }
379                 }
380         }
381 }
382
383
384 static void reshape(int x, int y)
385 {
386 }
387
388 static void keyboard(int key, int pressed)
389 {
390         /*char *name = 0;*/
391
392         if(!pressed) return;
393
394         switch(key) {
395         case 'a':
396         case KEY_LEFT:
397                 if(!pause) {
398                         next_pos[1] = pos[1] - 1;
399                         if(collision(cur_block, next_pos)) {
400                                 next_pos[1] = pos[1];
401                         } else {
402                                 /*snd_shift();*/
403                         }
404                 }
405                 break;
406
407         case 'd':
408         case KEY_RIGHT:
409                 if(!pause) {
410                         next_pos[1] = pos[1] + 1;
411                         if(collision(cur_block, next_pos)) {
412                                 next_pos[1] = pos[1];
413                         } else {
414                                 /*snd_shift();*/
415                         }
416                 }
417                 break;
418
419         case 'w':
420         case KEY_UP:
421         case ' ':
422                 if(!pause) {
423                         prev_rot = cur_rot;
424                         cur_rot = (cur_rot + 1) & 3;
425                         if(collision(cur_block, next_pos)) {
426                                 cur_rot = prev_rot;
427                         } else {
428                                 /*snd_rot();*/
429                         }
430                 }
431                 break;
432
433         case 's':
434         case KEY_DOWN:
435                 /* ignore drops until the first update after a spawn */
436                 if(cur_block >= 0 && !just_spawned && !pause) {
437                         next_pos[0] = pos[0] + 1;
438                         if(collision(cur_block, next_pos)) {
439                                 next_pos[0] = pos[0];
440                                 update_cur_block();
441                                 stick(cur_block, next_pos);     /* stick immediately */
442                         }
443                 }
444                 break;
445
446         case '\n':
447         case '\t':
448         case '0':
449                 if(!pause && cur_block >= 0) {
450                         next_pos[0] = pos[0] + 1;
451                         while(!collision(cur_block, next_pos)) {
452                                 next_pos[0]++;
453                         }
454                         next_pos[0]--;
455                         update_cur_block();
456                         stick(cur_block, next_pos);     /* stick immediately */
457                 }
458                 break;
459
460         case 'p':
461                 if(gameover) {
462                         /*
463                         if(score && is_highscore(score)) {
464                                 name = name_screen(score);
465                         }
466                         save_score(name, score, lines, level);
467                         */
468                         /* TODO: pop screen */
469                 } else {
470                         pause ^= 1;
471                 }
472                 break;
473
474         case '\b':
475                 /*
476                 if(score && is_highscore(score)) {
477                         name = name_screen(score);
478                 }
479                 save_score(name, score, lines, level);
480                 */
481                 /* TODO: pop screen */
482                 break;
483
484         default:
485                 break;
486         }
487 }
488
489 static void mouse(int bn, int pressed, int x, int y)
490 {
491         bnstate[bn] = pressed;
492         prev_mx = x;
493         prev_my = y;
494 }
495
496 static void motion(int x, int y)
497 {
498         float dx = x - prev_mx;
499         float dy = y - prev_my;
500         prev_mx = x;
501         prev_my = y;
502
503         if(bnstate[0]) {
504                 cam_theta += dx * 0.5;
505                 cam_phi += dy * 0.5;
506
507                 if(cam_phi < -90) cam_phi = -90;
508                 if(cam_phi > 90) cam_phi = 90;
509         }
510         if(bnstate[2]) {
511                 cam_dist += dy * 0.1;
512                 if(cam_dist < 0) cam_dist = 0;
513         }
514 }
515
516 static void wheel(int dir)
517 {
518 }
519
520 static void update_cur_block(void)
521 {
522         if(cur_block < 0) return;
523
524         memcpy(pos, next_pos, sizeof pos);
525         prev_rot = cur_rot;
526 }
527
528 static void addscore(int nlines)
529 {
530         static const int stab[] = {40, 100, 300, 1200}; /* bonus per line completed */
531
532         assert(nlines < 5);
533
534         score += stab[nlines - 1] * (level + 1);
535         lines += nlines;
536
537         level = lines / 10;
538         if(level > NUM_LEVELS - 1) level = NUM_LEVELS - 1;
539
540         tick_interval = level_speed[level];
541 }
542
543 static int spawn(void)
544 {
545         int r, tries = 2;
546
547         do {
548                 r = rand() % NUM_BLOCKS;
549         } while(tries-- > 0 && (r | prev_block | next_block) == prev_block);
550
551         cur_block = next_block;
552         next_block = r;
553
554         prev_rot = cur_rot = 0;
555         pos[0] = block_spawnpos[cur_block][0];
556         next_pos[0] = pos[0] + 1;
557         pos[1] = next_pos[1] = PF_COLS / 2 + block_spawnpos[cur_block][1];
558
559         if(collision(cur_block, next_pos)) {
560                 return -1;
561         }
562
563         just_spawned = 1;
564         return 0;
565 }
566
567 static int collision(int block, const int *pos)
568 {
569         int i;
570         unsigned char *p = blocks[block][cur_rot];
571
572         for(i=0; i<4; i++) {
573                 int x = pos[1] + BLKX(*p);
574                 int y = pos[0] + BLKY(*p);
575                 p++;
576
577                 if(y < 0) continue;
578
579                 if(x < 0 || x >= PF_COLS || y >= PF_ROWS) return 1;
580                 if(pfield[y * PF_COLS + x] & PF_FULL) return 1;
581         }
582
583         return 0;
584 }
585
586 static void stick(int block, const int *pos)
587 {
588         int i, j, nblank;
589         unsigned int *pfline;
590         unsigned char *p = blocks[block][cur_rot];
591
592         num_complines = 0;
593         prev_block = cur_block; /* used by the spawn routine */
594         cur_block = -1;
595
596         for(i=0; i<4; i++) {
597                 int x = pos[1] + BLKX(*p);
598                 int y = pos[0] + BLKY(*p);
599                 p++;
600
601                 pfline = pfield + y * PF_COLS;
602                 pfline[x] = PF_FULL | PF_VIS | block;
603
604                 nblank = 0;
605                 for(j=0; j<PF_COLS; j++) {
606                         if(!(pfline[j] & PF_FULL)) {
607                                 nblank++;
608                         }
609                 }
610
611                 if(nblank == 0) {
612                         complines[num_complines++] = y;
613                 }
614         }
615
616         /*snd_stick();*/
617
618         if(num_complines) {
619                 addscore(num_complines);
620         }
621 }
622
623 static void erase_completed(void)
624 {
625         int i, j, srow, drow;
626         unsigned int *pfstart = pfield;
627         unsigned int *dptr;
628
629         /* sort completed lines from highest to lowest row number */
630         for(i=0; i<num_complines-1; i++) {
631                 for(j=i+1; j<num_complines; j++) {
632                         if(complines[j] > complines[i]) {
633                                 int tmp = complines[j];
634                                 complines[j] = complines[i];
635                                 complines[i] = tmp;
636                         }
637                 }
638         }
639
640         srow = drow = PF_ROWS - 1;
641         dptr = pfstart + drow * PF_COLS;
642
643         for(i=0; i<PF_ROWS; i++) {
644                 for(j=0; j<num_complines; j++) {
645                         if(complines[j] == srow) {
646                                 srow--;
647                         }
648                 }
649
650                 if(srow < 0) {
651                         for(j=0; j<PF_COLS; j++) {
652                                 dptr[j] &= ~PF_FULL;
653                         }
654
655                 } else if(srow != drow) {
656                         unsigned int *sptr = pfstart + srow * PF_COLS;
657                         memcpy(dptr, sptr, PF_COLS * sizeof *dptr);
658                 }
659
660                 srow--;
661                 drow--;
662                 dptr -= PF_COLS;
663         }
664 }
665