From 56c0872b8afbb09d7ae636fe664510baa0c509af Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Thu, 20 Aug 2015 13:39:53 -0700 Subject: [PATCH] Use onWindowCreate() instead of init(), fix support for multiple GL windows, use window.renderer instead of currentRenderer --- lime/_backend/native/NativeRenderer.hx | 4 +++ lime/app/Application.hx | 48 ++++++++++++------------- lime/app/IModule.hx | 14 ++++---- lime/app/Module.hx | 14 ++++---- lime/graphics/Renderer.hx | 2 +- lime/ui/Window.hx | 7 ++-- project/include/graphics/Renderer.h | 2 ++ project/src/ExternalInterface.cpp | 18 ++++++++++ project/src/backend/sdl/SDLRenderer.cpp | 30 ++++++++++++++-- project/src/backend/sdl/SDLRenderer.h | 3 ++ tests/runtime/test/lime/WindowTest.hx | 16 ++++----- 11 files changed, 104 insertions(+), 54 deletions(-) diff --git a/lime/_backend/native/NativeRenderer.hx b/lime/_backend/native/NativeRenderer.hx index 91b9234a3..4e2d5e442 100644 --- a/lime/_backend/native/NativeRenderer.hx +++ b/lime/_backend/native/NativeRenderer.hx @@ -101,6 +101,8 @@ class NativeRenderer { public function render ():Void { + lime_renderer_make_current (handle); + if (!useHardware) { #if lime_cairo @@ -147,8 +149,10 @@ class NativeRenderer { private static var lime_renderer_create = System.load ("lime", "lime_renderer_create", 1); private static var lime_renderer_flip = System.load ("lime", "lime_renderer_flip", 1); + private static var lime_renderer_get_context = System.load ("lime", "lime_renderer_get_context", 1); private static var lime_renderer_get_type = System.load ("lime", "lime_renderer_get_type", 1); private static var lime_renderer_lock = System.load ("lime", "lime_renderer_lock", 1); + private static var lime_renderer_make_current = System.load ("lime", "lime_renderer_make_current", 1); private static var lime_renderer_unlock = System.load ("lime", "lime_renderer_unlock", 1); diff --git a/lime/app/Application.hx b/lime/app/Application.hx index 037bd0b56..9c6654175 100644 --- a/lime/app/Application.hx +++ b/lime/app/Application.hx @@ -39,7 +39,6 @@ class Application extends Module { public var windows (default, null):Array; @:noCompletion private var backend:ApplicationBackend; - @:noCompletion private var initialized:Bool; @:noCompletion private var windowByID:Map; @@ -79,11 +78,11 @@ class Application extends Module { modules.push (module); - if (initialized) { + if (windows.length > 0) { - if (renderer != null) { + for (window in windows) { - module.init (this); + module.onWindowCreate (window); } @@ -150,7 +149,11 @@ class Application extends Module { } - init (this); + if (preloader == null || preloader.complete) { + + onPreloadComplete (); + + } } @@ -168,6 +171,7 @@ class Application extends Module { window.onActivate.add (onWindowActivate.bind (window)); window.onClose.add (onWindowClose.bind (window)); + window.onCreate.add (onWindowCreate.bind (window)); window.onDeactivate.add (onWindowDeactivate.bind (window)); window.onEnter.add (onWindowEnter.bind (window)); window.onFocusIn.add (onWindowFocusIn.bind (window)); @@ -188,7 +192,7 @@ class Application extends Module { window.onTextEdit.add (onTextEdit.bind (window)); window.onTextInput.add (onTextInput.bind (window)); - if (window.currentRenderer == null) { + if (window.renderer == null) { var renderer = new Renderer (window); addRenderer (renderer); @@ -199,6 +203,8 @@ class Application extends Module { windows.push (window); windowByID.set (window.id, window); + window.onCreate.dispatch (); + } @@ -217,25 +223,6 @@ class Application extends Module { } - public override function init (application:Application):Void { - - for (module in modules) { - - module.init (application); - - } - - initialized = true; - - if (preloader == null || preloader.complete) { - - onPreloadComplete (); - - } - - } - - public override function onGamepadAxisMove (gamepad:Gamepad, axis:GamepadAxis, value:Float):Void { for (module in modules) { @@ -507,6 +494,17 @@ class Application extends Module { } + public override function onWindowCreate (window:Window):Void { + + for (module in modules) { + + module.onWindowCreate (window); + + } + + } + + public override function onWindowDeactivate (window:Window):Void { for (module in modules) { diff --git a/lime/app/IModule.hx b/lime/app/IModule.hx index e3eaee7e6..1f457a86a 100644 --- a/lime/app/IModule.hx +++ b/lime/app/IModule.hx @@ -15,13 +15,6 @@ import lime.ui.Window; interface IModule { - /** - * Called when the module is initialized - * @param application The parent application - */ - public function init (application:Application):Void; - - /** * Called when a gamepad axis move event is fired * @param gamepad The current gamepad @@ -216,6 +209,13 @@ interface IModule { public function onWindowClose (window:Window):Void; + /** + * Called when a window create event is fired + * @param window The window dispatching the event + */ + public function onWindowCreate (window:Window):Void; + + /** * Called when a window deactivate event is fired * @param window The window dispatching the event diff --git a/lime/app/Module.hx b/lime/app/Module.hx index cfb487821..102f0788b 100644 --- a/lime/app/Module.hx +++ b/lime/app/Module.hx @@ -28,13 +28,6 @@ class Module implements IModule { } - /** - * Called when the module is initialized - * @param application The parent application - */ - public function init (application:Application):Void { } - - /** * Called when a gamepad axis move event is fired * @param gamepad The current gamepad @@ -229,6 +222,13 @@ class Module implements IModule { public function onWindowClose (window:Window):Void { } + /** + * Called when a window create event is fired + * @param window The window dispatching the event + */ + public function onWindowCreate (window:Window):Void { } + + /** * Called when a window deactivate event is fired * @param window The window dispatching the event diff --git a/lime/graphics/Renderer.hx b/lime/graphics/Renderer.hx index 0ce4eb757..e442f2f6e 100644 --- a/lime/graphics/Renderer.hx +++ b/lime/graphics/Renderer.hx @@ -23,7 +23,7 @@ class Renderer { backend = new RendererBackend (this); - this.window.currentRenderer = this; + this.window.renderer = this; } diff --git a/lime/ui/Window.hx b/lime/ui/Window.hx index ba72c65fb..3f6c341c0 100644 --- a/lime/ui/Window.hx +++ b/lime/ui/Window.hx @@ -19,7 +19,6 @@ class Window { public var application (default, null):Application; - public var currentRenderer:Renderer; public var config:WindowConfig; public var display (get, null):Display; public var enableTextEvents (get, set):Bool; @@ -29,6 +28,7 @@ class Window { public var minimized (get, set):Bool; public var onActivate = new EventVoid> (); public var onClose = new EventVoid> (); + public var onCreate = new EventVoid> (); public var onDeactivate = new EventVoid> (); public var onEnter = new EventVoid> (); public var onFocusIn = new EventVoid> (); @@ -48,6 +48,7 @@ class Window { public var onRestore = new EventVoid> (); public var onTextEdit = new EventInt->Int->Void> (); public var onTextInput = new EventVoid> (); + public var renderer:Renderer; public var stage:Stage; public var title (get, set):String; public var width (get, set):Int; @@ -220,9 +221,9 @@ class Window { #end - if (currentRenderer != null) { + if (renderer != null) { - currentRenderer.create (); + renderer.create (); } diff --git a/project/include/graphics/Renderer.h b/project/include/graphics/Renderer.h index 5f1bb69ac..b87009f0c 100644 --- a/project/include/graphics/Renderer.h +++ b/project/include/graphics/Renderer.h @@ -15,7 +15,9 @@ namespace lime { public: virtual void Flip () = 0; + virtual void* GetContext () = 0; virtual value Lock () = 0; + virtual void MakeCurrent () = 0; virtual const char* Type () = 0; virtual void Unlock () = 0; diff --git a/project/src/ExternalInterface.cpp b/project/src/ExternalInterface.cpp index 9d250702f..75da57232 100644 --- a/project/src/ExternalInterface.cpp +++ b/project/src/ExternalInterface.cpp @@ -910,6 +910,14 @@ namespace lime { } + value lime_renderer_get_context (value renderer) { + + Renderer* targetRenderer = (Renderer*)(intptr_t)val_float (renderer); + return alloc_float ((intptr_t)targetRenderer->GetContext ()); + + } + + value lime_renderer_get_type (value renderer) { Renderer* targetRenderer = (Renderer*)(intptr_t)val_float (renderer); @@ -925,6 +933,14 @@ namespace lime { } + value lime_renderer_make_current (value renderer) { + + ((Renderer*)(intptr_t)val_float (renderer))->MakeCurrent (); + return alloc_null (); + + } + + value lime_renderer_unlock (value renderer) { ((Renderer*)(intptr_t)val_float (renderer))->Unlock (); @@ -1273,8 +1289,10 @@ namespace lime { DEFINE_PRIM (lime_png_decode_file, 2); DEFINE_PRIM (lime_renderer_create, 1); DEFINE_PRIM (lime_renderer_flip, 1); + DEFINE_PRIM (lime_renderer_get_context, 1); DEFINE_PRIM (lime_renderer_get_type, 1); DEFINE_PRIM (lime_renderer_lock, 1); + DEFINE_PRIM (lime_renderer_make_current, 1); DEFINE_PRIM (lime_renderer_unlock, 1); DEFINE_PRIM (lime_render_event_manager_register, 2); DEFINE_PRIM (lime_system_get_directory, 3); diff --git a/project/src/backend/sdl/SDLRenderer.cpp b/project/src/backend/sdl/SDLRenderer.cpp index b70085b5a..494894846 100644 --- a/project/src/backend/sdl/SDLRenderer.cpp +++ b/project/src/backend/sdl/SDLRenderer.cpp @@ -11,6 +11,7 @@ namespace lime { currentWindow = window; sdlWindow = ((SDLWindow*)window)->sdlWindow; sdlTexture = 0; + context = 0; width = 0; height = 0; @@ -46,6 +47,8 @@ namespace lime { } + context = SDL_GL_GetCurrentContext (); + OpenGLBindings::Init (); } @@ -69,6 +72,13 @@ namespace lime { } + void* SDLRenderer::GetContext () { + + return context; + + } + + value SDLRenderer::Lock () { int width; @@ -76,10 +86,13 @@ namespace lime { SDL_GetRendererOutputSize (sdlRenderer, &width, &height); - if ( width != this->width || height != this->height) { + if (width != this->width || height != this->height) { - if( sdlTexture ) - SDL_DestroyTexture( sdlTexture ); + if (sdlTexture) { + + SDL_DestroyTexture (sdlTexture); + + } sdlTexture = SDL_CreateTexture (sdlRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, width, height); @@ -104,6 +117,17 @@ namespace lime { } + void SDLRenderer::MakeCurrent () { + + if (sdlWindow && context) { + + SDL_GL_MakeCurrent (sdlWindow, context); + + } + + } + + const char* SDLRenderer::Type () { if (sdlRenderer) { diff --git a/project/src/backend/sdl/SDLRenderer.h b/project/src/backend/sdl/SDLRenderer.h index f2c1d6c02..ae530517a 100644 --- a/project/src/backend/sdl/SDLRenderer.h +++ b/project/src/backend/sdl/SDLRenderer.h @@ -17,7 +17,9 @@ namespace lime { ~SDLRenderer (); virtual void Flip (); + virtual void* GetContext (); virtual value Lock (); + virtual void MakeCurrent (); virtual const char* Type (); virtual void Unlock (); @@ -27,6 +29,7 @@ namespace lime { private: + SDL_GLContext context; int width; int height; diff --git a/tests/runtime/test/lime/WindowTest.hx b/tests/runtime/test/lime/WindowTest.hx index 2c6113709..cb8446778 100644 --- a/tests/runtime/test/lime/WindowTest.hx +++ b/tests/runtime/test/lime/WindowTest.hx @@ -27,7 +27,7 @@ class WindowTest { var window = new Window (); - Assert.isNull (window.currentRenderer); + Assert.isNull (window.renderer); Assert.isNull (window.config); Assert.isFalse (window.fullscreen); Assert.areEqual (0, window.height); @@ -37,7 +37,7 @@ class WindowTest { app.createWindow (window); - Assert.isNotNull (window.currentRenderer); + Assert.isNotNull (window.renderer); Assert.isNull (window.config); #if !html5 @@ -70,7 +70,7 @@ class WindowTest { window.width = 400; window.height = 300; - Assert.isNull (window.currentRenderer); + Assert.isNull (window.renderer); Assert.isNull (window.config); Assert.isFalse (window.fullscreen); Assert.areEqual (300, window.height); @@ -80,7 +80,7 @@ class WindowTest { app.createWindow (window); - Assert.isNotNull (window.currentRenderer); + Assert.isNotNull (window.renderer); Assert.isNull (window.config); Assert.isFalse (window.fullscreen); Assert.areEqual (300, window.height); @@ -104,7 +104,7 @@ class WindowTest { var config = {}; var window = new Window (config); - Assert.isNull (window.currentRenderer); + Assert.isNull (window.renderer); Assert.areEqual (config, window.config); Assert.isFalse (window.fullscreen); Assert.areEqual (0, window.height); @@ -114,7 +114,7 @@ class WindowTest { app.createWindow (window); - Assert.isNotNull (window.currentRenderer); + Assert.isNotNull (window.renderer); Assert.areEqual (config, window.config); #if !html5 @@ -143,7 +143,7 @@ class WindowTest { var config = { width: 400, height: 300 }; var window = new Window (config); - Assert.isNull (window.currentRenderer); + Assert.isNull (window.renderer); Assert.areEqual (config, window.config); Assert.isFalse (window.fullscreen); Assert.areEqual (300, window.height); @@ -153,7 +153,7 @@ class WindowTest { app.createWindow (window); - Assert.isNotNull (window.currentRenderer); + Assert.isNotNull (window.renderer); Assert.areEqual (config, window.config); Assert.isFalse (window.fullscreen); Assert.areEqual (300, window.height);