diff options
Diffstat (limited to 'source/base/base_platform.h')
| -rw-r--r-- | source/base/base_platform.h | 199 |
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 | ||
| 8 | typedef struct SharedLibrary SharedLibrary; | ||
| 9 | struct SharedLibrary { | ||
| 10 | void *mem; | ||
| 11 | const char *name; | ||
| 12 | }; | ||
| 13 | |||
| 14 | typedef struct Directory Directory; | ||
| 15 | struct Directory { | ||
| 16 | String8List *file_names; | ||
| 17 | }; | ||
| 18 | |||
| 19 | #if 0 // TODO: @WAYLAND and @X11 implementation | ||
| 20 | typedef struct LinuxWindowHandle LinuxWindowHandle; | ||
| 21 | struct LinuxWindowHandle { | ||
| 22 | Display *display; | ||
| 23 | Window window; | ||
| 24 | }; | ||
| 25 | |||
| 26 | #define LinuxLoadSharedLibrary(library, name) unix_ | ||
| 27 | #endif | ||
| 28 | |||
| 29 | //- Shared libraries | ||
| 30 | internal void *unix_shared_library_get_proc_from_name(SharedLibrary *library, const char *name); | ||
| 31 | internal void unix_load_shared_library(MemArena *arena, SharedLibrary *library); | ||
| 32 | internal int unix_shared_library_close(SharedLibrary *library); | ||
| 33 | |||
| 34 | internal String8 unix_get_env(const char *var); | ||
| 35 | |||
| 36 | //- File I/O | ||
| 37 | internal b32 unix_file_write(String8 data, String8 path); | ||
| 38 | internal String8 unix_file_load(MemArena *arena, String8 path); | ||
| 39 | internal b32 unix_file_exists(String8 path); | ||
| 40 | internal String8List *unix_open_dir(MemArena *arena, | ||
| 41 | String8 *original_path); // TODO: change the return value | ||
| 42 | |||
| 43 | internal s64 unix_read(int fd, const void *buf, u64 count); | ||
| 44 | internal void unix_os_exit() __attribute__((noreturn)); | ||
| 45 | |||
| 46 | internal s64 unix_log(int fd, const void *buf, u64 count); | ||
| 47 | internal 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 | ||
| 81 | internal LinuxWindowHandle x11_create_window(MemArena *arena, u32 window_height, u32 window_width, | ||
| 82 | u32 disp_x, u32 disp_y); | ||
| 83 | internal void x11_draw(LinuxWindowHandle *s, s32 start_x, s32 end_x, s32 start_y, s32 end_y); | ||
| 84 | internal s32 x11_cleanup(LinuxWindowHandle *s); | ||
| 85 | internal 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) \ | ||
| 91 | XReadBitmapFile(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 | ||
| 99 | global_variable const u32 wayland_display_object_id = 1; | ||
| 100 | global_variable const u16 wayland_wl_registry_event_global = 0; | ||
| 101 | global_variable const u16 wayland_shm_pool_event_format = 0; | ||
| 102 | global_variable const u16 wayland_wl_buffer_event_release = 0; | ||
| 103 | global_variable const u16 wayland_xdg_wm_base_event_ping = 0; | ||
| 104 | global_variable const u16 wayland_xdg_toplevel_event_configure = 0; | ||
| 105 | global_variable const u16 wayland_xdg_toplevel_event_close = 1; | ||
| 106 | global_variable const u16 wayland_xdg_surface_event_configure = 0; | ||
| 107 | global_variable const u16 wayland_wl_display_get_registry_opcode = 1; | ||
| 108 | global_variable const u16 wayland_wl_registry_bind_opcode = 0; | ||
| 109 | global_variable const u16 wayland_wl_compositor_create_surface_opcode = 0; | ||
| 110 | global_variable const u16 wayland_xdg_wm_base_pong_opcode = 3; | ||
| 111 | global_variable const u16 wayland_xdg_surface_ack_configure_opcode = 4; | ||
| 112 | global_variable const u16 wayland_wl_shm_create_pool_opcode = 0; | ||
| 113 | global_variable const u16 wayland_xdg_wm_base_get_xdg_surface_opcode = 2; | ||
| 114 | global_variable const u16 wayland_wl_shm_pool_create_buffer_opcode = 0; | ||
| 115 | global_variable const u16 wayland_wl_surface_attach_opcode = 1; | ||
| 116 | global_variable const u16 wayland_xdg_surface_get_toplevel_opcode = 1; | ||
| 117 | global_variable const u16 wayland_wl_surface_commit_opcode = 6; | ||
| 118 | global_variable const u16 wayland_wl_display_error_event = 0; | ||
| 119 | global_variable const u32 wayland_format_xrgb8888 = 1; | ||
| 120 | global_variable const u32 wayland_header_size = 8; | ||
| 121 | global_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 | ||
| 131 | typedef struct Win32WindowHandle Win32WindowHandle; | ||
| 132 | struct Win32WindowHandle { | ||
| 133 | HWND window_handle; | ||
| 134 | HINSTANCE instance; | ||
| 135 | HDC dc; | ||
| 136 | }; | ||
| 137 | |||
| 138 | global_variable b32 Win32Running = true; | ||
| 139 | |||
| 140 | #define STDIN 0 | ||
| 141 | #define STDOUT 1 | ||
| 142 | #define STDERR 2 | ||
| 143 | |||
| 144 | //- Shared libraries | ||
| 145 | internal void *win32_load_library(); | ||
| 146 | internal void win32_library_get_proc(); | ||
| 147 | |||
| 148 | //- File I/O | ||
| 149 | internal b32 win32_file_exists(u8 *path); | ||
| 150 | internal b32 win32_file_write(String8 data, String8 path); | ||
| 151 | internal String8 win32_file_load(MemArena *arena, String8 file); | ||
| 152 | internal String8List win32_open_dir(MemArena *arena, String8 *original_path); // TODO: ... | ||
| 153 | |||
| 154 | internal b32 win32_os_exit(); | ||
| 155 | internal void win32_handle_error(MemArena *arena, DWORD error); | ||
| 156 | internal u64 safe_truncate_64(u64 value); | ||
| 157 | |||
| 158 | internal 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))) | ||
| 170 | internal void win32_logf(const char *fmt, ...) PRINTF_FORMAT; | ||
| 171 | |||
| 172 | #elif COMPILER_MSVC | ||
| 173 | internal 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 | |||
| 186 | internal LRESULT CALLBACK win32_window_proc(HWND handle, UINT messages, WPARAM wparam, | ||
| 187 | LPARAM lparam); | ||
| 188 | internal 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) \ | ||
| 192 | win32_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 | ||
