Browse Source

fix build, change namespace, reorder init

master
noerw 1 year ago
parent
commit
59423478d8
8 changed files with 30 additions and 69 deletions
  1. 2
    0
      .gitignore
  2. 1
    1
      Makefile
  3. 1
    1
      game.cpp
  4. 1
    46
      game.hpp
  5. 19
    18
      main.cpp
  6. 1
    1
      main.hpp
  7. 4
    1
      mainmenu.cpp
  8. 1
    1
      mainmenu.hpp

+ 2
- 0
.gitignore View File

@@ -1,3 +1,5 @@
1
+game
2
+
1 3
 *.dll
2 4
 *.exe
3 5
 *.o

+ 1
- 1
Makefile View File

@@ -8,7 +8,7 @@ LDFLAGS = -L "SFML21/lib" -lsfml-system -lsfml-window -lsfml-graphics
8 8
 # for Windows using MinGW
9 9
 #LDFLAGS = -L "SFML21/lib" -lmingw32 -lsfml-main -lsfml-system -lsfml-window -lsfml-graphics
10 10
 
11
-OBJ = mainmenu.o main.o 
11
+OBJ = game.o mainmenu.o main.o 
12 12
 
13 13
 game: $(OBJ)
14 14
 	$(CC) $(CFLAGS) -o game $(OBJ) $(LDFLAGS)

+ 1
- 1
game.cpp View File

@@ -4,7 +4,7 @@
4 4
 
5 5
 #include "game.hpp"
6 6
 
7
-namespace Game {
7
+namespace Proto4 {
8 8
 
9 9
     bool Game::init() {
10 10
         return true;

+ 1
- 46
game.hpp View File

@@ -7,7 +7,7 @@
7 7
 
8 8
 #pragma once
9 9
 
10
-namespace Game {
10
+namespace Proto4 {
11 11
     enum class GameState { Uninitialized, Running, Paused, Finished };
12 12
 
13 13
     class Game : public sf::Drawable, public sf::Transformable {
@@ -29,49 +29,4 @@ namespace Game {
29 29
         uint32_t score = 0;
30 30
 
31 31
     };
32
-
33
-    bool Game::init() {
34
-        return true;
35
-    }
36
-
37
-    AppState Game::update(sf::RenderWindow &window, sf::Time timestep) {
38
-        sf::Event event;
39
-
40
-        while (window.pollEvent(event)) {
41
-            if (event.type == sf::Event::Closed) {
42
-                window.close();
43
-            } else if (event.type == sf::Event::Resized) {
44
-                // resize the views, so everything is still displayed
45
-                // proportionally
46
-                //view.setSize(event.size.width, event.size.height);
47
-                //view.setCenter(event.size.width / 2, event.size.height / 2);
48
-            } else if (event.type == sf::Event::KeyPressed) {
49
-                if (event.key.code == sf::Keyboard::Q)
50
-                    return AppState::MainMenu;
51
-            }
52
-        }
53
-
54
-        return AppState::Game;
55
-    }
56
-
57
-    void Game::draw(sf::RenderTarget &window, sf::RenderStates states) const {
58
-    }
59
-
60
-    bool Game::start() {
61
-        return true;
62
-    }
63
-
64
-    bool Game::pause() {
65
-        return true;
66
-    }
67
-
68
-    bool Game::resume() {
69
-        return true;
70
-    }
71
-
72
-    bool Game::reset() {
73
-        return true;
74
-    }
75
-
76
-
77 32
 }

+ 19
- 18
main.cpp View File

@@ -9,7 +9,7 @@
9 9
 #include "mainmenu.hpp"
10 10
 #include "game.hpp"
11 11
 
12
-namespace Game {
12
+namespace Proto4 {
13 13
 
14 14
     // because we want to create the objects in init(), we use the default
15 15
     // constructor, so nothing can go wrong here
@@ -21,18 +21,29 @@ namespace Game {
21 21
 
22 22
     bool init() {
23 23
 
24
-        sf::ContextSettings settings;
24
+        if (!font.loadFromFile("Audiowide-Regular.ttf")) {
25
+            std::cout << "Unable to load font. Aborting execution.";
26
+            return false;
27
+        }
28
+
29
+        if (
30
+            !menu.init(font) ||
31
+            !game.init()
32
+        ) {
33
+            return false;
34
+        }
25 35
 
26 36
         // set antialising for shapes
27 37
         // this doesnt affect textures
28 38
         // (requires sf::Texture::setSmooth() )
39
+        sf::ContextSettings settings;
29 40
         settings.antialiasingLevel = 4;
30 41
 
42
+        // open window after everything has loaded to avoid
43
+        // 'window not responding' warning
31 44
         window.create(sf::VideoMode(xResolution, yResolution, colorDepth),
32 45
                       gameTitle, sf::Style::Default, settings);
33 46
         window.setPosition(sf::Vector2i(0, 0));
34
-
35
-        // hide mouse cursor
36 47
         window.setMouseCursorVisible(false);
37 48
 
38 49
         if (!window.isOpen()) {
@@ -40,18 +51,6 @@ namespace Game {
40 51
             return false;
41 52
         }
42 53
 
43
-        if (!font.loadFromFile("Audiowide-Regular.ttf")) {
44
-            std::cout << "Unable to load font. Aborting execution.";
45
-            return false;
46
-        }
47
-
48
-        if (
49
-            !menu.init(font) ||
50
-            !game.init()
51
-        ) {
52
-            return false;
53
-        }
54
-
55 54
         return true;
56 55
     }
57 56
 
@@ -100,6 +99,8 @@ namespace Game {
100 99
 
101 100
     void mainloop() {
102 101
 
102
+        // draw as often as possible and
103
+        // update multiple times with a fixed timestep as needed
103 104
         sf::Clock clock;
104 105
         sf::Time previousTime = sf::milliseconds(0);
105 106
         sf::Time lagTime = sf::milliseconds(0);
@@ -120,7 +121,7 @@ namespace Game {
120 121
 }
121 122
 
122 123
 int main() {
123
-    if (!Game::init()) return 1;
124
-    Game::mainloop();
124
+    if (!Proto4::init()) return 1;
125
+    Proto4::mainloop();
125 126
     return 0;
126 127
 }

+ 1
- 1
main.hpp View File

@@ -5,7 +5,7 @@
5 5
 
6 6
 #pragma once
7 7
 
8
-namespace Game {
8
+namespace Proto4 {
9 9
 
10 10
     enum class AppState { Game, MainMenu, Exit };
11 11
 

+ 4
- 1
mainmenu.cpp View File

@@ -5,7 +5,10 @@
5 5
 #include "main.hpp"
6 6
 #include "mainmenu.hpp"
7 7
 
8
-namespace Game {
8
+namespace Proto4 {
9
+
10
+    // IDEA: implement Menu superclass, which has a sf::Text title, vector<MenuAction> actions, and implements updating and drawing?
11
+    // eg. struct MenuAction { sf::Keyboard key, String text, AppState transition }
9 12
 
10 13
     bool MainMenu::init(sf::Font &font) {
11 14
         font = font;

+ 1
- 1
mainmenu.hpp View File

@@ -6,7 +6,7 @@
6 6
 
7 7
 #include "main.hpp"
8 8
 
9
-namespace Game {
9
+namespace Proto4 {
10 10
 
11 11
     class MainMenu : public sf::Drawable, public sf::Transformable {
12 12
       public:

Loading…
Cancel
Save