1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
|
/*
* PROPRIETARY INFORMATION. This software is proprietary to POWDER
* Development, and is not to be reproduced, transmitted, or disclosed
* in any way without written permission.
*
* Produced by: Jeff Lait
*
* POWDER Development
*
* NAME: control.cpp ( POWDER Library, C++ )
*
* COMMENTS:
* Gameboy Advance specific implementation of controls.
*/
#include "control.h"
#include <mygba.h>
#include "control.h"
#include "gfxengine.h"
// Global variables, these store our previous button states.
// If old state is 1, the oldframe contains which frame the button
// was last pressed on.
int glb_oldstate[NUM_BUTTONS];
int glb_oldframe[NUM_BUTTONS];
#define SHORT_REPEAT 10 // 6 per sec
#define LONG_REPEAT 20 // 3 per sec
int glb_repeatrate[NUM_BUTTONS] =
{
SHORT_REPEAT, // UP
SHORT_REPEAT, // DOWN
SHORT_REPEAT, // LEFT
SHORT_REPEAT, // RIGHT
LONG_REPEAT, // A
LONG_REPEAT, // B
LONG_REPEAT, // START
LONG_REPEAT, // SELECT
LONG_REPEAT, // R
LONG_REPEAT, // L
LONG_REPEAT, // X
LONG_REPEAT, // Y
SHORT_REPEAT, // TOUCH (We use as directions when repeated)
LONG_REPEAT // LID
};
// Conversion from button enums to valid key presses.
#define MAX_KEYEQUIV 3
int glb_buttonkeys[NUM_BUTTONS][MAX_KEYEQUIV] =
{
{ GFX_KEYUP, 'k', '8' },
{ GFX_KEYDOWN, 'j', '2' },
{ GFX_KEYLEFT, 'h', '4' },
{ GFX_KEYRIGHT, 'l', '6' },
{ '\r', ' ', '5' }, // A
{ '\x1b', '0', 0 }, // B
{ 0, 0, 0 }, // START
{ 'i', 0, 0 }, // SELECT
{ 0, 0, 0 }, // R
{ 'p', 0, 0 }, // L
{ 0, 0, 0 }, // X
{ 0, 0, 0 }, // Y
{ GFX_KEYLMB, 0, 0 }, // TOUCH
{ 0, 0, 0 }, // LID
};
void
ctrl_init()
{
memset(glb_oldstate, 0, sizeof(int) * NUM_BUTTONS);
memset(glb_oldframe, 0, sizeof(int) * NUM_BUTTONS);
}
void
ctrl_setrepeat(BUTTONS button, int timedelay)
{
glb_repeatrate[button] = timedelay;
}
int
ctrl_anyrawpressed()
{
#ifdef HAS_KEYBOARD
return 0;
#else
if (~R_CTRLINPUT & 0x3FF)
return 1;
else
return 0;
#endif
}
int
ctrl_rawpressed(int button)
{
#ifdef HAS_KEYBOARD
return 0;
#else
switch (button)
{
case BUTTON_UP:
return F_CTRLINPUT_UP_PRESSED;
case BUTTON_DOWN:
return F_CTRLINPUT_DOWN_PRESSED;
case BUTTON_LEFT:
return F_CTRLINPUT_LEFT_PRESSED;
case BUTTON_RIGHT:
return F_CTRLINPUT_RIGHT_PRESSED;
case BUTTON_A:
return F_CTRLINPUT_A_PRESSED;
case BUTTON_B:
return F_CTRLINPUT_B_PRESSED;
case BUTTON_R:
return F_CTRLINPUT_R_PRESSED;
case BUTTON_L:
return F_CTRLINPUT_L_PRESSED;
case BUTTON_SELECT:
return F_CTRLINPUT_SELECT_PRESSED;
case BUTTON_START:
return F_CTRLINPUT_START_PRESSED;
#ifdef HAS_XYBUTTON
case BUTTON_X:
case BUTTON_Y:
return hamfake_isPressed((BUTTONS) button);
#else
case BUTTON_X:
case BUTTON_Y:
return false;
#endif
#ifdef USING_DS
case BUTTON_TOUCH:
case BUTTON_LID:
return hamfake_isPressed((BUTTONS) button);
#else
case BUTTON_TOUCH:
return hamfake_getstylusstate();
case BUTTON_LID:
return false;
#endif
}
#endif
return 0;
}
bool
ctrl_hit(int button)
{
#ifdef HAS_KEYBOARD
int key, equiv;
key = hamfake_peekKeyPress();
// Trivial failure if no key hit.
if (!key)
return false;
// See if this key matches this button's list.
for (equiv = 0; equiv < MAX_KEYEQUIV; equiv++)
{
if (glb_buttonkeys[button][equiv] == key)
{
// Consume the keypress.
hamfake_getKeyPress(false);
return true;
}
}
// Not found, ignore.
return false;
#else
int curstate;
int curframe = -1;
// Now, see if we are pressed...
curstate = ctrl_rawpressed(button);
if (!curstate)
{
// We know the answer, just make sure our old state is clear.
glb_oldstate[button] = 0;
return false;
}
if (glb_oldstate[button])
{
curframe = gfx_getframecount();
if (curframe < glb_oldframe[button] + glb_repeatrate[button])
{
// Not enough time since the last press, fail.
return false;
}
}
// Success! We will return 1 from here, but first update the
// old state with our current pos.
curframe = gfx_getframecount();
glb_oldframe[button] = curframe;
glb_oldstate[button] = 1;
return true;
#endif
}
void
ctrl_getdir(int &dx, int &dy, int raw, bool allowdiag)
{
dx = dy = 0;
#ifdef HAS_KEYBOARD
int key;
key = hamfake_peekKeyPress();
ctrl_getdirfromkey(key, dx, dy, allowdiag);
if (dx || dy)
{
// Consume the key
hamfake_getKeyPress(false);
}
#else
if (raw)
{
if (ctrl_rawpressed(BUTTON_LEFT))
dx--;
if (ctrl_rawpressed(BUTTON_RIGHT))
dx++;
if (ctrl_rawpressed(BUTTON_UP))
dy--;
if (ctrl_rawpressed(BUTTON_DOWN))
dy++;
}
else
{
if (ctrl_hit(BUTTON_LEFT))
dx--;
if (ctrl_hit(BUTTON_RIGHT))
dx++;
if (ctrl_hit(BUTTON_UP))
dy--;
if (ctrl_hit(BUTTON_DOWN))
dy++;
}
#endif
// We prohibit diagonals as they are really hard to do with our
// first down methods.
if (!allowdiag && dx && dy)
dy = 0;
}
void
ctrl_getdirfromkey(int keypress, int &dx, int &dy, bool allowdiag)
{
#ifndef HAS_KEYBOARD
ctrl_getdir(dx, dy);
#else
dx = dy = 0;
switch (keypress)
{
case '8':
case GFX_KEYUP:
case 'k':
dy--;
break;
case '2':
case GFX_KEYDOWN:
case 'j':
dy++;
break;
case '4':
case GFX_KEYLEFT:
case 'h':
dx--;
break;
case '6':
case GFX_KEYRIGHT:
case 'l':
dx++;
break;
}
if (allowdiag)
{
switch (keypress)
{
case '7':
case 'y':
dy--; dx--;
break;
case '9':
case 'u':
dy--; dx++;
break;
case '1':
case 'b':
dy++; dx--;
break;
case '3':
case 'n':
dy++; dx++;
break;
}
}
#endif
}
|