Added sound system
Added delete sound
This commit is contained in:
99
AudioPlayer.cpp
Normal file
99
AudioPlayer.cpp
Normal file
@@ -0,0 +1,99 @@
|
|||||||
|
#include "AudioPlayer.h"
|
||||||
|
#include "SDL.h"
|
||||||
|
#include "SDL_audio.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <malloc.h>
|
||||||
|
#include <string.h>
|
||||||
|
#define NUM_SOUNDS 2
|
||||||
|
SDL_AudioSpec fmt;
|
||||||
|
|
||||||
|
AudioPlayer::AudioPlayer(void)
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
AudioPlayer::~AudioPlayer(void)
|
||||||
|
{
|
||||||
|
SDL_CloseAudio();
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
|
||||||
|
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, 22050);
|
||||||
|
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;
|
||||||
|
}
|
||||||
10
AudioPlayer.h
Normal file
10
AudioPlayer.h
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
#include <G3DAll.h>
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
class AudioPlayer
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
AudioPlayer(void);
|
||||||
|
~AudioPlayer(void);
|
||||||
|
void PlaySound(std::string);
|
||||||
|
};
|
||||||
@@ -230,6 +230,10 @@
|
|||||||
Name="Source Files"
|
Name="Source Files"
|
||||||
Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
Filter="cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
|
||||||
>
|
>
|
||||||
|
<File
|
||||||
|
RelativePath=".\AudioPlayer.cpp"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\BaseButtonInstance.cpp"
|
RelativePath=".\BaseButtonInstance.cpp"
|
||||||
>
|
>
|
||||||
@@ -283,6 +287,10 @@
|
|||||||
Name="Header Files"
|
Name="Header Files"
|
||||||
Filter="h;hpp;hxx;hm;inl"
|
Filter="h;hpp;hxx;hm;inl"
|
||||||
>
|
>
|
||||||
|
<File
|
||||||
|
RelativePath=".\AudioPlayer.h"
|
||||||
|
>
|
||||||
|
</File>
|
||||||
<File
|
<File
|
||||||
RelativePath=".\BaseButtonInstance.h"
|
RelativePath=".\BaseButtonInstance.h"
|
||||||
>
|
>
|
||||||
|
|||||||
BIN
content/sounds/pageturn.wav
Normal file
BIN
content/sounds/pageturn.wav
Normal file
Binary file not shown.
7
main.cpp
7
main.cpp
@@ -16,6 +16,7 @@
|
|||||||
#include "PhysicalInstance.h"
|
#include "PhysicalInstance.h"
|
||||||
#include "TextButtonInstance.h"
|
#include "TextButtonInstance.h"
|
||||||
#include "ImageButtonInstance.h"
|
#include "ImageButtonInstance.h"
|
||||||
|
#include "AudioPlayer.h"
|
||||||
|
|
||||||
|
|
||||||
#if G3D_VER < 61000
|
#if G3D_VER < 61000
|
||||||
@@ -38,6 +39,7 @@ static int FPSVal[8] = {10, 20, 30, 60, 120, 240, INT_MAX,1};
|
|||||||
static int index = 2;
|
static int index = 2;
|
||||||
static float TIMERVAL = 60.0F;
|
static float TIMERVAL = 60.0F;
|
||||||
static int SCOREVAL = 0;
|
static int SCOREVAL = 0;
|
||||||
|
static AudioPlayer soundSystem;
|
||||||
static G3D::TextureRef go = NULL;
|
static G3D::TextureRef go = NULL;
|
||||||
static G3D::TextureRef go_ovr = NULL;
|
static G3D::TextureRef go_ovr = NULL;
|
||||||
static G3D::TextureRef go_dn = NULL;
|
static G3D::TextureRef go_dn = NULL;
|
||||||
@@ -269,6 +271,8 @@ public:
|
|||||||
|
|
||||||
void DeleteListener::onButton1MouseClick(BaseButtonInstance* button)
|
void DeleteListener::onButton1MouseClick(BaseButtonInstance* button)
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
if(selectedInstance != NULL)
|
if(selectedInstance != NULL)
|
||||||
{
|
{
|
||||||
for(size_t i = 0; i < instances.size(); i++)
|
for(size_t i = 0; i < instances.size(); i++)
|
||||||
@@ -279,6 +283,9 @@ void DeleteListener::onButton1MouseClick(BaseButtonInstance* button)
|
|||||||
instances.erase(instances.begin() + i);
|
instances.erase(instances.begin() + i);
|
||||||
delete deleting;
|
delete deleting;
|
||||||
selectedInstance = NULL;
|
selectedInstance = NULL;
|
||||||
|
soundSystem.PlaySound(GetFileInPath("/content/sounds/pageturn.wav"));
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user