Skip to content

Commit

Permalink
Merge pull request #8 from wowvain-dev/modifying-platform-impl
Browse files Browse the repository at this point in the history
Changed the way the platform layer works, now we conditionally compil…
  • Loading branch information
wowvain-dev authored Mar 25, 2024
2 parents 6f78b16 + e18efb1 commit 3eedf7a
Show file tree
Hide file tree
Showing 12 changed files with 170 additions and 215 deletions.
2 changes: 1 addition & 1 deletion Sandbox/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
18 changes: 7 additions & 11 deletions Vane/Core/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
10 changes: 5 additions & 5 deletions Vane/Core/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
#include "Defines.h"
#include <string>

#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;

Expand Down
4 changes: 2 additions & 2 deletions Vane/Core/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
103 changes: 65 additions & 38 deletions Vane/Core/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

#include "Defines.h"
#include <tuple>
#include "../Platform/Platform.h"
#include <cstdio>
#include <string>
#include <format>

#define LOG_WARN_ENABLED 1
#define LOG_INFO_ENABLED 1
Expand All @@ -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<typename... Args>
static void log_output(LOG_LEVEL level, const char *message, Args... args) {
std::tuple<const char *, const char *> 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 <typename... Args>
static void log_output(LOG_LEVEL level, std::format_string<Args...> message, Args&&... args) {
std::tuple<const char*, const char*> 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>(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__);
Expand Down
77 changes: 61 additions & 16 deletions Vane/Platform/Platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,76 @@

#include "../Core/Defines.h"

#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <iostream>
#include <string>


#ifdef VPLATFORM_LINUX

#include <xcb/xcb.h>
// #include <X11/keysyn.h>
#include <X11/XKBlib.h>
#include <X11/Xlib.h>
#include <X11/Xlib-xcb.h>
#include <sys/time.h>

#if _POSIX_C_SOURCE >= 199309L
#include <time.h>
#else
#include <unistd.h>
#endif

#elif VPLATFORM_WINDOWS

#include <Windows.h>
#include <windowsx.h>

#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);
};
};
28 changes: 14 additions & 14 deletions Vane/Platform/Platform_Linux.cpp
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 3eedf7a

Please sign in to comment.