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 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
int Collisionrectangle(GLfloat objetx, GLfloat objety, GLfloat objetz, GLfloat blockx, GLfloat blocky, GLfloat blockz, GLfloat objetl, GLfloat objetw, GLfloat objeth, GLfloat blockl, GLfloat blockw, GLfloat blockh) {
if(objetx >= blockx && objetx <= blockx+blockw) {
if(objetz >= blockz && objetz+objetl <= blockz+blockl) {
return 1;
}
}
else return 0;
}
int Collisioncercle(GLfloat objetx, GLfloat objety, GLfloat objetz, GLfloat blockx, GLfloat blocky, GLfloat blockz, GLfloat objetrayon, GLfloat blockrayon) {
GLfloat distancecercles;
distancecercles = (objetx-blockx)*(objetx-blockx)+(objety-blocky)*(objety-blocky)+(objetz-blockz)*(objetz-blockz);//on multiplie à chaque terme par le même facteur (c'est-à-dire qu'on met au carré) pour avoir tout le temps des résultats positives comme une distance ne peut-être négative;)
distancecercles = sqrt(distancecercles);//on prends la racine carrée de distancecercles pour supprimer les carré du précédent calcul ;)
return (distancecercles < (objetrayon+blockrayon));
}
typedef struct CameraGestion {
float posx;
float posy;
float posz;
float lookx;
float looky;
float lookz;
float oldx;
float oldy;
float oldz;
} CameraGestion;
CameraGestion camera;
typedef struct Mur {
float x;
float y;
float z;
float oldx;
float oldy;
float oldz;
int collision;
} Mur;
Mur mur;
typedef struct Perso {
int state;
float x;
float y;
float z;
} Perso;
Perso firstperso;
void Camera(float positionx, float positiony, float positionz, float rotationx, float rotationy, float rotationz) {
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(rotationx, 1.0f, 0.0f, 0.0f);
glRotatef(rotationy, 0.0f, 1.0f, 0.0f);
glRotatef(rotationz, 0.0f, 0.0f, 1.0f);
glTranslatef(positionx, positiony, positionz);
}
void Display(void){
int i;
int j;
glEnable(GL_TEXTURE_2D);
glEnable(GL_DEPTH_TEST);//pour autoriser le Z-Buffer
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//on nettoie l'écran et le Z-Buffer (==depth buffer)
glEnable2D();
sprintf(info, "Level: %d Power: %d", map.load[0][0], map.numberread);
writeSDLImageFont(texture[1], info, 11.0f, 10.0f, 10.0f);
glDisable2D();
Camera(0.0f, 0.0f, 0.0f, camera.lookx, 0.0f, 0.0f);//pour faire effet de remontée ou descente de la caméra appliqué sur le perso
glPushMatrix();
glTranslatef(firstperso.x, firstperso.y, firstperso.z);
if(firstperso.state == 0) {
glRotatef(90.0f, 0.0f, 0.0f, 1.0f);
glRotatef(90.0f, 0.0f, 1.0f, 0.0f);
}
else if(firstperso.state == 1) {
glRotatef(270.0f, 1.0f, 0.0f, 0.0f);
glRotatef(-90.0f, 0.0f, 0.0f, 1.0f);
}
MD2Animation(&firstperson, 0.05f, 40.0f, 46.0f);
MD2Display(&firstperson, firstperson.currentframe, texture[2]);
glPopMatrix();
glPushMatrix();
Camera(camera.posx, camera.posy, camera.posz, camera.lookx, camera.looky, camera.lookz);//on applique les changements de caméra juste à la scène et non aux personnages :)
for(i=0; i<map.lineread; i++) {
for(j=0; j<map.numberread; j++) {
glPushMatrix();
if(map.load[i][j] == 1) {
mur.x = 0.0f+j;
mur.y = 1.0f;
mur.z = -10.0f+0.0f-i;
glTranslatef(mur.x, mur.y, mur.z);
glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
MD2Display(&murenvironment, 0, texture[1]);
if(Collisionrectangle(firstperso.x, firstperso.y, firstperso.z, mur.x+camera.posx, mur.y+camera.posy, mur.z+camera.posz, -1.0, -1.0, 1.85, 1.0, 1.0, 2.0) == 1) {
mur.collision = 1;
}
}
glPopMatrix();
}
}
glPopMatrix();
}
int main(int argc, char **argv){
FPSmanager fpsmanager;
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO);
TTF_Init();
SDL_initFramerate(&fpsmanager);
SDL_Color color = {255, 0, 0};
SDL_Surface*ecran = NULL;
ecran = SDL_SetVideoMode(480, 272, 32, SDL_OPENGL);
SDL_Event event;
SDL_setFramerate(&fpsmanager, 60);
srand((unsigned int)time(NULL));
initGL(480.0f, 272.0f);
texture[1] = loadSDLTexture("./data/drfreak.tga", 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable2D();
writeSDLImageFont(texture[1], "Wait during the loading of files please", 12.0f, 0.0f, 12.0f);
glDisable2D();
glFlush();
SDL_GL_SwapBuffers();
loadMap(&map, "./data/map.txt");
loadMD2(&murenvironment, "./data/mur.md2");
loadMD2(&firstperson, "./data/tris.md2");
firstperson.currentframe = 40.0f;
texture[2] = loadSDLTexture("./data/cloth.pcx", 0);
int right = 0;
int left = 0;
int up = 0;
int down = 0;
int qpad = 0;
int apad = 0;
float deplx = 0.0f;
float deplz = 0.0f;
firstperso.state = 0;
firstperso.x = 0.0f;
firstperso.y = -0.2f;
firstperso.z = -0.64f;
mur.x = 0.0f;
mur.y = 0.0f;
mur.z = 0.0f;
mur.collision = 0;
camera.posx = 0.0f;
camera.posy = 0.0f;
camera.posz = 0.0f;
camera.lookx = 0.0f;
camera.looky = 0.0f;
camera.lookz = 0.0f;
game.state = 1;
while(1) {
SDL_PollEvent(&event);
switch(event.type) {
case SDL_QUIT:
exit(0);
break;
case SDL_KEYDOWN:
switch(event.key.keysym.sym) {
case SDLK_UP:
up = 1;
break;
case SDLK_DOWN:
down = 1;
break;
case SDLK_RIGHT:
right = 1;
break;
case SDLK_LEFT:
left = 1;
break;
case SDLK_a:
apad = 1;
break;
case SDLK_q:
qpad = 1;
break;
}
break;
case SDL_KEYUP:
switch(event.key.keysym.sym) {
case SDLK_UP:
up = 0;
break;
case SDLK_DOWN:
down = 0;
break;
case SDLK_RIGHT:
right = 0;
break;
case SDLK_LEFT:
left = 0;
break;
case SDLK_a:
apad = 0;
break;
case SDLK_q:
qpad = 0;
break;
}
break;
}
if(game.state == 1) {
//gestion de la caméra :)
if(up) {
firstperso.state = 0;
/*schéma si touche haut:
*****(= objet) OO (= caméra)
******
OO
si up alors
<--******
OO (donc c'est l'objet (la scène) qui fait la translation donc on doit trouver comment se déplace la scène suivant x et z à partir du schéma:
x se déplace dans les négatif et z dans les positif. Donc on doit trouver en combien d'unités se déplace la scène sur x et x en 3D est associé à sinus et z à cosinus dans un cercle trigo :):
|sin (x)
--|----cos(z)
|
*/
if(mur.collision == 1) {
mur.x = mur.oldx;
mur.y = mur.oldy;
mur.z = mur.oldz;
camera.posx = camera.oldx;
camera.posy = camera.oldy;
camera.posz = camera.oldz;
mur.collision = 0;
}
else if(mur.collision == 0) {
mur.oldx = mur.x;
mur.oldy = mur.y;
mur.oldz = mur.z;
camera.oldx = camera.posx;
camera.oldy = camera.posy;
camera.oldz = camera.posz;
camera.posx-=(float)sin((camera.looky*3.14)/180.0f)/2.0f;
camera.posz+=(float)cos((camera.looky*3.14)/180.0f)/2.0f;
}
}
if(down) {
firstperso.state = 1;
if(mur.collision == 1) {
mur.x = mur.oldx;
mur.y = mur.oldy;
mur.z = mur.oldz;
camera.posx = camera.oldx;
camera.posy = camera.oldy;
camera.posz = camera.oldz;
mur.collision = 0;
}
else if(mur.collision == 0) {
mur.oldx = mur.x;
mur.oldy = mur.y;
mur.oldz = mur.z;
camera.oldx = camera.posx;
camera.oldy = camera.posy;
camera.oldz = camera.posz;
camera.posx+=(float)sin((camera.looky*3.14)/180.0f)/2.0f;
camera.posz-=(float)cos((camera.looky*3.14)/180.0f)/2.0f;
}
}
if(right) {
camera.looky+=1.0f;
if(camera.looky > 360.0f) camera.looky-=360.0f;
}
if(left) {
camera.looky-=1.0f;
if(camera.looky < -360.0f) camera.looky+=360.0f;
}
if(qpad) {
if(camera.lookx >= -10.0f) camera.lookx-=1.0f;
}
if(apad) {
if(camera.lookx < 5.0f) camera.lookx+=1.0f;
}
Display();
}
SDL_framerateDelay(&fpsmanager);
glFlush();//permet de s'aasurer que toutes les fonctions d'opengl ont été exécuté
SDL_GL_SwapBuffers();//permet de mettre à jour l'affichage c'est l'équivalent de SDL_Flip() mais avec opengl :)
}
SDL_FreeSurface(ecran);
SDL_Quit();
TTF_Quit();
//on libère la mémoire allouée précedemment dynamiquement :)
freeMD2(&mur);
freeMD2(&firstperson);
glDeleteTextures(20, &texture);
return 0;
} |
Partager