macOS platform layer initialization and very basic update functions. The 4coder beta has been rendered successfully for the first time on macOS!

This commit is contained in:
Yuval Dolev 2020-01-02 01:13:32 +02:00
parent debdda8184
commit 6f827801e5
8 changed files with 120 additions and 66 deletions

View File

@ -144,12 +144,13 @@
#endif #endif
#endif #endif
// NOTE(yuval): Changed this so that CALL_CONVENTION will be defined for all platforms
#if OS_WINDOWS #if OS_WINDOWS
# if ARCH_32BIT # if ARCH_32BIT
# define CALL_CONVENTION __stdcall # define CALL_CONVENTION __stdcall
# else
# define CALL_CONVENTION
# endif # endif
#else
# define CALL_CONVENTION
#endif #endif
#if defined(JUST_GUESS_INTS) #if defined(JUST_GUESS_INTS)

View File

@ -12,10 +12,6 @@
#if !defined(FRED_OPENGL_DEFINES_H) #if !defined(FRED_OPENGL_DEFINES_H)
#define 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 <GL/gl.h>
#define GL_TEXTURE_MAX_LEVEL 0x813D #define GL_TEXTURE_MAX_LEVEL 0x813D
#define GL_MULTISAMPLE 0x809D #define GL_MULTISAMPLE 0x809D
@ -226,9 +222,6 @@ typedef void GL_Debug_Function(GLenum src,
void *user_data); void *user_data);
typedef GL_Debug_Function *GLDEBUGPROC; 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 #endif
// BOTTOM // BOTTOM

View File

