Skip to content

Commit

Permalink
support Haxe 4.3
Browse files Browse the repository at this point in the history
  • Loading branch information
saharan committed May 12, 2023
1 parent 1ddd90e commit cf29b92
Show file tree
Hide file tree
Showing 27 changed files with 332 additions and 202 deletions.
2 changes: 1 addition & 1 deletion Muun/src/muun/la/Mat2.hx
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ abstract Mat2(Mat2Data) from Mat2Data {
return a * b.inv;
}

@:op(A << B)
@:op(A <<= B)
extern public static inline function assign(a:Mat2, b:Mat2):Mat2 {
a.e00 = b.e00;
a.e01 = b.e01;
Expand Down
4 changes: 2 additions & 2 deletions Muun/src/muun/la/Mat3.hx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ abstract Mat3(Mat3Data) from Mat3Data {
}

extern public static inline function translate(v:Vec2):Mat3 {
return of(0, 0, v.x, 0, 0, v.y, 0, 0, 1);
return of(1, 0, v.x, 0, 1, v.y, 0, 0, 1);
}

public var row0(get, set):Vec3;
Expand Down Expand Up @@ -282,7 +282,7 @@ abstract Mat3(Mat3Data) from Mat3Data {
return a * b.inv;
}

@:op(A << B)
@:op(A <<= B)
extern public static inline function assign(a:Mat3, b:Mat3):Mat3 {
a.e00 = b.e00;
a.e01 = b.e01;
Expand Down
2 changes: 1 addition & 1 deletion Muun/src/muun/la/Mat4.hx
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ abstract Mat4(Mat4Data) from Mat4Data {
return a * b.inv;
}

@:op(A << B)
@:op(A <<= B)
extern public static inline function assign(a:Mat4, b:Mat4):Mat4 {
a.e00 = b.e00;
a.e01 = b.e01;
Expand Down
2 changes: 1 addition & 1 deletion Muun/src/muun/la/Quat.hx
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ abstract Quat(QuatData) from QuatData {
return unary(b, b -> a / b);
}

@:op(A << B)
@:op(A <<= B)
extern public static inline function assign(a:Quat, b:Quat):Quat {
a.x = b.x;
a.y = b.y;
Expand Down
2 changes: 1 addition & 1 deletion Muun/src/muun/la/Vec2.hx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ abstract Vec2(Vec2Data) from Vec2Data {
return unary(b, b -> a / b);
}

@:op(A << B)
@:op(A <<= B)
extern public static inline function assign(a:Vec2, b:Vec2):Vec2 {
a.x = b.x;
a.y = b.y;
Expand Down
2 changes: 1 addition & 1 deletion Muun/src/muun/la/Vec3.hx
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ abstract Vec3(Vec3Data) from Vec3Data {
return unary(b, b -> a / b);
}

@:op(A << B)
@:op(A <<= B)
extern public static inline function assign(a:Vec3, b:Vec3):Vec3 {
a.x = b.x;
a.y = b.y;
Expand Down
2 changes: 1 addition & 1 deletion Muun/src/muun/la/Vec4.hx
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ abstract Vec4(Vec4Data) from Vec4Data {
return unary(b, b -> a / b);
}

@:op(A << B)
@:op(A <<= B)
extern public static inline function assign(a:Vec4, b:Vec4):Vec4 {
a.x = b.x;
a.y = b.y;
Expand Down
1 change: 1 addition & 0 deletions Pot/src/import.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import ext.Std;
87 changes: 66 additions & 21 deletions Pot/src/pot/core/FrameRateManager.hx
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,21 @@ class FrameRateManager {
static inline final UPDATE_LOAD_COEFF:Float = 0.75;
static inline final MAX_UPDATE_COUNT:Int = 4;

final update:() -> Void;
static inline final MIN_UPDATE_TIME:Float = 4;
static inline final MAX_FRAMERATE_RATIO:Float = 4;

final update:(substepRatio:Float) -> Void;
final draw:() -> Void;
var targetInterval:Float = 1000 / 60;
var prevTime:Float;
var lastDrawBegin:Float;
var estimatedUpdateTime:Float;
var running:Bool;
var count:Int = 0;

public var doNotAdjust:Bool = false;

public function new(update:() -> Void, draw:() -> Void) {
public function new(update:(substepRatio:Float) -> Void, draw:() -> Void) {
this.update = update;
this.draw = draw;
}
Expand All @@ -30,6 +35,7 @@ class FrameRateManager {
if (running)
return;
prevTime = now() - targetInterval;
estimatedUpdateTime = targetInterval;
running = true;
Browser.window.setTimeout(loop, 0);
}
Expand All @@ -47,31 +53,70 @@ class FrameRateManager {
function loop():Void {
if (!running)
return;
count++;
if (doNotAdjust) {
doFunc(update);
doFunc(() -> update(1));
doFunc(draw);
} else {
final currentTime = now();
var updated = false;
var updateCount = 0;
while (currentTime - prevTime > targetInterval * 0.5) {
updateCount++;
doFunc(update);
updated = true;
prevTime += targetInterval;
final now = now();
final updateTime = now - currentTime;
final maxConsecutiveUpdates = count > 30 ? MAX_UPDATE_COUNT : 1;
if (updateTime > targetInterval * UPDATE_LOAD_COEFF || updateCount >= maxConsecutiveUpdates) { // overloaded
if (prevTime < now - targetInterval) { // do not accumulate too much
prevTime = now - targetInterval;
}
break;

final maxDrawBegin = max(lastDrawBegin + targetInterval * MAX_FRAMERATE_RATIO, currentTime + MIN_UPDATE_TIME);
final maxUpdateCount = count < 10 ? 1 : max(1, Math.round((maxDrawBegin - currentTime) / estimatedUpdateTime));
final idealUpdateCount = Math.round((currentTime - prevTime) / max(targetInterval * 0.01,
targetInterval - estimatedUpdateTime));
final updateCount = min(idealUpdateCount, maxUpdateCount);

if (updateCount > 0) {
var p = currentTime;
var nextLast = false;
for (i in 0...updateCount) {
doFunc(() -> update(nextLast ? 1 : (i + 1) / updateCount));
final n = now();
final updateTime = n - p;
estimatedUpdateTime += (updateTime - estimatedUpdateTime) * 0.5;
p = n;
prevTime += targetInterval;
if (nextLast)
break;
if (n > maxDrawBegin)
nextLast = true;
}
}
if (updated) {
prevTime = max(prevTime, p - max(targetInterval * MAX_FRAMERATE_RATIO, MIN_UPDATE_TIME));
// trace(prevTime + " " + p + " " + (p - prevTime) + " behind " + (maxDrawBegin - p) + " " + maxUpdateCount + " " +
// idealUpdateCount);
lastDrawBegin = p;
doFunc(draw);
}

// final maxConsecutiveUpdates = count > 10 ? MAX_UPDATE_COUNT : 1;
// final updateCount = min(MAX_UPDATE_COUNT, Math.round(idealUpdateCount));
// if (updateCount > 0) {
// for (i in 0...updateCount) {
// doFunc(update);
// prevTime += targetInterval;
// }
// }

// while (currentTime - prevTime > targetInterval * 0.5) {
// updateCount++;
// doFunc(update);
// updated = true;
// prevTime += targetInterval;
// final now = now();
// final updateTime = now - currentTime;
// estimatedUpdateTime = max(estimatedUpdateTime * 0.9, updateTime);
// final maxConsecutiveUpdates = count > 30 ? MAX_UPDATE_COUNT : 1;
// if (updateTime > targetInterval * UPDATE_LOAD_COEFF || updateCount >= maxConsecutiveUpdates) { // overloaded
// if (prevTime < now - targetInterval) { // do not accumulate too much
// prevTime = now - targetInterval;
// }
// break;
// }
// }
// if (updated) {
// lastDrawBegin = now();
// doFunc(draw);
// }
}
Browser.window.requestAnimationFrame(cast loop);
}
Expand All @@ -89,6 +134,6 @@ class FrameRateManager {
}

extern inline function now():Float {
return Date.now();
return Browser.window.performance.now();
}
}
15 changes: 10 additions & 5 deletions Pot/src/pot/core/Pot.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pot.core;

import muun.la.Vec2;
import js.html.Element;
import js.Browser;
import js.html.CanvasElement;
Expand All @@ -10,6 +11,7 @@ import js.html.CanvasElement;
class Pot {
public var width(default, null):Float;
public var height(default, null):Float;
public var size(get, never):Vec2;
public var pixelRatio(default, null):Float;

var app:App;
Expand Down Expand Up @@ -53,10 +55,13 @@ class Pot {
app.resized();
}

function get_size():Vec2 {
return Vec2.of(width, height);
}

function resize():Bool {
final rect = canvas.getBoundingClientRect();
final w = rect.width;
final h = rect.height;
final w = canvas.clientWidth;
final h = canvas.clientHeight;
final dpr = Browser.window.devicePixelRatio;
if (width != w || height != h || pixelRatio != dpr) {
width = w;
Expand All @@ -78,10 +83,10 @@ class Pot {
frameRateManager.stop();
}

function update():Void {
function update(substepRatio:Float):Void {
app.frameCount++;
if (app.input != null)
app.input.update();
app.input.update(substepRatio);
app.update();
}

Expand Down
8 changes: 3 additions & 5 deletions Pot/src/pot/graphics/gl/FloatBufferWriter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,15 @@ import pot.graphics.gl.low.FloatBuffer;

class FloatBufferWriter {
public final buffer:FloatBuffer;
public var kind:BufferKind;
public var usage:BufferUsage;
public var data(default, null):Float32Array = new Float32Array(512);
public var length(default, null):Int = 0;

var maxLength:Int;
var changed:Bool = false;

public function new(buffer:FloatBuffer, kind:BufferKind, usage:BufferUsage) {
public function new(buffer:FloatBuffer, usage:BufferUsage) {
this.buffer = buffer;
this.kind = kind;
this.usage = usage;
maxLength = data.length;
}
Expand Down Expand Up @@ -68,11 +66,11 @@ class FloatBufferWriter {
extern public inline function upload(force:Bool = false):Void {
if (changed || force) {
changed = false;
buffer.upload(kind, new Float32Array(data.buffer, 0, length), usage);
buffer.upload(new Float32Array(data.buffer, 0, length), usage);
}
}

extern inline function expand():Void {
function expand():Void {
final oldData = data;
data = new Float32Array(maxLength <<= 1);
data.set(oldData);
Expand Down
23 changes: 13 additions & 10 deletions Pot/src/pot/graphics/gl/FrameBuffer.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@ import js.html.webgl.GL2;
class FrameBuffer extends GLObject {
public final textures:Array<Texture>;

public final width:Int;
public final height:Int;
public var width(default, null):Int;
public var height(default, null):Int;

final depth:Renderbuffer;
final framebuffer:Framebuffer;

@:allow(pot.graphics.gl.Graphics)
function new(gl:GL2, textures:Array<Texture>) {
@:allow(pot.graphics.gl.Graphics, pot.graphics.gl.Texture)
function new(gl:GL2, textures:Array<Texture>, init:Bool) {
super(gl);
this.textures = textures.copy();
depth = gl.createRenderbuffer();
framebuffer = gl.createFramebuffer();
if (init) {
initBuffers();
}
}

@:allow(pot.graphics.gl.Texture)
function initBuffers():Void {
width = textures[0].width;
height = textures[0].height;
final type = textures[0].type;
Expand All @@ -28,12 +37,6 @@ class FrameBuffer extends GLObject {
throw "all texture sizes must be the same";
}
}
depth = gl.createRenderbuffer();
framebuffer = gl.createFramebuffer();
initBuffers();
}

function initBuffers():Void {
// init depth buffer
gl.bindRenderbuffer(GL2.RENDERBUFFER, depth);
gl.renderbufferStorage(GL2.RENDERBUFFER, GL2.DEPTH_COMPONENT32F, width, height);
Expand Down
Loading

0 comments on commit cf29b92

Please sign in to comment.