home *** CD-ROM | disk | FTP | other *** search
/ RISCWORLD 7 / RISCWORLD_VOL7.iso / Software / Issue4 / IYONIX / MANICMINER / SOURCE.ZIP / manicminer-1.6.3 / c / main < prev    next >
Encoding:
Text File  |  2000-12-10  |  8.7 KB  |  334 lines

  1. #include "manic.h"
  2. #include "systems/init.h"
  3. #include "gfxlibs/init.h"
  4. #include "sndlibs/init.h"
  5. #include "gfxlibs/keyboard.h"
  6. #include "gfxdata/gfxdata.h"
  7. #include "functions.h"
  8. #include "gfxlibs/gfx.h"
  9. #include "sndlibs/snd.h"
  10. #include "miscdata/misc.h"
  11. #include <stdlib.h>
  12.  
  13. #ifdef LINUX
  14. #ifdef ALLEGRO
  15. #include <allegro.h>
  16. #endif
  17. #endif
  18.  
  19. #ifdef __riscos
  20. extern int SOUND, VOLUME;
  21. #endif
  22.  
  23. void
  24. SaveInfo (void)
  25. {
  26.   FILE *file;
  27.  
  28.   file = fopen (CONFIGWRITE, "wb");
  29.   if (!file) {
  30.     fprintf (stderr, "Could not save the configuration file\n");
  31.     exit (2);
  32.   }
  33.   fwrite (&HISCORE, sizeof (unsigned long), 1, file);
  34.   fwrite (&SPEED, sizeof (BYTE), 1, file);
  35. #ifdef __riscos
  36.   fwrite (&VOLUME, sizeof (BYTE), 1, file);
  37.   {
  38.     BYTE i = MUSICtype | (BYTE) (SOUND << 2);
  39.     fwrite (&i, sizeof (BYTE), 1, file);
  40.   }
  41. #else
  42.   fwrite (&TONKS, sizeof (BYTE), 1, file);
  43.   fwrite (&MUSICtype, sizeof (BYTE), 1, file);
  44. #endif
  45.   fwrite (&VidMode, sizeof (BYTE), 1, file);
  46.   fwrite (&PAUSEtype, sizeof (BYTE), 1, file);
  47.   if (ferror (file) || fclose (file)) {
  48.     fprintf (stderr, "Could not save the configuration file\n");
  49.     exit (2);
  50.   }
  51. }
  52.  
  53. void
  54. loadfile (char *filename, unsigned char *array, unsigned length)
  55. {
  56.   FILE *file;
  57.   if ((file = fopen (filename, "rb")) == 0) {
  58.     fprintf (stderr, "Could not load %s\n", filename);
  59.     exit (-1);
  60.   }
  61.   fread (array, 1, length, file);
  62.   if (ferror (file) || fclose (file)) {
  63.     fprintf (stderr, "Could not read %s\n", filename);
  64.     exit (2);
  65.   }
  66. }
  67.  
  68. static BYTE
  69. readByte (FILE *file)
  70. {
  71.   BYTE c;
  72.   fread (&c, 1, 1, file);
  73.   return c;
  74. }
  75.  
  76. static WORD
  77. readShort (FILE *file)
  78. {
  79.   BYTE c[2];
  80.   fread (c, 1, 2, file);
  81.   return c[0] | c[1] << 8;
  82. }
  83.  
  84. int
  85. loadgame (const char *name)
  86. {
  87.   int n, i;
  88.   char key[8] = {0};
  89.   static const char lock[] = {'M', 'A', 'N', 'I', 'C', 13, 10, 26};
  90.   FILE *file;
  91.   if ((file = fopen (name, "rb")) == 0)
  92.     return 2; /* couldn't open the file */
  93.   fread (key, 1, 8, file);
  94.   if (memcmp (key, lock, 8)) {
  95.     fclose (file);
  96.     return 1; /* the file doesn't have the required signature */
  97.   }
  98.   fread (LEVELSNAME, 1, 16, file);
  99.   LEVELSNAME[15] = 0;
  100.   for (n = 0; n < 20; n++) {
  101.     fread (&map[n], sizeof (*map), 1, file);
  102.     fread (&title[n], sizeof (*title), 1, file);
  103.     BORDER[n] =        readByte (file);
  104.     BG[n].ink =        readByte (file);
  105.     BG[n].paper =    readByte (file);
  106.     PLAT1[n].ink =    readByte (file);
  107.     PLAT1[n].paper =    readByte (file);
  108.     PLAT1[n].gfx =    readByte (file);
  109.     PLAT2[n].ink =    readByte (file);
  110.     PLAT2[n].paper =    readByte (file);
  111.     PLAT2[n].gfx =    readByte (file);
  112.     WALL[n].ink =    readByte (file);
  113.     WALL[n].paper =    readByte (file);
  114.     WALL[n].gfx =    readByte (file);
  115.     CRUMB[n].ink =    readByte (file);
  116.     CRUMB[n].paper =    readByte (file);
  117.     CRUMB[n].gfx =    readByte (file);
  118.     KILL1[n].ink =    readByte (file);
  119.     KILL1[n].paper =    readByte (file);
  120.     KILL1[n].gfx =    readByte (file);
  121.     KILL2[n].ink =    readByte (file);
  122.     KILL2[n].paper =    readByte (file);
  123.     KILL2[n].gfx =    readByte (file);
  124.     CONV[n].s.ink =    readByte (file);
  125.     CONV[n].s.paper =    readByte (file);
  126.     CONV[n].s.gfx =    readByte (file);
  127.     WILLYs[n].x =    readShort (file);
  128.     WILLYs[n].y =    readShort (file);
  129.     WILLYsd[n] =    readByte (file);
  130.     CONV[n].pos.x =    readShort (file);
  131.     CONV[n].pos.y =    readShort (file);
  132.     CONV[n].d =        readByte (file);
  133.     CONV[n].l =        readByte (file);
  134.     for (i = 0; i < 5; i++) KEY[n].key[i].pos.x =    readShort (file);
  135.     for (i = 0; i < 5; i++) KEY[n].key[i].pos.y =    readShort (file);
  136.     KEY[n].gfx =    readByte (file);
  137.     for (i = 0; i < 5; i++) KEY[n].key[i].s =        readByte (file);
  138.     for (i = 0; i < 2; i++) SWITCH[n][i].pos.x =    readShort (file);
  139.     for (i = 0; i < 2; i++) SWITCH[n][i].pos.y =    readShort (file);
  140.     for (i = 0; i < 2; i++) SWITCH[n][i].s =        readByte (file);
  141.     EXIT[n].pos.x =    readShort (file);
  142.     EXIT[n].pos.y =    readShort (file);
  143.     EXIT[n].gfx =    readByte (file);
  144.     AIR[n] =        readByte (file);
  145.     for (i = 0; i < 4; i++) HROBO[n][i].colour.ink =    readByte (file);
  146.     for (i = 0; i < 4; i++) HROBO[n][i].colour.paper =    readByte (file);
  147.     for (i = 0; i < 4; i++) HROBO[n][i].pos.x =        readShort (file);
  148.     for (i = 0; i < 4; i++) HROBO[n][i].pos.y =        readShort (file);
  149.     for (i = 0; i < 4; i++) HROBO[n][i].min =        readShort (file);
  150.     for (i = 0; i < 4; i++) HROBO[n][i].max =        readShort (file);
  151.     for (i = 0; i < 4; i++) HROBO[n][i].d =        readByte (file);
  152.     for (i = 0; i < 4; i++) HROBO[n][i].s =        readByte (file);
  153.     for (i = 0; i < 4; i++) HROBO[n][i].gfx =        readShort (file);
  154.     for (i = 0; i < 4; i++) HROBO[n][i].flip =        readByte (file);
  155.     for (i = 0; i < 4; i++) HROBO[n][i].anim =        readByte (file);
  156.     for (i = 0; i < 4; i++) VROBO[n][i].colour.ink =    readByte (file);
  157.     for (i = 0; i < 4; i++) VROBO[n][i].colour.paper =    readByte (file);
  158.     for (i = 0; i < 4; i++) VROBO[n][i].pos.x =        readShort (file);
  159.     for (i = 0; i < 4; i++) VROBO[n][i].pos.y =        readShort (file);
  160.     for (i = 0; i < 4; i++) VROBO[n][i].min =        readShort (file);
  161.     for (i = 0; i < 4; i++) VROBO[n][i].max =        readShort (file);
  162.     for (i = 0; i < 4; i++) VROBO[n][i].d =        readByte (file);
  163.     for (i = 0; i < 4; i++) VROBO[n][i].s =        readByte (file);
  164.     for (i = 0; i < 4; i++) VROBO[n][i].gfx =        readShort (file);
  165.     for (i = 0; i < 4; i++) VROBO[n][i].anim =        readByte (file);
  166.   }
  167.   i = ferror (file);
  168.   fclose (file);
  169.   return i ? 3 : 0;
  170. }
  171.  
  172. static void
  173. loadgfx (void)
  174. {
  175.   loadfile (GFXDATA "air", GFXair, 2048);
  176.   loadfile (GFXDATA "blocks", GFXblocks, 104 * 64);
  177.   loadfile (GFXDATA "conv", GFXconv, 10 * 4 * 64);
  178.   loadfile (GFXDATA "eugene", GFXeugene, 256 * 2);
  179.   loadfile (GFXDATA "exits", GFXexit, 21 * 256);
  180.   loadfile (GFXDATA "fant", GFXfant, 8 * 96);
  181.   loadfile (GFXDATA "fill", GFXfill, 2112);
  182.   loadfile (GFXDATA "final", GFXfinal, 256 * 64);
  183.   loadfile (GFXDATA "fontb", fontb, 8 * 96);
  184.   loadfile (GFXDATA "fonts", fonts, 6 * 64);
  185.   loadfile (GFXDATA "house", GFXhouse, 1024);
  186.   loadfile (GFXDATA "hrobo", GFXhrobo, 120 * 256);
  187.   loadfile (GFXDATA "keys", GFXkey, 17 * 64);
  188.   loadfile (GFXDATA "kong", GFXkong, 256 * 4);
  189.   loadfile (GFXDATA "load", GFXload, 32 * 8 * 2);
  190.   loadfile (GFXDATA "over", GFXover, 256 * 2);
  191.   loadfile (GFXDATA "piano", GFXpiano, 256 * 64);
  192.   loadfile (GFXDATA "pkeys", GFXpkeys, 128 * 8);
  193.   loadfile (GFXDATA "sky", GFXsky, 128 * 16);
  194.   loadfile (GFXDATA "sun", GFXsun, 48 * 8);
  195.   loadfile (GFXDATA "switch", GFXswitch, 2 * 64);
  196.   loadfile (GFXDATA "tplate", GFXtplate, 32 * 64);
  197.   loadfile (GFXDATA "vrobo", GFXvrobo, 28 * 256);
  198.   loadfile (GFXDATA "willy", GFXwilly, 16 * 256);
  199.   loadfile (GFXDATA "win", GFXwin, 4864);
  200.   loadfile (GFXDATA "palmain", PALmain, 768);
  201. }
  202.  
  203. void
  204. LoadInfo (void)
  205. {
  206.   FILE *file;
  207.  
  208.   file = fopen (CONFIGREAD, "rb");
  209.   if (file == NULL) {
  210.     HISCORE = 0;
  211.     SPEED = 2;
  212.     TONKS = 0;
  213.     MUSICtype = 0;
  214.     VidMode = X320x200;
  215.     return;
  216.   }
  217.   fread (&HISCORE, sizeof (unsigned long), 1, file);
  218.   fread (&SPEED, sizeof (BYTE), 1, file);
  219.   if (SPEED > 4)
  220.     SPEED = 2;
  221. #ifdef __riscos
  222.   fread (&VOLUME, sizeof (BYTE), 1, file);
  223. #else
  224.   fread (&TONKS, sizeof (BYTE), 1, file);
  225. #endif
  226.   fread (&MUSICtype, sizeof (BYTE), 1, file);
  227. #ifdef __riscos
  228.   SOUND = MUSICtype >> 2;
  229.   MUSICtype &= 1;
  230. #else
  231.   MUSICtype = (MUSICtype != 0);
  232. #endif
  233.   /*if (!VS) {*/
  234.     fread (&VidMode, sizeof (BYTE), 1, file);
  235.     if (VidMode < XMIN || VidMode > XMAX)
  236.       VidMode = XMIN;
  237.   /*}*/
  238.   fread (&PAUSEtype, sizeof (BYTE), 1, file);
  239.   PAUSEtype = (PAUSEtype != 0);
  240.   if (ferror (file) || fclose (file)) {
  241.     fprintf (stderr, "Could not read %s\n", CONFIGREAD);
  242.     exit (2);
  243.   }
  244. }
  245.  
  246. static void
  247. init (void)
  248. {
  249.   loadgfx ();
  250.   switch (loadgame (dataname)) {
  251.   case 1:
  252.     fprintf (stderr, "%s is not a valid MM levels file\n", dataname);
  253.     exit (-1);
  254.   case 2:
  255.     fprintf (stderr, "Could not load %s\n", dataname);
  256.     exit (-1);
  257.   case 3:
  258.     fprintf (stderr, "Could not read %s\n", dataname);
  259.     exit (2);
  260.   }
  261.   mm_sys_init ();
  262.   mm_snd_init ();
  263.   mm_gfx_init ();
  264.   mm_gfx_palset (PALmain);
  265. }
  266.  
  267. static void
  268. shutdown (void)
  269. {
  270.   mm_gfx_shutdown ();
  271.   mm_snd_shutdown ();
  272.   mm_sys_shutdown ();
  273. }
  274.  
  275. void
  276. mainloop (void)
  277. {
  278.   for (;;) {
  279.     switch (MODE) {
  280.     case 0:
  281.       Titles ();
  282.       break;
  283.     case 1:
  284.       DoDemo ();
  285.       break;
  286.     case 2:
  287.       DoGame ();
  288.       break;
  289.     case 3:
  290.       DoLoading ();
  291.       break;
  292.     }
  293.     mm_snd_update ();
  294.     mm_keyb_update ();
  295. #ifdef __riscos
  296.     if (!(MODE == 2 && GAMEm > 1) && mm_keyb_pressed (KEYB_F12))
  297.       return;
  298. #else
  299.     if (mm_keyb_pressed (KEYB_F10))
  300.       return;
  301. #endif
  302.   }
  303. }
  304.  
  305. int
  306. main (int argc, const char *const argv[])
  307. {
  308.   cmdline (argc, argv);
  309.   LoadInfo ();
  310.   if (ResetHigh == 1)
  311.     HISCORE = 0;
  312.   init ();
  313.  
  314.   mm_gfx_cls (0);
  315.   mm_gfx_flush ();
  316.   LOADm = 0;
  317.  
  318.   mainloop ();
  319.  
  320.   mm_gfx_cls (0);
  321.   mm_gfx_palset (PALblack);
  322.   mm_gfx_flush ();
  323.  
  324.   shutdown ();
  325.   SaveInfo ();
  326.  
  327.   version ();
  328.   return (0);
  329. }
  330.  
  331. #if defined LINUX && defined ALLEGRO
  332. END_OF_MAIN ()
  333. #endif
  334.