Add localization, add plays sound when alert starting and pictures

This commit is contained in:
Dima yawaflua Andreev
2024-10-29 21:39:20 +02:00
parent 1e3e52c4cc
commit 27632fa5c6
10 changed files with 471 additions and 65 deletions

45
utils/audio_play.cpp Normal file
View File

@@ -0,0 +1,45 @@
#include <SDL2/SDL.h>
#include <iostream>
#include <SDL2/SDL_mixer.h>
#include <thread>
namespace tzeva_adom {
inline std::pmr::string filename;
// Асинхронная функция для воспроизведения аудио
inline void playAudio() {
if (SDL_Init(SDL_INIT_AUDIO) < 0) {
std::cerr << "Ошибка инициализации SDL: " << SDL_GetError() << std::endl;
return;
}
if (Mix_OpenAudio(44100, MIX_DEFAULT_FORMAT, 2, 2048) < 0) {
std::cerr << "Ошибка открытия аудио: " << Mix_GetError() << std::endl;
SDL_Quit();
return;
}
Mix_Music* music = Mix_LoadMUS(filename.c_str());
if (!music) {
std::cerr << "Ошибка загрузки MP3: " << Mix_GetError() << std::endl;
} else {
Mix_PlayMusic(music, 1);
// Ожидание завершения воспроизведения
while (Mix_PlayingMusic() != 0) {
SDL_Delay(100); // Пауза для проверки состояния воспроизведения
}
Mix_FreeMusic(music);
}
Mix_CloseAudio();
SDL_Quit();
}
// Функция для запуска воспроизведения аудио в отдельном потоке
inline void playAudioAsync(std::pmr::string file) {
filename = file;
std::thread audioThread(playAudio);
audioThread.detach(); // Отделяем поток, чтобы он работал асинхронно
}
}

View File

@@ -0,0 +1,34 @@
//
// Created by yawaflua on 29/10/2024.
//
#pragma once
#include <iostream>
#include <fstream>
#include <curl/curl.h>
namespace tzeva_adom {
inline size_t writeImageData(void* ptr, size_t size, size_t nmemb, FILE* stream) {
size_t written = fwrite(ptr, size, nmemb, stream);
return written;
}
inline void download_file(std::string url, std::string filename) {
CURL* curl = curl_easy_init();
if (curl) {
FILE* fp = fopen(filename.c_str(), "wb");
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writeImageData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
CURLcode res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
fclose(fp);
if (res != CURLE_OK) {
std::cout << "Failed to download image: " << curl_easy_strerror(res) << std::endl;
} else {
std::cout << "Image downloaded successfully: " << filename << std::endl;
}
}
}
}

View File

@@ -0,0 +1,58 @@
#ifndef LOCALIZATION_MANAGER_H
#define LOCALIZATION_MANAGER_H
#include <string>
#include <unordered_map>
#include <fstream>
#include <iostream>
#include <nlohmann/json.hpp> // Подключение библиотеки JSON
namespace tzeva_adom {
class LocalizationManager {
public:
LocalizationManager() : currentLanguage("en") {}
// Загрузить языковой файл
bool loadLanguage(std::string lang) {
std::string path = std::filesystem::current_path().c_str() + fmt::format("/lang/{}.json", lang);
std::ifstream file(path);
if (!file.is_open()) {
std::cerr << "Failed to open localization file: " << lang << std::endl;
std::cerr << "Failed to open localization file: " << path << std::endl;
return false;
}
nlohmann::json json;
file >> json;
translations[lang] = json;
return true;
}
// Установить текущий язык
void setCurrentLanguage(const std::string& langCode) {
loadLanguage(langCode);
if (translations.find(langCode) != translations.end()) {
currentLanguage = langCode;
} else {
std::cerr << "Language code not loaded: " << langCode << std::endl;
}
}
// Получить переведенную строку по ключу
std::string getString(const std::string& key) const {
if (!translations.find(currentLanguage)->second.empty()) {
auto& langData = translations.at(currentLanguage);
if (langData.contains(key)) {
return langData[key];
}
}
return "Translation not found";
}
private:
std::unordered_map<std::string, nlohmann::json> translations;
std::string currentLanguage;
};
}
#endif // LOCALIZATION_MANAGER_H