-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMainMenuState.cpp
More file actions
94 lines (75 loc) · 3.45 KB
/
MainMenuState.cpp
File metadata and controls
94 lines (75 loc) · 3.45 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
94
#include "MainMenuState.h"
#include "State.h"
#include "StateMachine.h"
class StateMachine;
#include "PlayingState.h"
#include "MoreInfo.h"
#include <iostream>
#include <fstream>
MainMenuState::MainMenuState(StateMachine& machine, sf::RenderWindow& window, bool replace)
: State{ machine, window, replace },
//Text
titleText(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 4 + 20, 50, SPACEINVADERS_FONT, "Space Invaders", sf::Color(255, 255, 255)),
ufoText(SCREEN_WIDTH / 2 + 50, SCREEN_HEIGHT / 2.5 - 040, 20, SPACEINVADERS_FONT, " = ? MYSTERY", sf::Color(255, 255, 255)),
t1Text(SCREEN_WIDTH / 2 + 50, SCREEN_HEIGHT / 2.5 + 040, 20, SPACEINVADERS_FONT, " = 30 Points", sf::Color(255, 255, 255)),
t2Text(SCREEN_WIDTH / 2 + 50, SCREEN_HEIGHT / 2.5 + 140, 20, SPACEINVADERS_FONT, " = 20 Points", sf::Color(255, 255, 255)),
t3Text(SCREEN_WIDTH / 2 + 50, SCREEN_HEIGHT / 2.5 + 240, 20, SPACEINVADERS_FONT, " = 10 Points", sf::Color(0, 128, 0)),
ccText(SCREEN_WIDTH - 200, 10, 15, ARIAL_FONT, "Created By LegatAbyssWalker - David Ayrton Dominguez", sf::Color(128, 128, 128)),
//Buttons
startGameButton(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 5, 100, 40, 30, SPACEINVADERS_FONT, "Play",
sf::Color(0, 0, 0), sf::Color(0, 0, 0), sf::Color(0, 0, 0), sf::Color(255, 255, 255), sf::Color(0, 255, 255)),
quitGameButton(SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 + 300, 100, 40, 30, SPACEINVADERS_FONT, "Quit",
sf::Color(0, 0, 0), sf::Color(0, 0, 0), sf::Color(0, 0, 0), sf::Color(255, 255, 255), sf::Color(0, 255, 255)) {
//Textures
invaderTexture[0].loadFromFile(INVADER_T3);
invaderTexture[1].loadFromFile(INVADER_T1);
invaderTexture[2].loadFromFile(INVADER_T2);
ufoTexture.loadFromFile(UFO_T);
for (int x = 0; x < 3; x++) {
invaders[x].setTexture(invaderTexture[x]);
invaders[x].setTextureRect(sf::IntRect(invaderTexture[x].getSize().x / 2 * 0, invaderTexture[x].getSize().y * 0, invaderTexture[x].getSize().x / 2, invaderTexture[x].getSize().y));
invaders[x].setPosition(sf::Vector2<float>(SCREEN_WIDTH / 2 - 100, SCREEN_HEIGHT / 2.5 + 30 + invaderYPosAdd));
invaderYPosAdd += 100;
if (x == 2) { invaders[x].setColor(sf::Color(0, 128, 0)); }
}
ufo.setTexture(ufoTexture);
ufo.setPosition(sf::Vector2<float>(SCREEN_WIDTH / 2 - 100, SCREEN_HEIGHT / 2.5 - 70));
ufo.setScale(1 * 1.5, 1 * 1.5);
}
void MainMenuState::updateEvents() {
sf::Vector2<int> mousePos = sf::Mouse::getPosition(window);
//Updating mouse position for button functionality
quitGameButton.update(sf::Vector2<float>(mousePos));
startGameButton.update(sf::Vector2<float>(mousePos));
//Events while loop
while (window.pollEvent(sfEvent)) {
switch (sfEvent.type) {
case sf::Event::Closed:
window.close();
break;
case sf::Event::MouseButtonPressed:
if (quitGameButton.isPressed() == true) { machine.quit(); }
if (startGameButton.isPressed() == true) { machine.run(machine.buildState<PlayingState>(machine, window, true)); }
break;
}
}
}
void MainMenuState::update() {
fpsCounter.updateCounter();
}
void MainMenuState::render() {
window.clear();
//Render items
quitGameButton.renderTo(window);
startGameButton.renderTo(window);
titleText.renderTo(window);
ufoText.renderTo(window);
t1Text.renderTo(window);
t2Text.renderTo(window);
t3Text.renderTo(window);
ccText.renderTo(window);
fpsCounter.renderTo(window);
for (int x = 0; x < 3; x++) { window.draw(invaders[x]); }
window.draw(ufo);
window.display();
}