Expose API to control surfaces z-order #73

Merged
MartinFouilleul merged 3 commits from surface_order into main 2023-08-28 15:47:20 +00:00
10 changed files with 267 additions and 95 deletions

View File

@ -3,4 +3,4 @@ set INCLUDES=/I ..\..\src /I ..\..\src\util /I ..\..\src\platform /I ../../ext /
if not exist "bin" mkdir bin
cl /we4013 /Zi /Zc:preprocessor /std:c11 /experimental:c11atomics %INCLUDES% main.c /link /LIBPATH:../../build/bin orca.dll.lib /out:bin/example_multi_surface.exe
cp ../../build/bin/orca.dll bin/
copy ..\..\build\bin\orca.dll bin

View File

@ -65,6 +65,22 @@ int main()
}
break;
case OC_EVENT_KEYBOARD_KEY:
{
if(event->key.action == OC_KEY_PRESS)
{
if(event->key.code == OC_KEY_UP)
{
oc_surface_bring_to_front(surface2);
}
else if(event->key.code == OC_KEY_DOWN)
{
oc_surface_send_to_back(surface2);
}
}
}
break;
default:
break;
}
@ -89,7 +105,7 @@ int main()
oc_clear();
oc_set_color_rgba(0, 0, 1, 1);
oc_rectangle_fill(300, 300, 300, 200);
oc_rectangle_fill(200, 200, 300, 200);
oc_render(surface2, canvas2);
//*/

View File

@ -32,6 +32,8 @@ typedef struct oc_osx_window_data
NSView* nsView;
NSObject* nsWindowDelegate;
oc_list layers;
} oc_osx_window_data;
#define OC_PLATFORM_WINDOW_DATA oc_osx_window_data osx;
@ -78,6 +80,9 @@ typedef uint32_t CAContextID;
typedef struct oc_layer
{
oc_window window;
oc_list_elt listElt;
CALayer* caLayer;
CAContext* caContext;
} oc_layer;

View File

