Skip to content

Commit

Permalink
draw directly to the quad in repeat sprite
Browse files Browse the repository at this point in the history
  • Loading branch information
MaybeMaru committed Apr 12, 2024
1 parent 2b6382e commit d6df14c
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 99 deletions.
25 changes: 13 additions & 12 deletions source/SplashState.hx
Original file line number Diff line number Diff line change
@@ -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());
});
});

Expand All @@ -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}]');

Expand All @@ -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);
}
}
9 changes: 6 additions & 3 deletions source/funkin/Preloader.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {
Expand Down
42 changes: 30 additions & 12 deletions source/funkin/Transition.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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() {
Expand All @@ -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;
Expand All @@ -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);
Expand Down
80 changes: 32 additions & 48 deletions source/funkin/graphics/FlxRepeatSprite.hx
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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
}
});
}
Expand All @@ -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);
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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;
Expand All @@ -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 {
Expand All @@ -257,6 +240,7 @@ class FlxRepeatSprite extends FlxSpriteExt

final frame = tileFrame.frame;
final baseFrame = baseFrame.frame;

final scaleX = scaleX();

// Cut if clipping left
Expand All @@ -277,7 +261,7 @@ class FlxRepeatSprite extends FlxSpriteExt
}
}

var scaleY = scaleY();
final scaleY = scaleY();

// Cut if clipping top
if (tilePos.y < 0) {
Expand Down
7 changes: 4 additions & 3 deletions source/funkin/graphics/FlxSkewRepeatSprite.hx
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
}

Expand All @@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions source/funkin/graphics/FlxSpriteExt.hx
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down
Loading

0 comments on commit d6df14c

Please sign in to comment.