home *** CD-ROM | disk | FTP | other *** search
- #include "manic.h"
- #include "systems/init.h"
- #include "gfxlibs/init.h"
- #include "sndlibs/init.h"
- #include "gfxlibs/keyboard.h"
- #include "gfxdata/gfxdata.h"
- #include "functions.h"
- #include "gfxlibs/gfx.h"
- #include "sndlibs/snd.h"
- #include "miscdata/misc.h"
- #include <stdlib.h>
-
- #ifdef LINUX
- #ifdef ALLEGRO
- #include <allegro.h>
- #endif
- #endif
-
- #ifdef __riscos
- extern int SOUND, VOLUME;
- #endif
-
- void
- SaveInfo (void)
- {
- FILE *file;
-
- file = fopen (CONFIGWRITE, "wb");
- if (!file) {
- fprintf (stderr, "Could not save the configuration file\n");
- exit (2);
- }
- fwrite (&HISCORE, sizeof (unsigned long), 1, file);
- fwrite (&SPEED, sizeof (BYTE), 1, file);
- #ifdef __riscos
- fwrite (&VOLUME, sizeof (BYTE), 1, file);
- {
- BYTE i = MUSICtype | (BYTE) (SOUND << 2);
- fwrite (&i, sizeof (BYTE), 1, file);
- }
- #else
- fwrite (&TONKS, sizeof (BYTE), 1, file);
- fwrite (&MUSICtype, sizeof (BYTE), 1, file);
- #endif
- fwrite (&VidMode, sizeof (BYTE), 1, file);
- fwrite (&PAUSEtype, sizeof (BYTE), 1, file);
- if (ferror (file) || fclose (file)) {
- fprintf (stderr, "Could not save the configuration file\n");
- exit (2);
- }
- }
-
- void
- loadfile (char *filename, unsigned char *array, unsigned length)
- {
- FILE *file;
- if ((file = fopen (filename, "rb")) == 0) {
- fprintf (stderr, "Could not load %s\n", filename);
- exit (-1);
- }
- fread (array, 1, length, file);
- if (ferror (file) || fclose (file)) {
- fprintf (stderr, "Could not read %s\n", filename);
- exit (2);
- }
- }
-
- static BYTE
- readByte (FILE *file)
- {
- BYTE c;
- fread (&c, 1, 1, file);
- return c;
- }
-
- static WORD
- readShort (FILE *file)
- {
- BYTE c[2];
- fread (c, 1, 2, file);
- return c[0] | c[1] << 8;
- }
-
- int
- loadgame (const char *name)
- {
- int n, i;
- char key[8] = {0};
- static const char lock[] = {'M', 'A', 'N', 'I', 'C', 13, 10, 26};
- FILE *file;
- if ((file = fopen (name, "rb")) == 0)
- return 2; /* couldn't open the file */
- fread (key, 1, 8, file);
- if (memcmp (key, lock, 8)) {
- fclose (file);
- return 1; /* the file doesn't have the required signature */
- }
- fread (LEVELSNAME, 1, 16, file);
- LEVELSNAME[15] = 0;
- for (n = 0; n < 20; n++) {
- fread (&map[n], sizeof (*map), 1, file);
- fread (&title[n], sizeof (*title), 1, file);
- BORDER[n] = readByte (file);
- BG[n].ink = readByte (file);
- BG[n].paper = readByte (file);
- PLAT1[n].ink = readByte (file);
- PLAT1[n].paper = readByte (file);
- PLAT1[n].gfx = readByte (file);
- PLAT2[n].ink = readByte (file);
- PLAT2[n].paper = readByte (file);
- PLAT2[n].gfx = readByte (file);
- WALL[n].ink = readByte (file);
- WALL[n].paper = readByte (file);
- WALL[n].gfx = readByte (file);
- CRUMB[n].ink = readByte (file);
- CRUMB[n].paper = readByte (file);
- CRUMB[n].gfx = readByte (file);
- KILL1[n].ink = readByte (file);
- KILL1[n].paper = readByte (file);
- KILL1[n].gfx = readByte (file);
- KILL2[n].ink = readByte (file);
- KILL2[n].paper = readByte (file);
- KILL2[n].gfx = readByte (file);
- CONV[n].s.ink = readByte (file);
- CONV[n].s.paper = readByte (file);
- CONV[n].s.gfx = readByte (file);
- WILLYs[n].x = readShort (file);
- WILLYs[n].y = readShort (file);
- WILLYsd[n] = readByte (file);
- CONV[n].pos.x = readShort (file);
- CONV[n].pos.y = readShort (file);
- CONV[n].d = readByte (file);
- CONV[n].l = readByte (file);
- for (i = 0; i < 5; i++) KEY[n].key[i].pos.x = readShort (file);
- for (i = 0; i < 5; i++) KEY[n].key[i].pos.y = readShort (file);
- KEY[n].gfx = readByte (file);
- for (i = 0; i < 5; i++) KEY[n].key[i].s = readByte (file);
- for (i = 0; i < 2; i++) SWITCH[n][i].pos.x = readShort (file);
- for (i = 0; i < 2; i++) SWITCH[n][i].pos.y = readShort (file);
- for (i = 0; i < 2; i++) SWITCH[n][i].s = readByte (file);
- EXIT[n].pos.x = readShort (file);
- EXIT[n].pos.y = readShort (file);
- EXIT[n].gfx = readByte (file);
- AIR[n] = readByte (file);
- for (i = 0; i < 4; i++) HROBO[n][i].colour.ink = readByte (file);
- for (i = 0; i < 4; i++) HROBO[n][i].colour.paper = readByte (file);
- for (i = 0; i < 4; i++) HROBO[n][i].pos.x = readShort (file);
- for (i = 0; i < 4; i++) HROBO[n][i].pos.y = readShort (file);
- for (i = 0; i < 4; i++) HROBO[n][i].min = readShort (file);
- for (i = 0; i < 4; i++) HROBO[n][i].max = readShort (file);
- for (i = 0; i < 4; i++) HROBO[n][i].d = readByte (file);
- for (i = 0; i < 4; i++) HROBO[n][i].s = readByte (file);
- for (i = 0; i < 4; i++) HROBO[n][i].gfx = readShort (file);
- for (i = 0; i < 4; i++) HROBO[n][i].flip = readByte (file);
- for (i = 0; i < 4; i++) HROBO[n][i].anim = readByte (file);
- for (i = 0; i < 4; i++) VROBO[n][i].colour.ink = readByte (file);
- for (i = 0; i < 4; i++) VROBO[n][i].colour.paper = readByte (file);
- for (i = 0; i < 4; i++) VROBO[n][i].pos.x = readShort (file);
- for (i = 0; i < 4; i++) VROBO[n][i].pos.y = readShort (file);
- for (i = 0; i < 4; i++) VROBO[n][i].min = readShort (file);
- for (i = 0; i < 4; i++) VROBO[n][i].max = readShort (file);
- for (i = 0; i < 4; i++) VROBO[n][i].d = readByte (file);
- for (i = 0; i < 4; i++) VROBO[n][i].s = readByte (file);
- for (i = 0; i < 4; i++) VROBO[n][i].gfx = readShort (file);
- for (i = 0; i < 4; i++) VROBO[n][i].anim = readByte (file);
- }
- i = ferror (file);
- fclose (file);
- return i ? 3 : 0;
- }
-
- static void
- loadgfx (void)
- {
- loadfile (GFXDATA "air", GFXair, 2048);
- loadfile (GFXDATA "blocks", GFXblocks, 104 * 64);
- loadfile (GFXDATA "conv", GFXconv, 10 * 4 * 64);
- loadfile (GFXDATA "eugene", GFXeugene, 256 * 2);
- loadfile (GFXDATA "exits", GFXexit, 21 * 256);
- loadfile (GFXDATA "fant", GFXfant, 8 * 96);
- loadfile (GFXDATA "fill", GFXfill, 2112);
- loadfile (GFXDATA "final", GFXfinal, 256 * 64);
- loadfile (GFXDATA "fontb", fontb, 8 * 96);
- loadfile (GFXDATA "fonts", fonts, 6 * 64);
- loadfile (GFXDATA "house", GFXhouse, 1024);
- loadfile (GFXDATA "hrobo", GFXhrobo, 120 * 256);
- loadfile (GFXDATA "keys", GFXkey, 17 * 64);
- loadfile (GFXDATA "kong", GFXkong, 256 * 4);
- loadfile (GFXDATA "load", GFXload, 32 * 8 * 2);
- loadfile (GFXDATA "over", GFXover, 256 * 2);
- loadfile (GFXDATA "piano", GFXpiano, 256 * 64);
- loadfile (GFXDATA "pkeys", GFXpkeys, 128 * 8);
- loadfile (GFXDATA "sky", GFXsky, 128 * 16);
- loadfile (GFXDATA "sun", GFXsun, 48 * 8);
- loadfile (GFXDATA "switch", GFXswitch, 2 * 64);
- loadfile (GFXDATA "tplate", GFXtplate, 32 * 64);
- loadfile (GFXDATA "vrobo", GFXvrobo, 28 * 256);
- loadfile (GFXDATA "willy", GFXwilly, 16 * 256);
- loadfile (GFXDATA "win", GFXwin, 4864);
- loadfile (GFXDATA "palmain", PALmain, 768);
- }
-
- void
- LoadInfo (void)
- {
- FILE *file;
-
- file = fopen (CONFIGREAD, "rb");
- if (file == NULL) {
- HISCORE = 0;
- SPEED = 2;
- TONKS = 0;
- MUSICtype = 0;
- VidMode = X320x200;
- return;
- }
- fread (&HISCORE, sizeof (unsigned long), 1, file);
- fread (&SPEED, sizeof (BYTE), 1, file);
- if (SPEED > 4)
- SPEED = 2;
- #ifdef __riscos
- fread (&VOLUME, sizeof (BYTE), 1, file);
- #else
- fread (&TONKS, sizeof (BYTE), 1, file);
- #endif
- fread (&MUSICtype, sizeof (BYTE), 1, file);
- #ifdef __riscos
- SOUND = MUSICtype >> 2;
- MUSICtype &= 1;
- #else
- MUSICtype = (MUSICtype != 0);
- #endif
- /*if (!VS) {*/
- fread (&VidMode, sizeof (BYTE), 1, file);
- if (VidMode < XMIN || VidMode > XMAX)
- VidMode = XMIN;
- /*}*/
- fread (&PAUSEtype, sizeof (BYTE), 1, file);
- PAUSEtype = (PAUSEtype != 0);
- if (ferror (file) || fclose (file)) {
- fprintf (stderr, "Could not read %s\n", CONFIGREAD);
- exit (2);
- }
- }
-
- static void
- init (void)
- {
- loadgfx ();
- switch (loadgame (dataname)) {
- case 1:
- fprintf (stderr, "%s is not a valid MM levels file\n", dataname);
- exit (-1);
- case 2:
- fprintf (stderr, "Could not load %s\n", dataname);
- exit (-1);
- case 3:
- fprintf (stderr, "Could not read %s\n", dataname);
- exit (2);
- }
- mm_sys_init ();
- mm_snd_init ();
- mm_gfx_init ();
- mm_gfx_palset (PALmain);
- }
-
- static void
- shutdown (void)
- {
- mm_gfx_shutdown ();
- mm_snd_shutdown ();
- mm_sys_shutdown ();
- }
-
- void
- mainloop (void)
- {
- for (;;) {
- switch (MODE) {
- case 0:
- Titles ();
- break;
- case 1:
- DoDemo ();
- break;
- case 2:
- DoGame ();
- break;
- case 3:
- DoLoading ();
- break;
- }
- mm_snd_update ();
- mm_keyb_update ();
- #ifdef __riscos
- if (!(MODE == 2 && GAMEm > 1) && mm_keyb_pressed (KEYB_F12))
- return;
- #else
- if (mm_keyb_pressed (KEYB_F10))
- return;
- #endif
- }
- }
-
- int
- main (int argc, const char *const argv[])
- {
- cmdline (argc, argv);
- LoadInfo ();
- if (ResetHigh == 1)
- HISCORE = 0;
- init ();
-
- mm_gfx_cls (0);
- mm_gfx_flush ();
- LOADm = 0;
-
- mainloop ();
-
- mm_gfx_cls (0);
- mm_gfx_palset (PALblack);
- mm_gfx_flush ();
-
- shutdown ();
- SaveInfo ();
-
- version ();
- return (0);
- }
-
- #if defined LINUX && defined ALLEGRO
- END_OF_MAIN ()
- #endif
-