-
Notifications
You must be signed in to change notification settings - Fork 0
/
SFMLUtilities.cpp
144 lines (126 loc) · 4.44 KB
/
SFMLUtilities.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
134
135
136
137
138
139
140
141
142
143
144
#include "SFMLUtilities.h"
#include <iostream>
namespace zmc
{
SFMLUtilities::SFMLUtilities(unsigned int seed)
: mSeed(seed)
, eng(mSeed)
{
}
SFMLUtilities::SFMLUtilities()
: mSeed((unsigned int)std::chrono::high_resolution_clock::now().time_since_epoch().count())
, eng(mSeed)
{
}
sf::Vector2f SFMLUtilities::getCenterPosition(sf::Vector2u parentSize, sf::Sprite &sprite)
{
sf::Vector2f vector((int)parentSize.x / 2 - (sprite.getTexture()->getSize().x / 2) * sprite.getScale().x , (int)parentSize.y / 2 - (sprite.getTexture()->getSize().y / 2)
* sprite.getScale().y);
return vector;
}
sf::Vector2f SFMLUtilities::getCenterPosition(sf::Vector2u parentSize, sf::Vector2u spriteSize)
{
sf::Vector2f vector((float)parentSize.x / 2 - (float)spriteSize.x / 2 , (float)parentSize.y / 2 - (float)spriteSize.y / 2);
return vector;
}
sf::Vector2u SFMLUtilities::getScaledSize(const sf::Sprite &sprite)
{
sf::Vector2u vector;
vector = sf::Vector2u(sprite.getTexture()->getSize().x * (unsigned int)sprite.getScale().x
, sprite.getTexture()->getSize().y * (unsigned int)sprite.getScale().y);
return vector;
}
const std::vector<int> SFMLUtilities::getDigits(int n)
{
std::vector<int> array;
std::vector<int> array2;
if (n == 0) {
array2.push_back(0);
return array2;
}
int number = n;
while (number > 0) {
int mod = number % 10;
array.push_back(mod);
number -= mod;
number /= 10;
}
for (unsigned int i = array.size() - 1; i < array.size(); i--) {
array2.push_back(array.at(i));
}
return array2;
}
const sf::IntRect SFMLUtilities::getScaledTextureRect(const sf::Sprite &sprite)
{
sf::IntRect intRect;
intRect.top = (int)sprite.getPosition().y;
intRect.left = (int)sprite.getPosition().x;
intRect.height = (int)getScaledSize(sprite).y;
intRect.width = (int)getScaledSize(sprite).x;
return intRect;
}
int SFMLUtilities::generateRandomNumber(int start, int end)
{
std::uniform_int_distribution<> distr(start, end);
return distr(eng);
}
void SFMLUtilities::setSeedForRandomNumberGeneration(unsigned int seed)
{
mSeed = seed;
eng.seed(mSeed);
}
sf::Vector2f SFMLUtilities::getFitToAlignScale(sf::Vector2u textureSize, sf::Vector2u containerSize)
{
float imageHeight = (float)textureSize.y;
float imageWidth = (float)textureSize.x;
float rImage = imageWidth / imageHeight;
float screenHeight = (float)containerSize.y;
float screenWidth = (float)containerSize.x;
float rScreen = screenWidth / screenHeight;
// rs > ri ? (wi * hs/hi, hs) : (ws, hi * ws/wi)
sf::Vector2f size(rScreen > rImage ? (imageWidth * screenHeight / imageHeight) : screenWidth,
rScreen > rImage ? screenHeight : imageHeight * screenWidth / imageWidth);
float scalex = (float)size.x / (float)textureSize.x;
float scaley = (float)size.y / (float)textureSize.y;
return sf::Vector2f(scalex, scaley);
}
float SFMLUtilities::toSFMLCoordinate(float box2dCoord)
{
return box2dCoord * 30;
}
float SFMLUtilities::toBox2DCoordinate(float sfmlCoord)
{
return sfmlCoord / 30;
}
float SFMLUtilities::toDegree(float radian)
{
const float PI = 3.14159265f;
return (radian * 180 / PI);
}
float SFMLUtilities::toRadian(float degree)
{
const float PI = 3.14159265f;
return (degree * PI / 180);
}
float SFMLUtilities::distanceBetweenTwoVectors(sf::Vector2f firstVector, sf::Vector2f secondVector)
{
return std::sqrt(std::pow((firstVector.x - secondVector.x), 2) + std::pow((firstVector.y - secondVector.y), 2));
}
float SFMLUtilities::getAngleBetweenTwoVectors(sf::Vector2f firstVector, sf::Vector2f secondVector)
{
float rotation = 0;
rotation = (float) toDegree(std::atan2(firstVector.x - secondVector.x, firstVector.y - secondVector.y));
if (rotation < 0) {
rotation += 360;
}
return rotation;
}
bool SFMLUtilities::intersects(const sf::Sprite *firstSprite, const sf::Sprite *secondSprite)
{
sf::IntRect firstRect((int)firstSprite->getPosition().x, (int)firstSprite->getPosition().y
, (int)getScaledSize(*firstSprite).x, (int)getScaledSize(*firstSprite).y);
sf::IntRect secondRect((int)secondSprite->getPosition().x, (int)secondSprite->getPosition().y
, (int)getScaledSize(*secondSprite).x, (int)getScaledSize(*secondSprite).y);
return firstRect.intersects(secondRect);
}
}