From 6f827801e5132abaaa29d6db204d76844708b13b Mon Sep 17 00:00:00 2001 From: Yuval Dolev Date: Thu, 2 Jan 2020 01:13:32 +0200 Subject: [PATCH] macOS platform layer initialization and very basic update functions. The 4coder beta has been rendered successfully for the first time on macOS! --- custom/4coder_base_types.h | 5 +- .../Resources/DWARF/metadata_generator | Bin 136740 -> 136740 bytes opengl/4ed_opengl_defines.h | 7 -- opengl/4ed_opengl_render.cpp | 19 ++-- platform_mac/mac_4ed.mm | 95 +++++++++++------- platform_mac/mac_4ed_functions.mm | 20 ++-- platform_mac/mac_4ed_opengl.mm | 23 +++++ platform_mac/mac_4ed_opengl_funcs.h | 17 ++++ 8 files changed, 120 insertions(+), 66 deletions(-) create mode 100644 platform_mac/mac_4ed_opengl.mm create mode 100644 platform_mac/mac_4ed_opengl_funcs.h diff --git a/custom/4coder_base_types.h b/custom/4coder_base_types.h index 20517f7f..6811fe58 100644 --- a/custom/4coder_base_types.h +++ b/custom/4coder_base_types.h @@ -144,12 +144,13 @@ #endif #endif +// NOTE(yuval): Changed this so that CALL_CONVENTION will be defined for all platforms #if OS_WINDOWS # if ARCH_32BIT # define CALL_CONVENTION __stdcall -# else -# define CALL_CONVENTION # endif +#else +# define CALL_CONVENTION #endif #if defined(JUST_GUESS_INTS) diff --git a/custom/metadata_generator.dSYM/Contents/Resources/DWARF/metadata_generator b/custom/metadata_generator.dSYM/Contents/Resources/DWARF/metadata_generator index 133e453d1c931a6805c0b6e109b54aada975ee7d..815fd87182819f59da407a6ef16a81b7f4629669 100644 GIT binary patch delta 1990 zcmY+Fdr;I>6vyx7cYgR2cDb7)gp3Re{h`iSP#H@r3e7YPGsPp3G?C%qLJnAimXuL0 zr9%(2hlM{IA7v6+B8Y>os6D1J>@{gGUd|8#W;r=I{o_(2yZ3Wm_uO;O zz1!AdwRKocJ#rx3{Sn0;5<=X-j}F@t>2itew$m~&bcQN2q&M1EZnz6)KwDvmIMc$Q z>VcbCRA~sc5tAT8l_7-MgxN)o6|h5@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M!w?{{yxA4vM}G=?ARovllIs^Q$_gf9@pM8pg-vg5rQQ ztcei3D^sj%A*hxu(z+3%Lo&zuI|T2@ITpcNM>r^z6$5DGaOw7uWirpdjZ_J+i$yP@ z4r?*Fz_6Vx;WEoH_0mRCwU{V36Y(I#vw=9}ESqB`n8Dm9hB-ol6fek=BwLz(gN(DO^qg^zvoM z<#Y(v*+bc~Dq+C9fi^I?kkJt_LL_svV}TXRWvfg&Z0N_CN?ei67rsM+X9C=$~g-BqN zgg4QX!jXR+CJ&0f35wcc#gbcWznxXU{b>&Sog)&dt)NbpoVO=(WW4`EP$5h{NtZbW zotK(Ol_KG}oaeJ7+|#_3X0l?;L=5Uz7{#;hIc)VWKn}z6I0q56AD?J0(^hhsshD_Z zJ?Z1gVx#i#W2hj%B}_gxm$?+CiS%Sx9UEXe(>Zq zBGnJLhczdtL46W}06n9I0K9!VNiDM%evo!JRT+hv4pS6&;1thykeQ!_NG|gaU?#od z+%FycRqWzFNOcZv*oDLq=-zz|yNCqn_S=bfCKC12@Ot3W`{YnW!9|+wzaL1XjB?>G zk@rA6dWyR;C>jomxC2GXHMSaYL}J0epzpa)Mgte`my7e0Qb@m``Yw5q+{~MqYR}sx zi~YL|HJ&U)Z#8`0GK4#iC+!AW#)HBAC}W2qe83JvzrXr9v~ihcLhUguq3+BOVvw!h zhU#vB-rHWl62N8F>;v2jpc?%48^}J&&xOxnS)1W93o-Ffsk6>XXs9fFopo5pWmamV zTF2-*-_N>mqYc_ZC9lR-GTiQy9B7g`af_Sw4Oj0)~!=p|>r7y$BfurqmEJ8jQcxjJ(%#bDa - #define GL_TEXTURE_MAX_LEVEL 0x813D #define GL_MULTISAMPLE 0x809D @@ -226,9 +222,6 @@ typedef void GL_Debug_Function(GLenum src, void *user_data); typedef GL_Debug_Function *GLDEBUGPROC; -#define GL_FUNC(N,R,P) typedef R (CALL_CONVENTION N##_Function)P; N##_Function *N = 0; -#include "4ed_opengl_funcs.h" - #endif // BOTTOM diff --git a/opengl/4ed_opengl_render.cpp b/opengl/4ed_opengl_render.cpp index 3e83095d..e3413ae0 100644 --- a/opengl/4ed_opengl_render.cpp +++ b/opengl/4ed_opengl_render.cpp @@ -9,8 +9,6 @@ // TOP -#include "4ed_opengl_defines.h" - internal void gl__bind_texture(Render_Target *t, i32 texid){ if (t->bound_texture != texid){ @@ -225,11 +223,16 @@ gl_render(Render_Target *t){ #if !SHIP_MODE glEnable(GL_DEBUG_OUTPUT); glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); - glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_NOTIFICATION, 0, 0, false); - glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_LOW, 0, 0, false); - glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_MEDIUM, 0, 0, true); - glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_HIGH, 0, 0, true); - glDebugMessageCallback(gl__error_callback, 0); + if (glDebugMessageControl){ + glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_NOTIFICATION, 0, 0, false); + glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_LOW, 0, 0, false); + glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_MEDIUM, 0, 0, true); + glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_HIGH, 0, 0, true); + } + + if (glDebugMessageCallback){ + glDebugMessageCallback(gl__error_callback, 0); + } #endif //////////////////////////////// @@ -256,7 +259,7 @@ gl_render(Render_Target *t){ //////////////////////////////// - { + { t->fallback_texture_id = gl__get_texture(V3i32(2, 2, 1), TextureKind_Mono); u8 white_block[] = { 0xFF, 0xFF, 0xFF, 0xFF, }; gl__fill_texture(TextureKind_Mono, 0, V3i32(0, 0, 0), V3i32(2, 2, 1), white_block); diff --git a/platform_mac/mac_4ed.mm b/platform_mac/mac_4ed.mm index 148108d0..3886cdcd 100644 --- a/platform_mac/mac_4ed.mm +++ b/platform_mac/mac_4ed.mm @@ -54,10 +54,6 @@ #include // NOTE(yuval): Used for proc_pidpath #include // NOTE(yuval): Used for mach_absolute_time, mach_timebase_info, mach_timebase_info_data_t -#define GL_GLEXT_LEGACY -#include -#include - #include // NOTE(yuval): Used for opendir, readdir #include // NOTE(yuval): Used for dlopen, dlclose, dlsym #include // NOTE(yuval): Used for errno @@ -75,9 +71,6 @@ #define global static #define external extern "C" -// NOTE(yuval): This is a hack to fix the CALL_CONVENTION not being defined problem in 4coder_base_types.h -#define CALL_CONVENTION - struct Control_Keys{ b8 l_ctrl; b8 r_ctrl; @@ -168,9 +161,12 @@ struct Mac_Vars { Thread_Context *tctx; - Arena* frame_arena; + Arena *frame_arena; Mac_Input_Chunk input_chunk; + void *base_ptr; + u64 timer_start; + b8 full_screen; b8 do_toggle; @@ -181,8 +177,8 @@ struct Mac_Vars { String_Const_u8 clipboard_contents; - NSWindow* window; - OpenGLView* view; + NSWindow *window; + OpenGLView *view; f32 screen_scale_factor; mach_timebase_info_data_t timebase_info; @@ -203,6 +199,7 @@ struct Mac_Vars { global Mac_Vars mac_vars; global Render_Target target; +global App_Functions app; //////////////////////////////// @@ -266,10 +263,12 @@ mac_to_object(Plat_Handle handle){ //////////////////////////////// -#include "4ed_font_provider_freetype.h" -#include "4ed_font_provider_freetype.h" +#include +#include +#import "mac_4ed_opengl.mm" -#include "opengl/4ed_opengl_render.cpp" +#include "4ed_font_provider_freetype.h" +#include "4ed_font_provider_freetype.cpp" #import "mac_4ed_functions.mm" @@ -297,6 +296,14 @@ mac_file_can_be_made(u8* filename){ return(result); } +function void +mac_resize(float width, float height){ + if ((width > 0.0f) && (height > 0.0f)){ + target.width = width; + target.height = height; + } +} + //////////////////////////////// @implementation AppDelegate @@ -323,13 +330,11 @@ mac_file_can_be_made(u8* filename){ return self; } -- (void)dealloc -{ +- (void)dealloc{ [super dealloc]; } -- (void)prepareOpenGL -{ +- (void)prepareOpenGL{ [super prepareOpenGL]; [[self openGLContext] makeCurrentContext]; @@ -339,8 +344,7 @@ mac_file_can_be_made(u8* filename){ [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; } -- (void)awakeFromNib -{ +- (void)awakeFromNib{ [self init_opengl]; } @@ -352,8 +356,23 @@ mac_file_can_be_made(u8* filename){ } - (void)drawRect:(NSRect)bounds{ - // [self getFrame]; - printf("Draw Rect!\n"); + // NOTE(yuval): Read comment in win32_4ed.cpp's main loop + system_mutex_release(mac_vars.global_frame_mutex); + + Application_Step_Input input = {}; + + // NOTE(yuval): Application Core Update + Application_Step_Result result = {}; + if (app.step != 0){ + result = app.step(mac_vars.tctx, &target, mac_vars.base_ptr, &input); + } + + CGLLockContext([[self openGLContext] CGLContextObj]); + [[self openGLContext] makeCurrentContext]; + gl_render(&target); + [[self openGLContext] flushBuffer]; + CGLUnlockContext([[self openGLContext] CGLContextObj]); + } - (BOOL)windowShouldClose:(NSWindow*)sender{ @@ -424,8 +443,6 @@ mac_file_can_be_made(u8* filename){ } - (void)requestDisplay{ - printf("Display Requested\n"); - [self setNeedsDisplayInRect:[mac_vars.window frame]]; } @end @@ -486,7 +503,6 @@ main(int arg_count, char **args){ // NOTE(yuval): Load core System_Library core_library = {}; - App_Functions app = {}; { App_Get_Functions *get_funcs = 0; Scratch_Block scratch(mac_vars.tctx, Scratch_Share); @@ -516,14 +532,14 @@ main(int arg_count, char **args){ // NOTE(yuval): Init & command line parameters Plat_Settings plat_settings = {}; - void *base_ptr = 0; + mac_vars.base_ptr = 0; { Scratch_Block scratch(mac_vars.tctx, Scratch_Share); String_Const_u8 curdir = system_get_path(scratch, SystemPath_CurrentDirectory); curdir = string_mod_replace_character(curdir, '\\', '/'); char **files = 0; i32 *file_count = 0; - base_ptr = app.read_command_line(mac_vars.tctx, curdir, &plat_settings, &files, &file_count, arg_count, args); + mac_vars.base_ptr = app.read_command_line(mac_vars.tctx, curdir, &plat_settings, &files, &file_count, arg_count, args); { i32 end = *file_count; i32 i = 0, j = 0; @@ -593,6 +609,9 @@ main(int arg_count, char **args){ // Window and GL View Initialization // + // NOTE(yuval): Load OpenGL functions + mac_gl_load_functions(); + // NOTE(yuval): Create NSWindow float w; float h; @@ -601,9 +620,11 @@ main(int arg_count, char **args){ h = (float)plat_settings.window_h; } else{ w = 800.0f; - h = 800.0f; + h = 600.0f; } + mac_resize(w, h); + NSRect screen_rect = [[NSScreen mainScreen] frame]; NSRect initial_frame = NSMakeRect((screen_rect.size.width - w) * 0.5f, (screen_rect.size.height - h) * 0.5f, w, h); @@ -635,6 +656,7 @@ main(int arg_count, char **args){ // // TODO(yuval): Misc System Initializations + // // NOTE(yuval): Get the timebase info mach_timebase_info(&mac_vars.timebase_info); @@ -647,9 +669,18 @@ main(int arg_count, char **args){ Scratch_Block scratch(mac_vars.tctx, Scratch_Share); String_Const_u8 curdir = system_get_path(scratch, SystemPath_CurrentDirectory); curdir = string_mod_replace_character(curdir, '\\', '/'); - app.init(mac_vars.tctx, &target, base_ptr, mac_vars.clipboard_contents, curdir, custom); + app.init(mac_vars.tctx, &target, mac_vars.base_ptr, mac_vars.clipboard_contents, curdir, custom); } + // + // Main loop + // + + mac_vars.global_frame_mutex = system_mutex_make(); + system_mutex_acquire(mac_vars.global_frame_mutex); + + mac_vars.timer_start = system_now_time(); + // NOTE(yuval): Start the app's run loop #if 1 printf("Running using NSApp run\n"); @@ -671,13 +702,5 @@ main(int arg_count, char **args){ } while (event != nil); } #endif - -#if 0 - // NOTE(yuval): Application Core Update - Application_Step_Result result = {}; - if (app.step != 0){ - result = app.step(mac_vars.tctx, &target, base_ptr, &input); - } -#endif } } \ No newline at end of file diff --git a/platform_mac/mac_4ed_functions.mm b/platform_mac/mac_4ed_functions.mm index bae982e8..46543bab 100644 --- a/platform_mac/mac_4ed_functions.mm +++ b/platform_mac/mac_4ed_functions.mm @@ -406,7 +406,10 @@ system_wake_up_timer_set_sig(){ function system_signal_step_sig(){ - [mac_vars.view requestDisplay]; + [NSTimer scheduledTimerWithTimeInterval:0.0 + target:mac_vars.view + selector:@selector(requestDisplay) + userInfo:nil repeats:NO]; } function @@ -801,19 +804,13 @@ system_get_keyboard_modifiers_sig(){ function graphics_get_texture_sig(){ - u32 result = 0; - - NotImplemented; - + u32 result = gl__get_texture(dim, texture_kind); return(result); } function graphics_fill_texture_sig(){ - b32 result = false; - - NotImplemented; - + b32 result = gl__fill_texture(texture_kind, texture, p, dim, data); return(result); } @@ -827,10 +824,7 @@ graphics_fill_texture_sig(){ function font_make_face_sig(){ - Face* result = 0; - - NotImplemented; - + Face* result = ft__font_make_face(arena, description, scale_factor); return(result); } diff --git a/platform_mac/mac_4ed_opengl.mm b/platform_mac/mac_4ed_opengl.mm new file mode 100644 index 00000000..b87492ba --- /dev/null +++ b/platform_mac/mac_4ed_opengl.mm @@ -0,0 +1,23 @@ +/* Mac OpenGL layer for 4coder */ + +#include "opengl/4ed_opengl_defines.h" + +#define GL_FUNC(N,R,P) typedef R (CALL_CONVENTION N##_Function)P; N##_Function *N = 0; +#include "mac_4ed_opengl_funcs.h" + +#include "opengl/4ed_opengl_render.cpp" + +function b32 +mac_gl_load_functions(){ + b32 result = true; + + // NOTE(yuval): Open the gl dynamic library + void* gl_image = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY); + + // NOTE(yuval): Load gl functions +#define GL_FUNC(f,R,P) Stmnt((f) = (f##_Function*)dlsym(gl_image, #f); \ + (result) &= (f != 0);); +#include "mac_4ed_opengl_funcs.h" + + return result; +} diff --git a/platform_mac/mac_4ed_opengl_funcs.h b/platform_mac/mac_4ed_opengl_funcs.h new file mode 100644 index 00000000..649847f9 --- /dev/null +++ b/platform_mac/mac_4ed_opengl_funcs.h @@ -0,0 +1,17 @@ +/* Mac OpenGL functions for 4coder */ + +// TOP +/* Usage: +#define GL_FUNC(N,R,P) ~~~~ +#include "4ed_opengl_funcs.h" +*/ + +GL_FUNC(glDebugMessageControl, void, (GLenum source, GLenum type, GLenum severity, GLsizei count, const GLuint *ids, GLboolean enabled)) +GL_FUNC(glDebugMessageCallback, void, (GLDEBUGPROC callback, const void *userParam)) + +GL_FUNC(glGenVertexArrays, void, (GLsizei n, GLuint *arrays)) +GL_FUNC(glBindVertexArray, void, (GLuint array)) + +GL_FUNC(glVertexAttribIPointer, void, (GLuint index, GLint size, GLenum type, GLsizei stride, const void *pointer)) + +#undef GL_FUNC