Add support for render context lost/restored events, initially with support for WebGL (thanks @mrcdk)
This commit is contained in:
@@ -105,6 +105,9 @@ class Application extends Module {
|
||||
TouchEventManager.onTouchMove.add (onTouchMove);
|
||||
TouchEventManager.onTouchEnd.add (onTouchEnd);
|
||||
|
||||
Renderer.onRenderContextLost.add (onRenderContextLost);
|
||||
Renderer.onRenderContextRestored.add (onRenderContextRestored);
|
||||
|
||||
Window.onWindowActivate.add (onWindowActivate);
|
||||
Window.onWindowClose.add (onWindowClose);
|
||||
Window.onWindowDeactivate.add (onWindowDeactivate);
|
||||
@@ -295,6 +298,19 @@ class Application extends Module {
|
||||
public function onMouseWheel (deltaX:Float, deltaY:Float):Void { }
|
||||
|
||||
|
||||
/**
|
||||
* Called when a render context is lost
|
||||
*/
|
||||
public function onRenderContextLost ():Void { }
|
||||
|
||||
|
||||
/**
|
||||
* Called when a render context is restored
|
||||
* @param context The current render context
|
||||
*/
|
||||
public function onRenderContextRestored (context:RenderContext):Void { }
|
||||
|
||||
|
||||
/**
|
||||
* Called when a touch end event is fired
|
||||
* @param x The current x coordinate of the touch point
|
||||
@@ -407,7 +423,7 @@ class Application extends Module {
|
||||
__eventInfo.deltaTime = 16; //TODO
|
||||
__dispatch ();
|
||||
|
||||
Renderer.dispatch ();
|
||||
Renderer.render ();
|
||||
|
||||
#if (js && html5)
|
||||
Browser.window.requestAnimationFrame (cast __triggerFrame);
|
||||
|
||||
@@ -23,9 +23,11 @@ import flash.Lib;
|
||||
class Renderer {
|
||||
|
||||
|
||||
public static var onRenderContextLost = new Event<Void->Void> ();
|
||||
public static var onRenderContextRestored = new Event<RenderContext->Void> ();
|
||||
public static var onRender = new Event<RenderContext->Void> ();
|
||||
|
||||
private static var eventInfo = new RenderEventInfo ();
|
||||
private static var eventInfo = new RenderEventInfo (RENDER);
|
||||
private static var registered:Bool;
|
||||
|
||||
public var context:RenderContext;
|
||||
@@ -44,6 +46,44 @@ class Renderer {
|
||||
|
||||
public function create ():Void {
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
|
||||
handle = lime_renderer_create (window.handle);
|
||||
|
||||
#end
|
||||
|
||||
createContext ();
|
||||
|
||||
#if (js && html5)
|
||||
|
||||
switch (context) {
|
||||
|
||||
case OPENGL (_):
|
||||
|
||||
window.canvas.addEventListener ("webglcontextlost", handleCanvasEvent, false);
|
||||
window.canvas.addEventListener ("webglcontextrestored", handleCanvasEvent, false);
|
||||
|
||||
default:
|
||||
|
||||
}
|
||||
|
||||
#end
|
||||
|
||||
if (!registered) {
|
||||
|
||||
registered = true;
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
lime_render_event_manager_register (dispatch, eventInfo);
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private function createContext ():Void {
|
||||
|
||||
#if (js && html5)
|
||||
|
||||
if (window.div != null) {
|
||||
@@ -94,7 +134,6 @@ class Renderer {
|
||||
|
||||
#elseif (cpp || neko || nodejs)
|
||||
|
||||
handle = lime_renderer_create (window.handle);
|
||||
context = OPENGL (new GLRenderContext ());
|
||||
|
||||
#elseif flash
|
||||
@@ -103,26 +142,14 @@ class Renderer {
|
||||
|
||||
#end
|
||||
|
||||
if (!registered) {
|
||||
|
||||
registered = true;
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
lime_render_event_manager_register (dispatch, eventInfo);
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static function dispatch ():Void {
|
||||
private function dispatch ():Void {
|
||||
|
||||
for (window in Application.__instance.windows) {
|
||||
switch (eventInfo.type) {
|
||||
|
||||
if (window.currentRenderer != null) {
|
||||
|
||||
var context = window.currentRenderer.context;
|
||||
case RENDER:
|
||||
|
||||
if (!Application.__initialized) {
|
||||
|
||||
@@ -134,18 +161,24 @@ class Renderer {
|
||||
Application.__instance.render (context);
|
||||
onRender.dispatch (context);
|
||||
|
||||
window.currentRenderer.flip ();
|
||||
flip ();
|
||||
|
||||
case RENDER_CONTEXT_LOST:
|
||||
|
||||
context = null;
|
||||
|
||||
onRenderContextLost.dispatch ();
|
||||
|
||||
case RENDER_CONTEXT_RESTORED:
|
||||
|
||||
createContext ();
|
||||
|
||||
onRenderContextRestored.dispatch (context);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#if (js && stats)
|
||||
Application.__instance.windows[0].stats.end ();
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function flip ():Void {
|
||||
|
||||
@@ -156,6 +189,53 @@ class Renderer {
|
||||
}
|
||||
|
||||
|
||||
#if (js && html5)
|
||||
private function handleCanvasEvent (event:js.html.Event):Void {
|
||||
|
||||
switch (event.type) {
|
||||
|
||||
case "webglcontextlost":
|
||||
|
||||
event.preventDefault ();
|
||||
eventInfo.type = RENDER_CONTEXT_LOST;
|
||||
dispatch ();
|
||||
|
||||
case "webglcontextrestored":
|
||||
|
||||
createContext ();
|
||||
|
||||
eventInfo.type = RENDER_CONTEXT_RESTORED;
|
||||
dispatch ();
|
||||
|
||||
default:
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
#end
|
||||
|
||||
|
||||
private static function render ():Void {
|
||||
|
||||
eventInfo.type = RENDER;
|
||||
|
||||
for (window in Application.__instance.windows) {
|
||||
|
||||
if (window.currentRenderer != null) {
|
||||
|
||||
window.currentRenderer.dispatch ();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#if (js && stats)
|
||||
Application.__instance.windows[0].stats.end ();
|
||||
#end
|
||||
|
||||
}
|
||||
|
||||
|
||||
#if (cpp || neko || nodejs)
|
||||
private static var lime_render_event_manager_register = System.load ("lime", "lime_render_event_manager_register", 2);
|
||||
private static var lime_renderer_create = System.load ("lime", "lime_renderer_create", 1);
|
||||
@@ -194,5 +274,7 @@ private class RenderEventInfo {
|
||||
@:enum private abstract RenderEventType(Int) {
|
||||
|
||||
var RENDER = 0;
|
||||
var RENDER_CONTEXT_LOST = 1;
|
||||
var RENDER_CONTEXT_RESTORED = 2;
|
||||
|
||||
}
|
||||
@@ -10,7 +10,9 @@ namespace lime {
|
||||
|
||||
enum RenderEventType {
|
||||
|
||||
RENDER
|
||||
RENDER,
|
||||
RENDER_CONTEXT_LOST,
|
||||
RENDER_CONTEXT_RESTORED
|
||||
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user