//focus on sdl
//random noise
//fosdl5_1.cpp
//16MAY2002
//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>
#include <stdio.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;
//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
SDL_Window* screen = SDL_CreateWindow("test", 0, 0, 480, 640, NULL);
//error check
if (screen == 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;
//fill in desired audio spec
g_SpecDesired->freq = 22050;
g_SpecDesired->format = AUDIO_S16LSB;
g_SpecDesired->channels = 1;
g_SpecDesired->samples = 8192;
g_SpecDesired->callback = FOSDLAudioCallback;
g_SpecDesired->userdata = NULL;
//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;
//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)
{
//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;
//return 0
return(0);
}
//audio callback
void FOSDLAudioCallback(void* userdata, Uint8* buffer, int len)
{
//loop variable
int index;
for (index = 0; index < len; index++)
{
//fill buffer with random bytes
buffer[index] = rand() % 256;
}
}
unable to play sound plz help