3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
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
;
67 unsigned short *init_bitmap (char *str
)
70 unsigned short *bitmap
= (unsigned short *) ptr
;
77 memset (bitmap
, 0, 2048+70);
80 int fd
= open (str
, O_RDONLY
);
83 puts ("error -> file not found\n");
87 if (!read (fd
, (unsigned char *) bitmap
, 2048+70)) {
88 puts ("error -> something was wrong !\n");
99 level_data
= (char *) malloc (26 * 18 + 1);
104 memset (level_data
, 0, 26 * 18);
106 int fd
= open ("level", O_RDONLY
);
109 puts ("error -> file 'level' not found\n");
113 if (!read (fd
, (char *) level_data
, 26 * 18)) {
114 puts ("error -> something was wrong !\n");
120 img_wall
= init_bitmap ("imgwall");
133 img_pacman
= init_bitmap ("imgpac");
151 img_beast
= init_bitmap ("imgobl");
158 pacman_maxpoints
= 0;
164 for (x
= 0; x
< 25; x
++) {
165 for (y
= 0; y
< 18; y
++) {
166 if (level_data
[x
+ y
* 26] == '3') {
167 if (i
>= beasts_count
)
170 beasts
[i
].x
= x
* 32;
171 beasts
[i
].y
= y
* 32;
176 if (level_data
[x
+ y
* 26] == '0')
185 /* HACK: errrrrr,very ugly */
186 ptr
= (void *) 0x300000;
192 /* Switch to VGA graphics mode */
204 void draw_bitmap (unsigned short *bitmap
, unsigned short x
, unsigned short y
)
211 for (i
= 0; i
< 32*32; i
++) {
222 if (bitmap
[pix
] != (unsigned short) 0)
223 if (k
<= 32 && j
<= 32)
224 xpixel ((unsigned) x
+k
, (unsigned) y
+j
, bitmap
[pix
]);
231 void draw_field (unsigned char x
, unsigned char y
)
233 draw_bitmap (img_wall
, x
* 32, y
* 32);
236 void draw_point (unsigned char x
, unsigned char y
)
238 xline (x
* 32 + 11, y
* 32 + 10, x
* 32 + 20, y
* 32 + 10, ~0);
239 xrectfill (x
* 32 + 10, y
* 32 + 11, x
* 32 + 21, y
* 32 + 20, ~0);
240 xline (x
* 32 + 11, y
* 32 + 21, x
* 32 + 20, y
* 32 + 21, ~0);
246 for (i
= 0; i
< beasts_count
; i
++)
247 draw_bitmap (img_beast
, beasts
[i
].x
, beasts
[i
].y
);
255 for (x
= 0; x
< 25; x
++) {
256 for (y
= 0; y
< 18; y
++) {
257 if (level_data
[x
+ y
* 26] == '1')
260 if (level_data
[x
+ y
* 26] == '0')
268 /* Draw image to vga buffer */
269 draw_bitmap (img_pacman
, pacman_x
, pacman_y
);
271 if (pacman_points
% 2) {
272 if (pacman_rot
== 0) {
273 xrectfill (pacman_x
, pacman_y
+11, pacman_x
+3, pacman_y
+20, 0);
274 xrectfill (pacman_x
+4, pacman_y
+13, pacman_x
+7, pacman_y
+18, 0);
275 xrectfill (pacman_x
+8, pacman_y
+15, pacman_x
+11, pacman_y
+16, 0);
276 } else if (pacman_rot
== 1) {
277 xrectfill (pacman_x
+29, pacman_y
+11, pacman_x
+32, pacman_y
+20, 0);
278 xrectfill (pacman_x
+25, pacman_y
+13, pacman_x
+28, pacman_y
+18, 0);
279 xrectfill (pacman_x
+21, pacman_y
+15, pacman_x
+24, pacman_y
+16, 0);
280 } else if (pacman_rot
== 2) {
281 xrectfill (pacman_x
+11, pacman_y
, pacman_x
+20, pacman_y
+3, 0);
282 xrectfill (pacman_x
+13, pacman_y
+4, pacman_x
+18, pacman_y
+7, 0);
283 xrectfill (pacman_x
+15, pacman_y
+8, pacman_x
+16, pacman_y
+11, 0);
284 } else if (pacman_rot
== 3) {
285 xrectfill (pacman_x
+11, pacman_y
+29, pacman_x
+20, pacman_y
+32, 0);
286 xrectfill (pacman_x
+13, pacman_y
+25, pacman_x
+18, pacman_y
+28, 0);
287 xrectfill (pacman_x
+15, pacman_y
+21, pacman_x
+16, pacman_y
+24, 0);
299 itoa (pacman_points
, buf_pts
, 10);
301 xtext_puts (20, 585, ~0, "Points:");
302 xtext_puts (80, 585, ~0, buf_pts
);
304 itoa (pacman_maxpoints
, buf_pts
, 10);
306 xtext_puts (100, 585, ~0, "/");
307 xtext_puts (110, 585, ~0, buf_pts
);
309 itoa (level_num
, buf_pts
, 10);
311 xtext_puts (200, 585, ~0, "Level:");
312 xtext_puts (250, 585, ~0, buf_pts
);
330 for (x
= 0; x
< 25; x
++) {
331 for (y
= 0; y
< 18; y
++) {
332 if (level_data
[x
+ y
* 26] == '3') {
333 if (i
>= beasts_count
)
336 beasts
[i
].x
= x
* 32;
337 beasts
[i
].y
= y
* 32;
342 if (level_data
[x
+ y
* 26] == '2')
343 level_data
[x
+ y
* 26] = '0';
348 void level_gameover ()
360 for (x
= 0; x
< 25; x
++) {
361 for (y
= 0; y
< 18; y
++) {
362 if (level_data
[x
+ y
* 26] == '3') {
363 if (i
>= beasts_count
)
366 beasts
[i
].x
= x
* 32;
367 beasts
[i
].y
= y
* 32;
372 if (level_data
[x
+ y
* 26] == '2')
373 level_data
[x
+ y
* 26] = '0';
378 void beast_stop (unsigned char i
)
380 if (beasts
[i
].rot
== 0) {
382 } else if (beasts
[i
].rot
== 1) {
384 } else if (beasts
[i
].rot
== 2) {
386 } else if (beasts
[i
].rot
== 3) {
393 if (pacman_rot
== 0) {
395 } else if (pacman_rot
== 1) {
397 } else if (pacman_rot
== 2) {
399 } else if (pacman_rot
== 3) {
408 if (scancode
== ARROWLEFT
)
410 else if (scancode
== ARROWRIGHT
)
412 else if (scancode
== ARROWUP
)
414 else if (scancode
== ARROWDOWN
)
417 if (pacman_rot
== 0) {
420 } else if (pacman_rot
== 1) {
421 if (pacman_x
< 800-32)
423 } else if (pacman_rot
== 2) {
426 } else if (pacman_rot
== 3) {
427 if (pacman_y
< 600-32)
431 if (!(pacman_x
% 32) && !(pacman_y
% 32))
432 pacman_rot
= pacman_rotn
;
441 for (i
= 0; i
< beasts_count
; i
++) {
442 if (beasts
[i
].rot
== 0) {
445 } else if (beasts
[i
].rot
== 1) {
446 if (beasts
[i
].x
< 800-32)
448 } else if (beasts
[i
].rot
== 2) {
451 } else if (beasts
[i
].rot
== 3) {
452 if (beasts
[i
].y
< 600-32)
456 /* collision with pacman and beasts */
457 if (beasts
[i
].x
/32 == pacman_x
/32 && beasts
[i
].y
/32 == pacman_y
/32)
467 scancode
= getkey ();
472 /* Flip double buffer - show image */
479 for (x
= 0; x
< 25; x
++) {
480 for (y
= 0; y
< 18; y
++) {
481 if (level_data
[x
+ y
* 26] == '1') {
482 if (pacman_x
> x
*32 && pacman_y
> y
*32 && pacman_x
< x
*32+32 && pacman_y
< y
*32+32)
484 else if (pacman_x
+31 > x
*32 && pacman_y
+31 > y
*32 && pacman_x
+31 < x
*32+32 && pacman_y
+31 < y
*32+32)
486 else if (pacman_x
+31 > x
*32 && pacman_y
> y
*32 && pacman_x
< x
*32+32 && pacman_y
< y
*32+32)
488 else if (pacman_x
> x
*32 && pacman_y
+31 > y
*32 && pacman_x
< x
*32+32 && pacman_y
< y
*32+32)
492 for (i
= 0; i
< beasts_count
; i
++) {
493 if (beasts
[i
].x
> 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
+31 > x
*32 && beasts
[i
].y
+31 > y
*32 && beasts
[i
].x
+31 < x
*32+32 && beasts
[i
].y
+31 < y
*32+32) {
498 beasts
[i
].rot
= rand () % 4;
499 } 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) {
501 beasts
[i
].rot
= rand () % 4;
502 } 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) {
504 beasts
[i
].rot
= rand () % 4;
508 if (level_data
[x
+ y
* 26] == '0') {
511 if (pacman_x
> x
*32 && pacman_y
> y
*32 && pacman_x
< x
*32+32 && pacman_y
< y
*32+32)
513 if (pacman_x
+31 > x
*32 && pacman_y
+31 > y
*32 && pacman_x
+31 < x
*32+32 && pacman_y
+31 < y
*32+32)
515 if (pacman_x
+31 > x
*32 && pacman_y
> y
*32 && pacman_x
< x
*32+32 && pacman_y
< y
*32+32)
517 if (pacman_x
> x
*32 && pacman_y
+31 > y
*32 && pacman_x
< x
*32+32 && pacman_y
< y
*32+32)
522 level_data
[x
+ y
* 26] = '2';
531 if (pacman_points
== pacman_maxpoints
)
537 int main (int argc
, char **argv
)
552 /* Go back to textual mode */