From 6d924e73db507826acbfd6b3015dee27a93d1f26 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Mon, 22 Feb 2010 06:02:44 +0000 Subject: [PATCH] working on audio support git-svn-id: svn://anubis/anaglym/trunk@257 99a6e188-d820-4881-8870-2d33a10e2619 --- Engine.cc | 3 +++ Makefile | 2 +- Video.cc | 34 ++++++++++++++++++++++++++++++++++ Video.h | 1 + 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/Engine.cc b/Engine.cc index 33c0119..0117ac4 100644 --- a/Engine.cc +++ b/Engine.cc @@ -888,6 +888,9 @@ void Engine::run() case SDLK_F5: reloadProgram(); break; + case SDLK_F6: + SDL_PauseAudio(0); + break; default: key_down_event(event.key.keysym.sym); } diff --git a/Makefile b/Makefile index 7e8a82e..1970eb6 100644 --- a/Makefile +++ b/Makefile @@ -28,7 +28,7 @@ FTGLINCLUDE := $(shell pkg-config --cflags ftgl) FTGLLIBS := $(shell pkg-config --libs ftgl) SDLINCLUDE := $(shell sdl-config --cflags) -SDLLIBS := $(shell sdl-config --libs) -lSDL_image +SDLLIBS := $(shell sdl-config --libs) -lSDL_image -lSDL_sound ODEINCLUDE := $(shell ode-config --cflags) ODELIBS := $(shell ode-config --libs) diff --git a/Video.cc b/Video.cc index d8f67ac..d997bce 100644 --- a/Video.cc +++ b/Video.cc @@ -4,8 +4,22 @@ #include #include #include +#include +#include using namespace std; +static void Video_sound_callback(void * userdata, Uint8 * stream, int len) +{ + static int pos = 0; + Sint16 * str = (Sint16 *) stream; + for (int i = 0; i < len / 4; i++) + { + str[i*2] = (Sint16) (32000.0 * sin(pos / 44100.0 * M_PI)); + str[i*2+1] = 0; + pos++; + } +} + Video::Video() { if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER)) @@ -30,6 +44,26 @@ Video::Video() m_surface = NULL; m_width = 0; m_height = 0; + +// Sound_Init(); + SDL_AudioSpec desired; + desired.freq = 44100; + desired.format = AUDIO_S16SYS; + desired.channels = 2; + desired.samples = 4096; + desired.callback = Video_sound_callback; + desired.userdata = this; + if (SDL_OpenAudio(&desired, NULL) < 0) + { + cerr << "Sound_OpenAudio() error." << endl; + exit(3); + } +} + +Video::~Video() +{ + Sound_Quit(); + SDL_Quit(); } void Video::start(int width, int height, bool fullscreen, bool grab_input, diff --git a/Video.h b/Video.h index 0efc275..35172cf 100644 --- a/Video.h +++ b/Video.h @@ -8,6 +8,7 @@ class Video { public: Video(); + ~Video(); void start(int width = 0, int height = 0, bool fullscreen = true, bool grab_input = true, int samples = 4);