-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCore.cpp
More file actions
112 lines (102 loc) · 2.35 KB
/
Copy pathCore.cpp
File metadata and controls
112 lines (102 loc) · 2.35 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
#include "Core.hpp"
#pragma comment (lib ,"imm32.lib")
namespace rpf {
Core* Core::CORE = nullptr;
GameOnline* Core::GAME = nullptr;
SocketManager* Core::sock = nullptr;
int Core::highest_score = 0;
Core::Core() : mode(Mode::MAIN_MENU), rm(&window) {
window.create(sf::VideoMode(rh.s_width, rh.s_height), "RUN TOGETHER v3.0", sf::Style::Resize | sf::Style::Close);
window.setFramerateLimit(60);
window.setKeyRepeatEnabled(false);
now = nullptr;
rh.music.play();
Core::CORE = this;
}
void Core::Run() {
switchMode(Mode::MAIN_MENU);//start with ...
while (window.isOpen())
this->update();
}
void Core::update() {
if (wait_switch) {
wait_switch = false;
this->switchMode(wait_mode);
}
sf::Event e;
while (window.pollEvent(e))
if (e.type == sf::Event::Closed)
window.close();
else if (e.type == sf::Event::Resized) {
float screenWidth = 160.f;
float screenHeight = 96.f;
sf::Vector2u size = window.getSize();
float heightRatio = screenHeight / screenWidth;
float widthRatio = screenWidth / screenHeight;
if (size.y * widthRatio <= size.x)
{
size.x = size.y * widthRatio;
}
else if (size.x * heightRatio <= size.y)
{
size.y = size.x * heightRatio;
}
rh.s_width = size.x;
rh.s_height = size.y;
window.setSize(size);
}
else
now->handleEvent(e);
now->update();
rm.Render();
}
void Core::switchMode(Mode mode) {
switch (mode) {
case Mode::MAIN_MENU:
if (now)
free(now);
rm.clear();
now = new MainMenu(&rm, &rh);
break;
case Mode::CONNECTION_MENU:
if (now)
free(now);
rm.clear();
now = new ConnectionMenu(&rm, &rh);
break;
//case Mode::IN_GAME:
case Mode::SINGLE_GAME:
if (now)
free(now);
rm.clear();
now = new Game(&rm, &rh);
break;
case Mode::MULTI_GAME:
if (now)
free(now);
rm.clear();
now = Core::GAME = new GameOnline(&rm, &rh);
break;
case Mode::GAME_OVER:
now = new GameOverMenu(&rm, &rh);
break;
case Mode::GAME_OVER_MUL:
now = new GameOverMulMenu(&rm, &rh);
break;
case Mode::CLOSED:
window.close();
break;
}
}
void Core::switchMode(Render* obj) {
if (!obj)
throw std::runtime_error("fatal error: obj shouldn't be NULL!");
if (now)
free(now);
now = obj;
}
void Core::switchModeAsync(Mode mode) {
wait_switch = true;
wait_mode = mode;
}
}