2016-02-20 21:36:16 +00:00
|
|
|
/*
|
|
|
|
* Mr. 4th Dimention - Allen Webster
|
|
|
|
*
|
|
|
|
* 12.12.2014
|
|
|
|
*
|
2017-01-07 02:59:55 +00:00
|
|
|
* Win32 layer for 4coder
|
2016-02-20 21:36:16 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
// TOP
|
|
|
|
|
2017-06-30 21:28:09 +00:00
|
|
|
#define IS_PLAT_LAYER
|
2017-07-18 20:51:44 +00:00
|
|
|
#include "4ed_os_comp_cracking.h"
|
2017-02-25 01:53:40 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Program setup
|
|
|
|
//
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
#define UNICODE
|
|
|
|
|
|
|
|
#define FPS 60
|
|
|
|
#define frame_useconds (1000000 / FPS)
|
|
|
|
|
2017-07-07 23:54:50 +00:00
|
|
|
#include "4ed_defines.h"
|
2017-01-23 06:19:43 +00:00
|
|
|
#include "4coder_API/version.h"
|
2016-08-30 19:30:41 +00:00
|
|
|
|
2017-06-30 01:13:20 +00:00
|
|
|
#define WINDOW_NAME L"4coder: " L_VERSION
|
|
|
|
|
|
|
|
#include <string.h>
|
2017-02-18 01:04:41 +00:00
|
|
|
#include "4coder_lib/4coder_utf8.h"
|
|
|
|
|
2016-08-31 02:18:47 +00:00
|
|
|
#if defined(FRED_SUPER)
|
2017-01-23 06:19:43 +00:00
|
|
|
# include "4coder_API/keycodes.h"
|
|
|
|
# include "4coder_API/style.h"
|
2016-08-30 19:30:41 +00:00
|
|
|
|
|
|
|
# define FSTRING_IMPLEMENTATION
|
2017-01-23 06:19:43 +00:00
|
|
|
# include "4coder_lib/4coder_string.h"
|
|
|
|
# include "4coder_lib/4coder_mem.h"
|
2016-08-30 19:30:41 +00:00
|
|
|
|
2017-01-23 06:19:43 +00:00
|
|
|
# include "4coder_API/types.h"
|
|
|
|
# include "4ed_os_custom_api.h"
|
2016-08-30 19:30:41 +00:00
|
|
|
|
2016-08-28 17:31:43 +00:00
|
|
|
#else
|
|
|
|
# include "4coder_default_bindings.cpp"
|
2016-08-30 19:30:41 +00:00
|
|
|
#endif
|
2016-08-29 02:14:02 +00:00
|
|
|
|
2016-08-29 01:03:26 +00:00
|
|
|
#include "4ed_math.h"
|
2016-02-20 21:36:16 +00:00
|
|
|
|
|
|
|
#include "4ed_system.h"
|
2017-06-12 21:35:06 +00:00
|
|
|
#include "4ed_log.h"
|
2016-02-20 21:36:16 +00:00
|
|
|
#include "4ed_rendering.h"
|
|
|
|
#include "4ed.h"
|
|
|
|
|
2016-08-29 02:14:02 +00:00
|
|
|
#include <Windows.h>
|
2016-02-20 21:36:16 +00:00
|
|
|
#include <GL/gl.h>
|
2017-01-29 00:03:23 +00:00
|
|
|
#include "win32_gl.h"
|
2016-08-29 13:57:05 +00:00
|
|
|
|
|
|
|
#define GL_TEXTURE_MAX_LEVEL 0x813D
|
2016-02-20 21:36:16 +00:00
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
//////////////////////////////
|
2016-06-01 16:03:45 +00:00
|
|
|
|
2017-03-23 22:45:32 +00:00
|
|
|
#include "win32_utf8.h"
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
#include "4ed_file_track.h"
|
2017-03-23 23:40:16 +00:00
|
|
|
#include "4ed_font_interface_to_os.h"
|
2017-03-18 21:07:25 +00:00
|
|
|
#include "4ed_system_shared.h"
|
2016-02-20 21:36:16 +00:00
|
|
|
|
2017-07-18 20:17:57 +00:00
|
|
|
#include "4ed_shared_thread_constants.h"
|
|
|
|
#include "win32_threading_wrapper.h"
|
|
|
|
|
2016-05-27 17:11:38 +00:00
|
|
|
//
|
|
|
|
// Win32_Vars structs
|
|
|
|
//
|
|
|
|
|
2017-03-18 21:07:25 +00:00
|
|
|
#define WM_4coder_ANIMATE (WM_USER + 0)
|
|
|
|
|
2016-02-20 21:36:16 +00:00
|
|
|
struct Control_Keys{
|
|
|
|
b8 l_ctrl;
|
|
|
|
b8 r_ctrl;
|
|
|
|
b8 l_alt;
|
|
|
|
b8 r_alt;
|
|
|
|
};
|
2017-07-18 16:41:40 +00:00
|
|
|
global Control_Keys null_control_keys = {0};
|
2016-02-20 21:36:16 +00:00
|
|
|
|
|
|
|
struct Win32_Input_Chunk_Transient{
|
|
|
|
Key_Input_Data key_data;
|
|
|
|
b8 mouse_l_press, mouse_l_release;
|
|
|
|
b8 mouse_r_press, mouse_r_release;
|
2016-02-27 07:44:17 +00:00
|
|
|
b8 out_of_window;
|
|
|
|
i8 mouse_wheel;
|
2016-03-16 16:50:26 +00:00
|
|
|
b8 trying_to_kill;
|
2016-02-20 21:36:16 +00:00
|
|
|
};
|
2017-07-18 16:41:40 +00:00
|
|
|
global Win32_Input_Chunk_Transient null_input_chunk_transient = {0};
|
2016-02-20 21:36:16 +00:00
|
|
|
|
|
|
|
struct Win32_Input_Chunk_Persistent{
|
|
|
|
i32 mouse_x, mouse_y;
|
|
|
|
b8 mouse_l, mouse_r;
|
|
|
|
|
|
|
|
Control_Keys controls;
|
2016-02-26 17:40:51 +00:00
|
|
|
b8 control_keys[MDFR_INDEX_COUNT];
|
2016-02-20 21:36:16 +00:00
|
|
|
};
|
|
|
|
|
2017-07-18 16:41:40 +00:00
|
|
|
struct Win32_Input_Chunk{
|
2016-02-20 21:36:16 +00:00
|
|
|
Win32_Input_Chunk_Transient trans;
|
|
|
|
Win32_Input_Chunk_Persistent pers;
|
2017-07-18 16:41:40 +00:00
|
|
|
};
|
2016-02-20 21:36:16 +00:00
|
|
|
|
2017-07-19 00:19:38 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2017-07-19 16:33:12 +00:00
|
|
|
#define SLASH '\\'
|
|
|
|
#define DLL "dll"
|
|
|
|
|
2017-07-19 00:23:38 +00:00
|
|
|
global System_Functions sysfunc;
|
2017-07-19 16:33:12 +00:00
|
|
|
#include "4ed_shared_library_constants.h"
|
|
|
|
#include "win32_library_wrapper.h"
|
2017-07-19 00:19:38 +00:00
|
|
|
#include "4ed_standard_libraries.cpp"
|
|
|
|
|
2017-07-19 20:07:50 +00:00
|
|
|
#include "4ed_coroutine.cpp"
|
|
|
|
|
2017-07-19 00:19:38 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2017-06-12 21:35:06 +00:00
|
|
|
struct Win32_Vars{
|
2016-05-29 19:05:27 +00:00
|
|
|
Win32_Input_Chunk input_chunk;
|
|
|
|
b32 lctrl_lalt_is_altgr;
|
|
|
|
b32 got_useful_event;
|
|
|
|
|
2016-09-01 20:29:07 +00:00
|
|
|
b32 full_screen;
|
|
|
|
b32 do_toggle;
|
2017-07-18 22:34:57 +00:00
|
|
|
WINDOWPLACEMENT bordered_win_pos;
|
2016-09-01 20:29:07 +00:00
|
|
|
b32 send_exit_signal;
|
2016-05-29 19:05:27 +00:00
|
|
|
|
|
|
|
HCURSOR cursor_ibeam;
|
|
|
|
HCURSOR cursor_arrow;
|
|
|
|
HCURSOR cursor_leftright;
|
|
|
|
HCURSOR cursor_updown;
|
2017-10-31 17:29:52 +00:00
|
|
|
i32 cursor_show;
|
|
|
|
i32 prev_cursor_show;
|
2017-03-23 23:40:16 +00:00
|
|
|
|
|
|
|
u8 *clip_buffer;
|
|
|
|
u32 clip_max;
|
2016-05-29 19:05:27 +00:00
|
|
|
String clipboard_contents;
|
|
|
|
b32 next_clipboard_is_self;
|
|
|
|
DWORD clipboard_sequence;
|
|
|
|
|
|
|
|
HWND window_handle;
|
2016-06-01 16:03:45 +00:00
|
|
|
i32 dpi_x, dpi_y;
|
2016-05-29 19:05:27 +00:00
|
|
|
|
2017-06-16 20:57:02 +00:00
|
|
|
f64 count_per_usecond;
|
2016-02-20 21:36:16 +00:00
|
|
|
b32 first;
|
2016-05-29 19:05:27 +00:00
|
|
|
i32 running_cli;
|
|
|
|
|
2017-06-12 17:40:54 +00:00
|
|
|
u32 log_position;
|
2017-06-12 21:35:06 +00:00
|
|
|
};
|
2016-02-20 21:36:16 +00:00
|
|
|
|
2017-07-18 19:52:41 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2017-01-23 06:19:43 +00:00
|
|
|
global Win32_Vars win32vars;
|
2017-07-18 23:17:40 +00:00
|
|
|
global Render_Target target;
|
2017-01-23 06:19:43 +00:00
|
|
|
global Application_Memory memory_vars;
|
2017-07-18 21:19:28 +00:00
|
|
|
global Plat_Settings plat_settings;
|
2016-02-20 21:36:16 +00:00
|
|
|
|
2017-07-19 00:19:38 +00:00
|
|
|
global Libraries libraries;
|
|
|
|
global App_Functions app;
|
2017-07-19 20:07:50 +00:00
|
|
|
global Custom_API custom_api;
|
|
|
|
|
|
|
|
global Coroutine_System_Auto_Alloc coroutines;
|
2017-07-19 00:19:38 +00:00
|
|
|
|
2017-07-18 17:44:54 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2017-07-18 23:41:28 +00:00
|
|
|
#include "win32_error_box.cpp"
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
2017-07-18 17:44:54 +00:00
|
|
|
internal HANDLE
|
|
|
|
handle_type(Plat_Handle h){
|
|
|
|
HANDLE result;
|
|
|
|
memcpy(&result, &h, sizeof(result));
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal Plat_Handle
|
|
|
|
handle_type(HANDLE h){
|
|
|
|
Plat_Handle result = {0};
|
|
|
|
memcpy(&result, &h, sizeof(h));
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
2017-07-18 21:29:39 +00:00
|
|
|
#include "win32_4ed_functions.cpp"
|
|
|
|
#include "4ed_shared_file_handling.cpp"
|
2017-07-18 19:52:41 +00:00
|
|
|
|
2017-07-18 20:51:44 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
2017-07-18 21:29:39 +00:00
|
|
|
internal void
|
|
|
|
system_schedule_step(){
|
|
|
|
PostMessage(win32vars.window_handle, WM_4coder_ANIMATE, 0, 0);
|
2017-07-18 20:51:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
|
|
|
|
#include "4ed_work_queues.cpp"
|
|
|
|
|
2017-07-18 22:09:16 +00:00
|
|
|
////////////////////////////////
|
|
|
|
|
|
|
|
// HACK(allen): Get this shit working more properly (look at pens)
|
|
|
|
internal void
|
|
|
|
win32_toggle_fullscreen(){
|
2017-07-18 22:34:57 +00:00
|
|
|
HWND win = win32vars.window_handle;
|
|
|
|
DWORD style = GetWindowLongW(win, GWL_STYLE);
|
2017-10-31 16:52:39 +00:00
|
|
|
b32 is_full = ((style & WS_OVERLAPPEDWINDOW) == 0);
|
|
|
|
if (!is_full){
|
2017-07-18 22:34:57 +00:00
|
|
|
MONITORINFO info = {sizeof(MONITORINFO)};
|
|
|
|
if (GetWindowPlacement(win, &win32vars.bordered_win_pos) && GetMonitorInfo(MonitorFromWindow(win, MONITOR_DEFAULTTOPRIMARY), &info)){
|
|
|
|
SetWindowLongW(win, GWL_STYLE, style & ~WS_OVERLAPPEDWINDOW);
|
|
|
|
|
|
|
|
i32 x = info.rcMonitor.left;
|
|
|
|
i32 y = info.rcMonitor.top;
|
|
|
|
i32 w = info.rcMonitor.right - info.rcMonitor.left;
|
|
|
|
i32 h = info.rcMonitor.bottom - info.rcMonitor.top;
|
|
|
|
|
|
|
|
SetWindowPos(win, HWND_TOP, x, y, w, h, SWP_NOOWNERZORDER | SWP_FRAMECHANGED | SWP_SHOWWINDOW);
|
2017-07-18 22:09:16 +00:00
|
|
|
win32vars.full_screen = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
2017-07-18 22:34:57 +00:00
|
|
|
SetWindowLongW(win, GWL_STYLE, style | WS_OVERLAPPEDWINDOW);
|
|
|
|
SetWindowPlacement(win, &win32vars.bordered_win_pos);
|
|
|
|
SetWindowPos(win, 0, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
|
2017-07-18 22:09:16 +00:00
|
|
|
win32vars.full_screen = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(allen): add a "shown but auto-hides on timer" setting here.
|
|
|
|
internal
|
|
|
|
Sys_Show_Mouse_Cursor_Sig(system_show_mouse_cursor){
|
2017-10-31 17:29:52 +00:00
|
|
|
win32vars.cursor_show = show;
|
2017-07-18 22:09:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
|
|
|
Sys_Set_Fullscreen_Sig(system_set_fullscreen){
|
2017-07-18 22:34:57 +00:00
|
|
|
// NOTE(allen): If the new value of full_screen does not match the current value,
|
|
|
|
// set toggle to true.
|
|
|
|
win32vars.do_toggle = (win32vars.full_screen != full_screen);
|
|
|
|
b32 success = true;
|
2017-07-18 22:09:16 +00:00
|
|
|
return(success);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
|
|
|
Sys_Is_Fullscreen_Sig(system_is_fullscreen){
|
|
|
|
// NOTE(allen): Report the fullscreen status as it would be set at the beginning of the next frame.
|
|
|
|
// That is, take into account all fullscreen toggle requests that have come in already this frame.
|
|
|
|
// Read: "full_screen XOR do_toggle"
|
|
|
|
b32 result = (win32vars.full_screen != win32vars.do_toggle);
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
|
|
|
Sys_Send_Exit_Signal_Sig(system_send_exit_signal){
|
|
|
|
win32vars.send_exit_signal = true;
|
|
|
|
}
|
|
|
|
|
2017-09-10 19:14:41 +00:00
|
|
|
#include "4ed_coroutine_functions.cpp"
|
2017-07-18 20:51:44 +00:00
|
|
|
|
2016-05-29 19:28:29 +00:00
|
|
|
//
|
|
|
|
// Clipboard
|
|
|
|
//
|
2016-02-27 07:44:17 +00:00
|
|
|
|
|
|
|
internal
|
2016-05-29 19:28:29 +00:00
|
|
|
Sys_Post_Clipboard_Sig(system_post_clipboard){
|
2017-01-03 20:05:35 +00:00
|
|
|
if (OpenClipboard(win32vars.window_handle)){
|
|
|
|
EmptyClipboard();
|
|
|
|
HANDLE memory_handle;
|
|
|
|
memory_handle = GlobalAlloc(GMEM_MOVEABLE, str.size+1);
|
|
|
|
if (memory_handle){
|
|
|
|
char *dest = (char*)GlobalLock(memory_handle);
|
2016-08-28 04:31:06 +00:00
|
|
|
copy_fast_unsafe_cs(dest, str);
|
2017-01-03 20:05:35 +00:00
|
|
|
GlobalUnlock(memory_handle);
|
|
|
|
SetClipboardData(CF_TEXT, memory_handle);
|
|
|
|
win32vars.next_clipboard_is_self = 1;
|
|
|
|
}
|
|
|
|
CloseClipboard();
|
|
|
|
}
|
2016-02-26 05:50:19 +00:00
|
|
|
}
|
|
|
|
|
2016-05-29 19:28:29 +00:00
|
|
|
internal b32
|
2017-03-23 23:40:16 +00:00
|
|
|
win32_read_clipboard_contents(){
|
|
|
|
b32 result = false;
|
2016-02-26 17:40:51 +00:00
|
|
|
|
2017-03-23 23:40:16 +00:00
|
|
|
if (IsClipboardFormatAvailable(CF_UNICODETEXT)){
|
|
|
|
result = true;
|
2016-05-29 19:28:29 +00:00
|
|
|
if (OpenClipboard(win32vars.window_handle)){
|
2017-03-23 23:40:16 +00:00
|
|
|
HANDLE clip_data = GetClipboardData(CF_UNICODETEXT);
|
|
|
|
if (clip_data != 0){
|
|
|
|
Partition *scratch = &shared_vars.scratch;
|
|
|
|
Temp_Memory temp = begin_temp_memory(scratch);
|
|
|
|
|
|
|
|
u16 *clip_16 = (u16*)GlobalLock(clip_data);
|
|
|
|
|
|
|
|
if (clip_16 != 0){
|
|
|
|
u32 clip_16_len = 0;
|
|
|
|
for(;clip_16[clip_16_len];++clip_16_len);
|
|
|
|
|
|
|
|
// TODO(allen): Extend the buffer if it is too small.
|
|
|
|
b32 error = false;
|
|
|
|
u32 clip_8_len = (u32)utf16_to_utf8_minimal_checking(win32vars.clip_buffer, win32vars.clip_max-1, clip_16, clip_16_len, &error);
|
|
|
|
|
|
|
|
if (clip_8_len < win32vars.clip_max && !error){
|
|
|
|
win32vars.clip_buffer[clip_8_len] = 0;
|
|
|
|
|
|
|
|
win32vars.clipboard_contents = make_string_cap(win32vars.clip_buffer, clip_8_len, win32vars.clip_max);
|
|
|
|
}
|
|
|
|
|
|
|
|
GlobalUnlock(clip_data);
|
|
|
|
}
|
|
|
|
|
2016-05-29 19:28:29 +00:00
|
|
|
if (win32vars.clipboard_contents.str){
|
|
|
|
win32vars.clipboard_contents.size = str_size((char*)win32vars.clipboard_contents.str);
|
|
|
|
}
|
2017-03-23 23:40:16 +00:00
|
|
|
|
|
|
|
end_temp_memory(temp);
|
2016-05-29 19:28:29 +00:00
|
|
|
}
|
|
|
|
CloseClipboard();
|
|
|
|
}
|
2016-02-26 17:40:51 +00:00
|
|
|
}
|
|
|
|
|
2016-05-29 19:28:29 +00:00
|
|
|
return(result);
|
2016-02-26 05:50:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-05-29 03:12:12 +00:00
|
|
|
//
|
|
|
|
// Command Line Exectuion
|
|
|
|
//
|
|
|
|
|
2016-02-20 21:36:16 +00:00
|
|
|
internal
|
|
|
|
Sys_CLI_Call_Sig(system_cli_call){
|
|
|
|
char cmd[] = "c:\\windows\\system32\\cmd.exe";
|
2017-03-23 19:14:39 +00:00
|
|
|
char *env_variables = 0;
|
2016-02-20 21:36:16 +00:00
|
|
|
char command_line[2048];
|
|
|
|
|
|
|
|
String s = make_fixed_width_string(command_line);
|
2016-08-28 04:31:06 +00:00
|
|
|
copy_ss(&s, make_lit_string("/C "));
|
|
|
|
append_partial_sc(&s, script_name);
|
2016-06-29 17:16:08 +00:00
|
|
|
b32 success = terminate_with_null(&s);
|
2016-02-20 21:36:16 +00:00
|
|
|
|
|
|
|
if (success){
|
|
|
|
success = 0;
|
|
|
|
|
2016-06-29 17:16:08 +00:00
|
|
|
SECURITY_ATTRIBUTES sec_attributes = {};
|
2016-02-20 21:36:16 +00:00
|
|
|
HANDLE out_read;
|
|
|
|
HANDLE out_write;
|
|
|
|
|
|
|
|
sec_attributes.nLength = sizeof(SECURITY_ATTRIBUTES);
|
|
|
|
sec_attributes.bInheritHandle = TRUE;
|
|
|
|
|
|
|
|
if (CreatePipe(&out_read, &out_write, &sec_attributes, 0)){
|
|
|
|
if (SetHandleInformation(out_read, HANDLE_FLAG_INHERIT, 0)){
|
|
|
|
STARTUPINFO startup = {};
|
|
|
|
startup.cb = sizeof(STARTUPINFO);
|
|
|
|
startup.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
|
|
|
|
startup.hStdError = out_write;
|
|
|
|
startup.hStdOutput = out_write;
|
|
|
|
startup.wShowWindow = SW_HIDE;
|
2016-06-29 17:16:08 +00:00
|
|
|
|
2016-02-20 21:36:16 +00:00
|
|
|
PROCESS_INFORMATION info = {};
|
2016-06-29 17:16:08 +00:00
|
|
|
|
2016-02-20 21:36:16 +00:00
|
|
|
Assert(sizeof(Plat_Handle) >= sizeof(HANDLE));
|
2017-03-23 22:45:32 +00:00
|
|
|
if (CreateProcess_utf8((u8*)cmd, (u8*)command_line, 0, 0, TRUE, 0, env_variables, (u8*)path, &startup, &info)){
|
2016-02-20 21:36:16 +00:00
|
|
|
success = 1;
|
|
|
|
CloseHandle(info.hThread);
|
|
|
|
*(HANDLE*)&cli_out->proc = info.hProcess;
|
|
|
|
*(HANDLE*)&cli_out->out_read = out_read;
|
|
|
|
*(HANDLE*)&cli_out->out_write = out_write;
|
2016-05-10 17:36:53 +00:00
|
|
|
|
|
|
|
++win32vars.running_cli;
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
CloseHandle(out_read);
|
|
|
|
CloseHandle(out_write);
|
|
|
|
*(HANDLE*)&cli_out->proc = INVALID_HANDLE_VALUE;
|
|
|
|
*(HANDLE*)&cli_out->out_read = INVALID_HANDLE_VALUE;
|
|
|
|
*(HANDLE*)&cli_out->out_write = INVALID_HANDLE_VALUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
// TODO(allen): failed SetHandleInformation
|
2016-05-10 17:36:53 +00:00
|
|
|
CloseHandle(out_read);
|
|
|
|
CloseHandle(out_write);
|
|
|
|
*(HANDLE*)&cli_out->proc = INVALID_HANDLE_VALUE;
|
|
|
|
*(HANDLE*)&cli_out->out_read = INVALID_HANDLE_VALUE;
|
|
|
|
*(HANDLE*)&cli_out->out_write = INVALID_HANDLE_VALUE;
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
// TODO(allen): failed CreatePipe
|
|
|
|
}
|
|
|
|
}
|
2016-08-28 19:25:24 +00:00
|
|
|
|
2016-02-20 21:36:16 +00:00
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct CLI_Loop_Control{
|
|
|
|
u32 remaining_amount;
|
|
|
|
};
|
|
|
|
|
|
|
|
internal
|
|
|
|
Sys_CLI_Begin_Update_Sig(system_cli_begin_update){
|
|
|
|
Assert(sizeof(cli->scratch_space) >= sizeof(CLI_Loop_Control));
|
|
|
|
CLI_Loop_Control *loop = (CLI_Loop_Control*)cli->scratch_space;
|
|
|
|
loop->remaining_amount = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
|
|
|
Sys_CLI_Update_Step_Sig(system_cli_update_step){
|
|
|
|
HANDLE handle = *(HANDLE*)&cli->out_read;
|
|
|
|
CLI_Loop_Control *loop = (CLI_Loop_Control*)cli->scratch_space;
|
|
|
|
b32 has_more = 0;
|
|
|
|
DWORD remaining = loop->remaining_amount;
|
|
|
|
u32 pos = 0;
|
|
|
|
DWORD read_amount = 0;
|
2016-08-28 19:25:24 +00:00
|
|
|
|
2016-02-20 21:36:16 +00:00
|
|
|
for (;;){
|
|
|
|
if (remaining == 0){
|
|
|
|
if (!PeekNamedPipe(handle, 0, 0, 0, &remaining, 0)) break;
|
|
|
|
if (remaining == 0) break;
|
|
|
|
}
|
2016-08-28 19:25:24 +00:00
|
|
|
|
2016-02-20 21:36:16 +00:00
|
|
|
if (remaining + pos < max){
|
|
|
|
has_more = 1;
|
|
|
|
ReadFile(handle, dest + pos, remaining, &read_amount, 0);
|
|
|
|
TentativeAssert(remaining == read_amount);
|
|
|
|
pos += remaining;
|
|
|
|
remaining = 0;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
has_more = 1;
|
|
|
|
ReadFile(handle, dest + pos, max - pos, &read_amount, 0);
|
|
|
|
TentativeAssert(max - pos == read_amount);
|
|
|
|
loop->remaining_amount = remaining - (max - pos);
|
|
|
|
pos = max;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*amount = pos;
|
2016-08-28 19:25:24 +00:00
|
|
|
|
|
|
|
return(has_more);
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
internal
|
|
|
|
Sys_CLI_End_Update_Sig(system_cli_end_update){
|
|
|
|
b32 close_me = 0;
|
|
|
|
HANDLE proc = *(HANDLE*)&cli->proc;
|
|
|
|
DWORD result = 0;
|
2016-08-28 19:25:24 +00:00
|
|
|
|
2016-02-20 21:36:16 +00:00
|
|
|
if (WaitForSingleObject(proc, 0) == WAIT_OBJECT_0){
|
2017-01-23 06:19:43 +00:00
|
|
|
if (GetExitCodeProcess(proc, &result) == 0){
|
2016-03-21 03:58:34 +00:00
|
|
|
cli->exit = -1;
|
2017-01-23 06:19:43 +00:00
|
|
|
}
|
|
|
|
else{
|
2016-03-21 03:58:34 +00:00
|
|
|
cli->exit = (i32)result;
|
2017-01-23 06:19:43 +00:00
|
|
|
}
|
2016-08-28 19:25:24 +00:00
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
close_me = 1;
|
|
|
|
CloseHandle(*(HANDLE*)&cli->proc);
|
|
|
|
CloseHandle(*(HANDLE*)&cli->out_read);
|
|
|
|
CloseHandle(*(HANDLE*)&cli->out_write);
|
2016-05-10 17:36:53 +00:00
|
|
|
|
|
|
|
--win32vars.running_cli;
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
2016-08-28 19:25:24 +00:00
|
|
|
return(close_me);
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
|
2016-05-29 03:12:12 +00:00
|
|
|
//
|
|
|
|
// Linkage to Custom and Application
|
|
|
|
//
|
|
|
|
|
2017-03-23 23:40:16 +00:00
|
|
|
#include "4ed_font_data.h"
|
|
|
|
#include "4ed_system_shared.cpp"
|
|
|
|
|
2016-05-29 03:12:12 +00:00
|
|
|
//
|
|
|
|
// Helpers
|
|
|
|
//
|
2016-05-27 02:48:20 +00:00
|
|
|
|
2017-02-12 06:01:01 +00:00
|
|
|
global Key_Code keycode_lookup_table[255];
|
2016-05-14 20:08:11 +00:00
|
|
|
|
|
|
|
internal void
|
2016-05-29 03:12:12 +00:00
|
|
|
Win32KeycodeInit(){
|
2016-05-14 20:08:11 +00:00
|
|
|
keycode_lookup_table[VK_BACK] = key_back;
|
|
|
|
keycode_lookup_table[VK_DELETE] = key_del;
|
|
|
|
keycode_lookup_table[VK_UP] = key_up;
|
|
|
|
keycode_lookup_table[VK_DOWN] = key_down;
|
|
|
|
keycode_lookup_table[VK_LEFT] = key_left;
|
|
|
|
keycode_lookup_table[VK_RIGHT] = key_right;
|
|
|
|
keycode_lookup_table[VK_INSERT] = key_insert;
|
|
|
|
keycode_lookup_table[VK_HOME] = key_home;
|
|
|
|
keycode_lookup_table[VK_END] = key_end;
|
|
|
|
keycode_lookup_table[VK_PRIOR] = key_page_up;
|
|
|
|
keycode_lookup_table[VK_NEXT] = key_page_down;
|
|
|
|
keycode_lookup_table[VK_ESCAPE] = key_esc;
|
2016-05-14 20:11:52 +00:00
|
|
|
|
|
|
|
keycode_lookup_table[VK_F1] = key_f1;
|
|
|
|
keycode_lookup_table[VK_F2] = key_f2;
|
|
|
|
keycode_lookup_table[VK_F3] = key_f3;
|
|
|
|
keycode_lookup_table[VK_F4] = key_f4;
|
|
|
|
keycode_lookup_table[VK_F5] = key_f5;
|
|
|
|
keycode_lookup_table[VK_F6] = key_f6;
|
|
|
|
keycode_lookup_table[VK_F7] = key_f7;
|
|
|
|
keycode_lookup_table[VK_F8] = key_f8;
|
|
|
|
keycode_lookup_table[VK_F9] = key_f9;
|
|
|
|
|
|
|
|
keycode_lookup_table[VK_F10] = key_f10;
|
|
|
|
keycode_lookup_table[VK_F11] = key_f11;
|
|
|
|
keycode_lookup_table[VK_F12] = key_f12;
|
|
|
|
keycode_lookup_table[VK_F13] = key_f13;
|
|
|
|
keycode_lookup_table[VK_F14] = key_f14;
|
|
|
|
keycode_lookup_table[VK_F15] = key_f15;
|
|
|
|
keycode_lookup_table[VK_F16] = key_f16;
|
2016-05-14 20:08:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-29 03:12:12 +00:00
|
|
|
internal void
|
|
|
|
Win32RedrawScreen(HDC hdc){
|
2017-07-18 23:17:40 +00:00
|
|
|
launch_rendering(&sysfunc, &target);
|
2016-05-29 03:12:12 +00:00
|
|
|
glFlush();
|
|
|
|
SwapBuffers(hdc);
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void
|
|
|
|
Win32Resize(i32 width, i32 height){
|
|
|
|
if (width > 0 && height > 0){
|
|
|
|
glViewport(0, 0, width, height);
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
glOrtho(0, width, height, 0, -1, 1);
|
|
|
|
glScissor(0, 0, width, height);
|
|
|
|
|
2017-07-18 23:17:40 +00:00
|
|
|
target.width = width;
|
|
|
|
target.height = height;
|
2016-05-29 03:12:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-29 00:03:23 +00:00
|
|
|
internal void*
|
|
|
|
win32_load_gl_always(char *name, HMODULE module){
|
|
|
|
void *p = (void *)wglGetProcAddress(name), *r = 0;
|
|
|
|
if(p == 0 ||
|
|
|
|
(p == (void*)0x1) || (p == (void*)0x2) || (p == (void*)0x3) ||
|
|
|
|
(p == (void*)-1) ){
|
|
|
|
r = (void *)GetProcAddress(module, name);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
r = p;
|
|
|
|
}
|
|
|
|
return(r);
|
|
|
|
}
|
|
|
|
|
2017-02-25 01:53:40 +00:00
|
|
|
internal void CALL_CONVENTION
|
|
|
|
OpenGLDebugCallback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const char *message, const void *userParam){
|
2017-01-29 00:03:23 +00:00
|
|
|
OutputDebugStringA(message);
|
|
|
|
OutputDebugStringA("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
internal void
|
|
|
|
Win32InitGL(){
|
|
|
|
// GL context initialization
|
2017-07-18 23:23:36 +00:00
|
|
|
PIXELFORMATDESCRIPTOR format;
|
|
|
|
format.nSize = sizeof(format);
|
|
|
|
format.nVersion = 1;
|
|
|
|
format.dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_WINDOW;
|
|
|
|
format.iPixelType = PFD_TYPE_RGBA;
|
|
|
|
format.cColorBits = 32;
|
|
|
|
format.cRedBits = 0;
|
|
|
|
format.cRedShift = 0;
|
|
|
|
format.cGreenBits = 0;
|
|
|
|
format.cGreenShift = 0;
|
|
|
|
format.cBlueBits = 0;
|
|
|
|
format.cBlueShift = 0;
|
|
|
|
format.cAlphaBits = 0;
|
|
|
|
format.cAlphaShift = 0;
|
|
|
|
format.cAccumBits = 0;
|
|
|
|
format.cAccumRedBits = 0;
|
|
|
|
format.cAccumGreenBits = 0;
|
|
|
|
format.cAccumBlueBits = 0;
|
|
|
|
format.cAccumAlphaBits = 0;
|
|
|
|
format.cDepthBits = 24;
|
|
|
|
format.cStencilBits = 8;
|
|
|
|
format.cAuxBuffers = 0;
|
|
|
|
format.iLayerType = PFD_MAIN_PLANE;
|
|
|
|
format.bReserved = 0;
|
|
|
|
format.dwLayerMask = 0;
|
|
|
|
format.dwVisibleMask = 0;
|
|
|
|
format.dwDamageMask = 0;
|
|
|
|
|
|
|
|
HDC dc = GetDC(win32vars.window_handle);
|
|
|
|
Assert(dc);
|
|
|
|
int format_id = ChoosePixelFormat(dc, &format);
|
|
|
|
Assert(format_id != 0);
|
|
|
|
BOOL success = SetPixelFormat(dc, format_id, &format);
|
|
|
|
Assert(success == TRUE); AllowLocal(success);
|
|
|
|
|
|
|
|
HGLRC glcontext = wglCreateContext(dc);
|
|
|
|
wglMakeCurrent(dc, glcontext);
|
|
|
|
|
|
|
|
HMODULE module = LoadLibraryA("opengl32.dll");
|
|
|
|
AllowLocal(module);
|
|
|
|
|
|
|
|
wglCreateContextAttribsARB_Function *wglCreateContextAttribsARB = 0;
|
|
|
|
wglCreateContextAttribsARB = (wglCreateContextAttribsARB_Function*)
|
|
|
|
win32_load_gl_always("wglCreateContextAttribsARB", module);
|
|
|
|
|
|
|
|
wglChoosePixelFormatARB_Function *wglChoosePixelFormatARB = 0;
|
|
|
|
wglChoosePixelFormatARB = (wglChoosePixelFormatARB_Function*)
|
|
|
|
win32_load_gl_always("wglChoosePixelFormatARB", module);
|
|
|
|
|
|
|
|
if (wglCreateContextAttribsARB != 0 && wglChoosePixelFormatARB != 0){
|
|
|
|
const int choosePixel_attribList[] =
|
|
|
|
{
|
|
|
|
WGL_DRAW_TO_WINDOW_ARB, TRUE,
|
|
|
|
WGL_SUPPORT_OPENGL_ARB, TRUE,
|
|
|
|
WGL_DOUBLE_BUFFER_ARB, GL_TRUE,
|
|
|
|
WGL_PIXEL_TYPE_ARB, WGL_TYPE_RGBA_ARB,
|
|
|
|
WGL_COLOR_BITS_ARB, 32,
|
|
|
|
WGL_DEPTH_BITS_ARB, 24,
|
|
|
|
WGL_STENCIL_BITS_ARB, 8,
|
|
|
|
0,
|
|
|
|
};
|
2017-02-25 01:53:40 +00:00
|
|
|
|
2017-07-18 23:23:36 +00:00
|
|
|
i32 extended_format_id = 0;
|
|
|
|
u32 num_formats = 0;
|
|
|
|
BOOL result = wglChoosePixelFormatARB(dc, choosePixel_attribList, 0, 1, &extended_format_id, &num_formats);
|
2017-02-25 01:53:40 +00:00
|
|
|
|
2017-07-18 23:23:36 +00:00
|
|
|
if (result != 0 && num_formats > 0){
|
|
|
|
const int createContext_attribList[] = {
|
|
|
|
WGL_CONTEXT_MAJOR_VERSION_ARB, 3,
|
|
|
|
WGL_CONTEXT_MINOR_VERSION_ARB, 2,
|
|
|
|
WGL_CONTEXT_PROFILE_MASK_ARB, WGL_CONTEXT_COMPATIBILITY_PROFILE_BIT_ARB,
|
|
|
|
0
|
2017-02-25 01:53:40 +00:00
|
|
|
};
|
2017-01-29 00:03:23 +00:00
|
|
|
|
2017-07-18 23:23:36 +00:00
|
|
|
if (extended_format_id == format_id){
|
|
|
|
HGLRC extended_context = wglCreateContextAttribsARB(dc, 0, createContext_attribList);
|
|
|
|
if (extended_context){
|
|
|
|
wglMakeCurrent(dc, extended_context);
|
|
|
|
wglDeleteContext(glcontext);
|
|
|
|
glcontext = extended_context;
|
2017-01-29 00:03:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-07-18 23:23:36 +00:00
|
|
|
}
|
|
|
|
|
2017-10-31 16:52:39 +00:00
|
|
|
ReleaseDC(win32vars.window_handle, dc);
|
|
|
|
|
2017-02-25 01:53:40 +00:00
|
|
|
#if (defined(BUILD_X64) && 1) || (defined(BUILD_X86) && 0)
|
2017-07-18 20:51:44 +00:00
|
|
|
#if defined(FRED_INTERNAL)
|
2017-07-18 23:23:36 +00:00
|
|
|
// NOTE(casey): This slows down GL but puts error messages to
|
|
|
|
// the debug console immediately whenever you do something wrong
|
|
|
|
glDebugMessageCallback_type *glDebugMessageCallback =
|
|
|
|
(glDebugMessageCallback_type *)win32_load_gl_always("glDebugMessageCallback", module);
|
|
|
|
glDebugMessageControl_type *glDebugMessageControl =
|
|
|
|
(glDebugMessageControl_type *)win32_load_gl_always("glDebugMessageControl", module);
|
|
|
|
if(glDebugMessageCallback != 0 && glDebugMessageControl != 0)
|
|
|
|
{
|
|
|
|
glDebugMessageCallback(OpenGLDebugCallback, 0);
|
|
|
|
glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, 0, GL_TRUE);
|
|
|
|
glEnable(GL_DEBUG_OUTPUT);
|
|
|
|
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
|
|
|
|
}
|
2017-02-25 01:53:40 +00:00
|
|
|
#endif
|
2017-02-03 00:52:39 +00:00
|
|
|
#endif
|
2017-07-18 23:23:36 +00:00
|
|
|
|
2017-01-29 00:03:23 +00:00
|
|
|
glEnable(GL_TEXTURE_2D);
|
|
|
|
glEnable(GL_SCISSOR_TEST);
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
}
|
|
|
|
|
2016-05-29 03:12:12 +00:00
|
|
|
internal void
|
|
|
|
Win32SetCursorFromUpdate(Application_Mouse_Cursor cursor){
|
|
|
|
switch (cursor){
|
|
|
|
case APP_MOUSE_CURSOR_ARROW:
|
2017-10-31 17:29:52 +00:00
|
|
|
{
|
|
|
|
SetCursor(win32vars.cursor_arrow);
|
|
|
|
}break;
|
2016-05-29 03:12:12 +00:00
|
|
|
|
|
|
|
case APP_MOUSE_CURSOR_IBEAM:
|
2017-10-31 17:29:52 +00:00
|
|
|
{
|
|
|
|
SetCursor(win32vars.cursor_ibeam);
|
|
|
|
}break;
|
2016-05-29 03:12:12 +00:00
|
|
|
|
|
|
|
case APP_MOUSE_CURSOR_LEFTRIGHT:
|
2017-10-31 17:29:52 +00:00
|
|
|
{
|
|
|
|
SetCursor(win32vars.cursor_leftright);
|
|
|
|
}break;
|
2016-05-29 03:12:12 +00:00
|
|
|
|
|
|
|
case APP_MOUSE_CURSOR_UPDOWN:
|
2017-10-31 17:29:52 +00:00
|
|
|
{
|
|
|
|
SetCursor(win32vars.cursor_updown);
|
|
|
|
}break;
|
2016-05-29 03:12:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-29 19:05:27 +00:00
|
|
|
internal u64
|
|
|
|
Win32HighResolutionTime(){
|
|
|
|
u64 result = 0;
|
|
|
|
LARGE_INTEGER t;
|
|
|
|
if (QueryPerformanceCounter(&t)){
|
2017-06-16 20:57:02 +00:00
|
|
|
result = (u64) (t.QuadPart / win32vars.count_per_usecond);
|
2016-05-29 19:05:27 +00:00
|
|
|
}
|
|
|
|
return(result);
|
|
|
|
}
|
|
|
|
|
2016-02-20 21:36:16 +00:00
|
|
|
internal LRESULT
|
2016-05-29 03:12:12 +00:00
|
|
|
Win32Callback(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
|
2016-06-13 11:50:28 +00:00
|
|
|
LRESULT result = 0;
|
2016-09-01 00:26:52 +00:00
|
|
|
|
2016-02-20 21:36:16 +00:00
|
|
|
switch (uMsg){
|
2017-01-29 00:03:23 +00:00
|
|
|
case WM_MENUCHAR:break;
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
case WM_SYSKEYDOWN:
|
|
|
|
case WM_SYSKEYUP:
|
|
|
|
case WM_KEYDOWN:
|
|
|
|
case WM_KEYUP:
|
2016-02-20 21:36:16 +00:00
|
|
|
{
|
2016-03-21 03:58:34 +00:00
|
|
|
switch (wParam){
|
|
|
|
case VK_CONTROL:case VK_LCONTROL:case VK_RCONTROL:
|
|
|
|
case VK_MENU:case VK_LMENU:case VK_RMENU:
|
|
|
|
case VK_SHIFT:case VK_LSHIFT:case VK_RSHIFT:
|
2016-02-20 21:36:16 +00:00
|
|
|
{
|
2017-01-14 03:01:35 +00:00
|
|
|
Control_Keys *controls = &win32vars.input_chunk.pers.controls;
|
|
|
|
b8 *control_keys = win32vars.input_chunk.pers.control_keys;
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
b8 down = ((lParam & Bit_31)?(0):(1));
|
|
|
|
b8 is_right = ((lParam & Bit_24)?(1):(0));
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
if (wParam != 255){
|
|
|
|
switch (wParam){
|
|
|
|
case VK_SHIFT:
|
|
|
|
{
|
|
|
|
control_keys[MDFR_SHIFT_INDEX] = down;
|
|
|
|
}break;
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
case VK_CONTROL:
|
|
|
|
{
|
|
|
|
if (is_right) controls->r_ctrl = down;
|
|
|
|
else controls->l_ctrl = down;
|
|
|
|
}break;
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
case VK_MENU:
|
|
|
|
{
|
|
|
|
if (is_right) controls->r_alt = down;
|
|
|
|
else controls->l_alt = down;
|
|
|
|
}break;
|
2016-03-15 14:12:06 +00:00
|
|
|
}
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2017-01-29 00:03:23 +00:00
|
|
|
b8 ctrl = (controls->r_ctrl || (controls->l_ctrl && !controls->r_alt));
|
|
|
|
b8 alt = (controls->l_alt || (controls->r_alt && !controls->l_ctrl));
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
if (win32vars.lctrl_lalt_is_altgr){
|
|
|
|
if (controls->l_alt && controls->l_ctrl){
|
|
|
|
ctrl = 0;
|
|
|
|
alt = 0;
|
|
|
|
}
|
2016-03-15 14:12:06 +00:00
|
|
|
}
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
control_keys[MDFR_CONTROL_INDEX] = ctrl;
|
|
|
|
control_keys[MDFR_ALT_INDEX] = alt;
|
|
|
|
}
|
|
|
|
}break;
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
default:
|
2016-07-08 15:24:07 +00:00
|
|
|
{
|
|
|
|
b8 current_state = ((lParam & Bit_31)?(0):(1));
|
2016-05-05 23:32:46 +00:00
|
|
|
|
2016-07-08 15:24:07 +00:00
|
|
|
if (current_state){
|
2017-02-12 06:01:01 +00:00
|
|
|
Key_Code key = keycode_lookup_table[(u8)wParam];
|
2016-07-08 15:24:07 +00:00
|
|
|
|
2017-01-29 00:03:23 +00:00
|
|
|
if (key != 0){
|
|
|
|
i32 *count = &win32vars.input_chunk.trans.key_data.count;
|
|
|
|
Key_Event_Data *data = win32vars.input_chunk.trans.key_data.keys;
|
|
|
|
b8 *control_keys = win32vars.input_chunk.pers.control_keys;
|
|
|
|
i32 control_keys_size = sizeof(win32vars.input_chunk.pers.control_keys);
|
|
|
|
|
|
|
|
Assert(*count < KEY_INPUT_BUFFER_SIZE);
|
|
|
|
data[*count].character = 0;
|
|
|
|
data[*count].character_no_caps_lock = 0;
|
|
|
|
data[*count].keycode = key;
|
2016-07-08 15:24:07 +00:00
|
|
|
memcpy(data[*count].modifiers, control_keys, control_keys_size);
|
|
|
|
++(*count);
|
2017-01-29 00:03:23 +00:00
|
|
|
|
2017-06-20 20:37:45 +00:00
|
|
|
win32vars.got_useful_event = true;
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
}
|
2016-07-08 15:24:07 +00:00
|
|
|
}break;
|
|
|
|
}/* switch */
|
2016-03-21 03:58:34 +00:00
|
|
|
}break;
|
2016-05-05 23:32:46 +00:00
|
|
|
|
2017-01-29 00:03:23 +00:00
|
|
|
case WM_CHAR: case WM_SYSCHAR: case WM_UNICHAR:
|
|
|
|
{
|
2017-02-12 06:01:01 +00:00
|
|
|
u16 character = (u16)wParam;
|
2017-01-29 00:03:23 +00:00
|
|
|
|
|
|
|
if (character == '\r'){
|
|
|
|
character = '\n';
|
|
|
|
}
|
2017-02-12 06:01:01 +00:00
|
|
|
else if (character == '\t'){
|
|
|
|
character = '\t';
|
|
|
|
}
|
|
|
|
else if (character < 32 || character == 127){
|
2017-01-29 00:03:23 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-02-12 06:01:01 +00:00
|
|
|
u16 character_no_caps_lock = character;
|
2017-01-29 00:03:23 +00:00
|
|
|
|
|
|
|
i32 *count = &win32vars.input_chunk.trans.key_data.count;
|
|
|
|
Key_Event_Data *data = win32vars.input_chunk.trans.key_data.keys;
|
|
|
|
b8 *control_keys = win32vars.input_chunk.pers.control_keys;
|
|
|
|
i32 control_keys_size = sizeof(win32vars.input_chunk.pers.control_keys);
|
|
|
|
|
|
|
|
BYTE state[256];
|
|
|
|
GetKeyboardState(state);
|
|
|
|
if (state[VK_CAPITAL]){
|
|
|
|
if (character_no_caps_lock >= 'a' && character_no_caps_lock <= 'z'){
|
2017-02-12 06:01:01 +00:00
|
|
|
character_no_caps_lock -= (u8)('a' - 'A');
|
2017-01-29 00:03:23 +00:00
|
|
|
}
|
|
|
|
else if (character_no_caps_lock >= 'A' && character_no_caps_lock <= 'Z'){
|
|
|
|
character_no_caps_lock += (u8)('a' - 'A');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Assert(*count < KEY_INPUT_BUFFER_SIZE);
|
|
|
|
data[*count].character = character;
|
|
|
|
data[*count].character_no_caps_lock = character_no_caps_lock;
|
|
|
|
data[*count].keycode = character_no_caps_lock;
|
|
|
|
memcpy(data[*count].modifiers, control_keys, control_keys_size);
|
|
|
|
++(*count);
|
|
|
|
|
2017-06-20 20:37:45 +00:00
|
|
|
win32vars.got_useful_event = true;
|
2017-01-29 00:03:23 +00:00
|
|
|
}break;
|
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
case WM_MOUSEMOVE:
|
|
|
|
{
|
2016-05-04 23:21:30 +00:00
|
|
|
i32 new_x = LOWORD(lParam);
|
|
|
|
i32 new_y = HIWORD(lParam);
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2017-01-07 02:59:55 +00:00
|
|
|
if (new_x != win32vars.input_chunk.pers.mouse_x || new_y != win32vars.input_chunk.pers.mouse_y){
|
2016-05-04 23:21:30 +00:00
|
|
|
win32vars.input_chunk.pers.mouse_x = new_x;
|
|
|
|
win32vars.input_chunk.pers.mouse_y = new_y;
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2017-06-20 20:37:45 +00:00
|
|
|
win32vars.got_useful_event = true;
|
2016-05-04 23:21:30 +00:00
|
|
|
}
|
2016-03-21 03:58:34 +00:00
|
|
|
}break;
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
case WM_MOUSEWHEEL:
|
|
|
|
{
|
2017-06-20 20:37:45 +00:00
|
|
|
win32vars.got_useful_event = true;
|
2017-03-25 03:42:16 +00:00
|
|
|
i32 rotation = GET_WHEEL_DELTA_WPARAM(wParam);
|
2016-03-21 03:58:34 +00:00
|
|
|
if (rotation > 0){
|
|
|
|
win32vars.input_chunk.trans.mouse_wheel = 1;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
win32vars.input_chunk.trans.mouse_wheel = -1;
|
|
|
|
}
|
|
|
|
}break;
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
case WM_LBUTTONDOWN:
|
|
|
|
{
|
2017-06-20 20:37:45 +00:00
|
|
|
win32vars.got_useful_event = true;
|
2016-03-21 03:58:34 +00:00
|
|
|
win32vars.input_chunk.trans.mouse_l_press = 1;
|
|
|
|
win32vars.input_chunk.pers.mouse_l = 1;
|
|
|
|
}break;
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
case WM_RBUTTONDOWN:
|
|
|
|
{
|
2017-06-20 20:37:45 +00:00
|
|
|
win32vars.got_useful_event = true;
|
2016-03-21 03:58:34 +00:00
|
|
|
win32vars.input_chunk.trans.mouse_r_press = 1;
|
|
|
|
win32vars.input_chunk.pers.mouse_r = 1;
|
|
|
|
}break;
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
case WM_LBUTTONUP:
|
|
|
|
{
|
2017-06-20 20:37:45 +00:00
|
|
|
win32vars.got_useful_event = true;
|
2016-03-21 03:58:34 +00:00
|
|
|
win32vars.input_chunk.trans.mouse_l_release = 1;
|
|
|
|
win32vars.input_chunk.pers.mouse_l = 0;
|
|
|
|
}break;
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
case WM_RBUTTONUP:
|
|
|
|
{
|
2017-06-20 20:37:45 +00:00
|
|
|
win32vars.got_useful_event = true;
|
2016-03-21 03:58:34 +00:00
|
|
|
win32vars.input_chunk.trans.mouse_r_release = 1;
|
|
|
|
win32vars.input_chunk.pers.mouse_r = 0;
|
|
|
|
}break;
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
case WM_KILLFOCUS:
|
|
|
|
case WM_SETFOCUS:
|
|
|
|
{
|
2017-06-20 20:37:45 +00:00
|
|
|
win32vars.got_useful_event = true;
|
2016-03-21 03:58:34 +00:00
|
|
|
win32vars.input_chunk.pers.mouse_l = 0;
|
|
|
|
win32vars.input_chunk.pers.mouse_r = 0;
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2017-01-14 03:01:35 +00:00
|
|
|
for (i32 i = 0; i < MDFR_INDEX_COUNT; ++i){
|
2016-09-08 22:03:43 +00:00
|
|
|
win32vars.input_chunk.pers.control_keys[i] = 0;
|
|
|
|
}
|
2016-09-08 00:32:31 +00:00
|
|
|
win32vars.input_chunk.pers.controls = null_control_keys;
|
2016-03-21 03:58:34 +00:00
|
|
|
}break;
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
case WM_SIZE:
|
|
|
|
{
|
2017-06-20 20:37:45 +00:00
|
|
|
win32vars.got_useful_event = true;
|
2017-01-29 00:03:23 +00:00
|
|
|
i32 new_width = LOWORD(lParam);
|
|
|
|
i32 new_height = HIWORD(lParam);
|
|
|
|
|
|
|
|
Win32Resize(new_width, new_height);
|
2016-03-21 03:58:34 +00:00
|
|
|
}break;
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2017-03-27 22:36:42 +00:00
|
|
|
case WM_DISPLAYCHANGE:
|
|
|
|
{
|
2017-06-20 20:37:45 +00:00
|
|
|
win32vars.got_useful_event = true;
|
2017-03-27 22:36:42 +00:00
|
|
|
|
|
|
|
LONG_PTR style = GetWindowLongPtr(hwnd, GWL_STYLE);
|
|
|
|
if (!(style & WS_OVERLAPPEDWINDOW)){
|
|
|
|
MONITORINFO monitor_info = {sizeof(MONITORINFO)};
|
|
|
|
if(GetMonitorInfo(MonitorFromWindow(hwnd, MONITOR_DEFAULTTOPRIMARY), &monitor_info))
|
|
|
|
{
|
|
|
|
SetWindowPos(hwnd, HWND_TOP,
|
|
|
|
monitor_info.rcMonitor.left, monitor_info.rcMonitor.top,
|
|
|
|
monitor_info.rcMonitor.right - monitor_info.rcMonitor.left,
|
|
|
|
monitor_info.rcMonitor.bottom - monitor_info.rcMonitor.top,
|
|
|
|
SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}break;
|
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
case WM_PAINT:
|
|
|
|
{
|
2017-06-20 20:37:45 +00:00
|
|
|
win32vars.got_useful_event = true;
|
2016-03-21 03:58:34 +00:00
|
|
|
PAINTSTRUCT ps;
|
|
|
|
HDC hdc = BeginPaint(hwnd, &ps);
|
|
|
|
Win32RedrawScreen(hdc);
|
|
|
|
EndPaint(hwnd, &ps);
|
|
|
|
}break;
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2016-05-27 17:11:38 +00:00
|
|
|
case WM_CLOSE:
|
2016-03-21 03:58:34 +00:00
|
|
|
case WM_DESTROY:
|
|
|
|
{
|
2017-06-20 20:37:45 +00:00
|
|
|
win32vars.got_useful_event = true;
|
2017-11-07 21:35:26 +00:00
|
|
|
win32vars.input_chunk.trans.trying_to_kill = true;
|
2016-03-21 03:58:34 +00:00
|
|
|
}break;
|
2016-06-07 17:22:42 +00:00
|
|
|
|
2016-05-04 23:21:30 +00:00
|
|
|
case WM_4coder_ANIMATE:
|
2017-02-03 00:52:39 +00:00
|
|
|
{
|
2017-06-20 20:37:45 +00:00
|
|
|
win32vars.got_useful_event = true;
|
2017-02-03 00:52:39 +00:00
|
|
|
}break;
|
2016-05-04 23:21:30 +00:00
|
|
|
|
2016-09-01 00:26:52 +00:00
|
|
|
case WM_CANCELMODE:
|
|
|
|
{
|
|
|
|
result = DefWindowProc(hwnd, uMsg, wParam, lParam);
|
|
|
|
}break;
|
|
|
|
|
2016-03-21 03:58:34 +00:00
|
|
|
default:
|
|
|
|
{
|
|
|
|
result = DefWindowProc(hwnd, uMsg, wParam, lParam);
|
|
|
|
}break;
|
|
|
|
}
|
2016-09-01 00:26:52 +00:00
|
|
|
|
2016-05-26 02:42:02 +00:00
|
|
|
return(result);
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 17:19:59 +00:00
|
|
|
#include "4ed_link_system_functions.cpp"
|
2017-07-18 23:17:40 +00:00
|
|
|
#include "4ed_shared_init_logic.cpp"
|
2017-07-18 17:19:59 +00:00
|
|
|
|
2017-02-25 01:53:40 +00:00
|
|
|
int CALL_CONVENTION
|
2017-02-03 00:52:39 +00:00
|
|
|
WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
|
2016-08-29 01:03:26 +00:00
|
|
|
i32 argc = __argc;
|
2016-05-10 17:36:53 +00:00
|
|
|
char **argv = __argv;
|
|
|
|
|
2017-07-19 20:23:39 +00:00
|
|
|
//
|
|
|
|
// System Linkage
|
|
|
|
//
|
|
|
|
|
|
|
|
link_system_code();
|
|
|
|
|
|
|
|
//
|
|
|
|
// Memory init
|
|
|
|
//
|
|
|
|
|
2017-07-19 20:07:50 +00:00
|
|
|
memset(&win32vars, 0, sizeof(win32vars));
|
2017-07-19 16:33:12 +00:00
|
|
|
memset(&target, 0, sizeof(target));
|
|
|
|
memset(&memory_vars, 0, sizeof(memory_vars));
|
|
|
|
memset(&plat_settings, 0, sizeof(plat_settings));
|
2016-05-29 17:07:24 +00:00
|
|
|
|
2017-07-19 16:33:12 +00:00
|
|
|
memset(&libraries, 0, sizeof(libraries));
|
|
|
|
memset(&app, 0, sizeof(app));
|
|
|
|
memset(&custom_api, 0, sizeof(custom_api));
|
2016-09-01 20:29:07 +00:00
|
|
|
|
2017-07-19 20:23:39 +00:00
|
|
|
memory_init();
|
|
|
|
|
2017-10-31 17:29:52 +00:00
|
|
|
win32vars.cursor_show = MouseCursorShow_Always;
|
|
|
|
win32vars.prev_cursor_show = MouseCursorShow_Always;
|
|
|
|
|
2017-07-19 20:23:39 +00:00
|
|
|
//
|
|
|
|
// HACK(allen):
|
|
|
|
// Previously zipped stuff is here, it should be zipped in the new pattern now.
|
|
|
|
//
|
|
|
|
|
2017-07-19 16:33:12 +00:00
|
|
|
init_shared_vars();
|
2017-07-18 19:52:41 +00:00
|
|
|
|
2016-05-29 17:07:24 +00:00
|
|
|
//
|
2017-07-19 20:23:39 +00:00
|
|
|
// Dynamic Linkage
|
2016-05-29 17:07:24 +00:00
|
|
|
//
|
|
|
|
|
2017-07-19 16:33:12 +00:00
|
|
|
load_app_code();
|
|
|
|
link_rendering();
|
|
|
|
#if defined(FRED_SUPER)
|
|
|
|
load_custom_code();
|
|
|
|
#else
|
|
|
|
custom_api.get_bindings = get_bindings;
|
|
|
|
#endif
|
2016-05-29 17:07:24 +00:00
|
|
|
|
|
|
|
//
|
2017-07-19 20:07:50 +00:00
|
|
|
// Read Command Line
|
2016-05-29 17:07:24 +00:00
|
|
|
//
|
|
|
|
|
2017-07-19 20:07:50 +00:00
|
|
|
read_command_line(argc, argv);
|
2016-05-29 17:07:24 +00:00
|
|
|
|
2016-08-22 23:54:20 +00:00
|
|
|
//
|
2017-07-19 20:07:50 +00:00
|
|
|
// Threads
|
2016-08-22 23:54:20 +00:00
|
|
|
//
|
|
|
|
|
2017-07-19 20:07:50 +00:00
|
|
|
work_system_init();
|
2016-08-22 23:54:20 +00:00
|
|
|
|
2016-05-29 17:07:24 +00:00
|
|
|
//
|
2017-07-19 20:07:50 +00:00
|
|
|
// Coroutines
|
2016-05-29 17:07:24 +00:00
|
|
|
//
|
|
|
|
|
2017-07-19 20:10:05 +00:00
|
|
|
coroutines_init();
|
2016-05-29 17:07:24 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Window and GL Initialization
|
|
|
|
//
|
2016-05-10 17:36:53 +00:00
|
|
|
|
2017-06-16 20:57:02 +00:00
|
|
|
WNDCLASS window_class = {0};
|
2016-05-29 17:07:24 +00:00
|
|
|
window_class.style = CS_HREDRAW|CS_VREDRAW;
|
2017-02-25 01:53:40 +00:00
|
|
|
window_class.lpfnWndProc = (WNDPROC)(Win32Callback);
|
2016-05-03 16:15:08 +00:00
|
|
|
window_class.hInstance = hInstance;
|
2017-03-23 22:45:32 +00:00
|
|
|
window_class.lpszClassName = L"4coder-win32-wndclass";
|
|
|
|
window_class.hIcon = LoadIcon(hInstance, L"main");
|
2016-05-27 17:11:38 +00:00
|
|
|
|
2016-05-03 16:15:08 +00:00
|
|
|
if (!RegisterClass(&window_class)){
|
2016-05-29 17:07:24 +00:00
|
|
|
exit(1);
|
2016-05-03 16:15:08 +00:00
|
|
|
}
|
2016-05-27 17:11:38 +00:00
|
|
|
|
2017-06-16 20:57:02 +00:00
|
|
|
RECT window_rect = {0};
|
2017-07-18 21:19:28 +00:00
|
|
|
if (plat_settings.set_window_size){
|
|
|
|
window_rect.right = plat_settings.window_w;
|
|
|
|
window_rect.bottom = plat_settings.window_h;
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
window_rect.right = 800;
|
|
|
|
window_rect.bottom = 600;
|
|
|
|
}
|
2016-05-29 17:07:24 +00:00
|
|
|
|
2016-02-20 21:36:16 +00:00
|
|
|
if (!AdjustWindowRect(&window_rect, WS_OVERLAPPEDWINDOW, false)){
|
2017-06-30 21:47:24 +00:00
|
|
|
LOG("Could not get adjusted window.\n");
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
2016-05-29 17:07:24 +00:00
|
|
|
|
2016-09-01 00:26:52 +00:00
|
|
|
i32 window_x = CW_USEDEFAULT;
|
|
|
|
i32 window_y = CW_USEDEFAULT;
|
2016-05-29 17:07:24 +00:00
|
|
|
|
2017-07-18 21:19:28 +00:00
|
|
|
if (plat_settings.set_window_pos){
|
|
|
|
window_x = plat_settings.window_x;
|
|
|
|
window_y = plat_settings.window_y;
|
2017-06-30 21:47:24 +00:00
|
|
|
LOGF("Setting window position (%d, %d)\n", window_x, window_y);
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
2016-05-29 17:07:24 +00:00
|
|
|
|
2017-07-18 22:34:57 +00:00
|
|
|
LOG("Creating window... ");
|
2017-11-09 00:56:26 +00:00
|
|
|
i32 window_style = WS_OVERLAPPEDWINDOW;
|
2017-07-18 21:19:28 +00:00
|
|
|
if (!plat_settings.fullscreen_window && plat_settings.maximize_window){
|
2016-02-20 21:36:16 +00:00
|
|
|
window_style |= WS_MAXIMIZE;
|
|
|
|
}
|
2017-07-18 22:34:57 +00:00
|
|
|
win32vars.window_handle = CreateWindowEx(0, window_class.lpszClassName, WINDOW_NAME, window_style, window_x, window_y, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top, 0, 0, hInstance, 0);
|
2016-05-29 17:07:24 +00:00
|
|
|
|
|
|
|
if (win32vars.window_handle == 0){
|
2017-06-30 21:47:24 +00:00
|
|
|
LOG("Failed\n");
|
2016-05-29 17:07:24 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2017-06-16 20:57:02 +00:00
|
|
|
else{
|
2017-06-30 21:47:24 +00:00
|
|
|
LOG("Success\n");
|
2017-06-16 20:57:02 +00:00
|
|
|
}
|
2016-05-29 17:07:24 +00:00
|
|
|
|
2017-03-17 18:38:09 +00:00
|
|
|
{
|
|
|
|
HDC hdc = GetDC(win32vars.window_handle);
|
|
|
|
|
|
|
|
// TODO(allen): not Windows XP compatible, how do I handle that?
|
|
|
|
SetProcessDPIAware();
|
|
|
|
win32vars.dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
|
|
|
|
win32vars.dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
|
|
|
|
|
|
|
|
GetClientRect(win32vars.window_handle, &window_rect);
|
|
|
|
ReleaseDC(win32vars.window_handle, hdc);
|
|
|
|
}
|
2016-05-27 02:07:58 +00:00
|
|
|
|
2017-01-29 00:03:23 +00:00
|
|
|
Win32InitGL();
|
2016-02-20 21:36:16 +00:00
|
|
|
Win32Resize(window_rect.right - window_rect.left, window_rect.bottom - window_rect.top);
|
2016-05-29 17:07:24 +00:00
|
|
|
|
2017-03-13 23:48:11 +00:00
|
|
|
//
|
|
|
|
// Font System Init
|
|
|
|
//
|
|
|
|
|
2017-06-30 21:47:24 +00:00
|
|
|
LOG("Initializing fonts\n");
|
2017-07-18 21:19:28 +00:00
|
|
|
system_font_init(&sysfunc.font, 0, 0, plat_settings.font_size, plat_settings.use_hinting);
|
2016-05-29 17:07:24 +00:00
|
|
|
|
|
|
|
//
|
|
|
|
// Misc System Initializations
|
|
|
|
//
|
|
|
|
|
2017-06-30 21:47:24 +00:00
|
|
|
LOG("Initializing clipboard\n");
|
2017-03-23 23:40:16 +00:00
|
|
|
win32vars.clip_max = KB(16);
|
|
|
|
win32vars.clip_buffer = (u8*)system_memory_allocate(win32vars.clip_max);
|
|
|
|
|
2016-02-20 21:36:16 +00:00
|
|
|
win32vars.clipboard_sequence = GetClipboardSequenceNumber();
|
|
|
|
if (win32vars.clipboard_sequence == 0){
|
|
|
|
system_post_clipboard(make_lit_string(""));
|
2016-05-29 17:07:24 +00:00
|
|
|
|
2016-02-20 21:36:16 +00:00
|
|
|
win32vars.clipboard_sequence = GetClipboardSequenceNumber();
|
|
|
|
win32vars.next_clipboard_is_self = 0;
|
2016-05-29 17:07:24 +00:00
|
|
|
|
2016-02-20 21:36:16 +00:00
|
|
|
if (win32vars.clipboard_sequence == 0){
|
2016-05-29 19:05:27 +00:00
|
|
|
OutputDebugStringA("Failure while initializing clipboard\n");
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
2016-05-03 16:15:08 +00:00
|
|
|
}
|
2016-02-20 21:36:16 +00:00
|
|
|
else{
|
2017-03-23 23:40:16 +00:00
|
|
|
win32_read_clipboard_contents();
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
2016-05-29 17:07:24 +00:00
|
|
|
|
|
|
|
Win32KeycodeInit();
|
|
|
|
|
|
|
|
win32vars.cursor_ibeam = LoadCursor(NULL, IDC_IBEAM);
|
|
|
|
win32vars.cursor_arrow = LoadCursor(NULL, IDC_ARROW);
|
|
|
|
win32vars.cursor_leftright = LoadCursor(NULL, IDC_SIZEWE);
|
|
|
|
win32vars.cursor_updown = LoadCursor(NULL, IDC_SIZENS);
|
|
|
|
|
2016-05-29 19:05:27 +00:00
|
|
|
LARGE_INTEGER f;
|
|
|
|
if (QueryPerformanceFrequency(&f)){
|
2017-06-16 20:57:02 +00:00
|
|
|
win32vars.count_per_usecond = (f32)f.QuadPart / 1000000.f;
|
2017-06-30 21:47:24 +00:00
|
|
|
LOGF("Got performance frequency %f\n", win32vars.count_per_usecond);
|
2016-05-29 19:05:27 +00:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
// NOTE(allen): Just guess.
|
2017-06-16 20:57:02 +00:00
|
|
|
win32vars.count_per_usecond = 1.f;
|
2017-06-30 21:47:24 +00:00
|
|
|
LOG("Did not get performance frequency, just guessing with 1.\n");
|
2016-05-29 19:05:27 +00:00
|
|
|
}
|
2017-06-16 20:57:02 +00:00
|
|
|
Assert(win32vars.count_per_usecond > 0.f);
|
2016-05-29 19:05:27 +00:00
|
|
|
|
2016-05-29 17:07:24 +00:00
|
|
|
//
|
2017-07-19 20:13:38 +00:00
|
|
|
// App init
|
2016-05-29 17:07:24 +00:00
|
|
|
//
|
|
|
|
|
2017-07-19 20:07:50 +00:00
|
|
|
char cwd[4096];
|
|
|
|
u32 size = sysfunc.get_current_path(cwd, sizeof(cwd));
|
|
|
|
if (size == 0 || size >= sizeof(cwd)){
|
|
|
|
system_error_box("Could not get current directory at launch.");
|
|
|
|
}
|
|
|
|
String curdir = make_string(cwd, size);
|
|
|
|
terminate_with_null(&curdir);
|
|
|
|
replace_char(&curdir, '\\', '/');
|
2016-05-29 17:07:24 +00:00
|
|
|
|
2017-07-19 20:07:50 +00:00
|
|
|
LOG("Initializing application variables\n");
|
|
|
|
app.init(&sysfunc, &target, &memory_vars, win32vars.clipboard_contents, curdir, custom_api);
|
2016-05-29 17:07:24 +00:00
|
|
|
|
2017-07-19 20:13:38 +00:00
|
|
|
//
|
|
|
|
// Main loop
|
|
|
|
//
|
|
|
|
|
2017-07-18 22:09:16 +00:00
|
|
|
b32 keep_running = true;
|
2017-06-16 20:57:02 +00:00
|
|
|
win32vars.first = true;
|
2016-05-03 16:15:08 +00:00
|
|
|
timeBeginPeriod(1);
|
2016-05-10 17:36:53 +00:00
|
|
|
|
2017-07-18 21:19:28 +00:00
|
|
|
if (plat_settings.fullscreen_window){
|
2017-07-18 22:09:16 +00:00
|
|
|
win32_toggle_fullscreen();
|
2016-09-01 03:06:46 +00:00
|
|
|
}
|
2016-05-29 19:05:27 +00:00
|
|
|
|
2016-05-29 17:07:24 +00:00
|
|
|
SetForegroundWindow(win32vars.window_handle);
|
|
|
|
SetActiveWindow(win32vars.window_handle);
|
2016-09-01 00:26:52 +00:00
|
|
|
ShowWindow(win32vars.window_handle, SW_SHOW);
|
2016-05-10 17:36:53 +00:00
|
|
|
|
2017-06-30 21:47:24 +00:00
|
|
|
LOG("Beginning main loop\n");
|
2016-05-29 19:05:27 +00:00
|
|
|
u64 timer_start = Win32HighResolutionTime();
|
2016-05-29 17:07:24 +00:00
|
|
|
system_acquire_lock(FRAME_LOCK);
|
2016-02-20 21:36:16 +00:00
|
|
|
MSG msg;
|
2017-07-18 22:09:16 +00:00
|
|
|
for (;keep_running;){
|
2016-05-10 17:36:53 +00:00
|
|
|
// TODO(allen): Find a good way to wait on a pipe
|
|
|
|
// without interfering with the reading process
|
2016-05-27 17:11:38 +00:00
|
|
|
// Looks like we can ReadFile with a size of zero
|
|
|
|
// in an IOCP for this effect.
|
2017-07-18 22:34:57 +00:00
|
|
|
if (!win32vars.first){
|
2016-09-02 15:58:04 +00:00
|
|
|
system_release_lock(FRAME_LOCK);
|
|
|
|
|
2017-02-03 00:52:39 +00:00
|
|
|
if (win32vars.running_cli == 0){
|
|
|
|
win32vars.got_useful_event = false;
|
|
|
|
}
|
|
|
|
|
2017-01-29 00:03:23 +00:00
|
|
|
b32 get_more_messages = true;
|
|
|
|
do{
|
|
|
|
if (win32vars.got_useful_event == 0){
|
|
|
|
get_more_messages = GetMessage(&msg, 0, 0, 0);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
get_more_messages = PeekMessage(&msg, 0, 0, 0, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (get_more_messages){
|
|
|
|
if (msg.message == WM_QUIT){
|
2017-07-18 22:09:16 +00:00
|
|
|
keep_running = false;
|
2017-01-29 00:03:23 +00:00
|
|
|
}else{
|
|
|
|
b32 treat_normally = true;
|
|
|
|
if (msg.message == WM_KEYDOWN || msg.message == WM_SYSKEYDOWN){
|
|
|
|
switch (msg.wParam){
|
|
|
|
case VK_CONTROL:case VK_LCONTROL:case VK_RCONTROL:
|
|
|
|
case VK_MENU:case VK_LMENU:case VK_RMENU:
|
|
|
|
case VK_SHIFT:case VK_LSHIFT:case VK_RSHIFT:break;
|
|
|
|
|
|
|
|
default: treat_normally = false; break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (treat_normally){
|
2016-09-02 15:58:04 +00:00
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
2017-01-29 00:03:23 +00:00
|
|
|
else{
|
|
|
|
Control_Keys *controls = &win32vars.input_chunk.pers.controls;
|
|
|
|
|
|
|
|
b8 ctrl = (controls->r_ctrl || (controls->l_ctrl && !controls->r_alt));
|
|
|
|
b8 alt = (controls->l_alt || (controls->r_alt && !controls->l_ctrl));
|
|
|
|
|
|
|
|
if (win32vars.lctrl_lalt_is_altgr){
|
|
|
|
if (controls->l_alt && controls->l_ctrl){
|
|
|
|
ctrl = 0;
|
|
|
|
alt = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
BYTE ctrl_state = 0, alt_state = 0;
|
|
|
|
BYTE state[256];
|
|
|
|
if (ctrl || alt){
|
|
|
|
GetKeyboardState(state);
|
|
|
|
if (ctrl){
|
|
|
|
ctrl_state = state[VK_CONTROL];
|
|
|
|
state[VK_CONTROL] = 0;
|
|
|
|
}
|
|
|
|
if (alt){
|
|
|
|
alt_state = state[VK_MENU];
|
|
|
|
state[VK_MENU] = 0;
|
|
|
|
}
|
|
|
|
SetKeyboardState(state);
|
|
|
|
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
|
|
|
|
if (ctrl){
|
|
|
|
state[VK_CONTROL] = ctrl_state;
|
|
|
|
}
|
|
|
|
if (alt){
|
|
|
|
state[VK_MENU] = alt_state;
|
|
|
|
}
|
|
|
|
SetKeyboardState(state);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
TranslateMessage(&msg);
|
|
|
|
DispatchMessage(&msg);
|
|
|
|
}
|
|
|
|
}
|
2016-05-10 17:36:53 +00:00
|
|
|
}
|
|
|
|
}
|
2017-07-18 22:34:57 +00:00
|
|
|
}while (get_more_messages);
|
2016-09-02 15:58:04 +00:00
|
|
|
|
|
|
|
system_acquire_lock(FRAME_LOCK);
|
2016-05-04 23:21:30 +00:00
|
|
|
}
|
|
|
|
|
2016-05-29 19:05:27 +00:00
|
|
|
POINT mouse_point;
|
|
|
|
if (GetCursorPos(&mouse_point) &&
|
|
|
|
ScreenToClient(win32vars.window_handle, &mouse_point)){
|
2016-05-27 17:11:38 +00:00
|
|
|
|
2017-01-23 06:19:43 +00:00
|
|
|
i32_Rect screen;
|
|
|
|
screen.x0 = 0;
|
|
|
|
screen.y0 = 0;
|
2017-07-18 23:17:40 +00:00
|
|
|
screen.x1 = target.width;
|
|
|
|
screen.y1 = target.height;
|
2017-01-23 06:19:43 +00:00
|
|
|
|
|
|
|
i32 mx = mouse_point.x;
|
|
|
|
i32 my = mouse_point.y;
|
|
|
|
|
|
|
|
b32 is_hit = false;
|
|
|
|
if (mx >= screen.x0 && mx < screen.x1 && my >= screen.y0 && my < screen.y1){
|
|
|
|
is_hit = true;
|
|
|
|
}
|
2016-05-29 19:05:27 +00:00
|
|
|
|
2017-01-23 06:19:43 +00:00
|
|
|
if (!is_hit){
|
2016-05-27 17:11:38 +00:00
|
|
|
win32vars.input_chunk.trans.out_of_window = 1;
|
|
|
|
}
|
|
|
|
|
2016-05-29 19:05:27 +00:00
|
|
|
win32vars.input_chunk.pers.mouse_x = mouse_point.x;
|
|
|
|
win32vars.input_chunk.pers.mouse_y = mouse_point.y;
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
win32vars.input_chunk.trans.out_of_window = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
Win32_Input_Chunk input_chunk = win32vars.input_chunk;
|
2016-09-01 20:29:07 +00:00
|
|
|
win32vars.input_chunk.trans = null_input_chunk_transient;
|
2016-05-29 19:05:27 +00:00
|
|
|
|
|
|
|
input_chunk.pers.control_keys[MDFR_CAPS_INDEX] = GetKeyState(VK_CAPITAL) & 0x1;
|
|
|
|
|
2016-09-04 17:09:13 +00:00
|
|
|
win32vars.clipboard_contents = null_string;
|
2016-05-29 19:05:27 +00:00
|
|
|
if (win32vars.clipboard_sequence != 0){
|
|
|
|
DWORD new_number = GetClipboardSequenceNumber();
|
|
|
|
if (new_number != win32vars.clipboard_sequence){
|
|
|
|
win32vars.clipboard_sequence = new_number;
|
|
|
|
if (win32vars.next_clipboard_is_self){
|
|
|
|
win32vars.next_clipboard_is_self = 0;
|
|
|
|
}
|
|
|
|
else{
|
2017-03-23 23:40:16 +00:00
|
|
|
win32_read_clipboard_contents();
|
2016-05-27 17:11:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-05-29 19:05:27 +00:00
|
|
|
|
2016-05-29 19:28:29 +00:00
|
|
|
Application_Step_Input input = {0};
|
|
|
|
|
|
|
|
input.first_step = win32vars.first;
|
|
|
|
|
|
|
|
// NOTE(allen): The expected dt given the frame limit in seconds.
|
|
|
|
input.dt = frame_useconds / 1000000.f;
|
|
|
|
|
|
|
|
input.keys = input_chunk.trans.key_data;
|
|
|
|
|
|
|
|
input.mouse.out_of_window = input_chunk.trans.out_of_window;
|
2016-05-29 19:05:27 +00:00
|
|
|
|
2016-05-29 19:28:29 +00:00
|
|
|
input.mouse.l = input_chunk.pers.mouse_l;
|
|
|
|
input.mouse.press_l = input_chunk.trans.mouse_l_press;
|
|
|
|
input.mouse.release_l = input_chunk.trans.mouse_l_release;
|
2016-05-29 19:05:27 +00:00
|
|
|
|
2016-05-29 19:28:29 +00:00
|
|
|
input.mouse.r = input_chunk.pers.mouse_r;
|
|
|
|
input.mouse.press_r = input_chunk.trans.mouse_r_press;
|
|
|
|
input.mouse.release_r = input_chunk.trans.mouse_r_release;
|
2016-05-29 19:05:27 +00:00
|
|
|
|
2016-05-29 19:28:29 +00:00
|
|
|
input.mouse.wheel = input_chunk.trans.mouse_wheel;
|
|
|
|
input.mouse.x = input_chunk.pers.mouse_x;
|
|
|
|
input.mouse.y = input_chunk.pers.mouse_y;
|
2016-05-29 19:05:27 +00:00
|
|
|
|
2016-05-29 19:28:29 +00:00
|
|
|
input.clipboard = win32vars.clipboard_contents;
|
2016-05-29 19:05:27 +00:00
|
|
|
|
|
|
|
|
2016-05-29 19:28:29 +00:00
|
|
|
Application_Step_Result result = {(Application_Mouse_Cursor)0};
|
2016-05-29 19:05:27 +00:00
|
|
|
result.mouse_cursor_type = APP_MOUSE_CURSOR_DEFAULT;
|
|
|
|
result.lctrl_lalt_is_altgr = win32vars.lctrl_lalt_is_altgr;
|
|
|
|
result.trying_to_kill = input_chunk.trans.trying_to_kill;
|
|
|
|
result.perform_kill = 0;
|
|
|
|
|
2016-09-01 20:29:07 +00:00
|
|
|
if (win32vars.send_exit_signal){
|
2017-07-18 22:09:16 +00:00
|
|
|
result.trying_to_kill = true;
|
|
|
|
win32vars.send_exit_signal = false;
|
2016-09-01 20:29:07 +00:00
|
|
|
}
|
|
|
|
|
2017-07-19 20:07:50 +00:00
|
|
|
app.step(&sysfunc, &target, &memory_vars, &input, &result);
|
2016-05-29 19:05:27 +00:00
|
|
|
|
|
|
|
if (result.perform_kill){
|
2017-07-18 22:09:16 +00:00
|
|
|
keep_running = false;
|
2016-05-29 19:05:27 +00:00
|
|
|
}
|
|
|
|
|
2016-09-01 20:29:07 +00:00
|
|
|
if (win32vars.do_toggle){
|
2017-07-18 22:09:16 +00:00
|
|
|
win32_toggle_fullscreen();
|
|
|
|
win32vars.do_toggle = false;
|
2016-09-01 03:06:46 +00:00
|
|
|
}
|
|
|
|
|
2016-05-29 19:05:27 +00:00
|
|
|
Win32SetCursorFromUpdate(result.mouse_cursor_type);
|
2017-10-31 17:29:52 +00:00
|
|
|
if (win32vars.cursor_show != win32vars.prev_cursor_show){
|
|
|
|
win32vars.prev_cursor_show = win32vars.cursor_show;
|
|
|
|
switch (win32vars.cursor_show){
|
|
|
|
case MouseCursorShow_Never:
|
|
|
|
{
|
|
|
|
i32 counter = 0;
|
|
|
|
do{
|
|
|
|
counter = ShowCursor(false);
|
|
|
|
}while(counter >= 0);
|
|
|
|
}break;
|
|
|
|
|
|
|
|
case MouseCursorShow_Always:
|
|
|
|
{
|
|
|
|
i32 counter = 0;
|
|
|
|
do{
|
|
|
|
counter = ShowCursor(true);
|
|
|
|
}while(counter <= 0);
|
|
|
|
}break;
|
|
|
|
|
|
|
|
// TODO(allen): MouseCursorShow_HideWhenStill
|
|
|
|
}
|
|
|
|
}
|
2016-05-29 19:05:27 +00:00
|
|
|
win32vars.lctrl_lalt_is_altgr = result.lctrl_lalt_is_altgr;
|
|
|
|
|
|
|
|
HDC hdc = GetDC(win32vars.window_handle);
|
|
|
|
Win32RedrawScreen(hdc);
|
|
|
|
ReleaseDC(win32vars.window_handle, hdc);
|
2016-07-19 23:47:27 +00:00
|
|
|
|
2016-05-29 19:05:27 +00:00
|
|
|
win32vars.first = 0;
|
|
|
|
|
|
|
|
if (result.animating){
|
2017-07-18 19:52:41 +00:00
|
|
|
system_schedule_step();
|
2016-05-29 19:05:27 +00:00
|
|
|
}
|
|
|
|
|
2016-07-19 23:47:27 +00:00
|
|
|
flush_thread_group(BACKGROUND_THREADS);
|
|
|
|
|
2016-05-29 19:05:27 +00:00
|
|
|
u64 timer_end = Win32HighResolutionTime();
|
|
|
|
u64 end_target = timer_start + frame_useconds;
|
|
|
|
|
|
|
|
system_release_lock(FRAME_LOCK);
|
|
|
|
while (timer_end < end_target){
|
|
|
|
DWORD samount = (DWORD)((end_target - timer_end) / 1000);
|
|
|
|
if (samount > 0) Sleep(samount);
|
|
|
|
timer_end = Win32HighResolutionTime();
|
|
|
|
}
|
|
|
|
system_acquire_lock(FRAME_LOCK);
|
|
|
|
timer_start = Win32HighResolutionTime();
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
2016-05-27 17:11:38 +00:00
|
|
|
|
2016-05-29 17:07:24 +00:00
|
|
|
return(0);
|
2016-02-20 21:36:16 +00:00
|
|
|
}
|
|
|
|
|
2017-07-18 21:19:28 +00:00
|
|
|
#include "4ed_shared_fonts.cpp"
|
2017-03-23 23:40:16 +00:00
|
|
|
#include "win32_4ed_file_track.cpp"
|
|
|
|
#include "4ed_font_static_functions.cpp"
|
2017-03-23 22:45:32 +00:00
|
|
|
#include "win32_utf8.cpp"
|
2017-03-12 01:20:24 +00:00
|
|
|
|
2016-05-29 03:12:12 +00:00
|
|
|
#if 0
|
2017-03-27 01:52:06 +00:00
|
|
|
// NOTE(allen): In case I want to switch back to a console application at some point.
|
2016-05-29 03:12:12 +00:00
|
|
|
int main(int argc, char **argv){
|
|
|
|
HINSTANCE hInstance = GetModuleHandle(0);
|
2017-03-12 01:20:24 +00:00
|
|
|
}
|
2016-05-29 03:12:12 +00:00
|
|
|
#endif
|
2017-03-12 01:20:24 +00:00
|
|
|
|
|
|
|
// BOTTOM
|
|
|
|
|