Nugget
game.hh
1 /*
2 
3 MIT License
4 
5 Copyright (c) 2022 PCSX-Redux authors
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in all
15 copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 SOFTWARE.
24 
25 */
26 
27 #pragma once
28 
29 #include <stdint.h>
30 
31 #include "psyqo/scene.hh"
32 #include "psyqo/simplepad.hh"
33 
34 // The main game scene is the most complex of all the scenes
35 // in this example. It is responsible for processing the logic
36 // of the game and reacting to all the events. It will hold
37 // a periodic timer sending tick events to advance the state
38 // of the game.
39 class MainGame final : public psyqo::Scene {
40  public:
41  // The `render` method is public in order to be called
42  // from the pause and game over scenes.
43  void render();
44 
45  private:
46  // These three methods are our familiar methods for
47  // the PSYQo framework to function.
48  void start(Scene::StartReason reason) override;
49  void frame() override;
50  void teardown(Scene::TearDownReason reason) override;
51 
52  // The `tick` method is called by the periodic timer.
53  // It is responsible for updating the state of the game.
54  // The logic is too complex to be added in-place within
55  // the timer registration. We will also call it manually
56  // in order to get certain events processed early.
57  void tick();
58  // The `buttonEvent` method is called by the input
59  // callback when the user changes a button state.
60  // The logic is too complex to be added in-place within
61  // the input callback.
62  void buttonEvent(const psyqo::SimplePad::Event& event);
63 
64  // These are helper methods for the game logic.
65  void createBlock();
66  void moveLeft();
67  void moveRight();
68  void rotateLeft();
69  void rotateRight();
70  // The `rotation` parameter is the absolute rotation
71  // value between 0 and 3.
72  void rotate(unsigned rotation);
73  // This method is responsible for updating the timer
74  // period according to the user score.
75  void recomputePeriod();
76 
77  // This is the id of the timer we're registering.
78  unsigned m_timer;
79  // The current score of the player. It will influence
80  // the speed of the game.
81  unsigned m_score;
82  // The current interval between ticks.
83  uint32_t m_period;
84  // This is basically a constant to set the periodic timer
85  // with when the player presses down. It could be an actual
86  // constant too, but this allows us to easily adjust it
87  // using debug primitives.
88  uint32_t m_fastPeriod;
89  // The current tetromino, between 1 and 7, or 0 if none,
90  // and its rotation between 0 and 3.
91  uint8_t m_currentBlock, m_blockRotation;
92  // The tetromino's location.
93  int8_t m_blockX, m_blockY;
94  // The various state transitions.
95  bool m_gameOver = false;
96  bool m_paused = false;
97  bool m_needsToUpdateFieldFragment = false;
98  bool m_needsToUpdateBlockFragment = false;
99 };
Definition: game.hh:39
The Scene class.
Definition: scene.hh:42
Definition: simplepad.hh:68