From 08bb51e3583452920e8071edddbcafe5a9a79d5b Mon Sep 17 00:00:00 2001 From: David Capello Date: Fri, 18 Nov 2016 09:38:25 -0300 Subject: [PATCH] Merge all she::instance() implementations Only she::create_system() must be provided by the platform impl. --- src/main/main.cpp | 2 +- src/she/CMakeLists.txt | 3 ++- src/she/alleg4/she.cpp | 16 +++------------- src/she/skia/she.cpp | 9 +-------- src/she/system.cpp | 23 +++++++++++++++++++++++ src/tests/test.h | 4 ++-- 6 files changed, 32 insertions(+), 25 deletions(-) create mode 100644 src/she/system.cpp diff --git a/src/main/main.cpp b/src/main/main.cpp index d2f99f467..69943d811 100644 --- a/src/main/main.cpp +++ b/src/main/main.cpp @@ -66,7 +66,7 @@ int app_main(int argc, char* argv[]) MemLeak memleak; base::SystemConsole systemConsole; app::AppOptions options(argc, const_cast(argv)); - she::ScopedHandle system(she::create_system()); + she::ScopedHandle system(she::instance()); app::App app; // Change the name of the memory dump file diff --git a/src/she/CMakeLists.txt b/src/she/CMakeLists.txt index d1d92f084..3360e5c3d 100644 --- a/src/she/CMakeLists.txt +++ b/src/she/CMakeLists.txt @@ -2,7 +2,8 @@ # Copyright (C) 2012-2016 David Capello set(SHE_SOURCES - common/freetype_font.cpp) + common/freetype_font.cpp + system.cpp) ###################################################################### # Allegro 4 backend diff --git a/src/she/alleg4/she.cpp b/src/she/alleg4/she.cpp index e2e1f5df0..2771ac533 100644 --- a/src/she/alleg4/she.cpp +++ b/src/she/alleg4/she.cpp @@ -63,8 +63,6 @@ #include "she/alleg4/mouse_poller.h" #endif -static she::System* g_instance = nullptr; - namespace she { class Alleg4EventQueue : public EventQueue { @@ -129,15 +127,11 @@ public: #ifdef USE_MOUSE_POLLER mouse_poller_init(); #endif - - g_instance = this; } ~Alleg4System() { remove_timer(); allegro_exit(); - - g_instance = nullptr; } void dispose() override { @@ -216,15 +210,11 @@ System* create_system() { return new Alleg4System(); } -System* instance() -{ - return g_instance; -} - void error_message(const char* msg) { - if (g_instance && g_instance->logger()) - g_instance->logger()->logError(msg); + System* sys = instance(); + if (sys && sys->logger()) + sys->logger()->logError(msg); #ifdef _WIN32 std::wstring wmsg = base::from_utf8(msg); diff --git a/src/she/skia/she.cpp b/src/she/skia/she.cpp index 0a904ba29..f95cc2c00 100644 --- a/src/she/skia/she.cpp +++ b/src/she/skia/she.cpp @@ -24,15 +24,8 @@ namespace she { -static System* g_instance; - System* create_system() { - return g_instance = new SkiaSystem(); -} - -System* instance() -{ - return g_instance; + return new SkiaSystem(); } void error_message(const char* msg) diff --git a/src/she/system.cpp b/src/she/system.cpp new file mode 100644 index 000000000..2b746792b --- /dev/null +++ b/src/she/system.cpp @@ -0,0 +1,23 @@ +// SHE library +// Copyright (C) 2016 David Capello +// +// This file is released under the terms of the MIT license. +// Read LICENSE.txt for more information. + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include "she/system.h" + +namespace she { + +System* instance() +{ + static System* sys = nullptr; + if (!sys) + sys = create_system(); + return sys; +} + +} // namespace she diff --git a/src/tests/test.h b/src/tests/test.h index 210dc4ecc..9896066e8 100644 --- a/src/tests/test.h +++ b/src/tests/test.h @@ -1,5 +1,5 @@ // Aseprite -// Copyright (C) 2001-2015 David Capello +// Copyright (C) 2001-2016 David Capello // // This program is distributed under the terms of // the End-User License Agreement for Aseprite. @@ -38,7 +38,7 @@ int main(int argc, char* argv[]) #ifdef TEST_GUI { // Do not create a she::System, as we don't need it for testing purposes. - //she::ScopedHandle system(she::create_system()); + //she::ScopedHandle system(she::instance()); ui::UISystem uiSystem; ui::Manager uiManager; #endif