浏览代码

a bit of NME support

Nicolas Cannasse 11 年之前
父节点
当前提交
037a51ab65
共有 5 个文件被更改,包括 88 次插入33 次删除
  1. 4 4
      h3d/impl/Driver.hx
  2. 51 20
      h3d/impl/GlDriver.hx
  3. 5 3
      hxd/BitmapData.hx
  4. 4 4
      hxd/Stage.hx
  5. 24 2
      hxd/System.hx

+ 4 - 4
h3d/impl/Driver.hx

@@ -8,10 +8,10 @@ typedef Texture = flash.display3D.textures.TextureBase;
 typedef IndexBuffer = js.html.webgl.Buffer;
 typedef VertexBuffer = { b : js.html.webgl.Buffer, stride : Int };
 typedef Texture = { t : js.html.webgl.Texture, width : Int, height : Int, fmt : Int, ?fb : js.html.webgl.Framebuffer, ?rb : js.html.webgl.Renderbuffer };
-#elseif cpp
-typedef IndexBuffer = openfl.gl.GLBuffer;
-typedef VertexBuffer = { b : openfl.gl.GLBuffer, stride : Int };
-typedef Texture = { t : openfl.gl.GLTexture };
+#elseif nme
+typedef IndexBuffer = nme.gl.GLBuffer;
+typedef VertexBuffer = { b : nme.gl.GLBuffer, stride : Int };
+typedef Texture = { t : nme.gl.GLTexture, width : Int, height : Int, fmt : Int, ?fb : nme.gl.GLFramebuffer, ?rb : nme.gl.GLRenderbuffer };
 #else
 typedef IndexBuffer = Int;
 typedef VertexBuffer = Int;

+ 51 - 20
h3d/impl/GlDriver.hx

@@ -10,15 +10,20 @@ import js.html.Uint8Array;
 import js.html.Float32Array;
 private typedef GL = js.html.webgl.GL;
 private typedef Uniform = js.html.webgl.UniformLocation;
