From 350f7a435713ec397f9a2ce321ca30de1551d796 Mon Sep 17 00:00:00 2001 From: Hermet Park Date: Sun, 14 Jul 2024 11:58:00 +0900 Subject: [PATCH] examples: ++exception handling Added handling for engine initialization failure. --- examples/Example.h | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/examples/Example.h b/examples/Example.h index 8f7199f1..0ddc651c 100644 --- a/examples/Example.h +++ b/examples/Example.h @@ -122,8 +122,6 @@ struct Example struct Window { - static Window* instance; - SDL_Window* window = nullptr; tvg::Canvas* canvas = nullptr; @@ -134,6 +132,7 @@ struct Window bool needResize = false; bool needDraw = false; + bool initialized = false; bool print = false; Window(tvg::CanvasEngine engine, Example* example, uint32_t width, uint32_t height, uint32_t threadsCnt) @@ -148,7 +147,7 @@ struct Window this->width = width; this->height = height; this->example = example; - this->instance = this; + this->initialized = true; } virtual ~Window() @@ -254,8 +253,6 @@ struct Window virtual void refresh() {} }; -Window* Window::instance = nullptr; - /************************************************************************/ /* SwCanvas Window Code */ @@ -267,6 +264,8 @@ struct SwWindow : Window SwWindow(Example* example, uint32_t width, uint32_t height, uint32_t threadsCnt) : Window(tvg::CanvasEngine::Sw, example, width, height, threadsCnt) { + if (!initialized) return; + window = SDL_CreateWindow("ThorVG Example (Software)", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_HIDDEN | SDL_WINDOW_RESIZABLE); //Create a Canvas @@ -308,6 +307,8 @@ struct GlWindow : Window GlWindow(Example* example, uint32_t width, uint32_t height, uint32_t threadsCnt) : Window(tvg::CanvasEngine::Gl, example, width, height, threadsCnt) { + if (!initialized) return; + #ifdef THORVG_GL_TARGET_GLES SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); @@ -364,6 +365,8 @@ struct WgWindow : Window WgWindow(Example* example, uint32_t width, uint32_t height, uint32_t threadsCnt) : Window(tvg::CanvasEngine::Wg, example, width, height, threadsCnt) { + if (!initialized) return; + window = SDL_CreateWindow("ThorVG Example (WebGPU)", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_HIDDEN); //Here we create our WebGPU surface from the window!