beuted | Bonjour à tous,
Je me lance dans le C++ et la bibliothèque sdl pour essayer de coder un truc très simple pour l'instant : un personnage qui se balade sur un terrain vue de 3/4. J'en suis à l’étape où mon personnage "glisse" (pas d'animation de marche pour l'instant) sur le terrain.
Et j'ai un problème que j'aimerais régler avant d'aller plus loin un consommation de 80% du cpu lors de l’exécution... Je trouve ca un peu excessif, dites moi si je me tompe. (d'un autre coté je viens de remarquer que firefox en utilise 64%)
Voici le code (je suis ouverts à tout conseil/remarque/insulte) :
Code :
- #include <stdlib.h>
- #include <stdio.h>
- #include <iostream>
- #include <SDL/SDL.h> //bibli de base de SDL
- #include <SDL/SDL_image.h> //pour inclure des images non bmp.
- #include "personnage.h"
- #include "map.h"
- #include "affichage.h"
- using namespace std;
- void pauseIci(); // pause() était deja pris ...
- int main()
- {
- cout << "begining of the story baby" << endl;
- //creation du personnage
- Personnage monPersonnage;
- monPersonnage.afficherEtat();
- //creation de la carte
- Map maCarte;
- maCarte.insererObjet(4,4,2);
- maCarte.afficherTerrainTerm();
-
- //testSDL
-
- Affichage monEcran;
- monEcran.initialisationFenetre();
- monEcran.definirTerrain();
- monEcran.afficherTerrain();
- monEcran.definirPersonnage();
- monEcran.afficherPersonnage();
- monEcran.flip();
- SDL_Event event;
- int continuer = 1;
- int tempsPrecedent = 0, tempsActuel = 0;
- while (continuer)
- {
- SDL_PollEvent(&event);
- switch(event.type)
- {
- case SDL_QUIT:
- continuer = 0;
- break;
- case SDL_KEYDOWN:
- tempsActuel = SDL_GetTicks();
- if (tempsActuel - tempsPrecedent > 30)
- // Si 30 ms se sont écoulées depuis le dernier tour de boucle
- {
- monEcran.deplacement(event);
- tempsPrecedent = tempsActuel;
- }
- else //on endort le programme le temps qu'il faut
- {
- SDL_Delay(30 - (tempsActuel - tempsPrecedent));
- }
- break;
- }
- monEcran.clearScreen();
- monEcran.afficherTerrain();
- monEcran.afficherPersonnage();
- monEcran.flip();
- }
- cout << "sortie de boucle, on désaloue la memoire" << endl;
- monEcran.libererMemoire();
- //monEcran.pauseIci();
- return 0;
- }
|
Et le code de Affichage.cpp dans la foulé :
Code :
- #include "affichage.h"
- Affichage::Affichage()
- {
- // Definition variables de l'ecran
- HAUTEUR_ECRAN = 500;
- LARGEUR_ECRAN = 500;
-
- //Personnage
- positionPersonnage.x = LARGEUR_ECRAN/2;
- positionPersonnage.y = HAUTEUR_ECRAN/2;
-
- //directions
- direction = 0;
- LEFT = 0;
- RIGHT = 1;
- UP = 2 ;
- DOWN = 4 ;
- //animation
- frame = 0;
-
- //Decoupe du personnage
- for(int i(0);i<4;i++) {
- decoupePersonnageL[ i ].x = 26 * i -23;
- decoupePersonnageL[ i ].y = 3*38 +2;
- decoupePersonnageL[ i ].w = 26;
- decoupePersonnageL[ i ].h = 35;
- }
- for(int i(0);i<4;i++) {
- decoupePersonnageR[ i ].x = 205-26 * i -3;
- decoupePersonnageR[ i ].y = 3*38 +2;
- decoupePersonnageR[ i ].w = 26;
- decoupePersonnageR[ i ].h = 35;
- }
- for(int i(0);i<4;i++) {
- decoupePersonnageU[ i ].x = 26 * i -23;
- decoupePersonnageU[ i ].y = 1*38 +2;
- decoupePersonnageU[ i ].w = 26;
- decoupePersonnageU[ i ].h = 35;
- }
- for(int i(0);i<4;i++) {
- decoupePersonnageD[ i ].x = 26 * i -23;
- decoupePersonnageD[ i ].y = 5*38 +3;
- decoupePersonnageD[ i ].w = 26;
- decoupePersonnageD[ i ].h = 35;
- }
- }
- Affichage::~Affichage()
- {
- }
- void Affichage::initialisationFenetre()
- {
- SDL_Init(SDL_INIT_VIDEO); // Initialisation de la SDL
- ecran = SDL_SetVideoMode(LARGEUR_ECRAN, HAUTEUR_ECRAN, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
- SDL_WM_SetCaption("Jeu cool", NULL); // Change titre de la fenetre
- SDL_EnableKeyRepeat(10, 10); // interval de repetition des touches
- coordX = 0;
- coordY = 0;
- }
- void Affichage::afficherTerrain()
- {
- SDL_Rect positionFond;
- positionFond.x = coordX;
- positionFond.y = coordY;
- SDL_BlitSurface(Terrain, NULL, ecran, &positionFond);
- }
- void Affichage::afficherPersonnage()
- {
- if (direction == LEFT)
- {
- SDL_BlitSurface(Personnage, &decoupePersonnageL[1], ecran, &positionPersonnage);
- }
- else if (direction == RIGHT)
- {
- SDL_BlitSurface(PersonnageInverse, &decoupePersonnageR[1], ecran, &positionPersonnage);
- }
- else if (direction == UP)
- {
- SDL_BlitSurface(Personnage, &decoupePersonnageU[1], ecran, &positionPersonnage);
- }
- else if (direction == DOWN)
- {
- SDL_BlitSurface(Personnage, &decoupePersonnageD[1], ecran, &positionPersonnage);
- }
- }
- void Affichage::definirTerrain()
- {
- Terrain = IMG_Load("ressources/herbe.jpg" );
- }
- void Affichage::definirPersonnage()
- {
- Personnage = IMG_Load("ressources/iceboy.png" );
- Personnage = SDL_DisplayFormat( Personnage );
- SDL_SetColorKey(Personnage, SDL_RLEACCEL | SDL_SRCCOLORKEY, SDL_MapRGB(Personnage->format, 180, 255, 180)); // vire le fond
- PersonnageInverse = copierEtRetourner(Personnage);
- }
- void Affichage::flip()
- {
- SDL_Flip(ecran);
- }
- void Affichage::pauseIci()
- {
- int continuer = 1;
- SDL_Event event;
- while (continuer)
- {
- SDL_WaitEvent(&event);
- switch(event.type)
- {
- case SDL_QUIT:
- continuer = 0;
- }
- }
- }
- void Affichage::deplacement(SDL_Event event)
- {
- switch(event.key.keysym.sym)
- {
- case SDLK_UP: // Fleche haut
- coordY++;direction = UP; break;
- case SDLK_DOWN: // Fleche bas
- coordY--;direction = DOWN; break;
- case SDLK_RIGHT: // Fleche droite
- coordX--; direction = RIGHT; break;
- case SDLK_LEFT: // Fleche gauche
- coordX++; direction = LEFT; break;
- }
- }
- void Affichage::clearScreen()
- {
- SDL_FillRect(ecran, NULL, SDL_MapRGB(ecran->format, 255, 255, 255));
- }
- void Affichage::libererMemoire()
- {
- SDL_FreeSurface(Personnage);
- SDL_FreeSurface(Terrain);
- }
|
Merci d'avance. |