src/Input.cc
author saiam
Mon, 08 Dec 2008 21:00:50 +0000
changeset 309 05a39422c81e
parent 300 417183866f35
child 311 440763821484
permissions -rw-r--r--
You shouldn't get frags by killing yourself anymore.
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     },
300
417183866f35 suicide key and respawning
terom
parents: 282
diff changeset
    25
    {   INPUT_SUICIDE,      CL_KEY_LCONTROL,    CL_KEY_K        },
282
e0e4dfc3e528 compiles cleanly with -Wall -Wextra -Wconversion, not tested, but that shouldn't break anything :)
terom
parents: 237
diff changeset
    26
    {   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
    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
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
    29
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
    30
    {   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
    31
    {   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
    32
    {   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
    33
};
230
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    34
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    35
Input::Input (CL_InputDevice &keyboard) :
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    36
    keyboard(keyboard)
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
    
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    41
bool Input::checkKeycode (int keycode) {
230
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    42
    if (keycode > 0)
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    43
        return keyboard.get_keycode(keycode);
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    44
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    45
    else if (keycode < 0)
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    46
        return !keyboard.get_keycode(-keycode);
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    47
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    48
    else // == 0
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    49
        return true;
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    50
}
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    51
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    52
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
    53
    BitMaskType input_mask = 0;
230
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    54
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
    55
    for (InputKeymapEntry<BitEnumType> *e = keymap; (e->keycode1 || e->keycode2) && e->input; e++) {
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    56
        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
    57
            input_mask |= e->input;
230
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    58
    }
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    59
    
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
    60
    return input_mask;
230
78cf0cd69af4 better input handling
terom
parents: 229
diff changeset
    61
}
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
    62
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    63
PlayerInput Input::readPlayerInput (void) {
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    64
    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
    65
}
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
    66
235
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    67
GuiInput Input::readGuiInput (void) {
0a0c729365ee code cleanup
terom
parents: 233
diff changeset
    68
    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
    69
}
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
    70