Skip to content
This repository
Browse code

Adds Gamepad class to gameplay.

Stores Gamepad objects in Game.
Adds Gamepad* createGamepad(const char* gamepadFormPath) to Game.
Adds Gamepad* getGamepad(unsigned int playerIndex=0) to Game.
Adds void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int index) event callback to Game.
Adds const char* getType() on Control and has each child of Control implement the function, returning the name of the class as a string in camel-casing.
Adds concept of contact index at the Control level. This allows for more than one Control to be active/in focus at once. Had to refactor some Control's touchEvents to handle this. Setting the contactIndex on a TOUCH_PRESS event, and then resetting it to INVALID_CONTACT_INDEX on TOUCH_RELEASE.
Fixes memory leak in FrameBuffer.
Code cleanup, removes some warnings.
  • Loading branch information...
commit a6c810d4175193a573064d50f19971002026f0f2 1 parent dae121c
Kieran Cunney kcunney authored

Showing 38 changed files with 835 additions and 849 deletions. Show diff stats Hide diff stats

  1. +41 36 gameplay-samples/sample03-character/game.dxt.config
  2. +40 35 gameplay-samples/sample03-character/game.png.config
  3. +40 35 gameplay-samples/sample03-character/game.pvrtc.config
  4. +2 2 gameplay-samples/sample03-character/sample03-character.vcxproj
  5. +6 6 gameplay-samples/sample03-character/sample03-character.vcxproj.filters
  6. +55 78 gameplay-samples/sample03-character/src/CharacterGame.cpp
  7. +8 3 gameplay-samples/sample03-character/src/CharacterGame.h
  8. +0 342 gameplay-samples/sample03-character/src/Gamepad.cpp
  9. +0 218 gameplay-samples/sample03-character/src/Gamepad.h
  10. +2 4 gameplay/gameplay.vcxproj
  11. +2 8 gameplay/gameplay.vcxproj.filters
  12. +16 3 gameplay/src/Button.cpp
  13. +9 1 gameplay/src/Button.h
  14. +5 0 gameplay/src/CheckBox.cpp
  15. +6 1 gameplay/src/CheckBox.h
  16. +8 8 gameplay/src/Container.cpp
  17. +7 2 gameplay/src/Container.h
  18. +18 7 gameplay/src/Control.cpp
  19. +19 1 gameplay/src/Control.h
  20. +6 0 gameplay/src/Form.cpp
  21. +6 1 gameplay/src/Form.h
  22. +1 0  gameplay/src/FrameBuffer.cpp
  23. +76 2 gameplay/src/Game.cpp
  24. +33 0 gameplay/src/Game.h
  25. +11 1 gameplay/src/Game.inl
  26. +181 0 gameplay/src/Gamepad.cpp
  27. +113 0 gameplay/src/Gamepad.h
  28. +65 47 gameplay/src/Joystick.cpp
  29. +7 1 gameplay/src/Joystick.h
  30. +6 1 gameplay/src/Label.cpp
  31. +6 1 gameplay/src/Label.h
  32. +6 1 gameplay/src/RadioButton.cpp
  33. +6 1 gameplay/src/RadioButton.h
  34. +5 0 gameplay/src/Slider.cpp
  35. +5 0 gameplay/src/Slider.h
  36. +11 2 gameplay/src/TextBox.cpp
  37. +6 1 gameplay/src/TextBox.h
  38. +1 0  gameplay/src/gameplay.h
