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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [services/] [gfx/] [mw/] [v2_0/] [src/] [demos/] [nanox/] [ntetris.h] - Blame information for rev 174

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
#ifndef NTETRIS_H
2
#define NTETRIS_H
3
 
4
/*
5
 * The contents of this file are subject to the Mozilla Public License
6
 * Version 1.1 (the "License"); you may not use this file except in
7
 * compliance with the License. You may obtain a copy of the License at
8
 * http://www.mozilla.org/MPL/
9
 *
10
 * Software distributed under the License is distributed on an "AS IS"
11
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
12
 * License for the specific language governing rights and limitations
13
 * under the License.
14
 *
15
 * The Original Code is NanoTetris.
16
 *
17
 * The Initial Developer of the Original Code is Alex Holden.
18
 * Portions created by Alex Holden are Copyright (C) 2000
19
 * Alex Holden <alex@linuxhacker.org>. All Rights Reserved.
20
 *
21
 * Contributor(s):
22
 *
23
 * Alternatively, the contents of this file may be used under the terms
24
 * of the GNU General Public license (the  "[GNU] License"), in which case the
25
 * provisions of [GNU] License are applicable instead of those
26
 * above.  If you wish to allow use of your version of this file only
27
 * under the terms of the [GNU] License and not to allow others to use
28
 * your version of this file under the MPL, indicate your decision by
29
 * deleting  the provisions above and replace  them with the notice and
30
 * other provisions required by the [GNU] License.  If you do not delete
31
 * the provisions above, a recipient may use your version of this file
32
 * under either the MPL or the [GNU] License.
33
 */
34
 
35
/*
36
 * Anything which is configurable should be in this file.
37
 * Unfortunately I haven't really bothered to comment anything except for the
38
 * array of shape descriptions (you can add your own new shapes quite easily).
39
 */
40
 
41
#ifndef __ECOS
42
#define USE_HISCORE_FILE
43
#define HISCORE_FILE "/usr/games/nanotetris.hiscore"
44
#endif
45
#undef HAVE_USLEEP
46
#define BLOCK_SIZE 10
47
#define BORDER_WIDTH 10
48
#define CONTROL_BAR_WIDTH 90
49
#define BUTTON_HEIGHT 20
50
#define BUTTON_WIDTH (CONTROL_BAR_WIDTH - BORDER_WIDTH)
51
#define BUTTON_BACKGROUND_COLOUR LTGRAY
52
#define BUTTON_FOREGROUND_COLOUR BLACK
53
#define MOVEMENT_BUTTON_WIDTH ((BUTTON_WIDTH / 2) - 1)
54
#define TEXT_X_POSITION 5
55
#define TEXT_Y_POSITION 15
56
#define TEXT2_Y_POSITION 30
57
#define WELL_WIDTH 12
58
#define WELL_HEIGHT 28
59
#define WELL_VISIBLE_HEIGHT 24
60
#define WELL_NOTVISIBLE (WELL_HEIGHT - WELL_VISIBLE_HEIGHT)
61
#define LEVEL_DIVISOR 500
62
#ifdef __ECOS
63
#define DROP_BLOCK_DELAY 10
64
#else
65
#define DROP_BLOCK_DELAY 25
66
#endif
67
#define DELETE_LINE_DELAY 300
68
#define SCORE_INCREMENT 100
69
 
70
#define MAIN_WINDOW_X_POSITION 0
71
#define MAIN_WINDOW_Y_POSITION 0
72
#define MAIN_WINDOW_WIDTH (CONTROL_BAR_WIDTH + (2 * BORDER_WIDTH) + \
73
                                        (WELL_WIDTH * BLOCK_SIZE))
74
#define MAIN_WINDOW_HEIGHT ((2 * BORDER_WIDTH) + \
75
                                        (WELL_VISIBLE_HEIGHT * BLOCK_SIZE))
76
#define MAIN_WINDOW_BACKGROUND_COLOUR BLUE
77
 
78
#define SCORE_WINDOW_WIDTH BUTTON_WIDTH
79
#define SCORE_WINDOW_HEIGHT 35
80
#define SCORE_WINDOW_X_POSITION BORDER_WIDTH
81
#define SCORE_WINDOW_Y_POSITION BORDER_WIDTH
82
#define SCORE_WINDOW_BACKGROUND_COLOUR BLACK
83
#define SCORE_WINDOW_FOREGROUND_COLOUR LTGREEN
84
 
