break; case sdl_quit: { printf("quit requested, quitting.\n"); exit(0); } break; } } /* this should never happen */ printf("sdl_waitevent error: %s\n", sdl_geterror()); exit(1); }
打开音频设备
sdl_audiospec wanted;
extern void fill_audio(void *udata, uint8 *stream, int len);
/* set the audio format */
wanted.freq = 22050;
wanted.format = audio_s16;
wanted.channels = 2; /* 1 = mono, 2 = stereo */
wanted.samples = 1024; /* good low-latency value for callback */
wanted.callback = fill_audio;
wanted.userdata = null;
/* open the audio device, forcing the desired format */
if ( sdl_openaudio(&wanted, null) < 0 ) {
fprintf(stderr, "couldn't open audio: %s\n", sdl_geterror());
return(-1);
}
return(0);
播放音频
static uint8 *audio_chunk;
static uint32 audio_len;
static uint8 *audio_pos;
/* the audio function callback takes the following parameters:
stream: a pointer to the audio buffer to be filled
len: the length (in bytes) of the audio buffer
*/
void fill_audio(void *udata, uint8 *stream, int len)
{
/* only play if we have data left */
if ( audio_len == 0 )
return;
/* mix as much data as possible */
len = ( len > audio_len ? audio_len : len );
sdl_mixaudio(stream, audio_pos, len, sdl_mix_maxvolume)
audio_pos += len;
audio_len -= len;
}
/* load the audio data ... */
;;;;;
audio_pos = audio_chunk;
/* let the callback function play the audio chunk */
sdl_pauseaudio(0);
/* do some processing */
;;;;;
/* wait for sound to complete */
while ( audio_len > 0 ) {
sdl_delay(100); /* sleep 1/10 second */
}
sdl_closeaudio();
列出所有cdrom
#include "sdl.h"
/* initialize sdl first */
if ( sdl_init(sdl_init_cdrom) < 0 ) {
fprintf(stderr, "couldn't initialize sdl: %s\n",sdl_geterror());
exit(1);
}
atexit(sdl_quit);
/* find out how many cd-rom drives are connected to the system */
printf("drives available: %d\n", sdl_cdnumdrives());
for ( i=0; i<sdl_cdnumdrives(); ++i ) {
printf("drive %d: \"%s\"\n", i, sdl_cdname(i));
}
打开缺省cdrom驱动器
sdl_cd *cdrom;
cdstatus status;
char *status_str;
cdrom = sdl_cdopen(0);
if ( cdrom == null ) {
fprintf(stderr, "couldn't open default cd-rom drive: %s\n",
sdl_geterror());
exit(2);
}
status = sdl_cdstatus(cdrom);
switch (status) {
case cd_trayempty:
status_str = "tray empty";
break;
case cd_stopped:
status_str = "stopped";
break;
case cd_playing:
status_str = "playing";
break;
case cd_paused:
status_str = "paused";
break;
case cd_error:
status_str = "error state";
break;
}
printf("drive status: %s\n", status_str);
if ( status >= cd_playing ) {
int m, s, f;
frames_to_msf(cdrom->cur_frame, &m, &s, &f);
printf("currently playing track %d, %d:%2.2d\n",
cdrom->track[cdrom->cur_track].id, m, s);
}
列出cd上所有音轨
sdl_cd *cdrom; /* assuming this has already been set.. */
int i;
int m, s, f;
sdl_cdstatus(cdrom);
printf("drive tracks: %d\n", cdrom->numtracks);
for ( i=0; i<cdrom->numtracks; ++i ) {
frames_to_msf(cdrom->track[i].length, &m, &s, &f);
if ( f > 0 )
++s;
printf("\ttrack (index %d) %d: %d:%2.2d\n", i,
cdrom->track[i].id, m, s);
}
播放cd
sdl_cd *cdrom; /* assuming this has already been set.. */
// play entire cd:
if ( cd_indrive(sdl_cdstatus(cdrom)) )
sdl_cdplaytracks(cdrom, 0, 0, 0, 0);
// play last track:
if ( cd_indrive(sdl_cdstatus(cdrom)) ) {
sdl_cdplaytracks(cdrom, cdrom->numtracks-1, 0, 0, 0);
}
// play first and second track and 10 seconds of third track:
if ( cd_indrive(sdl_cdstatus(cdrom)) )
sdl_cdplaytracks(cdrom, 0, 0, 2, 10);
基于时间的游戏主循环
#define tick_interval 30
uint32 timeleft(void)
{
static uint32 next_time = 0;
uint32 now;
now = sdl_getticks();
if ( next_time <= now ) {
next_time = now+tick_interval;
return(0);
}
return(next_time-now);
}
/* main game loop
while ( game_running ) {
updategamestate();