-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmenu-state.cpp
More file actions
79 lines (65 loc) · 1.77 KB
/
Copy pathmenu-state.cpp
File metadata and controls
79 lines (65 loc) · 1.77 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
#include "menu-state.hpp"
#include <memory>
#include <vector>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include "state.hpp"
#include "asteroid.hpp"
MenuState::MenuState(std::shared_ptr<sf::RenderWindow> window, std::shared_ptr<State::StateID> stateUpdater) :
State(window, stateUpdater)
{
sf::Vector2u windowSize = window_->getSize();
title_.setString("Asteroids");
title_.setFont(font_);
title_.setFillColor(sf::Color::Magenta);
title_.setCharacterSize(70);
title_.setOrigin((title_.getLocalBounds().width / 2.0f), (title_.getLocalBounds().height / 2.0f));
title_.setPosition(windowSize.x / 2.0f, windowSize.y / 3.0f);
play_.setString("Play");
play_.setFont(font_);
play_.setFillColor(sf::Color::Cyan);
play_.setCharacterSize(50);
play_.setPosition(windowSize.x / 2.0f, windowSize.y / 2.0f);
play_.setOrigin((play_.getLocalBounds().width / 2.0f), (play_.getLocalBounds().height / 2.0f));
for (int i = 0; i < 15; i++)
{
asteroids_.push_back(std::make_shared<Asteroid>());
}
}
void MenuState::handleEvents()
{
sf::Event event;
while (window_->pollEvent(event))
{
if (event.type == sf::Event::EventType::Closed)
{
window_->close();
}
if (event.type == sf::Event::EventType::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left)
{
if (play_.getGlobalBounds().contains(event.mouseButton.x, event.mouseButton.y))
{
*stateUpdater_ = StateID::PLAY;
}
}
}
}
void MenuState::update(sf::Time deltaTime)
{
for (auto a : asteroids_)
{
a->update(deltaTime);
}
}
void MenuState::render()
{
window_->clear();
window_->draw(background_);
for (auto a : asteroids_)
{
window_->draw(*a);
}
window_->draw(title_);
window_->draw(play_);
window_->display();
}