Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #629 from MattTuttle/haxe4
Browse files Browse the repository at this point in the history
Haxe 4 fixes
  • Loading branch information
matrefeytontias committed Dec 2, 2019
2 parents 816b63e + f175015 commit cf5e9a2
Show file tree
Hide file tree
Showing 14 changed files with 140 additions and 25 deletions.
6 changes: 5 additions & 1 deletion backend/lime/haxepunk/App.hx
Expand Up @@ -15,9 +15,13 @@ class App extends haxepunk._internal.FlashApp
#if (openfl >= "8.0.0")
// use the RenderEvent API
addEventListener(openfl.events.RenderEvent.RENDER_OPENGL, function(event) {
#if (openfl >= "8.9.2")
var renderer:openfl._internal.renderer.context3D.Context3DRenderer = cast event.renderer;
#else
var renderer:openfl.display.OpenGLRenderer = cast event.renderer;
haxepunk.graphics.hardware.opengl.GLInternal.renderer = renderer;
haxepunk.graphics.hardware.opengl.GLInternal.gl = renderer.gl;
#end
haxepunk.graphics.hardware.opengl.GLInternal.renderer = renderer;
engine.onRender();
});
#else
Expand Down
16 changes: 13 additions & 3 deletions backend/lime/haxepunk/graphics/hardware/opengl/GLInternal.hx
Expand Up @@ -6,13 +6,17 @@ import lime.graphics.WebGLRenderContext;

class GLInternal
{
#if (openfl >= "8.0.0")
#if (openfl >= "8.9.2")
public static var renderer:openfl._internal.renderer.context3D.Context3DRenderer;
#elseif (openfl >= "8.0.0")
public static var renderer:openfl.display.OpenGLRenderer;
public static var gl:WebGLRenderContext;
#end

@:access(openfl.display.Stage)
#if (openfl < "8.9.2")
@:access(openfl.display.OpenGLRenderer.__context3D)
#end
@:access(openfl.display.Stage)
@:access(openfl.display3D.textures.TextureBase.__getTexture)
@:allow(haxepunk.graphics.hardware.opengl.GLUtils)
static function bindTexture(texture:Texture)
Expand All @@ -21,7 +25,13 @@ class GLInternal
var renderer = @:privateAccess (HXP.app.stage.__renderer).renderSession;
#end
var bmd:BitmapData = cast texture;
GL.bindTexture(GL.TEXTURE_2D, bmd.getTexture(renderer.__context3D).__getTexture());
GL.bindTexture(GL.TEXTURE_2D, bmd.getTexture(
#if (openfl < "8.9.2")
renderer.__context3D
#else
renderer.context3D
#end
).__getTexture());
}

public static inline function invalid(object:Dynamic)
Expand Down
43 changes: 43 additions & 0 deletions haxepunk/Signal.hx
Expand Up @@ -66,6 +66,48 @@ class Signal2<A, B> extends Signal<A->B->Void>
/**
* A collection of named signals, which can be accessed as attributes.
*/
#if haxe4
abstract Signals(Map<String, Signal0>) from Map<String, Signal0> {

public function new()
{
this = new Map<String, Signal0>();
}

@:op([]) inline public function arrayRead(field:String)
{
return this[field];
}

@:op([]) inline public function arrayWrite(field:String, signal:Signal0)
{
return this[field] = signal;
}

@:op(a.b) inline public function fieldRead(field:String)
{
return resolve( field);
}

@:op(a.b) inline public function fieldWrite(field:String, signal:Signal0)
{
return this[field] = signal;
}

public inline function exists(field:String) return this.exists(field);

public inline function invoke(field:String)
{
if (exists(field)) this[field].invoke();
}

public function resolve(field:String)
{
if (!exists(field)) this[field] = new Signal0();
return this[field];
}
}
#else
class Signals implements Dynamic<Signal0>
{
var signals:Map<String, Signal0> = new Map();
Expand All @@ -85,3 +127,4 @@ class Signals implements Dynamic<Signal0>
return signals[field];
}
}
#end
4 changes: 2 additions & 2 deletions haxepunk/graphics/Image.hx
Expand Up @@ -216,14 +216,14 @@ class Image extends Graphic
/**
* The scaled width of the image.
*/
public var scaledWidth(get, set_scaledWidth):Float;
public var scaledWidth(get, set):Float;
inline function get_scaledWidth():Float return width * scaleX * scale;
inline function set_scaledWidth(w:Float):Float return scaleX = w / scale / width;

