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
| #include <stdlib.h>
#include <stdio.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
//#include "Bounding_Box.h"
//#include "Fonction_Collision_Circulaire.h"
typedef struct
{
char key[SDLK_LAST];
} Input;
void jouer(SDL_Surface *ecran); // fonction Jouer
void Evolue(Input* in,SDL_Rect *positionobjet,SDL_Rect*positionperso); // 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* positionobjet,SDL_Rect* positionperso,int vx,int vy);// fonction de test du deplacement du joueur pour les collisions
void Deplace(SDL_Rect* positionobjet,SDL_Rect* positionperso,int vx,int vy); // fonction de deplacment du joueur
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* fleche = SDL_LoadBMP("fleche.bmp");
SDL_Surface* titre = SDL_LoadBMP("titre.bmp");
SDL_Surface* bouton_jouer = SDL_LoadBMP("bouton_jouer.bmp");
SDL_Surface *changement=NULL;
// Definition de la transparence //
SDL_SetColorKey(fleche,SDL_SRCCOLORKEY, SDL_MapRGB(fleche->format,255,255,255));
SDL_SetColorKey(titre,SDL_SRCCOLORKEY, SDL_MapRGB(titre->format,255,255,255));
SDL_SetColorKey(bouton_jouer,SDL_SRCCOLORKEY, SDL_MapRGB(bouton_jouer->format,255,255,255));
// Definition des position des surfaces//
SDL_Rect pos_acceuil, pos_fleche,pos_titre,pos_bouton_jouer,pos_test;
// 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 ;
// 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)
{
jouer(ecran); // Fonction jouer appelle la SDL_Surface ecran
}
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;
SDL_Init( SDL_INIT_VIDEO );
ecran = SDL_SetVideoMode(1000, 1000, 16,SDL_HWSURFACE||SDL_DOUBLEBUF);
SDL_WM_SetCaption("Galactic craft", NULL);
univers = SDL_LoadBMP("environnement.bmp");
perso= SDL_LoadBMP("meteorite.bmp");
objet= SDL_LoadBMP("asteroide.bmp");
Input in;
memset(&in,0,sizeof(in));
SDL_Rect pos_fond;
pos_objet.x = 450;
pos_objet.y = 100;
pos_perso.x = ecran->w/4;
pos_perso.y = ecran->h/4;
pos_fond.x=0;
pos_fond.y=0;
while(!in.key[SDLK_ESCAPE])
{
SDL_Rect pos_copyperso;
SDL_Surface *copyperso;
UpdateEvents(&in);
Evolue(&in,&pos_objet,&pos_perso);
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(copyperso, NULL,ecran, &pos_copyperso);
SDL_Flip(ecran);
}
SDL_Quit();
}
void Evolue(Input* in,SDL_Rect *positionobjet,SDL_Rect*positionperso)
{
int vx,vy;
RecupererVecteur(in,&vx,&vy);
//Deplace(positionobjet,positionperso,vx,vy);
}
void UpdateEvents(Input* in)
{
SDL_Event event;
while(SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
in->key[event.key.keysym.sym]=1;
break;
case SDL_KEYUP:
in->key[event.key.keysym.sym]=0;
break;
default:
break;
}
}
}
void RecupererVecteur(Input* in,int* vx,int* vy)
{
int vitesse = 2;
*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;
}
//int EssaiDeplacement(SDL_Rect* positionobjet,SDL_Rect* positionperso,int vx,int vy)
//{
// SDL_Rect test;
// test = *positionperso;
// test.x+=vx;
// test.y+=vy;
// if (CollisionDecor(positionobjet,&test)==0)
// {
// *positionperso = test;
// return 1;
// }
// return 0;
//}
//
//void Deplace(SDL_Rect* positionobjet,SDL_Rect* positionperso,int vx,int vy)
//{
// int i;
// if (EssaiDeplacement(positionobjet,positionperso,vx,vy)==1)
// return;
//// affine
//
// for(i=0;i<ABS(vx);i++)
// {
// if (EssaiDeplacement(positionobjet,positionperso,SGN(vx),0)==0)
// break;
// }
// for(i=0;i<ABS(vy);i++)
// {
// if (EssaiDeplacement(positionobjet,positionperso,0,SGN(vy))==0)
// break;
// }
//} |
Partager