OpenCores
URL https://opencores.org/ocsvn/layer2/layer2/trunk

Subversion Repositories layer2

[/] [layer2/] [trunk/] [sw/] [tennmino/] [main.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 idiolatrie
/******************************************************************************
2
 * Tennmino Version 0.1                                                       *
3
 ******************************************************************************
4
 * Copyright (C)2011  Mathias Hörtnagl <mathias.hoertnagl@gmail.com>          *
5
 *                                                                            *
6
 * This program is free software: you can redistribute it and/or modify       *
7
 * it under the terms of the GNU General Public License as published by       *
8
 * the Free Software Foundation, either version 3 of the License, or          *
9
 * (at your option) any later version.                                        *
10
 *                                                                            *
11
 * This program is distributed in the hope that it will be useful,            *
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
14
 * GNU General Public License for more details.                               *
15
 *                                                                            *
16
 * You should have received a copy of the GNU General Public License          *
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.      *
18
 ******************************************************************************/
19
#include "tiles.h"
20
#include "view.h"
21
#include "interrupt.h"
22
#include "stdlib.h"
23
 
24
static uint interval;         // Timing interval.
25
static uint score;            // Game score.
26
 
27
/* Print the score. */
28
void printScore() {
29
   gotoxy(80, 6);
30
   printf("%x", score);
31
}
32
 
33
/* Randomly select the next tile. */
34
uchar nextTile() {
35
 
36
   uchar t;
37
 
38
   while( (t = rand() & 0x7) == 7 );
39
   return t;
40
}
41
 
42
/* With each move down, check if the tile is stuck. If so, either add a new tile
43
   or if the tile could not be moved any from initial position quit the game. */
44
void checkAndMoveDown() {
45
 
46
   moveDown();
47
 
48
   switch( getGameState() ) {
49
      case GAME_NEW_TILE:
50
         score += deleteCompletedRows(BOARD_LEFT + 2, BOARD_RIGHT - BOARD_LEFT);
51
         printScore();
52
         initTile( nextTile() );
53
         break;
54
 
55
      case GAME_OVER:
56
         unset_intr(INTR_KEYB | INTR_PIT0 | INTR_EN);
57
         drawGameOver();
58
         break;
59
 
60
      default:
61
         break;
62
   }
63
}
64
 
65
/* Callback function for the timer. On every tick reset the clock and move
66
   the tile one position downwards. */
67
void clock() {
68
   pit_reset();
69
   checkAndMoveDown();
70
   pit_run(interval - score);
71
}
72
 
73
/* Callback function for the keyboard. The user can move a tile left, right,
74
   down or rotate it counter-clockwise */
75
void keyboard() {
76
   switch(getc()->chr) {
77
      case KEY_ARROWU:
78
         rotate();
79
         break;
80
 
81
      case KEY_ARROWL:
82
         moveLeft();
83
         break;
84
 
85
      case KEY_ARROWR:
86
         moveRight();
87
         break;
88
 
89
      case KEY_ARROWD:
90
         checkAndMoveDown();
91
         break;
92
 
93
      case KEY_ESC:
94
         unset_intr(INTR_KEYB | INTR_PIT0 | INTR_EN);
95
         boot();
96
 
97
      default:
98
         break;
99
   }
100
}
101
 
102
int main() {
103
 
104
   drawBoard();
105
 
106
   interval = 50000 * 800;    // Movement period.
107
   score = 0;                 // Game score reset.
108
   gotoxy(80, 5);
109
   printf0("My Score:")
110
   printScore();
111
 
112
   // Enable timer and global interrupt.
113
   set_intr(INTR_KEYB | INTR_PIT0 | INTR_EN);
114
 
115
   // Set clock() as a callback for the PIT (IRQ_PIT0).
116
   register_callback(IRQ_PIT0, &clock);
117
 
118
   // Set keyboard() as a callback for the Keyboard (IRQ_KEYB).
119
   register_callback(IRQ_KEYB, &keyboard);
120
 
121
   // Just to be sure the PIT is ready.
122
   pit_reset();
123
   pit_run(interval);
124
 
125
   // Display the first tile.
126
   initTile( nextTile() );
127
 
128
   while(TRUE);
129
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.