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
| /*===============================================================================================
Search if a mp3 files contains the APIC tag, and display image
===============================================================================================*/
#include <sdl/sdl.h>
#include <fmodex/fmod.h>
#include <fmodex/fmod_errors.h>
#include <windows.h>
//#include <stdio.h>
//#include <dos.h>
//#include <dir.h>
#define STB_IMAGE_IMPLEMENTATION
#include "../stb_image/stb_image.c" // https://github.com/nothings/stb/blob/master/stb_image.h
void pause(void) {
SDL_Event event;
int done=0;
while(!done) {
SDL_WaitEvent(&event);
done=event.type==SDL_KEYDOWN || event.type==SDL_MOUSEBUTTONDOWN;
}
}
#define WNDP 32
#define BITS 8
void displayImg(SDL_Surface *wnd,int width,int height,int comp,stbi_uc *img) {
SDL_Surface *new;
// SDL_CreateRGBSurfaceFrom(): IMPORTANT: voir Description sur http://sdl.beuc.net/sdl.wiki/SDL_CreateRGBSurfaceFrom
new=SDL_CreateRGBSurfaceFrom(img,width,height,BITS*comp,width*comp,0,0,0,0);
if(new==NULL) puts("new null");
else {
SDL_Rect srcCoo;
SDL_FillRect(wnd,NULL,SDL_MapRGB(wnd->format,110,110,110));
srcCoo.x=0;
srcCoo.y=0;
srcCoo.w=(new->w>wnd->w)?wnd->w:new->w;
srcCoo.h=(new->h>wnd->h)?wnd->h:new->h;
SDL_BlitSurface(new,&srcCoo,wnd,NULL);
SDL_Flip(wnd);
pause();
SDL_FreeSurface(new);
}
}
// Affichage de l'image contenue dans un fichier mp3
// Recherche du tag avec fmod
// Affichage avec sdl
// Utilisation de stb_image pour ne pas passer par un fichier intermédiaire
int main(int argc,char *argv[]) {
FMOD_SYSTEM *system;
FMOD_SOUND *sound;
FMOD_RESULT result;
FMOD_TAG tag;
int numtags;
SDL_Surface *wnd;
const SDL_VideoInfo *videoInfo;
// Init sdl
SDL_Init(SDL_INIT_VIDEO);
putenv("SDL_VIDEO_WINDOW_POS=10,30");
videoInfo=SDL_GetVideoInfo();
wnd=SDL_SetVideoMode(videoInfo->current_w-20,videoInfo->current_h-70,WNDP,SDL_HWSURFACE|SDL_DOUBLEBUF);
SDL_WM_SetCaption("Test fmod+stb_image+sdl",NULL);
// Init fmod
FMOD_System_Create(&system);
FMOD_System_Init(system,100,FMOD_INIT_NORMAL,NULL);
FMOD_System_CreateSound(system,"music.mp3",FMOD_SOFTWARE|FMOD_2D|FMOD_CREATESTREAM|FMOD_OPENONLY,0,&sound);
FMOD_Sound_GetNumTags(sound,&numtags,NULL);
if(numtags) {
if(FMOD_Sound_GetTag(sound,"APIC",0,&tag)!=FMOD_ERR_TAGNOTFOUND) {
char *td=tag.data;
stbi_uc *img; // Well, stbi_uc is just an unsigned char
int x,y,comp,req_comp=STBI_rgb;
while(*(++td)); // Skip MIME msg
++td; // Skip content type
while(*(++td)); // Skip description
td++; // td is now pointing to the first byte of jpeg img
// Deflate and display jpg
img=stbi_load_from_memory((stbi_uc *)td,tag.datalen-(td-(char *)tag.data),&x,&y,&comp,req_comp);
displayImg(wnd,x,y,comp,img);
free(img);
}
else {} // Traitement si tag absent
}
else {} // Traitement si no tag
FMOD_Sound_Release(sound);
FMOD_System_Close(system);
FMOD_System_Release(system);
SDL_Quit();
return(0);
} |
Partager