-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.cpp
131 lines (102 loc) · 4.32 KB
/
Utils.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
#include "Utils.hpp"
SDL_Texture* Utils::loadTexture( SDL_Renderer* renderer, std::string path_to_file ) {
SDL_Surface* tmp = SDL_LoadBMP( path_to_file.c_str() );
SDL_Texture* texture = SDL_CreateTextureFromSurface( renderer, tmp );
SDL_FreeSurface( tmp );
tmp = nullptr;
return texture;
}
bool Utils::check_collision( SDL_Rect& object, SDL_Rect& collide_with, Direction direction ) {
int colliderLeft, objectLeft;
int colliderRight, objectRight;
int colliderTop, objectTop;
int colliderBottom, objectBottom;
// Object's colider boundaries
colliderLeft = object.x;
colliderRight = object.x + object.w;
colliderTop = object.y;
colliderBottom = object.y + object.h;
objectLeft = collide_with.x;
objectRight = collide_with.x + collide_with.w + 5;
objectTop = collide_with.y;
objectBottom = collide_with.y + collide_with.h + 5;
if ( direction == Direction::UP ) {
if ( colliderTop >= objectBottom - 1 || colliderLeft >= objectRight - 8 || colliderRight <= objectLeft + 4 || colliderBottom <= objectTop + 5 ) {
return false;
}
}
else if ( direction == Direction::DOWN ) {
if ( colliderBottom <= objectTop - 2 || colliderLeft >= objectRight - 8 || colliderRight <= objectLeft + 4 || colliderTop >= objectBottom - 6 ) {
return false;
}
}
else if ( direction == Direction::LEFT ) {
if ( colliderLeft >= objectRight - 3 || colliderTop >= objectBottom - 6 || colliderBottom <= objectTop + 5 || colliderRight <= objectLeft + 4 ) {
return false;
}
}
else if ( direction == Direction::RIGHT ) {
if ( colliderRight <= objectLeft || colliderTop >= objectBottom - 6 || colliderBottom <= objectTop + 5 || colliderLeft >= objectRight - 8 ) {
return false;
}
}
return true;
}
bool Utils::check_collision( SDL_Rect& object, SDL_Rect& collide_with ) {
int colliderLeft, objectLeft;
int colliderRight, objectRight;
int colliderTop, objectTop;
int colliderBottom, objectBottom;
// Object's colider boundaries
colliderLeft = object.x;
colliderRight = object.x + object.w;
colliderTop = object.y;
colliderBottom = object.y + object.h;
objectLeft = collide_with.x;
objectRight = collide_with.x + collide_with.w + 5;
objectTop = collide_with.y;
objectBottom = collide_with.y + collide_with.h + 5;
if ( colliderTop >= objectBottom - 1 || colliderLeft >= objectRight - 8 || colliderRight <= objectLeft + 4 || colliderBottom <= objectTop + 5 ) {
return false;
}
if ( colliderBottom <= objectTop - 2 || colliderLeft >= objectRight - 8 || colliderRight <= objectLeft + 4 || colliderTop >= objectBottom - 6 ) {
return false;
}
if ( colliderLeft >= objectRight - 3 || colliderTop >= objectBottom - 6 || colliderBottom <= objectTop + 5 || colliderRight <= objectLeft + 4 ) {
return false;
}
if ( colliderRight <= objectLeft || colliderTop >= objectBottom - 6 || colliderBottom <= objectTop + 5 || colliderLeft >= objectRight - 8 ) {
return false;
}
return true;
}
bool Utils::check_collision( SDL_Rect& object, std::vector<SDL_Rect> collide_with ) {
for ( int i = 0; i < (int)collide_with.size(); i++ ) {
int colliderLeft, objectLeft;
int colliderRight, objectRight;
int colliderTop, objectTop;
int colliderBottom, objectBottom;
// Object's colider boundaries
colliderLeft = object.x;
colliderRight = object.x + object.w;
colliderTop = object.y;
colliderBottom = object.y + object.h;
objectLeft = collide_with[i].x;
objectRight = collide_with[i].x + collide_with[i].w + 5;
objectTop = collide_with[i].y;
objectBottom = collide_with[i].y + collide_with[i].h + 5;
if ( colliderTop >= objectBottom - 1 || colliderLeft >= objectRight - 8 || colliderRight <= objectLeft + 4 || colliderBottom <= objectTop + 5 ) {
return false;
}
if ( colliderBottom <= objectTop - 2 || colliderLeft >= objectRight - 8 || colliderRight <= objectLeft + 4 || colliderTop >= objectBottom - 6 ) {
return false;
}
if ( colliderLeft >= objectRight - 3 || colliderTop >= objectBottom - 6 || colliderBottom <= objectTop + 5 || colliderRight <= objectLeft + 4 ) {
return false;
}
if ( colliderRight <= objectLeft || colliderTop >= objectBottom - 6 || colliderBottom <= objectTop + 5 || colliderLeft >= objectRight - 8 ) {
return false;
}
}
return true;
}