FreeBASIC  0.91.0
gfx_stick.c
Go to the documentation of this file.
1 /* QB compatible STICK(n) and STRIG(n) functions */
2 
3 #include "fb_gfx.h"
4 
5 /* save state for x,y,buttons for A,B sticks */
6 
7 static int stick_posn[4] = { 0, 0, 0, 0 };
8 static int stick_btns[2] = { 0, 0 };
9 
10 /*:::::*/
11 FBCALL int fb_GfxStickQB( int n )
12 {
13  /*
14  if n == 0 then
15  read and save x,y positions for stick A and B.
16  return A.x
17  if n == 1,2,3
18  return the last read position for A.y, B.x, B.y
19  */
20 
21  if( n >= 0 && n <= 3 )
22  {
23  if( n == 0 )
24  {
25  int i;
26  for( i=0; i<2; i++ )
27  {
28  float x, y, a;
29  ssize_t buttons;
30  int ret = fb_GfxGetJoystick( i, &buttons, &x, &y, &a, &a, &a, &a, &a, &a );
31  if( ret == FB_RTERROR_OK )
32  {
33  stick_posn[i*2] = (int)(x * 100 + 101);
34  stick_posn[i*2+1] = (int)(y * 100 + 101);
35  stick_btns[i] = buttons;
36  }
37  else
38  {
39  stick_posn[i*2] = 0;
40  stick_posn[i*2+1] = 0;
41  stick_btns[i] = 0;
42  }
43  }
44  }
45  return stick_posn[n];
46  }
47 
49  return 0;
50 }
51 
52 /*:::::*/
53 FBCALL int fb_GfxStrigQB( int n )
54 {
55  /*
56  n result ( -1 == TRUE, 0 == FALSE )
57  0 A button 1 pressed since last STICK(0)
58  1 A button 1 is pressed
59  2 B button 1 pressed since last STICK(0)
60  3 B button 1 is pressed
61  4 A button 2 pressed since last STICK(0)
62  5 A button 2 is pressed
63  6 B button 2 pressed since last STICK(0)
64  7 B button 2 is pressed
65  */
66 
67  if( (n >= 0) && (n <= 7) )
68  {
69  int i = (n>>1)&1;
70  int bmask = (n>>2)+1;
71 
72  if( n & 1 )
73  {
74  float a;
75  ssize_t buttons;
76  if( FB_RTERROR_OK == fb_GfxGetJoystick( i, &buttons, &a, &a, &a, &a, &a, &a, &a, &a ))
77  {
78  stick_btns[i] |= buttons;
79  return ( buttons & bmask) ? -1 : 0;
80  }
81  }
82  else
83  {
84  return ( stick_btns[i] & bmask) ? -1 : 0;
85  }
86  }
87 
89  return 0;
90 }