Skip to content

Commit

Permalink
quick n dirty lod support for FlxBackdrop
Browse files Browse the repository at this point in the history
  • Loading branch information
MaybeMaru committed Apr 12, 2024
1 parent d6df14c commit 2070ffa
Show file tree
Hide file tree
Showing 6 changed files with 162 additions and 7 deletions.
1 change: 0 additions & 1 deletion assets/songs/blammed/cutscene.hx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import flixel.addons.display.FlxBackdrop;
import flixel.util.FlxGradient;

var cars = [];
Expand Down
153 changes: 153 additions & 0 deletions source/funkin/graphics/FlxBackdropExt.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package funkin.graphics;

import flixel.graphics.frames.FlxFrame;
import flixel.addons.display.FlxBackdrop;
import flixel.math.FlxMatrix;
import flixel.util.FlxAxes;

using flixel.util.FlxColorTransformUtil;

// TODO: make this override FlxSpriteExt instead, im just lazy to convert the rest of the class rn lmao
class FlxBackdropExt extends FlxBackdrop
{
var tileMatrix:FlxMatrix;
var tileSize:FlxPoint = FlxPoint.get();

public function new(?graphic:Null<FlxGraphicAsset>, repeatAxes:FlxAxes = XY, spacingX:Float = 0.0, spacingY:Float = 0.0) {
super(graphic, repeatAxes, spacingX, spacingY);

this.tileMatrix = new FlxMatrix();
}

override function destroy() {
super.destroy();
tileMatrix = null;
tileSize = FlxDestroyUtil.put(tileSize);
}

private inline function prepareFrameMatrix(frame:FlxFrame, mat:FlxMatrix):Void
{
var flipX = (flipX != _frame.flipX);
var flipY = (flipY != _frame.flipY);

if (animation.curAnim != null)
{
flipX != animation.curAnim.flipX;
flipY != animation.curAnim.flipY;
}

@:privateAccess {
final tileMat = frame.tileMatrix;
mat.a = tileMat[0];
mat.b = tileMat[1];
mat.c = tileMat[2];
mat.d = tileMat[3];
mat.tx = tileMat[4];
mat.ty = tileMat[5];
}

if (frame.angle == 180) {
mat.rotateBy180();
mat.tx = (mat.tx + frame.sourceSize.y);
mat.ty = (mat.ty + frame.sourceSize.x);
}

if (lodScale != 1.0)
CoolUtil.matrixScale(mat, lodScale, lodScale);

if (flipX != frame.flipX) {
CoolUtil.matrixScale(mat, -1, 1);
mat.tx = (mat.tx + frame.sourceSize.x);
}

if (flipY != frame.flipY) {
CoolUtil.matrixScale(mat, 1, -1);
mat.tx = (mat.tx + frame.sourceSize.y);
}
}

override function drawComplex(camera:FlxCamera)
{
if (repeatAxes == NONE)
{
super.drawComplex(camera);
return;
}

prepareFrameMatrix(_frame, _matrix);
_matrix.translate(-origin.x, -origin.y);

tileSize.set(
(_frame.frame.width + spacing.x) * scale.x * lodScale,
(_frame.frame.height + spacing.y) * scale.y * lodScale,
);

CoolUtil.matrixScale(_matrix, scale.x, scale.y);

if (angle != 0) {
if (_angleChanged) {
final rads:Float = angle * CoolUtil.TO_RADS;
_cosAngle = CoolUtil.cos(rads);
_sinAngle = CoolUtil.sin(rads);
_angleChanged = false;
}
_matrix.rotateWithTrig(_cosAngle, _sinAngle);
}

getScreenPosition(_point, camera).subtractPoint(offset);

var tilesX = 1;
var tilesY = 1;

final viewMargins = camera.getViewMarginRect();
final bounds = getScreenBounds(camera);

if (repeatAxes.x)
{
final origTileSizeX = (frameWidth + spacing.x) * scale.x;
final left = modMin(bounds.right, origTileSizeX, viewMargins.left) - bounds.width;
final right = modMax(bounds.left, origTileSizeX, viewMargins.right) + origTileSizeX;
tilesX = Math.round((right - left) / tileSize.x);
_point.x = left + _point.x - bounds.x;
}

if (repeatAxes.y)
{
final origTileSizeY = (frameHeight + spacing.y) * scale.y;
final top = modMin(bounds.bottom, origTileSizeY, viewMargins.top) - bounds.height;
final bottom = modMax(bounds.top, origTileSizeY, viewMargins.bottom) + origTileSizeY;
tilesY = Math.round((bottom - top) / tileSize.y);
_point.y = top + _point.y - bounds.y;
}

viewMargins.put();
bounds.put();

_point.addPoint(origin);

var hasColors = (colorTransform != null) ? (colorTransform.hasRGBMultipliers() || colorTransform.hasRGBAOffsets()) : false;
var quad = camera.startQuadBatch(_frame.parent, hasColors, hasColors, blend, antialiasing, shader);

for (tileX in 0...tilesX)
{
for (tileY in 0...tilesY)
{
tileMatrix.copyFrom(_matrix);
tileMatrix.translate(_point.x + (tileSize.x * tileX), _point.y + (tileSize.y * tileY));
quad.addQuad(_frame, tileMatrix, colorTransform);
}
}
}

public var lodScale(default, null):Float = 1.0;

override function set_graphic(value:FlxGraphic):FlxGraphic
{
if (graphic != value) {
lodScale = (value is LodGraphic) ? cast(value, LodGraphic).lodScale : 1.0;
graphic = value;
}

return value;
}
}
5 changes: 1 addition & 4 deletions source/funkin/states/menus/MainMenuState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ class MainMenuState extends MusicBeatState {
menuItem.ID = i;
}

