Bonjour,
je suis en train de m’essayer au développement d'un Shoot'them'up en side-scrolling en C++ avec SDL.
Si je viens ici, vous vous doutez que ce n'est pas pour vous dire ça.
J'ai un problème ( et oui =/ ) !
Bref, lors de la création d'un niveau. La classe Level charge le premier niveau.
Elle charge alors trois images qui se déplace sur l'axe X.
Une image représentant un fond spatial (
Layer1x += 1)
Deux images transparentes représentants des particules spatiales (
Layer2x += 4 ;
Layer3x+= 8 )
Ensuite je blit la 1 dans une variable finalLayer, puis la 2 et enfin la 3.
Après tout ça, je retourne la
finalLayer au
screenManager pour qu'il l'affiche.
Ca fonctionne (en partie)
Il affiche bien les trois images mais les points blancs (particules spatiales) forment des lignes...
Un petit exemple en image ?
Remarque, on peut partir dans l'hyperespace comme ça, mais c'est pas ce que je recherche =/
Avez-vous une solutions ou une indications qui pourrait m'aider ?
-------
un peu de code maintenant pour ceux qui préfèrent ?
Code Level.cpp :
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
|
/** prepareLevel - Prepare le niveau demandé (à partir du main)
* @return void
*/
void Level::prepareLevel(int i)
{
switch (i)
{
case 1:
Level1();
break;
default:
Level1();
break;
}
}
/** loadImage(char* str) - Charge l'image (BMP, PNG, JPG, ...) selon le chemin str
* @return SDL_Surface - Renvoie l'image optimisée et transparente RGB(255,0,255)
*/
SDL_Surface *Level::loadImage(char* str)
{
this->loadedImg = IMG_Load(str);
this->optimizedImg = SDL_DisplayFormat(this->loadedImg);
SDL_FreeSurface(this->loadedImg);
Uint32 colorkey = SDL_MapRGB(this->optimizedImg->format, 0xFF, 0x00, 0xFF);
SDL_SetColorKey(this->optimizedImg, SDL_SRCCOLORKEY, colorkey);
return this->optimizedImg;
}
/** update() - Ajoute N à X1, N+4 à X2, N+8 à X3, ...
* @return void -
*/
void Level::update()
{
Layer1x -= 1;
Layer2x -= 1;
Layer3x -= 0;
}
/** Level1() - Charge les images nécéssaire au décor du niveau 1
* @return void -
*/
void Level::Level1()
{
this->Layer1 = this->loadImage("data/pictures/Space01001.jpg");
Layer1x = 0;
Layer1y = 0;
this->Layer2 = this->loadImage("data/pictures/Space01003.png");
Layer2x = 0;
Layer2y = 0;
this->Layer3 = this->loadImage("data/pictures/Space01004.png");
Layer3x = 0;
Layer3y = 0;
return;
}
/** getLevel1() - Renvoie la couche finale après blittin des 3 couches ensembles
* @return SDL_Surface - couche finale à blitter sur le screen
*/
SDL_Surface *Level::getLevel1()
{
SDL_Rect r;
SDL_Surface *finalLayer;
finalLayer = this->Layer1;
r.x = this->Layer2x;
r.y = this->Layer2y;
SDL_BlitSurface(this->Layer2, NULL, finalLayer, &r);
r.x = this->Layer3x;
r.y = this->Layer3y;
SDL_BlitSurface(this->Layer3, NULL, finalLayer, &r);
return finalLayer;
}
/** getLevelx() - Renvoie les coordonnées de Layer1x
* @return int - Renvoie les coordonées de Layer1x (référence)
*/
int Level::getLevelx() const
{
return Layer1x;
}
/** getLevely() - Renvoie les coordonnées de Layer1y
* @return int - Renvoie les coordonées de Layer1y (référence)
*/
int Level::getLevely() const
{
return Layer1y;
}
SDL_Surface *Level::getLevel2() const
{
return this->Layer2;
} |
Code main.cpp :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
|
// (...)
// Update player's posX, posY, status, HP, ...
player->update();
// Update level's posX, posY of each layer
level->update();
// Draw the level design
screen->applySurface(level->getLevelx(),level->getLevely(), level->getLevel1());
// Draw the player Sprite
screen->applySurface(player->getX(),player->getY(),player->getSprite());
// Flip Screen
screen->flip();
// ... |
Code screenManager.cpp :
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
|
ScreenManager::ScreenManager(int width, int height)
{
this->bpp = 32;
this->width = width;
this->height = height;
screen = SDL_SetVideoMode(this->width, this->height, this->bpp, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_ASYNCBLIT);
}
ScreenManager::~ScreenManager()
{
SDL_FreeSurface(screen);
}
/** toggleFullscreen() - Comme son nom l'indique, il toggle le fullscreen
* @return void
*/
void ScreenManager::toggleFullscreen()
{
if (!fullscreen)
screen = SDL_SetVideoMode(this->width, this->height, this->bpp, SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);
else
screen = SDL_SetVideoMode(this->width, this->height, this->bpp, SDL_HWSURFACE|SDL_DOUBLEBUF);
return;
}
/** setHeight(int) - Changer la valeur height du screen
* @return void
*/
void ScreenManager::setHeight(int height)
{
this->height = height;
}
/** setWidth(int) - Changer la valeur width du screen
* @return void
*/
void ScreenManager::setWidth(int width)
{
this->width = width;
}
/** getHeight(int) - Récupère la valeur height du screen
* @return void
*/
int ScreenManager::getHeight() const
{
return this->height;
}
/** getWidth(int) - Récupère la valeur width du screen
* @return void
*/
int ScreenManager::getWidth() const
{
return this->width;
}
/** clear() - Remplit l'écran d'un beau noir
* @return void
*/
void ScreenManager::clear()
{
SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0x00, 0x00, 0x00 ) );
}
/** flip() - Flip the screen
* @return void
*/
void ScreenManager::flip()
{
SDL_Flip(this->screen);
}
/** setCaption() - Change le titre de la fenetre
* @return void
*/
void ScreenManager::setCaption(char* cap = NULL, char* icon = NULL)
{
SDL_WM_SetCaption(cap, icon);
}
/** applySurface(int, int, SDL_Surface) - Applique une simple surface sur l'écran
* @return void
*/
void ScreenManager::applySurface(int x, int y, SDL_Surface *src)
{
SDL_Rect r;
r.x = x;
r.y = y;
SDL_BlitSurface(src, NULL, this->screen, &r);
} |