SDL simple example

It can't get much simpler than this. I did notice one odd thing, when I tried to create errs to test my case error handling, SDL_Init(value), doesn't give a rat's derriere what value is passed. I mixed the code up a little to make it look pretty here and so if you copy it, be warned it isn't some file.c that I have compiled. It is just reference to say, "this is pretty simple" and this much code is enough to make a sound play. I got most of this from Grubby Games and then tested it, modified it, compiled it, and now I am going to use the information gained to create my own application that is a bit more elegant. So thanks to them at Grubby Games, as their code actually works and that is not always true for some sites. I also got information from the SDL wiki, a German SDL site and many others. I like to investigate the breadth and depth of the issues and this looks like the way to go for sound. I could do all the stuff in buffers and then send it to dsp, but I don't want to reinvent fire today, maybe next week.

#include <stdlib.h> #include <stdio.h> #include "SDL/SDL.h" #include "SDL/SDL_audio.h" #include "SDL/SDL_mixer.h" #include "SDL/SDL_sound.h" int errPrint(int errno, char *namish); /*compile with [gcc -Wall sdlMix.c -lSDL -lSDL_mixer] Of course you would need several DEV headers and the libraries themselves, but this isn't a kindergarten Linux or C example.*/ int main(int argc,char * argv[]) { int audio_rate = 22050;Uint16 audio_format = AUDIO_S16SYS; int audio_channels = 2;int audio_buffers = 4096;int channel; char playName[]="res/applause.wav"; if (SDL_Init( SDL_INIT_AUDIO) != 0) {errPrint(1,playName)} if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers) != 0) {errPrint(2,playName);} Mix_Chunk *sound = NULL; sound = Mix_LoadWAV(playName); if(sound == NULL) {errPrint(3,playName);} channel = Mix_PlayChannel(-1, sound, 0); if(channel == -1) {errPrint(4,playName);} while(Mix_Playing(channel) != 0); Mix_FreeChunk(sound); Mix_CloseAudio(); SDL_Quit();return 0;} int errPrint(int errno, char *namish) { char errString[200]; switch (errno) { case 1: sprintf(errString, "Unable to initialize SDL: [%s]\n", Mix_GetError());break; case 2: sprintf(errString, "Unable to initialize audio: [%s]\n", Mix_GetError());break; case 3: sprintf(errString, "Unable to load WAV file: %s SDL err = [ %s ]\n",namish, Mix_GetError()); break; case 4: sprintf(errString, "Unable to play WAV file: %s SDL err = [%s]\n", Mix_GetError());break; default : sprintf(errString, "Unable to even make a simple err display work! DOH! \n");break;} fprintf(stderr,errString); exit(1);return 0;}

0 comments:

Contributors

Automated Intelligence

Automated Intelligence
Auftrag der unendlichen LOL katzen