Skip to content
This repository was archived by the owner on Nov 14, 2025. It is now read-only.

Commit 79175c7

Browse files
Vurv78Vurv78
authored andcommitted
Add most metamethods / operator overloads
Through abstracting all of the types, we now have metamethods. This would work perfectly if Haxe didn't interpret some type activities like multiplication as using integers... So now you can array index vectors, add and multiply them, same with colors.
1 parent e87f071 commit 79175c7

66 files changed

Lines changed: 1682 additions & 1565 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

examples/Raytracer.hx

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,50 @@ import sf.library.*;
22
import sf.library.Builtins.*;
33
import sf.type.*;
44

5-
final RES = 64;
6-
final FILTER = [chip()];
5+
final RES = 512;
6+
final FILTER = lua.Table.fromArray([chip()]);
77

88
typedef TraceResult = {
9-
HitNormal: Vector
9+
HitNormal: Vector,
10+
HitSky: Bool,
1011
// Hopefully will have these return values in a standard library at some point.
1112
}
1213

1314
function canRun() {
14-
// Using cast here, you can also just define the function returns up here.
15-
return cast(quotaTotalAverage(),Int) < cast(quotaMax(),Int)*0.5;
15+
var total_average: Float = quotaTotalAverage();
16+
var quota_max: Float = quotaMax();
17+
return total_average < quota_max*0.5;
1618
}
1719

