-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameObject.cpp
More file actions
79 lines (67 loc) · 1.85 KB
/
GameObject.cpp
File metadata and controls
79 lines (67 loc) · 1.85 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
74
75
76
77
78
79
#include "GameObject.h"
GameObject::GameObject(){
objectBox.x = 0;
objectBox.y = 0;
objectBox.w = 0;
objectBox.h = 0;
xmove = 0;
ymove = 0;
direction = 0;
frame = 0;
windowXSize = 640;
windowYSize = 480;
}
GameObject::GameObject(int x, int y, int w, int h){
objectBox.x = x;
objectBox.y = y;
objectBox.w = w;
objectBox.h = h;
xmove = 0;
ymove = 0;
direction = 0;
frame = 0;
windowXSize = 640;
windowYSize = 480;
}
bool GameObject::collisionCheck(std::vector<SDL_Rect> &B){
int maxXb, minXb, maxYb, minYb;
for(int i = 0; i<B.size();i++){
minXb = B[i].x;
maxXb = B[i].x + B[i].w;
minYb = B[i].y;
maxYb = B[i].y + B[i].h;
if(((objectBox.x+objectBox.w) > minXb)&&((objectBox.y+objectBox.h)>minYb)&&(objectBox.y<maxYb)&&(objectBox.x<maxXb)){
return true;
}
}
return false;
}
void GameObject::move(){
if(!(((objectBox.x+xmove)<0)||((objectBox.x+objectBox.w+xmove)>windowXSize)||collisionCheck(map))){
objectBox.x += xmove;
}
if(!(((objectBox.y+ymove)<0)||((objectBox.y+objectBox.h+xmove)>windowYSize)||collisionCheck(map))){
objectBox.y += ymove;
}
}
SDL_Surface* GameObject::loadImage(std::string file){
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = IMG_Load( file.c_str() );
if( loadedImage != NULL )
{
optimizedImage = SDL_DisplayFormat( loadedImage );
SDL_FreeSurface( loadedImage );
if( optimizedImage != NULL )
{
SDL_SetColorKey( optimizedImage, SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 0, 55, 0 ) );
}
}
return optimizedImage;
}
void GameObject::applySurface(int x ,int y ,SDL_Surface* source ,SDL_Surface* destination ,SDL_Rect* clip){
SDL_Rect offset;
offset.x = x;
offset.y = y;
SDL_BlitSurface( source, clip, destination, &offset );
}