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
| #include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <SDL.h>
#include <SDL_ttf.h>
#include <SDL/SDL_image.h>
#include <SDL_mixer.h>
#include <SDL_sound.h>
void audioCallBack(void *udata, Uint8 *stream, int len);
/* http://loka.developpez.com/tutoriel/sdl */
int main ( int argc, char** argv )
{
SDL_Surface* screen;
SDL_Surface *message = NULL;
SDL_Surface* bmp;
SDL_Rect dstrect;
unsigned short i=0;
bool done = false;
char ch[20];
TTF_Font *font;
SDL_Event event;
SDL_Color textColor = { 255, 255, 255 };
SDL_AudioSpec audioSortie,audioExacte;
SDL_AudioSpec audioBufferSpec;
Uint8 *audioBuffer;
Uint32 audioBufferLen;
Uint32 audioLen, audioPos;
// initialize SDL video
if ( SDL_Init( SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0 )
{ /* pour l'image pour le son */
printf( "Unable to init SDL: %s\n", SDL_GetError() );
return 1;
}
// make sure SDL cleans up before exit
atexit(SDL_Quit);
// Définition des propriétés audio
audioSortie.freq = 44100;
audioSortie.format = AUDIO_S16;
audioSortie.channels = 2;
audioSortie.samples = 1024;
audioSortie.callback =audioCallBack;
audioSortie.userdata = malloc(4*sizeof(void *));
// définition des données de la fonction callback
((Uint32 ** )(audioSortie.userdata))[0]=&audioLen;
((Uint32 ** )(audioSortie.userdata))[1]=&audioPos;
((Uint32 ** )(audioSortie.userdata))[2]=&(audioBufferSpec.size);
((Uint8 ** )(audioSortie.userdata))[3]=audioBuffer;
// Initialisation de la couche audio
if (SDL_OpenAudio(&audioSortie, &audioExacte) < 0)
{
fprintf(stderr, "Erreur d'ouverture audio: %s\n", SDL_GetError());
return (-1);
}
// A inclure dans votre code pour charger un fichier sonore dans audioBuffer
if(!SDL_LoadWAV("son/orage.wav", &audioBufferSpec,
&audioBuffer, &audioBufferLen))
{
printf("Erreur lors du chargement du fichier WAV.\n");
return 1;
}
printf("Propriétés du fichier audio %d : %d canaux, %d fréquence %d octets.\n",
audioBufferSpec.format & 0xff, audioBufferSpec.channels, audioBufferSpec.freq, audioBufferLen);
//SDL_FreeWAV(audioBuffer);
// Lancement de la lecture
SDL_PauseAudio(0);
// Attendre que la lecture du son soit terminée
audioLen=audioBufferLen;
printf("%d ",audioLen);
while ( audioLen > 0 )
{
SDL_Delay(100);
printf("%d ",audioLen);
}
printf("\n");
// Fermeture du module
SDL_CloseAudio();
// create a new window
screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE|SDL_DOUBLEBUF | SDL_RESIZABLE);
if (!screen)
{
printf("Unable to set 640x480 video: %s\n", SDL_GetError());
return 1;
}
// load an image
//bmp = SDL_LoadBMP("cb.bmp");
bmp = IMG_Load("images/saint_manchot.png");
if (!bmp)
{
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 1;
}
//Initialisation de SDL_TTF
if( TTF_Init() == -1 )
{
return false;
}
//Ouverture du Font
font = TTF_OpenFont( "/home/bs/.font/a010013l.ttf", 28 );
//S'il y a une erreur dans le chargement du Font
if( font == NULL )
{
printf("Erreur de chargement de la font\n");
return false;
}
//Mise en place du texte sur la surface message
message = TTF_RenderText_Solid( font, "Test pour sdl_ttf", textColor );
//S'il y a une erreur dans la mise en place du texte
if( message == NULL )
{
return 1;
}
// centre the bitmap on screen
//SDL_Rect dstrect;
//dstrect.x = (screen->w - bmp->w) / 2;
//dstrect.y = (screen->h - bmp->h) / 2;
dstrect.x =0;
dstrect.y =0;
// program main loop
while (!done)
{
// Tant qu'il y a un événement à traiter
dstrect.x =i%(screen->w - bmp->w);
dstrect.y =i%(screen->h - bmp->h);
while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type) // voir /usr/include/SDL/SDL_events.h
{
// exit if the window is closed
case SDL_QUIT:
done = true;
break;
// check for keypresses
case SDL_KEYDOWN: // voir /usr/include/SDL/SDL_keysym.h
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
done = true;
if (event.key.keysym.sym == SDLK_a) // 'a'
{
// A inclure dans votre code pour charger un fichier sonore dans audioBuffer
if(!SDL_LoadWAV("son/orage.wav", &audioBufferSpec,
&audioBuffer, &audioBufferLen))
{
printf("Erreur lors du chargement du fichier WAV.\n");
return 1;
}
// Lancement de la lecture
SDL_PauseAudio(0);
// Attendre que la lecture du son soit terminée
while ( audioLen > 0 )
{
printf("%d/%d ",audioLen,audioPos);
SDL_Delay(100);
}
// Fermeture du module
SDL_CloseAudio();
printf("%d/%d\n",audioLen,audioPos);
i=1;
//done = true;
}
break;
case SDL_MOUSEMOTION:
dstrect.x =event.motion.x;
dstrect.y =event.motion.y;
break;
case SDL_VIDEORESIZE : // changement de taille
screen = SDL_SetVideoMode( event.resize.w, event.resize.h,16, SDL_HWSURFACE|SDL_DOUBLEBUF | SDL_RESIZABLE);
break;
} // end switch
} // end of message processing
// DRAWING STARTS HERE
// clear screen
if (i==0)
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
// draw bitmap
SDL_BlitSurface(bmp, 0, screen, &dstrect);
textColor.r=(65536-i)%256;
textColor.g=i%256;
textColor.b=(3*i)%255;
message = TTF_RenderText_Solid( font, "Test pour sdl_ttf", textColor );
dstrect.x =(4*i)%(screen->w - message->w);
dstrect.y =(4*i)%(screen->h - message->h);
SDL_BlitSurface(message, 0, screen, &dstrect);
if (i%256==0)
{
sprintf(ch,"%hu",i>>8);
SDL_WM_SetCaption( ch, NULL );
}
i++;
// DRAWING ENDS HERE
// finally, update the screen :)
SDL_Flip(screen);
} // end main loop
// free loaded bitmap
SDL_FreeSurface(bmp);
// all is well ;)
printf("Exited cleanly\n");
return 0;
}
void audioCallBack(void *udata, Uint8 *stream, int len)
{
printf("*((Uint32 **)udata)[0]=%d et len=%d\n",*((Uint32 **)udata)[0],len);
// On ne lit que s'il reste des données à jouer
if ( *((Uint32 **)udata)[0] == 0 )
return;
// Remise à zéro du tampon de sortie
memset(stream, 0, len);
// Lecture du buffer audio
if (*((Uint32 **)udata)[1] < *((Uint32 **)udata)[2]) {
if (*((Uint32 **)udata)[1]+len > *((Uint32 **)udata)[2])
len = *((Uint32 **)udata)[2] = *((Uint32 **)udata)[1];
SDL_MixAudio(stream, ((Uint8 **)udata)[3] + *((Uint32 **)udata)[1],
len, SDL_MIX_MAXVOLUME);
*((Uint32 **)udata)[1] += len;
}
// Décrémentation de ce qu'il reste à lire
if (*((Uint32 **)udata)[0] > len)
*((Uint32 **)udata)[0] -= len;
else
*((Uint32 **)udata)[0] =0;
} |
Partager