-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayer.cpp
executable file
·246 lines (222 loc) · 7.13 KB
/
player.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include "main.h"
#include "tile.h"
#include "player.h"
Player::Player(void)
{
this->x = 60.0;
this->y = 40.0;
this->width = 94.0; // 94.0 // 56.0
this->height = 100.0; // 100.0 // 95.0
this->speed = 5.5;
this->helth = 3.0;
this->moveState.down = false;
this->moveState.right = false;
this->moveState.up = false;
this->moveState.left = false;
this->angle = PLAYER_ANGLE_DOWN;
}
Player::~Player(void)
{
}
void Player::move(void)
{
if(this->moveState.down)
{
this->y -= this->speed;
this->angle = PLAYER_ANGLE_DOWN;
}
else if(this->moveState.up)
{
this->y += this->speed;
this->angle = PLAYER_ANGLE_UP;
}
else if(this->moveState.left)
{
this->x -= this->speed;
this->angle = PLAYER_ANGLE_LEFT;
}
else if(this->moveState.right)
{
this->x += this->speed;
this->angle = PLAYER_ANGLE_RIGHT;
}
}
void Player::changeMoveState(int type, int state)
{
switch(type)
{
case PLAYER_MOVE_ADD:
switch(state)
{
case PLAYER_MOVE_DOWN:
this->moveState.down = true;
break;
case PLAYER_MOVE_RIGHT:
this->moveState.right = true;
break;
case PLAYER_MOVE_UP:
this->moveState.up = true;
break;
case PLAYER_MOVE_LEFT:
this->moveState.left = true;
break;
}
break;
case PLAYER_MOVE_DELETE:
switch(state)
{
case PLAYER_MOVE_DOWN:
this->moveState.down = false;
break;
case PLAYER_MOVE_RIGHT:
this->moveState.right = false;
break;
case PLAYER_MOVE_UP:
this->moveState.up = false;
break;
case PLAYER_MOVE_LEFT:
this->moveState.left = false;
break;
}
break;
}
}
unsigned int Player::loadModel(SDL_Surface *image, SDL_Rect imageRect)
{
SDL_Surface *tmpImage; // Tempoary surface
tmpImage = SDL_CreateRGBSurface(0, imageRect.w, imageRect.h,
image->format->BitsPerPixel, image->format->Rmask,
image->format->Gmask, image->format->Bmask, image->format->Amask);
SDL_SetAlpha(image, 0, 0);
SDL_Rect tmpRect; // Tempoary rectangle for dest information
tmpRect.x = 0;
tmpRect.y = 0;
tmpRect.w = imageRect.w;
tmpRect.h = imageRect.h;
SDL_BlitSurface(image, &imageRect, tmpImage, &tmpRect); // copy image
unsigned object(0);
glGenTextures(1, &object);
glBindTexture(GL_TEXTURE_2D, object);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tmpImage->w, tmpImage->h, 0,
GL_RGBA, GL_UNSIGNED_BYTE, tmpImage->pixels);
SDL_FreeSurface(tmpImage);
return object;
}
void Player::loadTexture(void)
{
SDL_Surface *image = IMG_Load("data/pinky1.png");
//SDL_Surface *image = IMG_Load("data/foka2.png");
SDL_DisplayFormatAlpha(image);
SDL_Rect imageRect;
/*
imageRect.x = 0;
imageRect.y = 0;
imageRect.w = 200.0;
imageRect.h = 159.0;
this->texture.right = this->loadModel(image, imageRect);
imageRect.x += imageRect.w;
this->texture.left = this->loadModel(image, imageRect);
imageRect.x += imageRect.w;
imageRect.w = 135.0;
this->texture.down = this->loadModel(image, imageRect);
imageRect.x += imageRect.w;
this->texture.up = this->loadModel(image, imageRect);
*/
/*
imageRect.x = 0;
imageRect.y = 0;
imageRect.w = 300;
imageRect.h = 300;
this->texture.down = this->loadModel(image, imageRect);
*/
imageRect.x = 0;
imageRect.y = 0;
imageRect.w = 300; // 250
imageRect.h = 300; // 450
image = IMG_Load("data/pinky1.png"); // data/ghost_baby1.png i sve ostalo zakomentarisano
this->texture.down = this->loadModel(image, imageRect);
image = IMG_Load("data/pinky2.png");
this->texture.right = this->loadModel(image, imageRect);
image = IMG_Load("data/pinky3.png");
this->texture.left = this->loadModel(image, imageRect);
image = IMG_Load("data/pinky4.png");
this->texture.up = this->loadModel(image, imageRect);
SDL_FreeSurface(image);
}
// Render player
void Player::render(void)
{
glColor4ub(255, 255, 255, 255); // White color
glEnable(GL_TEXTURE_2D);
switch(this->angle)
{
case PLAYER_ANGLE_DOWN:
glBindTexture(GL_TEXTURE_2D, this->texture.down);
break;
case PLAYER_ANGLE_RIGHT:
glBindTexture(GL_TEXTURE_2D, this->texture.right);
break;
case PLAYER_ANGLE_UP:
glBindTexture(GL_TEXTURE_2D, this->texture.up);
break;
case PLAYER_ANGLE_LEFT:
glBindTexture(GL_TEXTURE_2D, this->texture.left);
break;
}
glBegin(GL_QUADS);
glTexCoord2d(0, 1); glVertex2f(this->x, this->y);
glTexCoord2d(1, 1); glVertex2f(this->x + this->width, this->y);
glTexCoord2d(1, 0); glVertex2f(this->x + this->width, this->y + this->height);
glTexCoord2d(0, 0); glVertex2f(this->x, this->y + this->height);
glEnd();
glDisable(GL_TEXTURE_2D);
}
void Player::collision(float width, float height)
{
if(this->x < 0)
this->x = 0;
else if(this->x + this->width > width)
this->x = width - this->width;
if(this->y < 0)
this->y = 0;
else if(this->y + this->height > height)
this->y = height - this->height;
}
void Player::collision(Tile *tile)
{
if(this->x + this->width > tile->getX() &&
this->x < tile->getX() + tile->getWidth())
if(this->y + this->height > tile->getY() &&
this->y < tile->getY() + tile->getHeight())
{
/*
if(this->moveState.down)
this->y = tile->getY() + tile->getHeight();
else if(this->moveState.right)
this->x = tile->getX() - this->width;
else if(this->moveState.up)
this->y = tile->getY() - this->height;
else if(this->moveState.left)
this->x = tile->getX() + tile->getWidth();
*/
switch(this->angle)
{
case PLAYER_ANGLE_DOWN:
this->y = tile->getY() + tile->getHeight();
break;
case PLAYER_ANGLE_RIGHT:
this->x = tile->getX() - this->width;
break;
case PLAYER_ANGLE_UP:
this->y = tile->getY() - this->height;
break;
case PLAYER_ANGLE_LEFT:
this->x = tile->getX() + tile->getWidth();
break;
}
}
}