orca/sketches/render_thread/main.c

100 lines
2.1 KiB
C
Raw Normal View History

2023-08-19 12:49:23 +00:00
#include <stdio.h>
#include <stdlib.h>
#define MG_INCLUDE_GL_API 1
2023-08-19 12:49:23 +00:00
#include "milepost.h"
2023-08-19 12:49:23 +00:00
mg_surface surface = { 0 };
mg_canvas canvas = { 0 };
i32 render_thread(void* user)
{
2023-08-19 12:49:23 +00:00
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()
{
2023-08-19 12:49:23 +00:00
mp_init();
mp_clock_init(); //TODO put that in mp_init()?
2023-08-19 12:49:23 +00:00
mp_rect windowRect = { .x = 100, .y = 100, .w = 810, .h = 610 };
mp_window window = mp_window_create(windowRect, "test", 0);
2023-08-19 12:49:23 +00:00
mp_rect contentRect = mp_window_get_content_rect(window);
2023-08-19 12:49:23 +00:00
//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);
2023-08-19 12:49:23 +00:00
canvas = mg_canvas_create();
if(mg_canvas_is_nil(canvas))
{
printf("Error: couldn't create canvas 1\n");
return (-1);
}
2023-08-19 12:49:23 +00:00
mg_surface dummy = mg_surface_create_for_window(window, MG_CANVAS);
2023-08-19 12:49:23 +00:00
// start app
mp_window_center(window);
mp_window_bring_to_front(window);
mp_window_focus(window);
2023-08-19 12:49:23 +00:00
mp_thread* renderThread = mp_thread_create(render_thread, NULL);
2023-08-19 12:49:23 +00:00
while(!mp_should_quit())
{
mp_pump_events(0);
}
2023-08-19 12:49:23 +00:00
mp_thread_join(renderThread, NULL);
2023-08-19 12:49:23 +00:00
mg_canvas_destroy(canvas);
mg_surface_destroy(surface);
2023-08-19 12:49:23 +00:00
mp_window_destroy(window);
2023-08-19 12:49:23 +00:00
mp_terminate();
2023-08-19 12:49:23 +00:00
return (0);
}