some refactoring
This commit is contained in:
parent
51b02627b1
commit
8dd01f9dfc
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include "entry_linux.h"
|
#include "entry_linux.h"
|
||||||
|
|
||||||
#include "platform.c"
|
#include "platform/platform.c"
|
||||||
#include "ds.c"
|
#include "ds.c"
|
||||||
#include "util.c"
|
#include "util.c"
|
||||||
#include "allocators.c"
|
#include "allocators.c"
|
||||||
|
|||||||
@ -12,7 +12,7 @@
|
|||||||
|
|
||||||
#include "shared_types.h"
|
#include "shared_types.h"
|
||||||
#include "ds.h"
|
#include "ds.h"
|
||||||
#include "platform.h"
|
#include "platform/platform.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "allocators.h"
|
#include "allocators.h"
|
||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include "entry_windows.h"
|
#include "entry_windows.h"
|
||||||
|
|
||||||
#include "platform.c"
|
#include "platform/platform.c"
|
||||||
#include "ds.c"
|
#include "ds.c"
|
||||||
#include "util.c"
|
#include "util.c"
|
||||||
#include "allocators.c"
|
#include "allocators.c"
|
||||||
|
|||||||
@ -10,7 +10,7 @@
|
|||||||
|
|
||||||
#include "shared_types.h"
|
#include "shared_types.h"
|
||||||
#include "ds.h"
|
#include "ds.h"
|
||||||
#include "platform.h"
|
#include "platform/platform.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
#include "allocators.h"
|
#include "allocators.h"
|
||||||
#include "renderer.h"
|
#include "renderer.h"
|
||||||
|
|||||||
@ -154,8 +154,6 @@ static void RunCycle(GameContext *ctx, GameInput *inputs, u32 i_count)
|
|||||||
ctx->gui.indices_len = 0;
|
ctx->gui.indices_len = 0;
|
||||||
ctx->gui.instance_count = 0;
|
ctx->gui.instance_count = 0;
|
||||||
ArenaFree(ctx->arena);
|
ArenaFree(ctx->arena);
|
||||||
|
|
||||||
Printfln("end of frame %d", renderer.frame_state.frame_cnt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void DrawRect(GUIContext *ctx, Vec2 p0, Vec2 p1, Vec4 col)
|
static void DrawRect(GUIContext *ctx, Vec2 p0, Vec2 p1, Vec4 col)
|
||||||
|
|||||||
104
src/platform.c
104
src/platform.c
@ -1,104 +0,0 @@
|
|||||||
#if __linux__
|
|
||||||
#include "platform_linux.c"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if _WIN32
|
|
||||||
#include "platform_windows.c"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if __APPLE__ || __MACH__
|
|
||||||
#error Not yet implemented
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if __unix__ && !__linux__
|
|
||||||
#error Not yet implemented
|
|
||||||
#endif
|
|
||||||
|
|
||||||
b32 LoadLib(const char *name, Library *out_lib)
|
|
||||||
{
|
|
||||||
return _LoadLib(name, out_lib);
|
|
||||||
}
|
|
||||||
|
|
||||||
b32 LoadFn(const char *name, Library *lib, Function *out_fn)
|
|
||||||
{
|
|
||||||
return _LoadFn(name, lib, out_fn);
|
|
||||||
}
|
|
||||||
|
|
||||||
b32 InitPlatform()
|
|
||||||
{
|
|
||||||
return _InitPlatform();
|
|
||||||
}
|
|
||||||
|
|
||||||
rawptr MemAlloc(isize size)
|
|
||||||
{
|
|
||||||
return _MemAlloc(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
rawptr MemAllocZeroed(isize size)
|
|
||||||
{
|
|
||||||
return _MemAllocZeroed(size);
|
|
||||||
}
|
|
||||||
|
|
||||||
isize GetPageSize()
|
|
||||||
{
|
|
||||||
return _GetPageSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
i32 EPrint(void const *str)
|
|
||||||
{
|
|
||||||
return _EPrint(str);
|
|
||||||
}
|
|
||||||
|
|
||||||
i32 Printf(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list arg;
|
|
||||||
|
|
||||||
va_start(arg, fmt);
|
|
||||||
i32 result = _Printf(fmt, arg);
|
|
||||||
va_end(arg);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
i32 Printfln(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list arg;
|
|
||||||
|
|
||||||
va_start(arg, fmt);
|
|
||||||
i32 result = _Printfln(fmt, arg);
|
|
||||||
va_end(arg);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
i32 EPrintf(const char *fmt, ...)
|
|
||||||
{
|
|
||||||
va_list arg;
|
|
||||||
|
|
||||||
va_start(arg, fmt);
|
|
||||||
i32 result = _EPrintf(fmt, arg);
|
|
||||||
va_end(arg);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
b32 CreatePlatformWindow()
|
|
||||||
{
|
|
||||||
return _CreateWindow();
|
|
||||||
}
|
|
||||||
|
|
||||||
WindowSize GetWindowSize()
|
|
||||||
{
|
|
||||||
return _GetWindowSize();
|
|
||||||
}
|
|
||||||
|
|
||||||
b32 ShouldQuit()
|
|
||||||
{
|
|
||||||
return _ShouldQuit();
|
|
||||||
}
|
|
||||||
|
|
||||||
b32 NeedWindowResize()
|
|
||||||
{
|
|
||||||
return _NeedWindowResize();
|
|
||||||
}
|
|
||||||
|
|
||||||
49
src/platform/platform.c
Normal file
49
src/platform/platform.c
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
#if __linux__
|
||||||
|
#include "platform/platform_linux.c"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if _WIN32
|
||||||
|
#include "platform/platform_windows.c"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if __APPLE__ || __MACH__
|
||||||
|
#error Not yet implemented
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if __unix__ && !__linux__
|
||||||
|
#error Not yet implemented
|
||||||
|
#endif
|
||||||
|
|
||||||
|
i32 Printf(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list arg;
|
||||||
|
|
||||||
|
va_start(arg, fmt);
|
||||||
|
i32 result = _Printf(fmt, arg);
|
||||||
|
va_end(arg);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
i32 Printfln(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list arg;
|
||||||
|
|
||||||
|
va_start(arg, fmt);
|
||||||
|
i32 result = _Printfln(fmt, arg);
|
||||||
|
va_end(arg);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
i32 EPrintf(const char *fmt, ...)
|
||||||
|
{
|
||||||
|
va_list arg;
|
||||||
|
|
||||||
|
va_start(arg, fmt);
|
||||||
|
i32 result = _EPrintf(fmt, arg);
|
||||||
|
va_end(arg);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
@ -1,25 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
// ::Platform::Types::Header::
|
||||||
|
|
||||||
typedef enum Event_e Event_e;
|
typedef enum Event_e Event_e;
|
||||||
typedef struct WindowEvent WindowEvent;
|
typedef struct WindowEvent WindowEvent;
|
||||||
typedef struct WindowSize WindowSize;
|
typedef struct WindowSize WindowSize;
|
||||||
|
|
||||||
#if __linux__
|
|
||||||
#include "platform_linux.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if _WIN32
|
|
||||||
#include "platform_windows.h"
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if __APPLE__ || __MACH__
|
|
||||||
#error Not yet implemented
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#if __unix__ && !__linux__
|
|
||||||
#error Not yet implemented
|
|
||||||
#endif
|
|
||||||
|
|
||||||
enum Event_e
|
enum Event_e
|
||||||
{
|
{
|
||||||
EVENT_NONE = 0,
|
EVENT_NONE = 0,
|
||||||
@ -44,28 +30,81 @@ struct WindowEvent
|
|||||||
Event_e type;
|
Event_e type;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Init
|
#if __linux__
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *lib;
|
||||||
|
} Library;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *fn;
|
||||||
|
} Function;
|
||||||
|
|
||||||
|
#elif _WIN32
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
HMODULE module;
|
||||||
|
} Library;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
FARPROC fn;
|
||||||
|
} Function;
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// ::Platform::Includes::Header::
|
||||||
|
|
||||||
|
#if __linux__
|
||||||
|
#include "platform/platform_linux.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if _WIN32
|
||||||
|
#include "platform/platform_windows.h"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if __APPLE__ || __MACH__
|
||||||
|
#error Not yet implemented
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if __unix__ && !__linux__
|
||||||
|
#error Not yet implemented
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// ::Platform::Functions::Header::
|
||||||
|
|
||||||
|
// ::Platform::Functions::Init::Header::
|
||||||
|
|
||||||
b32 LoadLib(const char *name, Library *out_lib);
|
b32 LoadLib(const char *name, Library *out_lib);
|
||||||
b32 LoadFn(const char *name, Library *lib, Function *out_fn);
|
b32 LoadFn(const char *name, Library *lib, Function *out_fn);
|
||||||
b32 InitPlatform();
|
|
||||||
|
|
||||||
// Memory Functions
|
// ::Platform::Functions::Memory::Header::
|
||||||
|
|
||||||
rawptr MemAlloc(isize size);
|
rawptr MemAlloc(isize size);
|
||||||
rawptr MemAllocZeroed(isize size);
|
rawptr MemAllocZeroed(isize size);
|
||||||
|
void MemFree(rawptr ptr, isize size);
|
||||||
isize GetPageSize();
|
isize GetPageSize();
|
||||||
|
|
||||||
// Print Functions
|
// ::Platform::Functions::Print::Header::
|
||||||
|
|
||||||
i32 EPrint(void const *str);
|
i32 EPrint(void const *str);
|
||||||
i32 Printf(const char *fmt, ...);
|
i32 Printf(const char *fmt, ...);
|
||||||
i32 Printfln(const char *fmt, ...);
|
i32 Printfln(const char *fmt, ...);
|
||||||
i32 EPrintf(const char *fmt, ...);
|
i32 EPrintf(const char *fmt, ...);
|
||||||
|
|
||||||
// Window Functions
|
// ::Platform::Functions::Window::Header::
|
||||||
|
|
||||||
b32 CreatePlatformWindow();
|
b32 CreatePlatformWindow();
|
||||||
WindowSize GetWindowSize();
|
WindowSize GetWindowSize();
|
||||||
b32 NeedWindowResize();
|
|
||||||
b32 ShouldQuit();
|
b32 ShouldQuit();
|
||||||
|
PlatformWindow *GetPlatformWindow();
|
||||||
|
|
||||||
|
// ::Platform::Functions::SystemInfo::Header::
|
||||||
|
|
||||||
|
u32 AvailableCPUCount();
|
||||||
|
|
||||||
|
// ::Platform::Functions::Directory::Header::
|
||||||
|
|
||||||
// Directory Functions
|
|
||||||
b32 ChangeWorkingDir(const char *);
|
b32 ChangeWorkingDir(const char *);
|
||||||
u8 *OSOpenFile(const char *);
|
u8 *OSOpenFile(const char *);
|
||||||
@ -9,47 +9,13 @@ b32 global_quit = false;
|
|||||||
|
|
||||||
// Init
|
// Init
|
||||||
|
|
||||||
b32 _LoadLib(const char *name, Library *out_lib)
|
|
||||||
{
|
|
||||||
if (!name) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
out_lib->lib = dlopen(name, RTLD_NOW);
|
|
||||||
if (!out_lib->lib) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
b32 _LoadFn(const char *name, Library *lib, Function *out_fn)
|
|
||||||
{
|
|
||||||
if (!name) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
out_fn->fn = dlsym(lib->lib, name);
|
|
||||||
if (!out_fn->fn) {
|
|
||||||
Printf("unable to find function\n");
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
b32 _InitPlatform()
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
// Init End
|
// Init End
|
||||||
|
|
||||||
// General Utils
|
|
||||||
b32 CheckSyscallErr(void *ptr)
|
b32 CheckSyscallErr(void *ptr)
|
||||||
{
|
{
|
||||||
return (isize)ptr == SYS_ERR ? true : false;
|
return (isize)ptr == SYS_ERR ? true : false;
|
||||||
}
|
}
|
||||||
// General Utils End
|
|
||||||
|
|
||||||
i32 Write(int fd, void const *str, isize count)
|
i32 Write(int fd, void const *str, isize count)
|
||||||
{
|
{
|
||||||
@ -57,203 +23,6 @@ i32 Write(int fd, void const *str, isize count)
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
i32 _EPrint(void const *str)
|
|
||||||
{
|
|
||||||
return Write(_STDERR, str, (isize)StrLen(str));
|
|
||||||
}
|
|
||||||
|
|
||||||
i32 _EPrintf(const char *fmt, va_list arg)
|
|
||||||
{
|
|
||||||
char buffer[1024];
|
|
||||||
|
|
||||||
int sprf_res = stbsp_vsnprintf(&buffer[0], 1024, fmt, arg);
|
|
||||||
|
|
||||||
if (sprf_res < 0) return sprf_res;
|
|
||||||
|
|
||||||
return EPrint(&buffer);
|
|
||||||
}
|
|
||||||
|
|
||||||
i32 _Printf(const char *fmt, va_list arg)
|
|
||||||
{
|
|
||||||
char buffer[1024];
|
|
||||||
|
|
||||||
int sprf_res = stbsp_vsnprintf(&buffer[0], 1024, fmt, arg);
|
|
||||||
|
|
||||||
i32 pr_res;
|
|
||||||
if (sprf_res < 0)
|
|
||||||
pr_res = sprf_res;
|
|
||||||
else
|
|
||||||
pr_res = Write(_STDOUT, &buffer, sprf_res);
|
|
||||||
|
|
||||||
return pr_res;
|
|
||||||
}
|
|
||||||
|
|
||||||
i32 _Printfln(const char *fmt, va_list arg)
|
|
||||||
{
|
|
||||||
char buffer[1024];
|
|
||||||
|
|
||||||
int sprf_res = stbsp_vsnprintf(&buffer[0], 1023, fmt, arg);
|
|
||||||
|
|
||||||
i32 pr_res;
|
|
||||||
if (sprf_res < 0)
|
|
||||||
{
|
|
||||||
pr_res = sprf_res;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
buffer[sprf_res] = '\n';
|
|
||||||
buffer[sprf_res+1] = '\0';
|
|
||||||
pr_res = Write(_STDOUT, &buffer, sprf_res+1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return pr_res;
|
|
||||||
}
|
|
||||||
|
|
||||||
rawptr _MemAlloc(isize size)
|
|
||||||
{
|
|
||||||
void *addr = mmap(
|
|
||||||
NULL,
|
|
||||||
size,
|
|
||||||
PROT_READ | PROT_WRITE,
|
|
||||||
MAP_ANON | MAP_PRIVATE,
|
|
||||||
-1,
|
|
||||||
0
|
|
||||||
);
|
|
||||||
|
|
||||||
if (CheckSyscallErr(addr)) addr = NULL;
|
|
||||||
|
|
||||||
return addr;
|
|
||||||
}
|
|
||||||
|
|
||||||
rawptr _MemAllocZeroed(isize size)
|
|
||||||
{
|
|
||||||
rawptr ptr = MemAlloc(size);
|
|
||||||
MemZero(ptr, size);
|
|
||||||
return ptr;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MemFree(rawptr ptr, isize size)
|
|
||||||
{
|
|
||||||
Assert(munmap(ptr, size) == 0, "munmap failed");
|
|
||||||
}
|
|
||||||
|
|
||||||
isize _GetPageSize()
|
|
||||||
{
|
|
||||||
return (isize)sysconf(_SC_PAGESIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
b32 _CreateWindow()
|
|
||||||
{
|
|
||||||
PlatformWindow *window = &linux_window;
|
|
||||||
|
|
||||||
window->display = XOpenDisplay(NULL);
|
|
||||||
if (!window->display)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
window->connection = XGetXCBConnection(window->display);
|
|
||||||
|
|
||||||
if (!window->connection)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
xcb_void_cookie_t cookie;
|
|
||||||
xcb_generic_error_t *error;
|
|
||||||
|
|
||||||
const xcb_setup_t *setup = xcb_get_setup(window->connection);
|
|
||||||
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
|
|
||||||
xcb_screen_t *screen = iter.data;
|
|
||||||
|
|
||||||
const int event_mask = XCB_EVENT_MASK_EXPOSURE |
|
|
||||||
XCB_EVENT_MASK_KEY_PRESS |
|
|
||||||
XCB_EVENT_MASK_KEY_RELEASE |
|
|
||||||
XCB_EVENT_MASK_BUTTON_PRESS |
|
|
||||||
XCB_EVENT_MASK_BUTTON_RELEASE |
|
|
||||||
XCB_EVENT_MASK_POINTER_MOTION |
|
|
||||||
XCB_EVENT_MASK_STRUCTURE_NOTIFY;
|
|
||||||
|
|
||||||
const int val_win[] = {screen->black_pixel, event_mask};
|
|
||||||
const int val_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
|
|
||||||
|
|
||||||
window->window = xcb_generate_id(window->connection);
|
|
||||||
|
|
||||||
cookie = xcb_create_window(
|
|
||||||
window->connection,
|
|
||||||
XCB_COPY_FROM_PARENT,
|
|
||||||
window->window,
|
|
||||||
screen->root,
|
|
||||||
0, // x pos
|
|
||||||
0, // y pos
|
|
||||||
window->w, // width
|
|
||||||
window->h, // height
|
|
||||||
0,
|
|
||||||
XCB_WINDOW_CLASS_INPUT_OUTPUT,
|
|
||||||
screen->root_visual,
|
|
||||||
val_mask,
|
|
||||||
val_win
|
|
||||||
);
|
|
||||||
XCB_CHECK_ERROR(window, cookie, error, "Failed to create window.");
|
|
||||||
|
|
||||||
cookie = xcb_map_window_checked(window->connection, window->window);
|
|
||||||
XCB_CHECK_ERROR(window, cookie, error, "Failed to map window.");
|
|
||||||
|
|
||||||
cookie = xcb_change_property_checked(
|
|
||||||
window->connection,
|
|
||||||
XCB_PROP_MODE_REPLACE,
|
|
||||||
window->window,
|
|
||||||
XCB_ATOM_WM_NAME,
|
|
||||||
XCB_ATOM_STRING,
|
|
||||||
8,
|
|
||||||
StrLen(WINDOW_NAME),
|
|
||||||
WINDOW_NAME
|
|
||||||
);
|
|
||||||
XCB_CHECK_ERROR(window, cookie, error, "Failed to rename window.");
|
|
||||||
|
|
||||||
xcb_intern_atom_cookie_t c_proto = xcb_intern_atom(window->connection, 1, 12, "WM_PROTOCOLS");
|
|
||||||
xcb_intern_atom_reply_t *r_proto = xcb_intern_atom_reply(window->connection, c_proto, &error);
|
|
||||||
XCB_CHECK_CURRENT_ERROR(window, error, "Failed to get WM_PROTOCOLS.");
|
|
||||||
|
|
||||||
xcb_intern_atom_cookie_t c_close = xcb_intern_atom(window->connection, 0, 16, "WM_DELETE_WINDOW");
|
|
||||||
xcb_intern_atom_reply_t *r_close = xcb_intern_atom_reply(window->connection, c_close, &error);
|
|
||||||
XCB_CHECK_CURRENT_ERROR(window, error, "Failed to get WM_DELETE_WINDOW.");
|
|
||||||
|
|
||||||
xcb_intern_atom_cookie_t c_minimize = xcb_intern_atom(window->connection, 0, 20, "_NET_WM_STATE_HIDDEN");
|
|
||||||
xcb_intern_atom_reply_t *r_minimize = xcb_intern_atom_reply(window->connection, c_minimize, &error);
|
|
||||||
XCB_CHECK_CURRENT_ERROR(window, error, "Failed to get _NET_WM_STATE_HIDDEN");
|
|
||||||
|
|
||||||
cookie = xcb_change_property_checked(
|
|
||||||
window->connection,
|
|
||||||
XCB_PROP_MODE_REPLACE,
|
|
||||||
window->window,
|
|
||||||
r_proto->atom,
|
|
||||||
XCB_ATOM_ATOM,
|
|
||||||
32,
|
|
||||||
1,
|
|
||||||
&r_close->atom
|
|
||||||
);
|
|
||||||
XCB_CHECK_ERROR(window, cookie, error, "Failed to set window close event.");
|
|
||||||
|
|
||||||
window->close_event = r_close->atom;
|
|
||||||
window->minimize_event = r_minimize->atom;
|
|
||||||
|
|
||||||
free(r_proto);
|
|
||||||
free(r_close);
|
|
||||||
free(r_minimize);
|
|
||||||
|
|
||||||
xcb_map_window(window->connection, window->window);
|
|
||||||
|
|
||||||
i32 stream_result = xcb_flush(window->connection);
|
|
||||||
if (stream_result <= 0)
|
|
||||||
{
|
|
||||||
Printfln("Error flushing the stream: %d", stream_result);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
PlatformWindow *GetPlatformWindow() {
|
|
||||||
return &linux_window;
|
|
||||||
}
|
|
||||||
|
|
||||||
WindowSize _GetWindowSize()
|
WindowSize _GetWindowSize()
|
||||||
{
|
{
|
||||||
return (WindowSize) {
|
return (WindowSize) {
|
||||||
@ -554,11 +323,6 @@ u8 *OSOpenFile(const char *)
|
|||||||
return bytes;
|
return bytes;
|
||||||
}
|
}
|
||||||
|
|
||||||
b32 _NeedWindowResize()
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
b32 _ShouldQuit()
|
b32 _ShouldQuit()
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
@ -570,3 +334,5 @@ u32 AvailableCPUCount()
|
|||||||
sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
|
sched_getaffinity(0, sizeof(cpu_set), &cpu_set);
|
||||||
return CPU_COUNT(&cpu_set);
|
return CPU_COUNT(&cpu_set);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#include "platform_linux_public.c"
|
||||||
@ -55,26 +55,7 @@ typedef struct {
|
|||||||
u16 w, h;
|
u16 w, h;
|
||||||
} PlatformWindow;
|
} PlatformWindow;
|
||||||
|
|
||||||
typedef struct {
|
// ::Platform::Linux::Functions::Header::
|
||||||
void *lib;
|
|
||||||
} Library;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
void *fn;
|
|
||||||
} Function;
|
|
||||||
|
|
||||||
// Platform API
|
|
||||||
|
|
||||||
// Init Functions
|
|
||||||
b32 _LoadLib(const char *name, Library *out_lib);
|
|
||||||
b32 _LoadFn(const char *name, Library *lib, Function *out_fn);
|
|
||||||
b32 _InitPlatform();
|
|
||||||
|
|
||||||
// Memory Functions
|
|
||||||
rawptr _MemAlloc(isize size);
|
|
||||||
rawptr _MemAllocZeroed(isize size);
|
|
||||||
void MemFree(rawptr ptr, isize size);
|
|
||||||
isize _GetPageSize();
|
|
||||||
|
|
||||||
// Print Functions
|
// Print Functions
|
||||||
i32 _EPrint(void const *str);
|
i32 _EPrint(void const *str);
|
||||||
@ -83,10 +64,8 @@ i32 _Printfln(const char *fmt, va_list arg);
|
|||||||
i32 _EPrintf(const char *fmt, va_list arg);
|
i32 _EPrintf(const char *fmt, va_list arg);
|
||||||
|
|
||||||
// Window Functions
|
// Window Functions
|
||||||
b32 _CreateWindow();
|
|
||||||
void GetWindowEvents(GameInput *inputs, u32 *i_count);
|
void GetWindowEvents(GameInput *inputs, u32 *i_count);
|
||||||
b32 WaitForWindowEvent(GameInput *input);
|
b32 WaitForWindowEvent(GameInput *input);
|
||||||
WindowSize _GetWindowSize();
|
|
||||||
void HandleWindowEvent(GameInput *inputs, u32 *input_count, b32 wait_for_event);
|
void HandleWindowEvent(GameInput *inputs, u32 *input_count, b32 wait_for_event);
|
||||||
KeyboardInput ConvertInputEvent(u32 x_key);
|
KeyboardInput ConvertInputEvent(u32 x_key);
|
||||||
void RepaintWindow();
|
void RepaintWindow();
|
||||||
@ -95,11 +74,7 @@ void RepaintWindow();
|
|||||||
|
|
||||||
// General Utils
|
// General Utils
|
||||||
b32 CheckSyscallErr(void *ptr);
|
b32 CheckSyscallErr(void *ptr);
|
||||||
u32 AvailableCPUCount();
|
|
||||||
|
|
||||||
// Write Utils
|
// Write Utils
|
||||||
i32 Write(int fd, void const *str, isize count);
|
i32 Write(int fd, void const *str, isize count);
|
||||||
|
|
||||||
b32 _NeedWindowResize();
|
|
||||||
b32 _ShouldQuit();
|
|
||||||
|
|
||||||
230
src/platform/platform_linux_public.c
Normal file
230
src/platform/platform_linux_public.c
Normal file
@ -0,0 +1,230 @@
|
|||||||
|
// ::Platform::Functions::Start::
|
||||||
|
|
||||||
|
b32 LoadLib(const char *name, Library *out_lib)
|
||||||
|
{
|
||||||
|
if (!name) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
out_lib->lib = dlopen(name, RTLD_NOW);
|
||||||
|
if (!out_lib->lib) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
b32 LoadFn(const char *name, Library *lib, Function *out_fn)
|
||||||
|
{
|
||||||
|
if (!name) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
out_fn->fn = dlsym(lib->lib, name);
|
||||||
|
if (!out_fn->fn) {
|
||||||
|
Printf("unable to find function\n");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
i32 EPrint(void const *str)
|
||||||
|
{
|
||||||
|
return Write(_STDERR, str, (isize)StrLen(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
i32 _EPrintf(const char *fmt, va_list arg)
|
||||||
|
{
|
||||||
|
char buffer[1024];
|
||||||
|
|
||||||
|
int sprf_res = stbsp_vsnprintf(&buffer[0], 1024, fmt, arg);
|
||||||
|
|
||||||
|
if (sprf_res < 0) return sprf_res;
|
||||||
|
|
||||||
|
return EPrint(&buffer);
|
||||||
|
}
|
||||||
|
|
||||||
|
i32 _Printf(const char *fmt, va_list arg)
|
||||||
|
{
|
||||||
|
char buffer[1024];
|
||||||
|
|
||||||
|
int sprf_res = stbsp_vsnprintf(&buffer[0], 1024, fmt, arg);
|
||||||
|
|
||||||
|
i32 pr_res;
|
||||||
|
if (sprf_res < 0)
|
||||||
|
pr_res = sprf_res;
|
||||||
|
else
|
||||||
|
pr_res = Write(_STDOUT, &buffer, sprf_res);
|
||||||
|
|
||||||
|
return pr_res;
|
||||||
|
}
|
||||||
|
|
||||||
|
i32 _Printfln(const char *fmt, va_list arg)
|
||||||
|
{
|
||||||
|
char buffer[1024];
|
||||||
|
|
||||||
|
int sprf_res = stbsp_vsnprintf(&buffer[0], 1023, fmt, arg);
|
||||||
|
|
||||||
|
i32 pr_res;
|
||||||
|
if (sprf_res < 0)
|
||||||
|
{
|
||||||
|
pr_res = sprf_res;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
buffer[sprf_res] = '\n';
|
||||||
|
buffer[sprf_res+1] = '\0';
|
||||||
|
pr_res = Write(_STDOUT, &buffer, sprf_res+1);
|
||||||
|
}
|
||||||
|
|
||||||
|
return pr_res;
|
||||||
|
}
|
||||||
|
|
||||||
|
rawptr MemAlloc(isize size)
|
||||||
|
{
|
||||||
|
void *addr = mmap(
|
||||||
|
NULL,
|
||||||
|
size,
|
||||||
|
PROT_READ | PROT_WRITE,
|
||||||
|
MAP_ANON | MAP_PRIVATE,
|
||||||
|
-1,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
|
||||||
|
if (CheckSyscallErr(addr)) addr = NULL;
|
||||||
|
|
||||||
|
return addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
rawptr MemAllocZeroed(isize size)
|
||||||
|
{
|
||||||
|
rawptr ptr = MemAlloc(size);
|
||||||
|
MemZero(ptr, size);
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MemFree(rawptr ptr, isize size)
|
||||||
|
{
|
||||||
|
Assert(munmap(ptr, size) == 0, "munmap failed");
|
||||||
|
}
|
||||||
|
|
||||||
|
isize GetPageSize()
|
||||||
|
{
|
||||||
|
return (isize)sysconf(_SC_PAGESIZE);
|
||||||
|
}
|
||||||
|
|
||||||
|
b32 CreatePlatformWindow()
|
||||||
|
{
|
||||||
|
PlatformWindow *window = &linux_window;
|
||||||
|
|
||||||
|
window->display = XOpenDisplay(NULL);
|
||||||
|
if (!window->display)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
window->connection = XGetXCBConnection(window->display);
|
||||||
|
|
||||||
|
if (!window->connection)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
xcb_void_cookie_t cookie;
|
||||||
|
xcb_generic_error_t *error;
|
||||||
|
|
||||||
|
const xcb_setup_t *setup = xcb_get_setup(window->connection);
|
||||||
|
xcb_screen_iterator_t iter = xcb_setup_roots_iterator(setup);
|
||||||
|
xcb_screen_t *screen = iter.data;
|
||||||
|
|
||||||
|
const int event_mask = XCB_EVENT_MASK_EXPOSURE |
|
||||||
|
XCB_EVENT_MASK_KEY_PRESS |
|
||||||
|
XCB_EVENT_MASK_KEY_RELEASE |
|
||||||
|
XCB_EVENT_MASK_BUTTON_PRESS |
|
||||||
|
XCB_EVENT_MASK_BUTTON_RELEASE |
|
||||||
|
XCB_EVENT_MASK_POINTER_MOTION |
|
||||||
|
XCB_EVENT_MASK_STRUCTURE_NOTIFY;
|
||||||
|
|
||||||
|
const int val_win[] = {screen->black_pixel, event_mask};
|
||||||
|
const int val_mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK;
|
||||||
|
|
||||||
|
window->window = xcb_generate_id(window->connection);
|
||||||
|
|
||||||
|
cookie = xcb_create_window(
|
||||||
|
window->connection,
|
||||||
|
XCB_COPY_FROM_PARENT,
|
||||||
|
window->window,
|
||||||
|
screen->root,
|
||||||
|
0, // x pos
|
||||||
|
0, // y pos
|
||||||
|
window->w, // width
|
||||||
|
window->h, // height
|
||||||
|
0,
|
||||||
|
XCB_WINDOW_CLASS_INPUT_OUTPUT,
|
||||||
|
screen->root_visual,
|
||||||
|
val_mask,
|
||||||
|
val_win
|
||||||
|
);
|
||||||
|
XCB_CHECK_ERROR(window, cookie, error, "Failed to create window.");
|
||||||
|
|
||||||
|
cookie = xcb_map_window_checked(window->connection, window->window);
|
||||||
|
XCB_CHECK_ERROR(window, cookie, error, "Failed to map window.");
|
||||||
|
|
||||||
|
cookie = xcb_change_property_checked(
|
||||||
|
window->connection,
|
||||||
|
XCB_PROP_MODE_REPLACE,
|
||||||
|
window->window,
|
||||||
|
XCB_ATOM_WM_NAME,
|
||||||
|
XCB_ATOM_STRING,
|
||||||
|
8,
|
||||||
|
StrLen(WINDOW_NAME),
|
||||||
|
WINDOW_NAME
|
||||||
|
);
|
||||||
|
XCB_CHECK_ERROR(window, cookie, error, "Failed to rename window.");
|
||||||
|
|
||||||
|
xcb_intern_atom_cookie_t c_proto = xcb_intern_atom(window->connection, 1, 12, "WM_PROTOCOLS");
|
||||||
|
xcb_intern_atom_reply_t *r_proto = xcb_intern_atom_reply(window->connection, c_proto, &error);
|
||||||
|
XCB_CHECK_CURRENT_ERROR(window, error, "Failed to get WM_PROTOCOLS.");
|
||||||
|
|
||||||
|
xcb_intern_atom_cookie_t c_close = xcb_intern_atom(window->connection, 0, 16, "WM_DELETE_WINDOW");
|
||||||
|
xcb_intern_atom_reply_t *r_close = xcb_intern_atom_reply(window->connection, c_close, &error);
|
||||||
|
XCB_CHECK_CURRENT_ERROR(window, error, "Failed to get WM_DELETE_WINDOW.");
|
||||||
|
|
||||||
|
xcb_intern_atom_cookie_t c_minimize = xcb_intern_atom(window->connection, 0, 20, "_NET_WM_STATE_HIDDEN");
|
||||||
|
xcb_intern_atom_reply_t *r_minimize = xcb_intern_atom_reply(window->connection, c_minimize, &error);
|
||||||
|
XCB_CHECK_CURRENT_ERROR(window, error, "Failed to get _NET_WM_STATE_HIDDEN");
|
||||||
|
|
||||||
|
cookie = xcb_change_property_checked(
|
||||||
|
window->connection,
|
||||||
|
XCB_PROP_MODE_REPLACE,
|
||||||
|
window->window,
|
||||||
|
r_proto->atom,
|
||||||
|
XCB_ATOM_ATOM,
|
||||||
|
32,
|
||||||
|
1,
|
||||||
|
&r_close->atom
|
||||||
|
);
|
||||||
|
XCB_CHECK_ERROR(window, cookie, error, "Failed to set window close event.");
|
||||||
|
|
||||||
|
window->close_event = r_close->atom;
|
||||||
|
window->minimize_event = r_minimize->atom;
|
||||||
|
|
||||||
|
free(r_proto);
|
||||||
|
free(r_close);
|
||||||
|
free(r_minimize);
|
||||||
|
|
||||||
|
xcb_map_window(window->connection, window->window);
|
||||||
|
|
||||||
|
i32 stream_result = xcb_flush(window->connection);
|
||||||
|
if (stream_result <= 0)
|
||||||
|
{
|
||||||
|
Printfln("Error flushing the stream: %d", stream_result);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
PlatformWindow *GetPlatformWindow()
|
||||||
|
{
|
||||||
|
return &linux_window;
|
||||||
|
}
|
||||||
@ -34,7 +34,7 @@ LRESULT CALLBACK WindowProc(HWND window, UINT message, WPARAM w_param, LPARAM l_
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
b32 _LoadLib(const char *name, Library *out_lib)
|
b32 LoadLib(const char *name, Library *out_lib)
|
||||||
{
|
{
|
||||||
b32 success = true;
|
b32 success = true;
|
||||||
|
|
||||||
@ -45,7 +45,7 @@ b32 _LoadLib(const char *name, Library *out_lib)
|
|||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
b32 _LoadFn(const char *name, Library *lib, Function *out_fn)
|
b32 LoadFn(const char *name, Library *lib, Function *out_fn)
|
||||||
{
|
{
|
||||||
b32 success = true;
|
b32 success = true;
|
||||||
|
|
||||||
@ -56,13 +56,6 @@ b32 _LoadFn(const char *name, Library *lib, Function *out_fn)
|
|||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
b32 _InitPlatform()
|
|
||||||
{
|
|
||||||
b32 success = true;
|
|
||||||
|
|
||||||
return success;
|
|
||||||
}
|
|
||||||
|
|
||||||
rawptr _MemAlloc(isize size)
|
rawptr _MemAlloc(isize size)
|
||||||
{
|
{
|
||||||
return (rawptr)VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
|
return (rawptr)VirtualAlloc(0, size, MEM_RESERVE|MEM_COMMIT, PAGE_READWRITE);
|
||||||
@ -135,7 +128,7 @@ i32 _EPrintf(const char *fmt, va_list arg)
|
|||||||
return EPrint(&buffer);
|
return EPrint(&buffer);
|
||||||
}
|
}
|
||||||
|
|
||||||
b32 _CreateWindow()
|
b32 CreatePlatformWindow()
|
||||||
{
|
{
|
||||||
b32 success = true;
|
b32 success = true;
|
||||||
|
|
||||||
@ -213,11 +206,6 @@ b32 _ShouldQuit()
|
|||||||
return global_quit;
|
return global_quit;
|
||||||
}
|
}
|
||||||
|
|
||||||
b32 _NeedWindowResize()
|
|
||||||
{
|
|
||||||
return win32_window.resize_requested;
|
|
||||||
}
|
|
||||||
|
|
||||||
WindowSize _GetWindowSize()
|
WindowSize _GetWindowSize()
|
||||||
{
|
{
|
||||||
return (WindowSize){ .w = win32_window.w, .h = win32_window.h };
|
return (WindowSize){ .w = win32_window.w, .h = win32_window.h };
|
||||||
@ -6,11 +6,6 @@
|
|||||||
|
|
||||||
#define WINDOW_CLASS_NAME "GearsWindowClass"
|
#define WINDOW_CLASS_NAME "GearsWindowClass"
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
HMODULE module;
|
|
||||||
} Library;
|
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
HINSTANCE instance;
|
HINSTANCE instance;
|
||||||
@ -19,15 +14,6 @@ typedef struct
|
|||||||
b32 resize_requested;
|
b32 resize_requested;
|
||||||
} PlatformWindow;
|
} PlatformWindow;
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
FARPROC fn;
|
|
||||||
} Function;
|
|
||||||
|
|
||||||
b32 _LoadLib(const char *name, Library *out_lib);
|
|
||||||
b32 _LoadFn(const char *name, Library *lib, Function *out_fn);
|
|
||||||
|
|
||||||
b32 _InitPlatform();
|
|
||||||
rawptr _MemAlloc(isize size);
|
rawptr _MemAlloc(isize size);
|
||||||
rawptr _MemAllocZeroed(isize size);
|
rawptr _MemAllocZeroed(isize size);
|
||||||
isize _GetPageSize();
|
isize _GetPageSize();
|
||||||
@ -37,7 +23,5 @@ i32 _Printf(const char *fmt, va_list arg);
|
|||||||
i32 _Printfln(const char *fmt, va_list arg);
|
i32 _Printfln(const char *fmt, va_list arg);
|
||||||
i32 _EPrintf(const char *fmt, va_list arg);
|
i32 _EPrintf(const char *fmt, va_list arg);
|
||||||
|
|
||||||
b32 _CreateWindow();
|
|
||||||
void GetWindowEvents();
|
void GetWindowEvents();
|
||||||
void WaitForWindowEvent();
|
void WaitForWindowEvent();
|
||||||
WindowSize _GetWindowSize();
|
|
||||||
0
src/platform/platform_windows_public.c
Normal file
0
src/platform/platform_windows_public.c
Normal file
@ -1228,6 +1228,9 @@ void *VkLoaderStart(void *i)
|
|||||||
|
|
||||||
static void StartVkLoaderThreads()
|
static void StartVkLoaderThreads()
|
||||||
{
|
{
|
||||||
|
if (renderer.vk_conf.avail_threads == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
u32 count = renderer.vk_conf.avail_threads;
|
u32 count = renderer.vk_conf.avail_threads;
|
||||||
pthread_t *threads = ArenaAlloc(renderer.perm_arena, sizeof(pthread_t) * count);
|
pthread_t *threads = ArenaAlloc(renderer.perm_arena, sizeof(pthread_t) * count);
|
||||||
|
|
||||||
|
|||||||
@ -199,8 +199,6 @@ static b32 CreateBuffer(RenderBuffer *buffer)
|
|||||||
success = false;
|
success = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Printfln("buffer ptr: %d", buffer->buffer);
|
|
||||||
|
|
||||||
return success;
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user