-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_state_start.cpp
executable file
·106 lines (90 loc) · 3.16 KB
/
game_state_start.cpp
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
95
96
97
98
99
100
101
102
103
104
105
106
#include <SFML/Graphics.hpp>
#include "game_state_start.hpp"
#include "game_state_editor.hpp"
#include "game_state.hpp"
void GameStateStart::draw(const float dt)
{
this->game->window.setView(this->view);
this->game->window.clear(sf::Color::Black);
this->game->window.draw(this->game->background);
for(auto gui : this->guiSystem) this->game->window.draw(gui.second);
return;
}
void GameStateStart::update(const float dt)
{
}
void GameStateStart::handleInput()
{
sf::Event event;
sf::Vector2f mousePos = this->game->window.mapPixelToCoords(sf::Mouse::getPosition(this->game->window), this->view);
while(this->game->window.pollEvent(event))
{
switch(event.type)
{
/* Close the window */
case sf::Event::Closed:
{
game->window.close();
break;
}
/* Resize the window */
case sf::Event::Resized:
{
this->view.setSize(event.size.width, event.size.height);
this->game->background.setPosition(this->game->window.mapPixelToCoords(sf::Vector2i(0, 0), this->view));
sf::Vector2f pos = sf::Vector2f(event.size.width, event.size.height);
pos *= 0.5f;
pos = this->game->window.mapPixelToCoords(sf::Vector2i(pos), this->view);
this->guiSystem.at("menu").setPosition(pos);
this->game->background.setScale(
float(event.size.width) / float(this->game->background.getTexture()->getSize().x),
float(event.size.height) / float(this->game->background.getTexture()->getSize().y));
break;
}
/* Highlight menu items */
case sf::Event::MouseMoved:
{
this->guiSystem.at("menu").highlight(this->guiSystem.at("menu").getEntry(mousePos));
break;
}
/* Click on menu items */
case sf::Event::MouseButtonPressed:
{
if(event.mouseButton.button == sf::Mouse::Left)
{
std::string msg = this->guiSystem.at("menu").activate(mousePos);
if(msg == "load_game")
{
this->loadgame();
}
}
break;
}
case sf::Event::KeyPressed:
{
if(event.key.code == sf::Keyboard::Escape) this->game->window.close();
break;
}
default: break;
}
}
return;
}
void GameStateStart::loadgame()
{
this->game->pushState(new GameStateEditor(this->game));
return;
}
GameStateStart::GameStateStart(Game* game)
{
this->game = game;
sf::Vector2f pos = sf::Vector2f(this->game->window.getSize());
this->view.setSize(pos);
pos *= 0.5f;
this->view.setCenter(pos);
this->guiSystem.emplace("menu", Gui(sf::Vector2f(192, 32), 4, false, game->stylesheets.at("button"),
{ std::make_pair("Load Game", "load_game") }));
this->guiSystem.at("menu").setPosition(pos);
this->guiSystem.at("menu").setOrigin(96, 32*1/2);
this->guiSystem.at("menu").show();
}