home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d0xx / d033 / memview.lha / MemView / memview.c next >
Encoding:
C/C++ Source or Header  |  1986-09-01  |  4.2 KB  |  224 lines

  1. /*  :ts=8 bk=0
  2.  * memview:  A window into memory.
  3.  * Jon would call this a parlor trick, too.
  4.  *
  5.  * Leo L. Schwab        8608.5
  6.  */
  7.  
  8. /*  I shouldn't have to include graphics/copper.h myself  */
  9. #include <exec/types.h>
  10. #include <exec/memory.h>
  11. #include <graphics/gfxbase.h>
  12. #include <graphics/copper.h>
  13. #include <graphics/view.h>
  14. #include <graphics/rastport.h>
  15. #include <devices/gameport.h>
  16. #include <devices/inputevent.h>
  17.  
  18. #define REV        0L
  19. #define DEPTH        1L
  20. #define WIDTH        320L
  21. #define    HEIGHT        200L
  22. #define ever        (;;)
  23.  
  24. extern void    *OpenLibrary(), *AllocMem(), *GetColorMap(), *CreateStdIO(),
  25.         *CreatePort();
  26. extern long    OpenDevice(), DoIO();
  27.  
  28.  
  29. struct GamePortTrigger    gpt = {
  30.     GPTF_UPKEYS | GPTF_DOWNKEYS,
  31.     0,
  32.     1, 1
  33. };
  34. UWORD        colors[] = { 0, 0xfff };
  35.  
  36. struct View    v, *oldview;
  37. struct ViewPort    vp;
  38. struct ColorMap    *cm;
  39. struct RasInfo    ri;
  40. struct BitMap    *bm;
  41. struct GfxBase    *GfxBase;
  42. struct InputEvent    joyreport;
  43. struct IOStdReq    *gameio;
  44. struct MsgPort    *gameport;
  45.  
  46.  
  47.  
  48. main ()
  49. {
  50.     int inc = 40, lx;
  51.  
  52.     openstuff ();
  53.     makescreen ();        /*  NOT Intuition call  */
  54.     initjoystick ();
  55.  
  56.     SendIO (gameio);
  57.     for ever {
  58.         WaitIO (gameio);
  59.         if (joyreport.ie_Code == IECODE_LBUTTON)
  60.             /*  Fire button pressed; exit program  */
  61.             break;
  62.  
  63.         if (joyreport.ie_X != lx) {
  64.             lx = joyreport.ie_X;
  65.             if (joyreport.ie_X < 0)
  66.                 inc = 40;
  67.             else if (joyreport.ie_X > 0)
  68.                 inc = 80;
  69.             if (lx)
  70.                 remakedisp (inc);
  71.         }
  72.  
  73.         if (joyreport.ie_Y) {
  74.             if (joyreport.ie_Y > 0)
  75.                 bm -> Planes[0] += inc;
  76.             else if (joyreport.ie_Y < 0)
  77.                 bm -> Planes[0] -= inc;
  78.             WaitTOF ();
  79.             ScrollVPort (&vp);
  80.         }
  81.         SendIO (gameio);
  82.     }
  83.     closeeverything ();
  84. }
  85.  
  86.  
  87. openstuff ()
  88. {
  89.     long err;
  90.  
  91.     if (!(GfxBase = OpenLibrary ("graphics.library", REV)))
  92.         die ("Art shop closed.\n");
  93.  
  94.     if (!(gameport = CreatePort (0L, 0L)))
  95.         die ("Can't make msgport.\n");
  96.  
  97.     if (!(gameio = CreateStdIO (gameport)))
  98.         die ("Can't make IO packet.\n");
  99.  
  100.     if (err = OpenDevice ("gameport.device", 1L, gameio, 0L))
  101.         die ("Games closed.\n");
  102.  
  103.     if (!(bm = AllocMem ((long) sizeof (*bm), MEMF_CHIP | MEMF_CLEAR)))
  104.         die ("Can't allocate BitMap.\n");
  105. }
  106.  
  107. makescreen ()
  108. {
  109.     InitView (&v);
  110.     InitVPort (&vp);
  111.     InitBitMap (bm, DEPTH, WIDTH, HEIGHT);
  112.  
  113.     v.ViewPort = &vp;
  114.  
  115.     ri.BitMap = bm;
  116.     ri.RxOffset = ri.RyOffset = ri.Next = NULL;
  117.  
  118.     vp.DWidth = WIDTH;
  119.     vp.DHeight = HEIGHT;
  120.     vp.RasInfo = &ri;
  121.     vp.ColorMap = GetColorMap (2L);
  122.  
  123.     bm -> Planes[0] = NULL;    /*  Start looking at address 0  */
  124.  
  125.     MakeVPort (&v, &vp);
  126.     MrgCop (&v);
  127.     LoadRGB4 (&vp, colors, 2L);
  128.     oldview = GfxBase -> ActiView;
  129.     LoadView (&v);
  130. }
  131.  
  132. closeeverything ()
  133. {
  134.     register int i;
  135.  
  136.     if (oldview) {
  137.         LoadView (oldview);
  138.         WaitTOF ();    /*  Make sure copper is using old view  */
  139.         FreeVPortCopLists (&vp);
  140.         FreeCprList (v.LOFCprList);
  141.     }
  142.     if (vp.ColorMap)
  143.         FreeColorMap (vp.ColorMap);
  144.     if (bm)
  145.         FreeMem (bm, (long) sizeof (*bm));
  146.     if (gameio) {
  147.         if (gameio -> io_Device)
  148.             CloseDevice (gameio);
  149.         DeleteStdIO (gameio);
  150.     }
  151.     if (gameport)
  152.         DeletePort (gameport);
  153.     if (GfxBase)
  154.         CloseLibrary (GfxBase);
  155. }
  156.  
  157. die (str)
  158. char *str;
  159. {
  160.     puts (str);
  161.     closeeverything ();
  162.     exit (100);
  163. }
  164.  
  165. initjoystick ()
  166. {
  167.     UBYTE type = GPCT_RELJOYSTICK;
  168.  
  169.     gameio -> io_Command = GPD_SETCTYPE;
  170.     gameio -> io_Length = 1;
  171.     gameio -> io_Data = &type;
  172.     if (DoIO (gameio))
  173.         die ("Error in setting controller type.\n");
  174.  
  175.     gameio -> io_Command = GPD_SETTRIGGER;
  176.     gameio -> io_Length = sizeof (gpt);
  177.     gameio -> io_Data = &gpt;
  178.     if (DoIO (gameio))
  179.         die ("Error in setting trigger values.\n");
  180.  
  181.     gameio -> io_Command = GPD_READEVENT;
  182.     gameio -> io_Length = sizeof (joyreport);
  183.     gameio -> io_Data = &joyreport;
  184. }
  185.  
  186. remakedisp (line)
  187. int line;
  188. {
  189.     void *sav1, *sav2;
  190.  
  191.     LoadView (oldview);
  192.     WaitTOF ();    /*  Make sure copper is using old view  */
  193.     FreeVPortCopLists (&vp);
  194.     FreeCprList (v.LOFCprList);
  195.     sav1 = bm -> Planes[0];
  196.     sav2 = vp.ColorMap;
  197.  
  198.     InitView (&v);
  199.     InitVPort (&vp);
  200.  
  201.     v.ViewPort = &vp;
  202.  
  203.     vp.DHeight = HEIGHT;
  204.     vp.RasInfo = &ri;
  205.     vp.ColorMap = sav2;
  206.  
  207.     if (line == 80) {
  208.         InitBitMap (bm, DEPTH, WIDTH+WIDTH, HEIGHT);
  209.         vp.Modes |= HIRES;
  210.         vp.DWidth = WIDTH + WIDTH;
  211.     } else {
  212.         InitBitMap (bm, DEPTH, WIDTH, HEIGHT);
  213.         vp.Modes &= ~HIRES;
  214.         vp.DWidth = WIDTH;
  215.     }
  216.     bm -> Planes[0] = sav1;
  217.  
  218.     MakeVPort (&v, &vp);
  219.     MrgCop (&v);
  220.     LoadRGB4 (&vp, colors, 2L);
  221.     oldview = GfxBase -> ActiView;
  222.     LoadView (&v);
  223. }
  224.