diff options
| author | nasr <nsrddyn@gmail.com> | 2026-10-05 22:02:56 +0200 |
|---|---|---|
| committer | nasr <nsrddyn@gmail.com> | 2026-10-05 22:02:56 +0200 |
| commit | f6d3090f7fa76eb8e0dbe060b8490cb49c001c39 (patch) | |
| tree | 433c773997e51f22738acb8b04c4fd2fc6b1bdc9 /source/base/base_platform_win32.cpp | |
init: base
Diffstat (limited to 'source/base/base_platform_win32.cpp')
| -rw-r--r-- | source/base/base_platform_win32.cpp | 206 |
1 files changed, 206 insertions, 0 deletions
diff --git a/source/base/base_platform_win32.cpp b/source/base/base_platform_win32.cpp new file mode 100644 index 0000000..1b576ee --- /dev/null +++ b/source/base/base_platform_win32.cpp | |||
| @@ -0,0 +1,206 @@ | |||
| 1 | |||
| 2 | internal void | ||
| 3 | #if COMPILER_GCC || COMPILER_CLANG | ||
| 4 | win32_logf(const char *fmt, ...) PRINTF_FORMAT | ||
| 5 | #elif COMPILER_MSVC | ||
| 6 | win32_logf(_Printf_format_string_ const char *fmt, ...) | ||
| 7 | #endif | ||
| 8 | { | ||
| 9 | char buffer[4096]; | ||
| 10 | va_list args; | ||
| 11 | va_start(args, fmt); | ||
| 12 | int len = vsnprintf(buffer, sizeof(buffer), fmt, args); | ||
| 13 | va_end(args); | ||
| 14 | |||
| 15 | if (len < 0) | ||
| 16 | return; | ||
| 17 | if ((u64)len >= sizeof(buffer)) | ||
| 18 | len = sizeof(buffer) - 1; | ||
| 19 | |||
| 20 | HANDLE handle_stdout = GetStdHandle(STD_OUTPUT_HANDLE); | ||
| 21 | DWORD written = 0; | ||
| 22 | WriteConsoleA(handle_stdout, buffer, (DWORD)len, &written, NULL); | ||
| 23 | } | ||
| 24 | |||
| 25 | internal void win32_log(String8List messages) | ||
| 26 | { | ||
| 27 | HANDLE handle_stdout = GetStdHandle(STD_OUTPUT_HANDLE); | ||
| 28 | u64 count = 0; | ||
| 29 | for (String8Node *node = messages.first; count < messages.count; node = node->next) { | ||
| 30 | DWORD written = 0; | ||
| 31 | WriteConsoleA(handle_stdout, node->value.data, (DWORD)(node->value.size), &written, NULL); | ||
| 32 | ++count; | ||
| 33 | } | ||
| 34 | } | ||
| 35 | |||
| 36 | // exit win32 | ||
| 37 | // returns a boolean. i don't know what to do with the boolean if it fails hahahahhaa | ||
| 38 | // not like you can exit hahahahaha0 | ||
| 39 | internal b32 win32_os_exit() | ||
| 40 | { | ||
| 41 | ExitProcess(0); | ||
| 42 | return true; | ||
| 43 | } | ||
| 44 | |||
| 45 | // TODO: remove FORMAT_MESSAGE_ALLOCATE_BUFFER flag | ||
| 46 | internal void win32_handle_error(MemArena *arena, DWORD error) | ||
| 47 | { | ||
| 48 | ScratchArena scratch = scratch_start(arena); | ||
| 49 | String8 error_buffer = PushString8(arena, 255); | ||
| 50 | DWORD error_result = GetLastError(); | ||
| 51 | |||
| 52 | DWORD p_buffer = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, | ||
| 53 | NULL, error_result, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), | ||
| 54 | (LPTSTR)&error_buffer.data, 255, NULL); | ||
| 55 | |||
| 56 | Log("Error code: %lu\n", error_result); | ||
| 57 | Log("Error formatted message: %.*s\n", Str8Arg(error_buffer)); | ||
| 58 | scratch_end(&scratch); | ||
| 59 | } | ||
| 60 | |||
| 61 | internal u64 safe_truncate_64(u64 value) | ||
| 62 | { | ||
| 63 | Assert(value <= (u64)~0); | ||
| 64 | return (u64)value; | ||
| 65 | } | ||
| 66 | |||
| 67 | internal b32 win32_file_exists(u8 *path) | ||
| 68 | { | ||
| 69 | DWORD attributes = GetFileAttributesA((LPCSTR)path); | ||
| 70 | return ((attributes != INVALID_FILE_ATTRIBUTES) && !(attributes & FILE_ATTRIBUTE_DIRECTORY)); | ||
| 71 | } | ||
| 72 | |||
| 73 | internal b32 win32_file_write(String8 data, String8 path) | ||
| 74 | { | ||
| 75 | b32 result = false; | ||
| 76 | |||
| 77 | HANDLE file_handle = CreateFileA((LPCSTR)path.data, GENERIC_WRITE, 0, 0, CREATE_ALWAYS, 0, 0); | ||
| 78 | |||
| 79 | if (file_handle != INVALID_HANDLE_VALUE) { | ||
| 80 | DWORD bytes_written; | ||
| 81 | if (WriteFile(file_handle, data.data, (DWORD)data.size, &bytes_written, 0)) { | ||
| 82 | result = (bytes_written == data.size); | ||
| 83 | } else { | ||
| 84 | Log("Writing the file was unsuccessful"); | ||
| 85 | } | ||
| 86 | CloseHandle(file_handle); | ||
| 87 | } else { | ||
| 88 | Log("File handle has the INVALID_HANDLE_VALUE flag."); | ||
| 89 | } | ||
| 90 | |||
| 91 | return result; | ||
| 92 | } | ||
| 93 | |||
| 94 | internal String8 win32_file_load(MemArena *arena, String8 file) | ||
| 95 | { | ||
| 96 | String8 result = {0}; | ||
| 97 | HANDLE file_handle = | ||
| 98 | CreateFileA((LPCSTR)file.data, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); | ||
| 99 | |||
| 100 | if (file_handle == INVALID_HANDLE_VALUE) { | ||
| 101 | Log("Failed to open file\n"); | ||
| 102 | return result; | ||
| 103 | } | ||
| 104 | |||
| 105 | LARGE_INTEGER size; | ||
| 106 | |||
| 107 | DWORD get_file_size_result = GetFileSizeEx(file_handle, &size); | ||
| 108 | if (!get_file_size_result) { | ||
| 109 | Log("Failed to get file size"); | ||
| 110 | win32_handle_error(arena, get_file_size_result); | ||
| 111 | CloseHandle(file_handle); | ||
| 112 | return result; | ||
| 113 | } | ||
| 114 | |||
| 115 | ScratchArena file_load_scratch = scratch_start(arena); | ||
| 116 | u32 file_size32 = (u32)safe_truncate_64(size.QuadPart); | ||
| 117 | result = PushString8(arena, file_size32); | ||
| 118 | |||
| 119 | // return value in case of success should be true | ||
| 120 | |||
| 121 | DWORD bytes_read = 0; | ||
| 122 | ; | ||
| 123 | DWORD read_file_result = ReadFile(file_handle, (LPSTR)result.data, file_size32, &bytes_read, 0); | ||
| 124 | |||
| 125 | if (!read_file_result || (file_size32 != bytes_read)) { | ||
| 126 | Log("Failed to read file\n"); | ||
| 127 | win32_handle_error(arena, read_file_result); | ||
| 128 | CloseHandle(file_handle); | ||
| 129 | return result; | ||
| 130 | } | ||
| 131 | |||
| 132 | result.size = file_size32; | ||
| 133 | // TODO: find out if we want to free this or not. | ||
| 134 | CloseHandle(file_handle); | ||
| 135 | return result; | ||
| 136 | } | ||
| 137 | |||
| 138 | internal void *win32_load_library() | ||
| 139 | { | ||
| 140 | // TODO: ... | ||
| 141 | } | ||
| 142 | |||
| 143 | internal void win32_library_get_proc() | ||
| 144 | { | ||
| 145 | // TODO: ... | ||
| 146 | } | ||
| 147 | |||
| 148 | internal LRESULT CALLBACK win32_window_proc(HWND handle, UINT messages, WPARAM wparam, | ||
| 149 | LPARAM lparam) | ||
| 150 | { | ||
| 151 | |||
| 152 | switch (messages) { | ||
| 153 | case WM_DESTROY: | ||
| 154 | Win32Running = false; | ||
| 155 | break; | ||
| 156 | case WM_PAINT: { | ||
| 157 | } break; | ||
| 158 | case WM_SYSKEYDOWN: | ||
| 159 | case WM_SYSKEYUP: | ||
| 160 | case WM_KEYDOWN: | ||
| 161 | case WM_KEYUP: { | ||
| 162 | // TODO: ... | ||
| 163 | } break; | ||
| 164 | } | ||
| 165 | |||
| 166 | return DefWindowProcA(handle, messages, wparam, lparam); | ||
| 167 | } | ||
| 168 | |||
| 169 | internal HWND win32_create_window(HINSTANCE instance, int height, int width, int display_x, | ||
| 170 | int display_y, const char *window_name) | ||
| 171 | { | ||
| 172 | Win32Running = true; | ||
| 173 | WNDCLASS window_class = {0}; | ||
| 174 | |||
| 175 | // TODO(nasr): check if CS_OWNDC CS_HREDRAW CS_VEREDRAW still matter | ||
| 176 | window_class.style = CS_OWNDC | CS_HREDRAW | CS_VREDRAW; | ||
| 177 | window_class.lpfnWndProc = win32_window_proc; | ||
| 178 | window_class.hInstance = instance; | ||
| 179 | window_class.lpszClassName = window_name; | ||
| 180 | |||
| 181 | HWND window_handle = {}; | ||
| 182 | if (RegisterClass(&window_class)) { | ||
| 183 | window_handle = CreateWindowEx(0, window_class.lpszClassName, window_name, | ||
| 184 | WS_OVERLAPPEDWINDOW | WS_VISIBLE, height, width, display_x, | ||
| 185 | display_y, 0, 0, instance, 0); | ||
| 186 | } | ||
| 187 | |||
| 188 | Assert(window_handle); | ||
| 189 | ShowWindow(window_handle, true); | ||
| 190 | |||
| 191 | MSG message = {}; | ||
| 192 | while (Win32Running) { | ||
| 193 | Log("status : %d", Win32Running); | ||
| 194 | b32 result = GetMessage(&message, 0, 0, 0); | ||
| 195 | if (result > 0) { | ||
| 196 | TranslateMessage(&message); | ||
| 197 | DispatchMessage(&message); | ||
| 198 | } else { | ||
| 199 | break; | ||
| 200 | } | ||
| 201 | } // cleanup get's handled by the OS, so there is no need worry about that | ||
| 202 | |||
| 203 | return window_handle; | ||
| 204 | } | ||
| 205 | |||
| 206 | internal String8List win32_open_dir(MemArena *arena, String8 *original_path) {} | ||
