|
发表于 2006-8-7 22:53:19
|
显示全部楼层
背景音乐部分做好了,稍后会放出
[code:1]#include "stdlib.h"
#include "SDL/SDL.h"
// *** IF USING XCODE ON MACOS X, YOU MAY NEED TO CHANGE THE FOLLOWING LINE TO: #include "SDL_mixer/SDL_mixer.h"
#include "SDL/SDL_mixer.h"
#include <SDL_PlayMusic.h>
int musicPlaying = 0;
int musicInit=0;
char *music_file[MUSIC_NUM]={"01.ogg","02.ogg","03.ogg"};
Mix_Music *music[3];
int music_init()
{
//Pointer to our music, in memory
int i;
int audio_rate = 22050; //Frequency of audio playback
Uint16 audio_format = AUDIO_S16SYS; //Format of the audio we're playing
int audio_channels = 2; //2 channels = stereo
int audio_buffers = 4096; //Size of the audio buffers in memory
//Initialize SDL audio
if (SDL_Init(SDL_INIT_AUDIO) != 0)
{
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
//Initialize SDL_mixer with our chosen audio settings
if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0)
{
printf("Unable to initialize audio: %s\n", Mix_GetError());
return 1;
}
//Load our OGG file from disk
for(i=0;i<MUSIC_NUM;i++)
{
music[i] = Mix_LoadMUS(music_file[i]);
if(music[i] == NULL)
printf("Unable to load OGG file: %s\n", Mix_GetError());
}
musicInit=1;
return 0;
}
void music_close()
{
int i;
//Release the memory allocated to our music
Mix_HaltMusic();
for(i=0;i<MUSIC_NUM;i++)
Mix_FreeMusic(music[i]);
//Need to make sure that SDL_mixer and SDL have a chance to clean up
Mix_CloseAudio();
SDL_Quit();
musicInit=0;
//Return success!
}
int music_playing()
//Is the music playing, or not?
{
return musicPlaying;
}
int music_play(int i)
{
//int i;
/*if(i<0||i>=MUSIC_NUM)
{
printf("error for num%d\n",i);
return -1;
}*/
if(musicInit==0)
{
if(music_init()!=0)
return -1;
}
for(i=0;i<MUSIC_NUM;i++)
{
//Play that funky music!
if(Mix_PlayMusic(music[i], 0) == -1)
{
printf("Unable to play OGG file: %s\n", Mix_GetError());
return 1;
}
//The music is playing!
musicPlaying = 1;
//Make sure that the musicFinished() function is called when the music stops playing
Mix_HookMusicFinished(musicFinished);
//Wait for the music to stop
while(musicPlaying)
{
//Do nothing for a bit
SDL_Delay(100);
}
if(i==MUSIC_NUM-1) i=0;
}
return 0;
}
void musicFinished()
{
//Music is done!
musicPlaying = 0;
}
/*
int main()
{
music_init();
music_play(0);
music_close();
return 0;
}*/
[/code:1]
这个是SDL_PlayMusic.c
直接参考了SDL自己的示范代码和楼上那位兄弟的代码。
Play_sound这一部分没有动,想看看大家对SDL的看法。 |
|