diff --git a/Sandbox/main.cpp b/Sandbox/main.cpp index 3bad4d8..ae0ea0d 100755 --- a/Sandbox/main.cpp +++ b/Sandbox/main.cpp @@ -18,7 +18,7 @@ using namespace Vane; int main(int argc, char **argv) { - Logger::log_output(LOG_LEVEL::V_ERROR, "This is a test %f", 3.12f); + Logger::log_output(LOG_LEVEL::V_ERROR, "This is a test {}", 3.12f); ApplicationConfig config = { .startPosX = 100, .startPosY = 100, diff --git a/Vane/Core/Application.cpp b/Vane/Core/Application.cpp index 543381a..f5d8488 100644 --- a/Vane/Core/Application.cpp +++ b/Vane/Core/Application.cpp @@ -13,21 +13,17 @@ bool Application::create(ApplicationConfig* config) { Logger::initializeLogging(); // TODO(wowvain-dev): Remove later, used for testing - VFATAL("A test message: %f", 3.14f); - VERROR("A test message: %f", 3.14f); - VWARN("A test message: %f", 3.14f); - VINFO("A test message: %f", 3.14f); - VDEBUG("A test message: %f", 3.14f); - VTRACE("A test message: %f", 3.14f); + VFATAL("A test message: {}", 3.14f); + VERROR("A test message: {}", 3.14f); + VWARN("A test message: {}", 3.14f); + VINFO("A test message: {}", 3.14f); + VDEBUG("A test message: {}", 3.14f); + VTRACE("A test message: {}", 3.14f); isRunning = true; isSuspended = false; -#ifdef VPLATFORM_WINDOWS - platform = new Platform_Win32(); -#elif VPLATFORM_LINUX - platform = new Platform_Linux(); -#endif + platform = new Platform(); if (!platform->startup( config->name.c_str(), diff --git a/Vane/Core/Application.h b/Vane/Core/Application.h index d38fb83..94b1ff5 100644 --- a/Vane/Core/Application.h +++ b/Vane/Core/Application.h @@ -15,11 +15,11 @@ #include "Defines.h" #include -#ifdef VPLATFORM_WINDOWS -#include "../Platform/Platform_Win32.h" -#elif VPLATFORM_LINUX -#include "../Platform/Platform_Linux.h" -#endif +// #ifdef VPLATFORM_WINDOWS +// #include "../Platform/Platform_Win32.h" +// #elif VPLATFORM_LINUX +// #include "../Platform/Platform_Linux.h" +// #endif using std::string; diff --git a/Vane/Core/Logger.cpp b/Vane/Core/Logger.cpp index 4248116..5ea216e 100755 --- a/Vane/Core/Logger.cpp +++ b/Vane/Core/Logger.cpp @@ -14,7 +14,7 @@ namespace Vane { void reportAssertionFailure(const char* expression, const char* message, const char* file, i32 line) { - Logger::log_output(LOG_LEVEL::V_FATAL, "Assertion Failure: %s, message: '%s', in file: %s, line: %d\n", + Logger::log_output(LOG_LEVEL::V_FATAL, "Assertion Failure: {}, message: '{}', in file: {}, line: {}\n", expression, message, file, @@ -28,7 +28,7 @@ namespace Vane { std::string file, i32 line ) { - Logger::log_output(LOG_LEVEL::V_FATAL, "Assertion Failure: %s, message: '%s', in file: %s, line: %d\n", + Logger::log_output(LOG_LEVEL::V_FATAL, "Assertion Failure: {}, message: '{}', in file: {}, line: {}\n", expression, message, file, diff --git a/Vane/Core/Logger.h b/Vane/Core/Logger.h index 250d595..9f734b9 100755 --- a/Vane/Core/Logger.h +++ b/Vane/Core/Logger.h @@ -12,7 +12,10 @@ #include "Defines.h" #include +#include "../Platform/Platform.h" #include +#include +#include #define LOG_WARN_ENABLED 1 #define LOG_INFO_ENABLED 1 @@ -25,46 +28,70 @@ #define LOG_TRACE_ENABLED 0 #endif - namespace Vane { - enum LOG_LEVEL { - V_FATAL = 0, - V_ERROR = 1, - V_WARN = 2, - V_INFO = 3, - V_DEBUG = 4, - V_TRACE = 5 - }; - - class Logger { - public: - - private: - public: - static bool initializeLogging(); - - static void shutdownLogging(); - - template - static void log_output(LOG_LEVEL level, const char *message, Args... args) { - std::tuple level_strings[6] = { - std::make_tuple("[FATAL]: ", "0;41"), - std::make_tuple("[ERROR]: ", "1;31"), - std::make_tuple("[WARN]: ", "1;33"), - std::make_tuple("[INFO]: ", "1;32"), - std::make_tuple("[DEBUG]: ", "1;34"), - std::make_tuple("[TRACE]: ", "1:30") - }; - bool is_error = level < 2; - - - printf("\033[%sm%s - ", - std::get<1>(level_strings[level]), - std::get<0>(level_strings[level])); - printf(message, args...); - printf("\033[0m\n"); +enum LOG_LEVEL { + V_FATAL = 0, + V_ERROR = 1, + V_WARN = 2, + V_INFO = 3, + V_DEBUG = 4, + V_TRACE = 5 +}; + + +class Logger { +public: + +private: +public: + static bool initializeLogging(); + + static void shutdownLogging(); + + template + static void log_output(LOG_LEVEL level, std::format_string message, Args&&... args) { + std::tuple level_strings[6] = { + std::make_tuple("[FATAL]: ", "0;41"), + std::make_tuple("[ERROR]: ", "1;31"), + std::make_tuple("[WARN]: ", "1;33"), + std::make_tuple("[INFO]: ", "1;32"), + std::make_tuple("[DEBUG]: ", "1;34"), + std::make_tuple("[TRACE]: ", "1:30") + }; + const bool is_error = level < LOG_LEVEL::V_WARN; + + std::string label = std::format("\033[{}m{} - ", + std::get<1>(level_strings[level]), + std::get<0>(level_strings[level]) + ); + + std::string out_message; + std::string s; + + try { + out_message = std::format(message, std::forward(args)...); + s = std::format("{}{}\n", label, out_message); + } + catch (...) { + Platform::consoleWriteError("Bad string formatting", 1); + return; + } + + if (is_error) { + Platform::consoleWriteError(s.c_str(), level); } - }; + else { + Platform::consoleWrite(s.c_str(), level); + } + + + // printf("\033[%sm%s - ", + // std::get<1>(level_strings[level]), + // std::get<0>(level_strings[level])); + // printf(message, args...); + // printf("\033[0m\n"); + } +}; #ifndef VFATAL #define VFATAL(message, ...) Vane::Logger::log_output(Vane::LOG_LEVEL::V_FATAL, message, ##__VA_ARGS__); diff --git a/Vane/Platform/Platform.h b/Vane/Platform/Platform.h index 6e1bf99..fcc400a 100644 --- a/Vane/Platform/Platform.h +++ b/Vane/Platform/Platform.h @@ -2,31 +2,76 @@ #include "../Core/Defines.h" +#include +#include +#include +#include +#include + + +#ifdef VPLATFORM_LINUX + +#include +// #include +#include +#include +#include +#include + +#if _POSIX_C_SOURCE >= 199309L +#include +#else +#include +#endif + +#elif VPLATFORM_WINDOWS + +#include +#include + +#endif + namespace Vane { class VAPI Platform { - void* internalState; +#ifdef VPLATFORM_WINDOWS + HINSTANCE h_instance; + HWND hwnd; +#elif VPLATFORM_LINUX + Display *display; + xcb_connection_t *connection; + xcb_window_t window; + xcb_screen_t *screen; + xcb_atom_t wm_protocols; + xcb_atom_t wm_delete_win; +#endif + +private: +#ifdef VPLATFORM_WINDOWS + static f64 clock_frequency; + static LARGE_INTEGER start_time; +#elif VPLATFORM_LINUX + +#endif public: - virtual bool startup( + bool startup( const char* applicationName, i32 x, i32 y, i32 width, i32 height - ) = 0; - - virtual void shutdown() = 0; - - virtual bool pumpMessages() = 0; + ); + void shutdown(); + bool pumpMessages(); - virtual void* allocate(size_t size, bool aligned) = 0; - virtual void free(void*, bool) = 0; - virtual void* zeroMemory(void* block, size_t size) = 0; - virtual void* copyMemory(void* dest, const void* source, size_t size) = 0; - virtual void* setMemory(void* dest, i32 value, size_t size) = 0; - virtual void consoleWrite(const char* message, u8 color) = 0; - virtual void consoleWriteError(const char* message, u8 color) = 0; - virtual f64 getAbsoluteTime() = 0; - virtual void sleep(u64) = 0; + static void* allocate(size_t size, bool aligned); + static void free(void*, bool); + static void* zeroMemory(void* block, size_t size); + static void* copyMemory(void* dest, const void* source, size_t size); + static void* setMemory(void* dest, i32 value, size_t size); + static void consoleWrite(const char* message, u8 color); + static void consoleWriteError(const char* message, u8 color); + static f64 getAbsoluteTime(); + static void sleep(u64); }; }; diff --git a/Vane/Platform/Platform_Linux.cpp b/Vane/Platform/Platform_Linux.cpp index 8e710cc..a8ac62e 100644 --- a/Vane/Platform/Platform_Linux.cpp +++ b/Vane/Platform/Platform_Linux.cpp @@ -1,10 +1,10 @@ -#include "Platform_Linux.h" +#include "Platform.h" #ifdef VPLATFORM_LINUX using namespace Vane; -bool Platform_Linux::startup( +bool Platform::startup( const char *application_name, i32 x, i32 y, @@ -113,21 +113,21 @@ bool Platform_Linux::startup( if (stream_result <= 0) { - VFATAL("An error occured when flushing the stream: %d", stream_result); + VFATAL("An error occured when flushing the stream: {}", stream_result); return false; } return true; } -void Platform_Linux::shutdown() +void Platform::shutdown() { XAutoRepeatOn(display); xcb_destroy_window(connection, window); } -bool Platform_Linux::pumpMessages() +bool Platform::pumpMessages() { xcb_generic_event_t *event; xcb_client_message_event_t *cm; @@ -188,53 +188,53 @@ bool Platform_Linux::pumpMessages() return !quitFlagged; } -void *Platform_Linux::allocate(size_t size, bool aligned) +void *Platform::allocate(size_t size, bool aligned) { return ::operator new(size); } -void Platform_Linux::free(void *block, bool aligned) +void Platform::free(void *block, bool aligned) { ::operator delete(block); } -void *Platform_Linux::zeroMemory(void *block, size_t size) +void *Platform::zeroMemory(void *block, size_t size) { return std::memset(block, 0, size); } -void *Platform_Linux::copyMemory(void *dest, const void *source, size_t size) +void *Platform::copyMemory(void *dest, const void *source, size_t size) { return std::memcpy(dest, source, size); } -void *Platform_Linux::setMemory(void *dest, i32 value, size_t size) +void *Platform::setMemory(void *dest, i32 value, size_t size) { return std::memset(dest, value, size); } -void Platform_Linux::consoleWrite(const char *message, u8 color) +void Platform::consoleWrite(const char *message, u8 color) { // FATAL, ERROR, WARN, INFO, DEBUG, TRACE const char *color_strings[] = {"0;41", "1;31", "1;33", "1;32", "1;34", "1;30"}; printf("\033[%sm%s\033[0m", color_strings[color], message); } -void Platform_Linux::consoleWriteError(const char *message, u8 color) +void Platform::consoleWriteError(const char *message, u8 color) { // FATAL, ERROR, WARN, INFO, DEBUG, TRACE const char *color_strings[] = {"0;41", "1;31", "1;33", "1;32", "1;34", "1;30"}; printf("\033[%sm%s\033[0m", color_strings[color], message); } -f64 Platform_Linux::getAbsoluteTime() +f64 Platform::getAbsoluteTime() { timespec now; clock_gettime(CLOCK_MONOTONIC, &now); return now.tv_sec + now.tv_nsec * 0.000000001; } -void Platform_Linux::sleep(u64 ms) +void Platform::sleep(u64 ms) { #if _POSIX_C_SOURCE >= 199309L timespec ts; diff --git a/Vane/Platform/Platform_Linux.h b/Vane/Platform/Platform_Linux.h deleted file mode 100644 index 692bcb4..0000000 --- a/Vane/Platform/Platform_Linux.h +++ /dev/null @@ -1,62 +0,0 @@ -#include "Platform.h" - -#ifdef VPLATFORM_LINUX - -#include "../Core/Defines.h" -#include "../Core/Logger.h" - -#include -// #include -#include -#include -#include -#include - -#if _POSIX_C_SOURCE >= 199309L -#include -#else -#include -#endif - -#include -#include -#include -#include -#include - -namespace Vane -{ - class Platform_Linux : public Platform - { - Display *display; - xcb_connection_t *connection; - xcb_window_t window; - xcb_screen_t *screen; - xcb_atom_t wm_protocols; - xcb_atom_t wm_delete_win; - - public: - bool startup( - const char *, - i32, - i32, - i32, - i32) override; - - void shutdown() override; - - bool pumpMessages() override; - - void *allocate(size_t, bool) override; - void free(void *, bool) override; - void *zeroMemory(void *, size_t) override; - void *copyMemory(void *, const void *, size_t) override; - void *setMemory(void *, i32, size_t) override; - void consoleWrite(const char *, u8) override; - void consoleWriteError(const char *, u8) override; - f64 getAbsoluteTime() override; - void sleep(u64) override; - }; -}; - -#endif diff --git a/Vane/Platform/Platform_Win32.cpp b/Vane/Platform/Platform_Win32.cpp index 22c7d7e..c696230 100644 --- a/Vane/Platform/Platform_Win32.cpp +++ b/Vane/Platform/Platform_Win32.cpp @@ -1,4 +1,4 @@ -#include "Platform_Win32.h" +#include "Platform.h" #ifdef VPLATFORM_WINDOWS @@ -6,13 +6,13 @@ using namespace Vane; -f64 Platform_Win32::clock_frequency = 0.; -LARGE_INTEGER Platform_Win32::start_time = {0}; +f64 Platform::clock_frequency = 0.; +LARGE_INTEGER Platform::start_time = {0}; LRESULT CALLBACK win32_process_message(HWND hwnd, u32 msg, WPARAM w_param, LPARAM l_param); -bool Platform_Win32::startup(const char* application_name, i32 x, i32 y, +bool Platform::startup(const char* application_name, i32 x, i32 y, i32 width, i32 height) { h_instance = GetModuleHandleA(nullptr); @@ -92,14 +92,14 @@ bool Platform_Win32::startup(const char* application_name, i32 x, i32 y, return true; } -void Platform_Win32::shutdown() { +void Platform::shutdown() { if (hwnd) { DestroyWindow(hwnd); hwnd = 0; } } -bool Platform_Win32::pumpMessages() { +bool Platform::pumpMessages() { MSG message; while (PeekMessageA(&message, nullptr, 0, 0, PM_REMOVE)) { TranslateMessage(&message); @@ -109,27 +109,27 @@ bool Platform_Win32::pumpMessages() { return true; } -void* Platform_Win32::allocate(size_t size, bool aligned) { +void* Platform::allocate(size_t size, bool aligned) { return ::operator new(size); } -void Platform_Win32::free(void* block, bool aligned) { +void Platform::free(void* block, bool aligned) { ::operator delete(block); } -void* Platform_Win32::zeroMemory(void* block, size_t size) { +void* Platform::zeroMemory(void* block, size_t size) { return memset(block, 0, size); } -void* Platform_Win32::copyMemory(void* dest, const void* source, size_t size) { +void* Platform::copyMemory(void* dest, const void* source, size_t size) { return memcpy(dest, source, size); } -void* Platform_Win32::setMemory(void* dest, i32 value, size_t size) { +void* Platform::setMemory(void* dest, i32 value, size_t size) { return memset(dest, value, size); } -void Platform_Win32::consoleWrite(const char* message, u8 color) { +void Platform::consoleWrite(const char* message, u8 color) { HANDLE console_handle = GetStdHandle(STD_OUTPUT_HANDLE); // FATAL, ERROR, WARN, INFO, DEBUG, TRACE static u8 levels[6] = {64, 4, 6, 2, 1, 8}; @@ -142,7 +142,7 @@ void Platform_Win32::consoleWrite(const char* message, u8 color) { number_written, nullptr); } -void Platform_Win32::consoleWriteError(const char* message, u8 color) { +void Platform::consoleWriteError(const char* message, u8 color) { HANDLE console_handle = GetStdHandle(STD_OUTPUT_HANDLE); // FATAL, ERROR, WARN, INFO, DEBUG, TRACE static u8 levels[6] = {64, 4, 6, 2, 1, 8}; @@ -155,13 +155,13 @@ void Platform_Win32::consoleWriteError(const char* message, u8 color) { number_written, nullptr); } -f64 Platform_Win32::getAbsoluteTime() { +f64 Platform::getAbsoluteTime() { LARGE_INTEGER now_time; QueryPerformanceCounter(&now_time); return static_cast(now_time.QuadPart) * clock_frequency; } -void Platform_Win32::sleep(u64 ms) { Sleep(ms); } +void Platform::sleep(u64 ms) { Sleep(ms); } LRESULT win32_process_message(HWND hwnd, u32 msg, WPARAM w_param, LPARAM l_param) { switch (msg) { @@ -181,8 +181,6 @@ LRESULT win32_process_message(HWND hwnd, u32 msg, WPARAM w_param, LPARAM l_param u32 width = r.right - r.left; u32 height = r.bottom - r.top; - VINFO("WM_SIZE: %d %d %d %d", r.left, r.top, r.right, r.bottom); - // Fire an event for resize; } break; diff --git a/Vane/Platform/Platform_Win32.h b/Vane/Platform/Platform_Win32.h deleted file mode 100644 index abb29fa..0000000 --- a/Vane/Platform/Platform_Win32.h +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include "Platform.h" - -#if VPLATFORM_WINDOWS - -#include -#include -#include -#include -#include -#include -#include - -namespace Vane { -class Platform_Win32 : public Platform { - HINSTANCE h_instance; - HWND hwnd; - -private: - static f64 clock_frequency; - static LARGE_INTEGER start_time; - -public: - bool startup(const char*, i32, i32, i32, i32) override; - void shutdown() override; - bool pumpMessages() override; - - void* allocate(size_t, bool) override; - void free(void*, bool) override; - void* zeroMemory(void* block, size_t size) override; - void* copyMemory(void* dest, const void* source, size_t size) override; - void* setMemory(void* dest, i32 value, size_t size) override; - void consoleWrite(const char* message, u8 color) override; - void consoleWriteError(const char* message, u8 color) override; - void sleep(u64) override; - f64 getAbsoluteTime() override; -}; -}; // namespace Vane - -#endif diff --git a/Vane/Vane.vcxproj b/Vane/Vane.vcxproj index c8dfe0c..3f18ccc 100644 --- a/Vane/Vane.vcxproj +++ b/Vane/Vane.vcxproj @@ -101,8 +101,6 @@ - - diff --git a/Vane/Vane.vcxproj.filters b/Vane/Vane.vcxproj.filters index c2f5bee..3c52e9a 100644 --- a/Vane/Vane.vcxproj.filters +++ b/Vane/Vane.vcxproj.filters @@ -24,12 +24,6 @@ Platform - - Platform - - - Platform -