[win32, graphics] adding example to test rendering in a secondary thread

This commit is contained in:
martinfouilleul 2023-06-21 19:14:56 +02:00
parent 0366e3d724
commit 83f128fb55
4 changed files with 122 additions and 11 deletions

View File

@ -0,0 +1,7 @@
set pthread_dir=..\..\..\..\vcpkg\packages\pthreads_x64-windows
set INCLUDES=/I ..\..\src /I ..\..\src\util /I ..\..\src\platform /I ../../ext /I ../../ext/angle_headers /I %pthread_dir%\include
cl /we4013 /Zi /Zc:preprocessor /std:c11 %INCLUDES% main.c /link /MANIFEST:EMBED /MANIFESTINPUT:../../src/win32_manifest.xml /LIBPATH:../../bin milepost.dll.lib /LIBPATH:%pthread_dir%\lib pthreadVC3.lib /out:../../bin/example_render_thread.exe
copy %pthread_dir%\bin\pthreadVC3.dll ..\..\bin

View File

@ -0,0 +1,14 @@
#!/bin/bash
BINDIR=../../bin
RESDIR=../../resources
SRCDIR=../../src
EXTDIR=../../ext
INCLUDES="-I$SRCDIR -I$SRCDIR/util -I$SRCDIR/platform -I$SRCDIR/app -I$EXTDIR"
LIBS="-L$BINDIR -lmilepost"
FLAGS="-mmacos-version-min=10.15.4 -DDEBUG -DLOG_COMPILE_DEBUG"
clang -g $FLAGS $LIBS $INCLUDES -o $BINDIR/example_render_thread main.c
install_name_tool -add_rpath "@executable_path" $BINDIR/example_render_thread

View File

@ -0,0 +1,101 @@
#include<stdlib.h>
#include<stdio.h>
#include<pthread.h>
#define MG_INCLUDE_GL_API 1
#include"milepost.h"
mg_surface surface = {0};
mg_canvas canvas = {0};
void* render_thread(void* user)
{
while(!mp_should_quit())
{
mp_event* event = 0;
while((event = mp_next_event(mem_scratch())) != 0)
{
switch(event->type)
{
case MP_EVENT_WINDOW_CLOSE:
{
mp_request_quit();
} break;
default:
break;
}
}
mg_surface_prepare(surface);
mg_canvas_set_current(canvas);
mg_set_color_rgba(0, 0, 0.5, 0.5);
mg_clear();
mg_set_color_rgba(1, 0, 0, 1);
mg_rectangle_fill(100, 100, 300, 150);
mg_render(surface, canvas);
mg_surface_present(surface);
mem_arena_clear(mem_scratch());
}
return(0);
}
int main()
{
mp_init();
mp_clock_init(); //TODO put that in mp_init()?
mp_rect windowRect = {.x = 100, .y = 100, .w = 810, .h = 610};
mp_window window = mp_window_create(windowRect, "test", 0);
mp_rect contentRect = mp_window_get_content_rect(window);
//NOTE: create surface
surface = mg_surface_create_for_window(window, MG_CANVAS);
if(mg_surface_is_nil(surface))
{
printf("Error: couldn't create surface 1\n");
return(-1);
}
mg_surface_swap_interval(surface, 0);
canvas = mg_canvas_create();
if(mg_canvas_is_nil(canvas))
{
printf("Error: couldn't create canvas 1\n");
return(-1);
}
mg_surface dummy = mg_surface_create_for_window(window, MG_CANVAS);
// start app
mp_window_center(window);
mp_window_bring_to_front(window);
mp_window_focus(window);
pthread_t renderThread;
pthread_create(&renderThread, 0, render_thread, 0);
while(!mp_should_quit())
{
mp_pump_events(0);
}
void* res;
pthread_join(renderThread, &res);
mg_canvas_destroy(canvas);
mg_surface_destroy(surface);
mp_window_destroy(window);
mp_terminate();
return(0);
}

View File

@ -1957,9 +1957,6 @@ mg_canvas_backend* gl_canvas_backend_create(mg_wgl_surface* surface)
mg_gl_canvas_backend* backend = malloc_type(mg_gl_canvas_backend);
if(backend)
{
//////////////////////////////////////////////////////
//TODO
//////////////////////////////////////////////////////
memset(backend, 0, sizeof(mg_gl_canvas_backend));
backend->surface = surface;
@ -1970,14 +1967,6 @@ mg_canvas_backend* gl_canvas_backend_create(mg_wgl_surface* surface)
backend->interface.imageDestroy = mg_gl_canvas_image_destroy;
backend->interface.imageUploadRegion = mg_gl_canvas_image_upload_region;
/*
backend->interface.destroy = mg_gl_canvas_destroy;
backend->interface.begin = mg_gl_canvas_begin;
backend->interface.end = mg_gl_canvas_end;
backend->interface.clear = mg_gl_canvas_clear;
backend->interface.drawBatch = mg_gl_canvas_draw_batch;
*/
surface->interface.prepare((mg_surface_data*)surface);
glGenVertexArrays(1, &backend->vao);