85
#define NEXT_SHAPE_WINDOW_SIZE 6
86
#define NEXT_SHAPE_WINDOW_WIDTH (NEXT_SHAPE_WINDOW_SIZE * BLOCK_SIZE)
87
#define NEXT_SHAPE_WINDOW_HEIGHT (NEXT_SHAPE_WINDOW_SIZE * BLOCK_SIZE)
88
#define NEXT_SHAPE_WINDOW_X_POSITION (BORDER_WIDTH + 10)
89
#define NEXT_SHAPE_WINDOW_Y_POSITION ((2 * BORDER_WIDTH) + SCORE_WINDOW_HEIGHT)
90
#define NEXT_SHAPE_WINDOW_BACKGROUND_COLOUR BLACK
91
 
92
#define NEW_GAME_BUTTON_WIDTH BUTTON_WIDTH
93
#define NEW_GAME_BUTTON_HEIGHT BUTTON_HEIGHT
94
#define NEW_GAME_BUTTON_X_POSITION BORDER_WIDTH
95
#define NEW_GAME_BUTTON_Y_POSITION ((3 * BORDER_WIDTH) + SCORE_WINDOW_HEIGHT \
96
                                                + NEXT_SHAPE_WINDOW_HEIGHT)
97
 
98
#define PAUSE_CONTINUE_BUTTON_WIDTH BUTTON_WIDTH
99
#define PAUSE_CONTINUE_BUTTON_HEIGHT BUTTON_HEIGHT
100
#define PAUSE_CONTINUE_BUTTON_X_POSITION BORDER_WIDTH
101
#define PAUSE_CONTINUE_BUTTON_Y_POSITION ((4 * BORDER_WIDTH) + \
102
                        SCORE_WINDOW_HEIGHT + NEXT_SHAPE_WINDOW_HEIGHT + \
103
                                                NEW_GAME_BUTTON_HEIGHT)
104
 
105
#define ANTICLOCKWISE_BUTTON_WIDTH MOVEMENT_BUTTON_WIDTH
106
#define ANTICLOCKWISE_BUTTON_HEIGHT BUTTON_HEIGHT
107
#define ANTICLOCKWISE_BUTTON_X_POSITION BORDER_WIDTH
108
#define ANTICLOCKWISE_BUTTON_Y_POSITION ((5 * BORDER_WIDTH) + \
109
                        SCORE_WINDOW_HEIGHT + NEXT_SHAPE_WINDOW_HEIGHT + \
110
                        NEW_GAME_BUTTON_HEIGHT + PAUSE_CONTINUE_BUTTON_HEIGHT)
111
 
112
#define CLOCKWISE_BUTTON_WIDTH MOVEMENT_BUTTON_WIDTH
113
#define CLOCKWISE_BUTTON_HEIGHT BUTTON_HEIGHT
114
#define CLOCKWISE_BUTTON_X_POSITION (ANTICLOCKWISE_BUTTON_X_POSITION + \
115
                                        ANTICLOCKWISE_BUTTON_WIDTH + 2)
116
#define CLOCKWISE_BUTTON_Y_POSITION ANTICLOCKWISE_BUTTON_Y_POSITION
117
 
118
#define LEFT_BUTTON_WIDTH MOVEMENT_BUTTON_WIDTH
119
#define LEFT_BUTTON_HEIGHT BUTTON_HEIGHT
120
#define LEFT_BUTTON_X_POSITION BORDER_WIDTH
121
#define LEFT_BUTTON_Y_POSITION (ANTICLOCKWISE_BUTTON_Y_POSITION + \
122
                                ANTICLOCKWISE_BUTTON_HEIGHT + 2)
123
 
124
#define RIGHT_BUTTON_WIDTH MOVEMENT_BUTTON_WIDTH
125
#define RIGHT_BUTTON_HEIGHT BUTTON_HEIGHT
126
#define RIGHT_BUTTON_X_POSITION (LEFT_BUTTON_X_POSITION + LEFT_BUTTON_WIDTH + 2)
127
#define RIGHT_BUTTON_Y_POSITION LEFT_BUTTON_Y_POSITION
128
 
129
#define DROP_BUTTON_WIDTH BUTTON_WIDTH
130
#define DROP_BUTTON_HEIGHT BUTTON_HEIGHT
131
#define DROP_BUTTON_X_POSITION BORDER_WIDTH
132
#define DROP_BUTTON_Y_POSITION (LEFT_BUTTON_Y_POSITION + LEFT_BUTTON_HEIGHT + 2)
133
 
