Browse Source

implement menu

master
sauer2 2 years ago
parent
commit
84a5cfd2d3
7 changed files with 192 additions and 7 deletions
  1. BIN
      Audiowide-Regular.ttf
  2. 92
    0
      Audiowide-Regular_OFL.txt
  3. 2
    2
      Makefile
  4. BIN
      game
  5. 13
    5
      main.cpp
  6. 59
    0
      mainmenu.cpp
  7. 26
    0
      mainmenu.hpp

BIN
Audiowide-Regular.ttf View File


+ 92
- 0
Audiowide-Regular_OFL.txt View File

@@ -0,0 +1,92 @@
1
+Copyright (c) 2012, Brian J. Bonislawsky DBA Astigmatic (AOETI) (astigma@astigmatic.com), with Reserved Font Names "Audiowide"
2
+This Font Software is licensed under the SIL Open Font License, Version 1.1.
3
+This license is copied below, and is also available with a FAQ at:
4
+http://scripts.sil.org/OFL
5
+
6
+
7
+-----------------------------------------------------------
8
+SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
9
+-----------------------------------------------------------
10
+
11
+PREAMBLE
12
+The goals of the Open Font License (OFL) are to stimulate worldwide
13
+development of collaborative font projects, to support the font creation
14
+efforts of academic and linguistic communities, and to provide a free and
15
+open framework in which fonts may be shared and improved in partnership
16
+with others.
17
+
18
+The OFL allows the licensed fonts to be used, studied, modified and
19
+redistributed freely as long as they are not sold by themselves. The
20
+fonts, including any derivative works, can be bundled, embedded, 
21
+redistributed and/or sold with any software provided that any reserved
22
+names are not used by derivative works. The fonts and derivatives,
23
+however, cannot be released under any other type of license. The
24
+requirement for fonts to remain under this license does not apply
25
+to any document created using the fonts or their derivatives.
26
+
27
+DEFINITIONS
28
+"Font Software" refers to the set of files released by the Copyright
29
+Holder(s) under this license and clearly marked as such. This may
30
+include source files, build scripts and documentation.
31
+
32
+"Reserved Font Name" refers to any names specified as such after the
33
+copyright statement(s).
34
+
35
+"Original Version" refers to the collection of Font Software components as
36
+distributed by the Copyright Holder(s).
37
+
38
+"Modified Version" refers to any derivative made by adding to, deleting,
39
+or substituting -- in part or in whole -- any of the components of the
40
+Original Version, by changing formats or by porting the Font Software to a
41
+new environment.
42
+
43
+"Author" refers to any designer, engineer, programmer, technical
44
+writer or other person who contributed to the Font Software.
45
+
46
+PERMISSION & CONDITIONS
47
+Permission is hereby granted, free of charge, to any person obtaining
48
+a copy of the Font Software, to use, study, copy, merge, embed, modify,
49
+redistribute, and sell modified and unmodified copies of the Font
50
+Software, subject to the following conditions:
51
+
52
+1) Neither the Font Software nor any of its individual components,
53
+in Original or Modified Versions, may be sold by itself.
54
+
55
+2) Original or Modified Versions of the Font Software may be bundled,
56
+redistributed and/or sold with any software, provided that each copy
57
+contains the above copyright notice and this license. These can be
58
+included either as stand-alone text files, human-readable headers or
59
+in the appropriate machine-readable metadata fields within text or
60
+binary files as long as those fields can be easily viewed by the user.
61
+
62
+3) No Modified Version of the Font Software may use the Reserved Font
63
+Name(s) unless explicit written permission is granted by the corresponding
64
+Copyright Holder. This restriction only applies to the primary font name as
65
+presented to the users.
66
+
67
+4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
68
+Software shall not be used to promote, endorse or advertise any
69
+Modified Version, except to acknowledge the contribution(s) of the
70
+Copyright Holder(s) and the Author(s) or with their explicit written
71
+permission.
72
+
73
+5) The Font Software, modified or unmodified, in part or in whole,
74
+must be distributed entirely under this license, and must not be
75
+distributed under any other license. The requirement for fonts to
76
+remain under this license does not apply to any document created
77
+using the Font Software.
78
+
79
+TERMINATION
80
+This license becomes null and void if any of the above conditions are
81
+not met.
82
+
83
+DISCLAIMER
84
+THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
85
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
86
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
87
+OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
88
+COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
89
+INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
90
+DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
91
+FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
92
+OTHER DEALINGS IN THE FONT SOFTWARE.

