summaryrefslogtreecommitdiff
path: root/Camera.cpp
blob: 1f758753afa53c58e74626b99fee744aa6adea01 (plain) (blame)
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
/* *******************************************************************
 *  Includes
 * ******************************************************************/

#include "Camera.h"
#include "Cursor.h"
#include <stdint.h>
#include <stdbool.h>

/* *******************************************************************
 *  Defines
 * ******************************************************************/

/*****************************************************************//**
 *
 * \brief   This macro defines camera speed, in pixels per frame.
 *
 *********************************************************************/
#define CAMERA_SPEED    (static_cast<int8_t>(4))

/* *******************************************************************
 *  Types definition
 * ******************************************************************/

/* *******************************************************************
 *  Global variables definition
 * ******************************************************************/

/* *******************************************************************
 *  Local variables definition
 * ******************************************************************/

/* *******************************************************************
 *  Local prototypes declaration
 * ******************************************************************/

/* *******************************************************************
 *  Functions definition
 * ******************************************************************/

/*****************************************************************//**
 *
 * \brief   Constructor for Camera class.
 *
 *********************************************************************/
Camera::Camera(void) :
    _bLocked(false),
    _xOffset(0),
    _yOffset(0),
    _speedTimer(0)
{
}

/*****************************************************************//**
 *
 * \brief   This function transforms X coordinates for a given
 *          object to camera coordinates.
 *
 *********************************************************************/
uint8_t Camera::getX(const uint8_t x) const
{
    return x - _xOffset;
}

/*****************************************************************//**
 *
 * \brief   This function transforms Y coordinates for a given
 *          object to camera coordinates.
 *
 *********************************************************************/
uint8_t Camera::getY(const uint8_t y) const
{
    return y - _yOffset;
}

/*****************************************************************//**
 *
 * \brief   Event handler executed when human player presses
 *          left arrow button.
 *
 *********************************************************************/
void Camera::onLeftBtnPressed(Cursor& cursor)
{
    if (not cursor.isXCentered())
    {
        /* Move cursor to initial position. */
        cursor.move(-CAMERA_SPEED);
    }
    else if (_xOffset >= CAMERA_SPEED)
    {
        /* Move camera to the left. */
        _xOffset -= CAMERA_SPEED;
    }
    else
    {
        /* Left screen margin reached.
         * Cursor has to be moved. */
        cursor.move(-CAMERA_SPEED);
    }
}

/*****************************************************************//**
 *
 * \brief   Event handler executed when human player presses
 *          right arrow button.
 *
 *********************************************************************/
void Camera::onRightBtnPressed(Cursor& cursor)
{
    if (not cursor.isXCentered())
    {
        /* Move cursor to initial position. */
        cursor.move(CAMERA_SPEED);
    }
    else if (_xOffset < 512)
    {
        /* Move camera to the right. */
        _xOffset += CAMERA_SPEED;
    }
    else
    {
        /* Right screen margin reached.
         * Cursor has to be moved. */
        cursor.move(CAMERA_SPEED);
    }
}

/*****************************************************************//**
 *
 * \brief   Event handler executed when human player presses
 *          up arrow button.
 *
 *********************************************************************/
void Camera::onUpBtnPressed(Cursor& cursor)
{
    if (not cursor.isYCentered())
    {
        /* Move cursor to initial position. */
        cursor.move(0, -CAMERA_SPEED);
    }
    else if (_yOffset > 0)
    {
        /* Move camera to the right. */
        _yOffset -= CAMERA_SPEED;
    }
    else
    {
        /* Upper screen margin reached.
         * Cursor has to be moved. */
        cursor.move(0, -CAMERA_SPEED);
    }
}

/*****************************************************************//**
 *
 * \brief   Event handler executed when human player presses
 *          down arrow button.
 *
 *********************************************************************/
void Camera::onDownBtnPressed(Cursor& cursor)
{
    if (not cursor.isYCentered())
    {
        /* Move cursor to initial position. */
        cursor.move(0, CAMERA_SPEED);
    }
    else if (_yOffset < 512)
    {
        /* Move camera to the right. */
        _yOffset += CAMERA_SPEED;
    }
    else
    {
        /* Lower screen margin reached.
         * Cursor has to be moved. */
        cursor.move(0, CAMERA_SPEED);
    }
}

/*****************************************************************//**
 *
 * \brief   As its name suggests, this function adjusts camera lock.
 *
 *          When camera is locked, it cannot be moved when pressing
 *          direction keys.
 *
 * \param   bLock
 *              True if camera must be locked, false otherwise.
 *
 *********************************************************************/
void Camera::adjustLock(const bool bLock)
{
    _bLocked = bLock;
}

/*****************************************************************//**
 *
 * \brief   Returns camera lock state.
 *
 * \return  Returns true if camera is locked, false otherwise.
 *
 *********************************************************************/
bool Camera::isLocked(void) const
{
    return _bLocked;
}