@ -9,8 +9,6 @@
// TOP // TOP
#include "4ed_opengl_defines.h"
internal void internal void
gl__bind_texture(Render_Target *t, i32 texid){ gl__bind_texture(Render_Target *t, i32 texid){
if (t->bound_texture != texid){ if (t->bound_texture != texid){
@ -225,11 +223,16 @@ gl_render(Render_Target *t){
#if !SHIP_MODE #if !SHIP_MODE
glEnable(GL_DEBUG_OUTPUT); glEnable(GL_DEBUG_OUTPUT);
glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS); glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS);
glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_NOTIFICATION, 0, 0, false); if (glDebugMessageControl){
glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_LOW, 0, 0, false); glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_NOTIFICATION, 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_LOW, 0, 0, false);
glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_HIGH, 0, 0, true); glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_MEDIUM, 0, 0, true);
glDebugMessageCallback(gl__error_callback, 0); glDebugMessageControl(GL_DONT_CARE, GL_DONT_CARE, GL_DEBUG_SEVERITY_HIGH, 0, 0, true);
}
if (glDebugMessageCallback){
glDebugMessageCallback(gl__error_callback, 0);
}
#endif #endif
//////////////////////////////// ////////////////////////////////
@ -256,7 +259,7 @@ gl_render(Render_Target *t){
//////////////////////////////// ////////////////////////////////
{ {
t->fallback_texture_id = gl__get_texture(V3i32(2, 2, 1), TextureKind_Mono); t->fallback_texture_id = gl__get_texture(V3i32(2, 2, 1), TextureKind_Mono);
u8 white_block[] = { 0xFF, 0xFF, 0xFF, 0xFF, }; u8 white_block[] = { 0xFF, 0xFF, 0xFF, 0xFF, };
gl__fill_texture(TextureKind_Mono, 0, V3i32(0, 0, 0), V3i32(2, 2, 1), white_block); gl__fill_texture(TextureKind_Mono, 0, V3i32(0, 0, 0), V3i32(2, 2, 1), white_block);

View File

@ -54,10 +54,6 @@
#include <libproc.h> // NOTE(yuval): Used for proc_pidpath #include <libproc.h> // NOTE(yuval): Used for proc_pidpath
#include <mach/mach_time.h> // NOTE(yuval): Used for mach_absolute_time, mach_timebase_info, mach_timebase_info_data_t #include <mach/mach_time.h> // NOTE(yuval): Used for mach_absolute_time, mach_timebase_info, mach_timebase_info_data_t
#define GL_GLEXT_LEGACY
#include <OpenGL/OpenGL.h>
#include <OpenGL/gl.h>
#include <dirent.h> // NOTE(yuval): Used for opendir, readdir #include <dirent.h> // NOTE(yuval): Used for opendir, readdir
#include <dlfcn.h> // NOTE(yuval): Used for dlopen, dlclose, dlsym #include <dlfcn.h> // NOTE(yuval): Used for dlopen, dlclose, dlsym
#include <errno.h> // NOTE(yuval): Used for errno #include <errno.h> // NOTE(yuval): Used for errno
@ -75,9 +71,6 @@
#define global static #define global static
#define external extern "C" #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{ struct Control_Keys{
b8 l_ctrl; b8 l_ctrl;
b8 r_ctrl; b8 r_ctrl;
@ -168,9 +161,12 @@ struct Mac_Vars {
Thread_Context *tctx; Thread_Context *tctx;
Arena* frame_arena; Arena *frame_arena;
Mac_Input_Chunk input_chunk; Mac_Input_Chunk input_chunk;
void *base_ptr;
u64 timer_start;
b8 full_screen; b8 full_screen;
b8 do_toggle; b8 do_toggle;
@ -181,8 +177,8 @@ struct Mac_Vars {
String_Const_u8 clipboard_contents; String_Const_u8 clipboard_contents;
NSWindow* window; NSWindow *window;
OpenGLView* view; OpenGLView *view;
f32 screen_scale_factor; f32 screen_scale_factor;
mach_timebase_info_data_t timebase_info; mach_timebase_info_data_t timebase_info;
@ -203,6 +199,7 @@ struct Mac_Vars {
global Mac_Vars mac_vars; global Mac_Vars mac_vars;
global Render_Target target; 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 <OpenGL/OpenGL.h>
#include "4ed_font_provider_freetype.h" #include <OpenGL/gl.h>
#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" #import "mac_4ed_functions.mm"
@ -297,6 +296,14 @@ mac_file_can_be_made(u8* filename){
return(result); 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 @implementation AppDelegate
@ -323,13 +330,11 @@ mac_file_can_be_made(u8* filename){
return self; return self;
} }
- (void)dealloc - (void)dealloc{
{
[super dealloc]; [super dealloc];
} }
- (void)prepareOpenGL - (void)prepareOpenGL{
{
[super prepareOpenGL]; [super prepareOpenGL];
[[self openGLContext] makeCurrentContext]; [[self openGLContext] makeCurrentContext];
@ -339,8 +344,7 @@ mac_file_can_be_made(u8* filename){
[[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval]; [[self openGLContext] setValues:&swapInt forParameter:NSOpenGLCPSwapInterval];
} }
- (void)awakeFromNib - (void)awakeFromNib{
{
[self init_opengl]; [self init_opengl];
} }
@ -352,8 +356,23 @@ mac_file_can_be_made(u8* filename){
} }
- (void)drawRect:(NSRect)bounds{ - (void)drawRect:(NSRect)bounds{
// [self getFrame]; // NOTE(yuval): Read comment in win32_4ed.cpp's main loop
printf("Draw Rect!\n"); 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{ - (BOOL)windowShouldClose:(NSWindow*)sender{
@ -424,8 +443,6 @@ mac_file_can_be_made(u8* filename){
} }
- (void)requestDisplay{ - (void)requestDisplay{
printf("Display Requested\n");
[self setNeedsDisplayInRect:[mac_vars.window frame]]; [self setNeedsDisplayInRect:[mac_vars.window frame]];
} }
@end @end
@ -486,7 +503,6 @@ main(int arg_count, char **args){
// NOTE(yuval): Load core // NOTE(yuval): Load core
System_Library core_library = {}; System_Library core_library = {};
App_Functions app = {};
{ {
App_Get_Functions *get_funcs = 0; App_Get_Functions *get_funcs = 0;
Scratch_Block scratch(mac_vars.tctx, Scratch_Share); Scratch_Block scratch(mac_vars.tctx, Scratch_Share);
@ -516,14 +532,14 @@ main(int arg_count, char **args){
// NOTE(yuval): Init & command line parameters // NOTE(yuval): Init & command line parameters
Plat_Settings plat_settings = {}; Plat_Settings plat_settings = {};
void *base_ptr = 0; mac_vars.base_ptr = 0;
{ {
Scratch_Block scratch(mac_vars.tctx, Scratch_Share); Scratch_Block scratch(mac_vars.tctx, Scratch_Share);
String_Const_u8 curdir = system_get_path(scratch, SystemPath_CurrentDirectory); String_Const_u8 curdir = system_get_path(scratch, SystemPath_CurrentDirectory);
curdir = string_mod_replace_character(curdir, '\\', '/'); curdir = string_mod_replace_character(curdir, '\\', '/');
char **files = 0; char **files = 0;
i32 *file_count = 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 end = *file_count;
i32 i = 0, j = 0; i32 i = 0, j = 0;
@ -593,6 +609,9 @@ main(int arg_count, char **args){
// Window and GL View Initialization // Window and GL View Initialization
// //
// NOTE(yuval): Load OpenGL functions
mac_gl_load_functions();
// NOTE(yuval): Create NSWindow // NOTE(yuval): Create NSWindow
float w; float w;
float h; float h;
@ -601,9 +620,11 @@ main(int arg_count, char **args){
h = (float)plat_settings.window_h; h = (float)plat_settings.window_h;
} else{ } else{
w = 800.0f; w = 800.0f;
h = 800.0f; h = 600.0f;
} }
mac_resize(w, h);
NSRect screen_rect = [[NSScreen mainScreen] frame]; 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); 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 // TODO(yuval): Misc System Initializations
//
// NOTE(yuval): Get the timebase info // NOTE(yuval): Get the timebase info
mach_timebase_info(&mac_vars.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); Scratch_Block scratch(mac_vars.tctx, Scratch_Share);
String_Const_u8 curdir = system_get_path(scratch, SystemPath_CurrentDirectory); String_Const_u8 curdir = system_get_path(scratch, SystemPath_CurrentDirectory);
curdir = string_mod_replace_character(curdir, '\\', '/'); 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 // NOTE(yuval): Start the app's run loop
#if 1 #if 1
printf("Running using NSApp run\n"); printf("Running using NSApp run\n");
@ -671,13 +702,5 @@ main(int arg_count, char **args){
} while (event != nil); } while (event != nil);
} }
#endif #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
} }
} }

View File

@ -406,7 +406,10 @@ system_wake_up_timer_set_sig(){
function function
system_signal_step_sig(){ system_signal_step_sig(){
[mac_vars.view requestDisplay]; [NSTimer scheduledTimerWithTimeInterval:0.0
target:mac_vars.view
selector:@selector(requestDisplay)
userInfo:nil repeats:NO];
} }
function function
@ -801,19 +804,13 @@ system_get_keyboard_modifiers_sig(){
function function
graphics_get_texture_sig(){ graphics_get_texture_sig(){
u32 result = 0; u32 result = gl__get_texture(dim, texture_kind);
NotImplemented;
return(result); return(result);
} }
function function
graphics_fill_texture_sig(){ graphics_fill_texture_sig(){
b32 result = false; b32 result = gl__fill_texture(texture_kind, texture, p, dim, data);
NotImplemented;
return(result); return(result);
} }
@ -827,10 +824,7 @@ graphics_fill_texture_sig(){
function function
font_make_face_sig(){ font_make_face_sig(){
Face* result = 0; Face* result = ft__font_make_face(arena, description, scale_factor);
NotImplemented;
return(result); return(result);
} }

View File

@ -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;
}

View File

@ -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