Skip to content

Commit

Permalink
[haxe] Rewrite core to not use OpenFL classes. Needed for compatibili…
Browse files Browse the repository at this point in the history
…ty with Heaps engine.
  • Loading branch information
badlogic committed Sep 14, 2023
1 parent b5c5fb6 commit bb3bd3a
Show file tree
Hide file tree
Showing 74 changed files with 670 additions and 719 deletions.
2 changes: 1 addition & 1 deletion spine-haxe/example/src/BasicExample.hx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import starling.events.TouchEvent;
import starling.events.TouchPhase;

class BasicExample extends Scene {
var loadBinary = true;
var loadBinary = false;

public function load():Void {
var atlas = TextureAtlas.fromAssets("assets/raptor.atlas");
Expand Down
2 changes: 1 addition & 1 deletion spine-haxe/example/src/SequenceExample.hx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import starling.events.TouchEvent;
import starling.events.TouchPhase;

class SequenceExample extends Scene {
var loadBinary = true;
var loadBinary = false;

public function load():Void {
var atlas = TextureAtlas.fromAssets("assets/dragon.atlas");
Expand Down
40 changes: 28 additions & 12 deletions spine-haxe/spine-haxe/spine/BinaryInput.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,48 @@

package spine;

import openfl.utils.ByteArray;
import openfl.Vector;
import haxe.io.FPHelper;
import haxe.io.Bytes;

class BinaryInput {
private var bytes:ByteArray;
private var bytes:Bytes;
private var index:Int = 0;

public var strings:Vector<String> = new Vector<String>();
public var strings:Array<String> = new Array<String>();

public function new(bytes:ByteArray) {
public function new(bytes:Bytes) {
this.bytes = bytes;
}

public function readByte():Int {
return bytes.readByte();
var result = bytes.get(index++);
if ((result & 0x80) != 0) {
result |= 0xffffff00;
}
return result;
}

public function readUnsignedByte():Int {
return bytes.readUnsignedByte();
return bytes.get(index++);
}

public function readShort():Int {
return bytes.readShort();
var ch1 = readUnsignedByte();
var ch2 = readUnsignedByte();
var result = ((ch1 << 8) | ch2);
if ((result & 0x8000) != 0) {
result |= 0xFFFF0000;
}
return result;
}

public function readInt32():Int {
return bytes.readInt();
var ch1 = readUnsignedByte();
var ch2 = readUnsignedByte();
var ch3 = readUnsignedByte();
var ch4 = readUnsignedByte();
var result = (ch1 << 24) | (ch2 << 16) | (ch3 << 8) | ch4;
return result;
}

public function readInt(optimizePositive:Bool):Int {
Expand All @@ -80,8 +96,8 @@ class BinaryInput {
}

public function readStringRef():String {
var index:Int = readInt(true);
return index == 0 ? null : strings[index - 1];
var idx:Int = readInt(true);
return idx == 0 ? null : strings[idx - 1];
}

public function readString():String {
Expand Down Expand Up @@ -113,7 +129,7 @@ class BinaryInput {
}

public function readFloat():Float {
return bytes.readFloat();
return FPHelper.i32ToFloat(readInt32());
}

public function readBoolean():Bool {
Expand Down
4 changes: 1 addition & 3 deletions spine-haxe/spine-haxe/spine/BlendMode.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@

package spine;

import openfl.Vector;

class BlendMode {
public static var normal(default, never):BlendMode = new BlendMode(0, "normal");
public static var additive(default, never):BlendMode = new BlendMode(1, "additive");
public static var multiply(default, never):BlendMode = new BlendMode(2, "multiply");
public static var screen(default, never):BlendMode = new BlendMode(3, "screen");

public static var values(default, never):Vector<BlendMode> = Vector.ofArray([normal, additive, multiply, screen]);
public static var values(default, never):Array<BlendMode> = [normal, additive, multiply, screen];

public var ordinal(default, null):Int;
public var name(default, null):String;
Expand Down
12 changes: 5 additions & 7 deletions spine-haxe/spine-haxe/spine/Bone.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@

package spine;

import openfl.Vector;

class Bone implements Updatable {
static public var yDown:Bool = false;

private var _data:BoneData;
private var _skeleton:Skeleton;
private var _parent:Bone;
private var _children:Vector<Bone> = new Vector<Bone>();
private var _children:Array<Bone> = new Array<Bone>();

public var x:Float = 0;
public var y:Float = 0;
Expand Down Expand Up @@ -230,9 +228,9 @@ class Bone implements Updatable {
return _parent;
}

public var children(get, never):Vector<Bone>;
public var children(get, never):Array<Bone>;

private function get_children():Vector<Bone> {
private function get_children():Array<Bone> {
return _children;
}

Expand Down Expand Up @@ -308,15 +306,15 @@ class Bone implements Updatable {
}
}

public function worldToLocal(world:Vector<Float>):Void {
public function worldToLocal(world:Array<Float>):Void {
var a:Float = a, b:Float = b, c:Float = c, d:Float = d;
var invDet:Float = 1 / (a * d - b * c);
var x:Float = world[0] - worldX, y:Float = world[1] - worldY;
world[0] = (x * d * invDet - y * b * invDet);
world[1] = (y * a * invDet - x * c * invDet);
}

public function localToWorld(local:Vector<Float>):Void {
public function localToWorld(local:Array<Float>):Void {
var localX:Float = local[0], localY:Float = local[1];
local[0] = localX * a + localY * b + worldX;
local[1] = localX * c + localY * d + worldY;
Expand Down
6 changes: 2 additions & 4 deletions spine-haxe/spine-haxe/spine/IkConstraint.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,10 @@

package spine;

import openfl.Vector;

class IkConstraint implements Updatable {
private var _data:IkConstraintData;

public var bones:Vector<Bone>;
public var bones:Array<Bone>;
public var target:Bone;
public var bendDirection:Int = 0;
public var compress:Bool = false;
Expand All @@ -55,7 +53,7 @@ class IkConstraint implements Updatable {
compress = data.compress;
stretch = data.stretch;

bones = new Vector<Bone>();
bones = new Array<Bone>();
for (boneData in data.bones) {
bones.push(skeleton.findBone(boneData.name));
}
Expand Down
4 changes: 1 addition & 3 deletions spine-haxe/spine-haxe/spine/IkConstraintData.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@

package spine;

import openfl.Vector;

class IkConstraintData extends ConstraintData {
public var bones:Vector<BoneData> = new Vector<BoneData>();
public var bones:Array<BoneData> = new Array<BoneData>();
public var target:BoneData;
public var mix:Float = 1;
public var bendDirection:Int = 1;
Expand Down
55 changes: 27 additions & 28 deletions spine-haxe/spine-haxe/spine/PathConstraint.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@

package spine;

import openfl.Vector;
import spine.attachments.PathAttachment;

class PathConstraint implements Updatable {
Expand All @@ -39,7 +38,7 @@ class PathConstraint implements Updatable {
private static inline var epsilon:Float = 0.00001;

private var _data:PathConstraintData;
private var _bones:Vector<Bone>;
private var _bones:Array<Bone>;

public var target:Slot;
public var position:Float = 0;
Expand All @@ -48,12 +47,12 @@ class PathConstraint implements Updatable {
public var mixX:Float = 0;
public var mixY:Float = 0;

private var _spaces(default, never):Vector<Float> = new Vector<Float>();
private var _positions(default, never):Vector<Float> = new Vector<Float>();
private var _world(default, never):Vector<Float> = new Vector<Float>();
private var _curves(default, never):Vector<Float> = new Vector<Float>();
private var _lengths(default, never):Vector<Float> = new Vector<Float>();
private var _segments(default, never):Vector<Float> = new Vector<Float>(10, true);
private var _spaces(default, never):Array<Float> = new Array<Float>();
private var _positions(default, never):Array<Float> = new Array<Float>();
private var _world(default, never):Array<Float> = new Array<Float>();
private var _curves(default, never):Array<Float> = new Array<Float>();
private var _lengths(default, never):Array<Float> = new Array<Float>();
private var _segments(default, never):Array<Float> = new Array<Float>();

public var active:Bool = false;

Expand All @@ -63,7 +62,7 @@ class PathConstraint implements Updatable {
if (skeleton == null)
throw new SpineException("skeleton cannot be null.");
_data = data;
_bones = new Vector<Bone>();
_bones = new Array<Bone>();
for (boneData in data.bones) {
_bones.push(skeleton.findBone(boneData.name));
}
Expand Down Expand Up @@ -94,11 +93,11 @@ class PathConstraint implements Updatable {

var boneCount:Int = _bones.length;
var spacesCount:Int = fTangents ? boneCount : boneCount + 1;
var bones:Vector<Bone> = _bones;
_spaces.length = spacesCount;
var bones:Array<Bone> = _bones;
_spaces.resize(spacesCount);

if (fScale)
_lengths.length = boneCount;
_lengths.resize(boneCount);

var i:Int,
n:Int,
Expand Down Expand Up @@ -175,7 +174,7 @@ class PathConstraint implements Updatable {
}
}

var positions:Vector<Float> = computeWorldPositions(attachment, spacesCount, fTangents);
var positions:Array<Float> = computeWorldPositions(attachment, spacesCount, fTangents);
var boneX:Float = positions[0];
var boneY:Float = positions[1];
var offsetRotation:Float = data.offsetRotation;
Expand Down Expand Up @@ -253,18 +252,18 @@ class PathConstraint implements Updatable {
}
}

private function computeWorldPositions(path:PathAttachment, spacesCount:Int, tangents:Bool):Vector<Float> {
private function computeWorldPositions(path:PathAttachment, spacesCount:Int, tangents:Bool):Array<Float> {
var position:Float = this.position;
_positions.length = spacesCount * 3 + 2;
var out:Vector<Float> = _positions, world:Vector<Float>;
_positions.resize(spacesCount * 3 + 2);
var out:Array<Float> = _positions, world:Array<Float>;
var closed:Bool = path.closed;
var verticesLength:Int = path.worldVerticesLength;
var curveCount:Int = Std.int(verticesLength / 6);
var prevCurve:Int = NONE;
var multiplier:Float, i:Int;

if (!path.constantSpeed) {
var lengths:Vector<Float> = path.lengths;
var lengths:Array<Float> = path.lengths;
curveCount -= closed ? 1 : 2;
var pathLength:Float = lengths[curveCount];
if (data.positionMode == PositionMode.percent)
Expand All @@ -278,7 +277,7 @@ class PathConstraint implements Updatable {
multiplier = 1;
}

_world.length = 8;
_world.resize(8);
world = _world;
var i:Int = 0;
var o:Int = 0;
Expand Down Expand Up @@ -344,7 +343,7 @@ class PathConstraint implements Updatable {
// World vertices.
if (closed) {
verticesLength += 2;
_world.length = verticesLength;
_world.resize(verticesLength);
world = _world;
path.computeWorldVertices(target, 2, verticesLength - 4, world, 0, 2);
path.computeWorldVertices(target, 0, 2, world, verticesLength - 4, 2);
Expand All @@ -353,14 +352,14 @@ class PathConstraint implements Updatable {
} else {
curveCount--;
verticesLength -= 4;
_world.length = verticesLength;
_world.resize(verticesLength);
world = _world;
path.computeWorldVertices(target, 2, verticesLength, world, 0, 2);
}

// Curve lengths.
_curves.length = curveCount;
var curves:Vector<Float> = _curves;
_curves.resize(curveCount);
var curves:Array<Float> = _curves;
var pathLength:Float = 0;
var x1:Float = world[0],
y1:Float = world[1],
Expand Down Expand Up @@ -420,7 +419,7 @@ class PathConstraint implements Updatable {
multiplier = 1;
}

var segments:Vector<Float> = _segments;
var segments:Array<Float> = _segments;
var curveLength:Float = 0;
var segment:Int;
i = 0;
Expand Down Expand Up @@ -529,7 +528,7 @@ class PathConstraint implements Updatable {
return out;
}

private function addBeforePosition(p:Float, temp:Vector<Float>, i:Int, out:Vector<Float>, o:Int):Void {
private function addBeforePosition(p:Float, temp:Array<Float>, i:Int, out:Array<Float>, o:Int):Void {
var x1:Float = temp[i];
var y1:Float = temp[i + 1];
var dx:Float = temp[i + 2] - x1;
Expand All @@ -540,7 +539,7 @@ class PathConstraint implements Updatable {
out[o + 2] = r;
}

private function addAfterPosition(p:Float, temp:Vector<Float>, i:Int, out:Vector<Float>, o:Int):Void {
private function addAfterPosition(p:Float, temp:Array<Float>, i:Int, out:Array<Float>, o:Int):Void {
var x1:Float = temp[i + 2];
var y1:Float = temp[i + 3];
var dx:Float = x1 - temp[i];
Expand All @@ -551,7 +550,7 @@ class PathConstraint implements Updatable {
out[o + 2] = r;
}

private function addCurvePosition(p:Float, x1:Float, y1:Float, cx1:Float, cy1:Float, cx2:Float, cy2:Float, x2:Float, y2:Float, out:Vector<Float>, o:Int,
private function addCurvePosition(p:Float, x1:Float, y1:Float, cx1:Float, cy1:Float, cx2:Float, cy2:Float, x2:Float, y2:Float, out:Array<Float>, o:Int,
tangents:Bool):Void {
if (p == 0 || Math.isNaN(p)) {
out[o] = x1;
Expand Down Expand Up @@ -581,9 +580,9 @@ class PathConstraint implements Updatable {
}
}

public var bones(get, never):Vector<Bone>;
public var bones(get, never):Array<Bone>;

private function get_bones():Vector<Bone> {
private function get_bones():Array<Bone> {
return _bones;
}

Expand Down
8 changes: 3 additions & 5 deletions spine-haxe/spine-haxe/spine/PathConstraintData.hx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,8 @@

package spine;

import openfl.Vector;

class PathConstraintData extends ConstraintData {
private var _bones:Vector<BoneData> = new Vector<BoneData>();
private var _bones:Array<BoneData> = new Array<BoneData>();

public var target:SlotData;
public var positionMode:PositionMode = PositionMode.fixed;
Expand All @@ -49,9 +47,9 @@ class PathConstraintData extends ConstraintData {
super(name, 0, false);
}

public var bones(get, never):Vector<BoneData>;
public var bones(get, never):Array<BoneData>;

private function get_bones():Vector<BoneData> {
private function get_bones():Array<BoneData> {
return _bones;
}
}
Loading

0 comments on commit bb3bd3a

Please sign in to comment.