Skip to content

Commit cf29b92

Browse files
committed
support Haxe 4.3
1 parent 1ddd90e commit cf29b92

27 files changed

+332
-202
lines changed

Muun/src/muun/la/Mat2.hx

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ abstract Mat2(Mat2Data) from Mat2Data {
192192
return a * b.inv;
193193
}
194194

195-
@:op(A << B)
195+
@:op(A <<= B)
196196
extern public static inline function assign(a:Mat2, b:Mat2):Mat2 {
197197
a.e00 = b.e00;
198198
a.e01 = b.e01;

Muun/src/muun/la/Mat3.hx

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ abstract Mat3(Mat3Data) from Mat3Data {
3939
}
4040

4141
extern public static inline function translate(v:Vec2):Mat3 {
42-
return of(0, 0, v.x, 0, 0, v.y, 0, 0, 1);
42+
return of(1, 0, v.x, 0, 1, v.y, 0, 0, 1);
4343
}
4444

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

285-
@:op(A << B)
285+
@:op(A <<= B)
286286
extern public static inline function assign(a:Mat3, b:Mat3):Mat3 {
287287
a.e00 = b.e00;
288288
a.e01 = b.e01;

Muun/src/muun/la/Mat4.hx

+1-1
Original file line numberDiff line numberDiff line change
@@ -386,7 +386,7 @@ abstract Mat4(Mat4Data) from Mat4Data {
386386
return a * b.inv;
387387
}
388388

389-
@:op(A << B)
389+
@:op(A <<= B)
390390
extern public static inline function assign(a:Mat4, b:Mat4):Mat4 {
391391
a.e00 = b.e00;
392392
a.e01 = b.e01;

Muun/src/muun/la/Quat.hx

+1-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ abstract Quat(QuatData) from QuatData {
235235
return unary(b, b -> a / b);
236236
}
237237

238-
@:op(A << B)
238+
@:op(A <<= B)
239239
extern public static inline function assign(a:Quat, b:Quat):Quat {
240240
a.x = b.x;
241241
a.y = b.y;

Muun/src/muun/la/Vec2.hx

+1-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ abstract Vec2(Vec2Data) from Vec2Data {
162162
return unary(b, b -> a / b);
163163
}
164164

165-
@:op(A << B)
165+
@:op(A <<= B)
166166
extern public static inline function assign(a:Vec2, b:Vec2):Vec2 {
167167
a.x = b.x;
168168
a.y = b.y;

Muun/src/muun/la/Vec3.hx

+1-1
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ abstract Vec3(Vec3Data) from Vec3Data {
166166
return unary(b, b -> a / b);
167167
}
168168

169-
@:op(A << B)
169+
@:op(A <<= B)
170170
extern public static inline function assign(a:Vec3, b:Vec3):Vec3 {
171171
a.x = b.x;
172172
a.y = b.y;

Muun/src/muun/la/Vec4.hx

+1-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ abstract Vec4(Vec4Data) from Vec4Data {
167167
return unary(b, b -> a / b);
168168
}
169169

170-
@:op(A << B)
170+
@:op(A <<= B)
171171
extern public static inline function assign(a:Vec4, b:Vec4):Vec4 {
172172
a.x = b.x;
173173
a.y = b.y;

Pot/src/import.hx

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import ext.Std;

Pot/src/pot/core/FrameRateManager.hx

+66-21
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,21 @@ class FrameRateManager {
1212
static inline final UPDATE_LOAD_COEFF:Float = 0.75;
1313
static inline final MAX_UPDATE_COUNT:Int = 4;
1414

15-
final update:() -> Void;
15+
static inline final MIN_UPDATE_TIME:Float = 4;
16+
static inline final MAX_FRAMERATE_RATIO:Float = 4;
17+
18+
final update:(substepRatio:Float) -> Void;
1619
final draw:() -> Void;
1720
var targetInterval:Float = 1000 / 60;
1821
var prevTime:Float;
22+
var lastDrawBegin:Float;
23+
var estimatedUpdateTime:Float;
1924
var running:Bool;
2025
var count:Int = 0;
2126

2227
public var doNotAdjust:Bool = false;
2328

24-
public function new(update:() -> Void, draw:() -> Void) {
29+
public function new(update:(substepRatio:Float) -> Void, draw:() -> Void) {
2530
this.update = update;
2631
this.draw = draw;
2732
}
@@ -30,6 +35,7 @@ class FrameRateManager {
3035
if (running)
3136
return;
3237
prevTime = now() - targetInterval;
38+
estimatedUpdateTime = targetInterval;
3339
running = true;
3440
Browser.window.setTimeout(loop, 0);
3541
}
@@ -47,31 +53,70 @@ class FrameRateManager {
4753
function loop():Void {
4854
if (!running)
4955
return;
56+
count++;
5057
if (doNotAdjust) {
51-
doFunc(update);
58+
doFunc(() -> update(1));
5259
doFunc(draw);
5360
} else {
5461
final currentTime = now();
55-
var updated = false;
56-
var updateCount = 0;
57-
while (currentTime - prevTime > targetInterval * 0.5) {
58-
updateCount++;
59-
doFunc(update);
60-
updated = true;
61-
prevTime += targetInterval;
62-
final now = now();
63-
final updateTime = now - currentTime;
64-
final maxConsecutiveUpdates = count > 30 ? MAX_UPDATE_COUNT : 1;
65-
if (updateTime > targetInterval * UPDATE_LOAD_COEFF || updateCount >= maxConsecutiveUpdates) { // overloaded
66-
if (prevTime < now - targetInterval) { // do not accumulate too much
67-
prevTime = now - targetInterval;
68-
}
69-
break;
62+
63+
final maxDrawBegin = max(lastDrawBegin + targetInterval * MAX_FRAMERATE_RATIO, currentTime + MIN_UPDATE_TIME);
64+
final maxUpdateCount = count < 10 ? 1 : max(1, Math.round((maxDrawBegin - currentTime) / estimatedUpdateTime));
65+
final idealUpdateCount = Math.round((currentTime - prevTime) / max(targetInterval * 0.01,
66+
targetInterval - estimatedUpdateTime));
67+
final updateCount = min(idealUpdateCount, maxUpdateCount);
68+
69+
if (updateCount > 0) {
70+
var p = currentTime;
71+
var nextLast = false;
72+
for (i in 0...updateCount) {
73+
doFunc(() -> update(nextLast ? 1 : (i + 1) / updateCount));
74+
final n = now();
75+
final updateTime = n - p;
76+
estimatedUpdateTime += (updateTime - estimatedUpdateTime) * 0.5;
77+
p = n;
78+
prevTime += targetInterval;
79+
if (nextLast)
80+
break;
81+
if (n > maxDrawBegin)
82+
nextLast = true;
7083
}
71-
}
72-
if (updated) {
84+
prevTime = max(prevTime, p - max(targetInterval * MAX_FRAMERATE_RATIO, MIN_UPDATE_TIME));
85+
// trace(prevTime + " " + p + " " + (p - prevTime) + " behind " + (maxDrawBegin - p) + " " + maxUpdateCount + " " +
86+
// idealUpdateCount);
87+
lastDrawBegin = p;
7388
doFunc(draw);
7489
}
90+
91+
// final maxConsecutiveUpdates = count > 10 ? MAX_UPDATE_COUNT : 1;
92+
// final updateCount = min(MAX_UPDATE_COUNT, Math.round(idealUpdateCount));
93+
// if (updateCount > 0) {
94+
// for (i in 0...updateCount) {
95+
// doFunc(update);
96+
// prevTime += targetInterval;
97+
// }
98+
// }
99+
100+
// while (currentTime - prevTime > targetInterval * 0.5) {
101+
// updateCount++;
102+
// doFunc(update);
103+
// updated = true;
104+
// prevTime += targetInterval;
105+
// final now = now();
106+
// final updateTime = now - currentTime;
107+
// estimatedUpdateTime = max(estimatedUpdateTime * 0.9, updateTime);
108+
// final maxConsecutiveUpdates = count > 30 ? MAX_UPDATE_COUNT : 1;
109+
// if (updateTime > targetInterval * UPDATE_LOAD_COEFF || updateCount >= maxConsecutiveUpdates) { // overloaded
110+
// if (prevTime < now - targetInterval) { // do not accumulate too much
111+
// prevTime = now - targetInterval;
112+
// }
113+
// break;
114+
// }
115+
// }
116+
// if (updated) {
117+
// lastDrawBegin = now();
118+
// doFunc(draw);
119+
// }
75120
}
76121
Browser.window.requestAnimationFrame(cast loop);
77122
}
@@ -89,6 +134,6 @@ class FrameRateManager {
89134
}
90135

91136
extern inline function now():Float {
92-
return Date.now();
137+
return Browser.window.performance.now();
93138
}
94139
}

Pot/src/pot/core/Pot.hx

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package pot.core;
22

3+
import muun.la.Vec2;
34
import js.html.Element;
45
import js.Browser;
56
import js.html.CanvasElement;
@@ -10,6 +11,7 @@ import js.html.CanvasElement;
1011
class Pot {
1112
public var width(default, null):Float;
1213
public var height(default, null):Float;
14+
public var size(get, never):Vec2;
1315
public var pixelRatio(default, null):Float;
1416

1517
var app:App;
@@ -53,10 +55,13 @@ class Pot {
5355
app.resized();
5456
}
5557

58+
function get_size():Vec2 {
59+
return Vec2.of(width, height);
60+
}
61+
5662
function resize():Bool {
57-
final rect = canvas.getBoundingClientRect();
58-
final w = rect.width;
59-
final h = rect.height;
63+
final w = canvas.clientWidth;
64+
final h = canvas.clientHeight;
6065
final dpr = Browser.window.devicePixelRatio;
6166
if (width != w || height != h || pixelRatio != dpr) {
6267
width = w;
@@ -78,10 +83,10 @@ class Pot {
7883
frameRateManager.stop();
7984
}
8085

81-
function update():Void {
86+
function update(substepRatio:Float):Void {
8287
app.frameCount++;
8388
if (app.input != null)
84-
app.input.update();
89+
app.input.update(substepRatio);
8590
app.update();
8691
}
8792

Pot/src/pot/graphics/gl/FloatBufferWriter.hx

+3-5
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ import pot.graphics.gl.low.FloatBuffer;
77

88
class FloatBufferWriter {
99
public final buffer:FloatBuffer;
10-
public var kind:BufferKind;
1110
public var usage:BufferUsage;
1211
public var data(default, null):Float32Array = new Float32Array(512);
1312
public var length(default, null):Int = 0;
1413

1514
var maxLength:Int;
1615
var changed:Bool = false;
1716

18-
public function new(buffer:FloatBuffer, kind:BufferKind, usage:BufferUsage) {
17+
public function new(buffer:FloatBuffer, usage:BufferUsage) {
1918
this.buffer = buffer;
20-
this.kind = kind;
2119
this.usage = usage;
2220
maxLength = data.length;
2321
}
@@ -68,11 +66,11 @@ class FloatBufferWriter {
6866
extern public inline function upload(force:Bool = false):Void {
6967
if (changed || force) {
7068
changed = false;
71-
buffer.upload(kind, new Float32Array(data.buffer, 0, length), usage);
69+
buffer.upload(new Float32Array(data.buffer, 0, length), usage);
7270
}
7371
}
7472

75-
extern inline function expand():Void {
73+
function expand():Void {
7674
final oldData = data;
7775
data = new Float32Array(maxLength <<= 1);
7876
data.set(oldData);

Pot/src/pot/graphics/gl/FrameBuffer.hx

+13-10
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,25 @@ import js.html.webgl.GL2;
77
class FrameBuffer extends GLObject {
88
public final textures:Array<Texture>;
99

10-
public final width:Int;
11-
public final height:Int;
10+
public var width(default, null):Int;
11+
public var height(default, null):Int;
1212

1313
final depth:Renderbuffer;
1414
final framebuffer:Framebuffer;
1515

16-
@:allow(pot.graphics.gl.Graphics)
17-
function new(gl:GL2, textures:Array<Texture>) {
16+
@:allow(pot.graphics.gl.Graphics, pot.graphics.gl.Texture)
17+
function new(gl:GL2, textures:Array<Texture>, init:Bool) {
1818
super(gl);
1919
this.textures = textures.copy();
20+
depth = gl.createRenderbuffer();
21+
framebuffer = gl.createFramebuffer();
22+
if (init) {
23+
initBuffers();
24+
}
25+
}
26+
27+
@:allow(pot.graphics.gl.Texture)
28+
function initBuffers():Void {
2029
width = textures[0].width;
2130
height = textures[0].height;
2231
final type = textures[0].type;
@@ -28,12 +37,6 @@ class FrameBuffer extends GLObject {
2837
throw "all texture sizes must be the same";
2938
}
3039
}
31-
depth = gl.createRenderbuffer();
32-
framebuffer = gl.createFramebuffer();
33-
initBuffers();
34-
}
35-
36-
function initBuffers():Void {
3740
// init depth buffer
3841
gl.bindRenderbuffer(GL2.RENDERBUFFER, depth);
3942
gl.renderbufferStorage(GL2.RENDERBUFFER, GL2.DEPTH_COMPONENT32F, width, height);

0 commit comments

Comments
 (0)