/**
* The scaled height of the image.
*/
public var scaledHeight(get, set_scaledHeight):Float;
public var scaledHeight(get, set):Float;
inline function get_scaledHeight():Float return height * scaleY * scale;
inline function set_scaledHeight(h:Float):Float return scaleY = h / scale / height;

Expand Down
8 changes: 6 additions & 2 deletions haxepunk/graphics/hardware/RenderBuffer.hx
@@ -1,8 +1,12 @@
package haxepunk.graphics.hardware;

#if js
#if haxe4
import js.lib.Int32Array;
#else
import js.html.Int32Array;
#end
#end
import haxepunk.graphics.hardware.opengl.GL;
import haxepunk.graphics.hardware.opengl.GLBuffer;
import haxepunk.graphics.hardware.opengl.GLUtils;
Expand Down Expand Up @@ -45,7 +49,7 @@ class RenderBuffer
{
glBuffer = GL.createBuffer();
}

function bufferData(target, size, srcData, usage)
{
#if (html5 && lime >= "5.0.0")
Expand All @@ -56,7 +60,7 @@ class RenderBuffer
GL.bufferData(target, srcData, usage);
#end
}

public function ensureSize(triangles:Int, floatsPerTriangle:Int)
{
if (GLUtils.invalid(glBuffer))
Expand Down
8 changes: 8 additions & 0 deletions haxepunk/graphics/text/BitmapFontAtlas.hx
@@ -1,6 +1,10 @@
package haxepunk.graphics.text;

#if !haxe4
import haxe.xml.Fast;
#else
import haxe.xml.Access as Fast;
#end
import haxepunk.HXP;
import haxepunk.assets.AssetLoader;
import haxepunk.graphics.atlas.AtlasDataType;
Expand Down Expand Up @@ -84,7 +88,11 @@ class BitmapFontAtlas extends TextureAtlas implements IBitmapFont
}
else if (char.has.id)
{
#if haxe4
glyph = cast(char.att.id, Utf8String);
#else
glyph = Utf8String.fromCharCode(Std.parseInt(char.att.id));
#end
}
if (glyph == null) throw '"$file" is not a valid .fnt file!';

Expand Down
2 changes: 1 addition & 1 deletion haxepunk/graphics/text/BitmapText.hx
Expand Up @@ -30,7 +30,7 @@ typedef FormatTagOptions =
}

