diff options
author | Björn Stenberg <bjorn@haxx.se> | 2002-04-19 14:12:31 +0000 |
---|---|---|
committer | Björn Stenberg <bjorn@haxx.se> | 2002-04-19 14:12:31 +0000 |
commit | c7dd78e90df4a22dea39369657f62ca765c08034 (patch) | |
tree | 7b1d158724e9573182067ab4ee9a93d98f1f9647 /uisimulator | |
parent | 70ff3d900e2270680b3f57260dd1c06693b1d6de (diff) | |
download | rockbox-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')
-rw-r--r-- | uisimulator/Makefile | 12 | ||||
-rw-r--r-- | uisimulator/button-x11.c | 77 | ||||
-rw-r--r-- | uisimulator/tetris.c | 288 | ||||
-rw-r--r-- | uisimulator/uibasic.c | 15 |
4 files changed, 375 insertions, 17 deletions
diff --git a/uisimulator/Makefile b/uisimulator/Makefile index a8f8db2d47..8f7836ca04 100644 --- a/uisimulator/Makefile +++ b/uisimulator/Makefile | |||
@@ -27,10 +27,16 @@ RM = rm | |||
27 | CFLAGS = -g | 27 | CFLAGS = -g |
28 | CPPFLAGS = -DHAVE_CONFIG_H -DGETTIMEOFDAY_TWO_ARGS -DSIMULATOR -DHAVE_LCD_BITMAP -I$(FIRMWAREDIR) | 28 | CPPFLAGS = -DHAVE_CONFIG_H -DGETTIMEOFDAY_TWO_ARGS -DSIMULATOR -DHAVE_LCD_BITMAP -I$(FIRMWAREDIR) |
29 | LDFLAGS = -lX11 -lm -lXt -lXmu -lnsl | 29 | LDFLAGS = -lX11 -lm -lXt -lXmu -lnsl |
30 | INCLUDES = | 30 | UNAME := $(shell uname) |
31 | LIBDIRS = | 31 | ifeq ($(UNAME),Linux) |
32 | INCLUDES = -I/usr/X11R6/include | ||
33 | LIBDIRS = -L/usr/X11R6/lib | ||
34 | else | ||
35 | INCLUDES = | ||
36 | LIBDIRS = | ||
37 | endif | ||
32 | 38 | ||
33 | OBJS= screenhack.o uibasic.o resources.o visual.o lcd.o lcd-x11.o chartables.o | 39 | OBJS= screenhack.o uibasic.o resources.o visual.o lcd.o lcd-x11.o button-x11.o chartables.o tetris.o |
34 | 40 | ||
35 | SRCS = $(OBJS:%.o=%.c) | 41 | SRCS = $(OBJS:%.o=%.c) |
36 | HDRS = $(OBJS:%.o=%.h) | 42 | HDRS = $(OBJS:%.o=%.h) |
diff --git a/uisimulator/button-x11.c b/uisimulator/button-x11.c new file mode 100644 index 0000000000..92b054f265 --- /dev/null +++ b/uisimulator/button-x11.c | |||
@@ -0,0 +1,77 @@ | |||
1 | /*************************************************************************** | ||
2 | * __________ __ ___. | ||
3 | * Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
4 | * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
5 | * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
6 | * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
7 | * \/ \/ \/ \/ \/ | ||
8 | * $Id$ | ||
9 | * | ||
10 | * Copyright (C) 2002 by Björn Stenberg | ||
11 | * | ||
12 | * All files in this archive are subject to the GNU General Public License. | ||
13 | * See the file COPYING in the source tree root for full license agreement. | ||
14 | * | ||
15 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY | ||
16 | * KIND, either express or implied. | ||
17 | * | ||
18 | ****************************************************************************/ | ||
19 | #define HAVE_RECORDER_KEYPAD | ||
20 | #include "types.h" | ||
21 | #include "button.h" | ||
22 | |||
23 | #include "X11/keysym.h" | ||
24 | |||
25 | /* | ||
26 | *Initialize buttons | ||
27 | */ | ||
28 | void button_init() | ||
29 | { | ||
30 | } | ||
31 | |||
32 | /* | ||
33 | * Get button pressed from hardware | ||
34 | */ | ||
35 | static int get_raw_button (void) | ||
36 | { | ||
37 | int k = screenhack_handle_events(); | ||
38 | switch(k) | ||
39 | { | ||
40 | case XK_KP_Left: | ||
41 | case XK_Left: | ||
42 | case XK_KP_4: | ||
43 | return BUTTON_LEFT; | ||
44 | case XK_KP_Right: | ||
45 | case XK_Right: | ||
46 | case XK_KP_6: | ||
47 | return BUTTON_RIGHT; | ||
48 | case XK_KP_Up: | ||
49 | case XK_Up: | ||
50 | case XK_KP_8: | ||
51 | return BUTTON_UP; | ||
52 | case XK_KP_Down: | ||
53 | case XK_Down: | ||
54 | case XK_KP_2: | ||
55 | return BUTTON_DOWN; | ||
56 | default: | ||
57 | return 0; | ||
58 | } | ||
59 | } | ||
60 | |||
61 | /* | ||
62 | * Get the currently pressed button. | ||
63 | * Returns one of BUTTON_xxx codes, with possibly a modifier bit set. | ||
64 | * No modifier bits are set when the button is first pressed. | ||
65 | * BUTTON_HELD bit is while the button is being held. | ||
66 | * BUTTON_REL bit is set when button has been released. | ||
67 | */ | ||
68 | int button_get(void) | ||
69 | { | ||
70 | return get_raw_button(); | ||
71 | } | ||
72 | |||
73 | /* ----------------------------------------------------------------- | ||
74 | * local variables: | ||
75 | * eval: (load-file "rockbox-mode.el") | ||
76 | * end: | ||
77 | */ | ||
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 | |||
32 | int start_x = 1; | ||
33 | int start_y = 1; | ||
34 | int max_x = 14; | ||
35 | int max_y = 24; | ||
36 | int current_x = 0; | ||
37 | int current_y = 0; | ||
38 | int current_f = 0; | ||
39 | int current_b = 0; | ||
40 | int level = 0; | ||
41 | short lines = 0; | ||
42 | int score = 0; | ||
43 | int next_b = 0; | ||
44 | int next_f = 0; | ||
45 | char virtual[LCD_WIDTH*LCD_HEIGHT]; | ||
46 | short level_speeds[10] = {1000,900,800,700,600,500,400,300,250,200}; | ||
47 | int blocks = 7; | ||
48 | int block_frames[7] = {1,2,2,2,4,4,4}; | ||
49 | int 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 :) */ | ||
87 | int rand(int range) | ||
88 | { | ||
89 | static int count; | ||
90 | count++; | ||
91 | return count % range; | ||
92 | } | ||
93 | |||
94 | void 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 | |||
113 | void 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 | |||
125 | void 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 | |||
134 | int 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 | |||
148 | void 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 | |||
159 | void 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 | |||
182 | void 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 | |||
201 | int 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 | |||
224 | void 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 | |||
247 | void 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 | |||
279 | void 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 | } | ||
diff --git a/uisimulator/uibasic.c b/uisimulator/uibasic.c index 35b4b2d0f2..f1bb42f202 100644 --- a/uisimulator/uibasic.c +++ b/uisimulator/uibasic.c | |||
@@ -223,20 +223,7 @@ screenhack (Display *the_dpy, Window the_window) | |||
223 | 223 | ||
224 | Logf("Rockbox will kill ya!"); | 224 | Logf("Rockbox will kill ya!"); |
225 | 225 | ||
226 | lcd_position(0, 6, 0); | 226 | tetris(); |
227 | lcd_string( "Rock the box"); | ||
228 | |||
229 | lcd_position(8, 24, 0); | ||
230 | lcd_string( "Roolz" ); | ||
231 | |||
232 | lcd_update(); | ||
233 | |||
234 | while (1) { | ||
235 | /* deal with input here */ | ||
236 | |||
237 | XSync (dpy, False); | ||
238 | screenhack_handle_events (dpy); | ||
239 | } | ||
240 | } | 227 | } |
241 | 228 | ||
242 | void screen_redraw() | 229 | void screen_redraw() |