This commit is contained in:
2025-08-15 17:17:11 -04:00
parent f75369f670
commit 8388fd5d99
12 changed files with 97 additions and 29 deletions

View File

@@ -44,6 +44,8 @@ typedef double f64;
using namespace glm; using namespace glm;
#define PREFERENCES_DIRECTORY "anm2ed"
#define ROUND_NEAREST_MULTIPLE(value, multiple) (roundf((value) / (multiple)) * (multiple)) #define ROUND_NEAREST_MULTIPLE(value, multiple) (roundf((value) / (multiple)) * (multiple))
#define FLOAT_TO_U8(x) (static_cast<u8>((x) * 255.0f)) #define FLOAT_TO_U8(x) (static_cast<u8>((x) * 255.0f))
#define U8_TO_FLOAT(x) ((x) / 255.0f) #define U8_TO_FLOAT(x) ((x) / 255.0f)
@@ -98,19 +100,12 @@ static const vec4 COLOR_OPAQUE = {1.0f, 1.0f, 1.0f, 1.0f};
static const vec4 COLOR_TRANSPARENT = {0.0f, 0.0f, 0.0f, 0.0f}; static const vec4 COLOR_TRANSPARENT = {0.0f, 0.0f, 0.0f, 0.0f};
static const vec3 COLOR_OFFSET_NONE = {0.0f, 0.0f, 0.0f}; static const vec3 COLOR_OFFSET_NONE = {0.0f, 0.0f, 0.0f};
static inline void log_error(const std::string& string) static inline std::string preferences_path_get(void)
{ {
std::println("[ERROR] {}", string); char* preferencesPath = SDL_GetPrefPath("", PREFERENCES_DIRECTORY);
} std::string preferencesPathString = preferencesPath;
SDL_free(preferencesPath);
static inline void log_info(const std::string& string) return preferencesPathString;
{
std::println("[INFO] {}", string);
}
static inline void log_warning(const std::string& string)
{
std::println("[WARNING] {}", string);
} }
static inline bool string_to_bool(const std::string& string) static inline bool string_to_bool(const std::string& string)

View File

@@ -1,6 +1,5 @@
#pragma once #pragma once
#include "COMMON.h"
#include "resources.h" #include "resources.h"
#define ANM2_SCALE_CONVERT(x) ((f32)x / 100.0f) #define ANM2_SCALE_CONVERT(x) ((f32)x / 100.0f)

View File

@@ -73,11 +73,11 @@ void dialog_ffmpeg_path_set(Dialog* self)
void dialog_explorer_open(const std::string& path) void dialog_explorer_open(const std::string& path)
{ {
#ifdef _WIN32 #ifdef _WIN32
ShellExecuteA(NULL, "open", path.c_str(), NULL, NULL, SW_SHOWNORMAL); ShellExecuteA(NULL, DIALOG_FILE_EXPLORER_COMMAND, path.c_str(), NULL, NULL, SW_SHOWNORMAL);
#else #else
char cmd[512]; char command[DIALOG_FILE_EXPLORER_COMMAND_SIZE];
snprintf(cmd, sizeof(cmd), "xdg-open \"%s\" &", path.c_str()); snprintf(command, sizeof(command), DIALOG_FILE_EXPLORER_COMMAND, path.c_str());
system(cmd); system(command);
#endif #endif
} }

View File

