Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1cae1135b9 | ||
|
|
f78ca49ec0 | ||
|
|
e9c7607a4b | ||
|
|
6d49e266dd | ||
|
|
78cdb01ef1 | ||
|
|
1ed434c858 | ||
|
|
f4acbaacdc | ||
|
|
e777f3ccc4 | ||
|
|
9e61c9e4d2 | ||
|
|
3fb730ceba | ||
|
|
811ac871a3 | ||
|
|
54ed0299c0 | ||
|
|
09fcd72e43 | ||
|
|
0371836dc8 |
113
AudioPlayer.cpp
Normal file
113
AudioPlayer.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
#include "AudioPlayer.h"
|
||||
#include "SDL.h"
|
||||
#include "SDL_audio.h"
|
||||
#include <stdlib.h>
|
||||
#include <malloc.h>
|
||||
#include <string.h>
|
||||
#define NUM_SOUNDS 10
|
||||
static SDL_AudioSpec fmt;
|
||||
static bool initiated = false;
|
||||
|
||||
AudioPlayer::AudioPlayer(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
AudioPlayer::~AudioPlayer(void)
|
||||
{
|
||||
SDL_CloseAudio();
|
||||
}
|
||||
|
||||
void AudioPlayer::init()
|
||||
{
|
||||
initiated = true;
|
||||
extern void mixaudio(void *unused, Uint8 *stream, int len);
|
||||
fmt.freq = 22050;
|
||||
fmt.format = AUDIO_S16;
|
||||
fmt.channels = 2;
|
||||
fmt.samples = 1024; /* A good value for games */
|
||||
fmt.callback = mixaudio;
|
||||
fmt.userdata = NULL;
|
||||
|
||||
/* Open the audio device and start playing sound! */
|
||||
if ( SDL_OpenAudio(&fmt, NULL) < 0 ) {
|
||||
fprintf(stderr, "Unable to open audio: %s\n", SDL_GetError());
|
||||
exit(1);
|
||||
}
|
||||
SDL_PauseAudio(0);
|
||||
}
|
||||
|
||||
static struct sample {
|
||||
Uint8 *data;
|
||||
Uint32 dpos;
|
||||
Uint32 dlen;
|
||||
} sounds[NUM_SOUNDS];
|
||||
|
||||
void mixaudio(void *unused, Uint8 *stream, int len)
|
||||
{
|
||||
int i;
|
||||
Uint32 amount;
|
||||
|
||||
for ( i=0; i<NUM_SOUNDS; ++i ) {
|
||||
amount = (sounds[i].dlen-sounds[i].dpos);
|
||||
if ( amount > (Uint32)len ) {
|
||||
amount = len;
|
||||
}
|
||||
SDL_MixAudio(stream, &sounds[i].data[sounds[i].dpos], amount, SDL_MIX_MAXVOLUME);
|
||||
sounds[i].dpos += amount;
|
||||
}
|
||||
}
|
||||
|
||||
void AudioPlayer::PlaySound(std::string fileString)
|
||||
{
|
||||
|
||||
if(initiated)
|
||||
{
|
||||
char *file = new char[fileString.length() + 1];
|
||||
strcpy(file, fileString.c_str());
|
||||
|
||||
|
||||
int index;
|
||||
SDL_AudioSpec wave;
|
||||
Uint8 *data;
|
||||
Uint32 dlen;
|
||||
SDL_AudioCVT cvt;
|
||||
|
||||
/* Look for an empty (or finished) sound slot */
|
||||
for ( index=0; index<NUM_SOUNDS; ++index ) {
|
||||
if ( sounds[index].dpos == sounds[index].dlen ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( index == NUM_SOUNDS )
|
||||
return;
|
||||
|
||||
/* Load the sound file and convert it to 16-bit stereo at 22kHz */
|
||||
if ( SDL_LoadWAV(file, &wave, &data, &dlen) == NULL ) {
|
||||
fprintf(stderr, "Couldn't load %s: %s\n", file, SDL_GetError());
|
||||
return;
|
||||
}
|
||||
SDL_BuildAudioCVT(&cvt, wave.format, wave.channels, wave.freq,
|
||||
AUDIO_S16, 2, fmt.freq);
|
||||
cvt.buf = (Uint8*)malloc(dlen*cvt.len_mult);
|
||||
memcpy(cvt.buf, data, dlen);
|
||||
cvt.len = dlen;
|
||||
SDL_ConvertAudio(&cvt);
|
||||
SDL_FreeWAV(data);
|
||||
|
||||
/* Put the sound data in the slot (it starts playing immediately) */
|
||||
if ( sounds[index].data ) {
|
||||
free(sounds[index].data);
|
||||
}
|
||||
SDL_LockAudio();
|
||||
sounds[index].data = cvt.buf;
|
||||
sounds[index].dlen = cvt.len_cvt;
|
||||
sounds[index].dpos = 0;
|
||||
SDL_UnlockAudio();
|
||||
delete [] file;
|
||||
}
|
||||
else
|
||||
{
|
||||
OutputDebugString("Audio player not initialized, sound will not play\r\n");
|
||||
}
|
||||
}
|
||||
11
AudioPlayer.h
Normal file
11
AudioPlayer.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#include <G3DAll.h>
|
||||
#pragma once
|
||||
|
||||
class AudioPlayer
|
||||
{
|
||||
public:
|
||||
AudioPlayer(void);
|
||||
~AudioPlayer(void);
|
||||
static void PlaySound(std::string);
|
||||
static void init();
|
||||
};
|
||||
@@ -230,6 +230,10 @@
|
||||
Name="Source Files"
|
||||
Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\AudioPlayer.cpp"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\BaseButtonInstance.cpp"
|
||||
>
|
||||
@@ -283,6 +287,10 @@
|
||||
Name="Header Files"
|
||||
Filter="h;hpp;hxx;hm;inl"
|
||||
>
|
||||
<File
|
||||
RelativePath=".\AudioPlayer.h"
|
||||
>
|
||||
</File>
|
||||
<File
|
||||
RelativePath=".\BaseButtonInstance.h"
|
||||
>
|
||||
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
BIN
content/sounds/SWITCH3.wav
Normal file
BIN
content/sounds/SWITCH3.wav
Normal file
Binary file not shown.
BIN
content/sounds/pageturn.wav
Normal file
BIN
content/sounds/pageturn.wav
Normal file
Binary file not shown.
139
main.cpp
139
main.cpp
@@ -16,6 +16,7 @@
|
||||
#include "PhysicalInstance.h"
|
||||
#include "TextButtonInstance.h"
|
||||
#include "ImageButtonInstance.h"
|
||||
#include "AudioPlayer.h"
|
||||
|
||||
|
||||
#if G3D_VER < 61000
|
||||
@@ -36,11 +37,13 @@ static G3D::RealTime messageTime = 0;
|
||||
static G3D::RealTime inputTime = 0;
|
||||
static int FPSVal[8] = {10, 20, 30, 60, 120, 240, INT_MAX,1};
|
||||
static int index = 2;
|
||||
static std::string cameraSound = "";
|
||||
static float TIMERVAL = 60.0F;
|
||||
static int SCOREVAL = 0;
|
||||
static G3D::TextureRef go = NULL;
|
||||
static G3D::TextureRef go_ovr = NULL;
|
||||
static G3D::TextureRef go_dn = NULL;
|
||||
VARAreaRef varStatic = NULL;
|
||||
static float mousex = 0;
|
||||
static float mousey = 0;
|
||||
static int cursorid = 0;
|
||||
@@ -161,6 +164,7 @@ HWND App::getMainHWND()
|
||||
|
||||
|
||||
Demo::Demo(App* _app) : GApplet(_app), app(_app) {
|
||||
varStatic = VARArea::create(1024 * 1024);
|
||||
}
|
||||
|
||||
|
||||
@@ -246,6 +250,7 @@ public:
|
||||
|
||||
void CameraButtonListener::onButton1MouseClick(BaseButtonInstance* button)
|
||||
{
|
||||
AudioPlayer::PlaySound(cameraSound);
|
||||
CoordinateFrame frame = usableApp->debugCamera.getCoordinateFrame();
|
||||
if(button->name == "CenterCam")
|
||||
centerCam = true;
|
||||
@@ -260,6 +265,34 @@ void CameraButtonListener::onButton1MouseClick(BaseButtonInstance* button)
|
||||
}
|
||||
|
||||
|
||||
class DeleteListener : public ButtonListener {
|
||||
public:
|
||||
void onButton1MouseClick(BaseButtonInstance*);
|
||||
};
|
||||
|
||||
void DeleteListener::onButton1MouseClick(BaseButtonInstance* button)
|
||||
{
|
||||
|
||||
|
||||
if(selectedInstance != NULL)
|
||||
{
|
||||
for(size_t i = 0; i < instances.size(); i++)
|
||||
{
|
||||
if(instances.at(i) == selectedInstance)
|
||||
{
|
||||
Instance* deleting = instances.at(i);
|
||||
instances.erase(instances.begin() + i);
|
||||
delete deleting;
|
||||
selectedInstance = NULL;
|
||||
AudioPlayer::PlaySound(GetFileInPath("/content/sounds/pageturn.wav"));
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class ModeSelectionListener : public ButtonListener {
|
||||
public:
|
||||
void onButton1MouseClick(BaseButtonInstance*);
|
||||
@@ -481,15 +514,8 @@ void initGUI()
|
||||
instance->size = Vector2(40,46);
|
||||
instance->position = Vector2(20, 284);
|
||||
instance->parent = dataModel;
|
||||
|
||||
instance = makeImageButton(
|
||||
Texture::fromFile(GetFileInPath("/content/images/Delete.png")),
|
||||
Texture::fromFile(GetFileInPath("/content/images/Delete_ovr.png")),
|
||||
Texture::fromFile(GetFileInPath("/content/images/Delete_dn.png")),
|
||||
Texture::fromFile(GetFileInPath("/content/images/Delete_ds.png")));
|
||||
instance->size = Vector2(40,46);
|
||||
instance->position = Vector2(20, 284);
|
||||
instance->parent = dataModel;
|
||||
instance->name = "Delete";
|
||||
instance->setButtonListener(new DeleteListener());
|
||||
|
||||
instance = makeImageButton(
|
||||
Texture::fromFile(GetFileInPath("/content/images/CameraZoomIn.png")),
|
||||
@@ -703,6 +729,19 @@ void Demo::onCleanup() {
|
||||
|
||||
void Demo::onLogic() {
|
||||
// Add non-simulation game logic and AI code here
|
||||
for(size_t i = 0; i < instances_2D.size(); i++)
|
||||
{
|
||||
if(instances_2D.at(i)->name == "Delete")
|
||||
{
|
||||
ImageButtonInstance* button = (ImageButtonInstance*)instances_2D.at(i);
|
||||
if(selectedInstance == NULL)
|
||||
button->disabled = true;
|
||||
else
|
||||
button->disabled = false;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -840,11 +879,13 @@ void Demo::onUserInput(UserInput* ui) {
|
||||
|
||||
if(ui->keyPressed(SDL_MOUSE_WHEEL_UP_KEY))
|
||||
{
|
||||
AudioPlayer::PlaySound(cameraSound);
|
||||
CoordinateFrame frame = app->debugCamera.getCoordinateFrame();
|
||||
cameraPos = cameraPos + frame.lookVector()*2;
|
||||
}
|
||||
if(ui->keyPressed(SDL_MOUSE_WHEEL_DOWN_KEY))
|
||||
{
|
||||
AudioPlayer::PlaySound(cameraSound);
|
||||
CoordinateFrame frame = app->debugCamera.getCoordinateFrame();
|
||||
cameraPos = cameraPos - frame.lookVector()*2;
|
||||
}
|
||||
@@ -901,6 +942,27 @@ void Demo::onUserInput(UserInput* ui) {
|
||||
right = true;
|
||||
}
|
||||
|
||||
if(ui->keyPressed(SDL_LEFT_MOUSE_KEY))
|
||||
{
|
||||
bool onGUI = false;
|
||||
for(size_t i = 0; i < instances_2D.size(); i++)
|
||||
{
|
||||
if(instances_2D.at(i)->className == "TextButton" || instances_2D.at(i)->className == "ImageButton")
|
||||
{
|
||||
BaseButtonInstance* button = (BaseButtonInstance*)instances_2D.at(i);
|
||||
if(button->mouseInButton(ui->mouseXY().x, ui->mouseXY().y, app->renderDevice))
|
||||
{
|
||||
onGUI = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(!onGUI)
|
||||
{
|
||||
selectedInstance = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
if(ui->keyReleased(SDL_LEFT_MOUSE_KEY))
|
||||
{
|
||||
|
||||
@@ -1060,7 +1122,25 @@ void Demo::exitApplication()
|
||||
app->endProgram = true;
|
||||
}
|
||||
|
||||
|
||||
void makeBeveledBox(Box box, RenderDevice* rd, Color4 color, CoordinateFrame cFrame)
|
||||
{
|
||||
Vector3 v0, v1, v2, v3;
|
||||
//glDiffuse();
|
||||
rd->setColor(color);
|
||||
rd->setObjectToWorldMatrix(CoordinateFrame());
|
||||
rd->beginPrimitive(RenderDevice::QUADS);
|
||||
for (int f = 0; f < 6; ++f) {
|
||||
box.getFaceCorners(f, v0, v1, v2, v3);
|
||||
glShadeModel(GL_SMOOTH);
|
||||
//rd->setNormal((v1 - v0).cross(v3 - v0).direction());
|
||||
rd->sendVertex(v0);
|
||||
rd->sendVertex(v1);
|
||||
rd->sendVertex(v2);
|
||||
rd->sendVertex(v3);
|
||||
}
|
||||
rd->setColor(Color3::white());
|
||||
rd->endPrimitive();
|
||||
}
|
||||
|
||||
|
||||
void Demo::onGraphics(RenderDevice* rd) {
|
||||
@@ -1101,6 +1181,7 @@ void Demo::onGraphics(RenderDevice* rd) {
|
||||
// Setup lighting
|
||||
app->renderDevice->enableLighting();
|
||||
|
||||
app->renderDevice->setShadeMode(RenderDevice::SHADE_SMOOTH);
|
||||
app->renderDevice->setAmbientLightColor(Color3(1,1,1));
|
||||
Draw::axes(CoordinateFrame(Vector3(0, 0, 0)), app->renderDevice);
|
||||
|
||||
@@ -1128,6 +1209,33 @@ void Demo::onGraphics(RenderDevice* rd) {
|
||||
Vector3 pos3 = Vector3((pos.x+size.x/2)/2,(pos.y+size.y/2)/2,(pos.z+size.z/2)/2);
|
||||
Box box = Box(Vector3(0+size.x/4, 0+size.y/4, 0+size.z/4) ,Vector3(0-size.x/4,0-size.y/4,0-size.z/4));
|
||||
CoordinateFrame c = CoordinateFrame(part->getCFrame().rotation,Vector3(part->getCFrame().translation.x/2, part->getCFrame().translation.y/2, part->getCFrame().translation.z/2));
|
||||
//Box wsb = c.toWorldSpace(box);
|
||||
//makeBeveledBox(c.toWorldSpace(box), app->renderDevice, part->color, part->getCFrame());
|
||||
//G3D::MeshBuilder builder = G3D::MeshBuilder();
|
||||
//for(int i = 0; i < 6; i++)
|
||||
//{
|
||||
// Vector3 v1, v2, v3, v4;
|
||||
// wsb.getFaceCorners(i, v1, v2, v3, v4);
|
||||
// builder.addQuad(v1, v2, v3, v4);
|
||||
//}
|
||||
//std::string str;
|
||||
//G3D::Array<int> arrayInd;
|
||||
//G3D::Array<Vector3> arrayVector;
|
||||
//builder.commit(str, arrayInd, arrayVector);
|
||||
|
||||
|
||||
//IFSModel::save(ExePath() + "/content/model.ifs", str, arrayInd, arrayVector, NULL);
|
||||
//IFSModelRef model = IFSModel::create(ExePath() + "/content/model.ifs");
|
||||
//app->renderDevice->setLight(0, GLight::directional(lighting.lightDirection, lighting.lightColor));
|
||||
|
||||
//app->renderDevice->beginIndexedPrimitives();
|
||||
//{
|
||||
// app->renderDevice->setNormalArray(G3D::VAR(arrayVector, varStatic));
|
||||
//app->renderDevice->setVertexArray(G3D::VAR(arrayVector, varStatic));
|
||||
//app->renderDevice->sendIndices(RenderDevice::TRIANGLES, arrayInd);
|
||||
//}
|
||||
//app->renderDevice->endIndexedPrimitives();
|
||||
|
||||
Draw::box(c.toWorldSpace(box), app->renderDevice, part->color, Color4::clear());
|
||||
if(selectedInstance == part)
|
||||
{
|
||||
@@ -1266,6 +1374,7 @@ void App::main() {
|
||||
cursor = Texture::fromFile(GetFileInPath("/content/cursor2.png"));
|
||||
fntdominant = GFont::fromFile(GetFileInPath("/content/font/dominant.fnt"));
|
||||
fntlighttrek = GFont::fromFile(GetFileInPath("/content/font/lighttrek.fnt"));
|
||||
cameraSound = GetFileInPath("/content/sounds/SWITCH3.wav");
|
||||
sky = Sky::create(NULL, ExePath() + "/content/sky/");
|
||||
cursorid = cursor->openGLID();
|
||||
applet->run();
|
||||
@@ -1359,10 +1468,11 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
|
||||
int main(int argc, char** argv) {
|
||||
//_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
|
||||
//_CrtSetBreakAlloc(1279);
|
||||
|
||||
try{
|
||||
AudioPlayer::init();
|
||||
GAppSettings settings;
|
||||
settings.window.resizable = true;
|
||||
settings.window.fsaaSamples = 8;
|
||||
//settings.window.fsaaSamples = 8;
|
||||
settings.writeLicenseFile = false;
|
||||
settings.window.center = true;
|
||||
//Using the damned SDL window now
|
||||
@@ -1451,5 +1561,10 @@ int main(int argc, char** argv) {
|
||||
//messageTime = G3D::System::time();
|
||||
|
||||
app.run();
|
||||
}
|
||||
catch(std::exception)
|
||||
{
|
||||
OnError(-1);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user