-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.cpp
87 lines (74 loc) · 2.07 KB
/
util.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
#include <cassert>
#include <cmath>
#include "util.hpp"
using namespace sf;
using namespace std;
bool collision(CircleShape &a, CircleShape &b)
{
float r1 = a.getRadius();
float r2 = b.getRadius();
float dist = magnitude(a.getPosition() + Vector2f(r1, r1) - b.getPosition() - Vector2f(r2, r2));
float minDist = r1 + r2;
return dist < minDist;
}
// https://github.com/SFML/SFML/wiki/Source:-Letterbox-effect-using-a-view
// slightly modified
View getLetterboxView(int desiredWidth, int desiredHeight, int windowWidth, int windowHeight)
{
// Compares the aspect ratio of the window to the aspect ratio of the view,
// and sets the view's viewport accordingly in order to archieve a letterbox effect.
// A new view (with a new viewport set) is returned.
float windowRatio = windowWidth / (float) windowHeight;
float viewRatio = desiredWidth / (float) desiredHeight;
float sizeX = 1;
float sizeY = 1;
float posX = 0;
float posY = 0;
bool horizontalSpacing = true;
if (windowRatio < viewRatio) horizontalSpacing = false;
// If horizontalSpacing is true, the black bars will appear on the left and right side.
// Otherwise, the black bars will appear on the top and bottom.
if (horizontalSpacing) {
sizeX = viewRatio / windowRatio;
posX = (1 - sizeX) / 2.0;
}
else {
sizeY = windowRatio / viewRatio;
posY = (1 - sizeY) / 2.0;
}
View view(FloatRect(0, 0, desiredWidth, desiredHeight));
view.setViewport(FloatRect(posX, posY, sizeX, sizeY) );
return view;
}
Vector2f operator-(const sf::Vector2f &a, const sf::Vector2f &b)
{
Vector2f r(a.x - b.x, a.y - b.y);
return r;
}
Vector2f operator*(const Vector2f &a, float b)
{
Vector2f r(a.x * b, a.y * b);
return r;
}
Vector2f operator/(const Vector2f &a, float b)
{
Vector2f r(a.x / b, a.y / b);
return r;
}
float magnitude(const Vector2f &v)
{
return sqrt(v.x * v.x + v.y * v.y);
}
Vector2f normalize(const Vector2f &v)
{
if (v.x == 0 && v.y == 0) return v;
return v / magnitude(v);
}
Texture loadTexture(const std::string &filename)
{
Texture t;
if (!t.loadFromFile(filename)) {
assert(0);
}
return t;
}