@ -1311,10 +1311,10 @@ oc_window oc_window_create(oc_rect contentRect, oc_str8 title, oc_window_style s
oc_log_error("Could not allocate window data\n");
return ((oc_window){ 0 });
}
window->style = style;
window->shouldClose = false;
window->hidden = true;
window->osx.layers = (oc_list){ 0 };
u32 styleMask = oc_osx_get_window_style_mask(style);
@ -1735,10 +1735,79 @@ void oc_osx_surface_set_hidden(oc_surface_data* surface, bool hidden)
}
}
void oc_osx_update_layers(oc_window_data* window)
{
@autoreleasepool
{
int z = 0;
oc_list_for(&window->osx.layers, layer, oc_layer, listElt)
{
layer->caLayer.zPosition = (CGFloat)z;
z++;
}
}
}
void oc_osx_surface_bring_to_front(oc_surface_data* surface)
{
dispatch_block_t block = ^{
@autoreleasepool
{
oc_window_data* window = oc_window_ptr_from_handle(surface->layer.window);
if(window)
{
oc_list_remove(&window->osx.layers, &surface->layer.listElt);
oc_list_push_back(&window->osx.layers, &surface->layer.listElt);
oc_osx_update_layers(window);
}
}
};
if([NSThread isMainThread])
{
block();
}
else
{
dispatch_sync(dispatch_get_main_queue(), block);
}
}
void oc_osx_surface_send_to_back(oc_surface_data* surface)
{
dispatch_block_t block = ^{
@autoreleasepool
{
oc_window_data* window = oc_window_ptr_from_handle(surface->layer.window);
if(window)
{
oc_list_remove(&window->osx.layers, &surface->layer.listElt);
oc_list_push(&window->osx.layers, &surface->layer.listElt);
oc_osx_update_layers(window);
}
}
};
if([NSThread isMainThread])
{
block();
}
else
{
dispatch_sync(dispatch_get_main_queue(), block);
}
}
void oc_surface_cleanup(oc_surface_data* surface)
{
@autoreleasepool
{
oc_window_data* window = oc_window_ptr_from_handle(surface->layer.window);
if(window)
{
oc_list_remove(&window->osx.layers, &surface->layer.listElt);
oc_osx_update_layers(window);
}
[surface->layer.caLayer release];
}
}
@ -1747,12 +1816,17 @@ void oc_surface_init_for_window(oc_surface_data* surface, oc_window_data* window
{
@autoreleasepool
{
surface->layer.window = oc_window_handle_from_ptr(window);
surface->nativeLayer = oc_osx_surface_native_layer;
surface->contentsScaling = oc_osx_surface_contents_scaling;
surface->getSize = oc_osx_surface_get_size;
surface->getHidden = oc_osx_surface_get_hidden;
surface->setHidden = oc_osx_surface_set_hidden;
surface->bringToFront = oc_osx_surface_bring_to_front;
surface->sendToBack = oc_osx_surface_send_to_back;
surface->layer.caLayer = [[CALayer alloc] init];
[surface->layer.caLayer retain];
@ -1760,10 +1834,12 @@ void oc_surface_init_for_window(oc_surface_data* surface, oc_window_data* window
CGSize size = frame.size;
surface->layer.caLayer.frame = (CGRect){ { 0, 0 }, size };
surface->layer.caLayer.contentsScale = window->osx.nsView.layer.contentsScale;
surface->layer.caLayer.autoresizingMask = kCALayerWidthSizable | kCALayerHeightSizable;
[window->osx.nsView.layer addSublayer:surface->layer.caLayer];
oc_list_push_back(&window->osx.layers, &surface->layer.listElt);
oc_osx_update_layers(window);
}
}

View File

@ -266,20 +266,35 @@ static void oc_win32_update_child_layers(oc_window_data* window)
int clientWidth = (clientRect.right - clientRect.left);
int clientHeight = (clientRect.bottom - clientRect.top);
oc_list_for(&window->win32.layers, layer, oc_layer, listElt)
{
SetWindowPos(layer->hWnd,
0,
point.x,
point.y,
clientWidth,
clientHeight,
SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER);
}
}
static void oc_win32_update_child_layers_zorder(oc_window_data* window)
{
HWND insertAfter = window->win32.hWnd;
oc_list_for(&window->win32.layers, layer, oc_layer, listElt)
{
SetWindowPos(layer->hWnd,
insertAfter,
point.x,
point.y,
clientWidth,
clientHeight,
SWP_NOACTIVATE | SWP_NOOWNERZORDER | SWP_NOZORDER);
0, 0, 0, 0,
SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOREDRAW | SWP_NOOWNERZORDER);
insertAfter = layer->hWnd;
}
SetWindowPos(window->win32.hWnd,
insertAfter,
0, 0, 0, 0,
SWP_NOACTIVATE | SWP_NOMOVE | SWP_NOSIZE | SWP_NOREDRAW);
}
LRESULT oc_win32_win_proc(HWND windowHandle, UINT message, WPARAM wParam, LPARAM lParam)
@ -1172,6 +1187,20 @@ void oc_win32_surface_set_hidden(oc_surface_data* surface, bool hidden)
ShowWindow(surface->layer.hWnd, hidden ? SW_HIDE : SW_NORMAL);
}
void oc_win32_surface_bring_to_front(oc_surface_data* surface)
{
oc_list_remove(&surface->layer.parent->win32.layers, &surface->layer.listElt);
oc_list_push(&surface->layer.parent->win32.layers, &surface->layer.listElt);
oc_win32_update_child_layers_zorder(surface->layer.parent);
}
void oc_win32_surface_send_to_back(oc_surface_data* surface)
{
oc_list_remove(&surface->layer.parent->win32.layers, &surface->layer.listElt);
oc_list_push_back(&surface->layer.parent->win32.layers, &surface->layer.listElt);
oc_win32_update_child_layers_zorder(surface->layer.parent);
}
void* oc_win32_surface_native_layer(oc_surface_data* surface)
{
return ((void*)surface->layer.hWnd);
@ -1227,6 +1256,8 @@ void oc_surface_init_for_window(oc_surface_data* surface, oc_window_data* window
surface->getHidden = oc_win32_surface_get_hidden;
surface->setHidden = oc_win32_surface_set_hidden;
surface->nativeLayer = oc_win32_surface_native_layer;
surface->bringToFront = oc_win32_surface_bring_to_front;
surface->sendToBack = oc_win32_surface_send_to_back;
//NOTE(martin): create a child window for the surface
WNDCLASS layerWindowClass = { .style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC,

View File

@ -115,6 +115,9 @@ ORCA_API oc_vec2 oc_surface_contents_scaling(oc_surface surface);
ORCA_API bool oc_surface_get_hidden(oc_surface surface);
ORCA_API void oc_surface_set_hidden(oc_surface surface, bool hidden);
ORCA_API void oc_surface_bring_to_front(oc_surface surface);
ORCA_API void oc_surface_send_to_back(oc_surface surface);
//NOTE(martin): surface sharing
typedef u64 oc_surface_id;

View File

@ -372,6 +372,24 @@ void oc_surface_render_commands(oc_surface surface,
}
}
void oc_surface_bring_to_front(oc_surface handle)
{
oc_surface_data* surface = oc_surface_data_from_handle(handle);
if(surface && surface->bringToFront)
{
surface->bringToFront(surface);
}
}
void oc_surface_send_to_back(oc_surface handle)
{
oc_surface_data* surface = oc_surface_data_from_handle(handle);
if(surface && surface->sendToBack)
{
surface->sendToBack(surface);
}
}
//------------------------------------------------------------------------------------------
//NOTE(martin): images
//------------------------------------------------------------------------------------------

View File

@ -12,99 +12,104 @@
#include "graphics_common.h"
#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif
//---------------------------------------------------------------
// surface interface
//---------------------------------------------------------------
typedef struct oc_surface_data oc_surface_data;
typedef struct oc_canvas_backend oc_canvas_backend;
//---------------------------------------------------------------
// surface interface
//---------------------------------------------------------------
typedef struct oc_surface_data oc_surface_data;
typedef struct oc_canvas_backend oc_canvas_backend;
typedef void (*oc_surface_destroy_proc)(oc_surface_data* surface);
typedef void (*oc_surface_select_proc)(oc_surface_data* surface);
typedef void (*oc_surface_deselect_proc)(oc_surface_data* surface);
typedef void (*oc_surface_present_proc)(oc_surface_data* surface);
typedef void (*oc_surface_swap_interval_proc)(oc_surface_data* surface, int swap);
typedef oc_vec2 (*oc_surface_get_size_proc)(oc_surface_data* surface);
typedef oc_vec2 (*oc_surface_contents_scaling_proc)(oc_surface_data* surface);
typedef bool (*oc_surface_get_hidden_proc)(oc_surface_data* surface);
typedef void (*oc_surface_set_hidden_proc)(oc_surface_data* surface, bool hidden);
typedef void* (*oc_surface_native_layer_proc)(oc_surface_data* surface);
typedef oc_surface_id (*oc_surface_remote_id_proc)(oc_surface_data* surface);
typedef void (*oc_surface_host_connect_proc)(oc_surface_data* surface, oc_surface_id remoteId);
typedef void (*oc_surface_destroy_proc)(oc_surface_data* surface);
typedef void (*oc_surface_select_proc)(oc_surface_data* surface);
typedef void (*oc_surface_deselect_proc)(oc_surface_data* surface);
typedef void (*oc_surface_present_proc)(oc_surface_data* surface);
typedef void (*oc_surface_swap_interval_proc)(oc_surface_data* surface, int swap);
typedef oc_vec2 (*oc_surface_get_size_proc)(oc_surface_data* surface);
typedef oc_vec2 (*oc_surface_contents_scaling_proc)(oc_surface_data* surface);
typedef bool (*oc_surface_get_hidden_proc)(oc_surface_data* surface);
typedef void (*oc_surface_set_hidden_proc)(oc_surface_data* surface, bool hidden);
typedef void* (*oc_surface_native_layer_proc)(oc_surface_data* surface);
typedef oc_surface_id (*oc_surface_remote_id_proc)(oc_surface_data* surface);
typedef void (*oc_surface_host_connect_proc)(oc_surface_data* surface, oc_surface_id remoteId);
typedef struct oc_surface_data
{
oc_surface_api api;
oc_layer layer;
typedef void (*oc_surface_bring_to_front_proc)(oc_surface_data* surface);
typedef void (*oc_surface_send_to_back_proc)(oc_surface_data* surface);
oc_surface_destroy_proc destroy;
oc_surface_select_proc prepare;
oc_surface_present_proc present;
oc_surface_deselect_proc deselect;
oc_surface_swap_interval_proc swapInterval;
oc_surface_get_size_proc getSize;
oc_surface_contents_scaling_proc contentsScaling;
oc_surface_get_hidden_proc getHidden;
oc_surface_set_hidden_proc setHidden;
oc_surface_native_layer_proc nativeLayer;
oc_surface_remote_id_proc remoteID;
oc_surface_host_connect_proc hostConnect;
typedef struct oc_surface_data
{
oc_surface_api api;
oc_layer layer;
oc_canvas_backend* backend;
oc_surface_destroy_proc destroy;
oc_surface_select_proc prepare;
oc_surface_present_proc present;
oc_surface_deselect_proc deselect;
oc_surface_swap_interval_proc swapInterval;
oc_surface_get_size_proc getSize;
oc_surface_contents_scaling_proc contentsScaling;
oc_surface_get_hidden_proc getHidden;
oc_surface_set_hidden_proc setHidden;
oc_surface_native_layer_proc nativeLayer;
oc_surface_remote_id_proc remoteID;
oc_surface_host_connect_proc hostConnect;
} oc_surface_data;
oc_surface_bring_to_front_proc bringToFront;
oc_surface_send_to_back_proc sendToBack;
oc_surface oc_surface_handle_alloc(oc_surface_data* surface);
oc_surface_data* oc_surface_data_from_handle(oc_surface handle);
oc_canvas_backend* backend;
void oc_surface_init_for_window(oc_surface_data* surface, oc_window_data* window);
void oc_surface_init_remote(oc_surface_data* surface, u32 width, u32 height);
void oc_surface_init_host(oc_surface_data* surface, oc_window_data* window);
void oc_surface_cleanup(oc_surface_data* surface);
void* oc_surface_native_layer(oc_surface surface);
} oc_surface_data;
//---------------------------------------------------------------
// canvas backend interface
//---------------------------------------------------------------
typedef struct oc_image_data
{
oc_list_elt listElt;
u32 generation;
oc_surface surface;
oc_vec2 size;
oc_surface oc_surface_handle_alloc(oc_surface_data* surface);
oc_surface_data* oc_surface_data_from_handle(oc_surface handle);
} oc_image_data;
void oc_surface_init_for_window(oc_surface_data* surface, oc_window_data* window);
void oc_surface_init_remote(oc_surface_data* surface, u32 width, u32 height);
void oc_surface_init_host(oc_surface_data* surface, oc_window_data* window);
void oc_surface_cleanup(oc_surface_data* surface);
void* oc_surface_native_layer(oc_surface surface);
typedef void (*oc_canvas_backend_destroy_proc)(oc_canvas_backend* backend);
//---------------------------------------------------------------
// canvas backend interface
//---------------------------------------------------------------
typedef struct oc_image_data
{
oc_list_elt listElt;
u32 generation;
oc_surface surface;
oc_vec2 size;
typedef oc_image_data* (*oc_canvas_backend_image_create_proc)(oc_canvas_backend* backend, oc_vec2 size);
typedef void (*oc_canvas_backend_image_destroy_proc)(oc_canvas_backend* backend, oc_image_data* image);
typedef void (*oc_canvas_backend_image_upload_region_proc)(oc_canvas_backend* backend,
oc_image_data* image,
oc_rect region,
u8* pixels);
} oc_image_data;
typedef void (*oc_canvas_backend_render_proc)(oc_canvas_backend* backend,
oc_color clearColor,
u32 primitiveCount,
oc_primitive* primitives,
u32 eltCount,
oc_path_elt* pathElements);
typedef void (*oc_canvas_backend_destroy_proc)(oc_canvas_backend* backend);
typedef struct oc_canvas_backend
{
oc_canvas_backend_destroy_proc destroy;
typedef oc_image_data* (*oc_canvas_backend_image_create_proc)(oc_canvas_backend* backend, oc_vec2 size);
typedef void (*oc_canvas_backend_image_destroy_proc)(oc_canvas_backend* backend, oc_image_data* image);
typedef void (*oc_canvas_backend_image_upload_region_proc)(oc_canvas_backend* backend,
oc_image_data* image,
oc_rect region,
u8* pixels);
oc_canvas_backend_image_create_proc imageCreate;
oc_canvas_backend_image_destroy_proc imageDestroy;
oc_canvas_backend_image_upload_region_proc imageUploadRegion;
typedef void (*oc_canvas_backend_render_proc)(oc_canvas_backend* backend,
oc_color clearColor,
u32 primitiveCount,
oc_primitive* primitives,
u32 eltCount,
oc_path_elt* pathElements);
oc_canvas_backend_render_proc render;
typedef struct oc_canvas_backend
{
oc_canvas_backend_destroy_proc destroy;
} oc_canvas_backend;
oc_canvas_backend_image_create_proc imageCreate;
oc_canvas_backend_image_destroy_proc imageDestroy;
oc_canvas_backend_image_upload_region_proc imageUploadRegion;
oc_canvas_backend_render_proc render;
} oc_canvas_backend;
#ifdef __cplusplus
} // extern "C"

View File

@ -636,11 +636,11 @@ i32 orca_runloop(void* user)
{
if(event->key.action == OC_KEY_PRESS)
{
if(event->key.code == OC_KEY_D && (event->key.mods & (OC_KEYMOD_SHIFT | OC_KEYMOD_CMD)))
if(event->key.code == OC_KEY_D
&& (event->key.mods & OC_KEYMOD_SHIFT)
&& (event->key.mods & OC_KEYMOD_MAIN_MODIFIER))
{
#if 1 // EPILEPSY WARNING! on windows this has a bug which causes a pretty strong stroboscopic effect
debug_overlay_toggle(&app->debugOverlay);
#endif
}
if(exports[OC_EXPORT_KEY_DOWN])
@ -675,6 +675,8 @@ i32 orca_runloop(void* user)
if(app->debugOverlay.show)
{
oc_surface_bring_to_front(app->debugOverlay.surface);
oc_ui_style debugUIDefaultStyle = { .bgColor = { 0 },
.color = { 1, 1, 1, 1 },
.font = app->debugOverlay.fontReg,

View File

@ -36,6 +36,30 @@
{"name": "pixels",
"type": {"name": "u8*", "tag": "p"}}]
},
{
"name": "oc_surface_contents_scaling",
"cname": "oc_surface_contents_scaling",
"ret": {"name": "oc_vec2", "tag": "S"},
"args": [
{"name": "surface",
"type": {"name": "oc_surface", "tag": "S"}}]
},
{
"name": "oc_surface_bring_to_front",
"cname": "oc_surface_bring_to_front",
"ret": {"name": "void", "tag": "v"},
"args": [
{"name": "surface",
"type": {"name": "oc_surface", "tag": "S"}}]
},
{
"name": "oc_surface_send_to_back",
"cname": "oc_surface_send_to_back",
"ret": {"name": "void", "tag": "v"},
"args": [
{"name": "surface",
"type": {"name": "oc_surface", "tag": "S"}}]
},
{
"name": "oc_surface_select",
"cname": "oc_surface_select",
@ -52,14 +76,6 @@
{"name": "surface",
"type": {"name": "oc_surface", "tag": "S"}}]
},
{
"name": "oc_surface_contents_scaling",
"cname": "oc_surface_contents_scaling",
"ret": {"name": "oc_vec2", "tag": "S"},
"args": [
{"name": "surface",
"type": {"name": "oc_surface", "tag": "S"}}]
},
{
"name": "oc_surface_render_commands",
"cname": "orca_surface_render_commands",