+ 2
- 2
Makefile View File

@@ -1,5 +1,5 @@
1 1
 CC = g++
2
-CFLAGS = -O2 -Wall -Wextra -std=c++11
2
+CFLAGS = -O2 -Wall -Wextra -std=c++14
3 3
 # to avoid the console window with mingw, add -mwindow
4 4
 
5 5
 INC = -I "SFML21/include"
@@ -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 = main.o 
11
+OBJ = mainmenu.o main.o 
12 12
 
13 13
 game: $(OBJ)
14 14
 	$(CC) $(CFLAGS) -o game $(OBJ) $(LDFLAGS)

BIN
game View File


+ 13
- 5
main.cpp View File

@@ -6,13 +6,15 @@
6 6
 #include <SFML/Graphics.hpp>
7 7
 
8 8
 #include "main.hpp"
9
-
9
+#include "mainmenu.hpp"
10 10
 
11 11
 namespace Game {
12 12
     // because we want to create the objects in init(), we use the default constructor, so nothing can go wrong here
13 13
     sf::RenderWindow window{};
14 14
 
15 15
     GameState state = GameState::MainMenu;
16
+    MainMenu menu{};
17
+    sf::Font font;
16 18
 
17 19
     bool init() {
18 20
     
@@ -33,6 +35,13 @@ namespace Game {
33 35
             std::cout << "Unable to open window object. Aborting execution.";
34 36
             return false;
35 37
         }
38
+
39
+        if (!font.loadFromFile("Audiowide-Regular.ttf")) { 
40
+            std::cout << "Unable to load font. Aborting execution.";
41
+            return false;
42
+        }
43
+
44
+        menu.init(font);
36 45
         
37 46
         return true;
38 47
     }
@@ -47,9 +56,9 @@ namespace Game {
47 56
                 break;
48 57
                 
49 58
             case GameState::MainMenu:
50
-                // state = update menu
59
+                state = menu.update(window);
51 60
                 if(state == GameState::Game) {
52
-                    // start a game
61
+                    // start game
53 62
                 }
54 63
                 break;
55 64
             
@@ -69,9 +78,8 @@ namespace Game {
69 78
                 break;
70 79
                 
71 80
             case GameState::MainMenu:
72
-                // draw main menu
81
+                menu.draw(window);
73 82
                 break;
74
-
75 83
             
76 84
             case GameState::Exit:
77 85
                 break;

+ 59
- 0
mainmenu.cpp View File

@@ -0,0 +1,59 @@
1
+#include <SFML/System.hpp>
2
+#include <SFML/Window.hpp>
3
+#include <SFML/Graphics.hpp>
4
+
5
+#include "mainmenu.hpp"
6
+#include "main.hpp"
7
+
8
+namespace Game {
9
+
10
+    void MainMenu::draw(sf::RenderWindow &window) {
11
+        window.setView(view);
12
+		window.draw(mainLine);
13
+		window.draw(entry1);
14
+		window.draw(entry2);
15
+    }
16
+
17
+    GameState MainMenu::update(sf::RenderWindow &window) {
18
+        sf::Event event;
19
+        
20
+        while( window.pollEvent(event) ) {
21
+            if( event.type == sf::Event::Closed ) {
22
+                window.close();
23
+            }
24
+            else if(event.type == sf::Event::Resized) {
25
+                //resize the views, so everything is still displayed proportionally
26
+                view.setSize(event.size.width, event.size.height);
27
+                view.setCenter(event.size.width/2, event.size.height/2);
28
+            }
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
+        
35
+        
36
+        return GameState::MainMenu;
37
+    }
38
+    
39
+    void MainMenu::init(sf::Font &font) {
40
+		font = font;
41
+        view.setCenter(middleX, middleY);
42
+        view.setSize(xResolution, yResolution);
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
+
58
+}
59
+

+ 26
- 0
mainmenu.hpp View File

@@ -0,0 +1,26 @@
1
+#pragma once
2
+
3
+#include <SFML/System.hpp>
4
+#include <SFML/Window.hpp>
5
+#include <SFML/Graphics.hpp>
6
+
7
+#include "main.hpp"
8
+
9
+namespace Game {
10
+    
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;
20
+
21
+			sf::Text mainLine;
22
+			sf::Text entry1;
23
+			sf::Text entry2;
24
+    };
25
+
26
+}

Loading…
Cancel
Save