I am having trouble converting this code to work on SDL2.
//focus on sdl
//wav loading
//fosdl5_2.cpp
//17MAY2002
//ernest pazera
//requires static linkage to:
//sdl.lib, sdlmain.lib
//requires dynamic linkage to:
//sdl.dll
//include SDL stuff
#include "sdl.h"
//include ability to exit program
#include <stdlib.h>
//memory management functions
#include <memory.h>
//screen dimensions
const int SCREEN_WIDTH=640;
const int SCREEN_HEIGHT=480;
//display surface
SDL_Surface* g_pDisplaySurface = NULL;
//event structure
SDL_Event g_Event;
//audio specs
SDL_AudioSpec* g_SpecDesired;
SDL_AudioSpec* g_SpecObtained;
SDL_AudioSpec* g_SpecWAV;
//type for streaming wav data to sound buffer
typedef struct
{
//pointer to current sound's data
Uint8* m_CurrentSound;
//length of current sound
int m_CurrentSoundLength;
//pointer to next sound's data
Uint8* m_NextSound;
//length of next sound
int m_NextSoundLength;
} FOSDL_AudioStream;
//audio stream variable
FOSDL_AudioStream g_AudioStream;
//wave data
Uint8* g_WAVBuffer;
Uint32 g_WAVLength;
//prototype for audio callback function
void FOSDLAudioCallback(void* userdata,Uint8* buffer,int len);
//main function
int main(int argc, char* argv[])
{
//initialize SDL
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO)==-1)
{
//error initializing SDL
//report the error
fprintf(stderr,"Could not initialize SDL!\n");
//end the program
exit(1);
}
else
{
//SDL initialized
//report success
fprintf(stdout,"SDL initialized properly!\n");
//set up to uninitialize SDL at exit
atexit(SDL_Quit);
}
//create windowed environment
g_pDisplaySurface = SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,0,SDL_ANYFORMAT);
//error check
if (g_pDisplaySurface == NULL)
{
//report error
fprintf(stderr,"Could not set up display surface!\n");
//exit the program
exit(1);
}
//allocate desired and obtained audio specs
g_SpecDesired=new SDL_AudioSpec;
g_SpecObtained=new SDL_AudioSpec;
g_SpecWAV=new SDL_AudioSpec;
//set up the audio stream to point to nothing
g_AudioStream.m_CurrentSound=NULL;
g_AudioStream.m_CurrentSoundLength=0;
g_AudioStream.m_NextSound=NULL;
g_AudioStream.m_NextSoundLength=0;
//fill in desired audio spec
g_SpecDesired->freq=11025;
g_SpecDesired->format=AUDIO_U8;
g_SpecDesired->channels=1;
g_SpecDesired->samples=8192;
g_SpecDesired->callback=FOSDLAudioCallback;
g_SpecDesired->userdata=(void*)&g_AudioStream;
//open the audio device
if(SDL_OpenAudio(g_SpecDesired,g_SpecObtained)<0)
{
//error
fprintf(stderr,"Could not open audio device!\n");
delete g_SpecDesired;
delete g_SpecObtained;
//exit the program
exit(1);
}
//free the desired spec
delete g_SpecDesired;
//load wav file
g_SpecWAV=SDL_LoadWAV("song.wav",g_SpecWAV,&g_WAVBuffer,&g_WAVLength);
//lock the audio buffer
SDL_LockAudio();
//set current sound to WAV data
g_AudioStream.m_CurrentSound=g_WAVBuffer;
g_AudioStream.m_CurrentSoundLength=g_WAVLength;
//unlock the audio buffer
SDL_UnlockAudio();
//turn off the paused state of the sound buffer
SDL_PauseAudio(0);
//repeat forever
for(;;)
{
//wait for an event
if(SDL_PollEvent(&g_Event)==0)
{
//lock audio
SDL_LockAudio();
//check if next sound is NULL
if(g_AudioStream.m_NextSound==NULL)
{
//set next sound
g_AudioStream.m_NextSound=g_WAVBuffer;
g_AudioStream.m_NextSoundLength=g_WAVLength;
}
//unlock audio
SDL_UnlockAudio();
//update the screen
SDL_UpdateRect(g_pDisplaySurface,0,0,0,0);
}
else
{
//event occurred, check for quit
if(g_Event.type==SDL_QUIT) break;
}
}
//normal termination
fprintf(stdout,"Terminating normally.\n");
//close audio
SDL_CloseAudio();
//free the obtained spec
delete g_SpecObtained;
//free the wave spec
delete g_SpecWAV;
//free wav data
SDL_FreeWAV(g_WAVBuffer);
//return 0
return(0);
}
//audio callback
void FOSDLAudioCallback(void* userdata,Uint8* buffer,int len)
{
//cast user data to stream data
FOSDL_AudioStream* pstrm;
pstrm=(FOSDL_AudioStream*)userdata;
//continue while len > 0 and at least one sound is non-empty
while(len>0 && (pstrm->m_CurrentSoundLength>0 || pstrm->m_NextSoundLength>0))
{
//check for current sound being NULL
if(pstrm->m_CurrentSoundLength==0)
{
//copy next sound to current sound
pstrm->m_CurrentSound=pstrm->m_NextSound;
pstrm->m_CurrentSoundLength=pstrm->m_NextSoundLength;
//clear next sound
pstrm->m_NextSound=NULL;
pstrm->m_NextSoundLength=0;
}
//while len>0 and length of current sound>0, stream to buffer
while(len>0 && pstrm->m_CurrentSoundLength>0)
{
//stream a byte to audio buffer
*(buffer++)=*(pstrm->m_CurrentSound++);
//decrease lengths
len--;
pstrm->m_CurrentSoundLength--;
}
}
}