From 82882f22f5d0ca9ca07ff0060307ba6140ee77de Mon Sep 17 00:00:00 2001 From: Allen Webster Date: Sun, 5 Nov 2017 00:42:55 -0400 Subject: [PATCH] MAC init and basic frame update and render working --- platform_all/4ed_system_shared.cpp | 11 ++ platform_linux/linux_4ed.cpp | 14 +- platform_mac/mac_4ed.cpp | 200 +++++++++++++++++--- platform_mac/mac_4ed.m | 78 ++++++-- platform_mac/osx_objective_c_to_cpp_links.h | 7 +- 5 files changed, 255 insertions(+), 55 deletions(-) diff --git a/platform_all/4ed_system_shared.cpp b/platform_all/4ed_system_shared.cpp index 2df154c2..a64fd53a 100644 --- a/platform_all/4ed_system_shared.cpp +++ b/platform_all/4ed_system_shared.cpp @@ -490,6 +490,7 @@ private_draw_glyph_mono(System_Functions *system, Render_Target *t, Render_Font internal void launch_rendering(System_Functions *system, Render_Target *t){ + DBG_POINT(); char *cursor = t->push_buffer; char *cursor_end = cursor + t->size; @@ -563,16 +564,21 @@ launch_rendering(System_Functions *system, Render_Target *t){ internal void font_load_page_inner(Partition *part, Render_Font *font, FT_Library ft, FT_Face face, b32 use_hinting, Glyph_Page *page, u32 page_number, i32 tab_width){ + DBG_POINT(); Temp_Memory temp = begin_temp_memory(part); Assert(page != 0); page->page_number = page_number; + DBG_POINT(); // prepare to read glyphs into a temporary texture buffer i32 max_glyph_w = face->size->metrics.x_ppem; + + DBG_POINT(); i32 max_glyph_h = font_get_height(font); i32 tex_width = 64; i32 tex_height = 0; + DBG_POINT(); do { tex_width *= 2; float glyphs_per_row = ceilf(tex_width / (float) max_glyph_w); @@ -580,11 +586,13 @@ font_load_page_inner(Partition *part, Render_Font *font, FT_Library ft, FT_Face tex_height = ceil32(rows * (max_glyph_h + 2)); } while(tex_height > tex_width); + DBG_POINT(); tex_height = round_up_pot_u32(tex_height); i32 pen_x = 0; i32 pen_y = 0; + DBG_POINT(); u32* pixels = push_array(part, u32, tex_width * tex_height); memset(pixels, 0, tex_width * tex_height * sizeof(u32)); @@ -655,7 +663,10 @@ font_load_page_inner(Partition *part, Render_Font *font, FT_Library ft, FT_Face page->tex_width = tex_width; page->tex_height = tex_height; + DBG_POINT(); glGenTextures(1, &page->tex); + + DBG_POINT(); glBindTexture(GL_TEXTURE_2D, page->tex); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); diff --git a/platform_linux/linux_4ed.cpp b/platform_linux/linux_4ed.cpp index fe4ac9d3..eb8b751c 100644 --- a/platform_linux/linux_4ed.cpp +++ b/platform_linux/linux_4ed.cpp @@ -929,7 +929,7 @@ LinuxKeycodeInit(Display* dpy){ } internal void -LinuxPushKey(Key_Code code, Key_Code chr, Key_Code chr_nocaps, b8 (*mods)[MDFR_INDEX_COUNT]) +LinuxPushKey(Key_Code code, Key_Code chr, Key_Code chr_nocaps, b8 *mods) { i32 *count = &linuxvars.input.keys.count; Key_Event_Data *data = linuxvars.input.keys.keys; @@ -939,7 +939,7 @@ LinuxPushKey(Key_Code code, Key_Code chr, Key_Code chr_nocaps, b8 (*mods)[MDFR_I data[*count].character = chr; data[*count].character_no_caps_lock = chr_nocaps; - memcpy(data[*count].modifiers, *mods, sizeof(*mods)); + memcpy(data[*count].modifiers, mods, sizeof(*mods)*MDFR_INDEX_COUNT); ++(*count); } @@ -1307,11 +1307,11 @@ LinuxHandleX11Events(void) Key_Code special_key = keycode_lookup_table[(u8)Event.xkey.keycode]; if (special_key){ - LinuxPushKey(special_key, 0, 0, &mods); + LinuxPushKey(special_key, 0, 0, mods); } else if (key < 256){ - LinuxPushKey(key, key, key_no_caps, &mods); + LinuxPushKey(key, key, key_no_caps, mods); } else { - LinuxPushKey(0, 0, 0, &mods); + LinuxPushKey(0, 0, 0, mods); } }break; @@ -1748,7 +1748,7 @@ main(int argc, char **argv){ linuxvars.input.first_step = 1; linuxvars.input.dt = (frame_useconds / 1000000.f); - while (1){ + for (;;){ if (XEventsQueued(linuxvars.XDisplay, QueuedAlready)){ LinuxHandleX11Events(); } @@ -1825,7 +1825,7 @@ main(int argc, char **argv){ } b32 keep_running = linuxvars.keep_running; - + app.step(&sysfunc, &target, &memory_vars, &linuxvars.input, &result); if (result.perform_kill){ diff --git a/platform_mac/mac_4ed.cpp b/platform_mac/mac_4ed.cpp index 0e2b89d9..90e1835f 100644 --- a/platform_mac/mac_4ed.cpp +++ b/platform_mac/mac_4ed.cpp @@ -12,7 +12,12 @@ #define IS_PLAT_LAYER #include + +#if 0 #define DBG_POINT() fprintf(stdout, "%s\n", __FILE__ ":" LINE_STR ":") +#else +#define DBG_POINT() +#endif #include "4ed_defines.h" #include "4coder_API/version.h" @@ -78,8 +83,17 @@ global System_Functions sysfunc; //////////////////////////////// +struct OSX_Vars{ + Application_Step_Input input; + String clipboard_contents; + b32 keep_running; +}; + +//////////////////////////////// + #include "osx_objective_c_to_cpp_links.h" -OSX_Vars osx; +OSX_Objective_C_Vars osx_objc; +OSX_Vars osxvars; global Render_Target target; global Application_Memory memory_vars; global Plat_Settings plat_settings; @@ -135,20 +149,20 @@ Sys_Show_Mouse_Cursor_Sig(system_show_mouse_cursor){ internal Sys_Set_Fullscreen_Sig(system_set_fullscreen){ - osx.do_toggle = (osx.full_screen != full_screen); + osx_objc.do_toggle = (osx_objc.full_screen != full_screen); return(true); } internal Sys_Is_Fullscreen_Sig(system_is_fullscreen){ - b32 result = (osx.full_screen != osx.do_toggle); + b32 result = (osx_objc.full_screen != osx_objc.do_toggle); return(result); } // HACK(allen): Why does this work differently from the win32 version!? internal Sys_Send_Exit_Signal_Sig(system_send_exit_signal){ - osx.running = false; + osx_objc.running = false; } #include "4ed_coroutine_functions.cpp" @@ -161,16 +175,16 @@ internal Sys_Post_Clipboard_Sig(system_post_clipboard){ char *string = str.str; if (!terminate_with_null(&str)){ - if (osx.clipboard_space_max <= str.size + 1){ - if (osx.clipboard_space != 0){ - system_memory_free(osx.clipboard_space, osx.clipboard_space_max); + if (osx_objc.clipboard_space_max <= str.size + 1){ + if (osx_objc.clipboard_space != 0){ + system_memory_free(osx_objc.clipboard_space, osx_objc.clipboard_space_max); } - osx.clipboard_space_max = l_round_up_u32(str.size*2 + 1, KB(4096)); - osx.clipboard_space = (char*)system_memory_allocate(osx.clipboard_space_max); + osx_objc.clipboard_space_max = l_round_up_u32(str.size*2 + 1, KB(4096)); + osx_objc.clipboard_space = (char*)system_memory_allocate(osx_objc.clipboard_space_max); } - memcpy(osx.clipboard_space, str.str, str.size); - osx.clipboard_space[str.size] = 0; - string = osx.clipboard_space; + memcpy(osx_objc.clipboard_space, str.str, str.size); + osx_objc.clipboard_space[str.size] = 0; + string = osx_objc.clipboard_space; } osx_post_to_clipboard(string); } @@ -224,37 +238,172 @@ osx_allocate(umem size){ external void osx_resize(int width, int height){ DBG_POINT(); - osx.width = width; - osx.height = height; - // TODO + osx_objc.width = width; + osx_objc.height = 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); + + target.width = width; + target.height = height; + } +} + +internal void +osx_push_key(Key_Code code, Key_Code chr, Key_Code chr_nocaps, b8 *mods) +{ + i32 count = osxvars.input.keys.count; + + if (count < KEY_INPUT_BUFFER_SIZE){ + Key_Event_Data *data = osxvars.input.keys.keys; + + data[count].keycode = code; + data[count].character = chr; + data[count].character_no_caps_lock = chr_nocaps; + + memcpy(data[count].modifiers, mods, sizeof(*mods)*MDFR_INDEX_COUNT); + + osxvars.input.keys.count = count + 1; + } } external void osx_character_input(u32 code, OSX_Keyboard_Modifiers modifier_flags){ DBG_POINT(); - // TODO + Key_Code c = 0; + switch (code){ + // TODO(allen): Find the canonical list of these things. + case 0x007F: c = key_back; break; + case 0xF700: c = key_up; break; + case 0xF701: c = key_down; break; + case 0xF702: c = key_left; break; + case 0xF703: c = key_right; break; + case 0x001B: c = key_esc; break; + + case 0xF704: c = key_f1; break; + case 0xF705: c = key_f2; break; + case 0xF706: c = key_f3; break; + case 0xF707: c = key_f4; break; + + case 0xF708: c = key_f5; break; + case 0xF709: c = key_f6; break; + case 0xF70A: c = key_f7; break; + case 0xF70B: c = key_f8; break; + + case 0xF70C: c = key_f9; break; + case 0xF70D: c = key_f10; break; + case 0xF70E: c = key_f11; break; + case 0xF70F: c = key_f12; break; + + case 0xF710: c = key_f13; break; + case 0xF711: c = key_f14; break; + case 0xF712: c = key_f15; break; + case 0xF713: c = key_f16; break; + } + + b8 mods[MDFR_INDEX_COUNT] = {0}; + + if (modifier_flags.shift) mods[MDFR_SHIFT_INDEX] = 1; + if (modifier_flags.command) mods[MDFR_CONTROL_INDEX] = 1; + if (modifier_flags.caps) mods[MDFR_CAPS_INDEX] = 1; + if (modifier_flags.control) mods[MDFR_ALT_INDEX] = 1; + + if (c != 0){ + osx_push_key(c, 0, 0, mods); + } + else if (code != 0){ + if (code == '\r'){ + code = '\n'; + } + Key_Code nocaps = code; + if (modifier_flags.caps){ + if ('a' <= nocaps && nocaps <= 'z'){ + nocaps += 'A' - 'a'; + } + else if ('A' <= nocaps && nocaps <= 'Z'){ + nocaps += 'a' - 'A'; + } + } + osx_push_key(code, code, nocaps, mods); + } + else{ + osx_push_key(0, 0, 0, mods); + } } external void osx_mouse(i32 mx, i32 my, u32 type){ DBG_POINT(); - // TODO + osxvars.input.mouse.x = mx; + osxvars.input.mouse.y = my; + if (type == MouseType_Press){ + osxvars.input.mouse.press_l = true; + osxvars.input.mouse.l = true; + } + if (type == MouseType_Release){ + osxvars.input.mouse.l = false; + } } external void osx_mouse_wheel(float dx, float dy){ DBG_POINT(); - // TODO + if (dy > 0){ + osxvars.input.mouse.wheel = 1; + } + else if (dy < 0){ + osxvars.input.mouse.wheel = -1; + } } external void osx_step(){ - DBG_POINT(); - // TODO + Application_Step_Result result = {}; + result.mouse_cursor_type = APP_MOUSE_CURSOR_DEFAULT; + result.trying_to_kill = !osxvars.keep_running; + + osxvars.input.clipboard = null_string; + + app.step(&sysfunc, &target, &memory_vars, &osxvars.input, &result); + launch_rendering(&sysfunc, &target); + + osxvars.input.first_step = false; + osxvars.input.keys = null_key_input_data; + osxvars.input.mouse.press_l = false; + osxvars.input.mouse.release_l = false; + osxvars.input.mouse.press_r = false; + osxvars.input.mouse.release_r = false; + osxvars.input.mouse.wheel = 0; } external void osx_init(){ + // TODO(allen): Setup GL DEBUG MESSAGE +#if defined(FRED_INTERNAL) && 0 + // + // OpenGL Init + // + + typedef PFNGLDEBUGMESSAGECALLBACKARBPROC glDebugMessageCallbackProc; + + GLXLOAD(glDebugMessageCallback); + + if (glDebugMessageCallback){ + LOG("Enabling GL Debug Callback\n"); + glDebugMessageCallback(&LinuxGLDebugCallback, 0); + glEnable(GL_DEBUG_OUTPUT); + } +#endif + + glEnable(GL_TEXTURE_2D); + glEnable(GL_SCISSOR_TEST); + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); + // // System Linkage // @@ -276,6 +425,9 @@ osx_init(){ memset(&custom_api, 0, sizeof(custom_api)); memory_init(); + + osxvars.keep_running = true; + osxvars.input.first_step = true; // // HACK(allen): @@ -303,7 +455,7 @@ osx_init(){ // DBG_POINT(); - read_command_line(osx.argc, osx.argv); + read_command_line(osx_objc.argc, osx_objc.argv); // // Threads @@ -344,13 +496,13 @@ osx_init(){ DBG_POINT(); String clipboard_string = {0}; - if (osx.has_clipboard_item){ - clipboard_string = make_string(osx.clipboard_data, osx.clipboard_size); + if (osx_objc.has_clipboard_item){ + clipboard_string = make_string(osx_objc.clipboard_data, osx_objc.clipboard_size); } DBG_POINT(); fprintf(stdout, "%p\n", app.init); - + LOG("Initializing application variables\n"); app.init(&sysfunc, &target, &memory_vars, clipboard_string, curdir, custom_api); diff --git a/platform_mac/mac_4ed.m b/platform_mac/mac_4ed.m index dc863004..95876af7 100644 --- a/platform_mac/mac_4ed.m +++ b/platform_mac/mac_4ed.m @@ -11,6 +11,7 @@ #include "4ed_defines.h" #include "4coder_API/version.h" +#include "4coder_API/keycodes.h" #define WINDOW_NAME "4coder" VERSION @@ -35,7 +36,7 @@ osx_post_to_clipboard(char *str){ [board declareTypes:typesArray owner:nil]; NSString *paste_string = [NSString stringWithUTF8String:str]; [board setString:paste_string forType:utf8_type]; - osx.just_posted_to_clipboard = true; + osx_objc.just_posted_to_clipboard = true; } void @@ -81,6 +82,7 @@ static DISPLINK_SIG(osx_display_link); mods.command = ((flags & NSEventModifierFlagCommand) != 0); mods.control = ((flags & NSEventModifierFlagControl) != 0); mods.option = ((flags & NSEventModifierFlagOption) != 0); + mods.caps = ((flags & NSEventModifierFlagCapsLock) != 0); u32 length = real.length; for (u32 i = 0; i < length; ++i){ @@ -118,10 +120,10 @@ static DISPLINK_SIG(osx_display_link); - (CVReturn)getFrameForTime:(const CVTimeStamp*)time{ @autoreleasepool { - if (osx.running){ + if (osx_objc.running){ NSPasteboard *board = [NSPasteboard generalPasteboard]; - if (board.changeCount != osx.prev_clipboard_change_count){ - if (!osx.just_posted_to_clipboard){ + if (board.changeCount != osx_objc.prev_clipboard_change_count){ + if (!osx_objc.just_posted_to_clipboard){ NSString *utf8_type = @"public.utf8-plain-text"; NSArray *array = [NSArray arrayWithObjects: utf8_type, nil]; NSString *has_string = [board availableTypeFromArray:array]; @@ -131,20 +133,20 @@ static DISPLINK_SIG(osx_display_link); u32 copy_length = data.length; if (copy_length > 0){ // TODO(allen): Grow clipboard memory if needed. - if (copy_length+1 < osx.clipboard_max){ - osx.clipboard_size = copy_length; - [data getBytes: osx.clipboard_data length: copy_length]; - ((char*)osx.clipboard_data)[copy_length] = 0; - osx.has_clipboard_item = true; + if (copy_length+1 < osx_objc.clipboard_max){ + osx_objc.clipboard_size = copy_length; + [data getBytes: osx_objc.clipboard_data length: copy_length]; + ((char*)osx_objc.clipboard_data)[copy_length] = 0; + osx_objc.has_clipboard_item = true; } } } } } else{ - osx.just_posted_to_clipboard = false; + osx_objc.just_posted_to_clipboard = false; } - osx.prev_clipboard_change_count = board.changeCount; + osx_objc.prev_clipboard_change_count = board.changeCount; } CGLLockContext([[self openGLContext] CGLContextObj]); @@ -167,7 +169,7 @@ static DISPLINK_SIG(osx_display_link); - (void)init_gl { - if(osx.running) + if(osx_objc.running) { return; } @@ -201,7 +203,7 @@ static DISPLINK_SIG(osx_display_link); [context makeCurrentContext]; - osx.running = true; + osx_objc.running = true; } - (id)init @@ -284,6 +286,15 @@ DISPLINK_SIG(osx_display_link){ } @end +typedef struct File_Change_Queue{ + char *buffer; + char *read_ptr; + char *write_ptr; + char *end; +} File_Change_Queue; + +static File_Change_Queue file_change_queue = {0}; + void osx_add_file_listener(char *file_name){ NotImplemented; @@ -294,24 +305,47 @@ osx_remove_file_listener(char *file_name){ NotImplemented; } +void +block_split_copy(void *dst, void *src1, i32 size1, void *src2, i32 size2){ + memcpy(dst, src1, size1); + memcpy((u8*)dst + size1, src2, size2); +} + i32 osx_get_file_change_event(char *buffer, i32 max, i32 *size){ i32 result = 0; - NotImplemented; + if (file_change_queue.read_ptr != file_change_queue.write_ptr){ + i32 change_size = *(i32*)file_change_queue.read_ptr; + if (max <= change_size){ + char *b1 = file_change_queue.read_ptr + 4; + char *b2 = file_change_queue.buffer; + i32 b1_size = Min(change_size, (i32)(file_change_queue.end - b1)); + i32 b2_size = change_size - b1_size; + block_split_copy(buffer, b1, b1_size, b2, b2_size); + if (b1 < file_change_queue.end){ + file_change_queue.read_ptr = b1 + change_size; + } + else{ + file_change_queue.read_ptr = b2 + b2_size; + } + result = 1; + } + else{ + result = -1; + } + } return(result); } int main(int argc, char **argv){ - memset(&osx, 0, sizeof(osx)); + memset(&osx_objc, 0, sizeof(osx_objc)); umem clipboard_size = MB(4); - osx.clipboard_data = osx_allocate(clipboard_size); - osx.clipboard_max = clipboard_size; - osx.argc = argc; - osx.argv = argv; - - osx_init(); + osx_objc.clipboard_data = osx_allocate(clipboard_size); + osx_objc.clipboard_max = clipboard_size; + osx_objc.argc = argc; + osx_objc.argv = argv; @autoreleasepool{ NSApplication *app = [NSApplication sharedApplication]; @@ -337,6 +371,8 @@ main(int argc, char **argv){ [window setTitle:@WINDOW_NAME]; [window makeKeyAndOrderFront:nil]; + osx_init(); + [NSApp run]; } diff --git a/platform_mac/osx_objective_c_to_cpp_links.h b/platform_mac/osx_objective_c_to_cpp_links.h index 59ed9e43..e25fac70 100644 --- a/platform_mac/osx_objective_c_to_cpp_links.h +++ b/platform_mac/osx_objective_c_to_cpp_links.h @@ -23,9 +23,10 @@ typedef struct OSX_Keyboard_Modifiers{ b32 command; b32 control; b32 option; + b32 caps; } OSX_Keyboard_Modifiers; -typedef struct OSX_Vars{ +typedef struct OSX_Objective_C_Vars{ i32 width, height; b32 running; u32 key_count; @@ -45,10 +46,10 @@ typedef struct OSX_Vars{ i32 argc; char **argv; -} OSX_Vars; +} OSX_Objective_C_Vars; // In C++ layer. -extern OSX_Vars osx; +extern OSX_Objective_C_Vars osx_objc; external void* osx_allocate(umem size);