trying to resolve conflicts

This commit is contained in:
Альберт Гадиев 2025-03-02 21:54:32 +06:00
commit a580afb8b9
11 changed files with 178 additions and 24 deletions

90
AudioPlayerAsync.cpp Normal file
View File

@ -0,0 +1,90 @@
#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::stopAsync() {
std::unique_lock<std::mutex> lock(mtx);
taskQueue.push([this]() {
//audioPlayerMutex.lock();
audioPlayer->stop();
std::this_thread::sleep_for(std::chrono::seconds(1));
//audioPlayerMutex.unlock();
});
cv.notify_one();
}
void AudioPlayerAsync::resetAsync() {
std::unique_lock<std::mutex> lock(mtx);
taskQueue.push([this]() {
//audioPlayerMutex.lock();
audioPlayer.reset();
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();
}
}

48
AudioPlayerAsync.h Normal file
View File

@ -0,0 +1,48 @@
#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 stopAsync();
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();
};

View File

@ -28,7 +28,8 @@ void GameObjectManager::initialize() {
testObjMeshMutable.data = testObjMesh;
testObjMeshMutable.RefreshVBO();
textMesh = ZL::LoadFromTextFile("./textures/mesh_first_room.txt"); // Add ZL:: namespace
//textMesh = ZL::LoadFromTextFile("./textures/mesh_first_room.txt");
textMesh = ZL::LoadFromTextFile("./oneroom001.txt");
textMesh.Scale(10);
textMesh.SwapZandY();
textMesh.RotateByMatrix(QuatToMatrix(QuatFromRotateAroundX(M_PI * 0.5)));
@ -79,7 +80,7 @@ void GameObjectManager::initialize() {
*/
Room room_1;
room_1.roomTexture = std::make_shared<Texture>(CreateTextureDataFromBmp24("./Kitchen_ceramics.bmp"));
room_1.roomTexture = std::make_shared<Texture>(CreateTextureDataFromBmp24("./Material_Base_color_1001.bmp"));
room_1.objects.push_back(ao1);
room_1.sound_name = "Symphony No.6 (1st movement).ogg";
room_1.roomLogic = createRoom1Logic();
@ -95,10 +96,13 @@ void GameObjectManager::initialize() {
activeObjects = rooms[current_room_index].objects;
// Initialize audio
/*
audioPlayer = std::make_unique<AudioPlayer>();
if (audioPlayer) {
audioPlayer->playMusic(rooms[current_room_index].sound_name);
}
}*/
audioPlayerAsync.resetAsync();
audioPlayerAsync.playMusicAsync(rooms[current_room_index].sound_name);
// Initialize inventory
inventoryIconMesh = CreateRect2D(
@ -180,13 +184,17 @@ void GameObjectManager::switch_room(int index){
roomTexturePtr = rooms[current_room_index].roomTexture;
audioPlayer.reset(); // This deletes the current AudioPlayer
//audioPlayer.reset(); // This deletes the current AudioPlayer
// Reinitialize it
audioPlayer = std::make_unique<AudioPlayer>();
/*audioPlayer = std::make_unique<AudioPlayer>();
if (audioPlayer) {
audioPlayer->playMusic(rooms[current_room_index].sound_name);
}
}*/
audioPlayerAsync.stopAsync();
audioPlayerAsync.resetAsync();
audioPlayerAsync.playMusicAsync(rooms[current_room_index].sound_name);
activeObjects = rooms[current_room_index].objects;
@ -251,9 +259,7 @@ void GameObjectManager::handleEvent(const SDL_Event& event) {
case SDLK_LEFT:
case SDLK_a:
Environment::leftPressed = true;
if (audioPlayer) {
audioPlayer->playSound("Звук-Идут-по-земле.ogg");
}
audioPlayerAsync.playSoundAsync("Звук-Идут-по-земле.ogg"); // Заменено
if (Environment::violaCurrentAnimation == 0) {
Environment::violaCurrentAnimation = 1;
Environment::violaLastWalkFrame = -1;
@ -262,9 +268,7 @@ void GameObjectManager::handleEvent(const SDL_Event& event) {
case SDLK_RIGHT:
case SDLK_d:
Environment::rightPressed = true;
if (audioPlayer) {
audioPlayer->playSound("Звук-Идут-по-земле.ogg");
}
audioPlayerAsync.playSoundAsync("Звук-Идут-по-земле.ogg"); // Заменено
if (Environment::violaCurrentAnimation == 0) {
Environment::violaCurrentAnimation = 1;
Environment::violaLastWalkFrame = -1;
@ -273,9 +277,7 @@ void GameObjectManager::handleEvent(const SDL_Event& event) {
case SDLK_UP:
case SDLK_w:
Environment::upPressed = true;
if (audioPlayer) {
audioPlayer->playSound("Звук-Идут-по-земле.ogg");
}
audioPlayerAsync.playSoundAsync("Звук-Идут-по-земле.ogg"); // Заменено
if (Environment::violaCurrentAnimation == 0) {
Environment::violaCurrentAnimation = 1;
Environment::violaLastWalkFrame = -1;
@ -284,9 +286,7 @@ void GameObjectManager::handleEvent(const SDL_Event& event) {
case SDLK_DOWN:
case SDLK_s:
Environment::downPressed = true;
if (audioPlayer) {
audioPlayer->playSound("Звук-Идут-по-земле.ogg");
}
audioPlayerAsync.playSoundAsync("Звук-Идут-по-земле.ogg"); // Заменено
if (Environment::violaCurrentAnimation == 0) {
Environment::violaCurrentAnimation = 1;
Environment::violaLastWalkFrame = -1;

View File

@ -1,7 +1,7 @@
#pragma once
#include "TextureManager.h"
#include "BoneAnimatedModel.h"
#include "cmakeaudioplayer/include/AudioPlayer.hpp"
#include "AudioPlayerAsync.h"
#include <memory>
#include <vector>
#include "ActiveObject.h"
@ -11,6 +11,7 @@
#include <SDL2/SDL.h>
#endif
#include "OpenGlExtensions.h"
#include <thread>
namespace ZL {
@ -48,7 +49,8 @@ public:
std::vector<ZL::ActiveObject> activeObjects;
std::vector<ZL::Room> rooms;
std::unique_ptr<AudioPlayer> audioPlayer;
AudioPlayerAsync audioPlayerAsync;
ZL::VertexDataStruct inventoryIconMesh;
ZL::VertexRenderStruct inventoryIconMeshMutable;
@ -58,6 +60,8 @@ public:
ActiveObjectManager aoMgr;
int objects_in_inventory;
private:
//int animationCounter = 0;
int lastMouseX = 0; // Добавляем переменные для хранения позиции мыши

View File

@ -27,7 +27,7 @@ namespace ZL
}
InventoryItem* GetItemByName(const std::string name)
InventoryItem* GetItemByName(const std::string& name)
{
// Пытаемся найти элемент по ключу
auto it = gInventoryMap.find(name);

View File

@ -32,7 +32,7 @@ namespace ZL
// Поиск предмета по индексу (возвращает указатель или nullptr)
InventoryItem* GetItemByHotkey(int hot_key);
InventoryItem* GetItemSelected(bool isSelected);
InventoryItem* GetItemByName(std::string name);
InventoryItem* GetItemByName(const std::string& name);
// Вывести весь инвентарь в консоль
void PrintInventory();

View File

@ -217,7 +217,10 @@ void RenderSystem::drawUI(const GameObjectManager& gameObjects) {
if (ao->activeObjectScreenTexturePtr) {
std::cout << "Found activeObjectScreenTexturePtr" << std::endl;
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);
renderer.PushMatrix();
// Здесь можно использовать вычисленные screenX, screenY,

View File

@ -146,6 +146,7 @@
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="AudioPlayerAsync.cpp" />
<ClCompile Include="BoneAnimatedModel.cpp" />
<ClCompile Include="Environment.cpp" />
<ClCompile Include="Game.cpp" />
@ -170,6 +171,7 @@
<ItemGroup>
<ClInclude Include="ActiveObject.h" />
<ClInclude Include="AnimatedModel.h" />
<ClInclude Include="AudioPlayerAsync.h" />
<ClInclude Include="BoneAnimatedModel.h" />
<ClInclude Include="Environment.h" />
<ClInclude Include="Game.h" />

View File

@ -75,6 +75,9 @@
<ClCompile Include="QuestScripts.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="AudioPlayerAsync.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="TextureManager.h">
@ -140,5 +143,8 @@
<ClInclude Include="QuestScripts.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="AudioPlayerAsync.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project>

View File

@ -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 \
ObjLoader.cpp cmakeaudioplayer/src/AudioPlayer.cpp TextModel.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 \
$(pkg-config --cflags --libs sdl2 gl) \
$(pkg-config --cflags --libs vorbis vorbisfile ogg) \