cpp_photon/test/Audio_test.cpp

227 lines
5.9 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:
// $Id: Audio_test.cpp,v 1.9 2005/08/07 07:12:48 cozman Exp $
2005-05-15 02:50:07 +00:00
#include "photon.hpp"
using namespace photon;
#include "FPSDisplayTask.hpp" // used to display FPS in title bar
2005-07-04 03:06:48 +00:00
2005-07-19 18:35:20 +00:00
// actual test is only compiled if OpenAL is in use
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-19 18:35:20 +00:00
// sole task of AudioTest
class MainTask : public Task , public InputListener
2005-07-05 06:44:55 +00:00
{
public:
MainTask() :
Task("MainTask"),
2005-08-02 23:07:51 +00:00
video(Application::getVideoCore())
2005-07-05 06:44:55 +00:00
{
2005-07-19 18:35:20 +00:00
video.setOrthoView(800,600); // setup view
2005-07-17 07:14:09 +00:00
2005-07-20 07:30:13 +00:00
// add archives to search path
util::filesys::addToSearchPath("data/fonts.zip");
util::filesys::addToSearchPath("data/wavdata.zip");
2005-07-19 18:35:20 +00:00
// load a font
2005-07-20 07:30:13 +00:00
video::Font::addResource("font","FreeMono.ttf",20);
2005-07-17 07:14:09 +00:00
font.open("font");
2005-07-20 07:30:13 +00:00
2005-07-19 18:35:20 +00:00
// load the 6 sound effects
2005-07-20 07:30:13 +00:00
Sample::addResource("chimes","chimes.wav");
Sample::addResource("ocean","ocean.wav");
Sample::addResource("rain","rain.wav");
Sample::addResource("stream","stream.wav");
Sample::addResource("thunder","thunder.wav");
Sample::addResource("waterdrop","waterdrop.wav");
2005-07-17 07:14:09 +00:00
2005-07-19 18:35:20 +00:00
// open the sounds
2005-07-17 07:14:09 +00:00
chimes.open("chimes");
ocean.open("ocean");
rain.open("rain");
stream.open("stream");
thunder.open("thunder");
waterdrop.open("waterdrop");
2005-07-19 18:35:20 +00:00
// make all sounds looping
2005-07-17 07:14:09 +00:00
chimes.setLooping(true);
ocean.setLooping(true);
rain.setLooping(true);
stream.setLooping(true);
thunder.setLooping(true);
2005-07-18 05:14:18 +00:00
2005-07-19 18:35:20 +00:00
// test Sample::isLooping via assertions
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
2005-07-19 18:35:20 +00:00
// status holds strings describing playing status for nice output
2005-07-17 07:14:09 +00:00
for(int i=0; i < 6; ++i)
status[i] = "NOT ";
}
2005-07-19 18:35:20 +00:00
// executes the checking of the keys, when a key is pressed
// the state is toggled
void onKeyPress(int key)
2005-07-17 07:14:09 +00:00
{
static const std::string NOT_PLAYING = "NOT ";
2005-07-19 18:35:20 +00:00
switch(key)
2005-07-17 07:14:09 +00:00
{
2005-07-19 18:35:20 +00:00
case KEY_C:
if(!chimes.isPlaying())
{
chimes.play();
status[0] = "";
}
else
{
chimes.stop();
status[0] = NOT_PLAYING;
}
break;
case KEY_O:
if(!ocean.isPlaying())
{
ocean.play();
status[1] = "";
}
else
{
ocean.stop();
status[1] = NOT_PLAYING;
}
break;
case KEY_R:
if(!rain.isPlaying())
{
rain.play();
status[2] = "";
}
else
{
rain.stop();
status[2] = NOT_PLAYING;
}
break;
2005-07-17 07:14:09 +00:00
2005-07-19 18:35:20 +00:00
case KEY_S:
if(!stream.isPlaying())
2005-07-17 07:14:09 +00:00
{
2005-07-19 18:35:20 +00:00
stream.play();
status[3] = "";
2005-07-17 07:14:09 +00:00
}
2005-07-19 18:35:20 +00:00
else
2005-07-17 07:14:09 +00:00
{
2005-07-19 18:35:20 +00:00
stream.stop();
status[3] = NOT_PLAYING;
2005-07-17 07:14:09 +00:00
}
2005-07-19 18:35:20 +00:00
break;
case KEY_T:
if(!thunder.isPlaying())
2005-07-17 07:14:09 +00:00
{
2005-07-19 18:35:20 +00:00
thunder.play();
status[4] = "";
2005-07-17 07:14:09 +00:00
}
2005-07-19 18:35:20 +00:00
else
2005-07-17 07:14:09 +00:00
{
2005-07-19 18:35:20 +00:00
thunder.stop();
status[4] = NOT_PLAYING;
2005-07-17 07:14:09 +00:00
}
2005-07-19 18:35:20 +00:00
break;
case KEY_W:
if(!waterdrop.isPlaying())
2005-07-17 07:14:09 +00:00
{
2005-07-19 18:35:20 +00:00
waterdrop.play();
status[5] = "";
2005-07-17 07:14:09 +00:00
}
2005-07-19 18:35:20 +00:00
else
2005-07-17 07:14:09 +00:00
{
2005-07-19 18:35:20 +00:00
waterdrop.stop();
status[5] = NOT_PLAYING;
2005-07-17 07:14:09 +00:00
}
2005-07-19 18:35:20 +00:00
break;
default:
break;
2005-07-17 07:14:09 +00:00
}
2005-07-05 06:44:55 +00:00
}
2005-07-19 18:35:20 +00:00
// called once per frame
2005-07-05 06:44:55 +00:00
void update()
2005-07-17 07:14:09 +00:00
{
2005-07-19 18:35:20 +00:00
// used for calculating draw position
static const photon::uint fontHeight(font.getHeight());
2005-07-17 07:14:09 +00:00
2005-07-19 18:35:20 +00:00
// draw the status of all 6 sounds
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-19 18:35:20 +00:00
2005-07-05 06:44:55 +00:00
video::VideoCore& video;
};
2005-05-15 02:50:07 +00:00
class AudioTest : public Application
{
public:
int main(const StrVec& args)
{
2005-07-19 18:35:20 +00:00
// create window
2005-08-02 23:07:51 +00:00
Application::getAppCore().createDisplay(800,600,32,0,0,false);
2005-07-19 18:35:20 +00:00
// create sound device
2005-08-02 23:07:51 +00:00
Application::initAudioCore("OSS");
// be sure to add FPSDisplayTask
2005-08-02 23:07:51 +00:00
Application::getKernel().addTask(TaskPtr(new FPSDisplayTask()));
// add the main task to the Kernel
2005-08-02 23:07:51 +00:00
Application::getKernel().addTask(TaskPtr(new MainTask()));
2005-07-19 18:35:20 +00:00
// run Kernel until task finishes
Application::getKernel().run();
2005-05-15 02:50:07 +00:00
return 0;
}
};
2005-07-19 18:35:20 +00:00
ENTRYPOINT(AudioTest) // make AudioTest the entrypoint class
2005-07-04 03:06:48 +00:00
#else
2005-07-19 18:35:20 +00:00
// alternate application if OpenAL was not available
#include <iostream>
2005-07-04 03:06:48 +00:00
int main()
{
std::cerr << "Photon compiled without OpenAL support.\n";
}
#endif //PHOTON_USE_OPENAL