SUPER Space invader : Turbo edition DX - VS GOD 1.0.0
A simple space invader ripoff
drawMenus.cpp
Go to the documentation of this file.
1
12#include <chrono>
13#include <thread>
14#include "mingl/shape/rectangle.h"
15#include "playMode.h"
17#include "utils.h"
18
19using namespace nsShape;
20using namespace nsGraphics;
21
22void PixelManager::displayButton(const Position& baseVector, const string& text, nsGraphics::RGBAcolor& color){
23 window << Rectangle(baseVector, Position(180, 40)+baseVector, KGray);
24 window << Rectangle(baseVector+Position(2,2), Position(178, 38)+baseVector, KBlack);
25 window << nsGui::Text(baseVector+Position(10,22), text, color);
26}
27
28void PixelManager::drawMenu(const Position& pos, Menu& currentMenu){
29 startFrame();
31 drawSprite(logo,Position(100,50));
32 drawText(Position(10, 692), "tips:",nsGraphics::KWhite,Font::BITMAP_8_BY_13);
33 drawText(Position(10, 702), "use 'z','s','q','d' and 'enter' to navigate the menus",nsGraphics::KWhite,Font::BITMAP_8_BY_13);
34 drawText(Position(10, 712), "see the configuration file for player specific key bindings",nsGraphics::KWhite,Font::BITMAP_8_BY_13);
35 drawText(Position(1150, 712), "version 1.0.0");
36 unsigned margin = 0;
37 unsigned cpt = 0;
38 for(string& value : currentMenu.entries ){
39 displayButton(Position(0,0+margin)+ pos, value, (currentMenu.selectedEntry == cpt) ? currentMenu.selectedColor : currentMenu.unSelectedColor );
40 ++cpt;
41 margin += 50;
42 }
43 endFrame();
44}
45
46
47void PixelManager::drawMenu(const Position& pos, Menu& currentMenu, const vector<ScoreLink>& rankings, const WinValue& winner){
48 startFrame();
50 drawSprite(logo,Position(100,50));
51 drawText(Position(1150, 700), "version 1.0.0");
52 unsigned margin = 0;
53 unsigned cpt = 0;
54 if (winner == WinValue::PLAYERS) drawText(Position(0-55,0-20)+ pos,"The players won, earth is now safe",nsGraphics::KWhite);
55 else if (winner == WinValue::INVADERS) drawText(Position(0-55,0-20)+ pos,"The invaders have reached earth",nsGraphics::KWhite);
56 else drawText(Position(0-55,0-20)+ pos,"God won, as His power are infinite",nsGraphics::KWhite);
57 for(string& value : currentMenu.entries ){
58 displayButton(Position(0,0+margin)+ pos, value, (currentMenu.selectedEntry == cpt) ? currentMenu.selectedColor : currentMenu.unSelectedColor );
59 ++cpt;
60 margin += 50;
61 }
62 margin = 0;
63 drawText(Position(0,350), "Top 10 of the best players",nsGraphics::KWhite);
64 for (auto& value: rankings){
65 drawText(Position(0,400+margin),value.name,nsGraphics::KWhite,Font::BITMAP_HELVETICA_12);
66 drawText(Position(140,400+margin),to_string(value.score),nsGraphics::KWhite,Font::BITMAP_HELVETICA_12);
67 margin += 15;
68 }
69 endFrame();
70}
71
73 vector<string> entries {"single player","multi player (local)","exit"};
74 Menu initialMenu {entries, 0, nsGraphics::KRed, nsGraphics::KWhite};
75 unsigned xOffset = getScreenHeight() / 2 ;
76 unsigned yOffset = getScreenWidth() / 2 - 90;
77 chrono::milliseconds waitTime = chrono::milliseconds(100);
78 while(window.isOpen()){
79 drawMenu(Position(yOffset, xOffset), initialMenu);
80 // go down
81 if (window.isPressed({'s', false})){
82 ++initialMenu.selectedEntry;
83 if (initialMenu.selectedEntry > initialMenu.entries.size() - 1) initialMenu.selectedEntry = 0;
84 this_thread::sleep_for(waitTime);
85 }
86 // go up
87 if (window.isPressed({'z', false})){
88 if (initialMenu.selectedEntry == 0) initialMenu.selectedEntry = initialMenu.entries.size() - 1;
89 else --initialMenu.selectedEntry;
90 this_thread::sleep_for(waitTime);
91 }// select option
92 else if (window.isPressed({13, false})){
93 switch(initialMenu.selectedEntry){
94 case 0:
95 return PlayMode::SINGLE;
96 case 1:
98 case 2:
99 return PlayMode::EXIT;
100 default:
101 return PlayMode::SINGLE;
102 }
103 }
104 }
105 exit(0);
106}
107
108void PixelManager::askPlayerNameMenu(playerID pID, unsigned score, string& name) {
109 name = string(6, 'A');
110 unsigned currentSelected = 0 ;
111 chrono::milliseconds waitTime = chrono::milliseconds(100);
112 while (window.isOpen()){
113 startFrame();
115 drawText(Position(600, 100), "Nom du joueur " + to_string(pID + 1));
116 drawText(Position(600, 150), "Score : " + to_string(score));
117 for (unsigned i = 0; i < name.size(); ++i){
118 drawText(Position(600 + 30 * i, 200), string(1, name[i]),
119 (i == currentSelected) ? nsGraphics::KRed : nsGraphics::KWhite);
120 }
121 endFrame();
122 // go down
123 if (window.isPressed({'s', false})){
124 ++name[currentSelected];
125 if (name[currentSelected] > 90) name[currentSelected] = 65;
126 this_thread::sleep_for(waitTime);
127 }
128 // go up
129 if (window.isPressed({'z', false})){
130 --name[currentSelected];
131 if (name[currentSelected] < 65) name[currentSelected] = 90;
132 this_thread::sleep_for(waitTime);
133 }
134 // go right
135 if (window.isPressed({'d', false})){
136 ++currentSelected;
137 if (currentSelected > name.size()-1) currentSelected = 0;
138 this_thread::sleep_for(waitTime);
139 }
140 // go left
141 if (window.isPressed({'q', false})){
142 if (currentSelected == 0) currentSelected = name.size()-1;
143 else --currentSelected;
144 this_thread::sleep_for(waitTime);
145 }
146 // select option
147 else if (window.isPressed({13, false}))
148 {
149 window.resetKey({13, false});
150 return;
151 }
152 }
153 exit(0);
154}
155
156bool PixelManager::showDeathMenu(const vector<ScoreLink>& rankings,const WinValue& winner) {
157 vector<string> entries {"retry","main menu"};
158 Menu death {entries,0,nsGraphics::KRed,nsGraphics::KWhite};
159 unsigned xOffset = getScreenHeight() / 2 ;
160 unsigned yOffset = getScreenWidth() / 2 - 90;
161 chrono::milliseconds waitTime = chrono::milliseconds(100);
162 while(window.isOpen()){
163 drawMenu(Position(yOffset, xOffset), death, rankings, winner);
164 // go down
165 if (window.isPressed({'s', false})){
166 ++death.selectedEntry;
167 if (death.selectedEntry > death.entries.size() - 1) death.selectedEntry = 0;
168 this_thread::sleep_for(waitTime);
169 }
170 // go up
171 if (window.isPressed({'z', false})){
172 if (death.selectedEntry == 0) death.selectedEntry = death.entries.size() - 1;
173 else --death.selectedEntry;
174 this_thread::sleep_for(waitTime);
175 }// select option
176 else if (window.isPressed({13, false})){
177 switch(death.selectedEntry){
178 case 0:{
179 return true;
180 }
181 case 1:{
182 window.resetKey({13, false});
183 return false;
184 }
185 }
186 }
187 }
188 exit(0);
189}
190
191
192
void startFrame() const
clear the screen for a new frame
void drawSprite(const MySprite &msp, const Position &pos) const
display a sprite on screen
MySprite menuBackground
sprite of the background during menu
Definition: pixelManager.h:68
PlayMode showInitialMenu()
show the title screen of the game
Definition: drawMenus.cpp:72
void displayButton(const Position &baseVector, const string &text, nsGraphics::RGBAcolor &color)
display a menu button on screen
Definition: drawMenus.cpp:22
void endFrame() const
finish a frame render
void drawText(const Position &pos, const string &text, const RGBAcolor &color=nsGraphics::KWhite, Font font=Font::BITMAP_TIMES_ROMAN_24) const
display text on screen
unsigned getScreenHeight() const
give the height of the screen
unsigned getScreenWidth() const
give the width of the screen
MySprite logo
sprite of the logo of the game
Definition: pixelManager.h:63
MinGL & window
display window
Definition: pixelManager.h:51
bool showDeathMenu(const vector< ScoreLink > &rankings, const WinValue &winner)
show the menu after a player lose, or all invader has been defeated
Definition: drawMenus.cpp:156
void askPlayerNameMenu(playerID pID, unsigned score, string &name)
display the player name selection menu
Definition: drawMenus.cpp:108
Manages screen display.
game mode options
PlayMode
List of all game playmode.
Definition: playMode.h:17
menu stuct
Definition: menu.h:22
nsGraphics::RGBAcolor selectedColor
color of currently selected menu option
Definition: menu.h:36
nsGraphics::RGBAcolor unSelectedColor
color of unelected menu option
Definition: menu.h:40
unsigned selectedEntry
index of currently selected menu option
Definition: menu.h:31
vector< string > entries
list of all menu options
Definition: menu.h:26
utilies for the game
unsigned playerID
Definition: utils.h:52
nsGraphics::Vec2D Position
Definition: utils.h:51
WinValue
list of win values
Definition: utils.h:43