macos working Metal & Cocoa

This commit is contained in:
Carlos Aragones 2020-09-16 12:27:14 +02:00
parent 82595a8d74
commit 3a083fd3c8
2 changed files with 26 additions and 8 deletions

View File

@ -40,8 +40,7 @@ create_window_data(unsigned width, unsigned height) {
window_data->specific = window_data_osx; window_data->specific = window_data_osx;
window_data->dst_width = width; calc_dst_factor(window_data, width, height);
window_data->dst_height = height;
window_data->buffer_width = width; window_data->buffer_width = width;
window_data->buffer_height = height; window_data->buffer_height = height;
@ -366,6 +365,7 @@ mfb_set_viewport(struct mfb_window *window, unsigned offset_x, unsigned offset_y
window_data->dst_offset_y = offset_y; window_data->dst_offset_y = offset_y;
window_data->dst_width = width; window_data->dst_width = width;
window_data->dst_height = height; window_data->dst_height = height;
calc_dst_factor(window_data, window_data->window_width, window_data->window_height);
#if defined(USE_METAL_API) #if defined(USE_METAL_API)
float x1 = ((float) offset_x / window_data->window_width) * 2.0f - 1.0f; float x1 = ((float) offset_x / window_data->window_width) * 2.0f - 1.0f;

View File

@ -52,13 +52,27 @@
if (!window_data_osx || !window_data_osx->window || !window_data->draw_buffer) if (!window_data_osx || !window_data_osx->window || !window_data->draw_buffer)
return; return;
CGContextRef context = [[NSGraphicsContext currentContext] graphicsPort]; CGContextRef context = [[NSGraphicsContext currentContext] CGContext];
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB(); CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGDataProviderRef provider = CGDataProviderCreateWithData(0x0, window_data->draw_buffer, window_data->buffer_width * window_data->buffer_height * 4, 0x0); CGDataProviderRef provider = CGDataProviderCreateWithData(0x0,
window_data->draw_buffer,
window_data->buffer_width * window_data->buffer_height * 4,
0x0
);
CGImageRef img = CGImageCreate(window_data->buffer_width, window_data->buffer_height, 8, 32, window_data->buffer_width * 4, space, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little, CGImageRef img = CGImageCreate(window_data->buffer_width
provider, 0x0, false, kCGRenderingIntentDefault); , window_data->buffer_height
, 8
, 32
, window_data->buffer_width * 4
, space
, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Little
, provider
, 0x0
, false
, kCGRenderingIntentDefault
);
const CGFloat components[] = {0.0f, 0.0f, 0.0f, 1.0f}; const CGFloat components[] = {0.0f, 0.0f, 0.0f, 1.0f};
const CGColorRef black = CGColorCreate(space, components); const CGColorRef black = CGColorCreate(space, components);
@ -68,10 +82,14 @@
if(window_data->dst_offset_x != 0 || window_data->dst_offset_y != 0 || window_data->dst_width != window_data->window_width || window_data->dst_height != window_data->window_height) { if(window_data->dst_offset_x != 0 || window_data->dst_offset_y != 0 || window_data->dst_width != window_data->window_width || window_data->dst_height != window_data->window_height) {
CGContextSetFillColorWithColor(context, black); CGContextSetFillColorWithColor(context, black);
CGContextFillRect(context, CGRectMake(0, 0, window_data->window_width, window_data->window_height)); CGContextFillRect(context, rect);
} }
CGContextDrawImage(context, CGRectMake(window_data->dst_offset_x, window_data->dst_offset_y, window_data->dst_width, window_data->dst_height), img); // TODO: Sometimes there is a crash here
CGContextDrawImage(context,
CGRectMake(window_data->dst_offset_x, window_data->dst_offset_y, window_data->dst_width, window_data->dst_height),
img
);
CGImageRelease(img); CGImageRelease(img);
} }