@@ -3,6 +3,14 @@
#include "render.h" #include "render.h"
#include "window.h" #include "window.h"
#define DIALOG_FILE_EXPLORER_COMMAND_SIZE 512
#ifdef _WIN32
#define DIALOG_FILE_EXPLORER_COMMAND "open"
#else
#define DIALOG_FILE_EXPLORER_COMMAND "xdg-open \"%s\" &"
#endif
const SDL_DialogFileFilter DIALOG_FILE_FILTER_ANM2[] = const SDL_DialogFileFilter DIALOG_FILE_FILTER_ANM2[] =
{ {
{"Anm2 file", "anm2;xml"} {"Anm2 file", "anm2;xml"}

View File

@@ -207,7 +207,7 @@ struct ImguiHotkey
static void imgui_log_push(Imgui* self, const std::string& text) static void imgui_log_push(Imgui* self, const std::string& text)
{ {
self->log.push_back({text, IMGUI_LOG_DURATION}); self->log.push_back({text, IMGUI_LOG_DURATION});
std::println("[IMGUI] {}", text); log_imgui(text);
} }
static std::vector<ImguiHotkey>& imgui_hotkey_registry() static std::vector<ImguiHotkey>& imgui_hotkey_registry()

45
src/log.cpp Normal file
View File

@@ -0,0 +1,45 @@
#include "log.h"
inline std::ofstream logFile;
static void _log_write(const std::string& string)
{
std::println("{}", string);
if (logFile.is_open())
{
logFile << string << '\n';
logFile.flush();
}
}
void log_init(const std::string& file)
{
logFile.open(file, std::ios::out | std::ios::trunc);
if (!logFile) std::println("{}", std::format(LOG_INIT_ERROR, file));
}
void log_error(const std::string& error)
{
_log_write(LOG_ERROR_FORMAT + error);
}
void log_info(const std::string& info)
{
_log_write(LOG_INFO_FORMAT + info);
}
void log_warning(const std::string& warning)
{
_log_write(LOG_WARNING_FORMAT + warning);
}
void log_imgui(const std::string& imgui)
{
_log_write(LOG_IMGUI_FORMAT + imgui);
}
void log_free(void)
{
logFile.close();
}

17
src/log.h Normal file
View File

@@ -0,0 +1,17 @@
#pragma once
#include "COMMON.h"
#define LOG_WARNING_FORMAT "[WARNING] "
#define LOG_ERROR_FORMAT "[ERROR] "
#define LOG_INFO_FORMAT "[INFO] "
#define LOG_IMGUI_FORMAT "[IMGUI] "
#define LOG_INIT_ERROR "[ERROR] Failed to open log file: {}"
#define LOG_PATH "log.txt"
void log_init(const std::string& file);
void log_error(const std::string& error);
void log_info(const std::string& info);
void log_warning(const std::string& warning);
void log_imgui(const std::string& imgui);
void log_free(void);

View File

@@ -14,6 +14,8 @@ main(s32 argc, char* argv[])
{ {
State state; State state;
log_init(preferences_path_get() + LOG_PATH);
if (argc > 0 && argv[1]) if (argc > 0 && argv[1])
{ {
if (std::string(argv[1]) == ARGUMENT_RESCALE) if (std::string(argv[1]) == ARGUMENT_RESCALE)

View File

@@ -74,9 +74,7 @@ static void _settings_setting_load(Settings* self, const std::string& line)
std::string settings_path_get(void) std::string settings_path_get(void)
{ {
char* path = SDL_GetPrefPath("", SETTINGS_FOLDER); std::string filePath = preferences_path_get() + SETTINGS_PATH;
std::string filePath = std::string(path) + SETTINGS_PATH;
SDL_free(path);
return filePath; return filePath;
} }

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "COMMON.h" #include "log.h"
#define SHADER_INFO_LOG_MAX 0xFF #define SHADER_INFO_LOG_MAX 0xFF
#define SHADER_INIT_ERROR "Failed to initialize shader {}:\n{}" #define SHADER_INIT_ERROR "Failed to initialize shader {}:\n{}"

View File

@@ -24,9 +24,6 @@ static void _draw(State* self)
void init(State* self) void init(State* self)
{ {
log_info(STATE_INIT_INFO);
settings_init(&self->settings); settings_init(&self->settings);
if (!SDL_Init(SDL_INIT_VIDEO)) if (!SDL_Init(SDL_INIT_VIDEO))
@@ -79,18 +76,24 @@ void init(State* self)
self->glContext = SDL_GL_CreateContext(self->window); self->glContext = SDL_GL_CreateContext(self->window);
window_vsync_set(self->settings.isVsync);
if (!self->glContext) if (!self->glContext)
{ {
log_error(std::format(STATE_GL_CONTEXT_INIT_ERROR, SDL_GetError())); log_error(std::format(STATE_GL_CONTEXT_INIT_ERROR, SDL_GetError()));
quit(self); quit(self);
} }
glewInit(); GLenum glewError = glewInit();
if (glewError != GLEW_OK)
{
log_error(std::format(STATE_GL_CONTEXT_INIT_ERROR, (const char*)glewGetErrorString(glewError)));
quit(self);
}
log_info(std::format(STATE_GL_CONTEXT_INIT_INFO, (const char*)glGetString(GL_VERSION))); log_info(std::format(STATE_GL_CONTEXT_INIT_INFO, (const char*)glGetString(GL_VERSION)));
window_vsync_set(self->settings.isVsync);
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glLineWidth(STATE_GL_LINE_WIDTH); glLineWidth(STATE_GL_LINE_WIDTH);
@@ -191,4 +194,5 @@ void quit(State* self)
settings_save(&self->settings); settings_save(&self->settings);
log_info(STATE_QUIT_INFO); log_info(STATE_QUIT_INFO);
log_free();
} }

View File

@@ -1,6 +1,6 @@
#pragma once #pragma once
#include "COMMON.h" #include "log.h"
#define TEXTURE_CHANNELS 4 #define TEXTURE_CHANNELS 4
#define TEXTURE_INIT_INFO "Initialized texture from file: {}" #define TEXTURE_INIT_INFO "Initialized texture from file: {}"