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 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
| #include <stdlib.h>
#include <stdio.h>
#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <string>
#include "constante.h"
#include "map.h"
SDL_Surface *screen = NULL;
SDL_Surface *hero = NULL;
//Le nombre de frame par seconde
const int FRAMES_PER_SECOND = 50;
//Vitesse de deplacement du point (en pixel par seconde)
const int POINT_VITESSE = 900;
//Les dimensions du point (l'image qu'on va deplacer)
const int POINT_WIDTH = 40;
const int POINT_HEIGHT = 20;
//La structure d'événements que nous allons utiliser
SDL_Event event;
//La camera
SDL_Rect camera = { 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT };
void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination, SDL_Rect* clip = NULL )
{
SDL_Rect offset;
offset.x = x;
offset.y = y;
//On blitte la surface
SDL_BlitSurface( source, clip, destination, &offset );
}
class Hero
{
private:
//Les coordonnées x et y du point
int x, y;
//La vitesse du point
int xVel, yVel;
public:
//Initialisation des variables
Hero();
//Recupere la touche pressee et ajuste la vitesse du point
void handle_input();
//Montre le point sur l'ecran
void show();
//Bouge le point
void move();
//Met la camera sur le point
void set_camera();
};
Hero::Hero()
{
//Initialisation des coordonnees
x = 0;
y = 0;
//Initialisation de la vitesse
xVel = 0;
yVel = 0;
}
void Hero::handle_input()
{
//Si une touche a ete pressee
if( event.type == SDL_KEYDOWN )
{
//ajustement de la vitesse
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel -= (POINT_VITESSE / FRAMES_PER_SECOND); break;
case SDLK_DOWN: yVel += (POINT_VITESSE / FRAMES_PER_SECOND); break;
case SDLK_LEFT: xVel -= (POINT_VITESSE / FRAMES_PER_SECOND); break;
case SDLK_RIGHT: xVel += (POINT_VITESSE / FRAMES_PER_SECOND); break;
default: break;
}
}
//Si une touche a ete relachee
else if( event.type == SDL_KEYUP )
{
//ajustement de la vitesse
switch( event.key.keysym.sym )
{
case SDLK_UP: yVel += (POINT_VITESSE / FRAMES_PER_SECOND); break;
case SDLK_DOWN: yVel -= (POINT_VITESSE / FRAMES_PER_SECOND); break;
case SDLK_LEFT: xVel += (POINT_VITESSE / FRAMES_PER_SECOND); break;
case SDLK_RIGHT: xVel -= (POINT_VITESSE / FRAMES_PER_SECOND); break;
default: break;
}
}
}
void Hero::move()
{
//Bouge le point à gauche ou à droite
x += xVel;
//Si le point se rapproche trop des limites(gauche ou droite) de l'ecran
if( ( x < 0 ) || ( x + POINT_WIDTH > LEVEL_WIDTH ) )
{
//On revient
x -= xVel;
}
//Bouge le point en haut ou en bas
y += yVel;
//Si le point se rapproche trop des limites(haute ou basse) de l'ecran
if( ( y < 0 ) || ( y + POINT_HEIGHT > LEVEL_HEIGHT ) )
{
//On revient
y -= yVel;
}
apply_surface( x, y, hero, screen );
}
void Hero::show()
{
//Affiche le point
apply_surface( x - camera.x, y - camera.y, hero, screen );
}
void Hero::set_camera()
{
//Centre la camera sur le point
camera.x = ( x + POINT_WIDTH / 2 ) - SCREEN_WIDTH / 2;
camera.y = ( y + POINT_HEIGHT / 2 ) - SCREEN_HEIGHT / 2;
//Garde la camera sur les bords
if( camera.x < 0 )
{
camera.x = 0;
}
if( camera.y < 0 )
{
camera.y = 0;
}
if( camera.x > LEVEL_WIDTH - camera.w )
{
camera.x = LEVEL_WIDTH - camera.w;
}
if( camera.y > LEVEL_HEIGHT - camera.h )
{
camera.y = LEVEL_HEIGHT - camera.h;
}
}
//Le timer
class Timer
{
private:
//Le temps quand le timer est lancé
int startTicks;
//Les "ticks" enregistré quand le Timer a été mit en pause
int pausedTicks;
//Le status du Timer
bool paused;
bool started;
public:
//Initialise les variables (le constructeur)
Timer();
//Les différentes actions du timer
void start();
void stop();
void pause();
void unpause();
//recupére le nombre de ticks depuis que le timer a été lancé
//ou récupére le nombre de ticks depuis que le timer a été mis en pause
int get_ticks();
//Fonctions de vérification du status du timer
bool is_started();
bool is_paused();
};
Timer::Timer()
{
//Initialisation des variables
startTicks = 0;
pausedTicks = 0;
paused = false;
started = false;
}
void Timer::start()
{
//On demarre le timer
started = true;
//On enlève la pause du timer
paused = false;
//On récupére le temps courant
startTicks = SDL_GetTicks();
}
void Timer::stop()
{
//On stoppe le timer
started = false;
//On enlève la pause
paused = false;
}
void Timer::pause()
{
//Si le timer est en marche et pas encore en pause
if( ( started == true ) && ( paused == false ) )
{
//On met en pause le timer
paused = true;
//On calcul le pausedTicks
pausedTicks = SDL_GetTicks() - startTicks;
}
}
void Timer::unpause()
{
//Si le timer est en pause
if( paused == true )
{
//on enlève la pause du timer
paused = false;
//On remet à zero le startTicks
startTicks = SDL_GetTicks() - pausedTicks;
//Remise à zero du pausedTicks
pausedTicks = 0;
}
}
int Timer::get_ticks()
{
//Si le timer est en marche
if( started == true )
{
//Si le timer est en pause
if( paused == true )
{
//On retourne le nombre de ticks quand le timer a été mis en pause
return pausedTicks;
}
else
{
//On retourne le temps courant moins le temps quand il a démarré
return SDL_GetTicks() - startTicks;
}
}
//Si le timer n'est pas en marche
return 0;
}
bool Timer::is_started()
{
return started;
}
bool Timer::is_paused()
{
return paused;
}
SDL_Surface *load_image( std::string filename )
{
//L'image qui est chargée
SDL_Surface* loadedImage = NULL;
//L'image optimisée qu'on va utiliser
SDL_Surface* optimizedImage = NULL;
//Chargement de l'image
loadedImage = SDL_LoadBMP( filename.c_str() );
//Si l'image a bien chargée
if( loadedImage != NULL )
{
//Création de l'image optimisée
optimizedImage = SDL_DisplayFormat( loadedImage );
//Libération de l'ancienne image
SDL_FreeSurface( loadedImage );
//Si la surface a bien été optimisée
if( optimizedImage != NULL )
{
//transparence
SDL_SetColorKey( optimizedImage, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB( optimizedImage->format, 255, 255, 255 ) );
}
}
//On retourne l'image optimisée
return optimizedImage;
}
bool load_files()
{
//Chargement du point
hero = load_image( "SiOu_Left_1.bmp" );
//S'il y a eu un problème au chargement du point
if( hero == NULL )
{
return false;
}
//Si tout s'est bien passé
return true;
}
int main ( int argc, char** argv )
{
//SDL_Surface *screen = NULL;
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
// create a new window
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_HWSURFACE | SDL_DOUBLEBUF );
Timer fps;
Hero SiOu;
SDL_Surface *texture1 = NULL, *texture2 = NULL, *texture3 = NULL;
SDL_Rect position;
int map[NB_BLOCS_LARGEUR][NB_BLOCS_HAUTEUR];
int i,j;
int x,y;
//chargement des textures
//texture1 = SDL_LoadBMP("tn225.bmp");
texture1 = load_image("tn225.bmp");
//texture2 = SDL_LoadBMP("tn226.bmp");
texture2 = load_image("tn226.bmp");
//texture3 = SDL_LoadBMP("tn227.bmp");
texture3 = load_image("tn227.bmp");
if ( !screen )
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return 1;
}
//Chargement des fichiers
load_files();
// program main loop
bool done = false;
while (!done)
{
// message processing loop
SDL_Event event;
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN:
{
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
break;
}
} // end switch
} // end of message processing
fps.start();
//Tant qu'il y a un événement
while( SDL_PollEvent( &event ) )
{
//On recupere l'evenement pour le point
SiOu.handle_input();
//Si l'utilisateur a cliqué sur le X de la fenêtre
if( event.type == SDL_QUIT )
{
//On quitte the programme
//quit = true;
}
}
//Bouge le point
SiOu.move();
//Met la camera
SiOu.set_camera();
//Affiche le fond
apply_surface( 0, 0, NULL, screen, &camera );
if (!chargerMap(map))
{
exit(EXIT_FAILURE);
}
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
//Le point qu'on va utilise
x = 0;
y = 0;
for (i = 0 ; i < NB_BLOCS_LARGEUR ; i++)
{
for (j = 0 ; j < NB_BLOCS_HAUTEUR ; j++)
{
position.x = i * TAILLE_BLOC;
position.y = j * TAILLE_BLOC;
switch(map[i][j])
{
case VIDE:
SDL_BlitSurface(texture1, NULL, screen, &position);
break;
case MUR:
SDL_BlitSurface(texture2, NULL, screen, &position);
break;
case CAISSE:
SDL_BlitSurface(texture2, NULL, screen, &position);
break;
}
}
}
//Affiche le point sur l'écran
SiOu.show();
//Mise à jour de l'écran
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
//Tant que le timer fps n'est pas assez haut
while( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
//On attend...
}
} // end main loop
// all is well ;)
printf("Exited cleanly\n");
return 0;
} |
Partager