-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathframework.cpp
More file actions
74 lines (60 loc) · 1.8 KB
/
framework.cpp
File metadata and controls
74 lines (60 loc) · 1.8 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
#include "framework.hpp"
#include <SFML/Graphics.hpp>
#include <iostream>
using std::cout;
using std::endl;
Framework::Framework(sf::Window &app, const sf::Vector2f & viewSize)
{
float aspectRatio = (float) app.getSize().x / (float) app.getSize().y;
_xSize = app.getSize().x;
_ySize = app.getSize().y;
_aspectRatio = aspectRatio;
_viewSize = viewSize;
_initialWindowSize = sf::Vector2f(app.getSize().x, app.getSize().y);
}
float Framework::getAspectRatio() const
{
return _aspectRatio;
}
sf::Vector2f Framework::getCorrectedMousePosition(const sf::Window & app, const sf::Vector2f & vector) const {
float fixedX = (float(vector.x) / app.getSize().x) * _xSize;
float fixedY = (float(vector.y) / app.getSize().y) * _ySize;
return sf::Vector2f(fixedX, fixedY);
}
sf::Vector2f Framework::getCorrectedMousePosition(const sf::Window & app, float x, float y) const
{
float fixedX = (x / app.getSize().x) * _xSize;
float fixedY = (y / app.getSize().y) * _ySize;
return sf::Vector2f(fixedX, fixedY);
}
void Framework::handleEvents(sf::Window & app, sf::Event & event) const
{
if(event.type == sf::Event::Resized)
{
//Keep game to scale
sf::Vector2u newSize(app.getSize().y * _aspectRatio, app.getSize().y);
app.setSize(newSize);
}
// Close window: exit
if (event.type == sf::Event::Closed) {
app.close();
}
// Escape pressed: exit
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
app.close();
}
}
int Framework::getWindowX() const
{
return _xSize;
}
int Framework::getWindowY() const
{
return _ySize;
}
sf::Vector2f Framework::getViewSize() const {
return _viewSize;
}
sf::Vector2f Framework::getInitialWindowSize() const {
return _initialWindowSize;
}