var menuCam:FlxCamera = new FlxCamera();
FlxG.cameras.add(menuCam);
FlxG.cameras.setDefaultDrawTarget(menuCam, true);
menuCam.follow(camFollow, null, 0.06);
FlxG.camera.follow(camFollow, null, 0.06);

var versionText:String = 'Mau Engin v${Main.engineVersion}\nFriday Night Funkin v${Application.current.meta.get('version')}';

Expand Down
6 changes: 5 additions & 1 deletion source/funkin/states/options/items/ModItem.hx
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ class ModItem extends FlxSpriteGroup {
enableButton = new FlxSpriteExt(modBox.width,modBox.height).loadImageTiled('options/modButton', 60, 58);
enableButton.animation.add('on', [0]);
enableButton.animation.add('off', [1]);
add(enableButton);

enableButton.width *= enableButton.lodScale;
enableButton.height *= enableButton.lodScale;

enableButton.x -= enableButton.width + 5;
enableButton.y -= enableButton.height + 5;
add(enableButton);

enabled = ModdingUtil.activeMods.get(mod.folder);
updateUI();
Expand Down
1 change: 1 addition & 0 deletions source/funkin/util/modding/FunkScript.hx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ class FunkScript extends hscript.Script implements IFlxDestroyable {
set('FlxG', flixel.FlxG);
set('FlxSpriteExt', funkin.graphics.FlxSpriteExt); // Both r the same lol, just for backwards compatibility
set('FlxSprite', funkin.graphics.FlxSpriteExt);
set('FlxBackdrop', funkin.graphics.FlxBackdropExt); // Just a lil fix for lod
set('FlxText', flixel.text.FlxText);
set('FlxTypedGroup', TypedGroup);
set('FlxSpriteGroup', flixel.group.FlxSpriteGroup);
Expand Down
3 changes: 2 additions & 1 deletion source/funkin/util/song/Conductor.hx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ typedef BPMChangeEvent = {
var bpm:Float;
}

class Conductor {
class Conductor
{
inline public static var NOTE_DATA_LENGTH:Int = 4;
inline public static var STRUMS_LENGTH:Int = NOTE_DATA_LENGTH * 2;

Expand Down

0 comments on commit 2070ffa

Please sign in to comment.