134
#define WELL_WINDOW_WIDTH (BLOCK_SIZE * WELL_WIDTH)
135
#define WELL_WINDOW_HEIGHT (BLOCK_SIZE * WELL_VISIBLE_HEIGHT)
136
#define WELL_WINDOW_X_POSITION (CONTROL_BAR_WIDTH + BORDER_WIDTH)
137
#define WELL_WINDOW_Y_POSITION BORDER_WIDTH
138
#define WELL_WINDOW_BACKGROUND_COLOUR BLACK
139
 
140
enum {
141
        STATE_STOPPED,
142
        STATE_RUNNING,
143
        STATE_PAUSED,
144
        STATE_NEWGAME,
145
        STATE_EXIT,
146
        STATE_UNKNOWN
147
};
148
 
149
typedef GR_COLOR block;
150
 
151
struct ntetris_shape {
152
        int type;
153
        int orientation;
154
        GR_COLOR colour;
155
        int x;
156
        int y;
157
};
158
typedef struct ntetris_shape shape;
159
 
160
struct ntetris_state {
161
        block blocks[2][WELL_HEIGHT][WELL_WIDTH];
162
        int score;
163
        int hiscore;
164
        int fhiscore;
165
        int level;
166
        int state;
167
        int running_buttons_mapped;
168
        shape current_shape;
169
        shape next_shape;
170
        GR_WINDOW_ID main_window;
171
        GR_WINDOW_ID score_window;
172
        GR_WINDOW_ID next_shape_window;
173
        GR_WINDOW_ID new_game_button;
174
        GR_WINDOW_ID pause_continue_button;
175
        GR_WINDOW_ID anticlockwise_button;
176
        GR_WINDOW_ID clockwise_button;
177
        GR_WINDOW_ID left_button;
178
        GR_WINDOW_ID right_button;
179
        GR_WINDOW_ID drop_button;
180
        GR_WINDOW_ID well_window;
181
        GR_GC_ID scoregcf;
182
        GR_GC_ID scoregcb;
183
        GR_GC_ID nextshapegcf;
184
        GR_GC_ID nextshapegcb;
185
        GR_GC_ID buttongcf;
186
        GR_GC_ID buttongcb;
187
        GR_GC_ID wellgc;
188
        GR_EVENT event;
189
        struct timeval timeout;
190
};
191
typedef struct ntetris_state nstate;
192
 
193
void *my_malloc(size_t size);
194
void msleep(long ms);
195
void read_hiscore(nstate *state);
196
void write_hiscore(nstate *state);
197
int will_collide(nstate *state, int x, int y, int orientation);
198
void draw_shape(nstate *state, GR_COORD x, GR_COORD y, int erase);
199
void draw_well(nstate *state, int forcedraw);
200
void draw_score(nstate *state);
201
void draw_next_shape(nstate *state);
202
void draw_new_game_button(nstate *state);
203
void draw_anticlockwise_button(nstate *state);
204
void draw_clockwise_button(nstate *state);
205
void draw_left_button(nstate *state);
206
void draw_right_button(nstate *state);
207
void draw_drop_button(nstate *state);
208
void draw_pause_continue_button(nstate *state);
209
int block_is_all_in_well(nstate *state);
210
void delete_line(nstate *state, int line);
211
void block_reached_bottom(nstate *state);
212
void move_block(nstate *state, int direction);
213
void rotate_block(nstate *state, int direction);
214
void drop_block(nstate *state);
215
void handle_exposure_event(nstate *state);
216
void handle_mouse_event(nstate *state);
217
void handle_keyboard_event(nstate *state);
218
void handle_event(nstate *state);
219
void clear_well(nstate *state);
220
int random8(int limit);
221
void choose_new_shape(nstate *state);
222
void new_game(nstate *state);
223
void init_game(nstate *state);
224
void calculate_timeout(nstate *state);
225
unsigned long timeout_delay(nstate *state);
226
void do_update(nstate *state);
227
void do_pause(nstate *state);
228
void wait_for_start(nstate *state);
229
void run_game(nstate *state);
230
void main_game_loop(nstate *state);
231
 
232
#define LEVELS 12
233
static const int delays[] = {600, 550, 500, 450, 400, 350,
234
                                300, 250, 200, 150, 100, 50};
235
#define MAX_BLOCK_COLOUR 3
236
static const GR_COLOR block_colours[] = {
237
        LTRED, LTGREEN, LTBLUE, YELLOW
238
};
239
 
