20 int init_starfield(void);
21 void draw_starfield(void);
23 static int init(void);
24 static void cleanup(void);
25 static void start(void);
26 static void stop(void);
27 static void update(float dt);
28 static void draw(void);
29 static void draw_block(int block, const int *pos, int rot, float sat, float alpha);
30 static void drawpf(void);
31 static void reshape(int x, int y);
32 static void keyboard(int key, int pressed);
33 static void mouse(int bn, int pressed, int x, int y);
34 static void motion(int x, int y);
35 static void wheel(int dir);
37 static void update_cur_block(void);
38 static void addscore(int nlines);
39 static int spawn(void);
40 static int collision(int block, const int *pos);
41 static void stick(int block, const int *pos);
42 static void erase_completed(void);
44 struct game_screen game_screen = {
61 static struct cmesh *blkmesh, *wellmesh;
62 static unsigned int tex_well;
64 static struct dtx_font *scorefont;
66 static float cam_theta, cam_phi = -15, cam_dist = 30;
67 static int bnstate[16];
68 static int prev_mx, prev_my;
70 static long tick_interval;
72 /* dimensions of the playfield */
78 #define PF_VIS_SHIFT 9
80 static unsigned int pfield[PF_ROWS * PF_COLS];
82 static int pos[2], next_pos[2];
83 static int cur_block, next_block, prev_block;
84 static int cur_rot, prev_rot;
85 static int complines[4];
86 static int num_complines;
89 static int score, level, lines;
90 static int just_spawned;
93 static int vrbn_a = 0, vrbn_x = 4;
97 static const long level_speed[NUM_LEVELS] = {
98 887, 820, 753, 686, 619, 552, 469, 368, 285, 184,
99 167, 151, 134, 117, 107, 98, 88, 79, 69, 60, 50
102 static const float blkcolor[][4] = {
105 {0.65, 0.65, 1.0, 1},
113 #define GAMEOVER_FILL_RATE 50
116 static int init(void)
118 if(!(scorefont = dtx_open_font("data/score.font", 0))) {
119 error_log("failed to open score font\n");
122 dtx_prepare_range(scorefont, FONTSZ, 32, 127);
123 dtx_save_glyphmap("foo.ppm", dtx_get_glyphmap(scorefont, 0));
125 if(init_starfield() == -1) {
129 if(!(blkmesh = cmesh_alloc()) || cmesh_load(blkmesh, "data/noisecube.obj") == -1) {
130 error_log("failed to load block mesh\n");
134 if(!(wellmesh = cmesh_alloc()) || cmesh_load(wellmesh, "data/well.obj") == -1) {
135 error_log("failed to load well mesh\n");
139 if(!(tex_well = img_gltexture_load("data/grid.png"))) {
140 error_log("failed to load well texture\n");
147 static void cleanup(void)
150 cmesh_free(wellmesh);
151 glDeleteTextures(1, &tex_well);
152 dtx_close_font(scorefont);
155 static void start(void)
162 score = level = lines = 0;
163 tick_interval = level_speed[0];
166 next_block = rand() % NUM_BLOCKS;
168 memset(pfield, 0, PF_COLS * PF_ROWS * sizeof *pfield);
170 ginp_repeat(500, 75, GINP_LEFT | GINP_RIGHT | GINP_DOWN);
172 dtx_use_font(scorefont, FONTSZ);
176 int bn = goatvr_lookup_button("A");
177 if(bn >= 0) vrbn_a = bn;
178 debug_log("lookup button A: %d\n", bn);
180 bn = goatvr_lookup_button("X");
181 if(bn >= 0) vrbn_x = bn;
182 debug_log("lookup button X: %d\n", bn);
187 static void stop(void)
193 #define CHECK_BUTTON(idx, gbn) \
194 if(joy_bnstate & (1 << idx)) { \
195 ginp_bnstate |= gbn; \
198 static void update_input(float dtsec)
203 if((num_vr_sticks = goatvr_num_sticks()) > 0) {
206 goatvr_stick_pos(0, p);
208 if(fabs(p[0]) > fabs(joy_axis[GPAD_LSTICK_X])) {
209 joy_axis[GPAD_LSTICK_X] = p[0];
211 if(fabs(p[1]) > fabs(joy_axis[GPAD_LSTICK_Y])) {
212 joy_axis[GPAD_LSTICK_Y] = -p[1];
215 if(goatvr_button_state(vrbn_a)) {
216 joy_bnstate |= GPAD_A;
218 if(goatvr_button_state(vrbn_x)) {
219 joy_bnstate |= GPAD_START;
221 if(goatvr_action(0, GOATVR_ACTION_TRIGGER) || goatvr_action(1, GOATVR_ACTION_TRIGGER)) {
222 joy_bnstate |= GPAD_UP;
225 #endif /* BUILD_VR */
230 if(joy_axis[GPAD_LSTICK_X] >= JTHRES) {
231 ginp_bnstate |= GINP_RIGHT;
232 } else if(joy_axis[GPAD_LSTICK_X] <= -JTHRES) {
233 ginp_bnstate |= GINP_LEFT;
236 if(joy_axis[GPAD_LSTICK_Y] >= JTHRES) {
237 ginp_bnstate |= GINP_DOWN;
238 } else if(joy_axis[GPAD_LSTICK_Y] <= -JTHRES) {
239 ginp_bnstate |= GINP_UP;
242 CHECK_BUTTON(GPAD_LEFT, GINP_LEFT);
243 CHECK_BUTTON(GPAD_RIGHT, GINP_RIGHT);
244 CHECK_BUTTON(GPAD_UP, GINP_UP);
245 CHECK_BUTTON(GPAD_DOWN, GINP_DOWN);
246 CHECK_BUTTON(GPAD_A, GINP_ROTATE);
247 CHECK_BUTTON(GPAD_START, GINP_PAUSE);
251 if(GINP_PRESS(GINP_LEFT)) {
252 game_keyboard('a', 1);
254 if(GINP_PRESS(GINP_RIGHT)) {
255 game_keyboard('d', 1);
257 if(GINP_PRESS(GINP_DOWN)) {
258 game_keyboard('s', 1);
260 if(GINP_PRESS(GINP_UP)) {
261 game_keyboard('\t', 1);
263 if(GINP_PRESS(GINP_ROTATE)) {
264 game_keyboard('w', 1);
266 if(GINP_PRESS(GINP_PAUSE)) {
267 game_keyboard('p', 1);
271 memset(joy_axis, 0, sizeof joy_axis);
275 static void update(float dtsec)
277 static long prev_tick;
283 prev_tick = time_msec;
286 dt = time_msec - prev_tick;
289 int i, row = PF_ROWS - gameover;
292 if(dt < GAMEOVER_FILL_RATE) {
297 ptr = pfield + row * PF_COLS;
298 for(i=0; i<PF_COLS; i++) {
299 *ptr++ = PF_VIS | PF_FULL | 7;
303 prev_tick = time_msec;
309 /* lines where completed, we're in blinking mode */
310 int i, j, blink = dt >> 8;
318 for(i=0; i<num_complines; i++) {
319 unsigned int *ptr = pfield + complines[i] * PF_COLS;
320 for(j=0; j<PF_COLS; j++) {
321 *ptr = (*ptr & ~PF_VIS) | ((blink & 1) << PF_VIS_SHIFT);
329 while(dt >= tick_interval) {
332 next_pos[0] = pos[0] + 1;
333 if(collision(cur_block, next_pos)) {
334 next_pos[0] = pos[0];
335 stick(cur_block, next_pos);
347 prev_tick = time_msec;
353 static void draw(void)
355 static const int nextblk_pos[] = {0, 0};
356 static const float lpos[] = {-1, 1, 6, 1};
359 glTranslatef(0, 0, -cam_dist);
360 glRotatef(cam_phi, 1, 0, 0);
361 glRotatef(cam_theta, 0, 1, 0);
363 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
367 glPushAttrib(GL_ENABLE_BIT);
368 glBindTexture(GL_TEXTURE_2D, tex_well);
369 glEnable(GL_TEXTURE_2D);
370 glDisable(GL_LIGHTING);
372 cmesh_draw(wellmesh);
375 /* center playfield */
377 glTranslatef(-PF_COLS / 2 + 0.5, PF_ROWS / 2 - 0.5, 0);
381 draw_block(cur_block, pos, cur_rot, 1.0f, 1.0f);
385 glPushAttrib(GL_ENABLE_BIT);
387 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
390 t = (float)time_msec / 1000.0f;
391 glTranslatef(-PF_COLS / 2 + 0.5 + PF_COLS + 3, PF_ROWS / 2 - 0.5, 0);
392 glTranslatef(1.5, -1, 0);
393 glRotatef(cos(t) * 8.0f, 1, 0, 0);
394 glRotatef(sin(t * 1.2f) * 10.0f, 0, 1, 0);
395 glTranslatef(-1.5, 1, 0);
396 draw_block(next_block, nextblk_pos, 0, 0.25f, 0.75f);
400 glPushAttrib(GL_ENABLE_BIT);
401 glDisable(GL_LIGHTING);
404 glTranslatef(-11, 6, 0);
405 glScalef(0.05, 0.05, 0.05);
409 glTranslatef(0, -dtx_line_height() * 1.5, 0);
411 glScalef(1.5, 1.5, 1.5);
412 dtx_printf("%d", score);
415 glTranslatef(0, -dtx_line_height() * 2, 0);
417 glTranslatef(0, -dtx_line_height() * 1.5, 0);
419 glScalef(1.5, 1.5, 1.5);
420 dtx_printf("%d", level);
423 glTranslatef(0, -dtx_line_height() * 2, 0);
425 glTranslatef(0, -dtx_line_height() * 1.5, 0);
427 glScalef(1.5, 1.5, 1.5);
428 dtx_printf("%d", lines);
435 static const float blkspec[] = {0.85, 0.85, 0.85, 1};
437 static void draw_block(int block, const int *pos, int rot, float sat, float alpha)
440 unsigned char *p = blocks[block][rot];
441 float col[4], hsv[3];
443 rgb_to_hsv(blkcolor[block][0], blkcolor[block][1], blkcolor[block][2],
444 hsv, hsv + 1, hsv + 2);
445 hsv_to_rgb(hsv[0], hsv[1] * sat, hsv[2], col, col + 1, col + 2);
448 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
449 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blkspec);
450 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f);
453 int x = pos[1] + BLKX(*p);
454 int y = pos[0] + BLKY(*p);
460 glTranslatef(x, -y, 0);
466 static void drawpf(void)
469 unsigned int *sptr = pfield;
471 for(i=0; i<PF_ROWS; i++) {
472 for(j=0; j<PF_COLS; j++) {
473 unsigned int val = *sptr++;
475 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blkcolor[val & 7]);
476 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blkspec);
477 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f);
479 if((val & (PF_FULL | PF_VIS)) == (PF_FULL | PF_VIS)) {
481 glTranslatef(j, -i, 0);
490 static void reshape(int x, int y)
494 static void keyboard(int key, int pressed)
504 next_pos[1] = pos[1] - 1;
505 if(collision(cur_block, next_pos)) {
506 next_pos[1] = pos[1];
516 next_pos[1] = pos[1] + 1;
517 if(collision(cur_block, next_pos)) {
518 next_pos[1] = pos[1];
530 cur_rot = (cur_rot + 1) & 3;
531 if(collision(cur_block, next_pos)) {
541 /* ignore drops until the first update after a spawn */
542 if(cur_block >= 0 && !just_spawned && !pause) {
543 next_pos[0] = pos[0] + 1;
544 if(collision(cur_block, next_pos)) {
545 next_pos[0] = pos[0];
547 stick(cur_block, next_pos); /* stick immediately */
555 if(!pause && cur_block >= 0) {
556 next_pos[0] = pos[0] + 1;
557 while(!collision(cur_block, next_pos)) {
562 stick(cur_block, next_pos); /* stick immediately */
569 if(score && is_highscore(score)) {
570 name = name_screen(score);
572 save_score(name, score, lines, level);
574 /* TODO: pop screen */
582 if(score && is_highscore(score)) {
583 name = name_screen(score);
585 save_score(name, score, lines, level);
587 /* TODO: pop screen */
595 static void mouse(int bn, int pressed, int x, int y)
597 bnstate[bn] = pressed;
602 static void motion(int x, int y)
604 float dx = x - prev_mx;
605 float dy = y - prev_my;
610 cam_theta += dx * 0.5;
613 if(cam_phi < -90) cam_phi = -90;
614 if(cam_phi > 90) cam_phi = 90;
617 cam_dist += dy * 0.1;
618 if(cam_dist < 0) cam_dist = 0;
622 static void wheel(int dir)
626 static void update_cur_block(void)
628 if(cur_block < 0) return;
630 memcpy(pos, next_pos, sizeof pos);
634 static void addscore(int nlines)
636 static const int stab[] = {40, 100, 300, 1200}; /* bonus per line completed */
640 score += stab[nlines - 1] * (level + 1);
644 if(level > NUM_LEVELS - 1) level = NUM_LEVELS - 1;
646 tick_interval = level_speed[level];
649 static int spawn(void)
654 r = rand() % NUM_BLOCKS;
655 } while(tries-- > 0 && (r | prev_block | next_block) == prev_block);
657 cur_block = next_block;
660 prev_rot = cur_rot = 0;
661 pos[0] = block_spawnpos[cur_block][0];
662 next_pos[0] = pos[0] + 1;
663 pos[1] = next_pos[1] = PF_COLS / 2 + block_spawnpos[cur_block][1];
665 if(collision(cur_block, next_pos)) {
673 static int collision(int block, const int *pos)
676 unsigned char *p = blocks[block][cur_rot];
679 int x = pos[1] + BLKX(*p);
680 int y = pos[0] + BLKY(*p);
685 if(x < 0 || x >= PF_COLS || y >= PF_ROWS) return 1;
686 if(pfield[y * PF_COLS + x] & PF_FULL) return 1;
692 static void stick(int block, const int *pos)
695 unsigned int *pfline;
696 unsigned char *p = blocks[block][cur_rot];
699 prev_block = cur_block; /* used by the spawn routine */
703 int x = pos[1] + BLKX(*p);
704 int y = pos[0] + BLKY(*p);
707 pfline = pfield + y * PF_COLS;
708 pfline[x] = PF_FULL | PF_VIS | block;
711 for(j=0; j<PF_COLS; j++) {
712 if(!(pfline[j] & PF_FULL)) {
718 complines[num_complines++] = y;
725 addscore(num_complines);
729 static void erase_completed(void)
731 int i, j, srow, drow;
732 unsigned int *pfstart = pfield;
735 /* sort completed lines from highest to lowest row number */
736 for(i=0; i<num_complines-1; i++) {
737 for(j=i+1; j<num_complines; j++) {
738 if(complines[j] > complines[i]) {
739 int tmp = complines[j];
740 complines[j] = complines[i];
746 srow = drow = PF_ROWS - 1;
747 dptr = pfstart + drow * PF_COLS;
749 for(i=0; i<PF_ROWS; i++) {
750 for(j=0; j<num_complines; j++) {
751 if(complines[j] == srow) {
757 for(j=0; j<PF_COLS; j++) {
761 } else if(srow != drow) {
762 unsigned int *sptr = pfstart + srow * PF_COLS;
763 memcpy(dptr, sptr, PF_COLS * sizeof *dptr);