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 133e453d..815fd871 100644 Binary files a/custom/metadata_generator.dSYM/Contents/Resources/DWARF/metadata_generator and b/custom/metadata_generator.dSYM/Contents/Resources/DWARF/metadata_generator differ diff --git a/opengl/4ed_opengl_defines.h b/opengl/4ed_opengl_defines.h index 558b1e98..c64640cf 100644 --- a/opengl/4ed_opengl_defines.h +++ b/opengl/4ed_opengl_defines.h @@ -12,10 +12,6 @@ #if !defined(FRED_OPENGL_DEFINES_H) #define FRED_OPENGL_DEFINES_H -// NOTE(yuval): This file does not exist on MacOS. -// opengl has been included from the platform layer -// #include - #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