-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.cpp
133 lines (117 loc) · 3.77 KB
/
game.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include "Game.h"
Game::Game(sf::String title, sf::VideoMode size, bool fullscreen)
{
this->title = title;
this->fullscreen = fullscreen;
this->supportedResolutions = sf::VideoMode::getFullscreenModes();
if (fullscreen)
window.create(sf::VideoMode(size), title, sf::Style::Close | sf::Style::Titlebar | sf::Style::Fullscreen);
else
window.create(sf::VideoMode(size), title, sf::Style::Close | sf::Style::Titlebar);
}
void Game::run()
{
window.setFramerateLimit(160);
camera.reset(sf::FloatRect(0, 0, 1920, 1080));
load();
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::MouseButtonPressed:
mousePressed(event.mouseButton.button, sf::Vector2i(event.mouseButton.x, event.mouseButton.y));
break;
case sf::Event::MouseButtonReleased:
mouseReleased(event.mouseButton.button, sf::Vector2i(event.mouseButton.x, event.mouseButton.y));
break;
case sf::Event::MouseMoved:
mouseMoved(event.mouseMove.x, event.mouseMove.y);
case sf::Event::KeyPressed:
keyPressed(event.key.code);
break;
case sf::Event::KeyReleased:
keyReleased(event.key.code);
break;
case sf::Event::JoystickConnected:
joystickConnected(event.joystickConnect.joystickId);
break;
case sf::Event::JoystickDisconnected:
joystickDisconnected(event.joystickConnect.joystickId);
break;
default:
break;
}
}
sf::Time deltaTime = time.restart();
update(deltaTime.asSeconds());
if (window.getSize().x / 1920.f <= window.getSize().y / 1080.f)
{
float yScale = (window.getSize().x * (9.f / 16.f)) / window.getSize().y;
camera.setViewport(sf::FloatRect(0.f, (1.f - yScale) / 2, 1.f, yScale));
}
else
{
float xScale = (window.getSize().y * (16.f / 9.f)) / window.getSize().x;
camera.setViewport(sf::FloatRect((1.f - xScale) / 2, 0.f, xScale, 1.f));
}
window.clear();
window.setView(camera);
draw(&window);
window.setView(window.getDefaultView());
drawUnscaled(&window);
window.display();
}
onExit();
}
void Game::render(const sf::Drawable &drawable)
{
window.draw(drawable);
}
void Game::setResolution(sf::Vector2u size)
{
if (fullscreen)
window.create(sf::VideoMode(size.x, size.y), title, sf::Style::Close | sf::Style::Titlebar | sf::Style::Fullscreen);
else
window.create(sf::VideoMode(size.x, size.y), title, sf::Style::Close | sf::Style::Titlebar);
windowResized(size);
}
void Game::setCursorVisible(bool visible)
{
window.setMouseCursorVisible(visible);
}
void Game::toggleFullscreen()
{
fullscreen = !fullscreen;
if (fullscreen)
window.create(sf::VideoMode(window.getSize().x, window.getSize().y), title, sf::Style::Close | sf::Style::Titlebar | sf::Style::Fullscreen);
else
window.create(sf::VideoMode(window.getSize().x, window.getSize().y), title, sf::Style::Close | sf::Style::Titlebar);
}
std::vector<sf::VideoMode> Game::getSupportedResolutions()
{
return supportedResolutions;
}
sf::Vector2i Game::mousePos()
{
if (window.getSize().x / 1920.f <= window.getSize().y / 1080.f)
{
float yScale = (window.getSize().x * (9.f / 16.f)) / window.getSize().y;
return sf::Vector2i((int)floor(sf::Mouse::getPosition(window).x * (1920.f / window.getSize().x)), (int)floor((sf::Mouse::getPosition(window).y - ((1 - yScale) / 2) * window.getSize().y) * (1920.f / window.getSize().x)));
}
else
{
float xScale = (window.getSize().y * (16.f / 9.f)) / window.getSize().x;
return sf::Vector2i((int)floor((sf::Mouse::getPosition(window).x - ((1 - xScale) / 2) * window.getSize().x) * (1080.f / window.getSize().y)), (int)floor(sf::Mouse::getPosition(window).y * (1080.f / window.getSize().y)));
}
return sf::Mouse::getPosition(window);
}
void Game::exit()
{
window.close();
}