2 * The contents of this file are subject to the Mozilla Public License
3 * Version 1.1 (the "License"); you may not use this file except in
4 * compliance with the License. You may obtain a copy of the License at
5 * http://www.mozilla.org/MPL/
7 * Software distributed under the License is distributed on an "AS IS"
8 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
9 * License for the specific language governing rights and limitations
12 * The Original Code is NanoTetris.
14 * The Initial Developer of the Original Code is Alex Holden.
15 * Portions created by Alex Holden are Copyright (C) 2000
16 * Alex Holden <alex@linuxhacker.org>. All Rights Reserved.
20 * Alternatively, the contents of this file may be used under the terms
21 * of the GNU General Public license (the "[GNU] License"), in which case the
22 * provisions of [GNU] License are applicable instead of those
23 * above. If you wish to allow use of your version of this file only
24 * under the terms of the [GNU] License and not to allow others to use
25 * your version of this file under the MPL, indicate your decision by
26 * deleting the provisions above and replace them with the notice and
27 * other provisions required by the [GNU] License. If you do not delete
28 * the provisions above, a recipient may use your version of this file
29 * under either the MPL or the [GNU] License.
33 * A Nano-X Tetris clone by Alex Holden.
35 * The objective is to keep placing new pieces for as long as possible. When a
36 * horizontal line is filled with blocks, it will vanish, and everything above
37 * it will drop down a line. It quickly gets difficult because the speed
38 * increases with the score. Unlike with some Tetris clones, no bonus points
39 * are awarded for matching colours, completing more than one line at a time,
40 * or for using the "drop shape to bottom" function.
42 * The box in the top left of the game window is the score box. The top score
43 * is the highest score you have achieved since last resetting the high score
44 * counter. The counter is stored when the game exits in the file specified by
45 * the HISCORE_FILE parameter ("/usr/games/nanotetris.hiscore" by default).
46 * Note that no attempt is made to encrypt the file, so anybody with write
47 * access to the file can alter the contents of it using a text editor.
49 * The box below the score box is the next shape box. This contains a "preview"
50 * of the next shape to appear, so that you can plan ahead as you are building
53 * The game functions can be controlled using either the mouse (or a touch pad,
54 * touch screen, trackball, etc.) and the buttons below the next shape box, or
55 * with the following keyboard keys:
61 * D = rotate shape anticlockwise
62 * F = rotate shape clockwise
64 * K = move shape right
65 * Space Bar = drop shape to bottom.
67 * The reason for the unconventional use of D, F, J, and K keys is that they
68 * are all in the "home" position of a QWERTY keyboard, which makes them very
69 * easy to press if you are used to touch typing.
71 * I'll leave it to you to figure out which mouse operated movement button does
72 * what (it's pretty obvious).
89 #define MWINCLUDECOLORS
94 static void *my_malloc(size_t size)
98 if(!(ret = malloc(size))) {
99 fprintf(stderr, "Out of memory\n");
107 static void msleep(long ms)
112 static void msleep(long ms)
114 struct timespec req, rem;
116 req.tv_sec = ms / 1000000;
117 req.tv_nsec = (ms % 1000000) * 1000000;
119 while(nanosleep(&req, &rem) == -1) {
121 req.tv_sec = rem.tv_sec;
122 req.tv_nsec = rem.tv_nsec;
125 perror("nanosleep() failed");
132 #ifdef USE_HISCORE_FILE
133 static void read_hiscore(nstate *state)
138 if(!(f = fopen(HISCORE_FILE, "r"))) {
140 perror("Couldn't open high score file for reading");
141 state->hiscore = state->fhiscore = 0;
145 i = fscanf(f, "%d", &n);
149 fprintf(stderr, "Couldn't read high score file\n");
153 state->hiscore = state->fhiscore = n;
156 static void write_hiscore(nstate *state)
160 if(state->score > state->hiscore) state->hiscore = state->score;
161 if(state->hiscore <= state->fhiscore) return;
163 if(!(f = fopen(HISCORE_FILE, "w"))) {
164 perror("Couldn't open high score file for writing");
168 if((fprintf(f, "%d", state->hiscore)) == -1) {
169 perror("Couldn't write to high score file");
175 static void read_hiscore(nstate *state)
180 static void write_hiscore(nstate *state) {}
183 static int will_collide(nstate *state, int x, int y, int orientation)
188 draw_shape(state, state->current_shape.x, state->current_shape.y, 1);
189 for(r = 0; ch < 3; r++) {
191 for(c = 0; ch < 2; c++) {
192 ch = shapes[state->current_shape.type]
197 if((yy == WELL_HEIGHT) || (xx == WELL_WIDTH) ||
198 (state->blocks[0][yy][xx])) {
200 state->current_shape.x,
201 state->current_shape.y, 0);
207 draw_shape(state, state->current_shape.x, state->current_shape.y, 0);
212 static void draw_shape(nstate *state, GR_COORD x, GR_COORD y, int erase)
219 else col = state->current_shape.colour;
221 for(r = 0; ch < 3; r++) {
223 for(c = 0; ch < 2; c++) {
224 ch = shapes[state->current_shape.type]
225 [state->current_shape.orientation][r][c];
229 state->blocks[0][yy][xx] = col;
235 static void draw_well(nstate *state, int forcedraw)
239 for(y = WELL_NOTVISIBLE; y < WELL_HEIGHT; y++) {
240 for(x = 0; x < WELL_WIDTH; x++) {
241 if(forcedraw || (state->blocks[0][y][x] !=
242 state->blocks[1][y][x])) {
243 state->blocks[1][y][x] = state->blocks[0][y][x];
244 GrSetGCForeground(state->wellgc,
245 state->blocks[0][y][x]);
246 GrFillRect(state->well_window, state->wellgc,
248 (BLOCK_SIZE * (y - WELL_NOTVISIBLE)),
249 BLOCK_SIZE, BLOCK_SIZE);
257 static void draw_score(nstate *state)
261 GrFillRect(state->score_window, state->scoregcb, 0, 0,
262 SCORE_WINDOW_WIDTH, SCORE_WINDOW_HEIGHT);
264 sprintf(buf, "%d", state->score);
265 GrText(state->score_window, state->scoregcf, TEXT_X_POSITION,
266 TEXT2_Y_POSITION, buf, strlen(buf), 0);
267 sprintf(buf, "%d", state->hiscore);
268 GrText(state->score_window, state->scoregcf, TEXT_X_POSITION,
269 TEXT_Y_POSITION, buf, strlen(buf), 0);
272 static void draw_next_shape(nstate *state)
274 int r, c, startx, starty, x, y;
277 GrFillRect(state->next_shape_window, state->nextshapegcb, 0, 0,
278 NEXT_SHAPE_WINDOW_WIDTH, NEXT_SHAPE_WINDOW_HEIGHT);
280 GrSetGCForeground(state->nextshapegcf, state->next_shape.colour);
282 startx = (BLOCK_SIZE * ((NEXT_SHAPE_WINDOW_SIZE / 2) -
283 (shape_sizes[state->next_shape.type]
284 [state->next_shape.orientation][0] / 2)));
285 starty = (BLOCK_SIZE * ((NEXT_SHAPE_WINDOW_SIZE / 2) -
286 (shape_sizes[state->next_shape.type]
287 [state->next_shape.orientation][1] / 2)));
289 for(r = 0; ch < 3; r++) {
291 for(c = 0; ch < 2; c++) {
292 ch = shapes[state->next_shape.type]
293 [state->next_shape.orientation][r][c];
295 x = startx + (c * BLOCK_SIZE);
296 y = starty + (r * BLOCK_SIZE);
297 GrFillRect(state->next_shape_window,
298 state->nextshapegcf, x, y,
299 BLOCK_SIZE, BLOCK_SIZE);
305 static void draw_new_game_button(nstate *state)
307 GrFillRect(state->new_game_button, state->buttongcb, 0, 0,
308 NEW_GAME_BUTTON_WIDTH, NEW_GAME_BUTTON_HEIGHT);
309 GrText(state->new_game_button, state->buttongcf, TEXT_X_POSITION,
310 TEXT_Y_POSITION, "New Game", 8, 0);
313 static void draw_anticlockwise_button(nstate *state)
315 if(!state->running_buttons_mapped) return;
316 GrFillRect(state->anticlockwise_button, state->buttongcb, 0, 0,
317 ANTICLOCKWISE_BUTTON_WIDTH, ANTICLOCKWISE_BUTTON_HEIGHT);
318 GrText(state->anticlockwise_button, state->buttongcf, TEXT_X_POSITION,
319 TEXT_Y_POSITION, " /", 4, 0);
322 static void draw_clockwise_button(nstate *state)
324 if(!state->running_buttons_mapped) return;
325 GrFillRect(state->clockwise_button, state->buttongcb, 0, 0,
326 CLOCKWISE_BUTTON_WIDTH, CLOCKWISE_BUTTON_HEIGHT);
327 GrText(state->clockwise_button, state->buttongcf, TEXT_X_POSITION,
328 TEXT_Y_POSITION, " \\", 4, 0);
331 static void draw_left_button(nstate *state)
333 if(!state->running_buttons_mapped) return;
334 GrFillRect(state->left_button, state->buttongcb, 0, 0,
335 LEFT_BUTTON_WIDTH, LEFT_BUTTON_HEIGHT);
336 GrText(state->left_button, state->buttongcf, TEXT_X_POSITION,
337 TEXT_Y_POSITION, " <", 3, 0);
340 static void draw_right_button(nstate *state)
342 if(!state->running_buttons_mapped) return;
343 GrFillRect(state->right_button, state->buttongcb, 0, 0,
344 RIGHT_BUTTON_WIDTH, RIGHT_BUTTON_HEIGHT);
345 GrText(state->right_button, state->buttongcf, TEXT_X_POSITION,
346 TEXT_Y_POSITION, " >", 4, 0);
349 static void draw_drop_button(nstate *state)
351 if(!state->running_buttons_mapped) return;
352 GrFillRect(state->drop_button, state->buttongcb, 0, 0,
353 DROP_BUTTON_WIDTH, DROP_BUTTON_HEIGHT);
354 GrText(state->drop_button, state->buttongcf, TEXT_X_POSITION,
355 TEXT_Y_POSITION, " Drop", 8, 0);
358 static void draw_pause_continue_button(nstate *state)
360 if((state->running_buttons_mapped) && (state->state == STATE_STOPPED)) {
361 GrUnmapWindow(state->pause_continue_button);
362 GrUnmapWindow(state->anticlockwise_button);
363 GrUnmapWindow(state->clockwise_button);
364 GrUnmapWindow(state->left_button);
365 GrUnmapWindow(state->right_button);
366 GrUnmapWindow(state->drop_button);
367 state->running_buttons_mapped = 0;
370 if((!state->running_buttons_mapped) && (state->state == STATE_RUNNING)){
371 GrMapWindow(state->pause_continue_button);
372 GrMapWindow(state->anticlockwise_button);
373 GrMapWindow(state->clockwise_button);
374 GrMapWindow(state->left_button);
375 GrMapWindow(state->right_button);
376 GrMapWindow(state->drop_button);
377 state->running_buttons_mapped = 1;
380 if(!state->running_buttons_mapped) return;
381 GrFillRect(state->pause_continue_button, state->buttongcb, 0, 0,
382 PAUSE_CONTINUE_BUTTON_WIDTH, PAUSE_CONTINUE_BUTTON_HEIGHT);
383 if(state->state == STATE_PAUSED) {
384 GrText(state->pause_continue_button, state->buttongcf,
385 TEXT_X_POSITION, TEXT_Y_POSITION, " Continue", 9, 0);
387 GrText(state->pause_continue_button, state->buttongcf,
388 TEXT_X_POSITION, TEXT_Y_POSITION, " Pause", 8, 0);
392 static int block_is_all_in_well(nstate *state)
394 if(state->current_shape.y >= WELL_NOTVISIBLE)
400 static void delete_line(nstate *state, int line)
404 if(line < WELL_NOTVISIBLE) return;
406 for(y = line - 1; y; y--)
407 for(x = WELL_WIDTH; x; x--)
408 state->blocks[0][y + 1][x] = state->blocks[0][y][x];
413 static void block_reached_bottom(nstate *state)
417 if(!block_is_all_in_well(state)) {
418 state->state = STATE_STOPPED;
422 for(y = WELL_HEIGHT - 1; y; y--) {
423 for(x = 0; x < WELL_WIDTH; x++)
424 if(!state->blocks[0][y][x]) goto nr;
425 msleep(DELETE_LINE_DELAY);
426 delete_line(state, y);
427 state->score += SCORE_INCREMENT;
428 if((LEVELS > (state->level + 1)) && (((state->level + 1) *
429 LEVEL_DIVISOR) <= state->score))
436 choose_new_shape(state);
437 draw_next_shape(state);
440 static void move_block(nstate *state, int direction)
443 if(!state->current_shape.x) return;
445 if(!will_collide(state, (state->current_shape.x - 1),
446 state->current_shape.y,
447 state->current_shape.orientation)) {
448 draw_shape(state, state->current_shape.x,
449 state->current_shape.y, 1);
450 state->current_shape.x--;
451 draw_shape(state, state->current_shape.x,
452 state->current_shape.y, 0);
457 if(!will_collide(state, (state->current_shape.x + 1),
458 state->current_shape.y,
459 state->current_shape.orientation)) {
460 draw_shape(state, state->current_shape.x,
461 state->current_shape.y, 1);
462 state->current_shape.x++;
463 draw_shape(state, state->current_shape.x,
464 state->current_shape.y, 0);
470 static void rotate_block(nstate *state, int direction)
472 int neworientation = 0;
475 if(!state->current_shape.orientation)
476 neworientation = MAXORIENTATIONS - 1;
477 else neworientation = state->current_shape.orientation - 1;
479 neworientation = state->current_shape.orientation + 1;
480 if(neworientation == MAXORIENTATIONS) neworientation = 0;
483 if(!will_collide(state, state->current_shape.x, state->current_shape.y,
485 draw_shape(state, state->current_shape.x,
486 state->current_shape.y, 1);
487 state->current_shape.orientation = neworientation;
488 draw_shape(state, state->current_shape.x,
489 state->current_shape.y, 0);
494 static int drop_block_1(nstate *state)
496 if(will_collide(state, state->current_shape.x,
497 (state->current_shape.y + 1),
498 state->current_shape.orientation)) {
499 block_reached_bottom(state);
503 draw_shape(state, state->current_shape.x, state->current_shape.y, 1);
504 state->current_shape.y++;
505 draw_shape(state, state->current_shape.x, state->current_shape.y, 0);
512 static void drop_block(nstate *state)
514 while(!drop_block_1(state)) msleep(DROP_BLOCK_DELAY);
517 static void handle_exposure_event(nstate *state)
519 GR_EVENT_EXPOSURE *event = &state->event.exposure;
521 if(event->wid == state->score_window) {
525 if(event->wid == state->next_shape_window) {
526 draw_next_shape(state);
529 if(event->wid == state->new_game_button) {
530 draw_new_game_button(state);
533 if(event->wid == state->pause_continue_button) {
534 draw_pause_continue_button(state);
537 if(event->wid == state->anticlockwise_button) {
538 draw_anticlockwise_button(state);
541 if(event->wid == state->clockwise_button) {
542 draw_clockwise_button(state);
545 if(event->wid == state->left_button) {
546 draw_left_button(state);
549 if(event->wid == state->right_button) {
550 draw_right_button(state);
553 if(event->wid == state->drop_button) {
554 draw_drop_button(state);
557 if(event->wid == state->well_window) {
563 static void handle_mouse_event(nstate *state)
565 GR_EVENT_MOUSE *event = &state->event.mouse;
567 if(event->wid == state->new_game_button) {
568 state->state = STATE_NEWGAME;
571 if(event->wid == state->pause_continue_button) {
572 if(state->state == STATE_PAUSED) state->state = STATE_RUNNING;
573 else state->state = STATE_PAUSED;
576 if(event->wid == state->anticlockwise_button) {
577 if(state->state == STATE_PAUSED) state->state = STATE_RUNNING;
578 rotate_block(state, 0);
581 if(event->wid == state->clockwise_button) {
582 if(state->state == STATE_PAUSED) state->state = STATE_RUNNING;
583 rotate_block(state, 1);
586 if(event->wid == state->left_button) {
587 if(state->state == STATE_PAUSED) state->state = STATE_RUNNING;
588 move_block(state, 0);
591 if(event->wid == state->right_button) {
592 if(state->state == STATE_PAUSED) state->state = STATE_RUNNING;
593 move_block(state, 1);
596 if(event->wid == state->drop_button) {
597 if(state->state == STATE_PAUSED) state->state = STATE_RUNNING;
603 static void handle_keyboard_event(nstate *state)
605 GR_EVENT_KEYSTROKE *event = &state->event.keystroke;
611 state->state = STATE_EXIT;
616 state->state = STATE_NEWGAME;
620 if(state->state == STATE_STOPPED) return;
622 state->state = STATE_RUNNING;
627 state->state = STATE_PAUSED;
632 move_block(state, 0);
637 move_block(state, 1);
642 rotate_block(state, 0);
647 rotate_block(state, 1);
656 static void handle_event(nstate *state)
658 switch(state->event.type) {
659 case GR_EVENT_TYPE_EXPOSURE:
660 handle_exposure_event(state);
662 case GR_EVENT_TYPE_BUTTON_DOWN:
663 handle_mouse_event(state);
665 case GR_EVENT_TYPE_KEY_DOWN:
666 handle_keyboard_event(state);
668 case GR_EVENT_TYPE_CLOSE_REQ:
669 state->state = STATE_EXIT;
671 case GR_EVENT_TYPE_TIMEOUT:
674 fprintf(stderr, "Unhandled event type %d\n",
680 static void clear_well(nstate *state)
684 for(y = 0; y < WELL_HEIGHT; y++)
685 for(x = 0; x < WELL_WIDTH; x++) {
686 state->blocks[0][y][x] = 0;
687 state->blocks[1][y][x] = 0;
691 /* Dirty hack alert- this is to avoid using any floating point math */
692 static int random8(int limit)
696 do { ret = random() & 7; } while(ret > limit);
701 static void choose_new_shape(nstate *state)
703 state->current_shape.type = state->next_shape.type;
704 state->current_shape.orientation = state->next_shape.orientation;
705 state->current_shape.colour = state->next_shape.colour;
706 state->current_shape.x = (WELL_WIDTH / 2) - 2;
707 state->current_shape.y = WELL_NOTVISIBLE -
708 shape_sizes[state->next_shape.type]
709 [state->next_shape.orientation][1] - 1;
710 state->next_shape.type = random8(MAXSHAPES - 1);
711 state->next_shape.orientation = random8(MAXORIENTATIONS - 1);
712 state->next_shape.colour = block_colours[random8(MAX_BLOCK_COLOUR)];
715 static void new_game(nstate *state)
718 if(state->score > state->hiscore) state->hiscore = state->score;
722 choose_new_shape(state);
723 draw_next_shape(state);
725 if(state->state == STATE_NEWGAME) state->state = STATE_RUNNING;
728 static void init_game(nstate *state)
730 GR_WM_PROPERTIES props;
733 fprintf(stderr, "Couldn't connect to Nano-X server\n");
737 state->main_window = GrNewWindow(GR_ROOT_WINDOW_ID,
738 MAIN_WINDOW_X_POSITION,
739 MAIN_WINDOW_Y_POSITION,
741 MAIN_WINDOW_HEIGHT, 0,
742 MAIN_WINDOW_BACKGROUND_COLOUR, 0);
744 props.flags = GR_WM_FLAGS_TITLE | GR_WM_FLAGS_PROPS;
745 props.props = GR_WM_PROPS_BORDER | GR_WM_PROPS_CAPTION;
746 props.title = "Nano-Tetris";
747 GrSetWMProperties(state->main_window, &props);
748 GrSelectEvents(state->main_window, GR_EVENT_MASK_EXPOSURE |
749 GR_EVENT_MASK_CLOSE_REQ |
750 GR_EVENT_MASK_KEY_DOWN |
751 GR_EVENT_MASK_TIMEOUT);
753 state->score_window = GrNewWindow(state->main_window,
754 SCORE_WINDOW_X_POSITION,
755 SCORE_WINDOW_Y_POSITION,
757 SCORE_WINDOW_HEIGHT, 0,
758 SCORE_WINDOW_BACKGROUND_COLOUR, 0);
759 GrSelectEvents(state->score_window, GR_EVENT_MASK_EXPOSURE);
760 GrMapWindow(state->score_window);
761 state->scoregcf = GrNewGC();
762 GrSetGCForeground(state->scoregcf, SCORE_WINDOW_FOREGROUND_COLOUR);
763 GrSetGCBackground(state->scoregcf, SCORE_WINDOW_BACKGROUND_COLOUR);
764 state->scoregcb = GrNewGC();
765 GrSetGCForeground(state->scoregcb, SCORE_WINDOW_BACKGROUND_COLOUR);
767 state->next_shape_window = GrNewWindow(state->main_window,
768 NEXT_SHAPE_WINDOW_X_POSITION,
769 NEXT_SHAPE_WINDOW_Y_POSITION,
770 NEXT_SHAPE_WINDOW_WIDTH,
771 NEXT_SHAPE_WINDOW_HEIGHT, 0,
772 NEXT_SHAPE_WINDOW_BACKGROUND_COLOUR, 0);
773 GrSelectEvents(state->next_shape_window, GR_EVENT_MASK_EXPOSURE);
774 GrMapWindow(state->next_shape_window);
775 state->nextshapegcf = GrNewGC();
776 state->nextshapegcb = GrNewGC();
777 GrSetGCForeground(state->nextshapegcb,
778 NEXT_SHAPE_WINDOW_BACKGROUND_COLOUR);
780 state->new_game_button = GrNewWindow(state->main_window,
781 NEW_GAME_BUTTON_X_POSITION,
782 NEW_GAME_BUTTON_Y_POSITION,
783 NEW_GAME_BUTTON_WIDTH,
784 NEW_GAME_BUTTON_HEIGHT, 0,
785 BUTTON_BACKGROUND_COLOUR, 0);
786 GrSelectEvents(state->new_game_button, GR_EVENT_MASK_EXPOSURE |
787 GR_EVENT_MASK_BUTTON_DOWN);
788 GrMapWindow(state->new_game_button);
789 state->buttongcf = GrNewGC();
790 GrSetGCForeground(state->buttongcf, BUTTON_FOREGROUND_COLOUR);
791 GrSetGCBackground(state->buttongcf, BUTTON_BACKGROUND_COLOUR);
792 state->buttongcb = GrNewGC();
793 GrSetGCForeground(state->buttongcb, BUTTON_BACKGROUND_COLOUR);
795 state->pause_continue_button = GrNewWindow(state->main_window,
796 PAUSE_CONTINUE_BUTTON_X_POSITION,
797 PAUSE_CONTINUE_BUTTON_Y_POSITION,
798 PAUSE_CONTINUE_BUTTON_WIDTH,
799 PAUSE_CONTINUE_BUTTON_HEIGHT, 0,
800 BUTTON_BACKGROUND_COLOUR, 0);
801 GrSelectEvents(state->pause_continue_button, GR_EVENT_MASK_EXPOSURE |
802 GR_EVENT_MASK_BUTTON_DOWN);
804 state->anticlockwise_button = GrNewWindow(state->main_window,
805 ANTICLOCKWISE_BUTTON_X_POSITION,
806 ANTICLOCKWISE_BUTTON_Y_POSITION,
807 ANTICLOCKWISE_BUTTON_WIDTH,
808 ANTICLOCKWISE_BUTTON_HEIGHT, 0,
809 BUTTON_BACKGROUND_COLOUR,
811 GrSelectEvents(state->anticlockwise_button, GR_EVENT_MASK_EXPOSURE |
812 GR_EVENT_MASK_BUTTON_DOWN);
814 state->clockwise_button = GrNewWindow(state->main_window,
815 CLOCKWISE_BUTTON_X_POSITION,
816 CLOCKWISE_BUTTON_Y_POSITION,
817 CLOCKWISE_BUTTON_WIDTH,
818 CLOCKWISE_BUTTON_HEIGHT, 0,
819 BUTTON_BACKGROUND_COLOUR,
821 GrSelectEvents(state->clockwise_button, GR_EVENT_MASK_EXPOSURE |
822 GR_EVENT_MASK_BUTTON_DOWN);
824 state->left_button = GrNewWindow(state->main_window,
825 LEFT_BUTTON_X_POSITION,
826 LEFT_BUTTON_Y_POSITION,
828 LEFT_BUTTON_HEIGHT, 0,
829 BUTTON_BACKGROUND_COLOUR,
831 GrSelectEvents(state->left_button, GR_EVENT_MASK_EXPOSURE |
832 GR_EVENT_MASK_BUTTON_DOWN);
834 state->right_button = GrNewWindow(state->main_window,
835 RIGHT_BUTTON_X_POSITION,
836 RIGHT_BUTTON_Y_POSITION,
838 RIGHT_BUTTON_HEIGHT, 0,
839 BUTTON_BACKGROUND_COLOUR,
841 GrSelectEvents(state->right_button, GR_EVENT_MASK_EXPOSURE |
842 GR_EVENT_MASK_BUTTON_DOWN);
844 state->drop_button = GrNewWindow(state->main_window,
845 DROP_BUTTON_X_POSITION,
846 DROP_BUTTON_Y_POSITION,
848 DROP_BUTTON_HEIGHT, 0,
849 BUTTON_BACKGROUND_COLOUR,
851 GrSelectEvents(state->drop_button, GR_EVENT_MASK_EXPOSURE |
852 GR_EVENT_MASK_BUTTON_DOWN);
854 state->well_window = GrNewWindow(state->main_window,
855 WELL_WINDOW_X_POSITION,
856 WELL_WINDOW_Y_POSITION,
858 WELL_WINDOW_HEIGHT, 0,
859 WELL_WINDOW_BACKGROUND_COLOUR, 0);
860 GrSelectEvents(state->well_window, GR_EVENT_MASK_EXPOSURE);
861 GrMapWindow(state->well_window);
862 state->wellgc = GrNewGC();
864 GrMapWindow(state->main_window);
866 state->state = STATE_STOPPED;
870 state->running_buttons_mapped = 0;
874 choose_new_shape(state);
878 static void calculate_timeout(nstate *state)
883 gettimeofday(&t, NULL);
884 u = t.tv_usec + (delays[state->level] * 1000);
885 state->timeout.tv_sec = t.tv_sec + (u / 1000000);
886 state->timeout.tv_usec = u % 1000000;
889 static unsigned long timeout_delay(nstate *state)
892 signed long s, m, ret;
894 gettimeofday(&t, NULL);
896 if((t.tv_sec > state->timeout.tv_sec) ||
897 ((t.tv_sec == state->timeout.tv_sec) &&
898 t.tv_usec >= state->timeout.tv_usec)) return 1;
900 s = state->timeout.tv_sec - t.tv_sec;
901 m = ((state->timeout.tv_usec - t.tv_usec) / 1000);
902 ret = (unsigned long)((1000 * s) + m);
904 fprintf(stderr, "t.tv_sec = %ld, t.tv_usec = %ld, timeout.tv_sec = "
905 "%ld, timeout.tv_usec = %ld, s = %ld, m = %ld, ret = %ld\n",
906 t.tv_sec, t.tv_usec, state->timeout.tv_sec,
907 state->timeout.tv_usec, s, m, ret);
909 if(ret <= 0) return 1;
913 static void do_update(nstate *state)
917 gettimeofday(&t, NULL);
919 if((t.tv_sec > state->timeout.tv_sec) ||
920 ((t.tv_sec == state->timeout.tv_sec) &&
921 (t.tv_usec >= state->timeout.tv_usec))) {
923 calculate_timeout(state);
927 static void do_pause(nstate *state)
929 draw_pause_continue_button(state);
930 while(state->state == STATE_PAUSED) {
931 GrGetNextEvent(&state->event);
934 draw_pause_continue_button(state);
937 static void wait_for_start(nstate *state)
939 draw_pause_continue_button(state);
940 while(state->state == STATE_STOPPED) {
941 GrGetNextEvent(&state->event);
944 if(state->state == STATE_NEWGAME) state->state = STATE_RUNNING;
945 draw_pause_continue_button(state);
946 calculate_timeout(state);
949 static void run_game(nstate *state)
951 while(state->state == STATE_RUNNING) {
952 GrGetNextEventTimeout(&state->event, timeout_delay(state));
954 if(state->state == STATE_PAUSED) do_pause(state);
955 if(state->state == STATE_RUNNING) do_update(state);
959 static void main_game_loop(nstate *state)
961 wait_for_start(state);
962 while(state->state != STATE_EXIT) {
963 if(state->state == STATE_RUNNING) run_game(state);
964 if(state->state == STATE_STOPPED) wait_for_start(state);
965 if(state->state != STATE_EXIT) new_game(state);
970 #define main ntetris_main
973 int main(int argc, char *argv[])
975 nstate *state = my_malloc(sizeof(nstate));
977 bzero(state, sizeof(*state));
979 main_game_loop(state);
981 write_hiscore(state);