Переглянути джерело

hxsdl -> hlsdl, started hldx support

Nicolas Cannasse 8 роки тому
батько
коміт
9385d8d8d9

+ 3 - 1
h3d/Engine.hx

@@ -53,10 +53,12 @@ class Engine {
 		realFps = hxd.System.getDefaultFrameRate();
 		realFps = hxd.System.getDefaultFrameRate();
 		lastTime = haxe.Timer.stamp();
 		lastTime = haxe.Timer.stamp();
 		stage.addResizeEvent(onStageResize);
 		stage.addResizeEvent(onStageResize);
-		#if (js || cpp || hxsdl || psgl)
+		#if (js || cpp || hlsdl || psgl)
 		driver = new h3d.impl.GlDriver(antiAlias);
 		driver = new h3d.impl.GlDriver(antiAlias);
 		#elseif flash
 		#elseif flash
 		driver = new h3d.impl.Stage3dDriver(antiAlias);
 		driver = new h3d.impl.Stage3dDriver(antiAlias);
+		#elseif hldx
+		driver = new h3d.impl.DirectXDriver();
 		#else
 		#else
 		#if sys Sys.println #else trace #end("No output driver available.");
 		#if sys Sys.println #else trace #end("No output driver available.");
 		driver = new h3d.impl.LogDriver(new h3d.impl.NullDriver());
 		driver = new h3d.impl.LogDriver(new h3d.impl.NullDriver());

+ 38 - 0
h3d/impl/DirectXDriver.hx

@@ -0,0 +1,38 @@
+package h3d.impl;
+
+import h3d.impl.Driver;
+import dx.Driver;
+
+class DirectXDriver extends h3d.impl.Driver {
+
+	var driver : DriverInstance;
+
+	public function new() {
+		driver = Driver.create(@:privateAccess dx.Window.windows[0]);
+		if( driver == null ) throw "Failed to initialize DirectX driver";
+	}
+
+	override function isDisposed() {
+		return false;
+	}
+
+	override function init( onCreate : Bool -> Void, forceSoftware = false ) {
+		haxe.Timer.delay(onCreate.bind(false), 1);
+	}
+
+	override function clear(?color:h3d.Vector, ?depth:Float, ?stencil:Int) {
+		if( color != null )
+			Driver.clearColor(color.r, color.g, color.b, color.a);
+	}
+
+	override function getDriverName(details:Bool) {
+		var desc = "DirectX" + Driver.getSupportedVersion();
+		if( details ) desc += " " + Driver.getDeviceName();
+		return desc;
+	}
+
+	override public function present() {
+		Driver.present();
+	}
+
+}

+ 1 - 1
h3d/impl/Driver.hx

@@ -24,7 +24,7 @@ typedef VertexBuffer = { b : lime.graphics.opengl.GLBuffer, stride : Int };
 typedef Texture = { t : lime.graphics.opengl.GLTexture, width : Int, height : Int, internalFmt : Int, pixelFmt : Int, bits : Int };
 typedef Texture = { t : lime.graphics.opengl.GLTexture, width : Int, height : Int, internalFmt : Int, pixelFmt : Int, bits : Int };
 typedef DepthBuffer = { r : lime.graphics.opengl.GLRenderbuffer };
 typedef DepthBuffer = { r : lime.graphics.opengl.GLRenderbuffer };
 typedef Query = {};
 typedef Query = {};
-#elseif hxsdl
+#elseif hlsdl
 typedef IndexBuffer = sdl.GL.Buffer;
 typedef IndexBuffer = sdl.GL.Buffer;
 typedef VertexBuffer = { b : sdl.GL.Buffer, stride : Int };
 typedef VertexBuffer = { b : sdl.GL.Buffer, stride : Int };
 typedef Texture = { t : sdl.GL.Texture, width : Int, height : Int, internalFmt : Int, pixelFmt : Int, bits : Int };
 typedef Texture = { t : sdl.GL.Texture, width : Int, height : Int, internalFmt : Int, pixelFmt : Int, bits : Int };

+ 10 - 10
h3d/impl/GlDriver.hx

@@ -4,7 +4,7 @@ import h3d.mat.Pass;
 import h3d.mat.Stencil;
 import h3d.mat.Stencil;
 import h3d.mat.Data;
 import h3d.mat.Data;
 
 
-#if (js||cpp||hxsdl||psgl)
+#if (js||cpp||hlsdl||psgl)
 
 
 #if js
 #if js
 import js.html.Uint16Array;
 import js.html.Uint16Array;
@@ -33,7 +33,7 @@ private typedef Framebuffer = nme.gl.Framebuffer;
 private typedef Uint16Array = nme.utils.Int16Array;
 private typedef Uint16Array = nme.utils.Int16Array;
 private typedef Uint8Array = nme.utils.UInt8Array;
 private typedef Uint8Array = nme.utils.UInt8Array;
 private typedef Float32Array = nme.utils.Float32Array;
 private typedef Float32Array = nme.utils.Float32Array;
-#elseif hxsdl
+#elseif hlsdl
 import sdl.GL;
 import sdl.GL;
 private typedef Uniform = sdl.GL.Uniform;
 private typedef Uniform = sdl.GL.Uniform;
 private typedef Program = sdl.GL.Program;
 private typedef Program = sdl.GL.Program;
@@ -92,7 +92,7 @@ private class CompiledProgram {
 }
 }
 
 
 @:access(h3d.impl.Shader)
 @:access(h3d.impl.Shader)
