summaryrefslogtreecommitdiff
path: root/uisimulator/tetris.c
diff options
context:
space:
mode:
authorBjörn Stenberg <bjorn@haxx.se>2002-04-19 14:12:31 +0000
committerBjörn Stenberg <bjorn@haxx.se>2002-04-19 14:12:31 +0000
commitc7dd78e90df4a22dea39369657f62ca765c08034 (patch)
tree7b1d158724e9573182067ab4ee9a93d98f1f9647 /uisimulator/tetris.c
parent70ff3d900e2270680b3f57260dd1c06693b1d6de (diff)
downloadrockbox-c7dd78e90df4a22dea39369657f62ca765c08034.tar.gz
rockbox-c7dd78e90df4a22dea39369657f62ca765c08034.zip
Tetris!
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@148 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'uisimulator/tetris.c')
-rw-r--r--uisimulator/tetris.c288
1 files changed, 288 insertions, 0 deletions
diff --git a/uisimulator/tetris.c b/uisimulator/tetris.c
new file mode 100644
index 0000000000..d49cbecccc
--- /dev/null
+++ b/uisimulator/tetris.c
@@ -0,0 +1,288 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
9 *
10 * Copyright (C) 1999 Mattis Wadman (nappe@sudac.org)
11 *
12 * Heavily modified for embedded use by Björn Stenberg (bjorn@haxx.se)
13 *
14 * All files in this archive are subject to the GNU General Public License.
15 * See the file COPYING in the source tree root for full license agreement.
16 *
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
19 *
20 ****************************************************************************/
21
22#include "types.h"
23#include "lcd.h"
24#define HAVE_RECORDER_KEYPAD
25#include "button.h"
26
27#ifdef SIMULATOR
28#include <stdio.h>
29#include <unistd.h>
30#endif
31
32int start_x = 1;
33int start_y = 1;
34int max_x = 14;
35int max_y = 24;
36int current_x = 0;
37int current_y = 0;
38int current_f = 0;
39int current_b = 0;
40int level = 0;
41short lines = 0;
42int score = 0;
43int next_b = 0;
44int next_f = 0;
45char virtual[LCD_WIDTH*LCD_HEIGHT];
46short level_speeds[10] = {1000,900,800,700,600,500,400,300,250,200};
47int blocks = 7;
48int block_frames[7] = {1,2,2,2,4,4,4};
49int block_data[7][4][2][4] =
50{
51 {
52 {{0,1,0,1},{0,0,1,1}}
53 },
54 {
55 {{0,1,1,2},{1,1,0,0}},
56 {{0,0,1,1},{0,1,1,2}}
57 },
58 {
59 {{0,1,1,2},{0,0,1,1}},
60 {{1,1,0,0},{0,1,1,2}}
61 },
62 {
63 {{1,1,1,1},{0,1,2,3}},
64 {{0,1,2,3},{2,2,2,2}}
65 },
66 {
67 {{1,1,1,2},{2,1,0,0}},
68 {{0,1,2,2},{1,1,1,2}},
69 {{0,1,1,1},{2,2,1,0}},
70 {{0,0,1,2},{0,1,1,1}}
71 },
72 {
73 {{0,1,1,1},{0,0,1,2}},
74 {{0,1,2,2},{1,1,1,0}},
75 {{1,1,1,2},{0,1,2,2}},
76 {{0,0,1,2},{2,1,1,1}}
77 },
78 {
79 {{1,0,1,2},{0,1,1,1}},
80 {{2,1,1,1},{1,0,1,2}},
81 {{1,0,1,2},{2,1,1,1}},
82 {{0,1,1,1},{1,0,1,2}}
83 }
84};
85
86/* not even pseudo random :) */
87int rand(int range)
88{
89 static int count;
90 count++;
91 return count % range;
92}
93
94void draw_frame(int fstart_x,int fstop_x,int fstart_y,int fstop_y)
95{
96 int i;
97 for (i=0; fstart_x+i-1 < fstop_x; i++)
98 {
99 lcd_drawpixel(fstart_x+i,fstart_y);
100 lcd_drawpixel(fstart_x+i,fstop_y);
101 }
102 for (i=1; fstart_y+i < fstop_y; i++)
103 {
104 lcd_drawpixel(fstart_x,fstart_y+i);
105 lcd_drawpixel(fstop_x,fstart_y+i);
106 }
107 lcd_drawpixel(fstart_x,fstart_y);
108 lcd_drawpixel(fstop_x,fstart_y);
109 lcd_drawpixel(fstart_x,fstop_y);
110 lcd_drawpixel(fstop_x,fstop_y);
111}
112
113void draw_block(int x,int y,int block,int frame,int clear)
114{
115 int i;
116 for(i=0;i < 4;i++)
117 if ( (clear ? 0 : block+1) )
118 lcd_drawpixel(start_x+x+block_data[block][frame][0][i],
119 start_y+y+block_data[block][frame][1][i]);
120 else
121 lcd_clearpixel(start_x+x+block_data[block][frame][0][i],
122 start_y+y+block_data[block][frame][1][i]);
123}
124
125void to_virtual()
126{
127 int i;
128 for(i=0;i < 4;i++)
129 *(virtual+
130 ((current_y+block_data[current_b][current_f][1][i])*max_x)+
131 (current_x+block_data[current_b][current_f][0][i])) = current_b+1;
132}
133
134int valid_position(int x,int y,int block,int frame)
135{
136 int i;
137 for(i=0;i < 4;i++)
138 if( (*(virtual+((y+block_data[block][frame][1][i])*max_x)+x+
139 block_data[block][frame][0][i]) != 0) ||
140 (x+block_data[block][frame][0][i] < 0) ||
141 (x+block_data[block][frame][0][i] > max_x-1) ||
142 (y+block_data[block][frame][1][i] < 0) ||
143 (y+block_data[block][frame][1][i] > max_y-1))
144 return FALSE;
145 return TRUE;
146}
147
148void from_virtual()
149{
150 int x,y;
151 for(y=0;y < max_y;y++)
152 for(x=0;x < max_x;x++)
153 if(*(virtual+(y*max_x)+x))
154 lcd_drawpixel(start_x+x,start_y+y);
155 else
156 lcd_clearpixel(start_x+x,start_y+y);
157}
158
159void move_block(int x,int y,int f)
160{
161 int last_frame = current_f;
162 if(f != 0)
163 {
164 current_f += f;
165 if(current_f > block_frames[current_b]-1)
166 current_f = 0;
167 if(current_f < 0)
168 current_f = block_frames[current_b]-1;
169 }
170 if(valid_position(current_x+x,current_y+y,current_b,current_f))
171 {
172 draw_block(current_x,current_y,current_b,last_frame,TRUE);
173 current_x += x;
174 current_y += y;
175 draw_block(current_x,current_y,current_b,current_f,FALSE);
176 lcd_update();
177 }
178 else
179 current_f = last_frame;
180}
181
182void new_block()
183{
184 current_b = next_b;
185 current_f = next_f;
186 current_x = (int)((max_x)/2)-1;
187 current_y = 0;
188 next_b = rand(blocks);
189 next_f = rand(block_frames[next_b]);
190 draw_block(max_x+2,start_y-1,current_b,current_f,TRUE);
191 draw_block(max_x+2,start_y-1,next_b,next_f,FALSE);
192 if(!valid_position(current_x,current_y,current_b,current_f))
193 {
194 draw_block(current_x,current_y,current_b,current_f,FALSE);
195 lcd_update();
196 }
197 else
198 draw_block(current_x,current_y,current_b,current_f,FALSE);
199}
200
201int check_lines()
202{
203 int x,y,line,i;
204 int lines = 0;
205 for(y=0;y < max_y;y++)
206 {
207 line = TRUE;
208 for(x=0;x < max_x;x++)
209 if(virtual[y*max_x+x] == 0)
210 line = FALSE;
211 if(line)
212 {
213 lines++;
214 for(i=y;i > 1;i--)
215 for (x=0;x<max_x;x++)
216 virtual[i*max_x] = virtual[((i-1)*max_x)];
217 for (x=0;x<max_x;x++)
218 virtual[max_x] = 0;
219 }
220 }
221 return lines;
222}
223
224void move_down()
225{
226 int l;
227 if(!valid_position(current_x,current_y+1,current_b,current_f))
228 {
229 to_virtual();
230 l = check_lines();
231 if(l)
232 {
233 lines += l;
234 level = (int)lines/10;
235 if(level > 9)
236 level = 9;
237 from_virtual();
238 score += l*l;
239 }
240 new_block();
241 move_block(0,0,0);
242 }
243 else
244 move_block(0,1,0);
245}
246
247void game_loop(void)
248{
249 while(1)
250 {
251 int b=0;
252 int count = 0;
253 /* while(count*20 < level_speeds[level]) */
254 {
255 b = button_get();
256 if ( b & BUTTON_LEFT ) {
257 printf("Left\n");
258 move_block(-1,0,0);
259 }
260 if ( b & BUTTON_RIGHT ) {
261 printf("Right\n");
262 move_block(1,0,0);
263 }
264 if ( b & BUTTON_UP ) {
265 printf("Up\n");
266 move_block(0,0,1);
267 }
268 if ( b & BUTTON_DOWN ) {
269 printf("Down\n");
270 move_down();
271 }
272 count++;
273 sleep(1);
274 }
275 move_down();
276 }
277}
278
279void tetris(void)
280{
281 draw_frame(start_x-1,start_x+max_x,start_y-1,start_y+max_y);
282 lcd_update();
283
284 next_b = rand(blocks);
285 next_f = rand(block_frames[next_b]);
286 new_block();
287 game_loop();
288}