1+ package kitty ;
2+
3+ #if macro
4+ import haxe .macro .Compiler ;
5+ import haxe .macro .Context ;
6+ import haxe .macro .Expr ;
7+
8+ using StringTools ;
9+ using haxe .macro .ExprTools ;
10+
11+ class Macro {
12+ public static function init (): Void {
13+ var classPath : String = Std .string (Macro ).replace (' Class<' , ' ' ).replace (' >' , ' ' );
14+ Compiler .addMetadata (' @:build( $classPath .rebuildFlxText())' , ' flixel.text.FlxText' );
15+ Compiler .addMetadata (' @:build( $classPath .rebuildBitmapData())' , ' openfl.display.BitmapData' );
16+ Compiler .include (' kitty' , true , [' *Macro' ]);
17+ Compiler .include (' haxe' , true , [' haxe.atomic.*' , ' haxe.macro.*' ]);
18+ Compiler .include (' flixel' , true , [' flixel.addons.editors.spine.*' , ' flixel.addons.nape.*' , ' flixel.system.macros.*' , ' flixel.addons.tile.FlxRayCastTilemap' , ' flixel.addons.weapon.*' ]);
19+ Compiler .include (' moonchart' , true , [' moonchart.backend.*' ]);
20+ }
21+
22+ // fix for pixel font related shenanigans
23+ public static macro function rebuildFlxText (): Array <Field > {
24+ var classFields = Context .getBuildFields ();
25+ var tempClass = macro class TempClass {
26+ /**
27+ * If true, the text will be rendered with pixel-perfect sharpness.
28+ */
29+ public var pixelFont (default , set ): Bool ;
30+ function set_pixelFont (value : Bool ): Bool {
31+ if (value ) {
32+ textField .antiAliasType = ADVANCED ;
33+ textField .sharpness = 400 ;
34+ } else {
35+ textField .antiAliasType = NORMAL ;
36+ textField .sharpness = 100 ;
37+ }
38+ return pixelFont = value ;
39+ }
40+ }
41+
42+ var newConstructor = classFields .filter (field -> return field .name == ' new' )[0 ];
43+ switch (newConstructor .kind ) {
44+ case FFun (f ):
45+ var initExpr : Expr = f .expr ;
46+ f .expr = macro {
47+ $initExpr ;
48+ set_pixelFont (false );
49+ }
50+ newConstructor .kind = FFun (f );
51+ default :
52+ }
53+ classFields .remove (classFields .filter (field -> return field .name == ' set_antialiasing' )[0 ]);
54+
55+ return classFields .concat (tempClass .fields );
56+ }
57+
58+ // fix for GPU rendering related shenanigans
59+ public static macro function rebuildBitmapData (): Array <Field > {
60+ var classFields = Context .getBuildFields ();
61+ var tempClass = macro class TempClass {
62+ /**
63+ * If true, the bitmap data will render to your GPU.
64+ * NOTE: ISN'T WORKING RIGHT FOR SOME REASON, ALSO DON'T SET IT TO TRUE HERE, IT CRASHES
65+ */
66+ public var renderToGPU : Bool = false ;
67+ }
68+
69+ var fromImageFunc = classFields .filter (field -> return field .name == ' __fromImage' )[0 ];
70+ switch (fromImageFunc .kind ) {
71+ case FFun (f ):
72+ var initExpr : Expr = f .expr ;
73+ f .expr = macro {
74+ if (! renderToGPU ) {
75+ $initExpr ;
76+ return ;
77+ }
78+ if (image != null && image .buffer != null ) {
79+ this .image = image ;
80+
81+ width = image .width ;
82+ height = image .height ;
83+ rect = new Rectangle (0 , 0 , image .width , image .height );
84+
85+ __textureWidth = width ;
86+ __textureHeight = height ;
87+
88+ #if sys
89+ image .format = BGRA32 ;
90+ image .premultiplied = true ;
91+ #end
92+
93+ readable = true ;
94+ __isValid = true ;
95+
96+ // https://github.com/CodenameCrew/CodenameEngine/blob/main/source/funkin/backend/system/OptimizedBitmapData.hx#L9L46
97+ if (flixel. FlxG .stage .context3D != null ) {
98+ lock ();
99+ getTexture (flixel. FlxG .stage .context3D );
100+ getSurface ();
101+ readable = true ;
102+ this .image = null ;
103+ }
104+ }
105+ }
106+ fromImageFunc .kind = FFun (f );
107+ default :
108+ }
109+ var getSurfaceFunc = classFields .filter (field -> return field .name == ' getSurface' )[0 ];
110+ switch (getSurfaceFunc .kind ) {
111+ case FFun (f ):
112+ var initExpr : Expr = f .expr ;
113+ f .expr = macro {
114+ // https://github.com/CodenameCrew/CodenameEngine/blob/main/source/funkin/backend/system/OptimizedBitmapData.hx#L48L61
115+ if (renderToGPU )
116+ return __surface ?? = CairoImageSurface .fromImage (image );
117+ $initExpr ;
118+ }
119+ getSurfaceFunc .kind = FFun (f );
120+ default :
121+ }
122+
123+ return classFields .concat (tempClass .fields );
124+ }
125+ }
126+ #end
0 commit comments