240
/*
241
 * This isn't the most space efficient way of storing the shape patterns, but
242
 * it's quick and easy to parse, and only takes a few hundred bytes of code
243
 * space anyway. 0 means "not filled", 1 means "filled", 2 means "go to next
244
 * line", and 3 means "end of pattern". There are four patterns for each shape;
245
 * one for each orientation. Adding new shapes is quite easy- just increase
246
 * MAXSHAPES by the number of shapes you add, then add the four pattern
247
 * descriptions (each one should be the previous one rotated clockwise) for
248
 * each shape. If you need to use more than MAXROWS rows (including the "end
249
 * of line" marker), increase MAXROWS. Ditto for MAXCOLS. Increasing
250
 * MAXORIENTATIONS (to get more than four orientations) may work, but it's
251
 * untested, and remember that if you increase it you'll have to add the new
252
 * orientations for all of the original shapes as well as your new ones. Also
253
 * remember to add the new shapes the the array of shape sizes below.
254
 */
255
#define MAXSHAPES 7
256
#define MAXORIENTATIONS 4
257
#define MAXCOLS 5
258
#define MAXROWS 4
259
static const char shapes[MAXSHAPES][MAXORIENTATIONS][MAXROWS][MAXCOLS] = {
260
        { /* **** */
261
                {
262
                        {1,1,1,1,3}
263
                },
264
                {
265
                        {1,2},
266
                        {1,2},
267
                        {1,2},
268
                        {1,3}
269
                },
270
                {
271
                        {1,1,1,1,3}
272
                },
273
                {
274
                        {1,2},
275
                        {1,2},
276
                        {1,2},
277
                        {1,3}
278
                }
279
        },
280
        { /*    *
281
              ***  */
282
                {
283
                        {0,0,1,2},
284
                        {1,1,1,3}
285
                },
286
                {
287
                        {1,2},
288
                        {1,2},
289
                        {1,1,3}
290
                },
291
                {
292
                        {1,1,1,2},
293
                        {1,3}
294
                },
295
                {
296
                        {1,1,2},
297
                        {0,1,2},
298
                        {0,1,3}
299
                }
300
        },
301
        { /*  *
302
              ***  */
303
                {
304
                        {1,2},
305
                        {1,1,1,3}
306
                },
307
                {
308
                        {1,1,2},
309
                        {1,2},
310
                        {1,3}
311
                },
312
                {
313
                        {1,1,1,2},
314
                        {0,0,1,3}
315
                },
316
                {
317
                        {0,1,2},
318
                        {0,1,2},
319
                        {1,1,3}
320
                }
321
        },
322
        { /*  **
323
               **  */
324
                {
325
                        {1,1,2},
326
                        {0,1,1,3}
327
                },
328
                {
329
                        {0,1,2},
330
                        {1,1,2},
331
                        {1,3}
332
                },
333
                {
334
                        {1,1,2},
335
                        {0,1,1,3}
336
                },
337
                {
338
                        {0,1,2},
339
                        {1,1,2},
340
                        {1,3}
341
                }
342
        },
343
        { /*   **
344
              **   */
345
                {
346
                        {0,1,1,2},
347
                        {1,1,3}
348
                },
349
                {
350
                        {1,2},
351
                        {1,1,2},
352
                        {0,1,3}
353
                },
354
                {
355
                        {0,1,1,2},
356
                        {1,1,3}
357
                },
358
                {
359
                        {1,2},
360
                        {1,1,2},
361
                        {0,1,3}
362
                }
363
        },
364
        { /*  **
365
              **  */
366
                {
367
                        {1,1,2},
368
                        {1,1,3}
369
                },
370
                {
371
                        {1,1,2},
372
                        {1,1,3}
373
                },
374
                {
375
                        {1,1,2},
376
                        {1,1,3}
377
                },
378
                {
379
                        {1,1,2},
380
                        {1,1,3}
381
                }
382
        },
383
        { /*   *
384
              ***  */
385
                {
386
                        {0,1,2},
387
                        {1,1,1,3}
388
                },
389
                {
390
                        {1,2},
391
                        {1,1,2},
392
                        {1,3}
393
                },
394
                {
395
                        {1,1,1,2},
396
                        {0,1,3}
397
                },
398
                {
399
                        {0,1,2},
400
                        {1,1,2},
401
                        {0,1,3}
402
                }
403
        }
404
};
405
static const unsigned char shape_sizes[MAXSHAPES][MAXORIENTATIONS][2] = {
406
        {{4,1},{1,4},{4,1},{1,4}},
407
        {{3,2},{2,3},{3,2},{2,3}},
408
        {{3,2},{2,3},{3,2},{2,3}},
409
        {{3,2},{2,3},{3,2},{2,3}},
410
        {{3,2},{2,3},{3,2},{2,3}},
411
        {{2,2},{2,2},{2,2},{2,2}},
412
        {{3,2},{2,3},{3,2},{2,3}}
413
};
414
#endif

powered by: WebSVN 2.1.0

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