cpp_photon/test/Audio_test.cpp

224 lines
5.7 KiB
C++
Raw Normal View History

2005-05-15 02:50:07 +00:00
//This file is part of Photon (http://photon.sourceforge.net)
//Copyright (C) 2004-2005 James Turk
//
// Author:
// James Turk (jpt2433@rit.edu)
//
// Version:
2005-07-18 05:14:18 +00:00
// $Id: Audio_test.cpp,v 1.3 2005/07/18 05:14:19 cozman Exp $
2005-05-15 02:50:07 +00:00
#include "photon.hpp"
using namespace photon;
2005-07-05 06:44:55 +00:00
#include <boost/lexical_cast.hpp>
2005-07-04 03:06:48 +00:00
#ifdef PHOTON_USE_OPENAL
2005-05-15 02:50:07 +00:00
using namespace photon::audio;
2005-07-05 06:44:55 +00:00
class MainTask : public Task
{
public:
MainTask() :
Task("MainTask"),
app(AppCore::getInstance()),
video(video::VideoCore::getInstance())
{
LogSinkPtr csp( new ConsoleSink("console") );
log.addSink(csp);
video.setOrthoView(800,600);
2005-07-17 07:14:09 +00:00
video::Font::addResource("font","data/FreeMono.ttf",20);
font.open("font");
Sample::addResource("chimes","data/chimes.wav");
Sample::addResource("ocean","data/ocean.wav");
Sample::addResource("rain","data/rain.wav");
Sample::addResource("stream","data/stream.wav");
Sample::addResource("thunder","data/thunder.wav");
Sample::addResource("waterdrop","data/waterdrop.wav");
chimes.open("chimes");
ocean.open("ocean");
rain.open("rain");
stream.open("stream");
thunder.open("thunder");
waterdrop.open("waterdrop");
chimes.setLooping(true);
ocean.setLooping(true);
rain.setLooping(true);
stream.setLooping(true);
thunder.setLooping(true);
2005-07-18 05:14:18 +00:00
assert(!waterdrop.isLooping());
2005-07-17 07:14:09 +00:00
waterdrop.setLooping(true);
2005-07-18 05:14:18 +00:00
assert(waterdrop.isLooping());
2005-07-17 07:14:09 +00:00
for(int i=0; i < 6; ++i)
status[i] = "NOT ";
}
void checkKeys()
{
static const std::string NOT_PLAYING = "NOT ";
static scalar lastCheck = 0;
if(app.getTime() - lastCheck > 0.1)
{
lastCheck = app.getTime();
if(app.keyPressed(KEY_C))
{
2005-07-18 05:14:18 +00:00
if(!chimes.isPlaying())
2005-07-17 07:14:09 +00:00
{
chimes.play();
status[0] = "";
}
else
{
chimes.stop();
status[0] = NOT_PLAYING;
}
}
if(app.keyPressed(KEY_O))
{
2005-07-18 05:14:18 +00:00
if(!ocean.isPlaying())
2005-07-17 07:14:09 +00:00
{
ocean.play();
status[1] = "";
}
else
{
ocean.stop();
status[1] = NOT_PLAYING;
}
}
if(app.keyPressed(KEY_R))
{
2005-07-18 05:14:18 +00:00
if(!rain.isPlaying())
2005-07-17 07:14:09 +00:00
{
rain.play();
status[2] = "";
}
else
{
rain.stop();
status[2] = NOT_PLAYING;
}
}
if(app.keyPressed(KEY_S))
{
2005-07-18 05:14:18 +00:00
if(!stream.isPlaying())
2005-07-17 07:14:09 +00:00
{
stream.play();
status[3] = "";
}
else
{
stream.stop();
status[3] = NOT_PLAYING;
}
}
if(app.keyPressed(KEY_T))
{
2005-07-18 05:14:18 +00:00
if(!thunder.isPlaying())
2005-07-17 07:14:09 +00:00
{
thunder.play();
status[4] = "";
}
else
{
thunder.stop();
status[4] = NOT_PLAYING;
}
}
if(app.keyPressed(KEY_W))
{
2005-07-18 05:14:18 +00:00
if(!waterdrop.isPlaying())
2005-07-17 07:14:09 +00:00
{
waterdrop.play();
status[5] = "";
}
else
{
waterdrop.stop();
status[5] = NOT_PLAYING;
}
}
}
2005-07-05 06:44:55 +00:00
}
void update()
2005-07-17 07:14:09 +00:00
{
static const photon::uint fontHeight(font.getHeight());
2005-07-05 06:44:55 +00:00
static double t=0;
2005-07-17 07:14:09 +00:00
2005-07-05 06:44:55 +00:00
if(app.getTime() - t > 1.0)
{
app.setTitle("FPS: " +
boost::lexical_cast<std::string>(app.getFramerate()) );
t = app.getTime();
}
2005-07-17 07:14:09 +00:00
checkKeys();
2005-07-05 06:44:55 +00:00
video.clear();
2005-07-17 07:14:09 +00:00
font.beginDraw(0, 0*fontHeight) << "(C)himes is " << status[0] <<
"playing" << font.endDraw();
font.beginDraw(0, 1*fontHeight) << "(O)cean is " << status[1] <<
"playing" << font.endDraw();
font.beginDraw(0, 2*fontHeight) << "(R)ain is " << status[2] <<
"playing" << font.endDraw();
font.beginDraw(0, 3*fontHeight) << "(S)tream is " << status[3] <<
"playing" << font.endDraw();
font.beginDraw(0, 4*fontHeight) << "(T)hunder is " << status[4] <<
"playing" << font.endDraw();
font.beginDraw(0, 5*fontHeight) << "(W)aterdrop is " << status[5] <<
"playing" << font.endDraw();
2005-07-05 06:44:55 +00:00
}
private:
2005-07-17 07:14:09 +00:00
video::Font font;
audio::Sample chimes, ocean, rain, stream, thunder, waterdrop;
std::string status[6];
2005-07-05 06:44:55 +00:00
Log log;
AppCore& app;
video::VideoCore& video;
};
2005-05-15 02:50:07 +00:00
class AudioTest : public Application
{
public:
int main(const StrVec& args)
{
2005-07-05 06:44:55 +00:00
AppCore::getInstance().createDisplay(800,600,32,0,0,false);
AudioCore::setDesiredDevice("OSS");
new AudioCore;
Kernel::getInstance().addTask(TaskPtr(new MainTask()));
2005-07-04 03:06:48 +00:00
2005-07-05 06:44:55 +00:00
Kernel::getInstance().run();
2005-05-15 02:50:07 +00:00
return 0;
}
};
ENTRYPOINT(AudioTest)
2005-07-04 03:06:48 +00:00
#else
2005-07-18 05:14:18 +00:00
#include <iostream>
2005-07-04 03:06:48 +00:00
int main()
{
std::cerr << "Photon compiled without OpenAL support.\n";
}
#endif //PHOTON_USE_OPENAL