fix issue on macos x

This commit is contained in:
Carlos Aragones 2020-04-26 14:10:18 +02:00
parent 8d57780915
commit d0dd063b8f
9 changed files with 164 additions and 96 deletions

View File

@ -59,13 +59,13 @@ const uint8_t * mfb_get_key_buffer(struct mfb_window *window); // O
void mfb_set_target_fps(uint32_t fps); void mfb_set_target_fps(uint32_t fps);
bool mfb_wait_sync(struct mfb_window *window); bool mfb_wait_sync(struct mfb_window *window);
struct mfb_timer * mfb_timer_create(); struct mfb_timer * mfb_timer_create(void);
void mfb_timer_destroy(struct mfb_timer *tmr); void mfb_timer_destroy(struct mfb_timer *tmr);
void mfb_timer_reset(struct mfb_timer *tmr); void mfb_timer_reset(struct mfb_timer *tmr);
double mfb_timer_now(struct mfb_timer *tmr); double mfb_timer_now(struct mfb_timer *tmr);
double mfb_timer_delta(struct mfb_timer *tmr); double mfb_timer_delta(struct mfb_timer *tmr);
double mfb_timer_get_frequency(); double mfb_timer_get_frequency(void);
double mfb_timer_get_resolution(); double mfb_timer_get_resolution(void);
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -565,6 +565,9 @@ mfb_get_key_name(mfb_key key) {
case KB_KEY_MENU: case KB_KEY_MENU:
return "Menu"; return "Menu";
case KB_KEY_UNKNOWN:
return "Unknown";
} }
return "Unknown"; return "Unknown";
} }

View File

@ -18,7 +18,7 @@ extern "C" {
#endif #endif
extern short int g_keycodes[512]; extern short int g_keycodes[512];
void init_keycodes(); void init_keycodes(void);
void keyboard_default(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed); void keyboard_default(struct mfb_window *window, mfb_key key, mfb_key_mod mod, bool isPressed);
#if defined(__cplusplus) #if defined(__cplusplus)

View File

@ -1,3 +1,5 @@
#if defined(__linux__)
#include <time.h> #include <time.h>
#include <MiniFB.h> #include <MiniFB.h>
@ -30,3 +32,6 @@ mfb_timer_init() {
} }
g_timer_resolution = 1.0 / g_timer_frequency; g_timer_resolution = 1.0 / g_timer_frequency;
} }
#endif

View File

@ -8,8 +8,8 @@ double g_timer_resolution;
double g_time_for_frame = 1.0 / 60.0; double g_time_for_frame = 1.0 / 60.0;
//------------------------------------- //-------------------------------------
extern uint64_t mfb_timer_tick(); extern uint64_t mfb_timer_tick(void);
extern void mfb_timer_init(); extern void mfb_timer_init(void);
//------------------------------------- //-------------------------------------
void void

View File

