Music to separated thread
This commit is contained in:
parent
54d0af8afb
commit
c281070ff6
77
AudioPlayerAsync.cpp
Normal file
77
AudioPlayerAsync.cpp
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
#include "AudioPlayerAsync.h"
|
||||||
|
|
||||||
|
|
||||||
|
AudioPlayerAsync::AudioPlayerAsync() : worker(&AudioPlayerAsync::workerThread, this) {}
|
||||||
|
|
||||||
|
AudioPlayerAsync::~AudioPlayerAsync() {
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
|
stop = true;
|
||||||
|
cv.notify_all();
|
||||||
|
}
|
||||||
|
worker.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AudioPlayerAsync::resetAsync() {
|
||||||
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
|
taskQueue.push([this]() {
|
||||||
|
//audioPlayerMutex.lock();
|
||||||
|
audioPlayer = std::make_unique<AudioPlayer>();
|
||||||
|
//audioPlayerMutex.unlock();
|
||||||
|
});
|
||||||
|
cv.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AudioPlayerAsync::playSoundAsync(std::string soundName) {
|
||||||
|
|
||||||
|
soundNameMutex.lock();
|
||||||
|
latestSoundName = soundName;
|
||||||
|
soundNameMutex.unlock();
|
||||||
|
|
||||||
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
|
taskQueue.push([this]() {
|
||||||
|
//audioPlayerMutex.lock();
|
||||||
|
if (audioPlayer) {
|
||||||
|
audioPlayer->playSound(latestSoundName);
|
||||||
|
}
|
||||||
|
//audioPlayerMutex.unlock();
|
||||||
|
});
|
||||||
|
cv.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AudioPlayerAsync::playMusicAsync(std::string musicName) {
|
||||||
|
|
||||||
|
musicNameMutex.lock();
|
||||||
|
latestMusicName = musicName;
|
||||||
|
musicNameMutex.unlock();
|
||||||
|
|
||||||
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
|
taskQueue.push([this]() {
|
||||||
|
//audioPlayerMutex.lock();
|
||||||
|
if (audioPlayer) {
|
||||||
|
audioPlayer->playMusic(latestMusicName);
|
||||||
|
}
|
||||||
|
//audioPlayerMutex.unlock();
|
||||||
|
});
|
||||||
|
cv.notify_one();
|
||||||
|
}
|
||||||
|
|
||||||
|
void AudioPlayerAsync::workerThread() {
|
||||||
|
while (true) {
|
||||||
|
std::function<void()> task;
|
||||||
|
{
|
||||||
|
std::unique_lock<std::mutex> lock(mtx);
|
||||||
|
cv.wait(lock, [this]() { return !taskQueue.empty() || stop; });
|
||||||
|
|
||||||
|
if (stop && taskQueue.empty()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
task = taskQueue.front();
|
||||||
|
taskQueue.pop();
|
||||||
|
}
|
||||||
|
|
||||||
|
task();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
46
AudioPlayerAsync.h
Normal file
46
AudioPlayerAsync.h
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <iostream>
|
||||||
|
#include <thread>
|
||||||
|
#include <mutex>
|
||||||
|
#include <condition_variable>
|
||||||
|
#include <queue>
|
||||||
|
#include <functional>
|
||||||
|
#include "cmakeaudioplayer/include/AudioPlayer.hpp"
|
||||||
|
|
||||||
|
class AudioPlayerAsync {
|
||||||
|
public:
|
||||||
|
AudioPlayerAsync();
|
||||||
|
~AudioPlayerAsync();
|
||||||
|
|
||||||
|
void resetAsync();
|
||||||
|
|
||||||
|
void playSoundAsync(std::string soundName);
|
||||||
|
|
||||||
|
void playMusicAsync(std::string musicName);
|
||||||
|
|
||||||
|
void exit()
|
||||||
|
{
|
||||||
|
stop = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unique_ptr<AudioPlayer> audioPlayer;
|
||||||
|
//std::mutex audioPlayerMutex;
|
||||||
|
|
||||||
|
std::mutex soundNameMutex;
|
||||||
|
std::mutex musicNameMutex;
|
||||||
|
|
||||||
|
std::string latestSoundName;
|
||||||
|
std::string latestMusicName;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
std::thread worker;
|
||||||
|
std::mutex mtx;
|
||||||
|
std::condition_variable cv;
|
||||||
|
std::queue<std::function<void()>> taskQueue;
|
||||||
|
bool stop = false;
|
||||||
|
|
||||||
|
void workerThread();
|
||||||
|
|
||||||
|
};
|
||||||
@ -98,10 +98,13 @@ void GameObjectManager::initialize() {
|
|||||||
activeObjects = rooms[current_room_index].objects;
|
activeObjects = rooms[current_room_index].objects;
|
||||||
|
|
||||||
// Initialize audio
|
// Initialize audio
|
||||||
|
/*
|
||||||
audioPlayer = std::make_unique<AudioPlayer>();
|
audioPlayer = std::make_unique<AudioPlayer>();
|
||||||
if (audioPlayer) {
|
if (audioPlayer) {
|
||||||
audioPlayer->playMusic(rooms[current_room_index].sound_name);
|
audioPlayer->playMusic(rooms[current_room_index].sound_name);
|
||||||
}
|
}*/
|
||||||
|
audioPlayerAsync.resetAsync();
|
||||||
|
audioPlayerAsync.playMusicAsync(rooms[current_room_index].sound_name);
|
||||||
|
|
||||||
// Initialize inventory
|
// Initialize inventory
|
||||||
inventoryIconMesh = CreateRect2D(
|
inventoryIconMesh = CreateRect2D(
|
||||||
@ -129,13 +132,16 @@ void GameObjectManager::switch_room(int index){
|
|||||||
|
|
||||||
roomTexturePtr = rooms[current_room_index].roomTexture;
|
roomTexturePtr = rooms[current_room_index].roomTexture;
|
||||||
|
|
||||||
audioPlayer.reset(); // This deletes the current AudioPlayer
|
|
||||||
|
//audioPlayer.reset(); // This deletes the current AudioPlayer
|
||||||
|
|
||||||
// Reinitialize it
|
// Reinitialize it
|
||||||
audioPlayer = std::make_unique<AudioPlayer>();
|
/*audioPlayer = std::make_unique<AudioPlayer>();
|
||||||
if (audioPlayer) {
|
if (audioPlayer) {
|
||||||
audioPlayer->playMusic(rooms[current_room_index].sound_name);
|
audioPlayer->playMusic(rooms[current_room_index].sound_name);
|
||||||
}
|
}*/
|
||||||
|
audioPlayerAsync.resetAsync();
|
||||||
|
audioPlayerAsync.playMusicAsync(rooms[current_room_index].sound_name);
|
||||||
|
|
||||||
activeObjects = rooms[current_room_index].objects;
|
activeObjects = rooms[current_room_index].objects;
|
||||||
|
|
||||||
@ -211,9 +217,11 @@ void GameObjectManager::handleEvent(const SDL_Event& event) {
|
|||||||
case SDLK_LEFT:
|
case SDLK_LEFT:
|
||||||
case SDLK_a:
|
case SDLK_a:
|
||||||
Environment::leftPressed = true;
|
Environment::leftPressed = true;
|
||||||
if (audioPlayer) {
|
/*if (audioPlayer) {
|
||||||
audioPlayer->playSound("Звук-Идут-по-земле.ogg");
|
audioPlayer->playSound("walk.ogg");
|
||||||
}
|
}*/
|
||||||
|
audioPlayerAsync.playSoundAsync("walk.ogg");
|
||||||
|
|
||||||
if (Environment::violaCurrentAnimation == 0)
|
if (Environment::violaCurrentAnimation == 0)
|
||||||
{
|
{
|
||||||
Environment::violaCurrentAnimation = 1;
|
Environment::violaCurrentAnimation = 1;
|
||||||
@ -223,9 +231,10 @@ void GameObjectManager::handleEvent(const SDL_Event& event) {
|
|||||||
case SDLK_RIGHT:
|
case SDLK_RIGHT:
|
||||||
case SDLK_d:
|
case SDLK_d:
|
||||||
Environment::rightPressed = true;
|
Environment::rightPressed = true;
|
||||||
if (audioPlayer) {
|
/*if (audioPlayer) {
|
||||||
audioPlayer->playSound("Звук-Идут-по-земле.ogg");
|
audioPlayer->playSound("walk.ogg");
|
||||||
}
|
}*/
|
||||||
|
audioPlayerAsync.playSoundAsync("walk.ogg");
|
||||||
if (Environment::violaCurrentAnimation == 0)
|
if (Environment::violaCurrentAnimation == 0)
|
||||||
{
|
{
|
||||||
Environment::violaCurrentAnimation = 1;
|
Environment::violaCurrentAnimation = 1;
|
||||||
@ -235,9 +244,10 @@ void GameObjectManager::handleEvent(const SDL_Event& event) {
|
|||||||
case SDLK_UP:
|
case SDLK_UP:
|
||||||
case SDLK_w:
|
case SDLK_w:
|
||||||
Environment::upPressed = true;
|
Environment::upPressed = true;
|
||||||
if (audioPlayer) {
|
/*if (audioPlayer) {
|
||||||
audioPlayer->playSound("Звук-Идут-по-земле.ogg");
|
audioPlayer->playSound("walk.ogg");
|
||||||
}
|
}*/
|
||||||
|
audioPlayerAsync.playSoundAsync("walk.ogg");
|
||||||
if (Environment::violaCurrentAnimation == 0)
|
if (Environment::violaCurrentAnimation == 0)
|
||||||
{
|
{
|
||||||
Environment::violaCurrentAnimation = 1;
|
Environment::violaCurrentAnimation = 1;
|
||||||
@ -247,9 +257,10 @@ void GameObjectManager::handleEvent(const SDL_Event& event) {
|
|||||||
case SDLK_DOWN:
|
case SDLK_DOWN:
|
||||||
case SDLK_s:
|
case SDLK_s:
|
||||||
Environment::downPressed = true;
|
Environment::downPressed = true;
|
||||||
if (audioPlayer) {
|
/*if (audioPlayer) {
|
||||||
audioPlayer->playSound("Звук-Идут-по-земле.ogg");
|
audioPlayer->playSound("walk.ogg");
|
||||||
}
|
}*/
|
||||||
|
audioPlayerAsync.playSoundAsync("walk.ogg");
|
||||||
if (Environment::violaCurrentAnimation == 0)
|
if (Environment::violaCurrentAnimation == 0)
|
||||||
{
|
{
|
||||||
Environment::violaCurrentAnimation = 1;
|
Environment::violaCurrentAnimation = 1;
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include "TextureManager.h"
|
#include "TextureManager.h"
|
||||||
#include "BoneAnimatedModel.h"
|
#include "BoneAnimatedModel.h"
|
||||||
#include "cmakeaudioplayer/include/AudioPlayer.hpp"
|
#include "AudioPlayerAsync.h"
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "ActiveObject.h"
|
#include "ActiveObject.h"
|
||||||
@ -10,6 +10,7 @@
|
|||||||
#include <SDL2/SDL.h>
|
#include <SDL2/SDL.h>
|
||||||
#endif
|
#endif
|
||||||
#include "OpenGlExtensions.h"
|
#include "OpenGlExtensions.h"
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
namespace ZL {
|
namespace ZL {
|
||||||
|
|
||||||
@ -47,7 +48,8 @@ public:
|
|||||||
|
|
||||||
std::vector<ZL::ActiveObject> activeObjects;
|
std::vector<ZL::ActiveObject> activeObjects;
|
||||||
std::vector<ZL::Room> rooms;
|
std::vector<ZL::Room> rooms;
|
||||||
std::unique_ptr<AudioPlayer> audioPlayer;
|
|
||||||
|
AudioPlayerAsync audioPlayerAsync;
|
||||||
|
|
||||||
ZL::VertexDataStruct inventoryIconMesh;
|
ZL::VertexDataStruct inventoryIconMesh;
|
||||||
ZL::VertexRenderStruct inventoryIconMeshMutable;
|
ZL::VertexRenderStruct inventoryIconMeshMutable;
|
||||||
@ -57,6 +59,8 @@ public:
|
|||||||
ActiveObjectManager aoMgr;
|
ActiveObjectManager aoMgr;
|
||||||
int objects_in_inventory;
|
int objects_in_inventory;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
//int animationCounter = 0;
|
//int animationCounter = 0;
|
||||||
int lastMouseX = 0; // Добавляем переменные для хранения позиции мыши
|
int lastMouseX = 0; // Добавляем переменные для хранения позиции мыши
|
||||||
|
|||||||
@ -27,7 +27,7 @@ namespace ZL
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
InventoryItem* GetItemByName(const std::string name)
|
InventoryItem* GetItemByName(const std::string& name)
|
||||||
{
|
{
|
||||||
// Пытаемся найти элемент по ключу
|
// Пытаемся найти элемент по ключу
|
||||||
auto it = gInventoryMap.find(name);
|
auto it = gInventoryMap.find(name);
|
||||||
|
|||||||
@ -32,7 +32,7 @@ namespace ZL
|
|||||||
// Поиск предмета по индексу (возвращает указатель или nullptr)
|
// Поиск предмета по индексу (возвращает указатель или nullptr)
|
||||||
InventoryItem* GetItemByHotkey(int hot_key);
|
InventoryItem* GetItemByHotkey(int hot_key);
|
||||||
InventoryItem* GetItemSelected(bool isSelected);
|
InventoryItem* GetItemSelected(bool isSelected);
|
||||||
InventoryItem* GetItemByName(std::string name);
|
InventoryItem* GetItemByName(const std::string& name);
|
||||||
|
|
||||||
// Вывести весь инвентарь в консоль
|
// Вывести весь инвентарь в консоль
|
||||||
void PrintInventory();
|
void PrintInventory();
|
||||||
|
|||||||
@ -217,7 +217,10 @@ void RenderSystem::drawUI(const GameObjectManager& gameObjects) {
|
|||||||
if (ao->activeObjectScreenTexturePtr) {
|
if (ao->activeObjectScreenTexturePtr) {
|
||||||
std::cout << "Found activeObjectScreenTexturePtr" << std::endl;
|
std::cout << "Found activeObjectScreenTexturePtr" << std::endl;
|
||||||
int screenX, screenY;
|
int screenX, screenY;
|
||||||
worldToScreenCoordinates(ao->objectPos, currentProjectionModelView,
|
|
||||||
|
Vector3f objectPosPlusShift = ao->objectPos + Vector3f{ 0, -Environment::cameraDefaultVerticalShift, 0 };
|
||||||
|
|
||||||
|
worldToScreenCoordinates(objectPosPlusShift, currentProjectionModelView,
|
||||||
Environment::width, Environment::height, screenX, screenY);
|
Environment::width, Environment::height, screenX, screenY);
|
||||||
renderer.PushMatrix();
|
renderer.PushMatrix();
|
||||||
// Здесь можно использовать вычисленные screenX, screenY,
|
// Здесь можно использовать вычисленные screenX, screenY,
|
||||||
|
|||||||
@ -146,6 +146,7 @@
|
|||||||
</Link>
|
</Link>
|
||||||
</ItemDefinitionGroup>
|
</ItemDefinitionGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ClCompile Include="AudioPlayerAsync.cpp" />
|
||||||
<ClCompile Include="BoneAnimatedModel.cpp" />
|
<ClCompile Include="BoneAnimatedModel.cpp" />
|
||||||
<ClCompile Include="Environment.cpp" />
|
<ClCompile Include="Environment.cpp" />
|
||||||
<ClCompile Include="Game.cpp" />
|
<ClCompile Include="Game.cpp" />
|
||||||
@ -170,6 +171,7 @@
|
|||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="ActiveObject.h" />
|
<ClInclude Include="ActiveObject.h" />
|
||||||
<ClInclude Include="AnimatedModel.h" />
|
<ClInclude Include="AnimatedModel.h" />
|
||||||
|
<ClInclude Include="AudioPlayerAsync.h" />
|
||||||
<ClInclude Include="BoneAnimatedModel.h" />
|
<ClInclude Include="BoneAnimatedModel.h" />
|
||||||
<ClInclude Include="Environment.h" />
|
<ClInclude Include="Environment.h" />
|
||||||
<ClInclude Include="Game.h" />
|
<ClInclude Include="Game.h" />
|
||||||
|
|||||||
@ -75,6 +75,9 @@
|
|||||||
<ClCompile Include="QuestScripts.cpp">
|
<ClCompile Include="QuestScripts.cpp">
|
||||||
<Filter>Source Files</Filter>
|
<Filter>Source Files</Filter>
|
||||||
</ClCompile>
|
</ClCompile>
|
||||||
|
<ClCompile Include="AudioPlayerAsync.cpp">
|
||||||
|
<Filter>Source Files</Filter>
|
||||||
|
</ClCompile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ClInclude Include="TextureManager.h">
|
<ClInclude Include="TextureManager.h">
|
||||||
@ -140,5 +143,8 @@
|
|||||||
<ClInclude Include="QuestScripts.h">
|
<ClInclude Include="QuestScripts.h">
|
||||||
<Filter>Header Files</Filter>
|
<Filter>Header Files</Filter>
|
||||||
</ClInclude>
|
</ClInclude>
|
||||||
|
<ClInclude Include="AudioPlayerAsync.h">
|
||||||
|
<Filter>Header Files</Filter>
|
||||||
|
</ClInclude>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
</Project>
|
</Project>
|
||||||
3
start.sh
3
start.sh
@ -2,7 +2,8 @@ g++ Game.cpp main.cpp Math.cpp OpenGlExtensions.cpp Physics.cpp Renderer.cpp \
|
|||||||
ShaderManager.cpp TextureManager.cpp Utils.cpp BoneAnimatedModel.cpp \
|
ShaderManager.cpp TextureManager.cpp Utils.cpp BoneAnimatedModel.cpp \
|
||||||
ObjLoader.cpp cmakeaudioplayer/src/AudioPlayer.cpp TextModel.cpp \
|
ObjLoader.cpp cmakeaudioplayer/src/AudioPlayer.cpp TextModel.cpp \
|
||||||
Inventory.cpp Environment.cpp GameObjectManager.cpp RenderSystem.cpp QuestScripts.cpp \
|
Inventory.cpp Environment.cpp GameObjectManager.cpp RenderSystem.cpp QuestScripts.cpp \
|
||||||
-o sdl_app -O2 -std=c++17 \
|
AudioPlayerAsync.cpp \
|
||||||
|
-o sdl_app -O2 -std=c++17 -pthread \
|
||||||
-I cmakeaudioplayer/include \
|
-I cmakeaudioplayer/include \
|
||||||
$(pkg-config --cflags --libs sdl2 gl) \
|
$(pkg-config --cflags --libs sdl2 gl) \
|
||||||
$(pkg-config --cflags --libs vorbis vorbisfile ogg) \
|
$(pkg-config --cflags --libs vorbis vorbisfile ogg) \
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user