anaglym/AV.cc
Josh Holtrop 5fbc931c39 added IDSet template; filled out AV::Sound::load(); minor AV updates
git-svn-id: svn://anubis/anaglym/trunk@262 99a6e188-d820-4881-8870-2d33a10e2619
2010-02-24 20:08:02 +00:00

164 lines
3.8 KiB
C++

#include "AV.h"
#include <GL/gl.h>
#include <GL/glu.h>
#include <SDL.h>
#include <iostream>
#include <SDL_sound.h>
#include <math.h>
using namespace std;
static void AV_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 * 263.0 / AV_SOUND_RATE * 2.0 * M_PI));
str[i*2+1] = 0;
pos++;
}
}
AV::AV()
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER))
{
cerr << "SDL_Init() returned error!" << endl;
exit(3);
}
const SDL_VideoInfo * vidInfo = SDL_GetVideoInfo();
if (vidInfo != NULL)
{
m_defaultWidth = vidInfo->current_w;
m_defaultHeight = vidInfo->current_h;
}
else
{
cerr << "Warning: SDL_GetVideoInfo() returned NULL!" << endl;
m_defaultWidth = 1024;
m_defaultHeight = 768;
}
m_surface = NULL;
m_width = 0;
m_height = 0;
Sound_Init();
SDL_AudioSpec desired;
desired.freq = AV_SOUND_RATE;
desired.format = AUDIO_S16SYS;
desired.channels = 2;
desired.samples = 4096;
desired.callback = AV_sound_callback;
desired.userdata = this;
if (SDL_OpenAudio(&desired, NULL) < 0)
{
cerr << "SDL_OpenAudio() error." << endl;
exit(3);
}
}
AV::~AV()
{
SDL_CloseAudio();
Sound_Quit();
SDL_Quit();
}
void AV::start(int width, int height, bool fullscreen, bool grab_input,
int samples)
{
if (m_surface == NULL)
{
if (width == 0)
width = m_defaultWidth;
if (height == 0)
height = m_defaultHeight;
int flags = SDL_HWSURFACE | SDL_OPENGL;
if (fullscreen)
flags |= SDL_FULLSCREEN;
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
if (samples > 1)
{
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, samples);
}
m_surface = SDL_SetVideoMode(width, height, 32, flags);
if (grab_input)
{
SDL_ShowCursor(SDL_DISABLE);
SDL_WM_GrabInput(SDL_GRAB_ON);
}
SDL_WM_SetCaption("Anaglym", "Anaglym");
m_fullscreen = fullscreen;
m_width = width;
m_height = height;
glEnable(GL_DEPTH_TEST);
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glShadeModel(GL_SMOOTH);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(60.0, (double) width / (double) height, 0.01, 10000.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
}
void AV::stop()
{
if (m_surface != NULL)
{
SDL_FreeSurface(m_surface);
m_surface = NULL;
}
}
AV::Sound::Sound(AV & av)
: m_av(av)
{
m_rwops = NULL;
m_ss = NULL;
}
AV::Sound::~Sound()
{
if (m_ss != NULL)
{
Sound_FreeSample(m_ss);
}
}
bool AV::Sound::load(FileLoader & fileLoader, const FileLoader::Path & path)
{
FileLoader::Buffer buff = fileLoader.load(path);
if (buff.size > 0)
{
m_rwops = SDL_RWFromMem(buff.data, buff.size);
Sound_AudioInfo desired;
desired.channels = 2;
desired.format = AUDIO_S16SYS;
desired.rate = AV_SOUND_RATE;
m_ss = Sound_NewSample(m_rwops, NULL, &desired, 4192);
if (m_ss != NULL)
{
return true;
}
else
{
SDL_FreeRW(m_rwops);
m_rwops = NULL;
cerr << "Error loading sound " << path.toString() << ": "
<< Sound_GetError() << endl;
}
}
return false;
}