refactor: A little bit more similar iOS & MacOS

This commit is contained in:
Carlos Aragones 2020-05-17 21:22:12 +02:00
parent ed0afc6b22
commit 1c3054d320
2 changed files with 232 additions and 230 deletions

View File

@ -1,11 +1,12 @@
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#include <mach/mach_time.h>
#include <MiniFB.h>
#include "MiniFB_internal.h"
#include "WindowData.h"
#include "WindowData_IOS.h"
#include "iOSViewController.h"
#include "WindowData_IOS.h"
#include <MiniFB.h>
#include <MiniFB_internal.h>
#include <WindowData.h>
//-------------------------------------
SWindowData *
@ -27,10 +28,10 @@ create_window_data(unsigned width, unsigned height) {
}
memset((void *) window_data_ios, 0, sizeof(SWindowData_IOS));
float scale = [UIScreen mainScreen].scale;
window_data->specific = window_data_ios;
float scale = [UIScreen mainScreen].scale;
window_data->window_width = [UIScreen mainScreen].bounds.size.width * scale;
window_data->window_height = [UIScreen mainScreen].bounds.size.height * scale;
@ -43,8 +44,8 @@ create_window_data(unsigned width, unsigned height) {
window_data->draw_buffer = malloc(width * height * 4);
if (!window_data->draw_buffer) {
free(window_data);
free(window_data_ios);
free(window_data);
NSLog(@"Unable to create draw buffer");
return 0x0;
}
@ -68,6 +69,7 @@ mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
kUnused(title);
kUnused(flags);
@autoreleasepool {
SWindowData *window_data = create_window_data(width, height);
if (window_data == 0x0) {
return 0x0;
@ -101,6 +103,7 @@ mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
return (struct mfb_window *) window_data;
}
}
//-------------------------------------
static void
@ -144,7 +147,10 @@ mfb_update(struct mfb_window *window, void *buffer) {
//-------------------------------------
mfb_update_state
mfb_update_events(struct mfb_window *window) {
kUnused(window);
if(window == 0x0) {
return STATE_INVALID_WINDOW;
}
return STATE_OK;
}
@ -153,13 +159,20 @@ extern double g_time_for_frame;
bool
mfb_wait_sync(struct mfb_window *window) {
kUnused(window);
if(window == 0x0) {
return false;
}
return true;
}
//-------------------------------------
bool
mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) {
if(window == 0x0) {
return false;
}
SWindowData *window_data = (SWindowData *) window;
if(offset_x + width > window_data->window_width) {

View File

@ -1,10 +1,3 @@
#include "OSXWindow.h"
#include "OSXView.h"
#include "OSXViewDelegate.h"
#include "WindowData_OSX.h"
#include <MiniFB.h>
#include <MiniFB_enums.h>
#include <MiniFB_internal.h>
#include <Cocoa/Cocoa.h>
#if defined(USE_METAL_API)
#include <Carbon/Carbon.h>
@ -14,20 +7,22 @@
#include <sched.h>
#include <mach/mach_time.h>
#include "OSXWindow.h"
#include "OSXView.h"
#include "OSXViewDelegate.h"
#include "WindowData_OSX.h"
#include <MiniFB.h>
#include <MiniFB_internal.h>
#include <MiniFB_enums.h>
void init_keycodes();
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct mfb_window *
mfb_open(const char *title, unsigned width, unsigned height) {
return mfb_open_ex(title, width, height, 0);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------
SWindowData *
create_window_data(unsigned width, unsigned height) {
SWindowData *window_data = malloc(sizeof(SWindowData));
SWindowData *window_data;
window_data = malloc(sizeof(SWindowData));
if(window_data == 0x0) {
NSLog(@"Cannot allocate window data");
return 0x0;
@ -64,10 +59,16 @@ create_window_data(unsigned width, unsigned height) {
return window_data;
}
//-------------------------------------
struct mfb_window *
mfb_open(const char *title, unsigned width, unsigned height) {
return mfb_open_ex(title, width, height, 0);
}
//-------------------------------------
struct mfb_window *
mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags) {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
@autoreleasepool {
SWindowData *window_data = create_window_data(width, height);
if (window_data == 0x0) {
return 0x0;
@ -162,20 +163,17 @@ mfb_open_ex(const char *title, unsigned width, unsigned height, unsigned flags)
NSLog(@"Window created using Cocoa API");
#endif
[pool drain];
return (struct mfb_window *) window_data;
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------
static void
destroy_window_data(SWindowData *window_data) {
if(window_data == 0x0)
return;
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
@autoreleasepool {
SWindowData_OSX *window_data_osx = (SWindowData_OSX *) window_data->specific;
if(window_data_osx != 0x0) {
OSXWindow *window = window_data_osx->window;
@ -197,29 +195,25 @@ destroy_window_data(SWindowData *window_data) {
memset(window_data, 0, sizeof(SWindowData));
free(window_data);
[pool drain];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------
static void
update_events(SWindowData *window_data) {
NSEvent* event;
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
@autoreleasepool {
do {
event = [NSApp nextEventMatchingMask:NSEventMaskAny untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES];
if (event) {
[NSApp sendEvent:event];
}
} while ((window_data->close == false) && event);
[pool release];
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------
mfb_update_state
mfb_update(struct mfb_window *window, void *buffer) {
if(window == 0x0) {
@ -254,8 +248,7 @@ mfb_update(struct mfb_window *window, void *buffer) {
return STATE_OK;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------
mfb_update_state
mfb_update_events(struct mfb_window *window) {
if(window == 0x0) {
@ -280,8 +273,7 @@ mfb_update_events(struct mfb_window *window) {
return STATE_OK;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------
extern double g_time_for_frame;
bool
@ -289,7 +281,7 @@ mfb_wait_sync(struct mfb_window *window) {
NSEvent* event;
if(window == 0x0) {
return STATE_INVALID_WINDOW;
return false;
}
SWindowData *window_data = (SWindowData *) window;
@ -298,8 +290,7 @@ mfb_wait_sync(struct mfb_window *window) {
return false;
}
//NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
@autoreleasepool {
SWindowData_OSX *window_data_osx = (SWindowData_OSX *) window_data->specific;
if(window_data_osx == 0x0) {
return false;
@ -330,14 +321,12 @@ mfb_wait_sync(struct mfb_window *window) {
usleep(millis * 1000);
//sched_yield();
}
//[pool release];
}
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------
bool
mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) {
if(window == 0x0) {
@ -382,8 +371,7 @@ mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y
return true;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------
extern short int g_keycodes[512];
void
@ -508,8 +496,7 @@ init_keycodes() {
g_keycodes[0x4E] = KB_KEY_KP_SUBTRACT;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//-------------------------------------
extern double g_timer_frequency;
extern double g_timer_resolution;
@ -534,8 +521,10 @@ mfb_timer_tick() {
return (high << 32) + highRem + low;
}
//-------------------------------------
void
mfb_timer_init() {
g_timer_frequency = 1e+9;
g_timer_resolution = 1.0 / g_timer_frequency;
}