src/Input.cc
author terom
Mon, 08 Dec 2008 12:02:20 +0000
changeset 282 e0e4dfc3e528
parent 237 3d5465bcb67d
child 300 417183866f35
permissions -rw-r--r--
compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents:
diff changeset
     1
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents:
diff changeset
     2
#define INPUT_CC
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents:
diff changeset
     3
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents:
diff changeset
     4
#include "Input.hh"
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents:
diff changeset
     5
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
     6
template <typename BitEnumType> struct InputKeymapEntry {
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
     7
    BitEnumType input;
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
     8
    int keycode1, keycode2;
221
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents:
diff changeset
     9
};
fbc5db6fce45 reorganize the weapons code and input handling code
terom
parents:
diff changeset
    10
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    11
InputKeymapEntry<PlayerInputBit> INPUT_PLAYER_KEYMAP[] = {
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    12
    {   INPUT_AIM_UP,       -CL_KEY_ENTER,      CL_KEY_UP       },
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    13
    {   INPUT_AIM_DOWN,     -CL_KEY_ENTER,      CL_KEY_DOWN     },
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    14
    {   INPUT_MOVE_LEFT,    -CL_KEY_ENTER,      CL_KEY_LEFT     },
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    15
    {   INPUT_MOVE_RIGHT,   -CL_KEY_ENTER,      CL_KEY_RIGHT    },
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    16
    {   INPUT_JUMP,         -CL_KEY_ENTER,      CL_KEY_RSHIFT   },
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    17
    {   INPUT_DIG,          CL_KEY_LEFT,        CL_KEY_RIGHT    },
282
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 237
diff changeset
    18
    {   INPUT_SHOOT,        CL_KEY_RCONTROL,    0               },
237
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 235
diff changeset
    19
    {   INPUT_CHANGE_PREV,  CL_KEY_ENTER,       CL_KEY_LEFT     },
3d5465bcb67d tweak weapon params and improve weapon changing
terom
parents: 235
diff changeset
    20
    {   INPUT_CHANGE_NEXT,  CL_KEY_ENTER,       CL_KEY_RIGHT    },
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    21
    {   INPUT_ROPE,         CL_KEY_ENTER,       CL_KEY_RSHIFT   },
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    22
    {   INPUT_UNROPE,       -CL_KEY_ENTER,      CL_KEY_RSHIFT   },
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    23
    {   INPUT_ROPE_UP,      CL_KEY_ENTER,       CL_KEY_UP       },
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    24
    {   INPUT_ROPE_DOWN,    CL_KEY_ENTER,       CL_KEY_DOWN     },
282
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 237
diff changeset
    25
    {   INPUT_NONE,         0,                  0               }
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    26
};
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    27
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    28
InputKeymapEntry<GuiInputBit> INPUT_GUI_KEYMAP[] = {
282
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 237
diff changeset
    29
    {   GUI_INPUT_QUIT,             CL_KEY_ESCAPE,          0   },
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 237
diff changeset
    30
    {   GUI_INPUT_DISPLAY_WEAPON,   CL_KEY_ENTER,           0   },
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 237
diff changeset
    31
    {   GUI_INPUT_DEBUG_PLAYER,     CL_KEY_I,               0   },
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    32
};
230
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    33
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    34
Input::Input (CL_InputDevice &keyboard) :
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    35
    keyboard(keyboard)
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    36
{
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    37
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    38
}
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    39
    
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    40
bool Input::checkKeycode (int keycode) {
230
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    41
    if (keycode > 0)
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    42
        return keyboard.get_keycode(keycode);
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    43
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    44
    else if (keycode < 0)
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    45
        return !keyboard.get_keycode(-keycode);
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    46
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    47
    else // == 0
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    48
        return true;
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    49
}
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    50
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    51
template <typename BitEnumType, typename BitMaskType> BitMaskType Input::buildMask (InputKeymapEntry<BitEnumType> *keymap) {
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    52
    BitMaskType input_mask = 0;
230
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    53
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    54
    for (InputKeymapEntry<BitEnumType> *e = keymap; (e->keycode1 || e->keycode2) && e->input; e++) {
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    55
        if (checkKeycode(e->keycode1) && checkKeycode(e->keycode2))
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    56
            input_mask |= e->input;
230
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    57
    }
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    58
    
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    59
    return input_mask;
230
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    60
}
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    61
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    62
PlayerInput Input::readPlayerInput (void) {
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    63
    return buildMask<PlayerInputBit, PlayerInput>(INPUT_PLAYER_KEYMAP);
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    64
}
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    65
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    66
GuiInput Input::readGuiInput (void) {
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    67
    return buildMask<GuiInputBit, GuiInput>(INPUT_GUI_KEYMAP);
233
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    68
}
ff4ecea83cf5 start using CL_ResourceManager, change most draw methods to take a Graphics*, implment even better input handling, and draw weapon names
terom
parents: 230
diff changeset
    69