Browse Source

create style file

master
sauer2 2 years ago
parent
commit
b44f1cb545
6 changed files with 106 additions and 110 deletions
  1. 4
    0
      .clang-format
  2. BIN
      game
  3. 55
    59
      main.cpp
  4. 2
    3
      main.hpp
  5. 32
    34
      mainmenu.cpp
  6. 13
    14
      mainmenu.hpp

+ 4
- 0
.clang-format View File

@@ -0,0 +1,4 @@
1
+---
2
+Language: Cpp
3
+IndentWidth: 4
4
+NamespaceIndentation: All

BIN
game View File


+ 55
- 59
main.cpp View File

@@ -1,15 +1,16 @@
1 1
 #include <iostream>
2 2
 #include <string>
3 3
 
4
+#include <SFML/Graphics.hpp>
4 5
 #include <SFML/System.hpp>
5 6
 #include <SFML/Window.hpp>
6
-#include <SFML/Graphics.hpp>
7 7
 
8 8
 #include "main.hpp"
9 9
 #include "mainmenu.hpp"
10 10
 
11 11
 namespace Game {
12
-    // because we want to create the objects in init(), we use the default constructor, so nothing can go wrong here
12
+    // because we want to create the objects in init(), we use the default
13
+    // constructor, so nothing can go wrong here
13 14
     sf::RenderWindow window{};
14 15
 
15 16
     GameState state = GameState::MainMenu;
@@ -17,111 +18,106 @@ namespace Game {
17 18
     sf::Font font;
18 19
 
19 20
     bool init() {
20
-    
21
+
21 22
         sf::ContextSettings settings;
22
-        
23
+
23 24
         // set antialising for shapes
24 25
         // this doesnt affect textures
25 26
         // (requires sf::Texture::setSmooth() )
26 27
         settings.antialiasingLevel = 4;
27
-        
28
-        window.create(sf::VideoMode(xResolution, yResolution, colorDepth), gameTitle, sf::Style::Default, settings);
28
+
29
+        window.create(sf::VideoMode(xResolution, yResolution, colorDepth),
30
+                      gameTitle, sf::Style::Default, settings);
29 31
         window.setPosition(sf::Vector2i(0, 0));
30
-        
32
+
31 33
         // hide mouse cursor
32 34
         window.setMouseCursorVisible(false);
33
-        
34
-        if(!window.isOpen()) {
35
+
36
+        if (!window.isOpen()) {
35 37
             std::cout << "Unable to open window object. Aborting execution.";
36 38
             return false;
37 39
         }
38 40
 
39
-        if (!font.loadFromFile("Audiowide-Regular.ttf")) { 
41
+        if (!font.loadFromFile("Audiowide-Regular.ttf")) {
40 42
             std::cout << "Unable to load font. Aborting execution.";
41 43
             return false;
42 44
         }
43 45
 
44 46
         menu.init(font);
45
-        
47
+
46 48
         return true;
47 49
     }
48 50
 
49 51
     void update() {
50
-        switch(state) {
51
-            case GameState::Game:
52
-                // state = update game with updateTimestep.asSeconds()
53
-                if(state != GameState::Game) {
54
-                    // game.uninit();
55
-                }
56
-                break;
57
-                
58
-            case GameState::MainMenu:
59
-                state = menu.update(window);
60
-                if(state == GameState::Game) {
61
-                    // start game
62
-                }
63
-                break;
64
-            
65
-            case GameState::Exit:
66
-                window.close();
67
-                break;
52
+        switch (state) {
53
+        case GameState::Game:
54
+            // state = update game with updateTimestep.asSeconds()
55
+            if (state != GameState::Game) {
56
+                // game.uninit();
57
+            }
58
+            break;
59
+
60
+        case GameState::MainMenu:
61
+            state = menu.update(window);
62
+            if (state == GameState::Game) {
63
+                // start game
64
+            }
65
+            break;
66
+
67
+        case GameState::Exit:
68
+            window.close();
69
+            break;
68 70
         }
69 71
     }
70 72
 
71 73
     void draw(float lagTime) {
72
-    
74
+
73 75
         window.clear(sf::Color::Black);
74
-        
75
-        switch(state) {
76
-            case GameState::Game:
77
-                // draw game with lagTime
78
-                break;
79
-                
80
-            case GameState::MainMenu:
81
-                menu.draw(window);
82
-                break;
83
-            
84
-            case GameState::Exit:
85
-                break;
76
+
77
+        switch (state) {
78
+        case GameState::Game:
79
+            // draw game with lagTime
80
+            break;
81
+
82
+        case GameState::MainMenu:
83
+            menu.draw(window);
84
+            break;
85
+
86
+        case GameState::Exit:
87
+            break;
86 88
         }
87
-        
89
+
88 90
         window.display();
89 91
     }
90 92
 
91 93
     void mainloop() {
92
-    
94
+
93 95
         sf::Clock clock;
94 96
         sf::Time previousTime = sf::milliseconds(0);
95 97
         sf::Time lagTime = sf::milliseconds(0);
96
-        
97
-        while(window.isOpen()) {
98
+
99
+        while (window.isOpen()) {
98 100
             sf::Time currentTime = clock.getElapsedTime();
99 101
             sf::Time elapsedTime = currentTime - previousTime;
100 102
             previousTime = currentTime;
101 103
             lagTime += elapsedTime;
102
-            
104
+
103 105
             while (lagTime >= updateTimestep) {
104 106
                 update();
105 107
                 lagTime -= updateTimestep;
106 108
             }
107 109
             draw(lagTime.asSeconds());
108 110
         }
109
-    
110 111
     }
111
-    
112
-    
112
+
113 113
     int run() {
114
-        if(!init()) return 1;
115
-        
114
+        if (!init())
115
+            return 1;
116
+
116 117
         mainloop();
117
-        
118
+
118 119
         return 0;
119 120
     }
120
-
121 121
 }
122 122
 
123
-
124
-
125
-int main() {
126
-    return Game::run();
127
-}
123
+int main() { return Game::run(); }

+ 2
- 3
main.hpp View File

@@ -5,11 +5,10 @@
5 5
 
6 6
 #pragma once
7 7
 
8
-
9 8
 namespace Game {
10 9
 
11 10
     enum class GameState { Game, MainMenu, Exit };
12
-    
11
+
13 12
     // configuration
14 13
     const int xResolution = 1150;
15 14
     const int yResolution = 750;
@@ -17,7 +16,7 @@ namespace Game {
17 16
     const int colorDepth = 32;
18 17
     const std::string gameTitle = "Proto4";
19 18
     const sf::Time updateTimestep = sf::seconds(0.0004);
20
-    
19
+
21 20
     const float middleX = xResolution / 2;
22 21
     const float middleY = yResolution / 2;
23 22
 }

+ 32
- 34
mainmenu.cpp View File

@@ -1,59 +1,57 @@
1
+#include <SFML/Graphics.hpp>
1 2
 #include <SFML/System.hpp>
2 3
 #include <SFML/Window.hpp>
3
-#include <SFML/Graphics.hpp>
4 4
 
5
-#include "mainmenu.hpp"
6 5
 #include "main.hpp"
6
+#include "mainmenu.hpp"
7 7
 
8 8
 namespace Game {
9 9
 
10 10
     void MainMenu::draw(sf::RenderWindow &window) {
11 11
         window.setView(view);
12
-		window.draw(mainLine);
13
-		window.draw(entry1);
14
-		window.draw(entry2);
12
+        window.draw(mainLine);
13
+        window.draw(entry1);
14
+        window.draw(entry2);
15 15
     }
16 16
 
17 17
     GameState MainMenu::update(sf::RenderWindow &window) {
18 18
         sf::Event event;
19
-        
20
-        while( window.pollEvent(event) ) {
21
-            if( event.type == sf::Event::Closed ) {
19
+
20
+        while (window.pollEvent(event)) {
21
+            if (event.type == sf::Event::Closed) {
22 22
                 window.close();
23
-            }
24
-            else if(event.type == sf::Event::Resized) {
25
-                //resize the views, so everything is still displayed proportionally
23
+            } else if (event.type == sf::Event::Resized) {
24
+                // resize the views, so everything is still displayed
25
+                // proportionally
26 26
                 view.setSize(event.size.width, event.size.height);
27
-                view.setCenter(event.size.width/2, event.size.height/2);
27
+                view.setCenter(event.size.width / 2, event.size.height / 2);
28
+            } else if (event.type == sf::Event::KeyPressed) {
29
+                if (event.key.code == sf::Keyboard::Q)
30
+                    return GameState::Exit;
31
+                if (event.key.code == sf::Keyboard::S)
32
+                    return GameState::Game;
28 33
             }
29
-			else if(event.type == sf::Event::KeyPressed) {
30
-				if(event.key.code == sf::Keyboard::Q) return GameState::Exit;
31
-				if(event.key.code == sf::Keyboard::S) return GameState::Game;
32
-			}
33 34
         }
34
-        
35
-        
35
+
36 36
         return GameState::MainMenu;
37 37
     }
38
-    
38
+
39 39
     void MainMenu::init(sf::Font &font) {
40
-		font = font;
40
+        font = font;
41 41
         view.setCenter(middleX, middleY);
42 42
         view.setSize(xResolution, yResolution);
43 43
         view.setViewport(sf::FloatRect(0, 0, 1, 1));
44
-        
45
-		mainLine.setString("Proto 4");
46
-		entry1.setString("[S] Start New Game");
47
-		entry2.setString("[Q] Quit Game");
48
-
49
-		mainLine.setFont(font);
50
-		entry1.setFont(font);
51
-		entry2.setFont(font);
52
-
53
-		mainLine.setPosition(32.f, 32.f);
54
-		entry1.setPosition(32.f, 96.f);
55
-		entry2.setPosition(32.f, 160.f);
56
-    }
57 44
 
58
-}
45
+        mainLine.setString("Proto 4");
46
+        entry1.setString("[S] Start New Game");
47
+        entry2.setString("[Q] Quit Game");
59 48
 
49
+        mainLine.setFont(font);
50
+        entry1.setFont(font);
51
+        entry2.setFont(font);
52
+
53
+        mainLine.setPosition(32.f, 32.f);
54
+        entry1.setPosition(32.f, 96.f);
55
+        entry2.setPosition(32.f, 160.f);
56
+    }
57
+}

+ 13
- 14
mainmenu.hpp View File

@@ -1,26 +1,25 @@
1 1
 #pragma once
2 2
 
3
+#include <SFML/Graphics.hpp>
3 4
 #include <SFML/System.hpp>
4 5
 #include <SFML/Window.hpp>
5
-#include <SFML/Graphics.hpp>
6 6
 
7 7
 #include "main.hpp"
8 8
 
9 9
 namespace Game {
10
-    
10
+
11 11
     class MainMenu {
12
-        public:
13
-            void draw(sf::RenderWindow &window);
14
-            GameState update(sf::RenderWindow &window);
15
-            void init(sf::Font &font);
16
-            
17
-        private:
18
-            sf::View view{};
19
-			sf::Font *font = nullptr;
12
+      public:
13
+        void draw(sf::RenderWindow &window);
14
+        GameState update(sf::RenderWindow &window);
15
+        void init(sf::Font &font);
20 16
 
21
-			sf::Text mainLine;
22
-			sf::Text entry1;
23
-			sf::Text entry2;
24
-    };
17
+      private:
18
+        sf::View view{};
19
+        sf::Font *font = nullptr;
25 20
 
21
+        sf::Text mainLine;
22
+        sf::Text entry1;
23
+        sf::Text entry2;
24
+    };
26 25
 }

Loading…
Cancel
Save