remove desktop build (for now)
Signed-off-by: Peter Siegmund <developer@mars3142.org>
This commit is contained in:
@@ -1,106 +1,5 @@
|
||||
cmake_minimum_required(VERSION 3.30)
|
||||
|
||||
if (DEFINED ENV{IDF_PATH})
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(system_control)
|
||||
return()
|
||||
else ()
|
||||
set(MAJOR_VERSION 0)
|
||||
set(MINOR_VERSION 0)
|
||||
set(MICRO_VERSION 1)
|
||||
|
||||
project(
|
||||
SystemControl
|
||||
LANGUAGES CXX C
|
||||
VERSION "${MAJOR_VERSION}.${MINOR_VERSION}.${MICRO_VERSION}"
|
||||
)
|
||||
|
||||
set(CMAKE_C_STANDARD 23)
|
||||
set(CMAKE_CXX_STANDARD 23)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED YES)
|
||||
set(CMAKE_CXX_EXTENSIONS YES)
|
||||
|
||||
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
|
||||
set(CMAKE_INCLUDE_PATH include)
|
||||
set(CMAKE_SOURCE_DIR src)
|
||||
set(CMAKE_MODULE_PATH cmake)
|
||||
|
||||
include(FetchContent)
|
||||
function(include_dependency libName gitURL gitTag)
|
||||
FetchContent_Declare(${libName}
|
||||
GIT_REPOSITORY ${gitURL}
|
||||
GIT_TAG ${gitTag}
|
||||
GIT_SHALLOW TRUE
|
||||
GIT_PROGRESS FALSE
|
||||
)
|
||||
FetchContent_MakeAvailable(${libName})
|
||||
endfunction()
|
||||
|
||||
include_dependency(SDL3 https://github.com/libsdl-org/SDL release-3.2.x)
|
||||
include_dependency(SDL_image https://github.com/libsdl-org/SDL_image release-3.2.x)
|
||||
include_dependency(SDL_ttf https://github.com/libsdl-org/SDL_ttf release-3.2.x)
|
||||
include_dependency(u8g2 https://github.com/olikraus/u8g2 master)
|
||||
|
||||
add_subdirectory(components)
|
||||
|
||||
configure_file(
|
||||
"${CMAKE_SOURCE_DIR}/Version.h.in"
|
||||
"${CMAKE_SOURCE_DIR}/Version.h"
|
||||
)
|
||||
|
||||
include_directories(
|
||||
${CMAKE_INCLUDE_PATH}
|
||||
${CMAKE_SOURCE_DIR}
|
||||
${PROJECT_BINARY_DIR}/src
|
||||
)
|
||||
|
||||
add_executable(${PROJECT_NAME}
|
||||
WIN32 MACOSX_BUNDLE
|
||||
${CMAKE_SOURCE_DIR}/main.cpp
|
||||
${CMAKE_SOURCE_DIR}/Common.cpp
|
||||
${CMAKE_SOURCE_DIR}/debug/debug_overlay.cpp
|
||||
${CMAKE_SOURCE_DIR}/hal/u8x8_hal_sdl.cpp
|
||||
${CMAKE_SOURCE_DIR}/manager/ResourceManager.cpp
|
||||
${CMAKE_SOURCE_DIR}/model/AppContext.cpp
|
||||
${CMAKE_SOURCE_DIR}/model/Window.cpp
|
||||
${CMAKE_SOURCE_DIR}/ui/Device.cpp
|
||||
${CMAKE_SOURCE_DIR}/ui/UIWidget.cpp
|
||||
${CMAKE_SOURCE_DIR}/ui/widgets/Button.cpp
|
||||
${CMAKE_SOURCE_DIR}/ui/widgets/D_Pad.cpp
|
||||
)
|
||||
|
||||
if (APPLE)
|
||||
# copy assets to bundle directory
|
||||
set(ASSETS_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/assets)
|
||||
set(ASSETS_DEST_DIR $<TARGET_BUNDLE_DIR:${PROJECT_NAME}>/Contents/Resources)
|
||||
|
||||
if (EXISTS ${ASSETS_SRC_DIR})
|
||||
add_custom_command(
|
||||
TARGET ${PROJECT_NAME} POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory ${ASSETS_SRC_DIR} ${ASSETS_DEST_DIR}
|
||||
COMMENT "Copying assets to bundle via custom command"
|
||||
VERBATIM
|
||||
)
|
||||
else ()
|
||||
message(WARNING "Assets source directory ${ASSETS_SRC_DIR} does not exist. Skipping custom command.")
|
||||
endif ()
|
||||
elseif (WINDOWS)
|
||||
add_custom_command(
|
||||
TARGET ${PROJECT_NAME} POST_BUILD
|
||||
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||
${PROJECT_SOURCE_DIR}/assets ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/assets
|
||||
)
|
||||
endif ()
|
||||
target_compile_definitions(${PROJECT_NAME} PRIVATE SDL_MAIN_USE_CALLBACKS)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||
ImGui
|
||||
insa
|
||||
led-manager
|
||||
persistence-manager
|
||||
SDL3::SDL3
|
||||
SDL3_image::SDL3_image
|
||||
SDL3_ttf::SDL3_ttf
|
||||
u8g2
|
||||
)
|
||||
endif ()
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(system_control)
|
||||
return()
|
||||
|
@@ -1,10 +0,0 @@
|
||||
if (NOT DEFINED ENV{IDF_PATH})
|
||||
add_library(components INTERFACE)
|
||||
|
||||
add_subdirectory(imgui)
|
||||
add_subdirectory(insa)
|
||||
add_subdirectory(led-manager)
|
||||
add_subdirectory(persistence-manager)
|
||||
|
||||
target_link_libraries(components INTERFACE ImGui)
|
||||
endif ()
|
@@ -1,10 +1,7 @@
|
||||
if (DEFINED ENV{IDF_PATH})
|
||||
idf_component_register(SRCS
|
||||
src/ble_manager.c
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES
|
||||
bt
|
||||
nvs_flash
|
||||
)
|
||||
return()
|
||||
endif ()
|
||||
idf_component_register(SRCS
|
||||
src/ble_manager.c
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES
|
||||
bt
|
||||
nvs_flash
|
||||
)
|
@@ -1,44 +1,19 @@
|
||||
# Define the source files in a variable
|
||||
set(SOURCE_FILES
|
||||
src/common/InactivityTracker.cpp
|
||||
src/common/Menu.cpp
|
||||
src/common/ScrollBar.cpp
|
||||
src/common/Widget.cpp
|
||||
src/data/MenuItem.cpp
|
||||
src/ui/LightMenu.cpp
|
||||
src/ui/LightSettingsMenu.cpp
|
||||
src/ui/MainMenu.cpp
|
||||
src/ui/ClockScreenSaver.cpp
|
||||
src/ui/ScreenSaver.cpp
|
||||
src/ui/SettingsMenu.cpp
|
||||
src/ui/SplashScreen.cpp
|
||||
idf_component_register(SRCS
|
||||
src/common/InactivityTracker.cpp
|
||||
src/common/Menu.cpp
|
||||
src/common/ScrollBar.cpp
|
||||
src/common/Widget.cpp
|
||||
src/data/MenuItem.cpp
|
||||
src/ui/LightMenu.cpp
|
||||
src/ui/LightSettingsMenu.cpp
|
||||
src/ui/MainMenu.cpp
|
||||
src/ui/ClockScreenSaver.cpp
|
||||
src/ui/ScreenSaver.cpp
|
||||
src/ui/SettingsMenu.cpp
|
||||
src/ui/SplashScreen.cpp
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES
|
||||
u8g2
|
||||
led-manager
|
||||
persistence-manager
|
||||
)
|
||||
|
||||
if (DEFINED ENV{IDF_PATH})
|
||||
idf_component_register(SRCS
|
||||
${SOURCE_FILES}
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES
|
||||
u8g2
|
||||
led-manager
|
||||
persistence-manager
|
||||
)
|
||||
return()
|
||||
endif ()
|
||||
|
||||
cmake_minimum_required(VERSION 3.30)
|
||||
project(insa)
|
||||
|
||||
add_library(${PROJECT_NAME} STATIC
|
||||
${SOURCE_FILES}
|
||||
)
|
||||
|
||||
include_directories(include)
|
||||
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC include)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||
u8g2
|
||||
led-manager
|
||||
persistence-manager
|
||||
)
|
@@ -1,30 +1,10 @@
|
||||
if (DEFINED ENV{IDF_PATH})
|
||||
idf_component_register(SRCS
|
||||
src/hal_esp32/led_manager.cpp
|
||||
src/hal_esp32/led_status.cpp
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES
|
||||
u8g2
|
||||
esp_event
|
||||
esp_timer
|
||||
persistence-manager
|
||||
)
|
||||
return()
|
||||
endif ()
|
||||
|
||||
cmake_minimum_required(VERSION 3.30)
|
||||
project(led-manager)
|
||||
|
||||
add_library(${PROJECT_NAME} STATIC
|
||||
src/hal_native/led_manager.cpp
|
||||
src/hal_native/Matrix.cpp
|
||||
)
|
||||
|
||||
include_directories(include)
|
||||
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC include)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||
SDL3::SDL3
|
||||
persistence-manager
|
||||
idf_component_register(SRCS
|
||||
src/led_manager.cpp
|
||||
src/led_status.cpp
|
||||
INCLUDE_DIRS "include"
|
||||
PRIV_REQUIRES
|
||||
u8g2
|
||||
esp_event
|
||||
esp_timer
|
||||
persistence-manager
|
||||
)
|
||||
|
@@ -1,64 +0,0 @@
|
||||
#include "Matrix.h"
|
||||
|
||||
#include "SDL3/SDL.h"
|
||||
|
||||
Matrix::Matrix(uint32_t windowID, SDL_Renderer *renderer, const uint8_t cols, const uint8_t rows)
|
||||
: m_windowId(windowID), m_renderer(renderer), m_cols(cols), m_rows(rows)
|
||||
{
|
||||
}
|
||||
|
||||
SDL_Renderer *Matrix::renderer() const
|
||||
{
|
||||
return m_renderer;
|
||||
}
|
||||
|
||||
SDL_WindowID Matrix::windowId() const
|
||||
{
|
||||
return m_windowId;
|
||||
}
|
||||
|
||||
void Matrix::DrawColoredGrid() const
|
||||
{
|
||||
int i = 0;
|
||||
for (int w = 0; w < m_cols; w++)
|
||||
{
|
||||
const auto phase = w % (2 * m_rows);
|
||||
|
||||
for (int h_raw = 0; h_raw < m_rows; h_raw++)
|
||||
{
|
||||
int h;
|
||||
if (phase < m_rows)
|
||||
{
|
||||
h = h_raw;
|
||||
}
|
||||
else
|
||||
{
|
||||
h = m_rows - 1 - h_raw;
|
||||
}
|
||||
|
||||
constexpr auto rectSize = cellSize - 2.0f * spacing;
|
||||
|
||||
const auto x = static_cast<float>(w) * cellSize + spacing;
|
||||
const auto y = static_cast<float>(h) * cellSize + spacing;
|
||||
|
||||
auto rect = SDL_FRect{x, y, rectSize, rectSize};
|
||||
|
||||
i++;
|
||||
const auto red = static_cast<Uint8>(static_cast<float>(i) * 255.0f);
|
||||
const auto green = static_cast<Uint8>(static_cast<float>(i) * 255.0f);
|
||||
const auto blue = static_cast<Uint8>(static_cast<float>(i) * 255.0f);
|
||||
SDL_SetRenderDrawColor(m_renderer, red, green, blue, 255);
|
||||
SDL_RenderFillRect(m_renderer, &rect);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Matrix::Render() const
|
||||
{
|
||||
SDL_SetRenderDrawColor(m_renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(m_renderer);
|
||||
|
||||
DrawColoredGrid();
|
||||
|
||||
SDL_RenderPresent(m_renderer);
|
||||
}
|
@@ -1,16 +0,0 @@
|
||||
#include "led_manager.h"
|
||||
|
||||
uint64_t wled_init(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t register_handler(void)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint64_t send_event(uint32_t event, led_event_data_t *payload)
|
||||
{
|
||||
return 0;
|
||||
}
|
@@ -1,7 +0,0 @@
|
||||
#include "led_status.h"
|
||||
|
||||
void led_status_init()
|
||||
{
|
||||
// This function is intentionally left empty for the native implementation.
|
||||
// It serves as a placeholder to maintain compatibility with the HAL interface.
|
||||
}
|
@@ -20,7 +20,7 @@ uint64_t wled_init(void)
|
||||
{
|
||||
led_strip_config_t strip_config = {
|
||||
.strip_gpio_num = CONFIG_WLED_DIN_PIN,
|
||||
.max_leds = 64,
|
||||
.max_leds = 500,
|
||||
.led_model = LED_MODEL_WS2812,
|
||||
.color_component_format = LED_STRIP_COLOR_COMPONENT_FMT_GRB,
|
||||
.flags =
|
||||
@@ -55,7 +55,7 @@ void event_handler(void *arg, esp_event_base_t base, int32_t id, void *event_dat
|
||||
if (id == EVENT_LED_ON || id == EVENT_LED_OFF)
|
||||
{
|
||||
auto brightness = (id == EVENT_LED_ON) ? value : 0;
|
||||
for (uint32_t i = 0; i < 64; i++)
|
||||
for (uint32_t i = 0; i < 500; i++)
|
||||
{
|
||||
led_strip_set_pixel(led_strip, i, brightness, brightness, brightness);
|
||||
}
|
@@ -1,24 +1,6 @@
|
||||
if (DEFINED ENV{IDF_PATH})
|
||||
idf_component_register(SRCS
|
||||
src/hal_esp32/PersistenceManager.cpp
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES
|
||||
nvs_flash
|
||||
)
|
||||
return()
|
||||
endif ()
|
||||
|
||||
cmake_minimum_required(VERSION 3.30)
|
||||
project(persistence-manager)
|
||||
|
||||
add_library(${PROJECT_NAME} STATIC
|
||||
src/hal_native/PersistenceManager.cpp
|
||||
)
|
||||
|
||||
include_directories(include)
|
||||
|
||||
target_include_directories(${PROJECT_NAME} PUBLIC include)
|
||||
|
||||
target_link_libraries(${PROJECT_NAME} PRIVATE
|
||||
SDL3::SDL3
|
||||
idf_component_register(SRCS
|
||||
src/PersistenceManager.cpp
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES
|
||||
nvs_flash
|
||||
)
|
||||
|
@@ -1,338 +0,0 @@
|
||||
#include "hal_native/PersistenceManager.h"
|
||||
#include <SDL3/SDL.h>
|
||||
|
||||
#include <utility>
|
||||
|
||||
PersistenceManager::PersistenceManager(std::string filename) : m_filename(std::move(filename))
|
||||
{
|
||||
if (!SDL_WasInit(SDL_INIT_EVENTS))
|
||||
{
|
||||
SDL_Init(SDL_INIT_EVENTS);
|
||||
}
|
||||
|
||||
Load();
|
||||
}
|
||||
|
||||
PersistenceManager::~PersistenceManager()
|
||||
{
|
||||
Save();
|
||||
}
|
||||
|
||||
bool PersistenceManager::HasKey(const std::string &key) const
|
||||
{
|
||||
return m_data.contains(key);
|
||||
}
|
||||
|
||||
void PersistenceManager::RemoveKey(const std::string &key)
|
||||
{
|
||||
m_data.erase(key);
|
||||
}
|
||||
|
||||
void PersistenceManager::Clear()
|
||||
{
|
||||
m_data.clear();
|
||||
}
|
||||
|
||||
bool PersistenceManager::Save()
|
||||
{
|
||||
return SaveToFile(m_filename);
|
||||
}
|
||||
|
||||
bool PersistenceManager::Load()
|
||||
{
|
||||
return LoadFromFile(m_filename);
|
||||
}
|
||||
|
||||
bool PersistenceManager::SaveToFile(const std::string &filename)
|
||||
{
|
||||
SDL_IOStream *stream = SDL_IOFromFile(filename.c_str(), "wb");
|
||||
if (!stream)
|
||||
{
|
||||
SDL_Log("Error opening file for writing: %s", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t count = m_data.size();
|
||||
if (SDL_WriteIO(stream, &count, sizeof(count)) != sizeof(count))
|
||||
{
|
||||
SDL_Log("Error writing count: %s", SDL_GetError());
|
||||
SDL_CloseIO(stream);
|
||||
return false;
|
||||
}
|
||||
|
||||
for (const auto &[key, value] : m_data)
|
||||
{
|
||||
size_t keyLength = key.length();
|
||||
if (SDL_WriteIO(stream, &keyLength, sizeof(keyLength)) != sizeof(keyLength))
|
||||
{
|
||||
SDL_Log("Error writing key length: %s", SDL_GetError());
|
||||
SDL_CloseIO(stream);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (SDL_WriteIO(stream, key.c_str(), keyLength) != keyLength)
|
||||
{
|
||||
SDL_Log("Error writing key: %s", SDL_GetError());
|
||||
SDL_CloseIO(stream);
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!WriteValueToStream(stream, value))
|
||||
{
|
||||
SDL_CloseIO(stream);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_CloseIO(stream);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PersistenceManager::LoadFromFile(const std::string &filename)
|
||||
{
|
||||
SDL_IOStream *stream = SDL_IOFromFile(filename.c_str(), "rb");
|
||||
if (!stream)
|
||||
{
|
||||
SDL_Log("File not found or error opening: %s", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
m_data.clear();
|
||||
|
||||
size_t count;
|
||||
if (SDL_ReadIO(stream, &count, sizeof(count)) != sizeof(count))
|
||||
{
|
||||
SDL_Log("Error reading count: %s", SDL_GetError());
|
||||
SDL_CloseIO(stream);
|
||||
return false;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < count; ++i)
|
||||
{
|
||||
size_t keyLength;
|
||||
if (SDL_ReadIO(stream, &keyLength, sizeof(keyLength)) != sizeof(keyLength))
|
||||
{
|
||||
SDL_Log("Error reading key length: %s", SDL_GetError());
|
||||
SDL_CloseIO(stream);
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string key(keyLength, '\0');
|
||||
if (SDL_ReadIO(stream, key.data(), keyLength) != keyLength)
|
||||
{
|
||||
SDL_Log("Error reading key: %s", SDL_GetError());
|
||||
SDL_CloseIO(stream);
|
||||
return false;
|
||||
}
|
||||
|
||||
ValueType value;
|
||||
if (!ReadValueFromStream(stream, value))
|
||||
{
|
||||
SDL_CloseIO(stream);
|
||||
return false;
|
||||
}
|
||||
|
||||
m_data[key] = value;
|
||||
}
|
||||
|
||||
SDL_CloseIO(stream);
|
||||
return true;
|
||||
}
|
||||
|
||||
void PersistenceManager::SetValueImpl(const std::string &key, bool value)
|
||||
{
|
||||
m_data[key] = value;
|
||||
}
|
||||
|
||||
void PersistenceManager::SetValueImpl(const std::string &key, int value)
|
||||
{
|
||||
m_data[key] = value;
|
||||
}
|
||||
|
||||
void PersistenceManager::SetValueImpl(const std::string &key, float value)
|
||||
{
|
||||
m_data[key] = value;
|
||||
}
|
||||
|
||||
void PersistenceManager::SetValueImpl(const std::string &key, double value)
|
||||
{
|
||||
m_data[key] = value;
|
||||
}
|
||||
|
||||
void PersistenceManager::SetValueImpl(const std::string &key, const std::string &value)
|
||||
{
|
||||
m_data[key] = value;
|
||||
}
|
||||
|
||||
bool PersistenceManager::GetValueImpl(const std::string &key, bool defaultValue) const
|
||||
{
|
||||
if (const auto it = m_data.find(key); it != m_data.end() && std::holds_alternative<bool>(it->second))
|
||||
{
|
||||
return std::get<bool>(it->second);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
int PersistenceManager::GetValueImpl(const std::string &key, int defaultValue) const
|
||||
{
|
||||
if (const auto it = m_data.find(key); it != m_data.end() && std::holds_alternative<int>(it->second))
|
||||
{
|
||||
return std::get<int>(it->second);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
float PersistenceManager::GetValueImpl(const std::string &key, float defaultValue) const
|
||||
{
|
||||
if (const auto it = m_data.find(key); it != m_data.end() && std::holds_alternative<float>(it->second))
|
||||
{
|
||||
return std::get<float>(it->second);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
double PersistenceManager::GetValueImpl(const std::string &key, double defaultValue) const
|
||||
{
|
||||
if (const auto it = m_data.find(key); it != m_data.end() && std::holds_alternative<double>(it->second))
|
||||
{
|
||||
return std::get<double>(it->second);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
std::string PersistenceManager::GetValueImpl(const std::string &key, const std::string &defaultValue) const
|
||||
{
|
||||
if (const auto it = m_data.find(key); it != m_data.end() && std::holds_alternative<std::string>(it->second))
|
||||
{
|
||||
return std::get<std::string>(it->second);
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
bool PersistenceManager::WriteValueToStream(SDL_IOStream *stream, const ValueType &value)
|
||||
{
|
||||
const TypeId typeId = GetTypeId(value);
|
||||
|
||||
if (SDL_WriteIO(stream, &typeId, sizeof(typeId)) != sizeof(typeId))
|
||||
{
|
||||
SDL_Log("Error writing type ID: %s", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (typeId)
|
||||
{
|
||||
case TypeId::BOOL: {
|
||||
const bool val = std::get<bool>(value);
|
||||
return SDL_WriteIO(stream, &val, sizeof(val)) == sizeof(val);
|
||||
}
|
||||
case TypeId::INT: {
|
||||
const int val = std::get<int>(value);
|
||||
return SDL_WriteIO(stream, &val, sizeof(val)) == sizeof(val);
|
||||
}
|
||||
case TypeId::FLOAT: {
|
||||
const float val = std::get<float>(value);
|
||||
return SDL_WriteIO(stream, &val, sizeof(val)) == sizeof(val);
|
||||
}
|
||||
case TypeId::DOUBLE: {
|
||||
const double val = std::get<double>(value);
|
||||
return SDL_WriteIO(stream, &val, sizeof(val)) == sizeof(val);
|
||||
}
|
||||
case TypeId::STRING: {
|
||||
const auto &str = std::get<std::string>(value);
|
||||
const size_t length = str.length();
|
||||
|
||||
if (SDL_WriteIO(stream, &length, sizeof(length)) != sizeof(length))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return SDL_WriteIO(stream, str.c_str(), length) == length;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool PersistenceManager::ReadValueFromStream(SDL_IOStream *stream, ValueType &value)
|
||||
{
|
||||
TypeId typeId;
|
||||
|
||||
if (SDL_ReadIO(stream, &typeId, sizeof(typeId)) != sizeof(typeId))
|
||||
{
|
||||
SDL_Log("Error reading type ID: %s", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
switch (typeId)
|
||||
{
|
||||
case TypeId::BOOL: {
|
||||
bool val;
|
||||
if (SDL_ReadIO(stream, &val, sizeof(val)) == sizeof(val))
|
||||
{
|
||||
value = val;
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case TypeId::INT: {
|
||||
int val;
|
||||
if (SDL_ReadIO(stream, &val, sizeof(val)) == sizeof(val))
|
||||
{
|
||||
value = val;
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case TypeId::FLOAT: {
|
||||
float val;
|
||||
if (SDL_ReadIO(stream, &val, sizeof(val)) == sizeof(val))
|
||||
{
|
||||
value = val;
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case TypeId::DOUBLE: {
|
||||
double val;
|
||||
if (SDL_ReadIO(stream, &val, sizeof(val)) == sizeof(val))
|
||||
{
|
||||
value = val;
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case TypeId::STRING: {
|
||||
size_t length;
|
||||
if (SDL_ReadIO(stream, &length, sizeof(length)) != sizeof(length))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string str(length, '\0');
|
||||
if (SDL_ReadIO(stream, str.data(), length) == length)
|
||||
{
|
||||
value = str;
|
||||
return true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
SDL_Log("Error reading value: %s", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
PersistenceManager::TypeId PersistenceManager::GetTypeId(const ValueType &value)
|
||||
{
|
||||
if (std::holds_alternative<bool>(value))
|
||||
return TypeId::BOOL;
|
||||
if (std::holds_alternative<int>(value))
|
||||
return TypeId::INT;
|
||||
if (std::holds_alternative<float>(value))
|
||||
return TypeId::FLOAT;
|
||||
if (std::holds_alternative<double>(value))
|
||||
return TypeId::DOUBLE;
|
||||
if (std::holds_alternative<std::string>(value))
|
||||
return TypeId::STRING;
|
||||
|
||||
return TypeId::BOOL;
|
||||
}
|
Reference in New Issue
Block a user