12 static int init(void);
13 static void cleanup(void);
14 static void start(void);
15 static void stop(void);
16 static void update(float dt);
17 static void draw(void);
18 static void draw_block(int block, const int *pos, int rot);
19 static void drawpf(void);
20 static void reshape(int x, int y);
21 static void keyboard(int key, int pressed);
22 static void mouse(int bn, int pressed, int x, int y);
23 static void motion(int x, int y);
24 static void wheel(int dir);
26 static void update_cur_block(void);
27 static void addscore(int nlines);
28 static int spawn(void);
29 static int collision(int block, const int *pos);
30 static void stick(int block, const int *pos);
31 static void erase_completed(void);
33 struct game_screen game_screen = {
50 static struct cmesh *blkmesh, *wellmesh;
51 static unsigned int tex_well;
53 static float cam_theta, cam_phi, cam_dist = 30;
54 static int bnstate[16];
55 static int prev_mx, prev_my;
57 static long tick_interval;
59 /* dimensions of the playfield */
65 #define PF_VIS_SHIFT 9
67 static unsigned int pfield[PF_ROWS * PF_COLS];
69 static int pos[2], next_pos[2];
70 static int cur_block, next_block, prev_block;
71 static int cur_rot, prev_rot;
72 static int complines[4];
73 static int num_complines;
76 static int score, level, lines;
77 static int just_spawned;
80 static const long level_speed[NUM_LEVELS] = {
81 887, 820, 753, 686, 619, 552, 469, 368, 285, 184,
82 167, 151, 134, 117, 107, 98, 88, 79, 69, 60, 50
85 static const float blkcolor[][4] = {
98 if(!(blkmesh = cmesh_alloc()) || cmesh_load(blkmesh, "data/noisecube.obj") == -1) {
99 error_log("failed to load block mesh\n");
103 if(!(wellmesh = cmesh_alloc()) || cmesh_load(wellmesh, "data/well.obj") == -1) {
104 error_log("failed to load well mesh\n");
108 if(!(tex_well = img_gltexture_load("data/grid.png"))) {
109 error_log("failed to load well texture\n");
116 static void cleanup(void)
121 static void start(void)
128 score = level = lines = 0;
129 tick_interval = level_speed[0];
132 next_block = rand() % NUM_BLOCKS;
134 memset(pfield, 0, PF_COLS * PF_ROWS * sizeof *pfield);
137 static void stop(void)
141 static void update(float dtsec)
143 static long prev_tick;
147 prev_tick = time_msec;
150 dt = time_msec - prev_tick;
154 int i, row = PF_ROWS - gameover;
157 if(dt < GAMEOVER_FILL_RATE) {
162 ptr = pfield + row * PF_COLS;
163 for(i=0; i<PF_COLS; i++) {
164 *ptr++ = TILE_GAMEOVER;
175 /* lines where completed, we're in blinking mode */
176 int i, j, blink = dt >> 8;
184 for(i=0; i<num_complines; i++) {
185 unsigned int *ptr = pfield + complines[i] * PF_COLS;
186 for(j=0; j<PF_COLS; j++) {
187 *ptr = (*ptr & ~PF_VIS) | ((blink & 1) << PF_VIS_SHIFT);
194 while(dt >= tick_interval) {
197 next_pos[0] = pos[0] + 1;
198 if(collision(cur_block, next_pos)) {
199 next_pos[0] = pos[0];
200 stick(cur_block, next_pos);
212 prev_tick = time_msec;
218 static void draw(void)
220 const float lpos[] = {-1, 1, 6, 1};
222 glTranslatef(0, 0, -cam_dist);
223 glRotatef(cam_phi, 1, 0, 0);
224 glRotatef(cam_theta, 0, 1, 0);
226 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
228 glPushAttrib(GL_ENABLE_BIT);
229 glBindTexture(GL_TEXTURE_2D, tex_well);
230 glEnable(GL_TEXTURE_2D);
231 glDisable(GL_LIGHTING);
233 cmesh_draw(wellmesh);
236 /* center playfield */
238 glTranslatef(-PF_COLS / 2 + 0.5, PF_ROWS / 2 - 0.5, 0);
242 draw_block(cur_block, pos, cur_rot);
248 static const float blkspec[] = {1, 1, 1, 1};
250 static void draw_block(int block, const int *pos, int rot)
253 unsigned char *p = blocks[block][rot];
255 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blkcolor[block]);
256 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blkspec);
257 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f);
260 int x = pos[1] + BLKX(*p);
261 int y = pos[0] + BLKY(*p);
267 glTranslatef(x, -y, 0);
273 static void drawpf(void)
276 unsigned int *sptr = pfield;
278 for(i=0; i<PF_ROWS; i++) {
279 for(j=0; j<PF_COLS; j++) {
280 unsigned int val = *sptr++;
282 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, blkcolor[val & 7]);
283 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, blkspec);
284 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 50.0f);
288 glTranslatef(j, -i, 0);
297 static void reshape(int x, int y)
301 static void keyboard(int key, int pressed)
310 next_pos[1] = pos[1] - 1;
311 if(collision(cur_block, next_pos)) {
312 next_pos[1] = pos[1];
321 next_pos[1] = pos[1] + 1;
322 if(collision(cur_block, next_pos)) {
323 next_pos[1] = pos[1];
333 cur_rot = (cur_rot + 1) & 3;
334 if(collision(cur_block, next_pos)) {
343 /* ignore drops until the first update after a spawn */
344 if(cur_block >= 0 && !just_spawned && !pause) {
345 next_pos[0] = pos[0] + 1;
346 if(collision(cur_block, next_pos)) {
347 next_pos[0] = pos[0];
349 stick(cur_block, next_pos); /* stick immediately */
356 if(!pause && cur_block >= 0) {
357 next_pos[0] = pos[0] + 1;
358 while(!collision(cur_block, next_pos)) {
363 stick(cur_block, next_pos); /* stick immediately */
370 if(score && is_highscore(score)) {
371 name = name_screen(score);
373 save_score(name, score, lines, level);
375 /* TODO: pop screen */
383 if(score && is_highscore(score)) {
384 name = name_screen(score);
386 save_score(name, score, lines, level);
388 /* TODO: pop screen */
396 static void mouse(int bn, int pressed, int x, int y)
398 bnstate[bn] = pressed;
403 static void motion(int x, int y)
405 float dx = x - prev_mx;
406 float dy = y - prev_my;
411 cam_theta += dx * 0.5;
414 if(cam_phi < -90) cam_phi = -90;
415 if(cam_phi > 90) cam_phi = 90;
418 cam_dist += dy * 0.1;
419 if(cam_dist < 0) cam_dist = 0;
423 static void wheel(int dir)
427 static void update_cur_block(void)
429 if(cur_block < 0) return;
431 memcpy(pos, next_pos, sizeof pos);
435 static void addscore(int nlines)
437 static const int stab[] = {40, 100, 300, 1200}; /* bonus per line completed */
441 score += stab[nlines - 1] * (level + 1);
445 if(level > NUM_LEVELS - 1) level = NUM_LEVELS - 1;
447 tick_interval = level_speed[level];
450 static int spawn(void)
455 r = rand() % NUM_BLOCKS;
456 } while(tries-- > 0 && (r | prev_block | next_block) == prev_block);
458 cur_block = next_block;
461 prev_rot = cur_rot = 0;
462 pos[0] = block_spawnpos[cur_block][0];
463 next_pos[0] = pos[0] + 1;
464 pos[1] = next_pos[1] = PF_COLS / 2 + block_spawnpos[cur_block][1];
466 if(collision(cur_block, next_pos)) {
474 static int collision(int block, const int *pos)
477 unsigned char *p = blocks[block][cur_rot];
480 int x = pos[1] + BLKX(*p);
481 int y = pos[0] + BLKY(*p);
486 if(x < 0 || x >= PF_COLS || y >= PF_ROWS) return 1;
487 if(pfield[y * PF_COLS + x] & PF_FULL) return 1;
493 static void stick(int block, const int *pos)
496 unsigned int *pfline;
497 unsigned char *p = blocks[block][cur_rot];
500 prev_block = cur_block; /* used by the spawn routine */
504 int x = pos[1] + BLKX(*p);
505 int y = pos[0] + BLKY(*p);
508 pfline = pfield + y * PF_COLS;
509 pfline[x] = PF_FULL | PF_VIS | block;
512 for(j=0; j<PF_COLS; j++) {
513 if(!(pfline[j] & PF_FULL)) {
519 complines[num_complines++] = y;
526 addscore(num_complines);
530 static void erase_completed(void)
532 int i, j, srow, drow;
533 unsigned int *pfstart = pfield;
536 /* sort completed lines from highest to lowest row number */
537 for(i=0; i<num_complines-1; i++) {
538 for(j=i+1; j<num_complines; j++) {
539 if(complines[j] > complines[i]) {
540 int tmp = complines[j];
541 complines[j] = complines[i];
547 srow = drow = PF_ROWS - 1;
548 dptr = pfstart + drow * PF_COLS;
550 for(i=0; i<PF_ROWS; i++) {
551 for(j=0; j<num_complines; j++) {
552 if(complines[j] == srow) {
558 for(j=0; j<PF_COLS; j++) {
562 } else if(srow != drow) {
563 unsigned int *sptr = pfstart + srow * PF_COLS;
564 memcpy(dptr, sptr, PF_COLS * sizeof *dptr);