-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
377 lines (298 loc) · 8.45 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/*
** Copyright 1998-2008, Pete J. Jensen
** Copyright 1998-2008, Robin McNeil
** All Rights Reserved.
**
** This is UNPUBLISHED PROPRIETARY SOURCE CODE of the authors
** the contents of this file may not be disclosed to third parties, copied or
** duplicated in any form, in whole or in part, without the prior written
** permission of the authors.
**
** RESTRICTED RIGHTS LEGEND:
** Use, duplication or disclosure by the Government is subject to restrictions
** as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
** and Computer Software clause at DFARS 252.227-7013, and/or in similar or
** successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
** rights reserved under the Copyright Laws of the United States.
**
** ______
** /_____/\
** /_____\\ \ FILE: main.c
** /_____\ \\ / AUTHOR: Pete Jensen
** /_____/ \/ / / AUTHOR: Robin McNeil
** /_____/ / \//\ VERSION: 1.0.0
** \_____\//\ / /
** \_____/ / /\ /
** \_____/ \\ \
** \_____\ \\
** \_____\/
**/
#include <stdlib.h>
#include <stdio.h>
#include <dos.h>
#include <conio.h>
#include "error.h"
#include "graph.h"
#include "globals.h"
#include "board.h"
#include "sounds.h"
// #include "score.h"
/* Prototypes */
void gameLoop( void );
short int getKeyPress(void);
int handleKeyPress( short int );
void score_render( void );
void game_render( void );
void score_addToScore(unsigned long int value);
#define TEMPORARY_ANIM_DELAY 150
#define FORCED_ROTATIONS 1
enum {STATE_NORMAL, STATE_ROTATION,
STATE_GRAVITY} gameState;
enum {ROTATION_RIGHT, ROTATION_LEFT} rotationDirection;
// Rotation state variables.
short int rotationCounter;
short int direction;
// Score variables.
static unsigned long int score = 0;
static int intermediateScore; // For anim
// The number of matches completed.
int matchesCompleted;
int main(int argc, char **argv){
initializeGraphicsMode();
tile_initializeTileLevels();
board_init();
//SPLASHSCREEN
// MAIN MENU
board_render();
#ifdef __DEBUG
getch();
#endif
gameLoop();
endGraphicsMode();
return EXIT_SUCCESS;
}
void gameLoop(){
for ( ; ; ) {
switch( gameState )
{
case STATE_ROTATION:
#if FORCED_ROTATIONS
if( rotationCounter++ >= 3 )
#else
if( rotationCounter++ >= 1 )
#endif
{
sound_badSound();
rotationCounter = 0;
gameState = STATE_NORMAL;
break;
}
switch(rotationDirection)
{
case ROTATION_RIGHT:
board_rotateRight();
break;
case ROTATION_LEFT:
board_rotateLeft();
break;
}
gameState = STATE_NORMAL;
break;
case STATE_GRAVITY:
board_applyGravity();
board_dropTiles();
gameState = STATE_NORMAL;
delay(ANIM_DELAY_GRAVITY);
break;
case STATE_NORMAL:
game_render();
//score_displayScore();
if( board_hasEmpty() )
{
gameState = STATE_GRAVITY;
break;
}
if(rotationCounter < 3 && rotationCounter > 0)
delay(ANIM_DELAY_ROTATION);
if( matchesCompleted = processMatches() )
{
/* NOTE: If we had a real animation engine, we wouldn't need this */
game_render();
delay(ANIM_DELAY_GRAVITY);
rotationCounter = 0;
sound_marioSound();
gameState = STATE_GRAVITY;
break;
}
if(rotationCounter)
gameState = STATE_ROTATION;
else
if( !handleKeyPress( getKeyPress() ) ) return;
}
}
return;
}
/* ***************************************************************************
* Function: getKeyPress
* Purpose : Wait for the player to press a key; only then do something; if we
* did not do this the game would go flickering when we were nooping
*
* Inputs : Void
* Returns : The (int) key value that the player pressed.
* Sample Call : ch = getKeyPress();
*/
short int getKeyPress()
{
register int keyPressed;
/* 1) Check if a key has been pressed. */
/* 2) Store the key that was indeed pressed. */
for(;;) {
if ( kbhit() ) {
// Store the key pressed.
keyPressed = getch();
// Return the key pressed back to the calling function.
return keyPressed;
}
else {
// NOTE: placing a break here modifies the behavior of user
// input and the game significantly. Instead of waiting,
// eg (continue) for a keypress, it will continue on with
// the rest of the program.
//break;
continue;
}
}
return -1;
}
/* ***************************************************************************
* Function: handleKeyPress
* Purpose : Designed to work in conjunction with getKeyPress, except this
* fuction handles the dissemination of tasks; to map to pressed
* keys on the keyboard.
*
* Inputs : int inkey - the pressed key.
* Returns : void
* Sample Call : handleKeyPress( getKeyPress() ); //Waits for key, pass int.
*/
int handleKeyPress(short int inkey)
{
unsigned char done; // Are we done yet?
switch ( inkey )
{
/* Handle directional keys. */
case UP_ARROW_KEY: board_cursorMoveUp(); break;
case DOWN_ARROW_KEY: board_cursorMoveDown(); break;
case RIGHT_ARROW_KEY: board_cursorMoveRight(); break;
case LEFT_ARROW_KEY: board_cursorMoveLeft(); break;
/* Handle function and escape keys. */
case F1_KEY: break;
case ESC_KEY: return 0;
case 'r':
board_init();
break;
case 'g':
(void) board_applyGravity();
break;
case ROTATE_LEFT_KEY:
rotationDirection = ROTATION_LEFT;
gameState = STATE_ROTATION;
break;
case ROTATE_RIGHT_KEY:
rotationDirection = ROTATION_RIGHT;
gameState = STATE_ROTATION;
break;
}
return 1;
}
/* ***************************************************************************
* Function: processMatches
* Purpose:
*/
int processMatches()
{
MATCH matchStructure = {0};
int counter = 0;
while( board_checkPatterns( &matchStructure ) )
{
int scoreBase = 5;
enum {
SCORE_TYPE_THREE = 3,
SCORE_TYPE_FOUR = 4,
SCORE_TYPE_SIX = 6,
} scoreType;
int numStars = 0;
int superStar = 0;
int scoreValue = 0;
counter++;
switch( matchStructure.matchType )
{
case MATCH_TYPE_THREE:
if (matchStructure.match.three_match.p1.star)
numStars++;
if (matchStructure.match.three_match.p2.star)
numStars++;
if (matchStructure.match.three_match.p3.star)
numStars++;
scoreType = SCORE_TYPE_THREE;
if (matchStructure.type == TILE_SUPERSTAR)
superStar = 1;
break;
case MATCH_TYPE_FOUR:
if (matchStructure.match.four_match.p1.star)
numStars++;
if (matchStructure.match.four_match.p2.star)
numStars++;
if (matchStructure.match.four_match.p3.star)
numStars++;
if (matchStructure.match.four_match.p4.star)
numStars++;
scoreType = SCORE_TYPE_FOUR;
if( matchStructure.type == TILE_SUPERSTAR )
superStar = 1;
break;
case MATCH_TYPE_SIX:
// Actually we don't kill the center tile; we only change it.
// TODO: board_makeStar(POS)
board_makeStar(matchStructure.match.six_match.center.x,
matchStructure.match.six_match.center.y);
scoreType = SCORE_TYPE_SIX;
if( matchStructure.type == TILE_SUPERSTAR ){
// TODO: board_makeBlackDiamond( @ center)
superStar = 1;
}
break;
}
scoreValue = scoreBase * scoreType * (numStars ? numStars + 1 : 1) * (superStar ? 100 : 1);
score_addToScore(scoreValue);
board_killMatch(&matchStructure);
if( numStars >= 3){
//sound_explosion(numStars);
//sound_nested(numStars, scoreValue);
//sound_fart(numStars);
//delay(300);
sound_xplo(numStars);
board_explodeMatch(&matchStructure);
}
// Grrrrrr ... This could/should be rearranged and refactored.
//score_Score( matchStructure );
//score_processScore();
}
return counter;
}
void game_render()
{
board_render();
score_render();
}
void score_addToScore(unsigned long int value)
{
// Okay for now.
score += value;
}
void score_render(){
gotoxy(SCORE_DISPLAY_X_COORD, SCORE_DISPLAY_Y_COORD);
printf("%u", score);
gotoxy(SCORE_DISPLAY_X_COORD, SCORE_DISPLAY_Y_COORD + 1);
printf("%i ", matchesCompleted);
return;
}