summaryrefslogtreecommitdiff
path: root/source/base/base_platform.h
diff options
context:
space:
mode:
authornasr <nsrddyn@gmail.com>2026-10-05 22:02:56 +0200
committernasr <nsrddyn@gmail.com>2026-10-05 22:02:56 +0200
commitf6d3090f7fa76eb8e0dbe060b8490cb49c001c39 (patch)
tree433c773997e51f22738acb8b04c4fd2fc6b1bdc9 /source/base/base_platform.h
init: base
Diffstat (limited to 'source/base/base_platform.h')
-rw-r--r--source/base/base_platform.h199
1 files changed, 199 insertions, 0 deletions
diff --git a/source/base/base_platform.h b/source/base/base_platform.h
new file mode 100644
index 0000000..aa7ba4b
--- /dev/null
+++ b/source/base/base_platform.h
@@ -0,0 +1,199 @@
1#ifndef BASE_PLATFORM_H
2#define BASE_PLATFORM_H
3
4//- Linux
5#if PLATFORM_LINUX || PLATFORM_UNIX
6
7//- Types
8typedef struct SharedLibrary SharedLibrary;
9struct SharedLibrary {
10 void *mem;
11 const char *name;
12};
13
14typedef struct Directory Directory;
15struct Directory {
16 String8List *file_names;
17};
18
19#if 0 // TODO: @WAYLAND and @X11 implementation
20typedef struct LinuxWindowHandle LinuxWindowHandle;
21struct LinuxWindowHandle {
22 Display *display;
23 Window window;
24};
25
26#define LinuxLoadSharedLibrary(library, name) unix_
27#endif
28
29//- Shared libraries
30internal void *unix_shared_library_get_proc_from_name(SharedLibrary *library, const char *name);
31internal void unix_load_shared_library(MemArena *arena, SharedLibrary *library);
32internal int unix_shared_library_close(SharedLibrary *library);
33
34internal String8 unix_get_env(const char *var);
35
36//- File I/O
37internal b32 unix_file_write(String8 data, String8 path);
38internal String8 unix_file_load(MemArena *arena, String8 path);
39internal b32 unix_file_exists(String8 path);
40internal String8List *unix_open_dir(MemArena *arena,
41 String8 *original_path); // TODO: change the return value
42
43internal s64 unix_read(int fd, const void *buf, u64 count);
44internal void unix_os_exit() __attribute__((noreturn));
45
46internal s64 unix_log(int fd, const void *buf, u64 count);
47internal void unix_log_with_args(const char *format, ...);
48
49#define FileLoad(arena, path) unix_file_load(arena, path)
50#define FileWrite(data, path) unix_file_write(data, path)
51#define FileExists(path) unix_file_exists(path)
52#define DirLoad(arena, path) unix_open_dir(arena, path)
53
54#define OSExit() unix_os_exit();
55
56#if COMPILER_GCC || COMPILER_CLANG
57// TODO: __VA_ARGS__ CLANG
58#define Log(...) unix_log_with_args(__VA_ARGS__)
59#define LogStr8(message) \
60 Log(Str8Fmt, Str8Arg(message)) // NOTE: for this to wwork we need the /analyze argument
61#endif // COMPILER_GCC || COMPILER_CLANG
62
63//- Entrypoint and platform window layer
64#define ENTRYPOINT() int main(int count, char **args)
65
66#define PlatformCreateWindow(arena, height, width, display_x, display_y) \
67 LinuxCreateWindow(arena, height, width, display_x, display_y)
68
69#define PlatformGetDisplayWidth(display, screen_num) \
70 LinuxDisplayWdith(display, screen_num) // defined externally
71
72#define PlatformGetDisplayHeight(display, screen_num) \
73 LinuxDisplayHeight(display, screen_num) // defined externally
74
75#define PlatformLoadBitmapFromFile(display, window, path, width, height, out, hotspot_x, \
76 hotspot_y) \
77 LinuxLoadBitMapFromFile(display, window, path, width, height, out, hotspot_x, hotspot_y)
78
79//- X11
80#if X11
81internal LinuxWindowHandle x11_create_window(MemArena *arena, u32 window_height, u32 window_width,
82 u32 disp_x, u32 disp_y);
83internal void x11_draw(LinuxWindowHandle *s, s32 start_x, s32 end_x, s32 start_y, s32 end_y);
84internal s32 x11_cleanup(LinuxWindowHandle *s);
85internal s32 x11_read_input();
86
87#define LinuxCreateWindow(arena, height, width, x, y) x11_create_window(arena, height, width, x, y)
88#define LinuxGetDisplayWdith(display, screen_num) DisplayWidth(display, screen_num)
89#define LinuxGetDisplayHeight(display, screen_num) DisplayHeight(display, screen_num);
90#define LinuxLoadBitMapFromFile(display, window, path, width, height, out, hotspot_x, hotspot_y) \
91XReadBitmapFile(display, window, )
92
93#include "base_platform_linux_x11.cpp"
94
95//- Wayland
96#elif WAYLAND
97
98//- protocol values defined in the wayland library. it's a library load less i guess
99global_variable const u32 wayland_display_object_id = 1;
100global_variable const u16 wayland_wl_registry_event_global = 0;
101global_variable const u16 wayland_shm_pool_event_format = 0;
102global_variable const u16 wayland_wl_buffer_event_release = 0;
103global_variable const u16 wayland_xdg_wm_base_event_ping = 0;
104global_variable const u16 wayland_xdg_toplevel_event_configure = 0;
105global_variable const u16 wayland_xdg_toplevel_event_close = 1;
106global_variable const u16 wayland_xdg_surface_event_configure = 0;
107global_variable const u16 wayland_wl_display_get_registry_opcode = 1;
108global_variable const u16 wayland_wl_registry_bind_opcode = 0;
109global_variable const u16 wayland_wl_compositor_create_surface_opcode = 0;
110global_variable const u16 wayland_xdg_wm_base_pong_opcode = 3;
111global_variable const u16 wayland_xdg_surface_ack_configure_opcode = 4;
112global_variable const u16 wayland_wl_shm_create_pool_opcode = 0;
113global_variable const u16 wayland_xdg_wm_base_get_xdg_surface_opcode = 2;
114global_variable const u16 wayland_wl_shm_pool_create_buffer_opcode = 0;
115global_variable const u16 wayland_wl_surface_attach_opcode = 1;
116global_variable const u16 wayland_xdg_surface_get_toplevel_opcode = 1;
117global_variable const u16 wayland_wl_surface_commit_opcode = 6;
118global_variable const u16 wayland_wl_display_error_event = 0;
119global_variable const u32 wayland_format_xrgb8888 = 1;
120global_variable const u32 wayland_header_size = 8;
121global_variable const u32 color_channels = 4;
122
123#include "base_platform_linux_wayland.cpp"
124
125#endif // X11 and Wayland -- set by build params
126
127//- Windows
128#elif PLATFORM_WINDOWS
129
130//- Types
131typedef struct Win32WindowHandle Win32WindowHandle;
132struct Win32WindowHandle {
133 HWND window_handle;
134 HINSTANCE instance;
135 HDC dc;
136};
137
138global_variable b32 Win32Running = true;
139
140#define STDIN 0
141#define STDOUT 1
142#define STDERR 2
143
144//- Shared libraries
145internal void *win32_load_library();
146internal void win32_library_get_proc();
147
148//- File I/O
149internal b32 win32_file_exists(u8 *path);
150internal b32 win32_file_write(String8 data, String8 path);
151internal String8 win32_file_load(MemArena *arena, String8 file);
152internal String8List win32_open_dir(MemArena *arena, String8 *original_path); // TODO: ...
153
154internal b32 win32_os_exit();
155internal void win32_handle_error(MemArena *arena, DWORD error);
156internal u64 safe_truncate_64(u64 value);
157
158internal void win32_log(String8List messages);
159
160#define FileLoad(arena, path) win32_file_load(arena, path)
161#define FileWrite(data, path) win32_file_write(data, path)
162#define FileExists(path) win32_file_exists(path)
163#define Win32FileExists(path) win32_file_exists(path)
164#define DirLoad(arena, path) win32_open_dir(arena, original_path);
165#define OSExit() win32_os_exit();
166
167#if COMPILER_GCC || COMPILER_CLANG
168
169#define PRINTF_FORMAT(fmt, args) __attribute__((format(printf, fmt, args)))
170internal void win32_logf(const char *fmt, ...) PRINTF_FORMAT;
171
172#elif COMPILER_MSVC
173internal void win32_logf(_Printf_format_string_ const char *fmt, ...);
174
175#define Log(fmt, ...) win32_logf(fmt, __VA_ARGS__)
176
177#endif // COMPILER_GCC || COMPILER_CLANG
178
179//- Entrypoint and platform window layer
180#if COMPILER_MSVC
181#pragma warning(disable : 28251) // disabled the annotations warning that are defined in winbase.h
182#define ENTRYPOINT(hinstance, prev_instance, cmd_line, show_cmd) \
183 int WINAPI WinMain(hinstance, prev_instance, cmd_line, show_cmd)
184#endif // COMPIPILER_MSVC DISABLE WARNING
185
186internal LRESULT CALLBACK win32_window_proc(HWND handle, UINT messages, WPARAM wparam,
187 LPARAM lparam);
188internal HWND win32_create_window(HINSTANCE instance, int height, int width, int display_x,
189 int display_y, const char *window_name);
190
191#define Win32CreateWindow(instance, height, width, display_x, display_y) \
192win32_create_window(instance, height, width, display_x, display_y)
193
194#define PlatformCreateWindow(instance, height, width, display_x, display_y) \
195 Win32CreateWindow(instance, height, width, display_x, display_y)
196
197#endif // PLATFORM_LINUX || PLATFORM_WINDOWS
198
199#endif // BASE_PLATFORM_H