Improve render context initialization

This commit is contained in:
Joshua Granick
2014-06-12 11:54:10 -07:00
parent 9ab066c4db
commit 0ae3c49b4b
5 changed files with 94 additions and 75 deletions

View File

@@ -11,6 +11,6 @@ enum RenderContext {
OPENGL (gl:GLRenderContext); OPENGL (gl:GLRenderContext);
CANVAS (context:CanvasRenderContext); CANVAS (context:CanvasRenderContext);
DOM (element:DOMRenderContext); DOM (element:DOMRenderContext);
CUSTOM (context:Dynamic); CUSTOM (data:Dynamic);
} }

View File

@@ -1,11 +1,17 @@
package lime.graphics; package lime.graphics;
import lime.graphics.opengl.GL;
import lime.graphics.opengl.GLRenderContext; import lime.graphics.opengl.GLRenderContext;
import lime.system.System; import lime.system.System;
import lime.ui.Window; import lime.ui.Window;
#if js
import js.html.webgl.RenderingContext;
#end
@:access(lime.graphics.opengl.GL)
class Renderer { class Renderer {
@@ -25,9 +31,54 @@ class Renderer {
public function create ():Void { public function create ():Void {
#if (cpp || neko) #if js
if (window.div != null) {
context = DOM (window.div);
} else if (window.canvas != null) {
#if canvas
var webgl = null;
#else
var webgl:RenderingContext = cast window.canvas.getContext ("webgl");
if (webgl == null) {
webgl = cast window.canvas.getContext ("experimental-webgl");
}
#end
if (webgl == null) {
context = CANVAS (cast window.canvas.getContext ("2d"));
} else {
#if debug
webgl = untyped WebGLDebugUtils.makeDebugContext (webgl);
#end
GL.context = webgl;
context = OPENGL (new GLRenderContext ());
trace ("context ready");
}
}
#elseif (cpp || neko)
handle = lime_renderer_create (window.handle); handle = lime_renderer_create (window.handle);
context = OPENGL (new GLRenderContext ()); context = OPENGL (new GLRenderContext ());
#end #end
} }

View File

@@ -1,6 +1,6 @@
package lime.graphics.canvas; package lime.graphics.canvas;
#if js #if js
typedef CanvasRenderContext = js.html.CanvasRenderContext2D; typedef CanvasRenderContext = js.html.CanvasRenderingContext2D;
#else #else

View File

@@ -1,6 +1,6 @@
package lime.graphics.dom; package lime.graphics.dom;
#if js #if js
typedef DOMRenderContext = js.html.HtmlElement; typedef DOMRenderContext = js.html.DivElement;
#else #else

View File

@@ -10,9 +10,8 @@ import lime.graphics.RenderEventManager;
import lime.system.System; import lime.system.System;
#if js #if js
import lime.graphics.opengl.GL;
import js.html.webgl.RenderingContext;
import js.html.CanvasElement; import js.html.CanvasElement;
import js.html.DivElement;
import js.html.HtmlElement; import js.html.HtmlElement;
import js.Browser; import js.Browser;
#end #end
@@ -21,29 +20,27 @@ import js.Browser;
class Window { class Window {
public static var instance:Window;
public var currentRenderer:Renderer; public var currentRenderer:Renderer;
#if js public var fullscreen:Bool;
public var element:HtmlElement;
#end
public var handle:Dynamic;
public var height:Int; public var height:Int;
#if (js && stats)
public var stats:Dynamic;
#end
public var width:Int; public var width:Int;
private var application:Application; private var application:Application;
#if js #if js
private var canvas:CanvasElement; public var canvas:CanvasElement;
public var div:DivElement;
public var element:HtmlElement;
#if stats
public var stats:Dynamic;
#end
#elseif (cpp || neko)
public var handle:Dynamic;
#end #end
public function new (application:Application) { public function new (application:Application) {
instance = this;
this.application = application; this.application = application;
} }
@@ -59,28 +56,36 @@ class Window {
} else { } else {
#if dom
div = cast Browser.document.createElement ("div");
#else
canvas = cast Browser.document.createElement ("canvas"); canvas = cast Browser.document.createElement ("canvas");
#end
} }
var context:RenderingContext = cast canvas.getContext ("webgl"); if (canvas != null) {
if (context == null) {
context = cast canvas.getContext ("experimental-webgl"); var style = canvas.style;
style.setProperty ("-webkit-transform", "translateZ(0)", null);
style.setProperty ("transform", "translateZ(0)", null);
} else if (div != null) {
var style = div.style;
style.setProperty ("-webkit-transform", "translate3D(0,0,0)", null);
style.setProperty ("transform", "translate3D(0,0,0)", null);
//style.setProperty ("-webkit-transform-style", "preserve-3d", null);
//style.setProperty ("transform-style", "preserve-3d", null);
style.position = "relative";
style.overflow = "hidden";
style.setProperty ("-webkit-user-select", "none", null);
style.setProperty ("-moz-user-select", "none", null);
style.setProperty ("-ms-user-select", "none", null);
style.setProperty ("-o-user-select", "none", null);
} }
#if debug
context = untyped WebGLDebugUtils.makeDebugContext (context);
#end
GL.context = context;
var style = canvas.style;
style.setProperty ("-webkit-transform", "translateZ(0)", null);
style.setProperty ("transform", "translateZ(0)", null);
//__originalWidth = width; //__originalWidth = width;
//__originalHeight = height; //__originalHeight = height;
@@ -98,7 +103,7 @@ class Window {
} }
//__fullscreen = true; fullscreen = true;
} }
@@ -110,18 +115,15 @@ class Window {
canvas.width = width; canvas.width = width;
canvas.height = height; canvas.height = height;
/*} else { } else {
div.style.width = width + "px";
div.style.height = height + "px";
__div.style.width = width + "px";
__div.style.height = height + "px";
*/
} }
//__resize (); //__resize ();
//Browser.window.addEventListener ("resize", window_onResize); //Browser.window.addEventListener ("resize", window_onResize);
//Browser.window.addEventListener ("focus", window_onFocus);
//Browser.window.addEventListener ("blur", window_onBlur);
if (element != null) { if (element != null) {
@@ -135,7 +137,7 @@ class Window {
} else { } else {
//element.appendChild (__div); element.appendChild (div);
} }
@@ -148,40 +150,6 @@ class Window {
Browser.document.body.appendChild (stats.domElement); Browser.document.body.appendChild (stats.domElement);
#end #end
/*var keyEvents = [ "keydown", "keyup" ];
var touchEvents = [ "touchstart", "touchmove", "touchend" ];
var mouseEvents = [ "mousedown", "mousemove", "mouseup", "click", "dblclick", "mousewheel" ];
var focusEvents = [ "focus", "blur" ];
var element = __canvas != null ? __canvas : __div;
for (type in keyEvents) {
Browser.window.addEventListener (type, window_onKey, false);
}
for (type in touchEvents) {
element.addEventListener (type, element_onTouch, true);
}
for (type in mouseEvents) {
element.addEventListener (type, element_onMouse, true);
}
for (type in focusEvents) {
element.addEventListener (type, element_onFocus, true);
}
Browser.window.requestAnimationFrame (cast __render);
*/
#elseif (cpp || neko) #elseif (cpp || neko)
handle = lime_window_create (application.handle); handle = lime_window_create (application.handle);
#end #end