77 gameplay-samples/sample03-character/game.dxt.config
... ... @@ -1,43 +1,48 @@
1 1 window
2 2 {
3   - title = Character
4   - width = 1280
5   - height = 720
6   - fullscreen = false
  3 +title = Character
  4 +width = 1280
  5 +height = 720
  6 +fullscreen = false
7 7 }
8 8
9 9 aliases
10 10 {
11   - backboard = res/dxt/backboard.dds
12   - basketball = res/dxt/basketball.dds
13   - basketballnet = res/dxt/basketballnet.dds
14   - book = res/dxt/book.dds
15   - bookshelf = res/dxt/bookshelf.dds
16   - character = res/dxt/character.dds
17   - decals = res/dxt/decals.dds
18   - door = res/dxt/door.dds
19   - doorframe = res/dxt/doorframe.dds
20   - easel = res/dxt/easel.dds
21   - floor = res/dxt/floor.dds
22   - floortiles = res/dxt/floortiles.dds
23   - gamepad = res/dxt/gamepad.dds
24   - playtable = res/dxt/playtable.dds
25   - shadow = res/dxt/shadow.dds
26   - storageorganizer = res/dxt/storageorganizer.dds
27   - tableleg1 = res/dxt/tableleg1.dds
28   - tableleg2 = res/dxt/tableleg2.dds
29   - tableleg3 = res/dxt/tableleg3.dds
30   - tableleg4 = res/dxt/tableleg4.dds
31   - tabletop = res/dxt/tabletop.dds
32   - tiles = res/dxt/tiles.dds
33   - tilesn = res/dxt/tilesn.dds
34   - toybox = res/dxt/toybox.dds
35   - walleast = res/dxt/walleast.dds
36   - wallnorth = res/dxt/wallnorth.dds
37   - walloverhang = res/dxt/walloverhang.dds
38   - wallsouth = res/dxt/wallsouth.dds
39   - wallwest = res/dxt/wallwest.dds
40   - windowledge = res/dxt/windowledge.dds
41   - wood = res/dxt/wood.dds
42   - woodn = res/dxt/woodn.dds
  11 +backboard = res/dxt/backboard.dds
  12 +basketball = res/dxt/basketball.dds
  13 +basketballnet = res/dxt/basketballnet.dds
  14 +book = res/dxt/book.dds
  15 +bookshelf = res/dxt/bookshelf.dds
  16 +character = res/dxt/character.dds
  17 +decals = res/dxt/decals.dds
  18 +door = res/dxt/door.dds
  19 +doorframe = res/dxt/doorframe.dds
  20 +easel = res/dxt/easel.dds
  21 +floor = res/dxt/floor.dds
  22 +floortiles = res/dxt/floortiles.dds
  23 +gamepad = res/dxt/gamepad.dds
  24 +playtable = res/dxt/playtable.dds
  25 +shadow = res/dxt/shadow.dds
  26 +storageorganizer = res/dxt/storageorganizer.dds
  27 +tableleg1 = res/dxt/tableleg1.dds
  28 +tableleg2 = res/dxt/tableleg2.dds
  29 +tableleg3 = res/dxt/tableleg3.dds
  30 +tableleg4 = res/dxt/tableleg4.dds
  31 +tabletop = res/dxt/tabletop.dds
  32 +tiles = res/dxt/tiles.dds
  33 +tilesn = res/dxt/tilesn.dds
  34 +toybox = res/dxt/toybox.dds
  35 +walleast = res/dxt/walleast.dds
  36 +wallnorth = res/dxt/wallnorth.dds
  37 +walloverhang = res/dxt/walloverhang.dds
  38 +wallsouth = res/dxt/wallsouth.dds
  39 +wallwest = res/dxt/wallwest.dds
  40 +windowledge = res/dxt/windowledge.dds
  41 +wood = res/dxt/wood.dds
  42 +woodn = res/dxt/woodn.dds
  43 +}
  44 +
  45 +gamepad
  46 +{
  47 +form = res/common/gamepad.form
43 48 }
75 gameplay-samples/sample03-character/game.png.config
... ... @@ -1,35 +1,40 @@
1   -aliases
2   -{
3   - backboard = res/png/backboard.png
4   - basketball = res/png/basketball.png
5   - basketballnet = res/png/basketballnet.png
6   - book = res/png/book.png
7   - bookshelf = res/png/bookshelf.png
8   - character = res/png/character.png
9   - decals = res/png/decals.png
10   - door = res/png/door.png
11   - doorframe = res/png/doorframe.png
12   - easel = res/png/easel.png
13   - floor = res/png/floor.png
14   - floortiles = res/png/floortiles.png
15   - gamepad = res/png/gamepad.png
16   - playtable = res/png/playtable.png
17   - shadow = res/png/shadow.png
18   - storageorganizer = res/png/storageorganizer.png
19   - tableleg1 = res/png/tableleg1.png
20   - tableleg2 = res/png/tableleg2.png
21   - tableleg3 = res/png/tableleg3.png
22   - tableleg4 = res/png/tableleg4.png
23   - tabletop = res/png/tabletop.png
24   - tiles = res/png/tiles.png
25   - tilesn = res/png/tilesn.png
26   - toybox = res/png/toybox.png
27   - walleast = res/png/walleast.png
28   - wallnorth = res/png/wallnorth.png
29   - walloverhang = res/png/walloverhang.png
30   - wallsouth = res/png/wallsouth.png
31   - wallwest = res/png/wallwest.png
32   - windowledge = res/png/windowledge.png
33   - wood = res/png/wood.png
34   - woodn = res/png/woodn.png
35   -}
  1 +aliases
  2 +{
  3 +backboard = res/png/backboard.png
  4 +basketball = res/png/basketball.png
  5 +basketballnet = res/png/basketballnet.png
  6 +book = res/png/book.png
  7 +bookshelf = res/png/bookshelf.png
  8 +character = res/png/character.png
  9 +decals = res/png/decals.png
  10 +door = res/png/door.png
  11 +doorframe = res/png/doorframe.png
  12 +easel = res/png/easel.png
  13 +floor = res/png/floor.png
  14 +floortiles = res/png/floortiles.png
  15 +gamepad = res/png/gamepad.png
  16 +playtable = res/png/playtable.png
  17 +shadow = res/png/shadow.png
  18 +storageorganizer = res/png/storageorganizer.png
  19 +tableleg1 = res/png/tableleg1.png
  20 +tableleg2 = res/png/tableleg2.png
  21 +tableleg3 = res/png/tableleg3.png
  22 +tableleg4 = res/png/tableleg4.png
  23 +tabletop = res/png/tabletop.png
  24 +tiles = res/png/tiles.png
  25 +tilesn = res/png/tilesn.png
  26 +toybox = res/png/toybox.png
  27 +walleast = res/png/walleast.png
  28 +wallnorth = res/png/wallnorth.png
  29 +walloverhang = res/png/walloverhang.png
  30 +wallsouth = res/png/wallsouth.png
  31 +wallwest = res/png/wallwest.png
  32 +windowledge = res/png/windowledge.png
  33 +wood = res/png/wood.png
  34 +woodn = res/png/woodn.png
  35 +}
  36 +
  37 +gamepad
  38 +{
  39 +form = res/common/gamepad.form
  40 +}
