home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #6 / amigamamagazinepolishissue1998.iso / coders / jËzyki_programowania / amigae / e_v3.2a / rkrmsrc / gameport / absolute_joystick.e < prev   
Text File  |  1977-12-31  |  9KB  |  292 lines

  1. -> Absolute_Joystick.e - Gameport device absolute joystick example
  2.  
  3. ->>> Header (globals)
  4. MODULE 'amigalib/ports',
  5.        'amigalib/io',
  6.        'devices/gameport',
  7.        'devices/inputevent',
  8.        'devices/timer',
  9.        'exec/execbase',
  10.        'exec/io',
  11.        'exec/nodes',
  12.        'exec/ports'
  13.  
  14. ENUM ERR_NONE, ERR_DEV, ERR_IO, ERR_PORT
  15.  
  16. RAISE ERR_DEV IF OpenDevice()<>0
  17.  
  18. CONST JOY_X_DELTA=1, JOY_Y_DELTA=1, TIMEOUT_SECONDS=10
  19.  
  20. DEF exec:PTR TO execbase
  21. ->>>
  22.  
  23. ->>> PROC printInstructions()
  24. -> Routine to print out some information for the user.
  25. PROC printInstructions()
  26.   WriteF('\n >>> gameport.device Absolute Joystick Demo <<<\n\n')
  27.  
  28.   IF exec.vblankfrequency=60
  29.     WriteF(' Running on NTSC system (60 Hz).\n')
  30.   ELSEIF exec.vblankfrequency=50
  31.     WriteF(' Running on PAL system (50 Hz).\n')
  32.   ENDIF
  33.  
  34.   WriteF(' Attach joystick to rear connector (A3000) and (A1000).\n' +
  35.          ' Attach joystick to right connector (A2000).\n' +
  36.          ' Attach joystick to left connector (A500).\n' +
  37.          ' Then move joystick and click its button(s).\n\n' +
  38.          ' To EXIT program press and release fire button 3 consecutive ' +
  39.                'times.\n' +
  40.          ' The program also exits if no activity occurs for 1 minute.\n\n')
  41. ENDPROC
  42. ->>>
  43.  
  44. ->>> PROC check_move(game_event:PTR TO inputevent)
  45. -> Print out information on the event received.
  46. PROC check_move(game_event:PTR TO inputevent)
  47.   DEF xmove, ymove, timeout=FALSE
  48.   xmove:=game_event.x
  49.   ymove:=game_event.y
  50.  
  51.   IF xmove=1
  52.     IF ymove=1
  53.       WriteF('RIGHT DOWN\n')
  54.     ELSEIF ymove=0
  55.       WriteF('RIGHT\n')
  56.     ELSEIF ymove=-1
  57.       WriteF('RIGHT UP\n')
  58.     ELSE
  59.       WriteF('UNKNOWN Y\n')
  60.     ENDIF
  61.   ELSEIF xmove=-1
  62.     IF ymove=1
  63.       WriteF('LEFT DOWN\n')
  64.     ELSEIF ymove=0
  65.       WriteF('LEFT\n')
  66.     ELSEIF ymove=-1
  67.       WriteF('LEFT UP\n')
  68.     ELSE
  69.       WriteF('UNKNOWN Y\n')
  70.     ENDIF
  71.   ELSEIF xmove=0
  72.     IF ymove=1
  73.       WriteF('DOWN\n')
  74.     ELSEIF ymove=0
  75.       -> Note that 0,0 can be a timeout, or a direction release.
  76.       IF game_event.timestamp.secs >= (exec.vblankfrequency*TIMEOUT_SECONDS)
  77.         -> Under 1.3 (V34) and earlier versions of the Amiga OS, the
  78.         -> timestamp.secs field used in the IF statement above is not
  79.         -> correctly filled in.  Therefore, this program cannot tell the
  80.         -> difference between a release event and a timeout under 1.3 (release
  81.         -> events will be reported as timeouts).
  82.         WriteF('TIMEOUT\n')
  83.         timeout:=TRUE
  84.       ELSE
  85.         WriteF('RELEASE\n')
  86.       ENDIF
  87.     ELSEIF ymove=-1
  88.       WriteF('UP\n')
  89.     ELSE
  90.       WriteF('UNKNOWN Y\n')
  91.     ENDIF
  92.   ELSE
  93.     WriteF('UNKNOWN X ')
  94.     IF ymove=1
  95.       WriteF('unknown action\n')
  96.     ELSEIF ymove=0
  97.       WriteF('unknown action\n')
  98.     ELSEIF ymove=-1
  99.       WriteF('unknown action\n')
  100.     ELSE
  101.       WriteF('UNKNOWN Y\n')
  102.     ENDIF
  103.   ENDIF
  104. ENDPROC timeout
  105. ->>>
  106.  
  107. ->>> PROC send_read_request(game_event, game_io_msg:PTR TO iostd)
  108. -> Send a request to the gameport to read an event.
  109. PROC send_read_request(game_event, game_io_msg:PTR TO iostd)
  110.   game_io_msg.command:=GPD_READEVENT
  111.   game_io_msg.flags:=0
  112.   game_io_msg.data:=game_event
  113.   game_io_msg.length:=SIZEOF inputevent
  114.   SendIO(game_io_msg)  -> Asynchronous - message will return later
  115. ENDPROC
  116. ->>>
  117.  
  118. ->>> PROC processEvents(game_io_msg:PTR TO iostd, game_msg_port:PTR TO mp)
  119. -> Simple loop to process gameport events.
  120. PROC processEvents(game_io_msg:PTR TO iostd, game_msg_port:PTR TO mp)
  121.   DEF timeout, timeouts, button_count, not_finished, code,
  122.       game_event:inputevent  -> Where input event will be stored
  123.   -> From now on, just read input events into the event buffer,
  124.   -> one at a time.  READEVENT waits for the preset conditions.
  125.   timeouts:=0
  126.   button_count:=0
  127.   not_finished:=TRUE
  128.  
  129.   WHILE (timeouts<6) AND not_finished
  130.     -> Send the read request
  131.     send_read_request(game_event, game_io_msg)
  132.  
  133.     -> Wait for joystick action
  134.     Wait(Shl(1, game_msg_port.sigbit))
  135.     WHILE NIL<>GetMsg(game_msg_port)
  136.       timeout:=FALSE
  137.       code:=game_event.code
  138.       SELECT code
  139.       CASE IECODE_LBUTTON
  140.         WriteF(' FIRE BUTTON PRESSED \n')
  141.       CASE IECODE_LBUTTON OR IECODE_UP_PREFIX
  142.         WriteF(' FIRE BUTTON RELEASED \n')
  143.         button_count++
  144.         IF 3=button_count THEN not_finished:=FALSE
  145.       CASE IECODE_RBUTTON
  146.         WriteF(' ALT BUTTON PRESSED \n')
  147.         button_count:=0
  148.       CASE IECODE_RBUTTON OR IECODE_UP_PREFIX
  149.         WriteF(' ALT BUTTON RELEASED \n')
  150.         button_count:=0
  151.       CASE IECODE_NOBUTTON
  152.         -> Check for change in position
  153.         timeout:=check_move(game_event)
  154.         button_count:=0
  155.       DEFAULT
  156.       ENDSELECT
  157.  
  158.       IF timeout
  159.         INC timeouts
  160.       ELSE
  161.         timeouts:=0
  162.       ENDIF
  163.     ENDWHILE
  164.   ENDWHILE
  165. ENDPROC
  166. ->>>
  167.  
  168. ->>> PROC set_controller_type(type, game_io_msg:PTR TO iostd)
  169. -> Allocate the controller if it is available.  You allocate the controller by
  170. -> setting its type to something other than GPCT_NOCONTROLLER.  Before you
  171. -> allocate the thing you need to check if anyone else is using it (it is free
  172. -> if it is set to GPCT_NOCONTROLLER).
  173. PROC set_controller_type(type, game_io_msg:PTR TO iostd)
  174.   DEF success=FALSE, controller_type_addr
  175.   controller_type_addr:=[0]:CHAR
  176.   -> Begin critical section.
  177.   -> We need to be sure that between the time we check that the controller is
  178.   -> available and the time we allocate it, no one else steals it.
  179.   Forbid()
  180.  
  181.   game_io_msg.command:=GPD_ASKCTYPE  -> Enquire current status
  182.   game_io_msg.flags:=IOF_QUICK
  183.   game_io_msg.data:=controller_type_addr  -> Put answer in here
  184.   game_io_msg.length:=1
  185.   DoIO(game_io_msg)
  186.  
  187.   -> No one is using this device unit, let's claim it
  188.   IF controller_type_addr[]=GPCT_NOCONTROLLER
  189.     game_io_msg.command:=GPD_SETCTYPE
  190.     game_io_msg.flags:=IOF_QUICK
  191.     game_io_msg.data:=[type]:CHAR
  192.     game_io_msg.length:=1
  193.     DoIO( game_io_msg)
  194.     success:=TRUE
  195.   ENDIF
  196.  
  197.   Permit()  -> Critical section end
  198. ENDPROC success
  199. ->>>
  200.  
  201. ->>> PROC set_trigger_conditions(gpt:PTR TO gameporttrigger, game_io_msg:...)
  202. -> Tell the gameport when to trigger.
  203. PROC set_trigger_conditions(gpt:PTR TO gameporttrigger,
  204.                             game_io_msg:PTR TO iostd)
  205.   -> Trigger on all joystick key transitions
  206.   gpt.keys:=GPTF_UPKEYS OR GPTF_DOWNKEYS
  207.   gpt.xdelta:=JOY_X_DELTA
  208.   gpt.ydelta:=JOY_Y_DELTA
  209.   -> Timeout trigger every TIMEOUT_SECONDS second(s)
  210.   gpt.timeout:=exec.vblankfrequency*TIMEOUT_SECONDS
  211.  
  212.   game_io_msg.command:=GPD_SETTRIGGER
  213.   game_io_msg.flags:=IOF_QUICK
  214.   game_io_msg.data:=gpt
  215.   game_io_msg.length:=SIZEOF gameporttrigger
  216.   DoIO(game_io_msg)
  217. ENDPROC
  218. ->>>
  219.  
  220. ->>> PROC flush_buffer(game_io_msg:PTR TO iostd)
  221. -> Clear the buffer.  Do this before you begin to be sure you start in a known
  222. -> state.
  223. PROC flush_buffer(game_io_msg:PTR TO iostd)
  224.   game_io_msg.command:=CMD_CLEAR
  225.   game_io_msg.flags:=IOF_QUICK
  226.   game_io_msg.data:=NIL
  227.   game_io_msg.length:=0
  228.   DoIO(game_io_msg)
  229. ENDPROC
  230. ->>>
  231.  
  232. ->>> PROC free_gp_unit(game_io_msg:PTR TO iostd)
  233. -> Free the unit by setting its type back to GPCT_NOCONTROLLER.
  234. PROC free_gp_unit(game_io_msg:PTR TO iostd)
  235.   DEF type=GPCT_NOCONTROLLER
  236.   game_io_msg.command:=GPD_SETCTYPE
  237.   game_io_msg.flags:=IOF_QUICK
  238.   game_io_msg.data:=[type]:CHAR
  239.   game_io_msg.length:=1;
  240.   DoIO(game_io_msg)
  241. ENDPROC
  242. ->>>
  243.  
  244. ->>> PROC main()
  245. -> Allocate everything and go.  On failure, free any resources that have been
  246. -> allocated.
  247. PROC main() HANDLE
  248.   DEF joytrigger:gameporttrigger, game_io_msg=NIL:PTR TO iostd,
  249.       game_msg_port=NIL, open_dev=FALSE
  250.   -> E-Note: get the right type for exec
  251.   exec:=execbase
  252.   -> Create port for gameport device communications
  253.   IF NIL=(game_msg_port:=createPort('RKM_game_port', 0))
  254.     Raise(ERR_PORT)
  255.   ENDIF
  256.   -> Create message block for device IO
  257.   IF NIL=(game_io_msg:=createExtIO(game_msg_port, SIZEOF iostd))
  258.     Raise(ERR_IO)
  259.   ENDIF
  260.  
  261.   game_io_msg.mn.ln.type:=NT_UNKNOWN
  262.   -> Open the right/back (unit 1, number 2) gameport.device unit
  263.   OpenDevice('gameport.device', 1, game_io_msg, 0)
  264.   open_dev:=TRUE
  265.   -> Set controller type to joystick
  266.   IF set_controller_type(GPCT_ABSJOYSTICK, game_io_msg)
  267.     -> Specify the trigger conditions
  268.     set_trigger_conditions(joytrigger, game_io_msg)
  269.  
  270.     printInstructions()
  271.  
  272.     -> Clear device buffer to start from a known state.
  273.     -> There might still be events left.
  274.     flush_buffer(game_io_msg)
  275.  
  276.     processEvents(game_io_msg, game_msg_port)
  277.  
  278.     -> Free gameport unit so other applications can use it!
  279.     free_gp_unit(game_io_msg)
  280.   ENDIF
  281. EXCEPT DO
  282.   IF open_dev THEN CloseDevice(game_io_msg)
  283.   IF game_io_msg THEN deleteExtIO(game_io_msg)
  284.   IF game_msg_port THEN deletePort(game_msg_port)
  285.   SELECT exception
  286.   CASE ERR_DEV;   WriteF('Error: could not open gameport device\n')
  287.   CASE ERR_IO;    WriteF('Error: could not create I/O\n')
  288.   CASE ERR_PORT;  WriteF('Error: could not create port\n')
  289.   ENDSELECT
  290. ENDPROC
  291. ->>>
  292.