File: Controller.h

package info (click to toggle)
burgerspace 1.9.2-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,524 kB
  • sloc: sh: 10,939; cpp: 5,791; makefile: 240
file content (154 lines) | stat: -rw-r--r-- 4,294 bytes parent folder | download | duplicates (4)
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
/*  $Id: Controller.h,v 1.2 2010/05/26 03:13:01 sarrazip Exp $
    Controller.h - Control of the game's main character

    burgerspace - A hamburger-smashing video game.
    Copyright (C) 2001-2010 Pierre Sarrazin <http://sarrazip.com/>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU General Public License
    as published by the Free Software Foundation; either version 2
    of the License, or (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#ifndef _H_Controller
#define _H_Controller

#include <flatzebra/KeyState.h>
#include <flatzebra/Joystick.h>

#include "util.h"


class Controller
{
public:

    Controller(SDLKey pepperKey)
      : startKS(SDLK_SPACE),
        pauseKS(SDLK_p),
        quitKS(SDLK_ESCAPE),
        leftKS(SDLK_LEFT),
        rightKS(SDLK_RIGHT),
        upKS(SDLK_UP),
        downKS(SDLK_DOWN),
        shootKS(pepperKey, SDLK_RCTRL),
        saveKS(SDLK_s),
        loadKS(SDLK_l),
        fullScreenKS(SDLK_F11),
        joystick(),
        startBI(flatzebra::Joystick::START_BTN),
        pauseBI(flatzebra::Joystick::BACK_BTN),
        shootBI(flatzebra::Joystick::B_BTN)
    {
    }

    void check(SDLKey keysym, bool pressed)
    {
        startKS.check(keysym, pressed);
        pauseKS.check(keysym, pressed);
        quitKS.check(keysym, pressed);
        saveKS.check(keysym, pressed);
        loadKS.check(keysym, pressed);
        leftKS.check(keysym, pressed);
        rightKS.check(keysym, pressed);
        upKS.check(keysym, pressed);
        downKS.check(keysym, pressed);
        shootKS.check(keysym, pressed);
        fullScreenKS.check(keysym, pressed);
    }

    bool isShotRequested() const
    {
        return shootKS.justPressed() || joystick.buttonJustPressed(shootBI);
    }

    bool isStartRequested() const
    {
        return startKS.justPressed() || joystick.buttonJustPressed(startBI);
    }

    bool isPauseRequested() const
    {
        return pauseKS.justPressed() || joystick.buttonJustPressed(pauseBI);
    }

    bool isResumeRequested() const
    {
        return pauseKS.justPressed() || joystick.buttonJustPressed(startBI);
    }

    bool isQuitRequested() const
    {
        return quitKS.justPressed();
    }

    bool isSaveRequested() const
    {
        return saveKS.justPressed();
    }

    bool isLoadRequested() const
    {
        return loadKS.justPressed();
    }

    bool isFullScreenToggleRequested() const
    {
        return fullScreenKS.justPressed();
    }

    void update()
    {
        startKS.remember();
        pauseKS.remember();
        quitKS.remember();
        leftKS.remember();
        rightKS.remember();
        upKS.remember();
        downKS.remember();
        shootKS.remember();
        saveKS.remember();
        loadKS.remember();
        fullScreenKS.remember();
        joystick.update();
    }

    void getDesiredDirections(bool desiredDirs[4]) const
    {
        desiredDirs[RIGHT] = rightKS.isPressed() || joystick.getXAxisDisplacement() > 0;
        desiredDirs[UP]    = upKS.isPressed()    || joystick.getYAxisDisplacement() < 0;
        desiredDirs[LEFT]  = leftKS.isPressed()  || joystick.getXAxisDisplacement() < 0;
        desiredDirs[DOWN]  = downKS.isPressed()  || joystick.getYAxisDisplacement() > 0;
    }

private:
    flatzebra::KeyState startKS;
    flatzebra::KeyState pauseKS;
    flatzebra::KeyState quitKS;
    flatzebra::KeyState leftKS;
    flatzebra::KeyState rightKS;
    flatzebra::KeyState upKS;
    flatzebra::KeyState downKS;
    flatzebra::KeyState shootKS;
    flatzebra::KeyState saveKS;
    flatzebra::KeyState loadKS;
    flatzebra::KeyState fullScreenKS;
    flatzebra::Joystick joystick;

    // Joystick button indexes:
    int startBI;
    int pauseBI;
    int shootBI;
};

#endif  /* _H_Controller */