3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <libx/base.h>
25 #include <libx/object.h>
26 #include <libx/image.h>
27 #include <libx/cursor.h>
28 #include <libx/text.h>
38 unsigned short *img_pacman
;
39 unsigned short *img_wall
;
40 unsigned short *img_beast
;
53 unsigned char level_num
;
61 beast_t beasts
[MAXBEASTS
];
62 unsigned char beasts_count
;
66 unsigned short *init_bitmap (char *str
)
69 unsigned short *bitmap
= (unsigned short *) malloc (2120);
74 memset (bitmap
, 0, 2048+70);
77 int fd
= open (str
, O_RDONLY
);
80 puts ("error -> file not found\n");
84 if (!read (fd
, (unsigned char *) bitmap
, 2048+70)) {
85 puts ("error -> something was wrong !\n");
96 level_data
= (char *) malloc (26 * 18 + 1);
101 memset (level_data
, 0, 26 * 18);
103 int fd
= open ("level", O_RDONLY
);
106 puts ("error -> file 'level' not found\n");
110 if (!read (fd
, (char *) level_data
, 26 * 18)) {
111 puts ("error -> something was wrong !\n");
117 img_wall
= init_bitmap ("imgwall");
130 img_pacman
= init_bitmap ("imgpac");
148 img_beast
= init_bitmap ("imgobl");
155 pacman_maxpoints
= 0;
161 for (x
= 0; x
< 25; x
++) {
162 for (y
= 0; y
< 18; y
++) {
163 if (level_data
[x
+ y
* 26] == '3') {
164 if (i
>= beasts_count
)
167 beasts
[i
].x
= x
* 32;
168 beasts
[i
].y
= y
* 32;
173 if (level_data
[x
+ y
* 26] == '0')
186 /* Switch to VGA graphics mode */
198 void draw_bitmap (unsigned short *bitmap
, unsigned short x
, unsigned short y
)
205 for (i
= 0; i
< 32*32; i
++) {
216 if (bitmap
[pix
] != (unsigned short) 0)
217 if (k
<= 32 && j
<= 32)
218 xpixel ((unsigned) x
+k
, (unsigned) y
+j
, bitmap
[pix
]);
225 void draw_field (unsigned char x
, unsigned char y
)
227 draw_bitmap (img_wall
, x
* 32, y
* 32);
230 void draw_point (unsigned char x
, unsigned char y
)
232 xline (x
* 32 + 11, y
* 32 + 10, x
* 32 + 20, y
* 32 + 10, ~0);
233 xrectfill (x
* 32 + 10, y
* 32 + 11, x
* 32 + 21, y
* 32 + 20, ~0);
234 xline (x
* 32 + 11, y
* 32 + 21, x
* 32 + 20, y
* 32 + 21, ~0);
240 for (i
= 0; i
< beasts_count
; i
++)
241 draw_bitmap (img_beast
, beasts
[i
].x
, beasts
[i
].y
);
249 for (x
= 0; x
< 25; x
++) {
250 for (y
= 0; y
< 18; y
++) {
251 if (level_data
[x
+ y
* 26] == '1')
254 if (level_data
[x
+ y
* 26] == '0')
262 /* Draw image to vga buffer */
263 draw_bitmap (img_pacman
, pacman_x
, pacman_y
);
265 if (pacman_points
% 2) {
266 if (pacman_rot
== 0) {
267 xrectfill (pacman_x
, pacman_y
+11, pacman_x
+3, pacman_y
+20, 0);
268 xrectfill (pacman_x
+4, pacman_y
+13, pacman_x
+7, pacman_y
+18, 0);
269 xrectfill (pacman_x
+8, pacman_y
+15, pacman_x
+11, pacman_y
+16, 0);
270 } else if (pacman_rot
== 1) {
271 xrectfill (pacman_x
+29, pacman_y
+11, pacman_x
+32, pacman_y
+20, 0);
272 xrectfill (pacman_x
+25, pacman_y
+13, pacman_x
+28, pacman_y
+18, 0);
273 xrectfill (pacman_x
+21, pacman_y
+15, pacman_x
+24, pacman_y
+16, 0);
274 } else if (pacman_rot
== 2) {
275 xrectfill (pacman_x
+11, pacman_y
, pacman_x
+20, pacman_y
+3, 0);
276 xrectfill (pacman_x
+13, pacman_y
+4, pacman_x
+18, pacman_y
+7, 0);
277 xrectfill (pacman_x
+15, pacman_y
+8, pacman_x
+16, pacman_y
+11, 0);
278 } else if (pacman_rot
== 3) {
279 xrectfill (pacman_x
+11, pacman_y
+29, pacman_x
+20, pacman_y
+32, 0);
280 xrectfill (pacman_x
+13, pacman_y
+25, pacman_x
+18, pacman_y
+28, 0);
281 xrectfill (pacman_x
+15, pacman_y
+21, pacman_x
+16, pacman_y
+24, 0);
293 itoa (pacman_points
, buf_pts
, 10);
295 xtext_puts (20, 585, ~0, "Points:");
296 xtext_puts (80, 585, ~0, buf_pts
);
298 itoa (pacman_maxpoints
, buf_pts
, 10);
300 xtext_puts (100, 585, ~0, "/");
301 xtext_puts (110, 585, ~0, buf_pts
);
303 itoa (level_num
, buf_pts
, 10);
305 xtext_puts (200, 585, ~0, "Level:");
306 xtext_puts (250, 585, ~0, buf_pts
);
324 for (x
= 0; x
< 25; x
++) {
325 for (y
= 0; y
< 18; y
++) {
326 if (level_data
[x
+ y
* 26] == '3') {
327 if (i
>= beasts_count
)
330 beasts
[i
].x
= x
* 32;
331 beasts
[i
].y
= y
* 32;
336 if (level_data
[x
+ y
* 26] == '2')
337 level_data
[x
+ y
* 26] = '0';
342 void level_gameover ()
354 for (x
= 0; x
< 25; x
++) {
355 for (y
= 0; y
< 18; y
++) {
356 if (level_data
[x
+ y
* 26] == '3') {
357 if (i
>= beasts_count
)
360 beasts
[i
].x
= x
* 32;
361 beasts
[i
].y
= y
* 32;
366 if (level_data
[x
+ y
* 26] == '2')
367 level_data
[x
+ y
* 26] = '0';
372 void beast_stop (unsigned char i
)
374 if (beasts
[i
].rot
== 0) {
376 } else if (beasts
[i
].rot
== 1) {
378 } else if (beasts
[i
].rot
== 2) {
380 } else if (beasts
[i
].rot
== 3) {
387 if (pacman_rot
== 0) {
389 } else if (pacman_rot
== 1) {
391 } else if (pacman_rot
== 2) {
393 } else if (pacman_rot
== 3) {
402 if (scancode
== ARROWLEFT
)
404 else if (scancode
== ARROWRIGHT
)
406 else if (scancode
== ARROWUP
)
408 else if (scancode
== ARROWDOWN
)
411 if (pacman_rot
== 0) {
414 } else if (pacman_rot
== 1) {
415 if (pacman_x
< 800-32)
417 } else if (pacman_rot
== 2) {
420 } else if (pacman_rot
== 3) {
421 if (pacman_y
< 600-32)
425 if (!(pacman_x
% 32) && !(pacman_y
% 32))
426 pacman_rot
= pacman_rotn
;
435 for (i
= 0; i
< beasts_count
; i
++) {
436 if (beasts
[i
].rot
== 0) {
439 } else if (beasts
[i
].rot
== 1) {
440 if (beasts
[i
].x
< 800-32)
442 } else if (beasts
[i
].rot
== 2) {
445 } else if (beasts
[i
].rot
== 3) {
446 if (beasts
[i
].y
< 600-32)
450 /* collision with pacman and beasts */
451 if (beasts
[i
].x
/32 == pacman_x
/32 && beasts
[i
].y
/32 == pacman_y
/32)
461 scancode
= getkey ();
466 /* Flip double buffer - show image */
473 for (x
= 0; x
< 25; x
++) {
474 for (y
= 0; y
< 18; y
++) {
475 if (level_data
[x
+ y
* 26] == '1') {
476 if (pacman_x
> x
*32 && pacman_y
> y
*32 && pacman_x
< x
*32+32 && pacman_y
< y
*32+32)
478 else if (pacman_x
+31 > x
*32 && pacman_y
+31 > y
*32 && pacman_x
+31 < x
*32+32 && pacman_y
+31 < y
*32+32)
480 else if (pacman_x
+31 > x
*32 && pacman_y
> y
*32 && pacman_x
< x
*32+32 && pacman_y
< y
*32+32)
482 else if (pacman_x
> x
*32 && pacman_y
+31 > y
*32 && pacman_x
< x
*32+32 && pacman_y
< y
*32+32)
486 for (i
= 0; i
< beasts_count
; i
++) {
487 if (beasts
[i
].x
> x
*32 && beasts
[i
].y
> y
*32 && beasts
[i
].x
< x
*32+32 && beasts
[i
].y
< y
*32+32) {
489 beasts
[i
].rot
= rand () % 4;
490 } else if (beasts
[i
].x
+31 > x
*32 && beasts
[i
].y
+31 > y
*32 && beasts
[i
].x
+31 < x
*32+32 && beasts
[i
].y
+31 < y
*32+32) {
492 beasts
[i
].rot
= rand () % 4;
493 } else if (beasts
[i
].x
+31 > x
*32 && beasts
[i
].y
> y
*32 && beasts
[i
].x
< x
*32+32 && beasts
[i
].y
< y
*32+32) {
495 beasts
[i
].rot
= rand () % 4;
496 } else if (beasts
[i
].x
> x
*32 && beasts
[i
].y
+31 > y
*32 && beasts
[i
].x
< x
*32+32 && beasts
[i
].y
< y
*32+32) {
498 beasts
[i
].rot
= rand () % 4;
502 if (level_data
[x
+ y
* 26] == '0') {
505 if (pacman_x
> x
*32 && pacman_y
> y
*32 && pacman_x
< x
*32+32 && pacman_y
< y
*32+32)
507 if (pacman_x
+31 > x
*32 && pacman_y
+31 > y
*32 && pacman_x
+31 < x
*32+32 && pacman_y
+31 < y
*32+32)
509 if (pacman_x
+31 > x
*32 && pacman_y
> y
*32 && pacman_x
< x
*32+32 && pacman_y
< y
*32+32)
511 if (pacman_x
> x
*32 && pacman_y
+31 > y
*32 && pacman_x
< x
*32+32 && pacman_y
< y
*32+32)
516 level_data
[x
+ y
* 26] = '2';
525 if (pacman_points
== pacman_maxpoints
)
531 int main (int argc
, char **argv
)
546 /* Go back to textual mode */