75 gameplay-samples/sample03-character/game.pvrtc.config
... ... @@ -1,35 +1,40 @@
1   -aliases
2   -{
3   - backboard = res/pvrtc/backboard.pvr
4   - basketball = res/pvrtc/basketball.pvr
5   - basketballnet = res/pvrtc/basketballnet.pvr
6   - book = res/pvrtc/book.pvr
7   - bookshelf = res/pvrtc/bookshelf.pvr
8   - character = res/pvrtc/character.pvr
9   - decals = res/pvrtc/decals.pvr
10   - door = res/pvrtc/door.pvr
11   - doorframe = res/pvrtc/doorframe.pvr
12   - easel = res/pvrtc/easel.pvr
13   - floor = res/pvrtc/floor.pvr
14   - floortiles = res/pvrtc/floortiles.pvr
15   - gamepad = res/pvrtc/gamepad.pvr
16   - playtable = res/pvrtc/playtable.pvr
17   - shadow = res/pvrtc/shadow.pvr
18   - storageorganizer = res/pvrtc/storageorganizer.pvr
19   - tableleg1 = res/pvrtc/tableleg1.pvr
20   - tableleg2 = res/pvrtc/tableleg2.pvr
21   - tableleg3 = res/pvrtc/tableleg3.pvr
22   - tableleg4 = res/pvrtc/tableleg4.pvr
23   - tabletop = res/pvrtc/tabletop.pvr
24   - tiles = res/pvrtc/tiles.pvr
25   - tilesn = res/pvrtc/tilesn.pvr
26   - toybox = res/pvrtc/toybox.pvr
27   - walleast = res/pvrtc/walleast.pvr
28   - wallnorth = res/pvrtc/wallnorth.pvr
29   - walloverhang = res/pvrtc/walloverhang.pvr
30   - wallsouth = res/pvrtc/wallsouth.pvr
31   - wallwest = res/pvrtc/wallwest.pvr
32   - windowledge = res/pvrtc/windowledge.pvr
33   - wood = res/pvrtc/wood.pvr
34   - woodn = res/pvrtc/woodn.pvr
35   -}
  1 +aliases
  2 +{
  3 +backboard = res/pvrtc/backboard.pvr
  4 +basketball = res/pvrtc/basketball.pvr
  5 +basketballnet = res/pvrtc/basketballnet.pvr
  6 +book = res/pvrtc/book.pvr
  7 +bookshelf = res/pvrtc/bookshelf.pvr
  8 +character = res/pvrtc/character.pvr
  9 +decals = res/pvrtc/decals.pvr
  10 +door = res/pvrtc/door.pvr
  11 +doorframe = res/pvrtc/doorframe.pvr
  12 +easel = res/pvrtc/easel.pvr
  13 +floor = res/pvrtc/floor.pvr
  14 +floortiles = res/pvrtc/floortiles.pvr
  15 +gamepad = res/pvrtc/gamepad.pvr
  16 +playtable = res/pvrtc/playtable.pvr
  17 +shadow = res/pvrtc/shadow.pvr
  18 +storageorganizer = res/pvrtc/storageorganizer.pvr
  19 +tableleg1 = res/pvrtc/tableleg1.pvr
  20 +tableleg2 = res/pvrtc/tableleg2.pvr
  21 +tableleg3 = res/pvrtc/tableleg3.pvr
  22 +tableleg4 = res/pvrtc/tableleg4.pvr
  23 +tabletop = res/pvrtc/tabletop.pvr
  24 +tiles = res/pvrtc/tiles.pvr
  25 +tilesn = res/pvrtc/tilesn.pvr
  26 +toybox = res/pvrtc/toybox.pvr
  27 +walleast = res/pvrtc/walleast.pvr
  28 +wallnorth = res/pvrtc/wallnorth.pvr
  29 +walloverhang = res/pvrtc/walloverhang.pvr
  30 +wallsouth = res/pvrtc/wallsouth.pvr
  31 +wallwest = res/pvrtc/wallwest.pvr
  32 +windowledge = res/pvrtc/windowledge.pvr
  33 +wood = res/pvrtc/wood.pvr
  34 +woodn = res/pvrtc/woodn.pvr
  35 +}
  36 +
  37 +gamepad
  38 +{
  39 +form = res/common/gamepad.form
  40 +}
4 gameplay-samples/sample03-character/sample03-character.vcxproj
@@ -156,17 +156,17 @@
156 156 <None Include="game.dxt.config" />
157 157 <None Include="icon.png" />
158 158 <None Include="res\common\boy.animation" />
  159 + <None Include="res\common\gamepad.form" />
  160 + <None Include="res\common\gamepad.theme" />
159 161 <None Include="res\common\scene.material" />
160 162 <None Include="res\common\scene.physics" />
161 163 <None Include="res\common\scene.scene" />
162 164 </ItemGroup>
163 165 <ItemGroup>
164 166 <ClCompile Include="src\CharacterGame.cpp" />
165   - <ClCompile Include="src\Gamepad.cpp" />
166 167 </ItemGroup>
167 168 <ItemGroup>
168 169 <ClInclude Include="src\CharacterGame.h" />
169   - <ClInclude Include="src\Gamepad.h" />
170 170 </ItemGroup>
171 171 <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
172 172 <ImportGroup Label="ExtensionTargets">
12 gameplay-samples/sample03-character/sample03-character.vcxproj.filters
@@ -25,21 +25,21 @@
25 25 <Filter>res</Filter>
26 26 </None>
27 27 <None Include="game.dxt.config" />
  28 + <None Include="res\common\gamepad.form">
  29 + <Filter>res</Filter>
  30 + </None>
  31 + <None Include="res\common\gamepad.theme">
  32 + <Filter>res</Filter>
  33 + </None>
28 34 </ItemGroup>
29 35 <ItemGroup>
30 36 <ClCompile Include="src\CharacterGame.cpp">
31 37 <Filter>src</Filter>
32 38 </ClCompile>
33   - <ClCompile Include="src\Gamepad.cpp">
34   - <Filter>src</Filter>
35   - </ClCompile>
36 39 </ItemGroup>
37 40 <ItemGroup>
38 41 <ClInclude Include="src\CharacterGame.h">
39 42 <Filter>src</Filter>
40 43 </ClInclude>
41   - <ClInclude Include="src\Gamepad.h">
42   - <Filter>src</Filter>
43   - </ClInclude>
44 44 </ItemGroup>
45 45 </Project>
133 gameplay-samples/sample03-character/src/CharacterGame.cpp
@@ -19,7 +19,7 @@ CharacterGame game;
19 19 CharacterGame::CharacterGame()
20 20 : _font(NULL), _scene(NULL), _character(NULL), _characterMeshNode(NULL), _characterShadowNode(NULL),
21 21 _animation(NULL), _currentClip(NULL), _rotateX(0), _materialParameterAlpha(NULL),
22   - _keyFlags(0), _drawDebug(0), _buttonReleased(true)
  22 + _keyFlags(0), _drawDebug(0), _buttonReleased(true), _gamepad(NULL)
