From d6df14c99c44f708635c9656f968079f24b82b5a Mon Sep 17 00:00:00 2001 From: MaybeMaru <97055307+MaybeMaru@users.noreply.github.com> Date: Fri, 12 Apr 2024 20:59:43 +0200 Subject: [PATCH] draw directly to the quad in repeat sprite --- source/SplashState.hx | 25 +++--- source/funkin/Preloader.hx | 9 ++- source/funkin/Transition.hx | 42 +++++++--- source/funkin/graphics/FlxRepeatSprite.hx | 80 ++++++++----------- source/funkin/graphics/FlxSkewRepeatSprite.hx | 7 +- source/funkin/graphics/FlxSpriteExt.hx | 3 - source/funkin/objects/Character.hx | 43 +++++----- 7 files changed, 110 insertions(+), 99 deletions(-) diff --git a/source/SplashState.hx b/source/SplashState.hx index ac34eea5..cc022a7d 100644 --- a/source/SplashState.hx +++ b/source/SplashState.hx @@ -1,19 +1,18 @@ package; -class SplashState extends FlxState { - override function create() { +class SplashState extends FlxState +{ + override function create():Void { super.create(); final iconz:FunkinSprite = new FunkinSprite('title/healthHeads'); iconz.screenCenter(); - new FlxTimer().start(0.5, function(tmr:FlxTimer) { + new FlxTimer().start(0.5, (tmr) -> { add(iconz); - FlxG.sound.play(Paths.sound('intro/introSound'), 1, false, null, true, function() { - new FlxTimer().start(0.1, function(tmr:FlxTimer) iconz.destroy() ); - new FlxTimer().start(0.5, function(tmr:FlxTimer) { - switchStuff(); - }); + FlxG.sound.play(Paths.sound('intro/introSound'), 1, false, null, true, () -> { + new FlxTimer().start(0.1, (tmr) -> iconz.destroy() ); + new FlxTimer().start(0.5, (tmr) -> switchStuff()); }); }); @@ -24,12 +23,12 @@ class SplashState extends FlxState { trace('Checking if version is outdated'); var gitFile = new haxe.Http("https://raw.githubusercontent.com/MaybeMaru/FNF-Engine-Backend/main/gameVersion.json"); - gitFile.onError = function (error) { + gitFile.onError = (error) -> { trace('Error: $error'); } var openOutdated:Bool = false; - gitFile.onData = function (data:String) { + gitFile.onData = (data:String) -> { var newVersionData:EngineVersion = Json.parse(data); trace('curVer [${Main.engineVersion}] // newVer [${newVersionData.version}]'); @@ -40,11 +39,13 @@ class SplashState extends FlxState { } gitFile.request(); - openOutdated ? CoolUtil.switchState(new funkin.states.OutdatedState()) : #end startGame(); + openOutdated ? CoolUtil.switchState(new funkin.states.OutdatedState()) : + #end + startGame(); } public static function startGame() { CoolUtil.playMusic('freakyMenu', 0); - CoolUtil.switchState(new TitleState()); + CoolUtil.switchState(new TitleState(), true, true); } } \ No newline at end of file diff --git a/source/funkin/Preloader.hx b/source/funkin/Preloader.hx index d6958809..37418071 100644 --- a/source/funkin/Preloader.hx +++ b/source/funkin/Preloader.hx @@ -7,7 +7,8 @@ import flixel.addons.util.FlxAsyncLoop; Credits to Rozebud */ -class Preloader extends FlxState { +class Preloader extends FlxState +{ inline public static function cacheImage(key:String) { AssetManager.cacheGraphicPath(key, true); } @@ -61,8 +62,10 @@ class Preloader extends FlxState { var _index:Int = 0; var _length:Int = 0; - public function loadBitmap():Void { - if (imageCache.length <= 0) return; + public function loadBitmap():Void + { + if (imageCache.length <= 0) + return; final cacheStr = imageCache[0]; if (cacheStr != null) { diff --git a/source/funkin/Transition.hx b/source/funkin/Transition.hx index fa9342b1..5aeb1df5 100644 --- a/source/funkin/Transition.hx +++ b/source/funkin/Transition.hx @@ -22,7 +22,13 @@ class ResizableSprite extends Sprite { } } -class Transition extends ResizableSprite { +typedef TransitionData = { + var open:Float; + var close:Float; +} + +class Transition extends ResizableSprite +{ public static var skipTransOpen:Bool = false; public static var skipTransClose:Bool = false; @@ -31,10 +37,11 @@ class Transition extends ResizableSprite { skipTransClose = close ?? open; } - public static var times = { + public static var times:TransitionData = { open: 0.4, close: 0.3 } + var bitmap:Bitmap; public function new() { @@ -45,31 +52,42 @@ class Transition extends ResizableSprite { visible = false; } - public function set(?color:FlxColor, openTime:Float = 0.4, closeTime:Float = 0.3, ?asset:FlxGraphicAsset) { + public function set(?color:FlxColor, openTime:Float = 0.4, closeTime:Float = 0.3, ?asset:FlxGraphicAsset) + { times.open = openTime; times.close = closeTime; - if (asset != null) { + if (asset != null) + { if (asset is String) { bitmap.bitmapData = AssetManager.getFileBitmap(asset, true); } else if (asset is FlxGraphic) { - final _graphic = cast(asset, FlxGraphic); - bitmap.bitmapData = _graphic.bitmap; - _graphic.persist = true; - _graphic.destroyOnNoUse = false; + final graphic = cast(asset, FlxGraphic); + bitmap.bitmapData = graphic.bitmap; + graphic.persist = true; + graphic.destroyOnNoUse = false; } else if (asset is BitmapData) { bitmap.bitmapData = asset; } updateScale(); } - else { - color = color ?? FlxColor.BLACK; + else + { + color ??= FlxColor.BLACK; final bmp = new BitmapData(FlxG.width, FlxG.height * 2, true, color); for (i in 0...FlxG.height) { var lineAlpha = FlxMath.remapToRange(i, 0, FlxG.height, 0, color.alpha); - bmp.fillRect(new Rectangle(0, i, FlxG.width, 1), FlxColor.fromRGB(color.red,color.green,color.blue,Std.int(lineAlpha))); + var rect = CoolUtil.rectangle; + rect.setTo(0, i, FlxG.width, 1); + + bmp.fillRect(rect, FlxColor.fromRGB( + color.red, + color.green, + color.blue, + Std.int(lineAlpha) + )); } bitmap.bitmapData = bmp; bitmap.scaleX = bitmap.scaleY = 1; @@ -89,7 +107,7 @@ class Transition extends ResizableSprite { public function startTrans(?nextState:FlxState, ?completeCallback:()->Void) { scaleY = -Math.abs(scaleY); inExit = false; - setupTrans(0, height, times.open, function () { + setupTrans(0, height, times.open, () -> { if (completeCallback != null) completeCallback(); if (nextState != null) FlxG.switchState(nextState); }, true); diff --git a/source/funkin/graphics/FlxRepeatSprite.hx b/source/funkin/graphics/FlxRepeatSprite.hx index 8ec67583..c6294795 100644 --- a/source/funkin/graphics/FlxRepeatSprite.hx +++ b/source/funkin/graphics/FlxRepeatSprite.hx @@ -1,9 +1,10 @@ package funkin.graphics; -import flixel.math.FlxMatrix; -import openfl.display.BitmapData; +import flixel.graphics.tile.FlxDrawQuadsItem; import flixel.graphics.frames.FlxFrame; +using flixel.util.FlxColorTransformUtil; + enum abstract RepeatDrawStyle(Int) from Int to Int { var TOP_BOTTOM = 0; var BOTTOM_TOP = 1; @@ -62,21 +63,21 @@ class FlxRepeatSprite extends FlxSpriteExt return clipRect = rect; } - override function draw() { - if (tilesX == 0 || tilesY == 0) - return; - - inline checkEmptyFrame(); - if (alpha == 0 || !visible || (clipRect?.isEmpty)) - return; + override function draw():Void { + if (tilesX == 0) return; + if (tilesY == 0) return; + if (!visible) return; + if (alpha == 0) return; - if (dirty) - calcFrame(useFramePixels); // rarely + checkEmptyFrame(); + if (clipRect != null) if (clipRect.isEmpty) return; cameras.fastForEach((camera, i) -> { if (camera.visible) if (camera.exists) if (isOnScreen(camera)) { drawComplex(camera); - #if FLX_DEBUG FlxBasic.visibleCount++; #end + #if FLX_DEBUG + FlxBasic.visibleCount++; + #end } }); } @@ -95,26 +96,11 @@ class FlxRepeatSprite extends FlxSpriteExt } static final tempPoint:FlxPoint = FlxPoint.get(); - static final lastMatrix = FlxPoint.get(); // Nasty hack - - override function drawComplex(camera:FlxCamera) { - prepareFrameMatrix(_frame, _matrix); - - inline _matrix.translate(-origin.x, -origin.y); - inline _matrix.scale(scale.x, scale.y); + static final lastMatrix:FlxPoint = FlxPoint.get(); // Nasty hack - if (angle != 0) { - __updateTrig(); - _matrix.rotateWithTrig(_cosAngle, _sinAngle); - } - - getScreenPosition(_point, camera).subtractPoint(offset); - _point.add(origin.x, origin.y); - inline _matrix.translate(_point.x, _point.y); - - /* - * The actual code of the class lol - **/ + override function drawComplex(camera:FlxCamera):Void + { + __prepareDraw(camera); // Holds the og matrix position for each tile var point = CoolUtil.point.set(_matrix.tx, _matrix.ty); @@ -126,6 +112,9 @@ class FlxRepeatSprite extends FlxSpriteExt var scaleY = scaleY(); var fw:Float = frameWidth * scaleX; // TODO: replace this shit same way as Height + var hasColors = (colorTransform != null) ? (colorTransform.hasRGBMultipliers() || colorTransform.hasRGBAOffsets()) : false; + var quad = camera.startQuadBatch(_frame.parent, hasColors, hasColors, blend, antialiasing, shader); + switch (drawStyle) { // Draw from left top to right bottom style @@ -153,7 +142,7 @@ class FlxRepeatSprite extends FlxSpriteExt _matrix.ty = point.y + (addX * _sinAngle) + (addY * _cosAngle); tempPoint.set(addX,addY); - drawTile(xi, yi, _frame, frame, framePixels, tempPoint, camera); + drawTile(xi, yi, _frame, frame, quad, tempPoint, camera); } } // Draw from bottom to top style @@ -185,7 +174,7 @@ class FlxRepeatSprite extends FlxSpriteExt _matrix.ty = point.y + (addX * _sinAngle) + (heightPos * _cosAngle); tempPoint.set(addX,heightPos); - drawTile(xi, yi, _frame, frame, framePixels, tempPoint, camera); + drawTile(xi, yi, _frame, frame, quad, tempPoint, camera); } } } @@ -199,20 +188,21 @@ class FlxRepeatSprite extends FlxSpriteExt // Prepare tile dimensions function setupTile(tileX:Int, tileY:Int, baseFrame:FlxFrame) { - var frame = baseFrame.frame; - var point = tempPoint.set(frame.width * scaleX(), frame.height * scaleY()); - _frame.frame.copyFrom(frame); + var rect = baseFrame.frame; + var point = tempPoint.set(rect.width * scaleX(), rect.height * scaleY()); + _frame.frame.copyFrom(rect); _frame.angle = baseFrame.angle; return point; } var tileOffset:FlxPoint = FlxPoint.get(); - function drawTile(tileX:Int, tileY:Int, tileFrame:FlxFrame, baseFrame:FlxFrame, bitmap:BitmapData, tilePos:FlxPoint, camera:FlxCamera):Void + function drawTile(tileX:Int, tileY:Int, tileFrame:FlxFrame, baseFrame:FlxFrame, quad:FlxDrawQuadsItem, tilePos:FlxPoint, camera:FlxCamera):Void { // Do cliprect stuff - var doDraw:Bool = clipRect != null ? handleClipRect(tileFrame, baseFrame, tilePos) : true; - if (tileRect != null) tileFrame = tileFrame.clipTo(tileRect); + var doDraw:Bool = (clipRect != null) ? handleClipRect(tileFrame, baseFrame, tilePos) : true; + if (tileRect != null) + tileFrame = tileFrame.clipTo(tileRect); var mTx = _matrix.tx; var mTy = _matrix.ty; @@ -223,21 +213,14 @@ class FlxRepeatSprite extends FlxSpriteExt translateWithTrig(-tileOffset.x, -tileOffset.y); if (rectInBounds(mTx, mTy, tileFrame.frame.width, tileFrame.frame.height, camera)) // dont draw stuff out of bounds - drawTileToCamera(tileFrame, bitmap, _matrix, camera); + quad.addQuad(tileFrame, _matrix, colorTransform); translateWithTrig(tileOffset.x, tileOffset.y); tileOffset.set(); } } - inline function drawTileToCamera(tileFrame:FlxFrame, bitmap:BitmapData, tileMatrix:FlxMatrix, camera:FlxCamera) { - camera.drawPixels(tileFrame, bitmap, tileMatrix, colorTransform, blend, antialiasing, shader); - } - - @:noCompletion inline private function scaleX() return scale.x * lodScale; - - @:noCompletion inline private function scaleY() return scale.y * lodScale; inline function rectInBounds(x:Float, y:Float, w:Float, h:Float, cam:FlxCamera):Bool { @@ -257,6 +240,7 @@ class FlxRepeatSprite extends FlxSpriteExt final frame = tileFrame.frame; final baseFrame = baseFrame.frame; + final scaleX = scaleX(); // Cut if clipping left @@ -277,7 +261,7 @@ class FlxRepeatSprite extends FlxSpriteExt } } - var scaleY = scaleY(); + final scaleY = scaleY(); // Cut if clipping top if (tilePos.y < 0) { diff --git a/source/funkin/graphics/FlxSkewRepeatSprite.hx b/source/funkin/graphics/FlxSkewRepeatSprite.hx index 3022ab50..83677b83 100644 --- a/source/funkin/graphics/FlxSkewRepeatSprite.hx +++ b/source/funkin/graphics/FlxSkewRepeatSprite.hx @@ -1,5 +1,6 @@ package funkin.graphics; +import flixel.graphics.tile.FlxDrawQuadsItem; import flixel.math.FlxMatrix; import flixel.graphics.frames.FlxFrame; import openfl.display.BitmapData; @@ -20,9 +21,9 @@ class FlxSkewRepeatSprite extends FlxRepeatSprite static var scaledWiggleX:Float = 1.0; - override function drawTile(tileX:Int, tileY:Int, tileFrame:FlxFrame, baseFrame:FlxFrame, bitmap:BitmapData, tilePos:FlxPoint, camera:FlxCamera) { + override function drawTile(tileX:Int, tileY:Int, tileFrame:FlxFrame, baseFrame:FlxFrame, quad:FlxDrawQuadsItem, tilePos:FlxPoint, camera:FlxCamera) { if (wigglePower == 0) { - super.drawTile(tileX, tileY, tileFrame, baseFrame, bitmap, tilePos, camera); + super.drawTile(tileX, tileY, tileFrame, baseFrame, quad, tilePos, camera); return; } @@ -40,7 +41,7 @@ class FlxSkewRepeatSprite extends FlxRepeatSprite if (clipRect == null) offsetSkew(tileFrame, baseFrame); - super.drawTile(tileX, idY, tileFrame, baseFrame, bitmap, tilePos, camera); + super.drawTile(tileX, idY, tileFrame, baseFrame, quad, tilePos, camera); } inline function isLeftSkew():Bool { diff --git a/source/funkin/graphics/FlxSpriteExt.hx b/source/funkin/graphics/FlxSpriteExt.hx index 0cf7fe6d..128fbaf6 100644 --- a/source/funkin/graphics/FlxSpriteExt.hx +++ b/source/funkin/graphics/FlxSpriteExt.hx @@ -176,9 +176,6 @@ class FlxSpriteExt extends FlxSkewedSprite checkEmptyFrame(); if (_frame.type == EMPTY) return; - - if (dirty) - calcFrame(useFramePixels); // rarely cameras.fastForEach((camera, i) -> { if (camera.visible) if (camera.exists) if (isOnScreen(camera)) { diff --git a/source/funkin/objects/Character.hx b/source/funkin/objects/Character.hx index 5385b42b..f6fc5358 100644 --- a/source/funkin/objects/Character.hx +++ b/source/funkin/objects/Character.hx @@ -261,7 +261,7 @@ class Character extends FlxSpriteExt } } - var heyTimer:FlxTimer = null; + var heyTimer:FlxTimer; public function hey():Void { final heyAnim = isGF ? 'cheer' : 'hey'; @@ -269,15 +269,16 @@ class Character extends FlxSpriteExt playAnim(heyAnim, true); specialAnim = true; - if (heyTimer != null) - heyTimer.cancel(); - heyTimer = new FlxTimer().start(Conductor.crochetMills, function(tmr:FlxTimer) { + (heyTimer == null) ? heyTimer = new FlxTimer() : heyTimer.cancel(); + heyTimer.start(Conductor.crochetMills, (tmr:FlxTimer) -> { specialAnim = false; - final curAnim = animation.curAnim; - if (curAnim == null) return; - if (curAnim.name == 'hey' || curAnim.name == 'cheer') - restartDance(); + if (animation.curAnim != null) + { + var name = animation.curAnim.name; + if (name == 'hey' || name == 'cheer') + restartDance(); + } }); } @@ -292,21 +293,27 @@ class Character extends FlxSpriteExt public var danced:Bool = false; public var curDanceBeat:Int = 0; - public function inIdle() { - final curAnim = animation.curAnim; - if (curAnim == null) return false; - return curAnim.name.startsWith('dance') || curAnim.name.startsWith('idle'); + public function inIdle():Bool + { + if (animation.curAnim == null) + return false; + + var name = animation.curAnim.name; + return name.startsWith("dance") || name.startsWith("idle"); } - public function dance() { + public function dance():Void { if (!debugMode) if (forceDance) if (!specialAnim) getDanceAnim(); } - public function danceInBeat() { - final curAnim = animation.curAnim; - if (curAnim == null) return; - if (!animation.curAnim.name.startsWith("sing")) { + public function danceInBeat():Void + { + if (animation.curAnim == null) + return; + + if (!animation.curAnim.name.startsWith("sing")) + { curDanceBeat--; if (curDanceBeat < 0) { curDanceBeat = danceBeat; @@ -319,7 +326,7 @@ class Character extends FlxSpriteExt public inline function danceCheck() // Backwards compatibility lol danceInBeat(); - function isDoubleDancer() { + function isDoubleDancer():Bool { for(i in animOffsets.keys()) { if (i.startsWith("danceRight")) return true;