1 | /* |
---|
2 | * $Id: main.c 1096 2006-09-23 08:19:27Z jylam $ |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or modify |
---|
5 | * it under the terms of the GNU General Public License as published by |
---|
6 | * the Free Software Foundation; either version 2 of the License, or |
---|
7 | * (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is free software; you can redistribute it and/or |
---|
10 | * modify it under the terms of the Do What The Fuck You Want To |
---|
11 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
12 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
13 | */ |
---|
14 | |
---|
15 | #include "config.h" |
---|
16 | #include "cacatris.h" |
---|
17 | |
---|
18 | int main(int argc, char *argv[]) |
---|
19 | { |
---|
20 | static caca_display_t *dp; |
---|
21 | signed int x=(FIELD_WIDTH/2)-1, y=0, rotation=0, old_x=0, old_y=0, old_rotation=0; |
---|
22 | unsigned int current_piece, next_piece, baseTime = 0; |
---|
23 | unsigned char last_has_landed = 0; |
---|
24 | unsigned char left = 0, right = 0, down = 0; |
---|
25 | unsigned long long int curTime = 0; |
---|
26 | unsigned int speed = 32; |
---|
27 | unsigned int fixed_y = 0; |
---|
28 | |
---|
29 | |
---|
30 | field = cucul_create_canvas(0, 0); |
---|
31 | infos = cucul_create_canvas(0, 0); |
---|
32 | screen = cucul_create_canvas(0, 0); |
---|
33 | |
---|
34 | dp = caca_create_display(screen); |
---|
35 | if(!dp) |
---|
36 | return 1; |
---|
37 | |
---|
38 | cucul_set_canvas_size(infos, INFO_WIDTH, cucul_get_canvas_height(screen)); |
---|
39 | cucul_set_canvas_size(field, FIELD_CANVAS_WIDTH, FIELD_CANVAS_HEIGHT); |
---|
40 | |
---|
41 | caca_set_display_time(dp, 20000); |
---|
42 | |
---|
43 | /* Our playfied */ |
---|
44 | memset(playfield, 0, FIELD_WIDTH*FIELD_HEIGHT); |
---|
45 | |
---|
46 | |
---|
47 | /* Set current and next piece to random */ |
---|
48 | current_piece = cucul_rand(0, 6); |
---|
49 | next_piece = cucul_rand(0, 6); |
---|
50 | |
---|
51 | |
---|
52 | for(;;) |
---|
53 | { |
---|
54 | caca_event_t ev; |
---|
55 | left = 0; right = 0; down = 0; |
---|
56 | |
---|
57 | printf("%llu\n", curTime); |
---|
58 | |
---|
59 | /* Handle events */ |
---|
60 | |
---|
61 | while(caca_get_event(dp, CACA_EVENT_KEY_PRESS |
---|
62 | | CACA_EVENT_QUIT, &ev, 0)) |
---|
63 | { |
---|
64 | if(ev.type == CACA_EVENT_QUIT) |
---|
65 | goto end; |
---|
66 | switch(ev.data.key.ch) |
---|
67 | { |
---|
68 | case CACA_KEY_ESCAPE: |
---|
69 | goto end; |
---|
70 | break; |
---|
71 | case CACA_KEY_UP: |
---|
72 | rotation++; |
---|
73 | rotation = rotation&0x03; |
---|
74 | break; |
---|
75 | case CACA_KEY_DOWN: |
---|
76 | down = 1; |
---|
77 | break; |
---|
78 | case CACA_KEY_LEFT: |
---|
79 | left = 1; |
---|
80 | break; |
---|
81 | case CACA_KEY_RIGHT: |
---|
82 | right = 1; |
---|
83 | break; |
---|
84 | } |
---|
85 | } |
---|
86 | |
---|
87 | if(left) |
---|
88 | { |
---|
89 | if(movable(current_piece, x-1, y, rotation)) |
---|
90 | x--; |
---|
91 | } |
---|
92 | if(right) |
---|
93 | { |
---|
94 | if(movable(current_piece, x+1, y, rotation)) |
---|
95 | x++; |
---|
96 | } |
---|
97 | |
---|
98 | if(!last_has_landed) |
---|
99 | { |
---|
100 | remove_piece(current_piece, old_x ,old_y, old_rotation); |
---|
101 | } |
---|
102 | else |
---|
103 | { |
---|
104 | last_has_landed = 0; |
---|
105 | } |
---|
106 | |
---|
107 | put_piece(current_piece, x ,y, rotation); |
---|
108 | |
---|
109 | old_x = x; |
---|
110 | old_y = y; |
---|
111 | old_rotation = rotation; |
---|
112 | |
---|
113 | fixed_y+=speed; /* Fixed point */ |
---|
114 | y = fixed_y>>8; |
---|
115 | |
---|
116 | |
---|
117 | if(y==(FIELD_HEIGHT-4)) |
---|
118 | { |
---|
119 | fixed_y = 0; |
---|
120 | x = (FIELD_WIDTH/2)-1; |
---|
121 | current_piece = next_piece; |
---|
122 | rotation = 0; |
---|
123 | next_piece = cucul_rand(0, 6); |
---|
124 | last_has_landed = 1; |
---|
125 | old_x = x; |
---|
126 | old_y = 0; |
---|
127 | } |
---|
128 | |
---|
129 | /* Populate info canvas */ |
---|
130 | infos_populate(infos); |
---|
131 | /* Draw everything on playfield */ |
---|
132 | playfield_draw(field); |
---|
133 | /* blit infos canvas into general one */ |
---|
134 | cucul_blit(screen, (cucul_get_canvas_width(screen)) - INFO_WIDTH, 0, infos, NULL); |
---|
135 | /* blit playfield canvas into general one */ |
---|
136 | cucul_blit(screen, 18, 0, field, NULL); |
---|
137 | |
---|
138 | caca_refresh_display(dp); |
---|
139 | |
---|
140 | |
---|
141 | if(!baseTime) |
---|
142 | { |
---|
143 | baseTime = caca_get_display_time(dp); |
---|
144 | } |
---|
145 | curTime+=caca_get_display_time(dp); |
---|
146 | |
---|
147 | } |
---|
148 | |
---|
149 | end: |
---|
150 | |
---|
151 | |
---|
152 | return 0; |
---|
153 | } |
---|
154 | |
---|
155 | |
---|
156 | void infos_populate(cucul_canvas_t *inf) |
---|
157 | { |
---|
158 | unsigned int i; |
---|
159 | cucul_set_color(inf, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
160 | cucul_putstr(inf, 0, 0," =Cacatris= "); |
---|
161 | cucul_putstr(inf, 0, 1," Arrows : move "); |
---|
162 | cucul_putstr(inf, 0, 2," Space : pouf "); |
---|
163 | cucul_putstr(inf, 0, 3," __________________ "); |
---|
164 | cucul_putstr(inf, 0, 4," "); |
---|
165 | cucul_putstr(inf, 0, 5," Score : NXXXX "); |
---|
166 | cucul_putstr(inf, 0, 6," Time : XX:XX "); |
---|
167 | |
---|
168 | for(i = 6; i < cucul_get_canvas_height(inf); i++) |
---|
169 | { |
---|
170 | cucul_putstr(inf, 0, i," "); |
---|
171 | } |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | void playfield_draw(cucul_canvas_t *canvas) |
---|
176 | { |
---|
177 | unsigned int x, y; |
---|
178 | float ox=0, oy=0; |
---|
179 | float incx = (float)FIELD_WIDTH / (float)FIELD_CANVAS_WIDTH; |
---|
180 | float incy = (float)FIELD_HEIGHT / (float)FIELD_CANVAS_HEIGHT; |
---|
181 | |
---|
182 | for(y = 0; y < FIELD_CANVAS_WIDTH; y++) |
---|
183 | { |
---|
184 | for(x = 0; x < FIELD_CANVAS_WIDTH; x++) |
---|
185 | { |
---|
186 | unsigned int oxi = (unsigned int) ox; |
---|
187 | unsigned int oyi = (unsigned int) oy; |
---|
188 | unsigned int c = playfield[oxi+oyi*FIELD_WIDTH]; |
---|
189 | if(c) { |
---|
190 | cucul_set_color(canvas, CUCUL_COLOR_BLACK, blocks_palette[c-1]); |
---|
191 | cucul_putchar(canvas, x, y, ' '); |
---|
192 | } else { |
---|
193 | cucul_set_color(canvas, CUCUL_COLOR_BLACK, CUCUL_COLOR_DARKGRAY); |
---|
194 | cucul_putchar(canvas, x, y, ' '); |
---|
195 | |
---|
196 | } |
---|
197 | ox+=incx; |
---|
198 | } |
---|
199 | ox = 0; |
---|
200 | oy+=incy; |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | |
---|
205 | void put_piece(unsigned int id, unsigned int x, unsigned int y, unsigned int rot) |
---|
206 | { |
---|
207 | unsigned int ix, iy; |
---|
208 | printf("rotation %d\n", rot); |
---|
209 | piece_t *p = &pieces[(id*4)+rot]; |
---|
210 | |
---|
211 | for(iy = 0; iy < p->h; iy++) |
---|
212 | for(ix = 0; ix < p->w; ix++) |
---|
213 | if(p->data[ix+iy*4]) |
---|
214 | playfield[(ix+x)+(iy+y)*FIELD_WIDTH] = p->data[ix+iy*4]*p->color; |
---|
215 | } |
---|
216 | |
---|
217 | void remove_piece(unsigned int id, unsigned int x, unsigned int y, unsigned int rot) |
---|
218 | { |
---|
219 | unsigned int ix, iy; |
---|
220 | printf("rotation %d\n", rot); |
---|
221 | piece_t *p = &pieces[(id*4)+rot]; |
---|
222 | |
---|
223 | for(iy = 0; iy < p->h; iy++) |
---|
224 | for(ix = 0; ix < p->w; ix++) |
---|
225 | if(ix<p->w && iy<p->h) |
---|
226 | if(p->data[ix+iy*4]) |
---|
227 | playfield[(ix+x)+(iy+y)*FIELD_WIDTH] = 0; |
---|
228 | } |
---|
229 | |
---|
230 | unsigned char movable(unsigned int id, int x, int y, unsigned int rot) |
---|
231 | { |
---|
232 | piece_t *p = &pieces[(id*4)+rot]; |
---|
233 | int w, h; |
---|
234 | |
---|
235 | w = p->w; |
---|
236 | h = p->h; |
---|
237 | |
---|
238 | |
---|
239 | if(x>=0 && (x+w<=FIELD_WIDTH) && y<(FIELD_HEIGHT-(signed)h)) |
---|
240 | { |
---|
241 | return 1; |
---|
242 | } |
---|
243 | |
---|
244 | return 0; |
---|
245 | } |
---|