23 23 {
24 24 }
25 25
@@ -99,30 +99,15 @@ void CharacterGame::initializeCharacter()
99 99
100 100 void CharacterGame::initializeGamepad()
101 101 {
102   - _gamepad = new Gamepad("@gamepad", 1, 1);
103   -
104   - float scale = getHeight() / 720.0f;
105   -
106   - // Add a single gamepad joystick
107   - _gamepad->setJoystick(0,
108   - Rectangle(48.0f * scale, getHeight() - 248.0f * scale, 200.0f * scale, 200.0f * scale),
109   - Rectangle(256, 0, 256, 256),
110   - Rectangle(48.0f * scale, getHeight() - 248.0f * scale, 200.0f * scale, 200.0f * scale),
111   - Rectangle(0, 0, 256, 256),
112   - 64.0f);
113   -
114   - // Add a single gamepad button
115   - _gamepad->setButton(0,
116   - Rectangle(getWidth() - scale * (176.0f), getHeight() - 176.0f * scale, 128.0f * scale, 128.0f * scale),
117   - Rectangle(40, 310, 160, 160),
118   - Rectangle(300, 310, 160, 160));
  102 + // Get the gamepad loaded for player1 from game.config.
  103 + _gamepad = Game::getInstance()->getGamepad(0);
  104 + GP_ASSERT(_gamepad);
119 105 }
120 106
121 107 void CharacterGame::finalize()
122 108 {
123 109 SAFE_RELEASE(_scene);
124 110 SAFE_RELEASE(_font);
125   - SAFE_DELETE(_gamepad);
126 111 }
127 112
128 113 void CharacterGame::drawSplash(void* param)
@@ -190,53 +175,38 @@ bool CharacterGame::isOnFloor() const
190 175
191 176 void CharacterGame::update(float elapsedTime)
192 177 {
193   - Vector2 direction;
194   -
195   - if (_gamepad->getButtonState(0) == Gamepad::BUTTON_PRESSED)
196   - {
197   - if (_buttonReleased)
198   - {
199   - _buttonReleased = false;
200   - // Jump while the gamepad button is being pressed
201   - jump();
202   - }
203   - }
204   - else
205   - {
206   - _buttonReleased = true;
207   - }
208   -
209   - if (_gamepad->isJoystickActive(0))
210   - {
211   - // Get joystick direction
212   - direction = _gamepad->getJoystickState(0);
213   - }
214   - else
  178 + if (!_gamepad->isJoystickActive(0) && !_gamepad->isJoystickActive(1))
215 179 {
216 180 // Construct direction vector from keyboard input
217 181 if (_keyFlags & NORTH)
218   - direction.y = 1;
  182 + _currentDirection.y = 1;
219 183 else if (_keyFlags & SOUTH)
220   - direction.y = -1;
  184 + _currentDirection.y = -1;
  185 + else
  186 + _currentDirection.y = 0;
  187 +
221 188 if (_keyFlags & EAST)
222   - direction.x = 1;
  189 + _currentDirection.x = 1;
223 190 else if (_keyFlags & WEST)
224   - direction.x = -1;
  191 + _currentDirection.x = -1;
  192 + else
  193 + _currentDirection.x = 0;
225 194
226   - direction.normalize();
  195 +
  196 + _currentDirection.normalize();
227 197 if ((_keyFlags & RUNNING) == 0)
228   - direction *= 0.5f;
  198 + _currentDirection *= 0.5f;
229 199 }
230 200
231 201 // Update character animation and velocity
232   - if (direction.isZero())
  202 + if (_currentDirection.isZero())
233 203 {
234 204 play("idle", true);
235 205 _character->setVelocity(Vector3::zero());
236 206 }
237 207 else
238 208 {
239   - bool running = (direction.lengthSquared() > 0.75f);
  209 + bool running = (_currentDirection.lengthSquared() > 0.75f);
240 210 float speed = running ? RUN_SPEED : WALK_SPEED;
241 211
242 212 play(running ? "running" : "walking", true, 1.0f);
@@ -251,7 +221,7 @@ void CharacterGame::update(float elapsedTime)
251 221 Vector3 currentHeading(-_characterMeshNode->getForwardVectorWorld());
252 222
253 223 // Construct a new forward vector for the mesh node
254   - Vector3 newHeading(cameraForward * direction.y + cameraRight * direction.x);
  224 + Vector3 newHeading(cameraForward * _currentDirection.y + cameraRight * _currentDirection.x);
255 225
256 226 // Compute the rotation amount based on the difference between the current and new vectors
257 227 float angle = atan2f(newHeading.x, newHeading.z) - atan2f(currentHeading.x, currentHeading.z);
@@ -302,9 +272,6 @@ void CharacterGame::render(float elapsedTime)
302 272 break;
303 273 }
304 274
305   - // Draw gamepad for touch devices.
306   - _gamepad->draw(Vector4(1.0f, 1.0f, 1.0f, 0.7f));
307   -
308 275 // Draw FPS
309 276 _font->begin();
310 277 char fps[32];
@@ -384,33 +351,28 @@ void CharacterGame::keyEvent(Keyboard::KeyEvent evt, int key)
384 351
385 352 void CharacterGame::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
386 353 {
387   - // Send the touch event to the gamepad.
388   - _gamepad->touchEvent(evt, x, y, contactIndex);
389   -
390   - if (!_gamepad->isJoystickActive(0) || contactIndex != _gamepad->getJoystickContactIndex(0))
  354 + // This should only be called if the gamepad did not handle the touch event.
  355 + switch (evt)
391 356 {
392   - switch (evt)
  357 + case Touch::TOUCH_PRESS:
393 358 {
394   - case Touch::TOUCH_PRESS:
395   - {
396   - _rotateX = x;
397   - }
398   - break;
399   - case Touch::TOUCH_RELEASE:
400   - {
401   - _rotateX = 0;
402   - }
403   - break;
404   - case Touch::TOUCH_MOVE:
405   - {
406   - int deltaX = x - _rotateX;
407   - _rotateX = x;
408   - _character->getNode()->rotateY(-MATH_DEG_TO_RAD(deltaX * 0.5f));
409   - }
410   - break;
411   - default:
412   - break;
  359 + _rotateX = x;
  360 + }
  361 + break;
  362 + case Touch::TOUCH_RELEASE:
  363 + {
  364 + _rotateX = 0;
  365 + }
  366 + break;
  367 + case Touch::TOUCH_MOVE:
  368 + {
  369 + int deltaX = x - _rotateX;
  370 + _rotateX = x;
  371 + _character->getNode()->rotateY(-MATH_DEG_TO_RAD(deltaX * 0.5f));
413 372 }
  373 + break;
  374 + default:
  375 + break;
414 376 }
415 377 }
416 378
@@ -484,7 +446,22 @@ void CharacterGame::adjustCamera(float elapsedTime)
484 446 distance = 0;
485 447 }
486 448
487   -void CharacterGame::animationEvent(AnimationClip* clip, EventType type)
  449 +void CharacterGame::animationEvent(AnimationClip* clip, AnimationClip::Listener::EventType type)
