Browse Source

move gameView to Game, add Game:handleKey, cleanup

master
noerw 1 year ago
parent
commit
2ddef99993
6 changed files with 60 additions and 46 deletions
  1. 14
    19
      background.cpp
  2. 20
    1
      game.cpp
  3. 4
    0
      game.hpp
  4. 20
    23
      main.cpp
  5. 1
    2
      mainmenu.cpp
  6. 1
    1
      mainmenu.hpp

+ 14
- 19
background.cpp View File

@@ -3,11 +3,10 @@
3 3
 #include <SFML/Window.hpp>
4 4
 
5 5
 #include "background.hpp"
6
-#include "main.hpp"
7 6
 
8 7
 namespace Proto4 {
9 8
     void Background::init(sf::Texture bg0, sf::Texture bg1,
10
-                          sf::View &gameView) {
9
+                          sf::View &rootView) {
11 10
         bg0Tex = bg0;
12 11
         bg1Tex = bg1;
13 12
         bg0Tex.setRepeated(true);
@@ -17,31 +16,27 @@ namespace Proto4 {
17 16
         bg0Sprite.setTexture(bg0Tex);
18 17
         bg1Sprite.setTexture(bg1Tex);
19 18
 
20
-        bg0View = bg1View = gameView;
19
+        bg0View = bg1View = rootView;
21 20
         bg0Size = bg0Tex.getSize().x;
22 21
         bg1Size = bg1Tex.getSize().x;
23
-        bg0Sprite.setTextureRect(sf::IntRect(0, 0, xResolution + 2 * bg0Size,
24
-                                             yResolution + 2 * bg0Size));
25
-        bg1Sprite.setTextureRect(sf::IntRect(0, 0, xResolution + 2 * bg1Size,
26
-                                             yResolution + 2 * bg1Size));
27
-        // bg0Sprite.setPosition(window.mapPixelToCoords(sf::Vector2i(-bg0Size,
28
-        // -bg0Size), bg0View));
29
-        // bg1Sprite.setPosition(window.mapPixelToCoords(sf::Vector2i(-bg1Size,
30
-        // -bg1Size), bg1View));
31
-        bg0Sprite.setPosition(gameView.getCenter() - gameView.getSize() * 0.5f);
32
-        bg1Sprite.setPosition(gameView.getCenter() - gameView.getSize() * 0.5f);
22
+        sf::Vector2f rootViewSize = rootView.getSize();
23
+        bg0Sprite.setTextureRect(sf::IntRect(0, 0,
24
+                                             rootViewSize.x + 2 * bg0Size,
25
+                                             rootViewSize.y + 2 * bg0Size));
26
+        bg1Sprite.setTextureRect(sf::IntRect(0, 0,
27
+                                             rootViewSize.x + 2 * bg0Size,
28
+                                             rootViewSize.y + 2 * bg0Size));
29
+        bg0Sprite.setPosition(rootView.getCenter() - rootViewSize * 0.5f);
30
+        bg1Sprite.setPosition(rootView.getCenter() - rootViewSize * 0.5f);
33 31
     }
34 32
 
35
-    void Background::update(sf::View &gameView, sf::RenderTarget &window) {
33
+    void Background::update(sf::View &rootView, sf::RenderTarget &window) {
36 34
         // parallax
37
-        bg0View.setCenter(gameView.getCenter() / 3.5f);
38
-        bg1View.setCenter(gameView.getCenter() / 2.f);
35
+        bg0View.setCenter(rootView.getCenter() / 3.5f);
36
+        bg1View.setCenter(rootView.getCenter() / 2.f);
39 37
 
40 38
         // move endless backgrounds if parallax has moved one -step, to offset
41 39
         // parallax-movement
42
-        // sf::Vector2f bg0Pos = gameView.getCenter() - bg0View.getCenter(); //
43
-        // alternative solution without coord2pixel transformation? doesnt work
44
-        // currently
45 40
         sf::Vector2i bg0Pos =
46 41
             window.mapCoordsToPixel(bg0Sprite.getPosition(), bg0View);
47 42
         if (bg0Pos.x >= 0)

+ 20
- 1
game.cpp View File

@@ -2,17 +2,29 @@
2 2
 #include <SFML/System.hpp>
3 3
 #include <SFML/Window.hpp>
4 4
 
5
+#include "main.hpp"
5 6
 #include "game.hpp"
6 7
 
7 8
 namespace Proto4 {
8 9
 
9
-    bool Game::init() { return true; }
10
+    bool Game::init() {
11
+        mainView.setCenter(0, 0);
12
+        mainView.setSize(xResolution, yResolution);
13
+        return true;
14
+    }
15
+
16
+    AppState Game::handleKey(const sf::Event &event) {
17
+        if (event.key.code == sf::Keyboard::Q)
18
+            return AppState::MainMenu;
19
+        return AppState::Game;
20
+    }
10 21
 
11 22
     AppState Game::update(sf::RenderWindow &window, sf::Time timestep) {
12 23
         return AppState::Game;
13 24
     }
14 25
 
15 26
     void Game::draw(sf::RenderTarget &window, sf::RenderStates states) const {
27
+        window.setView(mainView);
16 28
         window.draw(player);
17 29
     }
18 30
 
@@ -23,4 +35,11 @@ namespace Proto4 {
23 35
     bool Game::resume() { return true; }
24 36
 
25 37
     bool Game::reset() { return true; }
38
+
39
+    void Game::resize(sf::Event &resizeEvent) {
40
+        mainView.setSize(resizeEvent.size.width, resizeEvent.size.height);
41
+        mainView.setCenter(resizeEvent.size.width / 2, resizeEvent.size.height / 2);
42
+    }
43
+
44
+    sf::View& Game::getMainView() { return mainView; }
26 45
 }

+ 4
- 0
game.hpp View File

@@ -16,6 +16,9 @@ namespace Proto4 {
16 16
         public:
17 17
         virtual void draw(sf::RenderTarget &window, sf::RenderStates states) const;
18 18
         AppState update(sf::RenderWindow &window, sf::Time timestep);
19
+        AppState handleKey(const sf::Event &event);
20
+        void resize(sf::Event& resizeEvent);
21
+        sf::View& getMainView();
19 22
 
20 23
         bool init();
21 24
 
@@ -25,6 +28,7 @@ namespace Proto4 {
25 28
         bool reset();
26 29
 
27 30
         private:
31
+        sf::View mainView{};
28 32
         GameState gamestate = GameState::Uninitialized;
29 33
         Player player{};
30 34
         uint32_t score = 0;

+ 20
- 23
main.cpp View File

@@ -6,9 +6,9 @@
6 6
 #include <SFML/System.hpp>
7 7
 #include <SFML/Window.hpp>
8 8
 
9
+#include "main.hpp"
9 10
 #include "background.hpp"
10 11
 #include "game.hpp"
11
-#include "main.hpp"
12 12
 #include "mainmenu.hpp"
13 13
 
14 14
 namespace Proto4 {
@@ -20,7 +20,6 @@ namespace Proto4 {
20 20
     MainMenu menu{};
21 21
     Game game{};
22 22
     Background background{};
23
-    sf::View gameView;
24 23
     sf::Texture bg0, bg1;
25 24
     sf::Font font;
26 25
 
@@ -38,19 +37,14 @@ namespace Proto4 {
38 37
             return false;
39 38
         }
40 39
 
41
-        gameView.setCenter(middleX, middleY);
42
-        gameView.setSize(xResolution, yResolution);
43
-        gameView.setViewport(sf::FloatRect(0, 0, 1, 1));
44
-
45
-        background.init(bg0, bg1, gameView);
40
+        background.init(bg0, bg1, game.getMainView());
46 41
 
47 42
         if (!menu.init(font) || !game.init()) {
48 43
             return false;
49 44
         }
50 45
 
51 46
         // set antialising for shapes
52
-        // this doesnt affect textures
53
-        // (requires sf::Texture::setSmooth() )
47
+        // this doesnt affect textures (requires sf::Texture::setSmooth())
54 48
         sf::ContextSettings settings;
55 49
         settings.antialiasingLevel = 4;
56 50
 
@@ -79,13 +73,14 @@ namespace Proto4 {
79 73
                 background.resize(event);
80 74
 
81 75
                 switch (state) {
82
-
83 76
                 case AppState::MainMenu:
84 77
                     menu.resize(event);
85 78
                     break;
86 79
 
87
-                /* TODO: add a resize method to the game object, similar to
88
-                 * the one in mainmenu */
80
+                case AppState::Game:
81
+                    game.resize(event);
82
+                    break;
83
+
89 84
                 default:
90 85
                     break;
91 86
                 }
@@ -94,9 +89,11 @@ namespace Proto4 {
94 89
             case sf::Event::KeyPressed:
95 90
                 switch (state) {
96 91
                 case AppState::MainMenu:
97
-                    state = menu.update(window, timestep, event);
92
+                    state = menu.handleKey(event);
93
+                    break;
94
+                case AppState::Game:
95
+                    state = game.handleKey(event);
98 96
                     break;
99
-                /* TODO: add keyboard input queue for game */
100 97
                 default:
101 98
                     break;
102 99
                 }
@@ -106,35 +103,35 @@ namespace Proto4 {
106 103
             }
107 104
         }
108 105
 
109
-        // (temporary!) move camera to show off parallax background
110
-        gameView.setCenter(sf::Vector2f(
111
-            gameView.getCenter().x +
112
-                timestep.asSeconds() * 100 * std::sin(currentTime.asSeconds()),
113
-            gameView.getCenter().y + timestep.asSeconds() * 100));
114
-        background.update(gameView, window);
115
-
116 106
         // Update everything that gets updated without events...
107
+        sf::View &gameView = game.getMainView();
117 108
         switch (state) {
109
+        case AppState::MainMenu:
110
+            // move camera for background movement
111
+            gameView.setCenter(sf::Vector2f(
112
+                gameView.getCenter().x + timestep.asSeconds() * 100 * std::sin(currentTime.asSeconds()),
113
+                gameView.getCenter().y - timestep.asSeconds() * 100));
114
+            break;
118 115
         case AppState::Game:
119 116
             state = game.update(window, timestep);
120 117
             if (state != AppState::Game) {
121 118
                 game.reset();
122 119
             }
123 120
             break;
124
-
125 121
         case AppState::Exit:
126 122
             window.close();
127 123
             break;
128 124
         default:
129 125
             break;
130 126
         }
127
+
128
+        background.update(gameView, window);
131 129
     }
132 130
 
133 131
     void draw() {
134 132
 
135 133
         window.clear(sf::Color::Black);
136 134
         window.draw(background);
137
-        window.setView(gameView);
138 135
 
139 136
         switch (state) {
140 137
         case AppState::Game:

+ 1
- 2
mainmenu.cpp View File

@@ -38,8 +38,7 @@ namespace Proto4 {
38 38
     }
39 39
 
40 40
     // Unlike in game, this gets only called on keypresses.
41
-    AppState MainMenu::update(sf::RenderWindow &window, sf::Time timestep,
42
-                              sf::Event keyEvent) {
41
+    AppState MainMenu::handleKey(const sf::Event &keyEvent) {
43 42
         if (keyEvent.key.code == sf::Keyboard::Q)
44 43
             return AppState::Exit;
45 44
         if (keyEvent.key.code == sf::Keyboard::S)

+ 1
- 1
mainmenu.hpp View File

@@ -11,7 +11,7 @@ namespace Proto4 {
11 11
     class MainMenu : public sf::Drawable, public sf::Transformable {
12 12
       public:
13 13
         bool init(sf::Font &font);
14
-        AppState update(sf::RenderWindow &window, sf::Time timestep, sf::Event keyEvent);
14
+        AppState handleKey(const sf::Event &keyEvent);
15 15
         virtual void draw(sf::RenderTarget &window, sf::RenderStates states) const;
16 16
         void resize(sf::Event& resizeEvent);
17 17
 

Loading…
Cancel
Save