Repository navigation
Expand file tree
/
Copy pathmain.cpp
More file actions
114 lines (98 loc) · 3.74 KB
/
Copy pathmain.cpp
File metadata and controls
114 lines (98 loc) · 3.74 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <SFML/Graphics.hpp>
#include "cMainMenu.h"
#include "cScene.h"
#include "cGameControl.h"
#include "cMenuSaves.h"
#include "cWinScreen.h"
#include "cLooseScreen.h"
#include "cGame.h"
#include "cGraphics.h"
#include "cOptionsMenu.h"
using namespace sf;
sf::Vector2u windowSize = { 1920, 1080 };
sf::Vector2u windowSizeCurrent = windowSize;
sf::Vector2f MouseToScene(sf::Vector2i mousePosition)
{
auto& scene = cScene::Get();
float kx = windowSizeCurrent.x / scene.GetSize().x;
float ky = windowSizeCurrent.y / scene.GetSize().y;
Vector2f pos = { mousePosition.x / kx, mousePosition.y / ky };
return pos;
}
int main()
{
//make a main window
std::srand(std::time({}));
auto window = sf::RenderWindow
(
sf::VideoMode(windowSize), "DefendTheForest",
sf::Style::Default, sf::State::Windowed,
sf::ContextSettings{ .antiAliasingLevel = 8 }
);
const auto cursor = sf::Cursor::createFromSystem(sf::Cursor::Type::Cross).value();
window.setMouseCursor(cursor);
sf::Image icon;
if (!icon.loadFromFile(AssetsPath() + "Visuals/icon2.png"))
{
return -1;
}
window.setIcon(icon.getSize(), icon.getPixelsPtr());
cMainMenu mainMenu(window.getSize().x, window.getSize().y);
cMenuSaves saves(window.getSize().x, window.getSize().y);
cOptionsMenu options(window.getSize().x, window.getSize().y);
cWinScreen winScreen(window.getSize().x, window.getSize().y);
cLooseScreen looseScreen(window.getSize().x, window.getSize().y);
while (window.isOpen())
{
//1.input handling
while (auto const event = window.pollEvent())
{
if (cMainMenu::IsOpened()) mainMenu.EventHandle(event, window);
if (cMenuSaves::IsOpened()) saves.EventHandle(event, window);
if (cOptionsMenu::IsOpened()) options.EventHandle(event, window);
if (cWinScreen::IsOpened()) winScreen.EventHandle(event, window);
if (cLooseScreen::IsOpened()) looseScreen.EventHandle(event, window);
if (event->is<sf::Event::Closed>())
{
window.close();
}
else if (const auto* resized = event->getIf<sf::Event::Resized>())
{
windowSizeCurrent = resized->size;
}
else if (auto const keyEvent = event->getIf<sf::Event::KeyReleased>())
{
switch (keyEvent->code)
{
case sf::Keyboard::Key::Escape: cMainMenu::ChangeOpened(); cGame::Get().Pause(mainMenu.IsOpened()); break;
case sf::Keyboard::Key::F5: cGame::Get().Save("QuickSave"); break;
case sf::Keyboard::Key::F9: cGame::Get().Load("QuickSave"); break;
}
}
//2. Do game control
cGameControl::Get().EventHandle(event);
}
//3. Do game logic
cGame::Get().Quant();
if (cGame::Get().Win())
{
cWinScreen::GameWon();
}
if (cGame::Get().Loose())
{
cLooseScreen::GameLost();
}
//4. Draw all
window.clear(sf::Color::White);
cGame::Get().Draw(window);
if (cMainMenu::IsOpened()) { mainMenu.Draw(window);}
else if (cMenuSaves::IsOpened()) { saves.Draw(window); }
else if (cOptionsMenu::IsOpened()) { options.Draw(window); }
else if (cWinScreen::IsOpened()) { cGame::Get().Pause(true); winScreen.Draw(window); }
else if (cLooseScreen::IsOpened()) { cGame::Get().Pause(true); looseScreen.Draw(window); }
else
{
}
window.display();
}
}