@:enum
abstract AlignType(Int)
abstract AlignType(Int) to Int
{
var Left = 0;
var Center = 1;
Expand Down
6 changes: 4 additions & 2 deletions haxepunk/input/Input.hx
Expand Up @@ -2,7 +2,9 @@ package haxepunk.input;

import haxepunk.HXP;
import haxepunk.Signal.Signals;
#if cpp
#if ((cpp || neko) && haxe4)
import sys.thread.Deque;
#elseif cpp
import cpp.vm.Deque;
#elseif neko
import neko.vm.Deque;
Expand All @@ -19,7 +21,7 @@ class Input
/**
* Array of currently active InputHandlers.
*/
public static var handlers:Array<InputHandler> = [Key, Mouse];
public static var handlers:Array<InputHandler> = [Key.Handler, Mouse];

/**
* Returns true if the device supports multi touch
Expand Down
34 changes: 29 additions & 5 deletions haxepunk/input/Key.hx
Expand Up @@ -184,7 +184,7 @@ abstract Key(Int) from Int to Int
}
}

public static function checkInput(input:InputType)
public static function checkInput(input:InputType):Bool
{
if (_control.exists(input))
{
Expand All @@ -196,7 +196,7 @@ abstract Key(Int) from Int to Int
return false;
}

public static function pressedInput(input:InputType)
public static function pressedInput(input:InputType):Bool
{
if (_control.exists(input))
{
Expand All @@ -208,7 +208,7 @@ abstract Key(Int) from Int to Int
return false;
}

public static function releasedInput(input:InputType)
public static function releasedInput(input:InputType):Bool
{
if (_control.exists(input))
{
Expand All @@ -235,8 +235,6 @@ abstract Key(Int) from Int to Int
return (key < 0 ? _releaseNum != 0 : HXP.indexOf(_release, key) >= 0);
}

public static function update() {}

public static function postUpdate()
{
while (_pressNum > 0) _press[--_pressNum] = -1;
Expand Down Expand Up @@ -304,3 +302,29 @@ abstract Key(Int) from Int to Int
static var _control:Map<InputType, Array<Key>> = new Map();
static var _keyMap:Map<Key, Array<InputType>> = new Map();
}

// can't use abstract for the handler so we pass it through this class
class Handler
{
public static function update() {}

public static function postUpdate()
{
Key.postUpdate();
}

public static function checkInput(input:InputType):Bool
{
return Key.checkInput(input);
}

public static function pressedInput(input:InputType):Bool
{
return Key.pressedInput(input);
}

public static function releasedInput(input:InputType):Bool
{
return Key.releasedInput(input);
}
}
4 changes: 4 additions & 0 deletions haxepunk/masks/Grid.hx
Expand Up @@ -346,7 +346,11 @@ class Grid extends Hitbox
{
_point.x = _parent.x + _x - _parent.originX;
_point.y = _parent.y + _y - _parent.originY;
#if haxe4
if (Std.downcast(other, Imagemask) != null) // 'other' inherits from Imagemask
#else
if (Std.instance(other, Imagemask) != null) // 'other' inherits from Imagemask
#end
{
_rect = cast(other, Imagemask).getBounds();
_rect.x += other._parent.x;
Expand Down
4 changes: 4 additions & 0 deletions haxepunk/masks/Imagemask.hx
Expand Up @@ -132,7 +132,11 @@ class Imagemask extends Pixelmask
rect.x += _parent.x;
rect.y += _parent.y;

#if haxe4
if (Std.downcast(other, Imagemask) != null) // 'other' inherits from Imagemask
#else
if (Std.instance(other, Imagemask) != null) // 'other' inherits from Imagemask
#end
{
_rect = cast(other, Imagemask).getBounds();
_rect.x += other._parent.x;
Expand Down
20 changes: 12 additions & 8 deletions haxepunk/masks/Pixelmask.hx
Expand Up @@ -7,6 +7,14 @@ import haxepunk.graphics.hardware.Texture;
import haxepunk.math.Rectangle;
import haxepunk.math.Vector2;

abstract PixelmaskSource(Texture) from Texture to Texture
{
@:dox(hide) @:from public static inline function fromString(source:String):PixelmaskSource
{
return AssetCache.global.getTexture(source);
}
}

/**
* A bitmap mask used for pixel-perfect collision.
*/
Expand All @@ -23,19 +31,15 @@ class Pixelmask extends Hitbox
* @param x X offset of the mask.
* @param y Y offset of the mask.
*/
public function new(source:Dynamic, x:Int = 0, y:Int = 0)
public function new(source:PixelmaskSource, x:Int = 0, y:Int = 0)
{
super();

// fetch mask data
if (Std.is(source, Texture))
_data = source;
else
_data = AssetCache.global.getTexture(source);

if (_data == null)
if (source == null)
throw "Invalid Pixelmask source image.";

_data = source;

threshold = 1;

_rect = HXP.rect;
Expand Down
2 changes: 1 addition & 1 deletion haxepunk/math/MathUtil.hx
Expand Up @@ -6,7 +6,7 @@ package haxepunk.math;
*/
class MathUtil
{
public static var NUMBER_MAX_VALUE(get_NUMBER_MAX_VALUE,never):Float;
public static var NUMBER_MAX_VALUE(get,never):Float;
public static inline function get_NUMBER_MAX_VALUE():Float { return 179 * Math.pow(10, 306); } // 1.79e+308

// Used for rad-to-deg and deg-to-rad conversion.
Expand Down
8 changes: 8 additions & 0 deletions haxepunk/utils/Utf8String.hx
@@ -1,5 +1,11 @@
package haxepunk.utils;

#if haxe4

typedef Utf8String = UnicodeString;

#else

import haxe.Utf8;

/**
Expand Down Expand Up @@ -48,3 +54,5 @@ abstract Utf8String(String) from String to String
return buf.toString();
}
}

#end

0 comments on commit cf5e9a2

Please sign in to comment.