-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWelcome.cpp
More file actions
93 lines (82 loc) · 3.1 KB
/
Welcome.cpp
File metadata and controls
93 lines (82 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <SFML/Graphics.hpp>
#include "Welcome.h"
#include "GameScreen.h"
#include "Tile.h"
#include <string>
#include <fstream>
#include "Configuration.h"
using namespace std;
void WelcomeScreen:: runWelcomeScreen(float width, float height) {
//Window Render
sf::RenderWindow welcome(sf::VideoMode(width, height), "Welcome Screen", sf::Style::Close);
sf::Font font;
if (!font.loadFromFile("font.ttf")) {
cerr << "Error loading font file" << endl;
return;
}
//Welcome to Minesweeper
sf::Text welcomeText;
welcomeText.setFont(font);
welcomeText.setString("WELCOME TO MINESWEEPER");
welcomeText.setCharacterSize(24);
welcomeText.setStyle(sf::Text::Underlined);
welcomeText.setFillColor(sf::Color::White);
Configuration::setText(welcomeText, (width/2), (height/2-150));
//Enter your name
sf::Text nameText;
nameText.setFont(font);
nameText.setString("Enter your name:");
nameText.setCharacterSize(20);
nameText.setFillColor(sf::Color::White);
Configuration::setText(nameText, (width/2), (height/2-75));
string input;
//User-typed name
sf::Text inputText;
inputText.setFont(font);
inputText.setString(input);
inputText.setCharacterSize(18);
inputText.setFillColor(sf::Color::Yellow);
inputText.setString("|");
Configuration::setText(inputText, (width/2), ((height/2)-45));
while (welcome.isOpen()) {
sf::Event event;
while (welcome.pollEvent(event)) {
if (event.type == sf::Event::TextEntered) {
if (event.text.unicode == 8 && !input.empty()) {
input.pop_back();
}
else if ((event.text.unicode >= L'a' && event.text.unicode <= L'z') || event.text.unicode >= L'A' && event.text.unicode <= L'Z') {
if (input.length() < 10) {
input += static_cast<char>(event.text.unicode);
input[0] = toupper(input[0]);
for (int i = 1; i < input.length(); i++) {
input[i] = tolower(input[i]);
}
}
}
inputText.setString(input + "|");
Configuration::setText(inputText, (width/2), ((height/2)-45));
}
if(event.type == sf::Event::Closed) {
welcome.close();
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::Enter) {
if (!(input.length() == 0)){
welcome.close();
GameScreen game;
game.runGameScreen(width, height);
}
}
}
}
welcome.clear(sf::Color::Blue);
welcome.draw(welcomeText);
welcome.draw(nameText);
welcome.draw(inputText);
welcome.display();
}
welcome.clear(sf::Color::Blue);
welcome.display();
}