1820
function loadRender() {
19-
var chip = cast(chip(), Entity);
21+
var chip:Entity = chip();
2022
var sundir: Vector = Game.getSunInfo();
2123

2224
var cam_pos: Vector = chip.getPos();
2325
var cam_angle: Angle = chip.getAngles();
2426

2527
var bench: Int = Timer.curtime();
28+
var scr_res = RES-1; // haxe ... loops are non-inclusive of the final value.
29+
for (Y in 0...RES) {
30+
for (X in 0...RES) {
31+
var dir:Vector = Vector(1,1-(X/scr_res)-0.5,1-(Y/scr_res)-0.5);
32+
dir.rotate(cam_angle);
33+
dir.mul(60000);
2634

27-
var scr_res = RES-1; // Since we start at 0, subtract the resolution by one.
28-
for (Y in 0...scr_res) {
29-
for (X in 0...scr_res) {
30-
trace(X,Y);
31-
var dir = cast(Vector(1,1-(X/scr_res)-0.5,1-(Y/scr_res)-0.5), Vector).getRotated(cam_angle);
32-
var trace: TraceResult = Trace.trace( cam_pos, cam_pos.__add(dir), FILTER, null, null, null );
33-
var shading = trace.HitNormal;
35+
var endpos: Vector = cam_pos + dir;
36+
37+
var trace: TraceResult = Trace.trace( cam_pos, endpos, FILTER, null, null, null );
3438
var col: Color = Color(255, 255, 255, 255);
35-
col = col.__mul(shading);
36-
untyped{ col[4] = 255; } // The fields of a color aren't documented, so they aren't defined, so we don't have array access, etc...
39+
if(trace.HitSky) {
40+
col = Color(0, 0, 255, 255);
41+
}else {
42+
var shading: Float = trace.HitNormal.dot(sundir);
43+
// TODO: When operator overloads work, get rid of this awful loop
44+
for(I in 1...4) {
45+
var v: Float = col[I];
46+
col[I] = v * shading;
47+
}
48+
}
3749
Render.selectRenderTarget("rt");
3850
Render.setColor( col );
3951
Render.drawRectFast(X, Y, 1, 1);
@@ -42,7 +54,8 @@ function loadRender() {
4254
Coroutine.yield();
4355
}
4456
}
45-
trace('Finished render in ${bench - cast Timer.curtime()} seconds!');
57+
var ct: Float = Timer.curtime();
58+
trace('Finished render in ${bench - ct} seconds!');
4659
return true;
4760
}
4861

haxelib.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
"license": "MIT",
55
"tags": ["lua", "garrysmod", "starfallex", "starfall", "extern"],
66
"description": "Autogenerated bindings for StarfallEx functions for use in the Haxe lua target.",
7-
"version": "0.1.0",
7+
"version": "0.1.1",
88
"classPath": "src",
9-
"releasenote": "",
9+
"releasenote": "Added most metamethods / operator overloads. Still buggy with Haxe's type inferrence.",
1010
"contributors": ["Vurv"],
1111
"main": "",
1212
"dependencies" : {}

src/sf/library/Bass.hx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
package sf.library;
22
@:native("bass") extern class Bass {
33
/**
4-
CLIENT
4+
CLIENT
55
Loads a sound channel from an URL.
66
**/
77
@:native("loadURL") public static function loadURL(path:Any,flags:Any,callback:Any):Void;
88
/**
9-
CLIENT
9+
CLIENT
1010
Loads a sound channel from a file.
1111
**/
1212
@:native("loadFile") public static function loadFile(path:Any,flags:Any,callback:Any):Void;
1313
/**
14-
CLIENT
14+
CLIENT
1515
Returns the number of sounds left that can be created
1616
**/
1717
@:native("soundsLeft") public static function soundsLeft():Any;

src/sf/library/Bit.hx

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,87 @@
11
package sf.library;
22
@:native("bit") extern class Bit {
33
/**
4-
SHARED
4+
SHARED
55
66
**/
77
@:native("rol") public static function rol():Void;
88
/**
9-
SHARED
9+
SHARED
1010
1111
**/
1212
@:native("ror") public static function ror():Void;
1313
/**
14-
SHARED
14+
SHARED
1515
Compresses a string
1616
**/
1717
@:native("compress") public static function compress(s:Any):Any;
1818
/**
19-
SHARED
19+
SHARED
2020
2121
**/
2222
@:native("bnot") public static function bnot():Void;
2323
/**
24-
SHARED
24+
SHARED
2525
2626
**/
2727
@:native("lshift") public static function lshift():Void;
2828
/**
29-
SHARED
29+
SHARED
3030
3131
**/
3232
@:native("tohex") public static function tohex():Void;
3333
/**
34-
SHARED
34+
SHARED
3535
Converts serialized string data to table
3636
**/
3737
@:native("stringToTable") public static function stringToTable(s:Any):Any;
3838
/**
39-
SHARED
39+
SHARED
4040
4141
**/
4242
@:native("bor") public static function bor():Void;
4343
/**
44-
SHARED
44+
SHARED
4545
4646
**/
4747
@:native("bswap") public static function bswap():Void;
4848
/**
49-
SHARED
49+
SHARED
5050
Converts a table to string serializing data types as best as it can
5151
**/
5252
@:native("tableToString") public static function tableToString(t:Any):Any;
5353
/**
54-
SHARED
54+
SHARED
5555
5656
**/
5757
@:native("rshift") public static function rshift():Void;
5858
/**
59-
SHARED
59+
SHARED
6060
6161
**/
6262
@:native("arshift") public static function arshift():Void;
6363
/**
64-
SHARED
64+
SHARED
6565
6666
**/
6767
@:native("bxor") public static function bxor():Void;
6868
/**
69-
SHARED
69+
SHARED
7070
7171
**/
7272
@:native("tobit") public static function tobit():Void;
7373
/**
74-
SHARED
74+
SHARED
7575
Creates a StringStream object
7676
**/
7777
@:native("stringstream") public static function stringstream(stream:Any,i:Any,endian:Any):Void;
7878
/**
79-
SHARED
79+
SHARED
8080
Decompresses a string
8181
**/
8282
@:native("decompress") public static function decompress(s:Any):Any;
8383
/**
84-
SHARED
84+
SHARED
8585
8686
**/
8787
@:native("band") public static function band():Void;

0 commit comments

Comments
 (0)