Voila j'ai vu le tutaux sur le deplacement en SDL
je voudrai savoir si il est possible de creer une fonction propre au deplacement du clavier et d'ajouter cette fonction
voici mon code
- Code: Tout sélectionner
#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL_audio.h>
#include <SDL_mixer.h>
#include <SDL_image.h>
#include <string>
#include <SDL_ttf.h>
Mix_Music* myMus;
SDL_Event event;
SDL_Surface *message = NULL, *intro;
SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;
SDL_Surface *back =NULL;
SDL_Surface *rectangle1=NULL;
SDL_Surface *rectangle;
SDL_Rect rect;
void clavier();
void clean_up();
void audio(void);
//The font that's going to be used
TTF_Font *font;
//The color of the font
SDL_Color TextColor = {0, 0, 255};
/*SDL_Surface *load_image ( std ::string filename)
{
SDL_Surface* loadedImage = NULL;
SDL_Surface* optimizedImage = NULL;
loadedImage = SDL_LoadBMP (filename.c_str() );
if ( loadedImage != NULL)
{ optimizedImage = SDL_DisplayFormat (loadedImage);
SDL_FreeSurface (loadedImage);
}
return optimizedImage;
}*/
void apply_surface (int x,int y, SDL_Surface* source, SDL_Surface* destination)
{ SDL_Rect offset;
offset.x = x;
offset.y = y;
// blit de la surface
SDL_BlitSurface (source, NULL, destination, &offset);
SDL_Flip(screen);
}
int main (int argc, char* args[])
{ if (SDL_Init(SDL_INIT_EVERYTHING) == -1)
{ return 1;
}
bool quit = false;
if( TTF_Init() == -1 )
{
return false; }
//Open the font
font = TTF_OpenFont( "police12.ttf", 30);
SDL_Init(SDL_INIT_AUDIO);
/* code à exécuter en cas de pression sur une touche du clavier */
Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT, 2, 1024);
screen= SDL_SetVideoMode (1024,768, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
if (screen == NULL)
{ return 1;
}
SDL_WM_SetCaption ("fab", NULL);
rectangle1 = IMG_Load ("fab.bmp");
background = IMG_Load ("fond.png");
//Render the text
message = TTF_RenderText_Solid( font, "Bienvenue au jeu de Basket", TextColor);
intro = TTF_RenderText_Solid( font, "appuyer sur entrée", TextColor);
//If there was an error in rendering the text
if( rectangle1 == NULL )
{
return 1;
}
apply_surface (0,0, background, screen);
apply_surface (10,100, message, screen);
//apply_surface( 10, 200, rectangle1, screen );
//rect=SDL_FillRect(rectangle,NULL,SDL_MapRGB(screen -> format, 100,10,30));
//SDL_BlitSurface(rectangle,NULL,screen,&rect);
if (SDL_Flip (screen) == -1)
{
return 1;
}
//While the user hasn't quit
while( quit == false )
{
//While there's events to handle
while ( SDL_PollEvent (&event ) )
{ clavier();
//If the user has Xed out the window
if (event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
}
}
clean_up();
return 0;
}
void audio (void)
{ // On charge un fichier audio
myMus = Mix_LoadMUS ("concentr.wav");
//on lance la musique
Mix_PlayMusic(myMus, 1);
while (Mix_PlayingMusic() ==1)
{ /* Tant que la musique ne s'est pas termine on fait quelquechose car sinon le programme
s 'eteint et la musique aussi*/
SDL_Delay(10);}
Mix_FreeMusic (myMus);
// on libere le materiel
Mix_CloseAudio();
}
void clean_up()
{
SDL_FreeSurface (message);
SDL_FreeSurface (background);
Mix_FreeMusic (myMus);
SDL_Quit();
}
void clavier()
{
SDL_Event event;
switch (event.type)
{ case SDL_KEYDOWN:
switch (event.key.keysym.sym)
{
case SDLK_ESCAPE: audio();
break;
default:;
}
}
}
le probleme est que quand j'execute le programme aucune musique ne se lance lorsque j'appuie sur la touche echape
ainsi y'aurai t'il un autre moyen pour gerer les deplacement en dehors du main
merci pour vos reponses