oc_render uses currently selected surface

This commit is contained in:
Martin Fouilleul 2023-09-10 15:21:11 +02:00
parent b223abcd5b
commit e6a3ac7694
19 changed files with 55 additions and 42 deletions

View File

@ -65,7 +65,7 @@ oc_canvas_select(canvas); // make the canvas current
//... add commands to the canvas command buffer using drawing functions //... add commands to the canvas command buffer using drawing functions
oc_surface_select(surface); // select the canvas surface oc_surface_select(surface); // select the canvas surface
oc_render(surface, canvas); // render the canvas commands into it oc_render(canvas); // render the canvas commands into it
oc_surface_present(surface); // display the result oc_surface_present(surface); // display the result
``` ```

View File

@ -16,8 +16,8 @@ oc_surface oc_surface_gles();
void oc_surface_destroy(oc_surface surface); void oc_surface_destroy(oc_surface surface);
void oc_surface_select(oc_surface surface); void oc_surface_select(oc_surface surface);
void oc_surface_present(oc_surface surface);
void oc_surface_deselect(void); void oc_surface_deselect(void);
void oc_surface_present(oc_surface surface);
oc_vec2 oc_surface_get_size(oc_surface surface); oc_vec2 oc_surface_get_size(oc_surface surface);
oc_vec2 oc_surface_contents_scaling(oc_surface surface); oc_vec2 oc_surface_contents_scaling(oc_surface surface);
@ -32,7 +32,7 @@ bool oc_canvas_is_nil(oc_canvas canvas);
oc_canvas oc_canvas_create(void); oc_canvas oc_canvas_create(void);
void oc_canvas_destroy(oc_canvas canvas); void oc_canvas_destroy(oc_canvas canvas);
oc_canvas oc_canvas_set_current(oc_canvas canvas); oc_canvas oc_canvas_set_current(oc_canvas canvas);
void oc_render(oc_surface surface, oc_canvas canvas); void oc_render(oc_canvas canvas);
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
// transform and clipping // transform and clipping

View File

@ -465,7 +465,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
oc_matrix_pop(); oc_matrix_pop();
oc_surface_select(surface); oc_surface_select(surface);
oc_render(surface, canvas); oc_render(canvas);
oc_surface_present(surface); oc_surface_present(surface);
oc_arena_clear(oc_scratch()); oc_arena_clear(oc_scratch());

View File

@ -133,6 +133,6 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
oc_circle_fill(centerX, centerY, 10 * uiScale); oc_circle_fill(centerX, centerY, 10 * uiScale);
oc_surface_select(surface); oc_surface_select(surface);
oc_render(surface, canvas); oc_render(canvas);
oc_surface_present(surface); oc_surface_present(surface);
} }

View File

@ -355,7 +355,7 @@ ORCA_EXPORT void oc_on_frame_refresh(void)
oc_canvas_select(canvas); oc_canvas_select(canvas);
oc_surface_select(surface); oc_surface_select(surface);
oc_ui_draw(); oc_ui_draw();
oc_render(surface, canvas); oc_render(canvas);
oc_surface_present(surface); oc_surface_present(surface);
oc_arena_clear(oc_scratch()); oc_arena_clear(oc_scratch());

View File

@ -87,7 +87,7 @@ int main()
oc_image_draw_region(image1.image, image1.rect, (oc_rect){ 100, 100, 300, 300 }); oc_image_draw_region(image1.image, image1.rect, (oc_rect){ 100, 100, 300, 300 });
oc_image_draw_region(image2.image, image2.rect, (oc_rect){ 300, 200, 300, 300 }); oc_image_draw_region(image2.image, image2.rect, (oc_rect){ 300, 200, 300, 300 });
oc_render(surface, canvas); oc_render(canvas);
oc_surface_present(surface); oc_surface_present(surface);
oc_arena_clear(oc_scratch()); oc_arena_clear(oc_scratch());

View File

@ -180,7 +180,7 @@ int main()
frameTime, frameTime,
1. / frameTime); 1. / frameTime);
oc_render(surface, canvas); oc_render(canvas);
oc_surface_present(surface); oc_surface_present(surface);
oc_arena_clear(oc_scratch()); oc_arena_clear(oc_scratch());

View File

@ -102,7 +102,7 @@ int main()
oc_image_draw(image, (oc_rect){ 100, 100, 300, 300 }); oc_image_draw(image, (oc_rect){ 100, 100, 300, 300 });
oc_image_draw(image2, (oc_rect){ 300, 200, 300, 300 }); oc_image_draw(image2, (oc_rect){ 300, 200, 300, 300 });
oc_render(surface, canvas); oc_render(canvas);
oc_surface_present(surface); oc_surface_present(surface);
oc_arena_clear(oc_scratch()); oc_arena_clear(oc_scratch());

View File

