Skip to content

Commit

Permalink
use cpp native array to clear arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
MaybeMaru committed Apr 19, 2024
1 parent db384d6 commit 616d99a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 43 deletions.
11 changes: 4 additions & 7 deletions source/flixel/graphics/tile/FlxDrawQuadsItem.hx
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ class FlxDrawQuadsItem extends FlxDrawBaseItem<FlxDrawQuadsItem>
super.reset();
rects.length = 0;
transforms.length = 0;
alphas.splice(0, alphas.length);
alphas.clear();
if (colored)
{
colorMultipliers.splice(0, colorMultipliers.length);
colorOffsets.splice(0, colorOffsets.length);
colorMultipliers.clear();
colorOffsets.clear();
}
}

Expand Down Expand Up @@ -122,9 +122,6 @@ class FlxDrawQuadsItem extends FlxDrawBaseItem<FlxDrawQuadsItem>
shader.colorOffset.value = colorOffsets;
}

if (blend == null)
blend = NORMAL;

setParameterValue(shader.hasColorTransform, colored);
drawFlxQuad(camera.canvas.graphics, shader, rects, transforms);

Expand All @@ -140,7 +137,7 @@ class FlxDrawQuadsItem extends FlxDrawBaseItem<FlxDrawQuadsItem>
function drawFlxQuad(graphics:Graphics, shader:FlxShader, rects:Vector<Float>, transforms:Vector<Float>):Void @:privateAccess
{
// Override blend mode
graphics.__commands.overrideBlendMode(blend);
graphics.__commands.overrideBlendMode(blend ?? NORMAL);

// Begin shader fill
final shaderBuffer = graphics.__shaderBufferPool.get();
Expand Down
19 changes: 12 additions & 7 deletions source/flixel/graphics/tile/FlxDrawTrianglesItem.hx
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
shader.colorOffset.value = colorOffsets;
}

if (blend == null)
blend = NORMAL;

setParameterValue(shader.hasColorTransform, colored);
drawFlxTriangle(camera.canvas.graphics);
#else
Expand All @@ -102,7 +99,7 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
function drawFlxTriangle(graphics:Graphics):Void @:privateAccess
{
#if (openfl > "8.7.0")
graphics.__commands.overrideBlendMode(blend);
graphics.__commands.overrideBlendMode(blend ?? NORMAL);
#end

graphics.beginShaderFill(shader);
Expand All @@ -115,19 +112,27 @@ class FlxDrawTrianglesItem extends FlxDrawBaseItem<FlxDrawTrianglesItem>
override public function reset():Void
{
super.reset();
#if (flash || openfl >= "4.0.0")
vertices.length = 0;
indices.length = 0;
uvtData.length = 0;
colors.length = 0;
#else
vertices.splice(0, vertices.length);
indices.splice(0, indices.length);
uvtData.splice(0, uvtData.length);
colors.splice(0, colors.length);
#end

verticesPosition = 0;
indicesPosition = 0;
colorsPosition = 0;

#if !flash
alphas.splice(0, alphas.length);
alphas.clear();
if (colored) {
colorMultipliers.splice(0, colorMultipliers.length);
colorOffsets.splice(0, colorOffsets.length);
colorMultipliers.clear();
colorOffsets.clear();
}
#end
}
Expand Down
2 changes: 1 addition & 1 deletion source/funkin/util/modding/ModdingUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class ModdingUtil
#end

scripts.copy().fastForEach((script, i) -> removeScript(script));
scripts.splice(0, scripts.length);
scripts.clear();

// Warn if the mod folder is outdated
if (curModData != null) if (curModData.apiVersion != API_VERSION)
Expand Down
2 changes: 1 addition & 1 deletion source/funkin/util/song/Conductor.hx
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Conductor

public static function mapBPMChanges(song:SwagSong):Void
{
bpmChangeMap.splice(0, bpmChangeMap.length);
bpmChangeMap.clear();

var curBPM:Float = song.bpm;
var totalSteps:Int = 0;
Expand Down
3 changes: 1 addition & 2 deletions source/import.hx
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,5 @@ import flixel.util.FlxArrayUtil;
import haxe.Json;

using StringTools;
using macros.UnsafeArray;
using macros.FastIterate;
using macros.FastArray;
#end
32 changes: 27 additions & 5 deletions source/macros/FastIterate.hx → source/macros/FastArray.hx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package macros;

import haxe.macro.Expr;
//import haxe.macro.Context;

macro function fastForEach(iterableExpr: Expr, callbackExpr: Expr) {
// Extract variable names and expression from `callbackExpr`
Expand All @@ -27,10 +26,6 @@ macro function fastForEach(iterableExpr: Expr, callbackExpr: Expr) {
case _: throw "`callbackExpr` must be a function with two arguments!";
}

// Make sure the array doesnt compile into dynamic
//final type = Context.toComplexType(Context.typeof(iterableExpr));

// Build the expression this macro call changes into:
return macro {
final iterable = $iterableExpr;
final len = iterable.length;
Expand All @@ -41,4 +36,31 @@ macro function fastForEach(iterableExpr: Expr, callbackExpr: Expr) {
$i{indexName}++;
}
}
}

class FastArray<T>
{
inline public static function unsafeGet<T>(array:Array<T>, index:Int):T {
#if cpp
return cpp.NativeArray.unsafeGet(array, index);
#else
return array[index];
#end
}

inline public static function unsafeSet<T>(array:Array<T>, index:Int, value:T):Void {
#if cpp
untyped array.__unsafe_set(index, value);
#else
array[index] = value;
#end
}

inline public static function clear<T>(array:Array<T>) {
#if cpp
cpp.NativeArray.setSize(array, 0);
#else
array.splice(0, array.length);
#end
}
}
20 changes: 0 additions & 20 deletions source/macros/UnsafeArray.hx

This file was deleted.

0 comments on commit 616d99a

Please sign in to comment.