home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 446.lha / Port2 / Port2.c < prev    next >
C/C++ Source or Header  |  1990-12-03  |  8KB  |  364 lines

  1. /* $Revision Header * Header built automatically - do not edit! *************
  2.  *
  3.  *    (C) Copyright 1990 by MXM
  4.  *
  5.  *    Name .....: Port2.c
  6.  *    Created ..: Thursday 02-Aug-90 13:59
  7.  *    Revision .: 0
  8.  *
  9.  *    Date            Author          Comment
  10.  *    =========       ========        ====================
  11.  *    02-Aug-90       Olsen           Created this file!
  12.  *
  13.  * $Revision Header ********************************************************/
  14.  
  15.     /* Included header files. */
  16.  
  17. #define __NO_PRAGMAS 1
  18.  
  19. #include <intuition/intuitionbase.h>
  20. #include <libraries/dosextens.h>
  21. #include <devices/inputevent.h>
  22. #include <devices/gameport.h>
  23. #include <graphics/gfxbase.h>
  24. #include <graphics/sprite.h>
  25. #include <devices/input.h>
  26. #include <exec/memory.h>
  27. #include <functions.h>
  28.  
  29.     /* Global symbols. */
  30.  
  31. struct IntuitionBase    *IntuitionBase;
  32. struct GfxBase        *GfxBase;
  33. struct Window        *Window;
  34. struct MsgPort        *GamePort;
  35. struct IOStdReq        *GameReq;
  36. struct MsgPort        *InputPort;
  37. struct IOStdReq        *InputReq;
  38. struct InputEvent    *InputEvent;
  39. struct SimpleSprite     PointerSprite;
  40. USHORT             *PointerImage;
  41. struct Preferences    *Preferences;
  42. BYTE             ControllerType = GPCT_NOCONTROLLER;
  43.  
  44.     /* Signal bits. */
  45.  
  46. #define SIG_GAMEPORT    (1 << GamePort -> mp_SigBit)
  47. #define SIG_WINDOW    (1 << Window -> UserPort -> mp_SigBit)
  48.  
  49.     /* Delta movement values we wish to be signalled. */
  50.  
  51. #define XMOVE 1
  52. #define YMOVE 1
  53.  
  54.     /* Window title and width of the window. */
  55.  
  56. #define NAME "Port2"
  57. #define WIDTH (84 + (sizeof(NAME) - 1) * 8)
  58.  
  59.     /* New window structure. */
  60.  
  61. struct NewWindow NewWindow =
  62. {
  63.     0,11,
  64.     WIDTH,10,
  65.     0,1,
  66.     CLOSEWINDOW,
  67.     RMBTRAP | WINDOWCLOSE | WINDOWDRAG | WINDOWDEPTH,
  68.     (struct Gadget *)NULL,
  69.     (struct Image *)NULL,
  70.     (STRPTR)NAME,
  71.     (struct Screen *)NULL,
  72.     (struct BitMap *)NULL,
  73.     0,0,
  74.     0,0,
  75.     WBENCHSCREEN
  76. };
  77.  
  78.     /* A GameTrigger structure. */
  79.  
  80. struct GamePortTrigger GameTrigger =
  81. {
  82.     GPTF_UPKEYS|GPTF_DOWNKEYS,    /* Key events. */
  83.     ~0,                /* Event timeout (very large). */
  84.     XMOVE,                /* Movement. */
  85.     YMOVE
  86. };
  87.  
  88.     /* Stub routines. */
  89.  
  90. VOID _cli_parse(){}
  91. VOID _wb_parse(){}
  92. LONG Chk_Abort(VOID) { return(0); }
  93.  
  94.     /* CloseAll(BYTE ExitCode):
  95.      *
  96.      *    Close all resources and exit.
  97.      */
  98.  
  99. VOID
  100. CloseAll(BYTE ExitCode)
  101. {
  102.         /* If we have allocated a controller, release the
  103.          * port.
  104.          */
  105.  
  106.     if(ControllerType == GPCT_MOUSE)
  107.     {
  108.         ControllerType = GPCT_NOCONTROLLER;
  109.  
  110.         GameReq -> io_Command    = GPD_SETCTYPE;
  111.         GameReq -> io_Length    = 1;
  112.         GameReq -> io_Flags    = IOF_QUICK;
  113.         GameReq -> io_Data    = (APTR)&ControllerType;
  114.  
  115.         DoIO(GameReq);
  116.     }
  117.  
  118.         /* Release the sprite. */
  119.  
  120.     if(PointerSprite . num != -1)
  121.         FreeSprite(PointerSprite . num);
  122.  
  123.     if(Window)
  124.         CloseWindow(Window);
  125.  
  126.     if(InputEvent)
  127.         FreeMem(InputEvent,sizeof(struct InputEvent));
  128.  
  129.     if(PointerImage)
  130.         FreeMem(PointerImage,POINTERSIZE * sizeof(USHORT));
  131.  
  132.     if(Preferences)
  133.         FreeMem(Preferences,sizeof(struct Preferences));
  134.  
  135.     if(GameReq)
  136.     {
  137.         if(GameReq -> io_Device)
  138.         {
  139.             GameReq -> io_Command = CMD_CLEAR;
  140.             DoIO(GameReq);
  141.  
  142.             CloseDevice(GameReq);
  143.         }
  144.  
  145.         DeleteStdIO(GameReq);
  146.     }
  147.  
  148.     if(GamePort)
  149.         DeletePort(GamePort);
  150.  
  151.     if(InputReq)
  152.     {
  153.         if(InputReq -> io_Device)
  154.             CloseDevice(InputReq);
  155.  
  156.         DeleteStdIO(InputReq);
  157.     }
  158.  
  159.     if(InputPort)
  160.         DeletePort(InputPort);
  161.  
  162.     if(GfxBase)
  163.         CloseLibrary(GfxBase);
  164.  
  165.     if(IntuitionBase)
  166.         CloseLibrary(IntuitionBase);
  167.  
  168.     exit(ExitCode);
  169. }
  170.  
  171.     /* OpenAll():
  172.      *
  173.      *    Open all the required resources.
  174.      */
  175.  
  176. VOID
  177. OpenAll()
  178. {
  179.     if(!(IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",0)))
  180.         CloseAll(RETURN_FAIL + 0);
  181.  
  182.     if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",0)))
  183.         CloseAll(RETURN_FAIL + 1);
  184.  
  185.     if(!(GamePort = (struct MsgPort *)CreatePort(NULL,0)))
  186.         CloseAll(RETURN_FAIL + 2);
  187.  
  188.     if(!(GameReq = (struct IOStdReq *)CreateStdIO(GamePort)))
  189.         CloseAll(RETURN_FAIL + 3);
  190.  
  191.     if(OpenDevice("gameport.device",1,GameReq,0))
  192.         CloseAll(RETURN_FAIL + 4);
  193.  
  194.     if(!(InputPort = (struct MsgPort *)CreatePort(NULL,0)))
  195.         CloseAll(RETURN_FAIL + 5);
  196.  
  197.     if(!(InputReq = (struct IOStdReq *)CreateStdIO(InputPort)))
  198.         CloseAll(RETURN_FAIL + 6);
  199.  
  200.     if(OpenDevice("input.device",0,InputReq,0))
  201.         CloseAll(RETURN_FAIL + 7);
  202.  
  203.     if(!(Preferences = (struct Preferences *)AllocMem(sizeof(struct Preferences),MEMF_PUBLIC | MEMF_CLEAR)))
  204.         CloseAll(RETURN_FAIL + 8);
  205.  
  206.     if(!(PointerImage = (USHORT *)AllocMem(POINTERSIZE * sizeof(USHORT),MEMF_PUBLIC | MEMF_CHIP | MEMF_CLEAR)))
  207.         CloseAll(RETURN_FAIL + 9);
  208.  
  209.     if(!(InputEvent = (struct InputEvent *)AllocMem(sizeof(struct InputEvent),MEMF_PUBLIC | MEMF_CLEAR)))
  210.         CloseAll(RETURN_FAIL + 10);
  211.  
  212.     if(!(Window = (struct Window *)OpenWindow(&NewWindow)))
  213.         CloseAll(RETURN_FAIL + 11);
  214.  
  215.         /* Get the current system preferences. */
  216.  
  217.     GetPrefs(Preferences,sizeof(struct Preferences));
  218.  
  219.         /* Copy pointer image to user buffer. */
  220.  
  221.     CopyMem(Preferences -> PointerMatrix,PointerImage,POINTERSIZE * sizeof(USHORT));
  222.  
  223.         /* Set up the sprite control structure. */
  224.  
  225.     PointerSprite . posctldata    = PointerImage;
  226.     PointerSprite . height        = 16;
  227.  
  228.         /* Allocate a hardware sprite. */
  229.  
  230.     if((PointerSprite . num = GetSprite(&PointerSprite,1)) == -1)
  231.         CloseAll(RETURN_FAIL + 12);
  232.  
  233.         /* Check if we are able to allocate the second controller
  234.          * port.
  235.          */
  236.  
  237.     GameReq -> io_Command    = GPD_ASKCTYPE;
  238.     GameReq -> io_Length    = 1;
  239.     GameReq -> io_Flags    = IOF_QUICK;
  240.     GameReq -> io_Data    = (APTR)&ControllerType;
  241.  
  242.     Forbid();
  243.  
  244.     DoIO(GameReq);
  245.  
  246.         /* The port is already allocated. */
  247.  
  248.     if(ControllerType != GPCT_NOCONTROLLER)
  249.     {
  250.         Permit();
  251.         CloseAll(RETURN_FAIL + 13);
  252.     }
  253.  
  254.         /* Install a mouse type controller. */
  255.  
  256.     ControllerType = GPCT_MOUSE;
  257.  
  258.     GameReq -> io_Command    = GPD_SETCTYPE;
  259.     GameReq -> io_Length    = 1;
  260.     GameReq -> io_Flags    = IOF_QUICK;
  261.     GameReq -> io_Data    = (APTR)&ControllerType;
  262.  
  263.     DoIO(GameReq);
  264.  
  265.     Permit();
  266.  
  267.         /* Start queueing. */
  268.  
  269.     GameReq -> io_Command    = GPD_SETTRIGGER;
  270.     GameReq -> io_Length    = sizeof(struct GamePortTrigger);
  271.     GameReq -> io_Data    = (APTR)&GameTrigger;
  272.  
  273.     DoIO(GameReq);
  274.  
  275.         /* Initialize IORequests. */
  276.  
  277.     GameReq -> io_Command    = GPD_READEVENT;
  278.     GameReq -> io_Length    = sizeof(struct InputEvent);
  279.     GameReq -> io_Data    = (APTR)InputEvent;
  280.  
  281.     InputReq -> io_Command    = IND_WRITEEVENT;
  282.     InputReq -> io_Length    = sizeof(struct InputEvent);
  283.     InputReq -> io_Data    = (APTR)InputEvent;
  284. }
  285.  
  286.     /* main():
  287.      *
  288.      *    The main program. 
  289.      */
  290.  
  291. VOID
  292. main()
  293. {
  294.     LONG    PointerX = 0,PointerY = 0;
  295.     ULONG    SignalSet;
  296.  
  297.     OpenAll();
  298.  
  299.         /* Read the first controller event. */
  300.  
  301.     SendIO(GameReq);
  302.  
  303.     FOREVER
  304.     {
  305.         SignalSet = Wait(SIG_GAMEPORT | SIG_WINDOW);
  306.  
  307.             /* Close the window? */
  308.  
  309.         if(SignalSet & SIG_WINDOW)
  310.             CloseAll(RETURN_OK);
  311.  
  312.             /* Remove queued messages. */
  313.  
  314.         while(GetMsg(GamePort));
  315.  
  316.             /* Mouse move event. */
  317.  
  318.         if(InputEvent -> ie_X || InputEvent -> ie_Y)
  319.         {
  320.                 /* Calculate delta values. */
  321.  
  322.             PointerX += InputEvent -> ie_X * 2;
  323.             PointerY += InputEvent -> ie_Y * 2;
  324.  
  325.                 /* Avoid view limits. */
  326.  
  327.             if(PointerX < 0)
  328.                 PointerX = 0;
  329.  
  330.             if(PointerY < 0)
  331.                 PointerY = 0;
  332.  
  333.             if(PointerX > 639)
  334.                 PointerX = 639;
  335.  
  336.             if(PointerY > 511)
  337.                 PointerY = 511;
  338.  
  339.                 /* Move second mouse pointer. */
  340.  
  341.             MoveSprite(NULL,&PointerSprite,(PointerX >> 1) + Preferences -> XOffset,(PointerY >> 1) + Preferences -> YOffset);
  342.         }
  343.  
  344.             /* Hit a mouse button, move the real mouse pointer
  345.              * to the current sprite position.
  346.              */
  347.  
  348.         if(InputEvent -> ie_Code != IECODE_NOBUTTON)
  349.         {
  350.             InputEvent -> ie_Qualifier    &= ~IEQUALIFIER_RELATIVEMOUSE;
  351.  
  352.             InputEvent -> ie_Class         = IECLASS_POINTERPOS;
  353.             InputEvent -> ie_X         = PointerX;
  354.             InputEvent -> ie_Y         = PointerY;
  355.  
  356.             DoIO(InputReq);
  357.         }
  358.  
  359.             /* Read the next event. */
  360.  
  361.         SendIO(GameReq);
  362.     }
  363. }
  364.