@ -95,7 +95,7 @@ int main()
oc_set_color_rgba(1, 0, 0, 1); oc_set_color_rgba(1, 0, 0, 1);
oc_rectangle_fill(100, 100, 300, 150); oc_rectangle_fill(100, 100, 300, 150);
oc_render(surface1, canvas1); oc_render(canvas1);
//* //*
oc_surface_select(surface2); oc_surface_select(surface2);
@ -107,7 +107,7 @@ int main()
oc_set_color_rgba(0, 0, 1, 1); oc_set_color_rgba(0, 0, 1, 1);
oc_rectangle_fill(200, 200, 300, 200); oc_rectangle_fill(200, 200, 300, 200);
oc_render(surface2, canvas2); oc_render(canvas2);
//*/ //*/
oc_surface_present(surface1); oc_surface_present(surface1);

View File

@ -298,7 +298,7 @@ int main()
f64 startFlushTime = oc_clock_time(OC_CLOCK_MONOTONIC); f64 startFlushTime = oc_clock_time(OC_CLOCK_MONOTONIC);
oc_surface_select(surface); oc_surface_select(surface);
oc_render(surface, canvas); oc_render(canvas);
f64 startPresentTime = oc_clock_time(OC_CLOCK_MONOTONIC); f64 startPresentTime = oc_clock_time(OC_CLOCK_MONOTONIC);
oc_surface_present(surface); oc_surface_present(surface);

View File

@ -36,7 +36,7 @@ i32 render_thread(void* user)
oc_set_color_rgba(1, 0, 0, 1); oc_set_color_rgba(1, 0, 0, 1);
oc_rectangle_fill(100, 100, 300, 150); oc_rectangle_fill(100, 100, 300, 150);
oc_render(surface, canvas); oc_render(canvas);
oc_surface_present(surface); oc_surface_present(surface);

View File

@ -198,7 +198,7 @@ int main()
1. / frameTime); 1. / frameTime);
oc_surface_select(surface); oc_surface_select(surface);
oc_render(surface, canvas); oc_render(canvas);
oc_surface_present(surface); oc_surface_present(surface);
oc_arena_clear(oc_scratch()); oc_arena_clear(oc_scratch());

View File

@ -233,7 +233,7 @@ int main()
frameTime, frameTime,
1. / frameTime); 1. / frameTime);
oc_render(surface, canvas); oc_render(canvas);
oc_surface_present(surface); oc_surface_present(surface);
oc_input_next_frame(&inputState); oc_input_next_frame(&inputState);

View File

@ -632,7 +632,7 @@ int main()
oc_ui_draw(); oc_ui_draw();
oc_render(surface, canvas); oc_render(canvas);
oc_surface_present(surface); oc_surface_present(surface);
oc_arena_clear(scratch); oc_arena_clear(scratch);

View File

@ -116,8 +116,10 @@ ORCA_API oc_surface oc_surface_gles(); //DOC: create a surface for use with GL
ORCA_API void oc_surface_destroy(oc_surface surface); //DOC: destroys the surface ORCA_API void oc_surface_destroy(oc_surface surface); //DOC: destroys the surface
ORCA_API void oc_surface_select(oc_surface surface); //DOC: selects the surface in the current thread before drawing ORCA_API void oc_surface_select(oc_surface surface); //DOC: selects the surface in the current thread before drawing
ORCA_API void oc_surface_present(oc_surface surface); //DOC: presents the surface to its window
ORCA_API void oc_surface_deselect(void); //DOC: deselects the current thread's previously selected surface ORCA_API void oc_surface_deselect(void); //DOC: deselects the current thread's previously selected surface
ORCA_API oc_surface oc_surface_get_selected();
ORCA_API void oc_surface_present(oc_surface surface); //DOC: presents the surface to its window
ORCA_API oc_vec2 oc_surface_get_size(oc_surface surface); ORCA_API oc_vec2 oc_surface_get_size(oc_surface surface);
ORCA_API oc_vec2 oc_surface_contents_scaling(oc_surface surface); //DOC: returns the scaling of the surface (pixels = points * scale) ORCA_API oc_vec2 oc_surface_contents_scaling(oc_surface surface); //DOC: returns the scaling of the surface (pixels = points * scale)
@ -214,7 +216,7 @@ ORCA_API bool oc_canvas_is_nil(oc_canvas canvas); //DOC: true if canvas is nil
ORCA_API oc_canvas oc_canvas_create(void); //DOC: create a new canvas ORCA_API oc_canvas oc_canvas_create(void); //DOC: create a new canvas
ORCA_API void oc_canvas_destroy(oc_canvas canvas); //DOC: destroys canvas ORCA_API void oc_canvas_destroy(oc_canvas canvas); //DOC: destroys canvas
ORCA_API oc_canvas oc_canvas_select(oc_canvas canvas); //DOC: selects canvas in the current thread ORCA_API oc_canvas oc_canvas_select(oc_canvas canvas); //DOC: selects canvas in the current thread
ORCA_API void oc_render(oc_surface surface, oc_canvas canvas); //DOC: renders all canvas commands onto surface ORCA_API void oc_render(oc_canvas canvas); //DOC: renders all canvas commands onto surface
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
//SECTION: fonts //SECTION: fonts