-#if (cpp||hxsdl||psgl)
+#if (cpp||hlsdl||psgl)
 @:build(h3d.impl.MacroHelper.replaceGL())
 @:build(h3d.impl.MacroHelper.replaceGL())
 #end
 #end
 class GlDriver extends Driver {
 class GlDriver extends Driver {
@@ -103,7 +103,7 @@ class GlDriver extends Driver {
 	public var gl : js.html.webgl.RenderingContext;
 	public var gl : js.html.webgl.RenderingContext;
 	#end
 	#end
 
 
-	#if (hxsdl||psgl)
+	#if (hlsdl||psgl)
 	var commonVA : VertexArray;
 	var commonVA : VertexArray;
 	#end
 	#end
 
 
@@ -145,7 +145,7 @@ class GlDriver extends Driver {
 		curAttribs = 0;
 		curAttribs = 0;
 		curMatBits = -1;
 		curMatBits = -1;
 		defStencil = new Stencil();
 		defStencil = new Stencil();
-		#if hxsdl
+		#if hlsdl
 		var v : String = gl.getParameter(GL.VERSION);
 		var v : String = gl.getParameter(GL.VERSION);
 		if( v.indexOf("ES") < 0 ){
 		if( v.indexOf("ES") < 0 ){
 			commonVA = gl.createVertexArray();
 			commonVA = gl.createVertexArray();
@@ -248,7 +248,7 @@ class GlDriver extends Driver {
 			p.vertex = compileShader(glout,shader.vertex);
 			p.vertex = compileShader(glout,shader.vertex);
 			p.fragment = compileShader(glout,shader.fragment);
 			p.fragment = compileShader(glout,shader.fragment);
 			p.p = gl.createProgram();
 			p.p = gl.createProgram();
-			#if hxsdl
+			#if hlsdl
 			if( !glout.glES ) {
 			if( !glout.glES ) {
 				var outCount = 0;
 				var outCount = 0;
 				for( v in shader.fragment.data.vars )
 				for( v in shader.fragment.data.vars )
@@ -275,7 +275,7 @@ class GlDriver extends Driver {
 				#if js
 				#if js
 				gl.deleteProgram(p.p);
 				gl.deleteProgram(p.p);
 				#end
 				#end
-				#if hxsdl
+				#if hlsdl
 				/*
 				/*
 					Tentative patch on some driver that report an higher shader version that it's allowed to use.
 					Tentative patch on some driver that report an higher shader version that it's allowed to use.
 				*/
 				*/
@@ -747,7 +747,7 @@ class GlDriver extends Driver {
 	}
 	}
 
 
 	override function uploadTextureBitmap( t : h3d.mat.Texture, bmp : hxd.BitmapData, mipLevel : Int, side : Int ) {
 	override function uploadTextureBitmap( t : h3d.mat.Texture, bmp : hxd.BitmapData, mipLevel : Int, side : Int ) {
-	#if (nme || hxsdl || psgl || openfl || lime)
+	#if (nme || hlsdl || psgl || openfl || lime)
 		var pixels = bmp.getPixels();
 		var pixels = bmp.getPixels();
 		uploadTexturePixels(t, pixels, mipLevel, side);
 		uploadTexturePixels(t, pixels, mipLevel, side);
 		pixels.dispose();
 		pixels.dispose();
@@ -768,7 +768,7 @@ class GlDriver extends Driver {
 	#end
 	#end
 	}
 	}
 
 
-	#if !(hxsdl || psgl)
+	#if !(hlsdl || psgl)
 	inline static function bytesToUint8Array( b : haxe.io.Bytes ) : Uint8Array {
 	inline static function bytesToUint8Array( b : haxe.io.Bytes ) : Uint8Array {
 		#if (lime && !js)
 		#if (lime && !js)
 		return new Uint8Array(b);
 		return new Uint8Array(b);
@@ -1080,7 +1080,7 @@ class GlDriver extends Driver {
 
 
 	override function hasFeature( f : Feature ) : Bool {
 	override function hasFeature( f : Feature ) : Bool {
 		return switch( f ) {
 		return switch( f ) {
-		#if (hxsdl || psgl)
+		#if (hlsdl || psgl)
 		case StandardDerivatives, FloatTextures, MultipleRenderTargets, Queries:
 		case StandardDerivatives, FloatTextures, MultipleRenderTargets, Queries:
 			true; // runtime extension detect required ?
 			true; // runtime extension detect required ?
 		#else
 		#else

+ 1 - 1
h3d/mat/Texture.hx

@@ -19,7 +19,7 @@ class Texture {
 	/**
 	/**
 		Tells if the Driver requires y-flipping the texture pixels before uploading.
 		Tells if the Driver requires y-flipping the texture pixels before uploading.
 	**/
 	**/
-	public static inline var nativeFlip = 	#if (hxsdl) true
+	public static inline var nativeFlip = 	#if (hlsdl) true
 											#elseif (openfl) false
 											#elseif (openfl) false
 											#elseif (lime && (cpp || neko || nodejs)) true
 											#elseif (lime && (cpp || neko || nodejs)) true
 											#else false #end;
 											#else false #end;

+ 2 - 0
hxd/Cursor.hx

@@ -18,6 +18,8 @@ class CustomCursor {
 	var offsetY : Int;
 	var offsetY : Int;
 	#if hlsdl
 	#if hlsdl
 	var alloc : sdl.Cursor;
 	var alloc : sdl.Cursor;
+	#elseif hldx
+	var alloc : dx.Cursor;
 	#elseif flash
 	#elseif flash
 	static var UID = 0;
 	static var UID = 0;
 	var name : String;
 	var name : String;

+ 8 - 3
hxd/Stage.hl.hx

@@ -14,9 +14,12 @@ class Stage {
 	public var mouseLock(get, set) : Bool;
 	public var mouseLock(get, set) : Bool;
 	public var vsync(get, set) : Bool;
 	public var vsync(get, set) : Bool;
 
 
-	#if !psgl
+	#if hlsdl
 	var window : sdl.Window;
 	var window : sdl.Window;
 	var fullScreenMode : sdl.Window.DisplayMode = Borderless;
 	var fullScreenMode : sdl.Window.DisplayMode = Borderless;
+	#elseif hldx
+	var window : dx.Window;
+	var fullScreenMode : dx.Window.DisplayMode = Borderless;
 	#end
 	#end
 	var windowWidth = 800;
 	var windowWidth = 800;
 	var windowHeight = 600;
 	var windowHeight = 600;
@@ -31,8 +34,10 @@ class Stage {
 		this.windowHeight = height;
 		this.windowHeight = height;
 		eventTargets = new List();
 		eventTargets = new List();
 		resizeEvents = new List();
 		resizeEvents = new List();
-		#if !psgl
+		#if hlsdl
 		window = new sdl.Window(title, width, height);
 		window = new sdl.Window(title, width, height);
+		#elseif hldx
+		window = new dx.Window(title, width, height);
 		#end
 		#end
 	}
 	}
 
 
@@ -111,7 +116,7 @@ class Stage {
 		return false;
 		return false;
 	}
 	}
 
 
-	#if psgl
+	#if (psgl || hldx)
 
 
 	function get_vsync() : Bool return true;
 	function get_vsync() : Bool return true;
 
 

+ 52 - 19
hxd/System.hl.hx

@@ -1,5 +1,11 @@
 package hxd;
 package hxd;
 
 
+#if hlsdl
+import sdl.Cursor;
+#elseif hldx
+import dx.Cursor;
+#end
+
 enum Platform {
 enum Platform {
 	IOS;
 	IOS;
 	Android;
 	Android;
@@ -28,7 +34,7 @@ class System {
 	static var loopFunc : Void -> Void;
 	static var loopFunc : Void -> Void;
 
 
 	// -- HL
 	// -- HL
-	static var currentNativeCursor : Cursor = Default;
+	static var currentNativeCursor : hxd.Cursor = Default;
 	static var cursorVisible = true;
 	static var cursorVisible = true;
 
 
 	public static function getCurrentLoop() : Void -> Void {
 	public static function getCurrentLoop() : Void -> Void {
@@ -54,8 +60,6 @@ class System {
 		@:privateAccess Stage.inst = new Stage("", psgl.Api.width, psgl.Api.height);
 		@:privateAccess Stage.inst = new Stage("", psgl.Api.width, psgl.Api.height);
 		init();
 		init();
 		#else
 		#else
-		sdl.Sdl.tick();
-		sdl.Sdl.init();
 		var width = 800;
 		var width = 800;
 		var height = 600;
 		var height = 600;
 		var size = haxe.macro.Compiler.getDefine("windowSize");
 		var size = haxe.macro.Compiler.getDefine("windowSize");
@@ -67,34 +71,42 @@ class System {
 			width = Std.parseInt(p[0]);
 			width = Std.parseInt(p[0]);
 			height = Std.parseInt(p[1]);
 			height = Std.parseInt(p[1]);
 		}
 		}
-		@:privateAccess Stage.initChars();
-		@:privateAccess Stage.inst = new Stage(title, width, height);
-		init();
-		sdl.Sdl.defaultEventHandler = @:privateAccess Stage.inst.onEvent;
+		#if hlsdl
+			sdl.Sdl.tick();
+			sdl.Sdl.init();
+			@:privateAccess Stage.initChars();
+			@:privateAccess Stage.inst = new Stage(title, width, height);
+			init();
+			sdl.Sdl.defaultEventHandler = @:privateAccess Stage.inst.onEvent;
+		#elseif hldx
+			@:privateAccess Stage.inst = new Stage(title, width, height);
+			init();
+		#end
+
 		#end
 		#end
 		haxe.MainLoop.add(mainLoop);
 		haxe.MainLoop.add(mainLoop);
 	}
 	}
 
 
-	public static function setNativeCursor( c : Cursor ) : Void {
+	public static function setNativeCursor( c : hxd.Cursor ) : Void {
 		#if !psgl
 		#if !psgl
 		if( c.equals(currentNativeCursor) )
 		if( c.equals(currentNativeCursor) )
 			return;
 			return;
 		currentNativeCursor = c;
 		currentNativeCursor = c;
 		if( c == Hide ) {
 		if( c == Hide ) {
 			cursorVisible = false;
 			cursorVisible = false;
-			sdl.Cursor.show(false);
+			Cursor.show(false);
 			return;
 			return;
 		}
 		}
-		var cur : sdl.Cursor;
+		var cur : Cursor;
 		switch( c ) {
 		switch( c ) {
 		case Default:
 		case Default:
-			cur = sdl.Cursor.createSystem(Arrow);
+			cur = Cursor.createSystem(Arrow);
 		case Button:
 		case Button:
-			cur = sdl.Cursor.createSystem(Hand);
+			cur = Cursor.createSystem(Hand);
 		case Move:
 		case Move:
 			throw "Cursor not supported";
 			throw "Cursor not supported";
 		case TextInput:
 		case TextInput:
-			cur = sdl.Cursor.createSystem(IBeam);
+			cur = Cursor.createSystem(IBeam);
 		case Hide:
 		case Hide:
 			throw "assert";
 			throw "assert";
 		case Custom(c):
 		case Custom(c):
@@ -102,9 +114,13 @@ class System {
 				if( c.frames.length > 1 ) throw "Animated cursor not supported";
 				if( c.frames.length > 1 ) throw "Animated cursor not supported";
 				var pixels = c.frames[0].getPixels();
 				var pixels = c.frames[0].getPixels();
 				pixels.convert(BGRA);
 				pixels.convert(BGRA);
+				#if hlsdl
 				var surf = sdl.Surface.fromBGRA(pixels.bytes, pixels.width, pixels.height);
 				var surf = sdl.Surface.fromBGRA(pixels.bytes, pixels.width, pixels.height);
 				c.alloc = sdl.Cursor.create(surf, c.offsetX, c.offsetY);
 				c.alloc = sdl.Cursor.create(surf, c.offsetX, c.offsetY);
 				surf.free();
 				surf.free();
+				#elseif hldx
+				throw "TODO";
+				#end
 				pixels.dispose();
 				pixels.dispose();
 			}
 			}
 			cur = c.alloc;
 			cur = c.alloc;
@@ -112,7 +128,7 @@ class System {
 		cur.set();
 		cur.set();
 		if( !cursorVisible ) {
 		if( !cursorVisible ) {
 			cursorVisible = true;
 			cursorVisible = true;
-			sdl.Cursor.show(true);
+			Cursor.show(true);
 		}
 		}
 		#end
 		#end
 	}
 	}
@@ -120,8 +136,10 @@ class System {
 	public static function getDeviceName() : String {
 	public static function getDeviceName() : String {
 		#if psgl
 		#if psgl
 		return psgl.Api.name;
 		return psgl.Api.name;
-		#else
+		#elseif hlsdl
 		return "PC/" + sdl.Sdl.getDevices()[0];
 		return "PC/" + sdl.Sdl.getDevices()[0];
+		#elseif hldx
+		return "PC/" + dx.Driver.getDeviceName();
 		#end
 		#end
 	}
 	}
 
 
@@ -162,9 +180,24 @@ class System {
 
 
 	// getters
 	// getters
 
 
-	static function get_width() : Int return #if psgl psgl.Api.width #else sdl.Sdl.getScreenWidth() #end;
-	static function get_height() : Int return #if psgl psgl.Api.height #else sdl.Sdl.getScreenHeight() #end;
-	static function get_platform() : Platform return #if psgl Console #else PC #end;
-	static function get_screenDPI() : Int return 72; // TODO : SDL ?
+	#if psgl
+	static function get_width() : Int return psgl.Api.width;
+	static function get_height() : Int return psgl.Api.height;
+	static function get_platform() : Platform return Console;
+	#elseif hldx
+	static function get_width() : Int return dx.Driver.getScreenWidth();
+	static function get_height() : Int return dx.Driver.getScreenHeight();
+	static function get_platform() : Platform return PC; // TODO : Xbox ?
+	#elseif hlsdl
+	static function get_width() : Int return sdl.Sdl.getScreenWidth();
+	static function get_height() : Int return sdl.Sdl.getScreenHeight();
+	static function get_platform() : Platform return PC; // TODO : Xbox ?
+	#else
+	static function get_width() : Int return 800;
+	static function get_height() : Int return 600;
+	static function get_platform() : Platform return PC;
+	#end
+
+	static function get_screenDPI() : Int return 72; // TODO
 
 
 }
 }

+ 2 - 26
hxd/snd/NativeChannel.hx

@@ -1,21 +1,6 @@
 package hxd.snd;
 package hxd.snd;
 
 
-#if hxsdl
-private class ChannelMapper extends sdl.SoundChannel {
-	var native : NativeChannel;
-	public function new(samples, native) {
-		super(samples);
-		this.native = native;
-	}
-	#if cpp
-	override function onSample( buf : cpp.Pointer<cpp.Float32>, len : Int ) {
-		var data : haxe.io.BytesData = [];
-		cpp.NativeArray.setUnmanagedData(data, buf.reinterpret(), len<<2);
-		@:privateAccess native.onSample(haxe.io.Float32Array.fromBytes(haxe.io.Bytes.ofData(data)));
-	}
-	#end
-}
-#elseif lime_openal
+#if lime_openal
 import lime.media.openal.AL;
 import lime.media.openal.AL;
 import lime.media.openal.ALBuffer;
 import lime.media.openal.ALBuffer;
 import lime.media.openal.ALSource;
 import lime.media.openal.ALSource;
@@ -51,7 +36,7 @@ private class ALChannel {
 	public function stop() {
 	public function stop() {
 		if ( src != null ){
 		if ( src != null ){
 			lime.app.Application.current.onUpdate.remove( onUpdate );
 			lime.app.Application.current.onUpdate.remove( onUpdate );
-			
+
 			AL.sourceStop(src);
 			AL.sourceStop(src);
 			AL.deleteSource(src);
 			AL.deleteSource(src);
 			AL.deleteBuffers(buffers);
 			AL.deleteBuffers(buffers);
@@ -118,8 +103,6 @@ class NativeChannel {
 	}
 	}
 	var sproc : js.html.audio.ScriptProcessorNode;
 	var sproc : js.html.audio.ScriptProcessorNode;
 	var tmpBuffer : haxe.io.Float32Array;
 	var tmpBuffer : haxe.io.Float32Array;
-	#elseif hxsdl
-	var channel : ChannelMapper;
 	#elseif lime_openal
 	#elseif lime_openal
 	var channel : ALChannel;
 	var channel : ALChannel;
 	#end
 	#end
@@ -138,8 +121,6 @@ class NativeChannel {
 		tmpBuffer = new haxe.io.Float32Array(bufferSamples * 2);
 		tmpBuffer = new haxe.io.Float32Array(bufferSamples * 2);
 		sproc.connect(ctx.destination);
 		sproc.connect(ctx.destination);
 		sproc.onaudioprocess = onJsSample;
 		sproc.onaudioprocess = onJsSample;
-		#elseif hxsdl
-		channel = new ChannelMapper(bufferSamples, this);
 		#elseif lime_openal
 		#elseif lime_openal
 		channel = new ALChannel(bufferSamples, this);
 		channel = new ALChannel(bufferSamples, this);
 		#end
 		#end
@@ -183,11 +164,6 @@ class NativeChannel {
 			sproc.disconnect();
 			sproc.disconnect();
 			sproc = null;
 			sproc = null;
 		}
 		}
-		#elseif hxsdl
-		if( channel != null ) {
-			channel.stop();
-			channel = null;
-		}
 		#elseif lime_openal
 		#elseif lime_openal
 		if( channel != null ) {
 		if( channel != null ) {
 			channel.stop();
 			channel.stop();

+ 6 - 3
samples/Base2D.hx

@@ -4,7 +4,7 @@ class Base2D extends hxd.App {
 	var tf : h2d.Text;
 	var tf : h2d.Text;
 
 
 	override function init() {
 	override function init() {
-
+/*
 		// creates a new sprite and put it at the center of the sceen
 		// creates a new sprite and put it at the center of the sceen
 		spr = new h2d.Sprite(s2d);
 		spr = new h2d.Sprite(s2d);
 		spr.x = Std.int(s2d.width / 2);
 		spr.x = Std.int(s2d.width / 2);
@@ -64,12 +64,14 @@ class Base2D extends hxd.App {
 
 
 		tf.y = 20;
 		tf.y = 20;
 		tf.x = 20;
 		tf.x = 20;
-		tf.scale(7);
+		tf.scale(7);*/
 	}
 	}
 
 
 	// if we the window has been resized
 	// if we the window has been resized
 	override function onResize() {
 	override function onResize() {
 
 
+		if( spr == null ) return;
+
 		// center our sprite
 		// center our sprite
 		spr.x = Std.int(s2d.width / 2);
 		spr.x = Std.int(s2d.width / 2);
 		spr.y = Std.int(s2d.height / 2);
 		spr.y = Std.int(s2d.height / 2);
@@ -80,10 +82,11 @@ class Base2D extends hxd.App {
 
 
 	override function update(dt:Float) {
 	override function update(dt:Float) {
 		// rotate our sprite every frame
 		// rotate our sprite every frame
-		spr.rotation += 0.01 * dt;
+		if( spr != null ) spr.rotation += 0.01 * dt;
 	}
 	}
 
 
 	static function main() {
 	static function main() {
+		trace(hxd.System.width + "x" + hxd.System.height + " " + hxd.System.getDeviceName());
 		hxd.Res.initEmbed();
 		hxd.Res.initEmbed();
 		new Base2D();
 		new Base2D();
 	}
 	}