example: synchronize the canvas before the window pops up.

This ensures that the window pops up with the appropriate content,
since windows can be popped up asynchronously with mainloop.
This commit is contained in:
Hermet Park 2024-06-25 21:36:47 +09:00 committed by Mira Grudzinska
parent 7145c66b00
commit 4c57a64a82

View file

@ -188,6 +188,17 @@ struct Window
return false;
}
bool ready()
{
if (!example->content(Window::canvas, width, height)) return false;
//initiate the first rendering before window pop-up.
if (!verify(canvas->draw())) return false;
if (!verify(canvas->sync())) return false;
return true;
}
void show()
{
SDL_ShowWindow(window);
@ -250,7 +261,6 @@ struct Window
}
}
virtual bool ready() { return false; }
virtual void resize() {}
virtual void refresh() {}
};
@ -269,22 +279,17 @@ struct SwWindow : Window
SwWindow(Example* example, uint32_t width, uint32_t height) : Window(tvg::CanvasEngine::Sw, example, width, height)
{
window = SDL_CreateWindow("ThorVG Example (Software)", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE);
}
bool ready() override
{
//Create a Canvas
canvas = tvg::SwCanvas::gen();
if (!canvas) {
cout << "SwCanvas is not supported. Did you enable the SwEngine?" << endl;
return false;
return;
}
Window::canvas = canvas.get();
resize();
return example->content(Window::canvas, width, height);
}
void resize() override
@ -325,27 +330,22 @@ struct GlWindow : Window
#endif
window = SDL_CreateWindow("ThorVG Example (OpenGL/ES)", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL | SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE);
context = SDL_GL_CreateContext(window);
}
virtual ~GlWindow()
{
SDL_GL_DeleteContext(context);
}
bool ready() override
{
//Create a Canvas
canvas = tvg::GlCanvas::gen();
if (!canvas) {
cout << "GlCanvas is not supported. Did you enable the GlEngine?" << endl;
return false;
return;
}
Window::canvas = canvas.get();
resize();
}
return example->content(Window::canvas, width, height);
virtual ~GlWindow()
{
SDL_GL_DeleteContext(context);
}
void resize() override
@ -419,6 +419,17 @@ struct WgWindow : Window
surfaceDesc.nextInChain = (const WGPUChainedStruct*)&surfaceNativeDesc;
surfaceDesc.label = "The surface";
surface = wgpuInstanceCreateSurface(instance, &surfaceDesc);
//Create a Canvas
canvas = tvg::WgCanvas::gen();
if (!canvas) {
cout << "WgCanvas is not supported. Did you enable the WgEngine?" << endl;
return;
}
Window::canvas = canvas.get();
resize();
}
virtual ~WgWindow()
@ -427,22 +438,6 @@ struct WgWindow : Window
wgpuInstanceRelease(instance);
}
bool ready() override
{
//Create a Canvas
canvas = tvg::WgCanvas::gen();
if (!canvas) {
cout << "WgCanvas is not supported. Did you enable the WgEngine?" << endl;
return false;
}
Window::canvas = canvas.get();
resize();
return example->content(Window::canvas, width, height);
}
void resize() override
{
//Set the canvas target and draw on it.