View File

@ -219,6 +219,14 @@ void* oc_graphics_data_from_handle(oc_graphics_handle_kind kind, u64 h)
return (data); return (data);
} }
//------------------------------------------------------------------------------------------
//NOTE(martin): surface nil and is nil
//------------------------------------------------------------------------------------------
oc_surface oc_surface_nil() { return ((oc_surface){ .h = 0 }); }
bool oc_surface_is_nil(oc_surface surface) { return (surface.h == 0); }
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
//NOTE(martin): graphics canvas internal //NOTE(martin): graphics canvas internal
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
@ -926,13 +934,14 @@ oc_canvas oc_canvas_select(oc_canvas canvas)
return (old); return (old);
} }
void oc_render(oc_surface surface, oc_canvas canvas) void oc_render(oc_canvas canvas)
{ {
oc_surface selectedSurface = oc_surface_get_selected();
oc_canvas_data* canvasData = oc_canvas_data_from_handle(canvas); oc_canvas_data* canvasData = oc_canvas_data_from_handle(canvas);
if(canvasData) if(canvasData && !oc_surface_is_nil(selectedSurface))
{ {
int eltCount = canvasData->path.startIndex + canvasData->path.count; int eltCount = canvasData->path.startIndex + canvasData->path.count;
oc_surface_render_commands(surface, oc_surface_render_commands(selectedSurface,
canvasData->clearColor, canvasData->clearColor,
canvasData->primitiveCount, canvasData->primitiveCount,
canvasData->primitives, canvasData->primitives,

View File

@ -14,6 +14,11 @@
oc_thread_local oc_surface oc_selectedSurface = { 0 }; oc_thread_local oc_surface oc_selectedSurface = { 0 };
oc_surface oc_surface_get_selected()
{
return (oc_selectedSurface);
}
//--------------------------------------------------------------- //---------------------------------------------------------------
// typed handles functions // typed handles functions
//--------------------------------------------------------------- //---------------------------------------------------------------
@ -98,10 +103,6 @@ bool oc_is_surface_backend_available(oc_surface_api api)
return (result); return (result);
} }
oc_surface oc_surface_nil() { return ((oc_surface){ .h = 0 }); }
bool oc_surface_is_nil(oc_surface surface) { return (surface.h == 0); }
oc_surface oc_surface_create_for_window(oc_window window, oc_surface_api api) oc_surface oc_surface_create_for_window(oc_window window, oc_surface_api api)
{ {
if(oc_graphicsData.init) if(oc_graphicsData.init)

View File

@ -836,7 +836,7 @@ i32 orca_runloop(void* user)
oc_clear(); oc_clear();
} }
oc_render(app->debugOverlay.surface, app->debugOverlay.canvas); oc_render(app->debugOverlay.canvas);
oc_surface_present(app->debugOverlay.surface); oc_surface_present(app->debugOverlay.surface);
oc_arena_clear(oc_scratch()); oc_arena_clear(oc_scratch());
@ -893,17 +893,6 @@ int main(int argc, char** argv)
oc_surface_deselect(); oc_surface_deselect();
//WARN: this is a workaround to avoid stalling the first few times we acquire drawables from
// the surfaces... This should probably be fixed in the implementation of mtl_surface!
for(int i = 0; i < 3; i++)
{
oc_surface_select(app->debugOverlay.surface);
oc_canvas_select(app->debugOverlay.canvas);
oc_render(app->debugOverlay.surface, app->debugOverlay.canvas);
oc_surface_present(app->debugOverlay.surface);
}
oc_ui_init(&app->debugOverlay.ui); oc_ui_init(&app->debugOverlay.ui);
//NOTE: show window and start runloop //NOTE: show window and start runloop

View File

@ -77,6 +77,18 @@
{"name": "surface", {"name": "surface",
"type": {"name": "oc_surface", "tag": "S"}}] "type": {"name": "oc_surface", "tag": "S"}}]
}, },
{
"name": "oc_surface_deselect",
"cname": "oc_surface_deselect",
"ret": {"name": "void", "tag": "v"},
"args": []
},
{
"name": "oc_surface_get_selected",
"cname": "oc_surface_get_selected",
"ret": {"name": "oc_surface", "tag": "S"},
"args": []
},
{ {
"name": "oc_surface_present", "name": "oc_surface_present",
"cname": "oc_surface_present", "cname": "oc_surface_present",