mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
refactor: logger (#35)
* refactor(log): convert Logger from namespace to class with static methods - Replace the Logger namespace of free functions with a Logger class containing static methods (info, error, warn, debug). - Simplify log calls by removing explicit source_location parameters; adopt `std::print` for output formatting. - Undefine common macro names (DEBUG, INFO, ERROR, WARN) to avoid collisions. - Update `renderer.cpp` to use `Logger::debug` instead of the old `Logger::log`. - Add missing `#include <source_location>` in `audio_error.hpp`. - Fix early return condition in `WorldScene::set_pause`. * refactor(camera): remove reset_camera method and first-mouse flag * refactor: replace --no-debug with logging options, add assertions, fix typo - Removed `--no-debug` CLI flag and added `--logs-path`, `--log-level`, `--enable-filelog`, `--enable-consolelog`. - Added file logging support with daily rotation, log level threshold, and console/file output control. - Added `m_init` check in `Config::get`/`set` to prevent use before initialization. - Fixed typo in `DebugCollector::destroy` (`distory` → `destory`). - Removed `debug_on` parameter from `Renderer::init`; OpenGL debug output is now unconditionally enabled in debug builds. * feat(debug): enable GL debug context flag in debug builds
This commit is contained in:
@@ -64,6 +64,7 @@ private:
|
|||||||
inline static int fps = 0;
|
inline static int fps = 0;
|
||||||
Argument m_argument;
|
Argument m_argument;
|
||||||
bool m_running = true;
|
bool m_running = true;
|
||||||
|
bool m_opengl_init = false;
|
||||||
void init(int argc, char** argv);
|
void init(int argc, char** argv);
|
||||||
void handle_argument(int argc, char** argv);
|
void handle_argument(int argc, char** argv);
|
||||||
auto init_camera();
|
auto init_camera();
|
||||||
|
|||||||
@@ -6,9 +6,12 @@ struct Argument {
|
|||||||
std::optional<int> port;
|
std::optional<int> port;
|
||||||
std::optional<std::string> ip;
|
std::optional<std::string> ip;
|
||||||
std::optional<std::string> player;
|
std::optional<std::string> player;
|
||||||
std::optional<bool> no_debug;
|
|
||||||
std::optional<std::string> language;
|
std::optional<std::string> language;
|
||||||
std::optional<std::string> video_driver;
|
std::optional<std::string> video_driver;
|
||||||
std::optional<bool> enable_exclusive;
|
std::optional<bool> enable_exclusive;
|
||||||
|
std::optional<std::string> logs_path;
|
||||||
|
std::optional<int> log_level;
|
||||||
|
std::optional<bool> enable_filelog;
|
||||||
|
std::optional<bool> enable_consolelog;
|
||||||
};
|
};
|
||||||
} // namespace Cubed
|
} // namespace Cubed
|
||||||
@@ -2,6 +2,7 @@
|
|||||||
#include "Cubed/tools/log.hpp"
|
#include "Cubed/tools/log.hpp"
|
||||||
|
|
||||||
#include <AL/al.h>
|
#include <AL/al.h>
|
||||||
|
#include <source_location>
|
||||||
namespace Cubed {
|
namespace Cubed {
|
||||||
inline void check_al_error(
|
inline void check_al_error(
|
||||||
std::source_location loaction = std::source_location::current()) {
|
std::source_location loaction = std::source_location::current()) {
|
||||||
|
|||||||
@@ -18,7 +18,6 @@ private:
|
|||||||
THIRD_PERSON_FRONT,
|
THIRD_PERSON_FRONT,
|
||||||
};
|
};
|
||||||
|
|
||||||
bool m_firse_mouse = true;
|
|
||||||
ClientPlayer* m_player;
|
ClientPlayer* m_player;
|
||||||
float m_last_mouse_x, m_last_mouse_y;
|
float m_last_mouse_x, m_last_mouse_y;
|
||||||
glm::vec3 m_camera_pos;
|
glm::vec3 m_camera_pos;
|
||||||
@@ -38,7 +37,6 @@ public:
|
|||||||
|
|
||||||
void camera_init(ClientPlayer* player);
|
void camera_init(ClientPlayer* player);
|
||||||
void hot_reload();
|
void hot_reload();
|
||||||
void reset_camera();
|
|
||||||
void update_cursor_position_camera(float offset_x, float offset_y);
|
void update_cursor_position_camera(float offset_x, float offset_y);
|
||||||
|
|
||||||
const glm::mat4 get_camera_lookat() const;
|
const glm::mat4 get_camera_lookat() const;
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include "Cubed/tools/cubed_assert.hpp"
|
||||||
#include "Cubed/tools/toml.utils.hpp"
|
#include "Cubed/tools/toml.utils.hpp"
|
||||||
|
|
||||||
namespace Cubed {
|
namespace Cubed {
|
||||||
@@ -19,6 +20,7 @@ public:
|
|||||||
|
|
||||||
template <TOML::TomlValueType T>
|
template <TOML::TomlValueType T>
|
||||||
T get(std::string_view key, T default_value) {
|
T get(std::string_view key, T default_value) {
|
||||||
|
ASSERT(m_init);
|
||||||
if (auto* node = find_node(m_tbl, key)) {
|
if (auto* node = find_node(m_tbl, key)) {
|
||||||
if (auto value = node->value<T>())
|
if (auto value = node->value<T>())
|
||||||
return *value;
|
return *value;
|
||||||
@@ -30,6 +32,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
template <TOML::TomlValueType T> void set(std::string_view key, T&& value) {
|
template <TOML::TomlValueType T> void set(std::string_view key, T&& value) {
|
||||||
|
ASSERT(m_init);
|
||||||
auto pos = key.rfind('.');
|
auto pos = key.rfind('.');
|
||||||
|
|
||||||
toml::table* table;
|
toml::table* table;
|
||||||
@@ -53,6 +56,7 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
toml::table m_tbl;
|
toml::table m_tbl;
|
||||||
|
bool m_init = false;
|
||||||
const std::string CONGIF_PATH;
|
const std::string CONGIF_PATH;
|
||||||
const toml::node* find_node(const toml::table& root,
|
const toml::node* find_node(const toml::table& root,
|
||||||
std::string_view path) const;
|
std::string_view path) const;
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ namespace Cubed {
|
|||||||
class DebugCollector {
|
class DebugCollector {
|
||||||
public:
|
public:
|
||||||
static DebugCollector& get();
|
static DebugCollector& get();
|
||||||
static void distory();
|
static void destory();
|
||||||
DebugCollector();
|
DebugCollector();
|
||||||
|
|
||||||
void report(const std::string& name, std::string_view content);
|
void report(const std::string& name, std::string_view content);
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ public:
|
|||||||
Renderer(TextureManager& texture_manager, Config& config);
|
Renderer(TextureManager& texture_manager, Config& config);
|
||||||
~Renderer();
|
~Renderer();
|
||||||
void reload_config();
|
void reload_config();
|
||||||
void init(bool debug_on);
|
void init();
|
||||||
const Shader& get_shader(const std::string& name) const;
|
const Shader& get_shader(const std::string& name) const;
|
||||||
void begin_frame();
|
void begin_frame();
|
||||||
void end_frame();
|
void end_frame();
|
||||||
|
|||||||
@@ -1,85 +1,184 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <atomic>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
|
#include <filesystem>
|
||||||
#include <format>
|
#include <format>
|
||||||
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <source_location>
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
|
#include <print>
|
||||||
|
#include <shared_mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <syncstream>
|
#include <syncstream>
|
||||||
|
#include <utility>
|
||||||
|
#ifdef DEBUG
|
||||||
|
#undef DEBUG
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef INFO
|
||||||
|
#undef INFO
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef ERROR
|
||||||
|
#undef ERROR
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef WARN
|
||||||
|
#undef WARN
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace Cubed {
|
namespace Cubed {
|
||||||
|
|
||||||
namespace Logger {
|
class Logger {
|
||||||
enum class Level { L_TRACE, L_DEBUG, L_INFO, L_ERROR, L_WARN };
|
public:
|
||||||
|
enum class Level { DEBUG = 0, INFO = 1, WARN = 2, ERROR = 3 };
|
||||||
|
static constexpr Level get_level(int i) {
|
||||||
|
switch (i) {
|
||||||
|
case std::to_underlying(Level::DEBUG):
|
||||||
|
return Level::DEBUG;
|
||||||
|
|
||||||
template <typename... Args>
|
case std::to_underlying(Level::INFO):
|
||||||
inline void info(std::format_string<Args...> fmt, Args&&... args) {
|
return Level::INFO;
|
||||||
auto now_time = std::chrono::time_point_cast<std::chrono::seconds>(
|
case std::to_underlying(Level::WARN):
|
||||||
std::chrono::system_clock::now());
|
return Level::WARN;
|
||||||
|
case std::to_underlying(Level::ERROR):
|
||||||
|
return Level::ERROR;
|
||||||
|
default:
|
||||||
|
return Level::INFO;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Logger() { m_file_stream = std::make_unique<std::ofstream>(); }
|
||||||
|
~Logger() { m_file_stream.reset(); }
|
||||||
|
static Logger& instance() {
|
||||||
|
static Logger inst;
|
||||||
|
return inst;
|
||||||
|
}
|
||||||
|
static void set_level(Level level) { instance().m_level = level; }
|
||||||
|
static void set_console_write(bool write) {
|
||||||
|
instance().m_console_write = write;
|
||||||
|
}
|
||||||
|
|
||||||
std::string msg = std::vformat(fmt.get(), std::make_format_args(args...));
|
static void set_file_write(bool write) { instance().m_file_write = write; }
|
||||||
std::osyncstream(std::cout)
|
|
||||||
<< "\033[1;32m" << std::format("[INFO][{:%Y-%m-%d %H:%M:%S}]", now_time)
|
static void set_logs_path(const std::string& path) {
|
||||||
<< msg << "\033[0m"
|
std::unique_lock lock(instance().m_mutex);
|
||||||
<< "\n";
|
instance().m_logs_path = path;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
inline void error(std::format_string<Args...> fmt, Args&&... args) {
|
static void info(std::format_string<Args...> fmt, Args&&... args) {
|
||||||
|
instance().log(Level::INFO, fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
template <typename... Args>
|
||||||
|
static void error(std::format_string<Args...> fmt, Args&&... args) {
|
||||||
|
instance().log(Level::ERROR, fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
template <typename... Args>
|
||||||
|
static void warn(std::format_string<Args...> fmt, Args&&... args) {
|
||||||
|
instance().log(Level::WARN, fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
template <typename... Args>
|
||||||
|
static void debug(std::format_string<Args...> fmt, Args&&... args) {
|
||||||
|
instance().log(Level::DEBUG, fmt, std::forward<Args>(args)...);
|
||||||
|
}
|
||||||
|
template <typename... Args>
|
||||||
|
void log(Level level, std::format_string<Args...> fmt, Args&&... args) {
|
||||||
auto now_time = std::chrono::time_point_cast<std::chrono::seconds>(
|
auto now_time = std::chrono::time_point_cast<std::chrono::seconds>(
|
||||||
std::chrono::system_clock::now());
|
std::chrono::system_clock::now());
|
||||||
std::string msg = std::vformat(fmt.get(), std::make_format_args(args...));
|
if (level < m_level) {
|
||||||
std::osyncstream(std::cerr)
|
return;
|
||||||
<< "\033[1;31m"
|
|
||||||
<< std::format("[ERROR][{:%Y-%m-%d %H:%M:%S}]", now_time) << msg
|
|
||||||
<< "\033[0m"
|
|
||||||
<< "\n";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename... Args>
|
auto msg = std::format(fmt, std::forward<Args>(args)...);
|
||||||
inline void warn(std::format_string<Args...> fmt, Args&&... args) {
|
std::string_view level_str;
|
||||||
auto now_time = std::chrono::time_point_cast<std::chrono::seconds>(
|
std::string_view color;
|
||||||
std::chrono::system_clock::now());
|
|
||||||
std::string msg = std::vformat(fmt.get(), std::make_format_args(args...));
|
|
||||||
std::osyncstream(std::cout)
|
|
||||||
<< "\033[1;33m" << std::format("[WARN][{:%Y-%m-%d %H:%M:%S}]", now_time)
|
|
||||||
<< msg << "\033[0m"
|
|
||||||
<< "\n";
|
|
||||||
}
|
|
||||||
|
|
||||||
template <typename... Args>
|
|
||||||
inline void log(Level level, std::source_location loc,
|
|
||||||
std::format_string<Args...> fmt, Args&&... args) {
|
|
||||||
auto now_time = std::chrono::time_point_cast<std::chrono::seconds>(
|
|
||||||
std::chrono::system_clock::now());
|
|
||||||
std::string msg = std::vformat(fmt.get(), std::make_format_args(args...));
|
|
||||||
switch (level) {
|
switch (level) {
|
||||||
case Logger::Level::L_TRACE:
|
case Level::INFO:
|
||||||
std::osyncstream(std::cout)
|
color = INFO_ANSI;
|
||||||
<< "\033[1;34m"
|
level_str = "[INFO]";
|
||||||
<< std::format("[TRACE][{:%Y-%m-%d %H:%M:%S}]", now_time) << "["
|
|
||||||
<< loc.file_name() << ":" << loc.line() << "]"
|
|
||||||
<< "[" << loc.function_name() << "]" << msg << "\033[0m"
|
|
||||||
<< "\n";
|
|
||||||
break;
|
break;
|
||||||
case Logger::Level::L_DEBUG:
|
case Level::ERROR:
|
||||||
std::osyncstream(std::cout)
|
color = ERROR_ANSI;
|
||||||
<< "\033[1;34m"
|
level_str = "[ERROR]";
|
||||||
<< std::format("[DEBUG][{:%Y-%m-%d %H:%M:%S}]", now_time) << msg
|
|
||||||
<< "\033[0m"
|
|
||||||
<< "\n";
|
|
||||||
break;
|
break;
|
||||||
case Logger::Level::L_INFO:
|
case Level::WARN:
|
||||||
info(fmt, std::forward<Args>(args)...);
|
color = WARN_ANSI;
|
||||||
|
level_str = "[WARN]";
|
||||||
break;
|
break;
|
||||||
case Logger::Level::L_WARN:
|
case Level::DEBUG:
|
||||||
warn(fmt, std::forward<Args>(args)...);
|
color = DEBUG_ANSI;
|
||||||
break;
|
level_str = "[DEBUG]";
|
||||||
case Logger::Level::L_ERROR:
|
|
||||||
error(fmt, std::forward<Args>(args)...);
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
std::string message =
|
||||||
|
std::format("{}[{:%Y-%m-%d %H:%M:%S}]{}", level_str, now_time, msg);
|
||||||
|
|
||||||
|
if (m_file_write) {
|
||||||
|
|
||||||
|
std::shared_lock lock{m_mutex};
|
||||||
|
auto today = std::chrono::floor<std::chrono::days>(
|
||||||
|
std::chrono::system_clock::now());
|
||||||
|
auto date = std::chrono::year_month_day{today};
|
||||||
|
|
||||||
|
if (!m_today.ok() || date > m_today) {
|
||||||
|
lock.unlock();
|
||||||
|
std::unique_lock l(m_mutex);
|
||||||
|
|
||||||
|
auto today = std::chrono::floor<std::chrono::days>(
|
||||||
|
std::chrono::system_clock::now());
|
||||||
|
auto date = std::chrono::year_month_day{today};
|
||||||
|
|
||||||
|
if (!m_today.ok() || date > m_today) {
|
||||||
|
m_today = date;
|
||||||
|
try {
|
||||||
|
std::filesystem::create_directories(m_logs_path);
|
||||||
|
m_file_stream->close();
|
||||||
|
std::filesystem::path log =
|
||||||
|
m_logs_path / std::format("{:%Y-%m-%d}.log", date);
|
||||||
|
m_file_stream->open(log, std::ios::app);
|
||||||
|
if (!m_file_stream->is_open()) {
|
||||||
|
std::osyncstream(std::cerr)
|
||||||
|
<< "File Stream Open Error\n";
|
||||||
|
}
|
||||||
|
// direct write
|
||||||
|
if (m_file_stream && m_file_stream->is_open()) {
|
||||||
|
std::osyncstream sync_out{*m_file_stream};
|
||||||
|
std::println(sync_out, "{}", message);
|
||||||
|
}
|
||||||
|
} catch (const std::exception& e) {
|
||||||
|
std::osyncstream(std::cerr)
|
||||||
|
<< "Catch Logger Error " << e.what() << "\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (m_file_stream && m_file_stream->is_open()) {
|
||||||
|
std::osyncstream sync_out{*m_file_stream};
|
||||||
|
std::println(sync_out, "{}", message);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Logger
|
if (m_console_write) {
|
||||||
|
std::osyncstream sync_out(level == Level::ERROR ? std::cerr
|
||||||
|
: std::cout);
|
||||||
|
std::println(sync_out, "{}{}{}", color, message, CLEAR_ANSI);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
static constexpr std::string_view CLEAR_ANSI = "\033[0m";
|
||||||
|
static constexpr std::string_view INFO_ANSI = "\033[1;32m";
|
||||||
|
static constexpr std::string_view ERROR_ANSI = "\033[1;31m";
|
||||||
|
static constexpr std::string_view DEBUG_ANSI = "\033[1;34m";
|
||||||
|
static constexpr std::string_view WARN_ANSI = "\033[1;33m";
|
||||||
|
std::filesystem::path m_logs_path{"logs"};
|
||||||
|
std::shared_mutex m_mutex;
|
||||||
|
std::atomic<bool> m_file_write{false};
|
||||||
|
std::atomic<bool> m_console_write{true};
|
||||||
|
std::atomic<Level> m_level{Level::INFO};
|
||||||
|
std::chrono::year_month_day m_today{};
|
||||||
|
std::unique_ptr<std::ofstream> m_file_stream;
|
||||||
|
};
|
||||||
|
|
||||||
} // namespace Cubed
|
} // namespace Cubed
|
||||||
|
|||||||
92
src/app.cpp
92
src/app.cpp
@@ -1,6 +1,5 @@
|
|||||||
#include "Cubed/app.hpp"
|
#include "Cubed/app.hpp"
|
||||||
|
|
||||||
#include "Cubed/camera.hpp"
|
|
||||||
#include "Cubed/config.hpp"
|
#include "Cubed/config.hpp"
|
||||||
#include "Cubed/debug_collector.hpp"
|
#include "Cubed/debug_collector.hpp"
|
||||||
#include "Cubed/localization.hpp"
|
#include "Cubed/localization.hpp"
|
||||||
@@ -25,13 +24,43 @@ App::App()
|
|||||||
|
|
||||||
App::~App() {
|
App::~App() {
|
||||||
stop_text_input();
|
stop_text_input();
|
||||||
|
if (m_opengl_init) {
|
||||||
Font::destroy();
|
Font::destroy();
|
||||||
DebugCollector::distory();
|
DebugCollector::destory();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void App::init(int argc, char** argv) {
|
void App::init(int argc, char** argv) {
|
||||||
handle_argument(argc, argv);
|
bool debug = false;
|
||||||
|
Logger::set_console_write(true);
|
||||||
|
#ifdef DEBUG_MODE
|
||||||
|
Logger::set_level(Logger::Level::DEBUG);
|
||||||
|
Logger::set_file_write(false);
|
||||||
|
debug = true;
|
||||||
|
|
||||||
|
#else
|
||||||
|
Logger::set_level(Logger::Level::INFO);
|
||||||
|
debug = false;
|
||||||
|
#endif
|
||||||
|
handle_argument(argc, argv);
|
||||||
|
if (m_argument.log_level) {
|
||||||
|
Logger::set_level(Logger::get_level(*m_argument.log_level));
|
||||||
|
}
|
||||||
|
if (m_argument.logs_path) {
|
||||||
|
Logger::set_logs_path(*m_argument.logs_path);
|
||||||
|
Logger::set_file_write(true);
|
||||||
|
}
|
||||||
|
if (!debug) {
|
||||||
|
Logger::set_file_write(true);
|
||||||
|
Logger::set_console_write(false);
|
||||||
|
}
|
||||||
|
if (m_argument.enable_consolelog) {
|
||||||
|
Logger::set_console_write(true);
|
||||||
|
}
|
||||||
|
if (m_argument.enable_filelog) {
|
||||||
|
Logger::set_file_write(true);
|
||||||
|
}
|
||||||
|
m_game_config.load_config();
|
||||||
if (m_argument.language) {
|
if (m_argument.language) {
|
||||||
Localization::instance().load_language(*m_argument.language);
|
Localization::instance().load_language(*m_argument.language);
|
||||||
m_game_config.set("language", *m_argument.language);
|
m_game_config.set("language", *m_argument.language);
|
||||||
@@ -47,16 +76,12 @@ void App::init(int argc, char** argv) {
|
|||||||
|
|
||||||
m_window.init(m_argument);
|
m_window.init(m_argument);
|
||||||
m_window.imgui_init();
|
m_window.imgui_init();
|
||||||
|
m_opengl_init = true;
|
||||||
Logger::info("Window Init Success");
|
Logger::info("Window Init Success");
|
||||||
|
|
||||||
m_audio.init();
|
m_audio.init();
|
||||||
BlockManager::init();
|
BlockManager::init();
|
||||||
if (m_argument.no_debug) {
|
m_renderer.init();
|
||||||
m_renderer.init(*m_argument.no_debug);
|
|
||||||
} else {
|
|
||||||
m_renderer.init(true);
|
|
||||||
}
|
|
||||||
Logger::info("Renderer Init Success");
|
Logger::info("Renderer Init Success");
|
||||||
// MapTable::init_map();
|
// MapTable::init_map();
|
||||||
m_texture_manager.init_texture();
|
m_texture_manager.init_texture();
|
||||||
@@ -90,16 +115,16 @@ void App::handle_argument(int argc, char** argv) {
|
|||||||
int port;
|
int port;
|
||||||
auto r =
|
auto r =
|
||||||
std::from_chars(arg.data(), arg.data() + arg.size(), port);
|
std::from_chars(arg.data(), arg.data() + arg.size(), port);
|
||||||
m_argument.port = port;
|
|
||||||
if (r.ec != std::errc{} || r.ptr != arg.data() + arg.size()) {
|
if (r.ec != std::errc{} || r.ptr != arg.data() + arg.size()) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
std::format("Invalid port: {}", arg));
|
std::format("Invalid port: {}", arg));
|
||||||
}
|
}
|
||||||
|
if (port > 65535) {
|
||||||
if (m_argument.port > 65535) {
|
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
std::format("Port {} out of range", *m_argument.port));
|
std::format("Port {} out of range", port));
|
||||||
}
|
}
|
||||||
|
m_argument.port = port;
|
||||||
}},
|
}},
|
||||||
|
|
||||||
{"--ip",
|
{"--ip",
|
||||||
@@ -117,12 +142,6 @@ void App::handle_argument(int argc, char** argv) {
|
|||||||
std::cout << CUBED_VERSION << "\n";
|
std::cout << CUBED_VERSION << "\n";
|
||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}},
|
}},
|
||||||
|
|
||||||
{"--no-debug",
|
|
||||||
[&](ArgParser) {
|
|
||||||
m_argument.no_debug = false;
|
|
||||||
Logger::info("Switch off opengl debug out put");
|
|
||||||
}},
|
|
||||||
{"--language",
|
{"--language",
|
||||||
[&](ArgParser& p) {
|
[&](ArgParser& p) {
|
||||||
auto arg = p.require_next("--language");
|
auto arg = p.require_next("--language");
|
||||||
@@ -134,7 +153,32 @@ void App::handle_argument(int argc, char** argv) {
|
|||||||
m_argument.video_driver = arg;
|
m_argument.video_driver = arg;
|
||||||
}},
|
}},
|
||||||
{"--enable-exclusive",
|
{"--enable-exclusive",
|
||||||
[&](ArgParser&) { m_argument.enable_exclusive = true; }}
|
[&](ArgParser&) { m_argument.enable_exclusive = true; }},
|
||||||
|
{"--logs-path",
|
||||||
|
[&](ArgParser& p) {
|
||||||
|
m_argument.logs_path = p.require_next("--logs-path");
|
||||||
|
}},
|
||||||
|
{"--log-level",
|
||||||
|
[&](ArgParser& p) {
|
||||||
|
auto arg = p.require_next("--log-level");
|
||||||
|
int level;
|
||||||
|
auto r = std::from_chars(arg.data(), arg.data() + arg.size(),
|
||||||
|
level);
|
||||||
|
|
||||||
|
if (r.ec != std::errc{} || r.ptr != arg.data() + arg.size()) {
|
||||||
|
throw std::runtime_error(
|
||||||
|
std::format("Invalid log Level: {}", arg));
|
||||||
|
}
|
||||||
|
if (level > 3) {
|
||||||
|
throw std::runtime_error(
|
||||||
|
std::format("Level {} out of range", level));
|
||||||
|
}
|
||||||
|
m_argument.log_level = level;
|
||||||
|
}},
|
||||||
|
{"--enable-filelog",
|
||||||
|
[&](ArgParser&) { m_argument.enable_filelog = true; }},
|
||||||
|
{"--enale-consolelog",
|
||||||
|
[&](ArgParser&) { m_argument.enable_consolelog = true; }}
|
||||||
|
|
||||||
};
|
};
|
||||||
ArgParser parser(argc, argv);
|
ArgParser parser(argc, argv);
|
||||||
@@ -563,10 +607,6 @@ void App::handle_sdl_mouse_button(SDL_Event& e) {
|
|||||||
void App::handle_window_focus(bool focused) {
|
void App::handle_window_focus(bool focused) {
|
||||||
|
|
||||||
if (focused) {
|
if (focused) {
|
||||||
auto camera = m_window.camera();
|
|
||||||
if (camera) {
|
|
||||||
camera->reset_camera();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -675,6 +715,10 @@ void App::handle_sdl_event(SDL_Event& e) {
|
|||||||
break;
|
break;
|
||||||
case SDL_EVENT_MOUSE_MOTION:
|
case SDL_EVENT_MOUSE_MOTION:
|
||||||
if (!imgui_enable) {
|
if (!imgui_enable) {
|
||||||
|
if (std::abs(e.motion.xrel) > 200 ||
|
||||||
|
std::abs(e.motion.yrel) > 200) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
handle_mouse_move(e.motion.x, e.motion.y, e.motion.xrel,
|
handle_mouse_move(e.motion.x, e.motion.y, e.motion.xrel,
|
||||||
e.motion.yrel);
|
e.motion.yrel);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -61,19 +61,12 @@ void Camera::update_move_camera() {
|
|||||||
void Camera::camera_init(ClientPlayer* player) {
|
void Camera::camera_init(ClientPlayer* player) {
|
||||||
m_player = player;
|
m_player = player;
|
||||||
update_move_camera();
|
update_move_camera();
|
||||||
reset_camera();
|
|
||||||
hot_reload();
|
hot_reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Camera::hot_reload() {}
|
void Camera::hot_reload() {}
|
||||||
|
|
||||||
void Camera::reset_camera() { m_firse_mouse = true; }
|
|
||||||
|
|
||||||
void Camera::update_cursor_position_camera(float offset_x, float offset_y) {
|
void Camera::update_cursor_position_camera(float offset_x, float offset_y) {
|
||||||
if (m_firse_mouse) {
|
|
||||||
m_firse_mouse = false;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ASSERT_MSG(m_player, "nullptr");
|
ASSERT_MSG(m_player, "nullptr");
|
||||||
m_player->update_front_vec(offset_x, offset_y);
|
m_player->update_front_vec(offset_x, offset_y);
|
||||||
@@ -109,7 +102,8 @@ bool Camera::is_first_person() const {
|
|||||||
}
|
}
|
||||||
ClientPlayer* Camera::player() { return m_player; }
|
ClientPlayer* Camera::player() { return m_player; }
|
||||||
bool Camera::handle_event(const Event& e) {
|
bool Camera::handle_event(const Event& e) {
|
||||||
return std::visit(Overloaded{[this](const MouseMoveEvent& e) {
|
return std::visit(
|
||||||
|
Overloaded{[this](const MouseMoveEvent& e) {
|
||||||
if (handle_mouse_move_event(e)) {
|
if (handle_mouse_move_event(e)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -125,14 +119,8 @@ bool Camera::handle_event(const Event& e) {
|
|||||||
return false;
|
return false;
|
||||||
},
|
},
|
||||||
[](const TextInputEvent&) { return false; },
|
[](const TextInputEvent&) { return false; },
|
||||||
[this](const WindowResizeEvent&) {
|
[](const WindowResizeEvent&) { return false; },
|
||||||
reset_camera();
|
[](const FrameBufferResizeEvent&) { return false; }
|
||||||
return false;
|
|
||||||
},
|
|
||||||
[this](const FrameBufferResizeEvent&) {
|
|
||||||
reset_camera();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace Cubed
|
} // namespace Cubed
|
||||||
,
|
,
|
||||||
@@ -161,10 +149,6 @@ glm::vec3 Camera::camera_collision(glm::vec3 start, glm::vec3 end,
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool Camera::handle_key_event(const KeyEvent& e) {
|
bool Camera::handle_key_event(const KeyEvent& e) {
|
||||||
if (e.key == Key::LEFT_ALT && e.action == KeyAction::PRESS) {
|
|
||||||
reset_camera();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
if (e.key == Key::F5 && e.action == KeyAction::PRESS) {
|
if (e.key == Key::F5 && e.action == KeyAction::PRESS) {
|
||||||
change_perspective();
|
change_perspective();
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -10,7 +10,7 @@ using namespace std::string_view_literals;
|
|||||||
|
|
||||||
namespace Cubed {
|
namespace Cubed {
|
||||||
|
|
||||||
Config::Config(std::string_view path) : CONGIF_PATH(path) { load_config(); }
|
Config::Config(std::string_view path) : CONGIF_PATH(path) {}
|
||||||
|
|
||||||
Config::~Config() { save_to_file(); }
|
Config::~Config() { save_to_file(); }
|
||||||
|
|
||||||
@@ -23,6 +23,7 @@ void Config::load_config() {
|
|||||||
try {
|
try {
|
||||||
m_tbl = toml::parse_file(config_path.string());
|
m_tbl = toml::parse_file(config_path.string());
|
||||||
Logger::info("Load Config File Success");
|
Logger::info("Load Config File Success");
|
||||||
|
m_init = true;
|
||||||
} catch (const toml::parse_error& err) {
|
} catch (const toml::parse_error& err) {
|
||||||
Logger::error("Load Config Error: \"{}\"", err.what());
|
Logger::error("Load Config Error: \"{}\"", err.what());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ std::unique_ptr<DebugCollector>& DebugCollector::get_ptr() {
|
|||||||
std::make_unique<DebugCollector>();
|
std::make_unique<DebugCollector>();
|
||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
void DebugCollector::distory() { get_ptr().reset(); }
|
void DebugCollector::destory() { get_ptr().reset(); }
|
||||||
void DebugCollector::init(int width, int height) {
|
void DebugCollector::init(int width, int height) {
|
||||||
constexpr float SCALE = 0.6f;
|
constexpr float SCALE = 0.6f;
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ void Renderer::reload_config() {
|
|||||||
update_fov(m_config.get("player.fov", 70.0f));
|
update_fov(m_config.get("player.fov", 70.0f));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Renderer::init(bool debug_on) {
|
void Renderer::init() {
|
||||||
|
|
||||||
Logger::info("OpenGL Version: {}.{}", GLVersion.major, GLVersion.minor);
|
Logger::info("OpenGL Version: {}.{}", GLVersion.major, GLVersion.minor);
|
||||||
Logger::info("Renderer: {}",
|
Logger::info("Renderer: {}",
|
||||||
@@ -51,17 +51,15 @@ void Renderer::init(bool debug_on) {
|
|||||||
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
||||||
|
|
||||||
#ifdef DEBUG_MODE
|
#ifdef DEBUG_MODE
|
||||||
if (debug_on) {
|
|
||||||
glEnable(GL_DEBUG_OUTPUT);
|
glEnable(GL_DEBUG_OUTPUT);
|
||||||
glDebugMessageCallback(
|
glDebugMessageCallback(
|
||||||
[](GLenum, GLenum, GLuint, GLenum, GLsizei, const GLchar* message,
|
[](GLenum, GLenum, GLuint, GLenum, GLsizei, const GLchar* message,
|
||||||
const void*) {
|
const void*) {
|
||||||
Logger::log(Logger::Level::L_DEBUG,
|
Logger::debug("GL Debug: {}",
|
||||||
std::source_location::current(), "GL Debug: {}",
|
|
||||||
reinterpret_cast<const char*>(message));
|
reinterpret_cast<const char*>(message));
|
||||||
},
|
},
|
||||||
nullptr);
|
nullptr);
|
||||||
}
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
@@ -282,6 +282,9 @@ ClientWorld& WorldScene::client_world() { return m_client_world; }
|
|||||||
ServerWorld& WorldScene::server_world() { return m_server.server_world(); }
|
ServerWorld& WorldScene::server_world() { return m_server.server_world(); }
|
||||||
bool WorldScene::pause() const { return m_paused; }
|
bool WorldScene::pause() const { return m_paused; }
|
||||||
void WorldScene::set_pause(bool pause) {
|
void WorldScene::set_pause(bool pause) {
|
||||||
|
if (m_paused == pause) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
m_paused = pause;
|
m_paused = pause;
|
||||||
auto& window = m_scene_manager.app().window();
|
auto& window = m_scene_manager.app().window();
|
||||||
window.set_game_running(!m_paused);
|
window.set_game_running(!m_paused);
|
||||||
|
|||||||
@@ -238,6 +238,10 @@ void Window::init(const Argument& argument) {
|
|||||||
|
|
||||||
SDL_SetWindowPosition(m_window, SDL_WINDOWPOS_CENTERED,
|
SDL_SetWindowPosition(m_window, SDL_WINDOWPOS_CENTERED,
|
||||||
SDL_WINDOWPOS_CENTERED);
|
SDL_WINDOWPOS_CENTERED);
|
||||||
|
#ifdef DEBUG_MODE
|
||||||
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
|
||||||
|
Logger::debug("Debug Flag On");
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void Window::reload_config() {
|
void Window::reload_config() {
|
||||||
@@ -298,9 +302,6 @@ void Window::enable_mouse() {
|
|||||||
}
|
}
|
||||||
void Window::disable_mouse() {
|
void Window::disable_mouse() {
|
||||||
SDL_SetWindowRelativeMouseMode(m_window, true);
|
SDL_SetWindowRelativeMouseMode(m_window, true);
|
||||||
if (m_camera) {
|
|
||||||
m_camera->reset_camera();
|
|
||||||
}
|
|
||||||
m_mouse_enable = false;
|
m_mouse_enable = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user