15 int init_starfield(void);
16 void draw_starfield(void);
18 static int init(void);
19 static void cleanup(void);
20 static void start(void);
21 static void stop(void);
22 static void update(float dt);
23 static void draw(void);
24 static void draw_block(int block, const int *pos, int rot, float sat, float alpha);
25 static void drawpf(void);
26 static void reshape(int x, int y);
27 static void keyboard(int key, int pressed);
28 static void mouse(int bn, int pressed, int x, int y);
29 static void motion(int x, int y);
30 static void wheel(int dir);
32 static void update_cur_block(void);
33 static void addscore(int nlines);
34 static int spawn(void);
35 static int collision(int block, const int *pos);
36 static void stick(int block, const int *pos);
37 static void erase_completed(void);
39 struct game_screen game_screen = {
56 static struct cmesh *blkmesh, *wellmesh;
57 static unsigned int tex_well;
59 static float cam_theta, cam_phi, cam_dist = 30;
60 static int bnstate[16];
61 static int prev_mx, prev_my;
63 static long tick_interval;
65 /* dimensions of the playfield */
71 #define PF_VIS_SHIFT 9
73 static unsigned int pfield[PF_ROWS * PF_COLS];
75 static int pos[2], next_pos[2];
76 static int cur_block, next_block, prev_block;
77 static int cur_rot, prev_rot;
78 static int complines[4];
79 static int num_complines;
82 static int score, level, lines;
83 static int just_spawned;
86 static const long level_speed[NUM_LEVELS] = {
87 887, 820, 753, 686, 619, 552, 469, 368, 285, 184,
88 167, 151, 134, 117, 107, 98, 88, 79, 69, 60, 50
91 static const float blkcolor[][4] = {
102 #define GAMEOVER_FILL_RATE 50
105 static int init(void)
107 if(init_starfield() == -1) {
111 if(!(blkmesh = cmesh_alloc()) || cmesh_load(blkmesh, "data/noisecube.obj") == -1) {
112 error_log("failed to load block mesh\n");
116 if(!(wellmesh = cmesh_alloc()) || cmesh_load(wellmesh, "data/well.obj") == -1) {
117 error_log("failed to load well mesh\n");
121 if(!(tex_well = img_gltexture_load("data/grid.png"))) {
122 error_log("failed to load well texture\n");
129 static void cleanup(void)
132 cmesh_free(wellmesh);
133 glDeleteTextures(1, &tex_well);
136 static void start(void)
143 score = level = lines = 0;
144 tick_interval = level_speed[0];
147 next_block = rand() % NUM_BLOCKS;
149 memset(pfield, 0, PF_COLS * PF_ROWS * sizeof *pfield);
151 ginp_repeat(500, 75, GINP_LEFT | GINP_RIGHT | GINP_DOWN);
154 static void stop(void)
160 #define CHECK_BUTTON(idx, gbn) \
161 if(joy_bnstate & (1 << idx)) { \
162 ginp_bnstate |= gbn; \
165 static void update_input(float dtsec)
169 if((num_vr_sticks = goatvr_num_sticks()) > 0) {
172 goatvr_stick_pos(0, p);
174 if(fabs(p[0]) > fabs(joy_axis[GPAD_LSTICK_X])) {
175 joy_axis[GPAD_LSTICK_X] = p[0];
177 if(fabs(p[1]) > fabs(joy_axis[GPAD_LSTICK_Y])) {
178 joy_axis[GPAD_LSTICK_Y] = p[1];
185 if(joy_axis[GPAD_LSTICK_X] >= JTHRES) {
186 ginp_bnstate |= GINP_RIGHT;
187 } else if(joy_axis[GPAD_LSTICK_X] <= -JTHRES) {
188 ginp_bnstate |= GINP_LEFT;
191 if(joy_axis[GPAD_LSTICK_Y] >= JTHRES) {
192 ginp_bnstate |= GINP_DOWN;
193 } else if(joy_axis[GPAD_LSTICK_Y] <= -JTHRES) {
194 ginp_bnstate |= GINP_UP;
197 CHECK_BUTTON(GPAD_LEFT, GINP_LEFT);
198 CHECK_BUTTON(GPAD_RIGHT, GINP_RIGHT);
199 CHECK_BUTTON(GPAD_UP, GINP_UP);
200 CHECK_BUTTON(GPAD_DOWN, GINP_DOWN);
201 CHECK_BUTTON(GPAD_A, GINP_ROTATE);
202 CHECK_BUTTON(GPAD_START, GINP_PAUSE);
206 if(GINP_PRESS(GINP_LEFT)) {
207 game_keyboard('a', 1);
209 if(GINP_PRESS(GINP_RIGHT)) {
210 game_keyboard('d', 1);
212 if(GINP_PRESS(GINP_DOWN)) {
213 game_keyboard('s', 1);
215 if(GINP_PRESS(GINP_UP)) {
216 game_keyboard('\t', 1);
218 if(GINP_PRESS(GINP_ROTATE)) {
219 game_keyboard('w', 1);
221 if(GINP_PRESS(GINP_PAUSE)) {
222 game_keyboard('p', 1);
226 static void update(float dtsec)
228 static long prev_tick;
234 prev_tick = time_msec;
237 dt = time_msec - prev_tick;
240 int i, row = PF_ROWS - gameover;
243 if(dt < GAMEOVER_FILL_RATE) {
248 ptr = pfield + row * PF_COLS;
249 for(i=0; i<PF_COLS; i++) {
250 *ptr++ = PF_VIS | PF_FULL | 7;
254 prev_tick = time_msec;
260 /* lines where completed, we're in blinking mode */
261 int i, j, blink = dt >> 8;
269 for(i=0; i<num_complines; i++) {
270 unsigned int *ptr = pfield + complines[i] * PF_COLS;
271 for(j=0; j<PF_COLS; j++) {
272 *ptr = (*ptr & ~PF_VIS) | ((blink & 1) << PF_VIS_SHIFT);
280 while(dt >= tick_interval) {
283 next_pos[0] = pos[0] + 1;
284 if(collision(cur_block, next_pos)) {
285 next_pos[0] = pos[0];
286 stick(cur_block, next_pos);
298 prev_tick = time_msec;
304 static void draw(void)
306 static const int nextblk_pos[] = {0, 0};
307 static const float lpos[] = {-1, 1, 6, 1};
310 glTranslatef(0, 0, -cam_dist);
311 glRotatef(cam_phi, 1, 0, 0);
312 glRotatef(cam_theta, 0, 1, 0);
314 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
318 glPushAttrib(GL_ENABLE_BIT);
319 glBindTexture(GL_TEXTURE_2D, tex_well);
320 glEnable(GL_TEXTURE_2D);
321 glDisable(GL_LIGHTING);
323 cmesh_draw(wellmesh);
326 /* center playfield */
328 glTranslatef(-PF_COLS / 2 + 0.5, PF_ROWS / 2 - 0.5, 0);
332 draw_block(cur_block, pos, cur_rot, 1.0f, 1.0f);
336 glPushAttrib(GL_ENABLE_BIT);
338 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
341 t = (float)time_msec / 1000.0f;
342 glTranslatef(-PF_COLS / 2 + 0.5 + PF_COLS + 3, PF_ROWS / 2 - 0.5, 0);
343 glTranslatef(1.5, -1, 0);
344 glRotatef(cos(t) * 8.0f, 1, 0, 0);
345 glRotatef(sin(t * 1.2f) * 10.0f, 0, 1, 0);
346 glTranslatef(-1.5, 1, 0);
347 draw_block(next_block, nextblk_pos, 0, 0.25f, 0.75f);
353 static const float blkspec[] = {0.85, 0.85, 0.85, 1};
355 static void draw_block(int block, const int *pos, int rot, float sat, float alpha)
358 unsigned char *p = blocks[block][rot];
359 float col[4], hsv[3];
361 rgb_to_hsv(blkcolor[block][0], blkcolor[block][1], blkcolor[block][2],
362 hsv, hsv + 1, hsv + 2);
363 hsv_to_rgb(hsv[0], hsv[1] * sat, hsv[2], col, col + 1, col + 2);
366 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, col);
367 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blkspec);
368 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f);
371 int x = pos[1] + BLKX(*p);
372 int y = pos[0] + BLKY(*p);
378 glTranslatef(x, -y, 0);
384 static void drawpf(void)
387 unsigned int *sptr = pfield;
389 for(i=0; i<PF_ROWS; i++) {
390 for(j=0; j<PF_COLS; j++) {
391 unsigned int val = *sptr++;
393 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blkcolor[val & 7]);
394 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blkspec);
395 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f);
397 if((val & (PF_FULL | PF_VIS)) == (PF_FULL | PF_VIS)) {
399 glTranslatef(j, -i, 0);
408 static void reshape(int x, int y)
412 static void keyboard(int key, int pressed)
422 next_pos[1] = pos[1] - 1;
423 if(collision(cur_block, next_pos)) {
424 next_pos[1] = pos[1];
434 next_pos[1] = pos[1] + 1;
435 if(collision(cur_block, next_pos)) {
436 next_pos[1] = pos[1];
448 cur_rot = (cur_rot + 1) & 3;
449 if(collision(cur_block, next_pos)) {
459 /* ignore drops until the first update after a spawn */
460 if(cur_block >= 0 && !just_spawned && !pause) {
461 next_pos[0] = pos[0] + 1;
462 if(collision(cur_block, next_pos)) {
463 next_pos[0] = pos[0];
465 stick(cur_block, next_pos); /* stick immediately */
473 if(!pause && cur_block >= 0) {
474 next_pos[0] = pos[0] + 1;
475 while(!collision(cur_block, next_pos)) {
480 stick(cur_block, next_pos); /* stick immediately */
487 if(score && is_highscore(score)) {
488 name = name_screen(score);
490 save_score(name, score, lines, level);
492 /* TODO: pop screen */
500 if(score && is_highscore(score)) {
501 name = name_screen(score);
503 save_score(name, score, lines, level);
505 /* TODO: pop screen */
513 static void mouse(int bn, int pressed, int x, int y)
515 bnstate[bn] = pressed;
520 static void motion(int x, int y)
522 float dx = x - prev_mx;
523 float dy = y - prev_my;
528 cam_theta += dx * 0.5;
531 if(cam_phi < -90) cam_phi = -90;
532 if(cam_phi > 90) cam_phi = 90;
535 cam_dist += dy * 0.1;
536 if(cam_dist < 0) cam_dist = 0;
540 static void wheel(int dir)
544 static void update_cur_block(void)
546 if(cur_block < 0) return;
548 memcpy(pos, next_pos, sizeof pos);
552 static void addscore(int nlines)
554 static const int stab[] = {40, 100, 300, 1200}; /* bonus per line completed */
558 score += stab[nlines - 1] * (level + 1);
562 if(level > NUM_LEVELS - 1) level = NUM_LEVELS - 1;
564 tick_interval = level_speed[level];
567 static int spawn(void)
572 r = rand() % NUM_BLOCKS;
573 } while(tries-- > 0 && (r | prev_block | next_block) == prev_block);
575 cur_block = next_block;
578 prev_rot = cur_rot = 0;
579 pos[0] = block_spawnpos[cur_block][0];
580 next_pos[0] = pos[0] + 1;
581 pos[1] = next_pos[1] = PF_COLS / 2 + block_spawnpos[cur_block][1];
583 if(collision(cur_block, next_pos)) {
591 static int collision(int block, const int *pos)
594 unsigned char *p = blocks[block][cur_rot];
597 int x = pos[1] + BLKX(*p);
598 int y = pos[0] + BLKY(*p);
603 if(x < 0 || x >= PF_COLS || y >= PF_ROWS) return 1;
604 if(pfield[y * PF_COLS + x] & PF_FULL) return 1;
610 static void stick(int block, const int *pos)
613 unsigned int *pfline;
614 unsigned char *p = blocks[block][cur_rot];
617 prev_block = cur_block; /* used by the spawn routine */
621 int x = pos[1] + BLKX(*p);
622 int y = pos[0] + BLKY(*p);
625 pfline = pfield + y * PF_COLS;
626 pfline[x] = PF_FULL | PF_VIS | block;
629 for(j=0; j<PF_COLS; j++) {
630 if(!(pfline[j] & PF_FULL)) {
636 complines[num_complines++] = y;
643 addscore(num_complines);
647 static void erase_completed(void)
649 int i, j, srow, drow;
650 unsigned int *pfstart = pfield;
653 /* sort completed lines from highest to lowest row number */
654 for(i=0; i<num_complines-1; i++) {
655 for(j=i+1; j<num_complines; j++) {
656 if(complines[j] > complines[i]) {
657 int tmp = complines[j];
658 complines[j] = complines[i];
664 srow = drow = PF_ROWS - 1;
665 dptr = pfstart + drow * PF_COLS;
667 for(i=0; i<PF_ROWS; i++) {
668 for(j=0; j<num_complines; j++) {
669 if(complines[j] == srow) {
675 for(j=0; j<PF_COLS; j++) {
679 } else if(srow != drow) {
680 unsigned int *sptr = pfstart + srow * PF_COLS;
681 memcpy(dptr, sptr, PF_COLS * sizeof *dptr);