217 lines
4.9 KiB
C++
217 lines
4.9 KiB
C++
|
/* [CONSTRUCTOR] Constructeur de la classe
|
||
|
=========================================================*/
|
||
|
sdltl::sdltl(const char *t, int w, int h){
|
||
|
// default values
|
||
|
_lasttick = 0;
|
||
|
_fpstime = 1000/60;
|
||
|
_window = NULL;
|
||
|
_renderer = NULL;
|
||
|
_texture = NULL;
|
||
|
|
||
|
|
||
|
// Initialisation des sous-sys. SDL
|
||
|
SDL_Init( SDL_INIT_EVERYTHING );
|
||
|
|
||
|
|
||
|
// Creation de la fenetre
|
||
|
_window = SDL_CreateWindow(
|
||
|
t,
|
||
|
SDL_WINDOWPOS_CENTERED,
|
||
|
SDL_WINDOWPOS_CENTERED,
|
||
|
w,
|
||
|
h,
|
||
|
SDL_WINDOW_SHOWN
|
||
|
);
|
||
|
|
||
|
// Gestion erreur
|
||
|
if( _window == NULL )
|
||
|
return;
|
||
|
|
||
|
|
||
|
// Creation du renderer
|
||
|
_renderer = SDL_CreateRenderer(
|
||
|
_window,
|
||
|
-1,
|
||
|
SDL_RENDERER_ACCELERATED
|
||
|
);
|
||
|
|
||
|
}
|
||
|
|
||
|
/* [DESTROYER] Destructeur de la classe
|
||
|
=========================================================*/
|
||
|
sdltl::~sdltl(){
|
||
|
SDL_DestroyTexture(_texture);
|
||
|
SDL_DestroyRenderer(_renderer);
|
||
|
SDL_DestroyWindow( _window );
|
||
|
SDL_Quit();
|
||
|
}
|
||
|
|
||
|
/* [STATUS] Retourne le status
|
||
|
=========================================================*/
|
||
|
bool sdltl::status(){
|
||
|
return _window != NULL && _renderer != NULL;
|
||
|
}
|
||
|
|
||
|
|
||
|
/* [WINDOW] Retourne la fenetre
|
||
|
=========================================================*/
|
||
|
SDL_Window* sdltl::window(){ return _window; }
|
||
|
|
||
|
|
||
|
/* [SCREEN] Retourne la fenetre
|
||
|
=========================================================*/
|
||
|
SDL_Renderer* sdltl::renderer(){ return _renderer; }
|
||
|
|
||
|
|
||
|
/* [SETBACKGROUND] Modifie la couleur de fond
|
||
|
=========================================================*/
|
||
|
bool sdltl::setBackground(int r, int g, int b){
|
||
|
/*// On cree la surface
|
||
|
SDL_Surface *surf = NULL;
|
||
|
int win[2];
|
||
|
SDL_GetWindowSize(_window, &win[0], &win[1]);
|
||
|
|
||
|
// On cree la surface
|
||
|
surf = SDL_CreateRGBSurface(0, win[0], win[1], 32, 0, 0, 0, 0);
|
||
|
|
||
|
if( surf == NULL )
|
||
|
return false;
|
||
|
|
||
|
// On cree la couleur
|
||
|
Uint32 color = SDL_MapRGB(surf->format, r, g, b);
|
||
|
|
||
|
// On colore la surface
|
||
|
SDL_FillRect(surf, NULL, color);
|
||
|
|
||
|
|
||
|
// On applique a la texture
|
||
|
_texture = SDL_CreateTextureFromSurface(_renderer, surf);
|
||
|
|
||
|
if( _texture == NULL )
|
||
|
return false;
|
||
|
|
||
|
|
||
|
// On libere la memoire
|
||
|
SDL_FreeSurface(surf);
|
||
|
surf = NULL;
|
||
|
*/
|
||
|
|
||
|
SDL_SetRenderDrawColor( _renderer, 0xFF, 0xFF, 0xFF, 0x00 );
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
/* [SETIMAGE] Met une image en fond
|
||
|
=========================================================*/
|
||
|
bool sdltl::setImage(const char *url){
|
||
|
|
||
|
SDL_Surface *image = NULL;
|
||
|
image = SDL_LoadBMP( url );
|
||
|
|
||
|
// Si erreur de chargement image
|
||
|
if( image == NULL )
|
||
|
return false;
|
||
|
|
||
|
// On cree la texture associee
|
||
|
_texture = SDL_CreateTextureFromSurface(
|
||
|
_renderer,
|
||
|
image
|
||
|
);
|
||
|
|
||
|
// On libere la surface pour l'image
|
||
|
SDL_FreeSurface(image);
|
||
|
delete image;
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
/* [ADD] Ajoute une texture au rendu principal
|
||
|
=========================================================*/
|
||
|
void sdltl::add(SDL_Texture *t, SDL_Rect *origin, SDL_Rect *dest){
|
||
|
_sprites.push_back( t );
|
||
|
_origins.push_back( origin );
|
||
|
_dests.push_back( dest );
|
||
|
}
|
||
|
|
||
|
|
||
|
/* [REMOVE] Retire une texture du rendu principal
|
||
|
=========================================================*/
|
||
|
void sdltl::remove(SDL_Texture *t){
|
||
|
// On cherche l'indice de la texture
|
||
|
int index = -1;
|
||
|
|
||
|
for( int i = 0 ; i < _sprites.size() ; i++ )
|
||
|
if( _sprites[i] == t ) index = i;
|
||
|
|
||
|
// Si on a rien trouve
|
||
|
if( index == -1 )
|
||
|
return;
|
||
|
|
||
|
// On supprime la texture et ses dimensions
|
||
|
_sprites.erase( _sprites.begin() + index );
|
||
|
_origins.erase( _origins.begin() + index );
|
||
|
_dests.erase( _dests.begin() + index );
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
/* [UPDATE] Mise a jour du rendu
|
||
|
=========================================================*/
|
||
|
void sdltl::update(){
|
||
|
// On efface l'ecran
|
||
|
SDL_RenderClear(_renderer);
|
||
|
|
||
|
// On ajoute le rendu principal au rendu
|
||
|
if( _texture != NULL)
|
||
|
SDL_RenderCopy(_renderer, _texture, NULL, NULL);
|
||
|
|
||
|
// On ajoute toutes les textures ajoutees
|
||
|
cout << "Update MAIN SPRITE +" << _sprites.size() << " added sprites.." << endl;
|
||
|
// for( int i = _sprites.size()-1 ; i >= 0 ; i-- )
|
||
|
for( int i = 0 ; i < _sprites.size() ; i++ )
|
||
|
SDL_RenderCopy(_renderer, _sprites[i], _origins[i], _dests[i]);
|
||
|
|
||
|
// On affiche le resultat
|
||
|
SDL_RenderPresent(_renderer);
|
||
|
}
|
||
|
|
||
|
/* [WAITEVENT] Attends un evenement pour executer
|
||
|
=========================================================*/
|
||
|
void sdltl::waitEvent(SDL_EventType t, void(*handler)(SDL_Event*)){
|
||
|
SDL_Event event;
|
||
|
|
||
|
// On attends un evenement
|
||
|
while( SDL_PollEvent(&event) ){
|
||
|
|
||
|
// quand evenement, si type = t
|
||
|
if( event.type == t )
|
||
|
(*handler)(&event);
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/* [MANAGEFTP] Gestion de la vitesse de boucle
|
||
|
=========================================================*/
|
||
|
void sdltl::manageFps(const int fps){
|
||
|
/* (1) Definition de fps */
|
||
|
if( fps != 0 ) _fpstime = 1000/fps;
|
||
|
|
||
|
/* (2) Initialisation timer */
|
||
|
if( _lasttick == 0 )
|
||
|
_lasttick = SDL_GetTicks();
|
||
|
|
||
|
/* (3) Utilisation en fin de WHILE() */
|
||
|
|
||
|
// 1 > Si trop rapide, on attends + definit _lasttick
|
||
|
else if( SDL_GetTicks()-_lasttick < _fpstime ){
|
||
|
SDL_Delay( _fpstime - (SDL_GetTicks()-_lasttick) );
|
||
|
|
||
|
_lasttick = SDL_GetTicks() + _fpstime - (SDL_GetTicks()-_lasttick);
|
||
|
|
||
|
// 2 > Temps ok
|
||
|
}else
|
||
|
_lasttick = SDL_GetTicks();
|
||
|
|
||
|
|
||
|
}
|