488 450 {
489 451 clip->crossFade(_currentClip, 150);
490 452 }
  453 +
  454 +void CharacterGame::gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int index)
  455 +{
  456 + switch(evt)
  457 + {
  458 + case Gamepad::BUTTON_EVENT:
  459 + if (gamepad->getButtonState(index) == Gamepad::BUTTON_PRESSED)
  460 + jump();
  461 + break;
  462 + case Gamepad::JOYSTICK_EVENT:
  463 + _currentDirection = gamepad->getJoystickValue(index);
  464 + break;
  465 + }
  466 + return;
  467 +}
11 gameplay-samples/sample03-character/src/CharacterGame.h
@@ -2,7 +2,6 @@
2 2 #define CHARACTERGAME_H_
3 3
4 4 #include "gameplay.h"
5   -#include "Gamepad.h"
6 5 using namespace gameplay;
7 6
8 7 /**
@@ -30,7 +29,12 @@ class CharacterGame: public Game, public AnimationClip::Listener
30 29 /**
31 30 * @see AnimationClip::Listener::animationEvent
32 31 */
33   - void animationEvent(AnimationClip* clip, EventType type);
  32 + void animationEvent(AnimationClip* clip, AnimationClip::Listener::EventType type);
  33 +
  34 + /**
  35 + * @see Game::gamepadEvent
  36 + */
  37 + void gamepadEvent(Gamepad::GamepadEvent evt, Gamepad* gamepad, unsigned int index);
34 38
35 39 protected:
36 40
@@ -76,11 +80,12 @@ class CharacterGame: public Game, public AnimationClip::Listener
76 80 AnimationClip* _currentClip;
77 81 AnimationClip* _jumpClip;
78 82 int _rotateX;
79   - Gamepad* _gamepad;
80 83 MaterialParameter* _materialParameterAlpha;
81 84 unsigned int _keyFlags;
82 85 int _drawDebug;
83 86 bool _buttonReleased;
  87 + Gamepad* _gamepad;
  88 + Vector2 _currentDirection;
