home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format 98 / af098sub.adf / asteroids2.LZX / asteroids2 / rotx / init.c < prev    next >
C/C++ Source or Header  |  2009-09-17  |  9KB  |  515 lines

  1. #include <exec/types.h>
  2. #include <exec/memory.h>
  3. #include <exec/execbase.h>
  4. #include <dos/dos.h>
  5. #include <intuition/intuition.h>
  6. #include <graphics/gfx.h>
  7. #include <graphics/gfxbase.h>
  8. #include <graphics/gfxmacros.h>
  9. #include <graphics/gfxbase.h>
  10. #include <graphics/rastport.h>
  11. #include <graphics/view.h>
  12. #include <graphics/displayinfo.h>
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <h/rot.h>
  16. #include <h/define.h>
  17.  
  18. extern struct ExecBase *SysBase;
  19.  
  20. extern struct gameinput in;
  21. extern struct control control;
  22. extern struct gameinfo gi;
  23. extern struct ship ship[7];
  24. extern struct asteroid a[32];
  25. extern struct saucer saucer;
  26. extern struct MsgPort *inputDevPort;
  27. extern struct keys k;
  28.  
  29. struct NewWindow newmasterwindow =
  30. {
  31. 0,0,
  32. 0,0,
  33. 0,0,
  34. RAWKEY,
  35. NOCAREREFRESH | ACTIVATE | BACKDROP | BORDERLESS,
  36. NULL,
  37. NULL,NULL,NULL,NULL,
  38. NULL,NULL,NULL,NULL,
  39. CUSTOMSCREEN};
  40.  
  41.  
  42. struct TextAttr basic;
  43. struct TextAttr hires;
  44. struct TextAttr fixplain7;
  45. struct TextFont *basicfont,*fixplain7font,*hiresfont;
  46. struct TextFont *lfont,*mfont,*sfont;
  47.  
  48. struct BitMap *bm1[2];
  49.  
  50. struct RasInfo *ri1;
  51. struct ViewPort *vp;
  52. struct RastPort *rp1[2];
  53.  
  54. struct GfxBase *GfxBase;
  55. struct IntuitionBase *IntuitionBase;
  56. struct Library *DiskfontBase;
  57. struct Library    *IFFParseBase;
  58.  
  59. struct Screen *screen;
  60. struct Screen *screen2;
  61. struct Window *masterwindow;
  62. struct RastPort *mwrp;
  63. UWORD *pdata;
  64.  
  65.  
  66. LONG AllocateBitmaps();
  67. LONG CreateDisplay();
  68. LONG GetWorkbenchData();
  69.  
  70.  
  71. Initialization()
  72. {
  73. LONG error;
  74.  
  75. LoadFonts();
  76.  
  77. error = AllocateBitmaps();
  78. if (error == -1)
  79.     {
  80.     makerequest("Bitmap Allocation Error");
  81.     DeallocateBitmaps();
  82.     CloseFonts();
  83.     CloseLibraries();
  84.     exit(NULL);
  85.     }
  86.  
  87.  
  88. error = CreateDisplay();
  89. if (error == -1)
  90.     {
  91.     ClearPointer(masterwindow);
  92.     if (pdata) FreeMem(pdata,12);
  93.     if (masterwindow) CloseWindow(masterwindow);
  94.     if (screen) CloseScreen(screen);
  95.     if (rp1[1]) FreeMem(rp1[1],sizeof(struct RastPort));
  96.  
  97.     DeallocateBitmaps();
  98.     CloseFonts();
  99.     CloseLibraries();
  100.     exit(NULL);
  101.     }
  102.  
  103.  
  104. SetRGB4(vp,0,0,0,0);
  105. SetRGB4(vp,1,15,15,15);
  106. SetRGB4(vp,2,5,7,7);
  107. SetRGB4(vp,3,15,10,10);
  108.  
  109. LoadAllImages();
  110. DefineShips();
  111. RendVectors();
  112. LoadAllSounds();
  113. LoadHighScores();
  114. initaudio();
  115. addtimer();
  116. addgameport();
  117. addhandler();
  118. }
  119.  
  120.  
  121. Cleanup()
  122. {
  123. removehandler();
  124. removegameport();
  125. removetimer();
  126. closeaudio();
  127. freeimages();
  128.  
  129. ClearPointer(masterwindow);
  130. if (pdata) FreeMem(pdata,12);
  131. if (masterwindow) CloseWindow(masterwindow);
  132. if (screen) CloseScreen(screen);
  133. if (rp1[1]) FreeMem(rp1[1],sizeof(struct RastPort));
  134.  
  135. DeallocateBitmaps();
  136.  
  137. CloseFonts();
  138. CloseLibraries();
  139. exit(0);
  140. }
  141.  
  142.  
  143. CloseFonts()
  144. {
  145. if (lfont) CloseFont(lfont);
  146. if (mfont) CloseFont(mfont);
  147. if (sfont) CloseFont(sfont);
  148. }
  149.  
  150. CloseLibraries()
  151. {
  152. if (IFFParseBase)  CloseLibrary(IFFParseBase);
  153. if (DiskfontBase)  CloseLibrary((struct Library *)DiskfontBase);
  154. if (GfxBase)        CloseLibrary(GfxBase);
  155. if (IntuitionBase) CloseLibrary(IntuitionBase);
  156. }
  157.  
  158. DeallocateBitmaps()
  159. {
  160. LONG i,x;
  161.  
  162. for (i=0;i<2;i++)
  163.     {
  164.     for (x=0;x<gi.de;x++)
  165.         if (bm1[i]->Planes[x]) FreeRaster(bm1[i]->Planes[x],gi.wi,gi.he);
  166.  
  167.     if (bm1[i]) FreeMem(bm1[i],sizeof(struct BitMap));
  168.     }
  169. }
  170.  
  171.  
  172. makerequest(string)
  173. UBYTE *string;
  174. {
  175.  
  176. struct EasyStruct es = 
  177. {
  178.  sizeof(struct EasyStruct),
  179.  NULL,
  180.  "AsteriodsII Error",
  181.  "%s",
  182.  "ok"
  183. };
  184.  
  185. EasyRequest(masterwindow,&es,NULL,string);
  186. }
  187.  
  188.  
  189. OpenLibraries()
  190. {
  191. IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",NULL);
  192. if (IntuitionBase == NULL) makerequest("IntuitionBase Error");
  193.  
  194. GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",NULL);
  195. if (GfxBase == NULL) makerequest("GfxBase Error");
  196.  
  197. DiskfontBase = (struct Library *) OpenLibrary("diskfont.library",NULL);
  198. if (DiskfontBase == NULL) makerequest("DiskfontBase Error");
  199.  
  200. IFFParseBase = (struct Library *)OpenLibrary("iffparse.library",37);
  201. if (IFFParseBase == NULL) makerequest("IFFParseBase Error");
  202.  
  203. if ((IntuitionBase==NULL)||(GfxBase==NULL)||(DiskfontBase==NULL)||(IFFParseBase==NULL))
  204.     {
  205.     CloseLibraries();
  206.     exit(NULL);
  207.     }
  208. }
  209.  
  210.  
  211. LoadFonts()
  212. {
  213. basic.ta_Name  = "basic.font";
  214. basic.ta_YSize = 32;
  215. basicfont = (struct TextFont *)OpenDiskFont(&basic);
  216. if (basicfont == NULL) makerequest("Basic.font Error");
  217. lfont = basicfont;
  218.  
  219. fixplain7.ta_Name  = "diamond.font";
  220. fixplain7.ta_YSize = 12;
  221. fixplain7font = (struct TextFont *)OpenDiskFont(&fixplain7);
  222. if (fixplain7font == NULL) makerequest("Diamond.font Error");
  223. mfont = fixplain7font;
  224.  
  225. hires.ta_Name  = "hires-5a.font";
  226. hires.ta_YSize = 8;
  227. hiresfont = (struct TextFont *)OpenDiskFont(&hires);
  228. if (hiresfont == NULL) makerequest("Hires-5a.font error");
  229. sfont = hiresfont;
  230.  
  231. if ((lfont == NULL) || (mfont == NULL) || (sfont == NULL))
  232.     {
  233.     makerequest("Copy Game Fonts to FONTS:");
  234.     CloseFonts();
  235.     CloseLibraries();
  236.     exit(NULL);
  237.     }
  238. }
  239.  
  240.  
  241.  
  242. LONG AllocateBitmaps()
  243. {
  244. LONG i,x;
  245.  
  246. for (i=0;i<2;i++)
  247.     {
  248.     bm1[i] = (struct BitMap *)AllocMem(sizeof(struct BitMap),MEMF_CLEAR);
  249.     InitBitMap(bm1[i],gi.de,gi.wi,gi.he);
  250.  
  251.     for (x=0;x<gi.de;x++)
  252.         {
  253.         bm1[i]->Planes[x] = (PLANEPTR)AllocRaster(gi.wi,gi.he);
  254.         if (bm1[i]->Planes[x] == NULL) return(-1);
  255.         else BltClear(bm1[i]->Planes[x],RASSIZE(gi.wi,gi.he),NULL);
  256.         }
  257.     }
  258.  
  259. }
  260.  
  261. LONG CreateDisplay()
  262. {
  263. LONG i,error;
  264.  
  265. screen = (struct Screen *)OpenScreenTags(NULL,
  266.                     SA_BlockPen,0,
  267.                     SA_DetailPen,0,
  268.                     SA_Width,gi.wi,
  269.                     SA_Height,gi.he,
  270.                     SA_Depth,gi.de,
  271.                     SA_DisplayID,gi.screentype,
  272.                     SA_Quiet,TRUE,
  273.                     SA_Overscan, OSCAN_TEXT,
  274.                     SA_BitMap,bm1[0],
  275.                     SA_ErrorCode,&error,
  276.                     TAG_DONE);
  277. if (screen == NULL)
  278.     {
  279.     makerequest("Unable to Open Screen");
  280.     return(-1);
  281.     }
  282. else
  283.     {
  284.     rp1[0] = &(screen->RastPort);
  285.     vp = &(screen->ViewPort);
  286.     ri1 = vp->RasInfo;
  287.     rp1[1] = (struct RastPort *)AllocMem(sizeof(struct RastPort),MEMF_CLEAR);
  288.     InitRastPort(rp1[1]);
  289.     rp1[1]->BitMap = bm1[1];
  290.  
  291.     newmasterwindow.Width = gi.wi;
  292.     newmasterwindow.Height= gi.he;
  293.     newmasterwindow.Screen = screen;
  294.     masterwindow = (struct Window *)OpenWindow(&newmasterwindow);
  295.     if (masterwindow == NULL)
  296.         {
  297.         makerequest("Unable to Open Window");
  298.         return(-1);
  299.         }
  300.     mwrp = masterwindow->RPort;
  301.     for (i=0;i<2;i++) SetDrMd(rp1[i],JAM1);
  302.  
  303.     pdata = (UWORD *)AllocMem(12,MEMF_CHIP|MEMF_CLEAR);
  304.     SetPointer(masterwindow,pdata,1,16,0,0);
  305.  
  306.     return(0);
  307.     }
  308. }
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315. changeview(bit)
  316. LONG bit;
  317. {
  318. ri1->BitMap = bm1[bit];
  319. MakeScreen(screen);
  320. RethinkDisplay();
  321. }
  322.  
  323.  
  324. SwapScreen()
  325. {
  326. LONG error;
  327.  
  328. ClearPointer(masterwindow);
  329. if (pdata) FreeMem(pdata,12);
  330. if (masterwindow) CloseWindow(masterwindow);
  331. if (screen) CloseScreen(screen);
  332. if (rp1[1]) FreeMem(rp1[1],sizeof(struct RastPort));
  333. DeallocateBitmaps();
  334.  
  335.  
  336. SetGameScreen();
  337.  
  338. error =  AllocateBitmaps();
  339. if (error == -1)
  340.     {
  341.     makerequest("Error Allocating Bitmap");
  342.     Cleanup();
  343.     }
  344.  
  345. error = CreateDisplay();
  346. if (error == -1)
  347.     {
  348.     makerequest("Error Changing Screen");
  349.     Cleanup();
  350.     }
  351.  
  352. SetRGB4(vp,0,0,0,0);
  353. SetRGB4(vp,1,15,15,15);
  354. SetRGB4(vp,2,5,7,7);
  355. SetRGB4(vp,3,15,10,10);
  356.  
  357. RethinkDisplay();
  358. }
  359.  
  360.  
  361. GetDefaults()
  362. {
  363. LONG x;
  364.  
  365. k.left = L;
  366. k.right= R;
  367. k.fire = F;
  368. k.thrust=T;
  369. k.hyperspace=P;
  370. k.pause= SPACE;
  371.  
  372. control.screentype = 0;
  373. control.fontsize = 0;
  374.  
  375. gi.largefontheight = 32;
  376. gi.mediumfontheight = 12;
  377. gi.smallfontheight = 8;
  378.  
  379. gi.de = 2;
  380. gi.wi = 640;
  381. gi.he = 440;
  382. gi.screentype = HIRESLACE_KEY;
  383.  
  384. control.delay = 42000;
  385. control.maxenemynum = 4;
  386. control.maxplayernum = 2;
  387. control.enemyonscreen = 3;
  388. control.playmode = 0;
  389. control.difficulty = 2;
  390.  
  391. #if REGISTERED == TRUE
  392.     control.game = 2;
  393. #else
  394.     control.game = 0;
  395. #endif
  396.  
  397. for(x=0;x<control.maxenemynum+control.playernum;x++)
  398.     ship[x].pilot = DESTROYED;
  399.  
  400. for(x=0;x<control.maxplayernum;x++)
  401.     {
  402.     control.wait[x] = 30;
  403.     control.firedelay[x] = 4;
  404.     }
  405. saucer.flag = FALSE;
  406. control.input[0] = 0;
  407. control.input[1] = 1;
  408. control.playernum = 1;
  409. control.asteroidnum = 16;
  410. control.explosionnum = 32;
  411. control.hypernum = 40;
  412. control.boxnum = 20;
  413. control.thrustlength = 17;
  414. control.ftrnum = 8;
  415. control.minenum = 40;
  416. control.debrisnum = 60;
  417. control.battleshipnum = 1;
  418. control.startlevel = 1;
  419. control.standarddebris = 16;
  420. control.audio = TRUE;
  421. }
  422.  
  423.  
  424.  
  425. SetGameFont()
  426. {
  427. if (control.fontsize == 0)
  428.     {
  429.     gi.smallfontheight = 8;
  430.     hiresfont = sfont;
  431.     }
  432. else
  433. if (control.fontsize == 1)
  434.     {
  435.     gi.smallfontheight = 12;
  436.     hiresfont = mfont;
  437.     }
  438. }
  439.  
  440.  
  441. SetGameScreen()
  442. {
  443. LONG error;
  444.  
  445. if (control.screentype == 0)
  446.     {
  447.     gi.de = 2;
  448.     error = GetWorkbenchData();
  449.     if (error == -1)
  450.         {
  451.         makerequest("Cant Find Public Screen");
  452.         control.screentype = 1;
  453.         }
  454.     }
  455.  
  456. if (control.screentype == 1)
  457.     {
  458.     gi.de = 2;
  459.     gi.wi = 640;
  460.     gi.he = 440;
  461.     gi.screentype = HIRESLACE_KEY;
  462.     }
  463.  
  464. if (control.screentype == 2)
  465.     {
  466.     gi.de = 2;
  467.     gi.wi = 640;
  468.     gi.he = 520;
  469.     gi.screentype = HIRESLACE_KEY | PAL_MONITOR_ID;
  470.     }
  471.  
  472. if (control.screentype == 3)
  473.     {
  474.     gi.de = 2;
  475.     gi.wi = 1280;
  476.     gi.he = 440;
  477.     gi.screentype = SUPERLACE_KEY;
  478.     }
  479.  
  480. if (control.screentype == 4)
  481.     {
  482.     gi.de = 2;
  483.     gi.wi = 1280;
  484.     gi.he = 520;
  485.     gi.screentype = SUPERLACE_KEY | PAL_MONITOR_ID;
  486.     }
  487.  
  488.  
  489. gi.x1 = 45;
  490. gi.y1 = 45;
  491. gi.x2 = gi.wi-45;
  492. gi.y2 = gi.he-45;
  493. gi.dx = gi.x2-gi.x1;
  494. gi.dy = gi.y2-gi.y1;
  495. }
  496.  
  497.  
  498. LONG GetWorkbenchData()
  499. {
  500. struct Screen *clone;
  501. UBYTE *name = "Workbench";
  502.  
  503. clone = (struct Screen *)LockPubScreen(name);
  504.  
  505. if (clone == NULL) return(-1);
  506. else
  507.     {
  508.     gi.screentype = GetVPModeID(&(clone->ViewPort));
  509.     gi.wi = clone->Width;
  510.     gi.he = clone->Height;
  511.     }
  512.  
  513. UnlockPubScreen(name,clone);
  514. }
  515.