Skip to content

Commit 1c61a39

Browse files
authored
Merge pull request #19 from lr8soft/dev-y
v0.26
2 parents b50f1ca + 268337a commit 1c61a39

6 files changed

Lines changed: 65 additions & 39 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
x64
22
.vs
33
Debug
44
Release

.idea/workspace.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python2D/XCCore/Item/Player.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ void Player::hurtPlayer()
133133
{
134134
if (itemTimer.getAccumlateTime() - lastHitTime > HitProtectTime || lastHitTime == 0) {
135135
lastHitTime = itemTimer.getAccumlateTime();
136-
AudioHelper::playerWavFile(playerHurtAudio);
136+
AudioHelper::playFromBuffer(playerHurtAudio.wavBuffer);
137137
fxParticleManager.addNewParticle(250, 25.0f, 1.6f, 0.8f, glm::vec4(1.0f, 0.1f, 0.1f, 1.0f), glm::vec3(NowPosition[0], NowPosition[1], NowPosition[2]));
138138
isHitTime = true;
139139
}
@@ -142,7 +142,7 @@ void Player::hurtPlayer()
142142

143143
void Player::grazePlayer()
144144
{
145-
AudioHelper::playerWavFile(playerGrazeAudio);
145+
AudioHelper::playFromBuffer(playerGrazeAudio.wavBuffer);
146146
fxParticleManager.addNewParticle(1, 12.0f, 0.6f, 0.6f, glm::vec4(1.0f), glm::vec3(NowPosition[0], NowPosition[1], NowPosition[2]));
147147
}
148148

Python2D/XCCore/XCAudio/AudioHelper.cpp

Lines changed: 53 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ALCdevice* AudioHelper::device = nullptr;
88
ALCcontext* AudioHelper::context = nullptr;
99

1010
std::map<std::string, XCWavFile> AudioHelper::wavSourceGroup;
11-
std::map<ALuint, std::atomic_bool> AudioHelper::playingGroup;
11+
std::map<ALuint, ALuint> AudioHelper::effectPlayingList;
1212
void AudioHelper::EvonInit()
1313
{
1414
if (!isEvonInit) {
@@ -34,6 +34,17 @@ void AudioHelper::EvonInit()
3434

3535
void AudioHelper::UnloadEvon()
3636
{
37+
38+
for (auto work = effectPlayingList.begin(); work != effectPlayingList.end(); work++) {
39+
ALint state;
40+
alGetSourcei(work->second, AL_SOURCE_STATE, &state);
41+
if (state == AL_PLAYING) {
42+
alSourceStop(work->second);
43+
}
44+
alDeleteBuffers(1, &work->first);
45+
alDeleteSources(1, &work->second);
46+
}
47+
3748
alcMakeContextCurrent(NULL);
3849
alcDestroyContext(context);
3950
alcCloseDevice(device);
@@ -65,43 +76,56 @@ XCWavFile AudioHelper::loadWavFromFile(const std::string filename)
6576
return wavFile;
6677
}
6778

68-
void AudioHelper::playerWavFile(XCWavFile file)
79+
void AudioHelper::setVolume(float volume)
80+
{
81+
audioVolume = volume;
82+
}
83+
84+
void AudioHelper::playFromBuffer(ALuint buffer)
6985
{
70-
auto target = playingGroup.find(file.wavSource);
71-
if (target != playingGroup.end()) {
72-
bool isPlaying = target->second;
73-
if (!isPlaying) {
74-
std::thread playThread(threadWork,file);
75-
playThread.detach();
86+
auto work = effectPlayingList.find(buffer);
87+
if (work!=effectPlayingList.end()) {
88+
ALint state;
89+
alGetSourcei(work->second, AL_SOURCE_STATE, &state);
90+
if (state!=AL_PLAYING) {
91+
alSourcePlay(work->second);
7692
}
7793
}
7894
else {
79-
playingGroup[file.wavSource] = true;
80-
std::thread playThread(threadWork, file);
81-
playThread.detach();
95+
ALuint source;
96+
alGenSources(1, &source);
97+
alSourcei(source, AL_BUFFER, buffer);
98+
alSourcef(source, AL_GAIN, audioVolume);
99+
alSourcePlay(source);
100+
101+
effectPlayingList.insert(std::make_pair(buffer, source));
82102
}
83103
}
84104

85-
void AudioHelper::threadWork(XCWavFile file)
105+
void AudioHelper::stopByBuffer(ALuint buffer)
86106
{
87-
ALuint source;
88-
alGenSources(1, &source);
89-
alSourcei(source, AL_BUFFER, file.wavSource);
90-
alSourcef(source, AL_GAIN, audioVolume);
91-
alSourcePlay(source);
92-
93-
ALint state;
94-
do {
95-
alGetSourcei(source, AL_SOURCE_STATE, &state);
96-
} while (state == AL_PLAYING);
97-
alDeleteSources(1, &source);
98-
playingGroup[file.wavSource] = false;
107+
auto work = effectPlayingList.find(buffer);
108+
if (work != effectPlayingList.end()) {
109+
ALint state;
110+
alGetSourcei(work->second, AL_SOURCE_STATE, &state);
111+
if (state == AL_PLAYING) {
112+
alSourceStop(work->second);
113+
}
114+
}
99115
}
100116

101-
102-
void AudioHelper::setVolume(float volume)
117+
void AudioHelper::releaseByBuffer(ALuint buffer)
103118
{
104-
audioVolume = volume;
119+
auto work = effectPlayingList.find(buffer);
120+
if (work != effectPlayingList.end()) {
121+
ALint state;
122+
alGetSourcei(work->second, AL_SOURCE_STATE, &state);
123+
if (state == AL_PLAYING) {
124+
alSourceStop(work->second);
125+
}
126+
alDeleteSources(1, &work->second);
127+
effectPlayingList.erase(work);
128+
}
105129
}
106130

107131
bool AudioHelper::loadWavFile(const std::string filename, XCWavFile *wavFile) {
@@ -180,11 +204,11 @@ bool AudioHelper::loadWavFile(const std::string filename, XCWavFile *wavFile) {
180204
wavFile->wavFormat = AL_FORMAT_STEREO16;
181205
}
182206
//create our openAL buffer and check for success
183-
alGenBuffers(1, &(wavFile->wavSource));
207+
alGenBuffers(1, &(wavFile->wavBuffer));
184208
//errorCheck();
185209
//now we put our data into the openAL buffer and
186210
//check for success
187-
alBufferData(wavFile->wavSource, wavFile->wavFormat, (void*)data,
211+
alBufferData(wavFile->wavBuffer, wavFile->wavFormat, (void*)data,
188212
wavFile->wavSize, wavFile->wavFrequent);
189213
//errorCheck();
190214
//clean up and return true if successful

Python2D/XCCore/XCAudio/AudioHelper.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#include <AL/alc.h>
77
#include <map>
88
#include <iostream>
9-
#include <atomic>
109
struct WAVE_Data {
1110
char subChunkID[4]; //should contain the word data
1211
long subChunk2Size; //Stores the size of the data block
@@ -31,7 +30,7 @@ struct RIFF_Header {
3130
};
3231

3332
struct XCWavFile {
34-
ALuint wavSource;
33+
ALuint wavBuffer;
3534
ALsizei wavSize;
3635
ALsizei wavFrequent;
3736
ALenum wavFormat;
@@ -45,17 +44,19 @@ class AudioHelper {
4544
static float audioVolume;
4645

4746
static std::map<std::string, XCWavFile> wavSourceGroup;
48-
static std::map<ALuint, std::atomic_bool> playingGroup;
47+
static std::map<ALuint, ALuint> effectPlayingList;/*buffer, source*/
4948

5049
static bool loadWavFile(const std::string filename, XCWavFile *wavFile);
51-
52-
static void threadWork(XCWavFile);
50+
5351
public:
5452
static void EvonInit();
5553
static void UnloadEvon();
5654

5755
static XCWavFile loadWavFromFile(const std::string filename);
58-
static void playerWavFile(XCWavFile file);
5956
static void setVolume(float volume);
57+
58+
static void playFromBuffer(ALuint buffer);
59+
static void stopByBuffer(ALuint buffer);
60+
static void releaseByBuffer(ALuint buffer);
6061
};
6162
#endif

Python2D/script/XCCore.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
winWidth = 1280
1010
winHeight = 720
1111

12-
winTitle = "Python2D v0.24"
12+
winTitle = "Python2D v0.25"
1313
winScaleToMonitor = False
1414
winResize = False
1515

0 commit comments

Comments
 (0)