added IDSet template; filled out AV::Sound::load(); minor AV updates

git-svn-id: svn://anubis/anaglym/trunk@262 99a6e188-d820-4881-8870-2d33a10e2619
This commit is contained in:
Josh Holtrop 2010-02-24 20:08:02 +00:00
parent 11680bf3d7
commit 5fbc931c39
5 changed files with 82 additions and 16 deletions

36
AV.cc
View File

@ -14,7 +14,7 @@ static void AV_sound_callback(void * userdata, Uint8 * stream, int len)
Sint16 * str = (Sint16 *) stream;
for (int i = 0; i < len / 4; i++)
{
str[i*2] = (Sint16) (32000.0 * sin(pos * 263.0 / 44100.0 * 2.0 * M_PI));
str[i*2] = (Sint16) (32000.0 * sin(pos * 263.0 / AV_SOUND_RATE * 2.0 * M_PI));
str[i*2+1] = 0;
pos++;
}
@ -45,9 +45,9 @@ AV::AV()
m_width = 0;
m_height = 0;
// Sound_Init();
Sound_Init();
SDL_AudioSpec desired;
desired.freq = 44100;
desired.freq = AV_SOUND_RATE;
desired.format = AUDIO_S16SYS;
desired.channels = 2;
desired.samples = 4096;
@ -55,13 +55,14 @@ AV::AV()
desired.userdata = this;
if (SDL_OpenAudio(&desired, NULL) < 0)
{
cerr << "Sound_OpenAudio() error." << endl;
cerr << "SDL_OpenAudio() error." << endl;
exit(3);
}
}
AV::~AV()
{
SDL_CloseAudio();
Sound_Quit();
SDL_Quit();
}
@ -120,9 +121,19 @@ void AV::stop()
}
}
AV::Sound::Sound()
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)
@ -131,7 +142,22 @@ bool AV::Sound::load(FileLoader & fileLoader, const FileLoader::Path & 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;
}

10
AV.h
View File

@ -3,7 +3,11 @@
#define VIDEO_H
#include "FileLoader/FileLoader.h"
#include "refptr/refptr.h"
#include <SDL.h>
#include <SDL_sound.h>
#define AV_SOUND_RATE 44100
class AV
{
@ -11,11 +15,14 @@ class AV
class Sound
{
public:
Sound();
Sound(AV & av);
~Sound();
bool load(FileLoader & fileLoader,
const FileLoader::Path & path);
protected:
AV & m_av;
SDL_RWops * m_rwops;
Sound_Sample * m_ss;
};
AV();
@ -46,6 +53,7 @@ class AV
{
return SDL_WM_GrabInput(SDL_GRAB_QUERY) == SDL_GRAB_ON;
}
refptr<Sound> createSound() { return new Sound(*this); }
protected:
int m_defaultWidth;

View File

@ -89,7 +89,6 @@ Engine::Engine(const string & path, AV & av)
: m_av(av)
{
m_next_object_index = 1;
m_next_sound_index = 1;
m_next_joint_index = 1;
m_eye[0] = 0;
m_eye[1] = -1;
@ -298,11 +297,9 @@ int Engine::addObject(bool is_static, bool is_reference,
return id;
}
int Engine::addSound(AV::Sound * avs)
int Engine::addSound(refptr<AV::Sound> avs)
{
int id = m_next_sound_index++;
m_sounds[id] = avs;
return id;
return m_sounds.add(avs);
}
int Engine::addAMotor(Object * o1, Object * o2)
@ -578,7 +575,7 @@ int Engine::loadSound(const string & name)
{
FileLoader::Path path("", name);
AV::Sound * avs = new AV::Sound();
refptr<AV::Sound> avs = m_av.createSound();
if (avs->load(*m_fileLoader, path))
{

View File

@ -15,6 +15,7 @@
#include "refptr/refptr.h"
#include "ftgl.h"
#include "PhyObj/PhyObj.h"
#include "IDSet.h"
class Engine
{
@ -148,7 +149,7 @@ class Engine
int addObject(bool is_static, bool is_reference,
OdeWorld::GeomType geom_type,
refptr< std::vector<float> > args);
int addSound(AV::Sound * avs);
int addSound(refptr<AV::Sound> avs);
int addAMotor(Object * o1, Object * o2);
int addHinge(Object * o1, Object * o2,
dReal anchor_x, dReal anchor_y, dReal anchor_z,
@ -264,10 +265,9 @@ class Engine
std::string m_engine_path;
OdeWorld m_world;
std::map<int, Object *> m_objects;
std::map<int, AV::Sound *> m_sounds;
IDSet< refptr<AV::Sound> > m_sounds;
std::map<int, dJointID> m_joints;
int m_next_object_index;
int m_next_sound_index;
int m_next_joint_index;
GLdouble m_eye[3];
GLdouble m_center[3];

35
IDSet.h Normal file
View File

@ -0,0 +1,35 @@
#ifndef IDSET_H
#define IDSET_H
#include <map>
template <class T>
class IDSet
{
public:
IDSet() { m_next_index = 1; }
int add(const T & o)
{
int id = m_next_index++;
m_data[id] = o;
return id;
}
void remove(int id)
{
m_data.erase(id);
}
T & operator[](int id)
{
return m_data[id];
}
bool contains(int id)
{
return m_data.find(id) != m_data.end();
}
protected:
std::map<int, T> m_data;
int m_next_index;
};
#endif