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
|
#include <stdlib.h>
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include "Bounding_Box.h"
#define SGN(X) (((X)==0)?(0):(((X)<0)?(-1):(1)))
#define ABS(X) ((((X)<0)?(-(X)):(X)))
typedef struct
{
char key[SDLK_LAST];
} Input;
void jouer(SDL_Surface *ecran); // fonction Jouer
void Evolue(Input* in,SDL_Surface *perso,struct B_B_rectangulaire box_objet,SDL_Rect* pos_objet,SDL_Rect* pos_perso); // fonction de gestion des deplacement du joueur
void UpdateEvents(Input* in); // fonction de gestion des évènements
void RecupererVecteur(Input* in,int* vx,int* vy);//fonction de recuperation du vecteur de deplacement du joueur
int EssaiDeplacement(SDL_Rect* pos_objet,struct B_B_rectangulaire box_perso,struct B_B_rectangulaire box_objet,SDL_Rect* pos_perso,int vx,int vy);// fonction de test du deplacement du joueur pour les collisions
void Deplace(struct B_B_rectangulaire box_perso,struct B_B_rectangulaire box_objet,SDL_Rect* pos_objet,SDL_Rect* pos_perso,int vx,int vy); // fonction de deplacment du joueur
bool CollisionRR(struct B_B_rectangulaire, struct B_B_rectangulaire); // fonction booléenne de collision entre 2 bounding box rectangulaire
int CollisionRR2(struct B_B_rectangulaire box_perso,struct B_B_rectangulaire box_objet); // fonction entiére de collision entre 2 bounding box rectangulaire
int main ( int argc, char** argv )
{
// initialisation de la SDL//
SDL_Init( SDL_INIT_VIDEO );
SDL_Surface* ecran = SDL_SetVideoMode(640, 640, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);
SDL_WM_SetCaption("Galactic craft", NULL);
// Definition des surface pour l'ecran d'accueil //
SDL_Surface* acceuil = SDL_LoadBMP("fond.bmp");
SDL_Surface* titre = SDL_LoadBMP("titre.bmp");
SDL_SetColorKey(titre,SDL_SRCCOLORKEY, SDL_MapRGB(titre->format,255,255,255));
SDL_Surface* fleche = SDL_LoadBMP("fleche.bmp");
SDL_SetColorKey(fleche,SDL_SRCCOLORKEY, SDL_MapRGB(fleche->format,255,255,255));
SDL_Surface* bouton_jouer = SDL_LoadBMP("bouton_jouer.bmp");
SDL_SetColorKey(bouton_jouer,SDL_SRCCOLORKEY, SDL_MapRGB(bouton_jouer->format,255,0,0));
// Definition des position des surfaces//
SDL_Rect pos_acceuil, pos_fleche,pos_titre,pos_bouton_jouer;
// Surface accueil
pos_acceuil.x = 0;
pos_acceuil.y = 0;
// Surface fleche
pos_fleche.x = 60;
pos_fleche.y = 60;
// Surface titre
pos_titre.x = ecran->w/2 - titre->w/2;
pos_titre.y = 60;
// Surface bouton_jouer
pos_bouton_jouer.x = ecran->w/2 - bouton_jouer->w/2;
pos_bouton_jouer.y = pos_titre.y + titre->h ;
// bounding box de la fleche//
struct B_B_rectangulaire Fleche;
Fleche.x=pos_fleche.x;
Fleche.y=pos_fleche.y;
Fleche.h=fleche->h;
Fleche.w=fleche->w;
//Bounding box du bouton_jouer//
struct B_B_rectangulaire Bouton_jouer;
Bouton_jouer.x=pos_bouton_jouer.x;
Bouton_jouer.y=pos_bouton_jouer.y;
Bouton_jouer.h=bouton_jouer->h;
Bouton_jouer.w=bouton_jouer->w;
// Definition de la variable d'évènement//
SDL_Event event;
int continuer=1;
//Boucle d'évènement//
while (continuer)
{
SDL_WaitEvent(&event);
switch(event.type)
{
case SDL_KEYDOWN:
switch(event.key.keysym.sym)
{
case SDLK_ESCAPE:
continuer = 0;
default:
break;
}
// Deplacement de la fléche
case SDL_MOUSEMOTION:
pos_fleche.x = event.motion.x;
pos_fleche.y = event.motion.y;
break;
// Le clic
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_LEFT)
{
if( CollisionRR(Fleche,Bouton_jouer)== true)
{
jouer(ecran);// Fonction jouer appelle la SDL_Surface ecran
exit(-5);
}
}
break;
}
SDL_ShowCursor(SDL_DISABLE);
SDL_FillRect(ecran, 0, SDL_MapRGB(ecran->format, 255, 255, 255));
SDL_BlitSurface(acceuil, 0, ecran, &pos_acceuil);
SDL_BlitSurface(titre, 0, ecran, &pos_titre);
SDL_BlitSurface(bouton_jouer, 0, ecran, &pos_bouton_jouer);
SDL_BlitSurface(fleche, 0, ecran, &pos_fleche);
SDL_Flip(ecran);
}
SDL_FreeSurface(acceuil);
SDL_FreeSurface(ecran);
SDL_FreeSurface(titre);
SDL_FreeSurface(fleche);
SDL_Quit();
return EXIT_SUCCESS ;
}
void jouer(SDL_Surface *ecran)
{
SDL_Surface *univers,*perso,*objet;
SDL_Rect pos_perso, pos_objet;
univers = SDL_LoadBMP("environnement.bmp");
int h,w;
h=univers->h;
w=univers->w;
perso= SDL_LoadBMP("meteorite.bmp");
SDL_SetColorKey(perso,SDL_SRCCOLORKEY, SDL_MapRGB(perso->format,255,255,255));
objet= SDL_LoadBMP("asteroide.bmp");
SDL_SetColorKey(objet,SDL_SRCCOLORKEY, SDL_MapRGB(objet->format,255,0,0));
Input in;
memset(&in,0,sizeof(in));
SDL_Rect pos_fond;
pos_objet.x = 450;
pos_objet.y = 100;
// Bounding Box de l'objet//
struct B_B_rectangulaire box_objet;
box_objet.x=pos_objet.x;
box_objet.y=pos_objet.y;
box_objet.h=objet->h;
box_objet.w=objet->w;
pos_perso.x = ecran->w/4;
pos_perso.y = ecran->h/4;
pos_fond.x=0;
pos_fond.y=0;
SDL_Init( SDL_INIT_VIDEO );
ecran = SDL_SetVideoMode(h, w, 16,SDL_HWSURFACE||SDL_DOUBLEBUF);
SDL_WM_SetCaption("Galactic Craft Niveau", NULL);
while(!in.key[SDLK_ESCAPE])
{
UpdateEvents(&in); // Fonction des gestion des touches
Evolue(&in,perso,box_objet,&pos_objet,&pos_perso); // fonction d'evolution des touche et de deplacement du joueur
SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 0, 0, 0));
SDL_BlitSurface(univers, NULL, ecran,&pos_fond);
SDL_BlitSurface(objet, NULL, ecran, &pos_objet);
SDL_BlitSurface(perso, NULL,ecran, &pos_perso);
SDL_Flip(ecran);
}
SDL_FreeSurface(perso);
SDL_FreeSurface(objet);
SDL_FreeSurface(univers);
}
void UpdateEvents(Input* in)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
in->key[event.key.keysym.sym]=1; // On deplace le joueur
break;
case SDL_KEYUP:
in->key[event.key.keysym.sym]=0; // On ne deplace pas le joueur
break;
default:
break;
}
}
}
void Evolue(Input* in,SDL_Surface *perso,struct B_B_rectangulaire box_objet,SDL_Rect* pos_objet,SDL_Rect* pos_perso)
{
int vx,vy;
//Bounding Box du perso //
struct B_B_rectangulaire box_perso;
box_perso.x=pos_perso.x;
box_perso.y=pos_perso.y;
box_perso.h=perso->h;
box_perso.w=perso->w;
RecupererVecteur(in,&vx,&vy); // on recupere le deplacement
Deplace(box_perso,box_objet,pos_objet,pos_perso,vx,vy); // On deplace le joueur dans le sens du deplacement
// et on test s'il entre en collision avec l'objet
}
void RecupererVecteur(Input* in,int* vx,int* vy)
{
int vitesse = 5;
*vx = *vy = 0;
if (in->key[SDLK_UP])
*vy = -vitesse;
if (in->key[SDLK_DOWN])
*vy = vitesse;
if (in->key[SDLK_LEFT])
*vx = -vitesse;
if (in->key[SDLK_RIGHT])
*vx = vitesse;
}
// Fonction qui permet de deplacer le joueur et de simuler les murs //
void Deplace(struct B_B_rectangulaire box_perso,struct B_B_rectangulaire box_objet,SDL_Rect* pos_objet,SDL_Rect* pos_perso,int vx,int vy)
{
int i;
if (EssaiDeplacement(pos_objet,box_perso,box_objet,pos_perso,vx,vy)==1) // le joueur n'a pas touche l'objet //
return;
for(i=0;i<ABS(vx);i++) // si le joueur touche l'objet
{
if (EssaiDeplacement(pos_objet,box_perso,box_objet,pos_perso,SGN(vx),0)==0) // on se rapproche de l'objet au plus prés
break;
}
for(i=0;i<ABS(vy);i++)
{
if (EssaiDeplacement(pos_objet,box_perso,box_objet,pos_perso,0,SGN(vy))==0) // on se rapproche de l'objet au plus prés
break;
}
}
// Fonction qui test si le joueur à attiend le bout de l'ecran ou non)
int EssaiDeplacement(SDL_Rect* pos_objet,struct B_B_rectangulaire box_perso,struct B_B_rectangulaire box_objet,SDL_Rect* pos_perso,int vx,int vy)
{
SDL_Rect test;
test = *pos_perso;
test.x+=vx;
test.y+=vy;
if (CollisionRR2(box_perso,box_objet)== 0) // on test si il ya collision entre la Bouning box du perso et la bounding box l'objet
{
*pos_perso = test; // le joueur reste prés de l'objet mais ne le traverse pas
return 1;
}
return 0;
}
/* Collision entre une bounding box rectangulaire et une Bounding box rectangulaire*/
bool CollisionRR(struct B_B_rectangulaire R1, struct B_B_rectangulaire R2)
{
if((R2.x>=R1.x+R1.w)||(R2.x+R2.w<=R1.x)||(R2.y>=R1.y+R1.h)||(R2.y+R2.h<=R1.y))
{
return true;
}
else
{
return false;
}
}
int CollisionRR2(struct B_B_rectangulaire box1,struct B_B_rectangulaire box2)
{
if (box1.x >= box2.x+box2.w //la boîte Box1 est à droite de la boîte Box2
&& box1.x + box1.w <= box2.w // la boîte Box1 est à gauche de la boîte Box2
&& box1.y >= box2.y +box2.h // la boîte Box1 est en haut de la boîte Box2
&& box1.y + box1.h <= box2.y ) //la boîte Box1 est en bas de la boîte box2
return 1;
else
return 0;
} |
Partager