16
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-07-22 23:19:19 +02:00

refactor: create paths namespace instead of global variables

This commit is contained in:
jerrrrycho
2026-03-15 11:55:30 +01:00
parent 383788b30a
commit 9f43dca48b
20 changed files with 112 additions and 100 deletions

View File

@@ -23,18 +23,25 @@ http://mozilla.org/MPL/2.0/.
#include "utilities/Timer.h"
#include "vao.h"
void
global_settings::LoadIniFile(std::string asFileName) {
void global_settings::LoadIniFile(std::string asFileName)
{
// initialize season data in case the main config file doesn't
std::time_t timenow = std::time(nullptr);
// initialize season data in case the main config file doesn't
std::time_t timenow = std::time( 0 );
std::tm *localtime = std::localtime( &timenow );
fMoveLight = localtime->tm_yday + 1; // numer bieżącego dnia w roku
simulation::Environment.compute_season( fMoveLight );
std::tm tm{};
cParser parser(asFileName, cParser::buffer_FILE);
ConfigParse(parser);
};
#ifdef _WIN32
localtime_s(&tm, &timenow);
#else
localtime_r(&timenow, &tm);
#endif
fMoveLight = tm.tm_yday + 1; // numer bieżącego dnia w roku
simulation::Environment.compute_season(fMoveLight);
cParser parser(asFileName, cParser::buffer_FILE);
ConfigParse(parser);
}
void
global_settings::ConfigParse(cParser &Parser) {

View File

@@ -65,7 +65,7 @@ struct global_settings {
int iPause{ 0 }; // globalna pauza ruchu: b0=start,b1=klawisz,b2=tło,b3=lagi,b4=wczytywanie
float AirTemperature{ 15.f };
std::string asCurrentSceneryPath{ "scenery/" };
std::string asCurrentTexturePath{ szTexturePath };
std::string asCurrentTexturePath{ paths::textures };
std::string asCurrentDynamicPath;
int CurrentMaxTextureSize{ 4096 };
bool UpdateMaterials{ true };

View File

@@ -158,55 +158,57 @@ void LogService()
}
void WriteLog(const char *str, logtype const Type, bool isError)
bool ShouldSkipLog(std::string_view str, logtype type)
{
if (!str || *str == '\0')
return;
if (TestFlag(Global.DisabledLogTypes, static_cast<unsigned int>(Type)))
return;
// time calculation
auto now = std::chrono::steady_clock::now();
auto elapsed = now - Global.startTimestamp;
double seconds = std::chrono::duration_cast<std::chrono::duration<double>>(elapsed).count();
// time format
std::ostringstream oss;
oss << "[ " << std::fixed << std::setprecision(3) << seconds << " ] ";
// wyrownanie do np. 10 znaków długości + dwie tabulacje
std::ostringstream final;
final << std::setw(10) << oss.str() << "\t\t" << str;
logMutex.lock();
InfoStack.emplace_back(final.str(), isError);
logMutex.unlock();
return str.empty() ||
TestFlag(Global.DisabledLogTypes, static_cast<unsigned int>(type));
}
void ErrorLog(const char *str, logtype const Type)
std::string FormatLogMessage(std::string_view str)
{
if (!str || *str == '\0')
return;
if (TestFlag(Global.DisabledLogTypes, static_cast<unsigned int>(Type)))
const auto now = std::chrono::steady_clock::now();
const auto elapsed = now - Global.startTimestamp;
const double seconds = std::chrono::duration<double>(elapsed).count();
return std::format("[ {:8.3f} ]\t\t{}", seconds, str);
}
void WriteLog(std::string_view str, logtype type, bool isError)
{
if (ShouldSkipLog(str, type))
return;
// time calculation
auto now = std::chrono::steady_clock::now();
auto elapsed = now - Global.startTimestamp;
double seconds = std::chrono::duration_cast<std::chrono::duration<double>>(elapsed).count();
const auto message = FormatLogMessage(str);
// time format
std::ostringstream oss;
oss << "[ " << std::fixed << std::setprecision(3) << seconds << " ] ";
std::lock_guard<std::mutex> lock(logMutex);
InfoStack.push_back({message, isError});
}
// wyrownanie do np. 10 znaków długości + dwie tabulacje
std::ostringstream final;
final << std::setw(10) << oss.str() << "\t\t" << str;
void ErrorLog(std::string_view str, logtype type)
{
if (ShouldSkipLog(str, type))
return;
logMutex.lock();
ErrorStack.emplace_back(final.str());
logMutex.unlock();
const auto message = FormatLogMessage(str);
std::lock_guard<std::mutex> lock(logMutex);
ErrorStack.push_back(message);
}
void WriteLog(const char* str, logtype type, bool isError)
{
if (str == nullptr || *str == '\0')
return;
WriteLog(std::string_view{str}, type, isError);
}
void ErrorLog(const char* str, logtype type)
{
if (str == nullptr || *str == '\0')
return;
ErrorLog(std::string_view{str}, type);
}

View File

@@ -29,12 +29,15 @@ template <typename T> T sign(T x)
#define DegToRad(a) ((M_PI / 180.0) * (a)) //(a) w nawiasie, bo może być dodawaniem
#define RadToDeg(r) ((180.0 / M_PI) * (r))
#define szSceneryPath "scenery/"
#define szTexturePath "textures/"
#define szModelPath "models/"
#define szDynamicPath "dynamic/"
#define szSoundPath "sounds/"
#define szDataPath "data/"
namespace paths
{
inline constexpr const char *scenery = "scenery/";
inline constexpr const char *textures = "textures/";
inline constexpr const char *models = "models/";
inline constexpr const char *dynamic = "dynamic/";
inline constexpr const char *sounds = "sounds/";
inline constexpr const char *data = "data/";
}
#define MAKE_ID4(a, b, c, d) (((std::uint32_t)(d) << 24) | ((std::uint32_t)(c) << 16) | ((std::uint32_t)(b) << 8) | (std::uint32_t)(a))

69
utilities/uuid.hpp Normal file
View File

@@ -0,0 +1,69 @@
#pragma once
#include <array>
#include <random>
#include <sstream>
#include <iomanip>
#include <string>
#include <algorithm>
#include <cstdint>
class UID {
public:
std::array<uint8_t,16> bytes;
static UID random() {
static thread_local std::mt19937_64 gen(std::random_device{}());
UID u;
uint64_t a = gen();
uint64_t b = gen();
for (int i = 0; i < 8; ++i) u.bytes[i] = uint8_t((a >> (i*8)) & 0xFF);
for (int i = 0; i < 8; ++i) u.bytes[8 + i] = uint8_t((b >> (i*8)) & 0xFF);
u.bytes[6] = (u.bytes[6] & 0x0F) | 0x40;
u.bytes[8] = (u.bytes[8] & 0x3F) | 0x80;
return u;
}
std::string to_string() const {
std::ostringstream os;
os << std::hex << std::setfill('0');
auto put = [&](int i){
os << std::setw(2) << static_cast<int>(bytes[i]);
};
// format 8-4-4-4-12
for (int i = 0; i < 4; ++i) put(i);
for (int i = 4; i < 6; ++i) put(i);
os << '-';
for (int i = 6; i < 8; ++i) put(i);
os << '-';
for (int i = 8; i < 10; ++i) put(i);
os << '-';
for (int i = 10; i < 12; ++i) put(i);
os << '-';
for (int i = 12; i < 16; ++i) put(i);
return os.str();
}
static UID from_string(const std::string& str) {
std::istringstream is(str);
is >> std::hex;
UID u;
for (int i = 0; i < 16; ++i) {
int byte;
is >> byte;
u.bytes[i] = static_cast<uint8_t>(byte);
if (is.peek() == '-') is.get();
}
return u;
}
bool operator==(const UID &other) const noexcept {
return bytes == other.bytes;
}
bool operator!=(const UID &other) const noexcept {
return !(*this == other);
}
};