-#elseif cpp
-import openfl.gl.GL;
-private typedef Uint16Array = openfl.utils.Int16Array;
-private typedef Uint8Array = openfl.utils.UInt8Array;
-private typedef Float32Array = openfl.utils.Float32Array;
+private typedef Program = js.html.webgl.Program;
+private typedef GLShader = js.html.webgl.Shader;
+#elseif nme
+import nme.gl.GL;
+private typedef Uniform = Dynamic;
+private typedef Program = nme.gl.GLProgram;
+private typedef GLShader = nme.gl.GLShader;
+private typedef Uint16Array = nme.utils.Int16Array;
+private typedef Uint8Array = nme.utils.UInt8Array;
+private typedef Float32Array = nme.utils.Float32Array;
 #end
 
 private class CompiledShader {
-	public var s : js.html.webgl.Shader;
+	public var s : GLShader;
 	public var vertex : Bool;
 	public var globals : Uniform;
 	public var params : Uniform;
@@ -32,7 +37,7 @@ private class CompiledShader {
 }
 
 private class CompiledProgram {
-	public var p : js.html.webgl.Program;
+	public var p : Program;
 	public var vertex : CompiledShader;
 	public var fragment : CompiledShader;
 	public var stride : Int;
@@ -61,6 +66,9 @@ class GlDriver extends Driver {
 	var hasTargetFlip : Bool;
 	var frame : Int;
 
+	var bufferWidth : Int;
+	var bufferHeight : Int;
+
 	public function new() {
 		#if js
 		canvas = @:privateAccess hxd.Stage.getCanvas();
@@ -74,6 +82,7 @@ class GlDriver extends Driver {
 		var tmp = new Float32Array(8);
 		var sub = new Float32Array(tmp.buffer, 0, 4);
 		fixMult = sub.length == 1; // should be 4
+		trace(fixMult);
 		#end
 		programs = new Map();
 		curAttribs = 0;
@@ -183,9 +192,23 @@ class GlDriver extends Driver {
 	function uploadBuffer( s : CompiledShader, buf : h3d.shader.Buffers.ShaderBuffers, which : h3d.shader.Buffers.BufferKind ) {
 		switch( which ) {
 		case Globals:
-			if( s.globals != null ) gl.uniform4fv(s.globals, new Float32Array(buf.globals.toData()).subarray(0, s.shader.globalsSize * 4));
+			if( s.globals != null ) {
+				#if js
+				var a = new Float32Array(buf.globals.toData()).subarray(0, s.shader.globalsSize * 4);
+				#else
+				var a = new Float32Array(buf.globals.toData(), 0, s.shader.globalsSize * 4);
+				#end
+				gl.uniform4fv(s.globals, a);
+			}
 		case Params:
-			if( s.params != null ) gl.uniform4fv(s.params, new Float32Array(buf.params.toData()).subarray(0, s.shader.paramsSize * 4));
+			if( s.params != null ) {
+				#if js
+				var a = new Float32Array(buf.params.toData()).subarray(0, s.shader.paramsSize * 4);
+				#else
+				var a = new Float32Array(buf.params.toData(), 0, s.shader.paramsSize * 4);
+				#end
+				gl.uniform4fv(s.params, a);
+			}
 		case Textures:
 			for( i in 0...s.textures.length ) {
 				var t = buf.tex[i];
@@ -308,6 +331,8 @@ class GlDriver extends Driver {
 		#elseif cpp
 		// resize window
 		#end
+		bufferWidth = width;
+		bufferHeight = height;
 		gl.viewport(0, 0, width, height);
 	}
 
@@ -350,32 +375,28 @@ class GlDriver extends Driver {
 
 	override function allocVertexes( m : ManagedBuffer ) : VertexBuffer {
 		var b = gl.createBuffer();
-		#if js
 		gl.bindBuffer(GL.ARRAY_BUFFER, b);
 		if( m.size * m.stride == 0 ) throw "assert";
+		#if js
 		gl.bufferData(GL.ARRAY_BUFFER, m.size * m.stride * 4, m.flags.has(Dynamic) ? GL.DYNAMIC_DRAW : GL.STATIC_DRAW);
-		gl.bindBuffer(GL.ARRAY_BUFFER, null);
 		#else
-		var tmp = new Uint8Array(count * stride * 4);
-		gl.bindBuffer(GL.ARRAY_BUFFER, b);
-		gl.bufferData(GL.ARRAY_BUFFER, tmp, GL.STATIC_DRAW);
-		gl.bindBuffer(GL.ARRAY_BUFFER, null);
+		var tmp = new Uint8Array(m.size * m.stride * 4);
+		gl.bufferData(GL.ARRAY_BUFFER, tmp, m.flags.has(Dynamic) ? GL.DYNAMIC_DRAW : GL.STATIC_DRAW);
 		#end
+		gl.bindBuffer(GL.ARRAY_BUFFER, null);
 		return { b : b, stride : m.stride };
 	}
 
 	override function allocIndexes( count : Int ) : IndexBuffer {
 		var b = gl.createBuffer();
-		#if js
 		gl.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, b);
+		#if js
 		gl.bufferData(GL.ELEMENT_ARRAY_BUFFER, count * 2, GL.STATIC_DRAW);
-		gl.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, null);
 		#else
 		var tmp = new Uint16Array(count);
-		gl.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, b);
 		gl.bufferData(GL.ELEMENT_ARRAY_BUFFER, tmp, GL.STATIC_DRAW);
-		gl.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, null);
 		#end
+		gl.bindBuffer(GL.ELEMENT_ARRAY_BUFFER, null);
 		return b;
 	}
 
@@ -394,11 +415,17 @@ class GlDriver extends Driver {
 	}
 
 	override function uploadTextureBitmap( t : h3d.mat.Texture, bmp : hxd.BitmapData, mipLevel : Int, side : Int ) {
+		#if nme
+		var pixels = bmp.getPixels();
+		uploadTexturePixels(t, pixels, mipLevel, side);
+		pixels.dispose();
+		#else
 		var img = bmp.toNative();
 		gl.bindTexture(GL.TEXTURE_2D, t.t.t);
 		gl.texImage2D(GL.TEXTURE_2D, mipLevel, GL.RGBA, GL.RGBA, GL.UNSIGNED_BYTE, img.getImageData(0, 0, bmp.width, bmp.height));
 		if( t.flags.has(MipMapped) ) gl.generateMipmap(GL.TEXTURE_2D);
 		gl.bindTexture(GL.TEXTURE_2D, null);
+		#end
 	}
 
 	override function uploadTexturePixels( t : h3d.mat.Texture, pixels : hxd.Pixels, mipLevel : Int, side : Int ) {
@@ -508,13 +535,17 @@ class GlDriver extends Driver {
 	}
 
 	override function isDisposed() {
+		#if (nme || openfl)
+		return false;
+		#else
 		return gl.isContextLost();
+		#end
 	}
 
 	override function setRenderTarget( tex : h3d.mat.Texture ) {
 		if( tex == null ) {
 			gl.bindFramebuffer(GL.FRAMEBUFFER, null);
-			gl.viewport(0, 0, canvas.width, canvas.height);
+			gl.viewport(0, 0, bufferWidth, bufferHeight);
 			hasTargetFlip = false;
 			return;
 		}

+ 5 - 3
hxd/BitmapData.hx

@@ -5,13 +5,15 @@ private typedef InnerData =
 	flash.display.BitmapData
 #elseif js
 	js.html.CanvasRenderingContext2D
+#elseif nme
+	nme.display.BitmapData
 #else
 	Int
 #end;
 
 abstract BitmapData(InnerData) {
 
-	#if flash
+	#if (flash || nme || openfl)
 	static var tmpRect = new flash.geom.Rectangle();
 	static var tmpPoint = new flash.geom.Point();
 	static var tmpMatrix = new flash.geom.Matrix();
@@ -21,7 +23,7 @@ abstract BitmapData(InnerData) {
 	public var height(get, never) : Int;
 
 	public inline function new(width:Int, height:Int) {
-		#if (flash||openfl)
+		#if (flash||openfl||nme)
 		this = new flash.display.BitmapData(width, height, true, 0);
 		#else
 		var canvas = js.Browser.document.createCanvasElement();
@@ -40,7 +42,7 @@ abstract BitmapData(InnerData) {
 	}
 
 	public function fill( x : Int, y : Int, width : Int, height : Int, color : Int ) {
-		#if flash
+		#if (flash || openfl || nme)
 		var r = tmpRect;
 		r.x = x;
 		r.y = y;

+ 4 - 4
hxd/Stage.hx

@@ -2,7 +2,7 @@ package hxd;
 
 class Stage {
 
-	#if (flash || openfl)
+	#if (flash || openfl || nme)
 	var stage : flash.display.Stage;
 	var fsDelayed : Bool;
 	#end
@@ -148,7 +148,7 @@ class Stage {
 		return inst;
 	}
 
-#if (flash || openfl)
+#if (flash || openfl || nme)
 
 	inline function get_mouseX() {
 		return Std.int(stage.mouseX);
@@ -167,7 +167,7 @@ class Stage {
 	}
 
 	inline function get_mouseLock() {
-		#if openfl
+		#if cpp
 		return false;
 		#else
 		return stage.mouseLock;
@@ -175,7 +175,7 @@ class Stage {
 	}
 
 	inline function set_mouseLock(v) {
-		#if openfl
+		#if cpp
 		return false;
 		#else
 		return stage.mouseLock = v;

+ 24 - 2
hxd/System.hx

@@ -20,15 +20,23 @@ class System {
 
 	public static var screenDPI(get,never) : Float;
 
-	#if flash
+	#if (flash || nme || openfl)
 
 	static function get_isWindowed() {
+		#if cpp
+		return true;
+		#else
 		var p = flash.system.Capabilities.playerType;
 		return p == "ActiveX" || p == "PlugIn" || p == "StandAlone" || p == "Desktop";
+		#end
 	}
 
 	static function get_isTouch() {
+		#if cpp
+		return false;
+		#else
 		return flash.system.Capabilities.touchscreenType == flash.system.TouchscreenType.FINGER;
+		#end
 	}
 
 	static function get_width() {
@@ -42,7 +50,11 @@ class System {
 	}
 
 	static function get_isAndroid() {
+		#if cpp
+		return #if android true #else false #end;
+		#else
 		return flash.system.Capabilities.manufacturer.indexOf('Android') != -1;
+		#end
 	}
 
 	static function get_screenDPI() {
@@ -62,21 +74,28 @@ class System {
 		}
 	}
 
+	#if flash
 	static function isAir() {
 		return flash.system.Capabilities.playerType == "Desktop";
 	}
+	#end
 
 	public static function exit() {
+		#if flash
 		if( isAir() ) {
 			var d : Dynamic = flash.Lib.current.loaderInfo.applicationDomain.getDefinition("flash.desktop.NativeApplication");
 			Reflect.field(Reflect.field(d,"nativeApplication"),"exit")();
 		} else
+		#end
 			flash.system.System.exit(0);
 	}
 
 	public static var setCursor = setNativeCursor;
 
 	public static function setNativeCursor( c : Cursor ) {
+		#if cpp
+		// TODO
+		#else
 		flash.ui.Mouse.cursor = switch( c ) {
 		case Default: "auto";
 		case Button: "button";
@@ -84,7 +103,7 @@ class System {
 		case TextInput: "ibeam";
 		case Hide: "auto";
 		case Custom(frames, speed, offsetX, offsetY):
-			#if openfl
+			#if cpp
 				throw "not supported on openFL for now";
 			#else
 				var customCursor = new flash.ui.MouseCursorData();
@@ -97,6 +116,7 @@ class System {
 				"custom";
 			#end
 		}
+		#end
 		if( c == Hide ) flash.ui.Mouse.hide() else flash.ui.Mouse.show();
 	}
 
@@ -112,6 +132,7 @@ class System {
 		if( CACHED_NAME != null )
 			return CACHED_NAME;
 		var name;
+		#if flash
 		if( isAndroid && isAir() ) {
 			try {
 				var f : Dynamic = Type.createInstance(flash.Lib.current.loaderInfo.applicationDomain.getDefinition("flash.filesystem.File"), ["/system/build.prop"]);
@@ -123,6 +144,7 @@ class System {
 				name = "Android";
 			}
 		} else
+		#end
 			name = "PC";
 		CACHED_NAME = name;
 		return name;