84 89 };
85 90
86 91 #endif
342 gameplay-samples/sample03-character/src/Gamepad.cpp
... ... @@ -1,342 +0,0 @@
1   -#include "Base.h"
2   -#include "Gamepad.h"
3   -#include "Texture.h"
4   -
5   -#define INVALID_CONTACT_INDEX ((unsigned int) -1)
6   -
7   -Gamepad::Gamepad(const char* texturePath, unsigned int joysticks, unsigned int buttons)
8   - : _joystickCount(joysticks), _buttonCount(buttons), _texture(NULL), _spriteBatch(NULL)
9   -{
10   - assert (texturePath);
11   - assert(joysticks <= _joystickCount);
12   - assert(buttons <= _buttonCount);
13   -
14   - // Load the gamepad texture.
15   - if (strlen(texturePath) == 0)
16   - {
17   - printError("Error loading Gamepad: No valid texture path specified, in %s", texturePath);
18   - }
19   -
20   - _texture = Texture::create(texturePath, false);
21   - assert(_texture);
22   -
23   - // TODO: determine the "actual" number of sprites required when parsing the .gamepad file.
24   - _spriteBatch = SpriteBatch::create(_texture, 0, _buttonCount + _joystickCount);
25   -
26   - // Create the joystick objects.
27   - _joysticks = new Joystick*[_joystickCount];
28   - for (unsigned int i = 0; i < _joystickCount; i++)
29   - {
30   - _joysticks[i] = new Joystick();
31   - }
32   -
33   - // Create the button objects.
34   - _buttons = new Button*[_buttonCount];
35   - for (unsigned int i = 0; i < _buttonCount; i++)
36   - {
37   - _buttons[i] = new Button();
38   - }
39   -}
40   -
41   -Gamepad::Gamepad(const Gamepad* g)
42   -{
43   -}
44   -
45   -Gamepad::~Gamepad()
46   -{
47   - SAFE_RELEASE(_texture);
48   - SAFE_DELETE(_spriteBatch);
49   -
50   - for (unsigned int i = 0; i < _joystickCount; i++)
51   - {
52   - delete _joysticks[i];
53   - }
54   - SAFE_DELETE_ARRAY(_joysticks);
55   -
56   - for (unsigned int i = 0; i < _buttonCount; i++)
57   - {
58   - delete _buttons[i];
59   - }
60   - SAFE_DELETE_ARRAY(_buttons);
61   -}
62   -
63   -Gamepad::Button::Button()
64   - : _pressed(BUTTON_RELEASED), _defaultTextureEnabled(false), _focusTextureEnabled(false), _contactIndex(INVALID_CONTACT_INDEX)
65   -{
66   -
67   -}
68   -
69   -Gamepad::Button::Button(const Button& button)
70   -{
71   -}
72   -
73   -Gamepad::Button::~Button()
74   -{
75   -}
76   -
77   -Gamepad::Joystick::Joystick()
78   - : _displacement(Vector2::zero()), _direction(Vector2::zero()), _radius(0.0f),
79   - _defaultTextureInnerEnabled(false), _defaultTextureOuterEnabled(false), _contactIndex(INVALID_CONTACT_INDEX)
80   -{
81   -}
82   -
83   -Gamepad::Joystick::Joystick(const Joystick& joystick)
84   -{
85   -}
86   -
87   -Gamepad::Joystick::~Joystick()
88   -{
89   -}
90   -
91   -void Gamepad::setButton(unsigned int buttonId, const Rectangle& screenRegion, const Rectangle& releasedTextureRegion, const Rectangle& pressedTextureRegion)
92   -{
93   - assert(buttonId < _buttonCount);
94   -
95   - _buttons[buttonId]->_region.x = screenRegion.x;
96   - _buttons[buttonId]->_region.y = screenRegion.y;
97   - _buttons[buttonId]->_region.width = screenRegion.width;
98   - _buttons[buttonId]->_region.height = screenRegion.height;
99   -
100   - _buttons[buttonId]->_defaultTexCoord.u1 = (releasedTextureRegion.x / _texture->getWidth());
101   - _buttons[buttonId]->_defaultTexCoord.v1 = 1.0f - (releasedTextureRegion.y / _texture->getHeight());
102   - _buttons[buttonId]->_defaultTexCoord.u2 = _buttons[buttonId]->_defaultTexCoord.u1 + (releasedTextureRegion.width / _texture->getWidth());
103   - _buttons[buttonId]->_defaultTexCoord.v2 = _buttons[buttonId]->_defaultTexCoord.v1 - (releasedTextureRegion.height / _texture->getHeight());
104   - _buttons[buttonId]->_defaultTextureEnabled = true;
105   -
106   - _buttons[buttonId]->_focusTexCoord.u1 = (pressedTextureRegion.x / _texture->getWidth());
107   - _buttons[buttonId]->_focusTexCoord.v1 = 1.0f - (pressedTextureRegion.y / _texture->getHeight());
108   - _buttons[buttonId]->_focusTexCoord.u2 = _buttons[buttonId]->_focusTexCoord.u1 + (pressedTextureRegion.width / _texture->getWidth());
109   - _buttons[buttonId]->_focusTexCoord.v2 = _buttons[buttonId]->_focusTexCoord.v1 - (pressedTextureRegion.height / _texture->getHeight());
110   - _buttons[buttonId]->_focusTextureEnabled = true;
111   -}
112   -
113   -Gamepad::ButtonState Gamepad::getButtonState(unsigned int index) const
114   -{
115   - assert(index < _buttonCount);
116   -
117   - return _buttons[index]->_pressed;
118   -}
119   -
120   -void Gamepad::setJoystick(unsigned int joystickId, const Rectangle& thumbScreenRegion, const Rectangle& thumbTextureRegion, const Rectangle& dockScreenRegion, const Rectangle& dockTextureRegion, float radius)
121   -{
122   - assert (joystickId < _joystickCount);
123   -
124   - _joysticks[joystickId]->_radius = radius;
125   -
126   - _joysticks[joystickId]->_regionInner.x = thumbScreenRegion.x;
127   - _joysticks[joystickId]->_regionInner.y = thumbScreenRegion.y;
128   - _joysticks[joystickId]->_regionInner.width = thumbScreenRegion.width;
129   - _joysticks[joystickId]->_regionInner.height = thumbScreenRegion.height;
130   -
131   - _joysticks[joystickId]->_defaultTexCoordInner.u1 = (thumbTextureRegion.x / _texture->getWidth());
132   - _joysticks[joystickId]->_defaultTexCoordInner.v1 = 1.0f - (thumbTextureRegion.y / _texture->getHeight());
133   - _joysticks[joystickId]->_defaultTexCoordInner.u2 = _joysticks[joystickId]->_defaultTexCoordInner.u1 + (thumbTextureRegion.width / _texture->getWidth());
134   - _joysticks[joystickId]->_defaultTexCoordInner.v2 = _joysticks[joystickId]->_defaultTexCoordInner.v1 - (thumbTextureRegion.height / _texture->getHeight());
135   - _joysticks[joystickId]->_defaultTextureInnerEnabled = true;
136   -
137   - _joysticks[joystickId]->_regionOuter.x = dockScreenRegion.x;
138   - _joysticks[joystickId]->_regionOuter.y = dockScreenRegion.y;
139   - _joysticks[joystickId]->_regionOuter.width = dockScreenRegion.width;
140   - _joysticks[joystickId]->_regionOuter.height = dockScreenRegion.height;
141   -
142   - _joysticks[joystickId]->_defaultTexCoordOuter.u1 = (dockTextureRegion.x / _texture->getWidth());
143   - _joysticks[joystickId]->_defaultTexCoordOuter.v1 = 1.0f - (dockTextureRegion.y / _texture->getHeight());
144   - _joysticks[joystickId]->_defaultTexCoordOuter.u2 = _joysticks[joystickId]->_defaultTexCoordOuter.u1 + (dockTextureRegion.width / _texture->getWidth());
145   - _joysticks[joystickId]->_defaultTexCoordOuter.v2 = _joysticks[joystickId]->_defaultTexCoordOuter.v1 - (dockTextureRegion.height / _texture->getHeight());
146   - _joysticks[joystickId]->_defaultTextureOuterEnabled = true;
147   -}
148   -
149   -bool Gamepad::isJoystickActive(unsigned int joystickId) const
150   -{
151   - assert(joystickId < _joystickCount);
152   -
153   - return _joysticks[joystickId]->_contactIndex != INVALID_CONTACT_INDEX;
154   -}
155   -
156   -const Vector2& Gamepad::getJoystickState(unsigned int joystickId) const
157   -{
158   - assert(joystickId < _joystickCount);
159   -
160   - return _joysticks[joystickId]->_direction;
161   -}
162   -
163   -unsigned int Gamepad::getJoystickContactIndex(unsigned int joystickId) const
164   -{
165   - assert(joystickId < _joystickCount);
166   -
167   - return _joysticks[joystickId]->_contactIndex;
168   -}
169   -
170   -void Gamepad::setSpriteBatch(SpriteBatch* spriteBatch)
171   -{
172   - _spriteBatch = spriteBatch;
173   -}
174   -
175   -void Gamepad::draw(const Vector4& color)
176   -{
177   - _spriteBatch->begin();
178   -
179   - // Draw buttons.
180   - for (unsigned int i = 0; i < _buttonCount; ++i)
181   - {
182   - if ((_buttons[i]->_pressed == BUTTON_PRESSED) && (_buttons[i]->_focusTextureEnabled))
183   - {
184   - _spriteBatch->draw(_buttons[i]->_region.x, _buttons[i]->_region.y, _buttons[i]->_region.width, _buttons[i]->_region.height,
185   - _buttons[i]->_focusTexCoord.u1, _buttons[i]->_focusTexCoord.v1, _buttons[i]->_focusTexCoord.u2, _buttons[i]->_focusTexCoord.v2,
186   - color);
187   - continue;
188   - }
189   - else if (_buttons[i]->_defaultTextureEnabled)
190   - {
191   - _spriteBatch->draw(_buttons[i]->_region.x, _buttons[i]->_region.y, _buttons[i]->_region.width, _buttons[i]->_region.height,
192   - _buttons[i]->_defaultTexCoord.u1, _buttons[i]->_defaultTexCoord.v1, _buttons[i]->_defaultTexCoord.u2, _buttons[i]->_defaultTexCoord.v2,
193   - color);
194   - }
195   - }
196   -
197   - // Draw joysticks.
198   - for (unsigned int i = 0; i < _joystickCount; ++i)
199   - {
200   - float x, y, width, height;
201   -
202   - if (_joysticks[i]->_defaultTextureOuterEnabled)
203   - {
204   - // Draw Outer joggle.
205   - x = _joysticks[i]->_regionOuter.x;
206   - y = _joysticks[i]->_regionOuter.y;
207   - width = _joysticks[i]->_regionOuter.width;
208   - height = _joysticks[i]->_regionOuter.height;
209   -
210   - _spriteBatch->draw(x, y, width, height, _joysticks[i]->_defaultTexCoordOuter.u1, _joysticks[i]->_defaultTexCoordOuter.v1, _joysticks[i]->_defaultTexCoordOuter.u2, _joysticks[i]->_defaultTexCoordOuter.v2, color);
211   - }
212   -
213   - if (_joysticks[i]->_defaultTextureInnerEnabled)
214   - {
215   - // Draw Inner joggle.
216   - x = _joysticks[i]->_regionInner.x;
217   - y = _joysticks[i]->_regionInner.y;
218   - width = _joysticks[i]->_regionInner.width;
219   - height = _joysticks[i]->_regionInner.height;
220   - float radius = _joysticks[i]->_radius;
221   - float displacementX = _joysticks[i]->_displacement.x;
222   - float displacementY = _joysticks[i]->_displacement.y;
223   -
224   - // Move position to reflect displacement.
225   - if (((displacementX * displacementX) + (displacementY * displacementY)) <= (radius * radius))
226   - {
227   - x += displacementX;
228   - y += displacementY;
229   - }
230   - else
231   - {
232   - // Find the point on the joystick's circular bound where
233   - // the vector intersects. This would be the position of the
234   - // inner joggle.
235   - Vector2 delta = Vector2(displacementX, displacementY);
236   - delta.normalize();
237   - delta.scale(radius);
238   - x += delta.x;
239   - y += delta.y;
240   - }
241   -
242   - _spriteBatch->draw(x, y, width, height, _joysticks[i]->_defaultTexCoordInner.u1, _joysticks[i]->_defaultTexCoordInner.v1, _joysticks[i]->_defaultTexCoordInner.u2, _joysticks[i]->_defaultTexCoordInner.v2, color);
243   - }
244   - }
245   - _spriteBatch->end();
246   -}
247   -
248   -void Gamepad::touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex)
249   -{
250   - for (unsigned int i = 0; i < _buttonCount; ++i)
251   - {
252   - switch (evt)
253   - {
254   - case Touch::TOUCH_PRESS:
255   - {
256   - if (_buttons[i]->_contactIndex == INVALID_CONTACT_INDEX)
257   - {
258   - if ((x >= _buttons[i]->_region.x) && (x <= (_buttons[i]->_region.x + _buttons[i]->_region.width)) &&
259   - (y >= _buttons[i]->_region.y) && (y <= (_buttons[i]->_region.y + _buttons[i]->_region.height)))
260   - {
261   - _buttons[i]->_contactIndex = contactIndex;
262   - _buttons[i]->_pressed = BUTTON_PRESSED;
263   - }
264   - }
265   - }
266   - break;
267   - case Touch::TOUCH_RELEASE:
268   - {
269   - if (_buttons[i]->_contactIndex == contactIndex)
270   - {
271   - _buttons[i]->_contactIndex = -1;
272   - _buttons[i]->_pressed = BUTTON_RELEASED;
273   - }
274   - }
275   - break;
276   - }
277   - }
278   -
279   - for (unsigned int i = 0; i < _joystickCount; ++i)
280   - {
281   - switch (evt)
282   - {
283   - case Touch::TOUCH_PRESS:
284   - {
285   - // Activate the joystick only when it is touched inside the
286   - // outer joggle.
287   - float bx = _joysticks[i]->_regionInner.x + _joysticks[i]->_regionInner.width * 0.5f;
288   - float by = _joysticks[i]->_regionInner.y + _joysticks[i]->_regionInner.height * 0.5f;
289   - float bradius = _joysticks[i]->_radius;
290   -
291   - if ((x >= (bx - bradius)) && (x <= (bx + bradius)) &&
292   - (y >= (by - bradius)) && (y <= (by + bradius)) && _joysticks[i]->_contactIndex == INVALID_CONTACT_INDEX)
293   - {
294   - _joysticks[i]->_contactIndex = contactIndex;
295   - _joysticks[i]->_displacement.set(0.0f, 0.0f);
296   - _joysticks[i]->_direction.set(0.0f, 0.0f);
297   - }
298   - }
299   - case Touch::TOUCH_MOVE:
300   - {
301   - if (_joysticks[i]->_contactIndex == contactIndex)
302   - {
303   - float displacementX = x - (_joysticks[i]->_regionInner.x + _joysticks[i]->_regionInner.width * 0.5f);
304   - float displacementY = y - (_joysticks[i]->_regionInner.y + _joysticks[i]->_regionInner.height * 0.5f);
305   - float radius = _joysticks[i]->_radius;
306   -
307   - // Determine the position of the finger. If finger moves beyond the joystick radius,
308   - // clamp the direction vector to bounding circle.
309   - if (((displacementX * displacementX) + (displacementY * displacementY)) <= (radius * radius))
310   - {
311   - // Negate the y in the direction vector so that y is positive up.
312   - _joysticks[i]->_direction.set(displacementX, -displacementY);
313   - _joysticks[i]->_direction.scale(1.0f / radius);
314   - }
315   - else
316   - {
317   - // Negate the displacementY here so that y is positive up.
318   - Vector2 delta = Vector2(displacementX, -displacementY);
319   - delta.normalize();
320   - delta.scale(radius);
321   - delta.normalize();
322   - _joysticks[i]->_direction.set(delta);
323   - }
324   -
325   - _joysticks[i]->_displacement.set(displacementX, displacementY);
326   - }
327   - }
328   - break;
329   - case Touch::TOUCH_RELEASE:
330   - {
331   - if (_joysticks[i]->_contactIndex == contactIndex)
332   - {
333   - // Reset displacement and direction vectors.
334   - _joysticks[i]->_contactIndex = -1;
335   - _joysticks[i]->_displacement.set(0.0f, 0.0f);
336   - _joysticks[i]->_direction.set(0.0f, 0.0f);
337   - }
338   - }
339   - break;
340   - }
341   - }
342   -}
218 gameplay-samples/sample03-character/src/Gamepad.h
... ... @@ -1,218 +0,0 @@
1   -#ifndef GAMEPAD_H_
2   -#define GAMEPAD_H_
3   -
4   -#include "Keyboard.h"
5   -#include "Properties.h"
6   -#include "SpriteBatch.h"
7   -#include "Touch.h"
8   -#include "Vector2.h"
9   -
10   -using namespace gameplay;
11   -
12   -/**
13   - * Defines a class for accessing gamepad controls: Buttons and Joysticks.
14   - */
15   -class Gamepad
16   -{
17   -public:
18   -
19   - /**
20   - * The button state's.
21   - */
22   - enum ButtonState
23   - {
24   - BUTTON_RELEASED,
25   - BUTTON_PRESSED
26   - };
27   -
28   - /**
29   - * Constructor.
30   - *
31   - * @param texturePath The path to the gamepad's texture atlas.
32   - * @param joysticks The number of joysticks the game pad will have.
33   - * @param buttons The number of buttons the game pad will have.
34   - */
35   - Gamepad(const char* texturePath, unsigned int joystickCount, unsigned int buttonCount);
36   -
37   - /**
38   - * Destructor.
39   - */
40   - ~Gamepad();
41   -
42   - /**
43   - * Sets the screen region and textures to use for the specified button on the gamepad.
44   - *
45   - * @param buttonId The unique integer ID of the button to set.
46   - * @param screenRegion The region on the screen to draw the button.
47   - * @param releasedTextureRegion The region on the gamepad's texture atlas to draw as the BUTTON_RELEASED state.
48   - * @param pressedTextureRegion The region on the gamepad's texture atlas to draw as the BUTTON_PRESSED state.
49   - */
50   - void setButton(unsigned int buttonId, const Rectangle& screenRegion, const Rectangle& releasedTextureRegion, const Rectangle& pressedTextureRegion);
51   -
52   - /**
53   - * Returns the current state of the specified button.
54   - *
55   - * @param buttonId The ID of the button on the gamepad.
56   - * @return The state of the specified button. BUTTON_PRESSED if the button is pressed; BUTTON_RELEASED otherwise.
57   - */
58   - ButtonState getButtonState(unsigned int buttonId) const;
59   -
60   - /**
61   - * Sets the screen region and textures to use for the specified joystick on the gamepad.
62   - *
63   - * @param joystickId The unique integer ID of the joystick to set.
64   - * @param thumbScreenRegion The region on the screen to draw the thumb stick of the joystick.
65   - * @param thumbTextureRegion The region on the gamepad's texture atlas to draw as the thumb stick of the joystick.
66   - * @param dockScreenRegion The region on the screen to draw the dock that the thumb stick sits in.
67   - * @param dockTextureRegion The region on the gamepad's texture atlas to draw as the dock for the thumb stick of the joystick.
68   - * @param radius The maximum radius that the thumb stick will revolve around the dock region.
69   - */
70   - void setJoystick(unsigned int joystickId, const Rectangle& thumbScreenRegion, const Rectangle& thumbTextureRegion, const Rectangle& dockScreenRegion, const Rectangle& dockTextureRegion, float radius);
71   -
72   - /**
73   - * Gets whether the specified joystick is active or not.
74   - *
75   - * @param joystickId The unique integer ID of the joystick to set.
76   - * @return Whether the given joystick is active or not.
77   - */
78   - bool isJoystickActive(unsigned int joystickId) const;
79   -
80   - /**
81   - * Returns the specified joystick's state as a Vector2.
82   - *
83   - * @param joystickId The unique integer ID of the joystick to set.
84   - * @return A Vector2 of the joystick displacement for the specified joystick.
85   - */
86   - const Vector2& getJoystickState(unsigned int joystickId) const;
87   -
88   - /**
89   - * Returns the current touch contact index for the specified joystick.
90   - *
91   - * @return The contact index for the specified joystick.
92   - */
93   - unsigned int getJoystickContactIndex(unsigned int joystickId) const;
94   -
95   - /**
96   - * Draws the gamepad.
97   - *
98   - * @param color
99   - */
100   - void draw(const Vector4& color = Vector4::one());
101   -
102   - /**
103   - * Input callback on keyPress events.
104   - *
105   - * @param key The key code pressed.
106   - * @param keyEvent The key event that occured.
107   - *
108   - * @see Input::Key
109   - * @see Input::KeyEvent
110   - */
111   - void keyPress(int key, int keyEvent);
112   -
113   - /**
114   - * Input callback on touch events.
115   - *
116   - * @param x The x position of the touch.
117   - * @param y The y position of the touch.
118   - * @param touchEvent The touch event that occurred.
119   - *
120   - * @see Input::TouchEvent
121   - */
122   - void touchEvent(Touch::TouchEvent evt, int x, int y, unsigned int contactIndex);
123   -
124   -private:
125   -
126   - /**
127   - * Hidden copy constructor.
128   - */
129   - Gamepad(const Gamepad* gamepad);
130   -
131   - /**
132   - * Sets a spritebatch to this gamepad.
133   - */
134   - void setSpriteBatch(SpriteBatch* spriteBatch);
135   -
136   - /**
137   - * Structure for holding texture coordinates for a rectangle.
138   - */
139   - struct TexCoords
140   - {
141   - float u1;
142   - float u2;
143   - float v1;
144   - float v2;
145   - };
146   -
147   - /**
148   - * Defines a gamepad button.
149   - */
150   - class Button
151   - {
152   - public:
153   - /**
154   - * Constructor.
155   - */
156   - Button();
157   -
158   - /**
159   - * Copy constructor.
160   - */
161   - Button(const Button& button);
162   -
163   - /**
164   - * Destructor.
165   - */
166   - ~Button();
167   -
168   - Gamepad::ButtonState _pressed;
169   - Rectangle _region;
170   - bool _defaultTextureEnabled;
171   - TexCoords _defaultTexCoord;
172   - bool _focusTextureEnabled;
173   - TexCoords _focusTexCoord;
174   - unsigned int _contactIndex;
175   - };
176   -
177   - /**
178   - * Defines a gamepad joystick.
179   - */