@ -310,10 +310,13 @@ mfb_update(struct mfb_window *window, void *buffer) {
#endif #endif
update_events(window_data); update_events(window_data);
if(window_data->close == false) { if(window_data->close) {
destroy_window_data(window_data);
return STATE_EXIT;
}
SWindowData_OSX *window_data_osx = (SWindowData_OSX *) window_data->specific; SWindowData_OSX *window_data_osx = (SWindowData_OSX *) window_data->specific;
[[window_data_osx->window contentView] setNeedsDisplay:YES]; [[window_data_osx->window contentView] setNeedsDisplay:YES];
}
return STATE_OK; return STATE_OK;
} }
@ -333,10 +336,13 @@ mfb_update_events(struct mfb_window *window) {
} }
update_events(window_data); update_events(window_data);
if(window_data->close == false) { if(window_data->close) {
destroy_window_data(window_data);
return STATE_EXIT;
}
SWindowData_OSX *window_data_osx = (SWindowData_OSX *) window_data->specific; SWindowData_OSX *window_data_osx = (SWindowData_OSX *) window_data->specific;
[[window_data_osx->window contentView] setNeedsDisplay:YES]; [[window_data_osx->window contentView] setNeedsDisplay:YES];
}
return STATE_OK; return STATE_OK;
} }
@ -362,6 +368,10 @@ mfb_wait_sync(struct mfb_window *window) {
//NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init]; //NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
SWindowData_OSX *window_data_osx = (SWindowData_OSX *) window_data->specific; SWindowData_OSX *window_data_osx = (SWindowData_OSX *) window_data->specific;
if(window_data_osx == 0x0) {
return false;
}
double current; double current;
uint32_t millis = 1; uint32_t millis = 1;
while(1) { while(1) {
@ -397,6 +407,10 @@ mfb_wait_sync(struct mfb_window *window) {
bool bool
mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y, unsigned width, unsigned height) { 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; SWindowData *window_data = (SWindowData *) window;
if(offset_x + width > window_data->window_width) { if(offset_x + width > window_data->window_width) {

View File

@ -72,6 +72,9 @@
- (void)flagsChanged:(NSEvent *)event - (void)flagsChanged:(NSEvent *)event
{ {
if(window_data == 0x0)
return;
const uint32_t flags = [event modifierFlags]; const uint32_t flags = [event modifierFlags];
uint32_t mod_keys = 0, mod_keys_aux = 0; uint32_t mod_keys = 0, mod_keys_aux = 0;
@ -135,29 +138,34 @@
- (void)keyDown:(NSEvent *)event - (void)keyDown:(NSEvent *)event
{ {
if(window_data != 0x0) {
short int key_code = g_keycodes[[event keyCode] & 0x1ff]; short int key_code = g_keycodes[[event keyCode] & 0x1ff];
window_data->key_status[key_code] = true; window_data->key_status[key_code] = true;
kCall(keyboard_func, key_code, window_data->mod_keys, true); kCall(keyboard_func, key_code, window_data->mod_keys, true);
}
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)keyUp:(NSEvent *)event - (void)keyUp:(NSEvent *)event
{ {
if(window_data != 0x0) {
short int key_code = g_keycodes[[event keyCode] & 0x1ff]; short int key_code = g_keycodes[[event keyCode] & 0x1ff];
window_data->key_status[key_code] = false; window_data->key_status[key_code] = false;
kCall(keyboard_func, key_code, window_data->mod_keys, false); kCall(keyboard_func, key_code, window_data->mod_keys, false);
}
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)insertText:(id)string replacementRange:(NSRange)replacementRange - (void)insertText:(id)string replacementRange:(NSRange)replacementRange
{ {
kUnused(replacementRange);
if(window_data != 0x0) {
NSString *characters; NSString *characters;
NSUInteger length; NSUInteger length;
kUnused(replacementRange);
if ([string isKindOfClass:[NSAttributedString class]]) if ([string isKindOfClass:[NSAttributedString class]])
characters = [string string]; characters = [string string];
else else
@ -172,6 +180,7 @@
kCall(char_input_func, code); kCall(char_input_func, code);
} }
}
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -180,20 +189,22 @@
{ {
kUnused(notification); kUnused(notification);
if(window_data != 0x0) {
if(window_data->is_active == true) { if(window_data->is_active == true) {
window_data->is_active = false; window_data->is_active = false;
kCall(active_func, false); kCall(active_func, false);
} }
}
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)setContentView:(NSView *)aView - (void)setContentView:(NSView *)aView
{ {
if ([childContentView isEqualTo:aView]) if ([childContentView isEqualTo:aView]) {
{
return; return;
} }
NSRect bounds = [self frame]; NSRect bounds = [self frame];
bounds.origin = NSZeroPoint; bounds.origin = NSZeroPoint;
@ -232,15 +243,19 @@
- (void)windowDidBecomeKey:(NSNotification *)notification - (void)windowDidBecomeKey:(NSNotification *)notification
{ {
kUnused(notification); kUnused(notification);
if(window_data != 0x0) {
window_data->is_active = true; window_data->is_active = true;
kCall(active_func, true); kCall(active_func, true);
}
} }
- (void)windowDidResignKey:(NSNotification *)notification - (void)windowDidResignKey:(NSNotification *)notification
{ {
kUnused(notification); kUnused(notification);
if(window_data) {
window_data->is_active = false; window_data->is_active = false;
kCall(active_func, false); kCall(active_func, false);
}
} }
- (void)windowWillClose:(NSNotification *)notification { - (void)windowWillClose:(NSNotification *)notification {
@ -277,19 +292,23 @@
- (void)willClose - (void)willClose
{ {
if(window_data != 0x0) {
window_data->close = true; window_data->close = true;
}
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)windowDidResize:(NSNotification *)notification { - (void)windowDidResize:(NSNotification *)notification {
kUnused(notification); kUnused(notification);
if(window_data != 0x0) {
CGSize size = [self contentRectForFrameRect:[self frame]].size; CGSize size = [self contentRectForFrameRect:[self frame]].size;
window_data->window_width = size.width; window_data->window_width = size.width;
window_data->window_height = size.height; window_data->window_height = size.height;
kCall(resize_func, size.width, size.height); kCall(resize_func, size.width, size.height);
}
} }
@end @end

View File

@ -136,7 +136,7 @@ extern Vertex g_vertices[4];
{ {
(void)rect; (void)rect;
if(!window_data) if(window_data == 0x0)
return; return;
SWindowData_OSX *window_data_osx = (SWindowData_OSX *) window_data->specific; SWindowData_OSX *window_data_osx = (SWindowData_OSX *) window_data->specific;
@ -181,7 +181,9 @@ extern Vertex g_vertices[4];
- (void)mouseDown:(NSEvent*)event - (void)mouseDown:(NSEvent*)event
{ {
(void)event; (void)event;
if(window_data != 0x0) {
kCall(mouse_btn_func, MOUSE_BTN_1, window_data->mod_keys, true); kCall(mouse_btn_func, MOUSE_BTN_1, window_data->mod_keys, true);
}
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -189,7 +191,9 @@ extern Vertex g_vertices[4];
- (void)mouseUp:(NSEvent*)event - (void)mouseUp:(NSEvent*)event
{ {
(void)event; (void)event;
if(window_data != 0x0) {
kCall(mouse_btn_func, MOUSE_BTN_1, window_data->mod_keys, false); kCall(mouse_btn_func, MOUSE_BTN_1, window_data->mod_keys, false);
}
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -197,7 +201,9 @@ extern Vertex g_vertices[4];
- (void)rightMouseDown:(NSEvent*)event - (void)rightMouseDown:(NSEvent*)event
{ {
(void)event; (void)event;
if(window_data != 0x0) {
kCall(mouse_btn_func, MOUSE_BTN_2, window_data->mod_keys, true); kCall(mouse_btn_func, MOUSE_BTN_2, window_data->mod_keys, true);
}
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -205,7 +211,9 @@ extern Vertex g_vertices[4];
- (void)rightMouseUp:(NSEvent*)event - (void)rightMouseUp:(NSEvent*)event
{ {
(void)event; (void)event;
if(window_data != 0x0) {
kCall(mouse_btn_func, MOUSE_BTN_1, window_data->mod_keys, false); kCall(mouse_btn_func, MOUSE_BTN_1, window_data->mod_keys, false);
}
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -213,7 +221,9 @@ extern Vertex g_vertices[4];
- (void)otherMouseDown:(NSEvent *)event - (void)otherMouseDown:(NSEvent *)event
{ {
(void)event; (void)event;
if(window_data != 0x0) {
kCall(mouse_btn_func, [event buttonNumber], window_data->mod_keys, true); kCall(mouse_btn_func, [event buttonNumber], window_data->mod_keys, true);
}
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -221,14 +231,18 @@ extern Vertex g_vertices[4];
- (void)otherMouseUp:(NSEvent *)event - (void)otherMouseUp:(NSEvent *)event
{ {
(void)event; (void)event;
if(window_data != 0x0) {
kCall(mouse_btn_func, [event buttonNumber], window_data->mod_keys, false); kCall(mouse_btn_func, [event buttonNumber], window_data->mod_keys, false);
}
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- (void)scrollWheel:(NSEvent *)event - (void)scrollWheel:(NSEvent *)event
{ {
if(window_data != 0x0) {
kCall(mouse_wheel_func, window_data->mod_keys, [event deltaX], [event deltaY]); kCall(mouse_wheel_func, window_data->mod_keys, [event deltaX], [event deltaY]);
}
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@ -250,11 +264,13 @@ extern Vertex g_vertices[4];
- (void)mouseMoved:(NSEvent *)event - (void)mouseMoved:(NSEvent *)event
{ {
if(window_data != 0x0) {
NSPoint point = [event locationInWindow]; NSPoint point = [event locationInWindow];
//NSPoint localPoint = [self convertPoint:point fromView:nil]; //NSPoint localPoint = [self convertPoint:point fromView:nil];
window_data->mouse_pos_x = point.x; window_data->mouse_pos_x = point.x;
window_data->mouse_pos_y = point.y; window_data->mouse_pos_y = point.y;
kCall(mouse_move_func, point.x, point.y); kCall(mouse_move_func, point.x, point.y);
}
} }
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

View File

@ -107,6 +107,7 @@ main()
mfb_set_mouse_scroll_callback(window_a, mouse_scroll); mfb_set_mouse_scroll_callback(window_a, mouse_scroll);
mfb_set_user_data(window_a, (void *) "Window A"); mfb_set_user_data(window_a, (void *) "Window A");
mfb_set_viewport(window_a, 25, 25, WIDTH_A-25, HEIGHT_A-25);
//-- //--
struct mfb_window *window_b = mfb_open_ex("Secondary Window", WIDTH_B, HEIGHT_B, WF_RESIZABLE); struct mfb_window *window_b = mfb_open_ex("Secondary Window", WIDTH_B, HEIGHT_B, WF_RESIZABLE);
@ -150,6 +151,7 @@ main()
mfb_update_state state_a, state_b; mfb_update_state state_a, state_b;
if(window_a != 0x0) {
for (i = 0; i < WIDTH_A * HEIGHT_A; ++i) for (i = 0; i < WIDTH_A * HEIGHT_A; ++i)
{ {
noise = seed; noise = seed;
@ -164,6 +166,14 @@ main()
} }
//-- //--
state_a = mfb_update(window_a, g_buffer_a);
if (state_a != STATE_OK) {
window_a = 0x0;
}
}
//--
if(window_b != 0x0) {
time_x = sinf(time * kPI / 180.0f); time_x = sinf(time * kPI / 180.0f);
time_y = cosf(time * kPI / 180.0f); time_y = cosf(time * kPI / 180.0f);
i = 0; i = 0;
@ -178,27 +188,28 @@ main()
} }
time += 0.1f; time += 0.1f;
//--
state_a = mfb_update(window_a, g_buffer_a);
if (state_a != STATE_OK) {
window_a = 0x0;
}
//-- //--
state_b = mfb_update(window_b, g_buffer_b); state_b = mfb_update(window_b, g_buffer_b);
if (state_b != STATE_OK) { if (state_b != STATE_OK) {
window_b = 0x0; window_b = 0x0;
} }
}
if(window_a == 0x0 && window_b == 0x0) { if(window_a == 0x0 && window_b == 0x0) {
break; break;
} }
// Don't need to do this for both windows in the same thread // Don't need to do this for both windows in the same thread
if(window_a != 0x0) if(window_a != 0x0) {
mfb_wait_sync(window_a); if(mfb_wait_sync(window_a) == false) {
else if(window_b != 0x0) window_a = 0x0;
mfb_wait_sync(window_b); }
}
else if(window_b != 0x0) {
if(mfb_wait_sync(window_b) == false) {
window_b = 0x0;
}
}
} }
return 0; return 0;