246 lines
5.5 KiB
C++
246 lines
5.5 KiB
C++
|
|
#include <GL/gl.h>
|
|
#include <GL/glu.h>
|
|
#include <SDL.h>
|
|
#include <SDL_sound.h>
|
|
#include <math.h>
|
|
#include <limits.h>
|
|
|
|
#include <iostream>
|
|
|
|
#include "AV.h"
|
|
|
|
using namespace std;
|
|
|
|
#define SOUND_RATE 44100
|
|
#define CALLBACK_SAMPLES 4096
|
|
#define CHANNELS 2
|
|
#define BYTES_PER_SAMPLE 2
|
|
|
|
void AV_sound_callback(void * userdata, Uint8 * stream, int len)
|
|
{
|
|
((AV *)userdata)->playCallback(stream, len);
|
|
}
|
|
|
|
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 = SOUND_RATE;
|
|
desired.format = AUDIO_S16SYS;
|
|
desired.channels = CHANNELS;
|
|
desired.samples = CALLBACK_SAMPLES;
|
|
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;
|
|
}
|
|
}
|
|
|
|
refptr<AV::Sound> AV::createSound()
|
|
{
|
|
refptr<Sound> s = new Sound(*this);
|
|
#if 0
|
|
m_sounds.insert(s);
|
|
#endif
|
|
return s;
|
|
}
|
|
|
|
void AV::playSound(AV::Sound * s)
|
|
{
|
|
m_active_sounds.insert(s);
|
|
SDL_PauseAudio(0);
|
|
}
|
|
|
|
void AV::playCallback(Uint8 * stream, int len)
|
|
{
|
|
memset(stream, 0, len);
|
|
for (std::set<Sound *>::const_iterator it = m_active_sounds.begin();
|
|
it != m_active_sounds.end();
|
|
it++)
|
|
{
|
|
if ((*it)->decode(stream, len) == 0)
|
|
{
|
|
m_active_sounds.erase(it);
|
|
}
|
|
}
|
|
if (m_active_sounds.size() == 0)
|
|
{
|
|
SDL_PauseAudio(1);
|
|
}
|
|
}
|
|
|
|
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 = SOUND_RATE;
|
|
m_ss = Sound_NewSample(m_rwops, NULL, &desired,
|
|
CALLBACK_SAMPLES * BYTES_PER_SAMPLE * CHANNELS);
|
|
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;
|
|
}
|
|
|
|
void AV::Sound::play()
|
|
{
|
|
if (m_ss != NULL)
|
|
{
|
|
#if 0
|
|
cerr << "play()" << endl;
|
|
Sound_Rewind(m_ss);
|
|
cerr << "end play()" << endl;
|
|
#endif
|
|
m_av.playSound(this);
|
|
}
|
|
}
|
|
|
|
void AV::Sound::stop()
|
|
{
|
|
}
|
|
|
|
void AV::Sound::loop(int times)
|
|
{
|
|
}
|
|
|
|
int AV::Sound::decode(Uint8 * stream, int len)
|
|
{
|
|
int len_decoded = 0, len_decoded_now;
|
|
int samples_decoded = 0, samples_decoded_now;
|
|
int spos = 0;
|
|
int16_t * s16stream = (int16_t *) stream;
|
|
int16_t * decoded_stream = (int16_t *) m_ss->buffer;
|
|
do
|
|
{
|
|
len_decoded_now = Sound_Decode(m_ss);
|
|
samples_decoded_now = len_decoded_now / BYTES_PER_SAMPLE;
|
|
len_decoded += len_decoded_now;
|
|
samples_decoded += len_decoded / BYTES_PER_SAMPLE;
|
|
for (int i = 0; i < samples_decoded_now; i++)
|
|
{
|
|
int val = s16stream[spos] + decoded_stream[i];
|
|
s16stream[spos] = (val > SHRT_MAX) ? SHRT_MAX : val;
|
|
spos++;
|
|
}
|
|
} while ( (len_decoded_now == (int) m_ss->buffer_size)
|
|
&& (len_decoded < len) );
|
|
|
|
return len_decoded;
|
|
}
|