Skip to content
This repository has been archived by the owner on Jan 26, 2025. It is now read-only.

Commit

Permalink
fix and optimize check in screen bounds
Browse files Browse the repository at this point in the history
  • Loading branch information
MaybeMaru committed Jan 21, 2024
1 parent 2cea6c2 commit fa92383
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 17 deletions.
1 change: 0 additions & 1 deletion source/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ class InitState extends FlxState {
override function update(elapsed:Float) {
super.update(elapsed);
var accept = Controls.getKey('ACCEPT-P');
var back = Controls.getKey('BACK-P');

if (accept || Controls.getKey('BACK-P') && !selected) {
selected = true;
Expand Down
27 changes: 14 additions & 13 deletions source/funkin/graphics/FlxRepeatSprite.hx
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,15 @@ class FlxRepeatSprite extends FlxSpriteExt {
}

override function getScreenBounds(?newRect:FlxRect, ?camera:FlxCamera):FlxRect {
if (newRect == null) newRect = FlxRect.get();
if (newRect == null) newRect = CoolUtil.rect;
if (camera == null) camera = FlxG.camera;

newRect.setPosition(x, y);
_scaledOrigin.set(origin.x * scale.x, origin.y * scale.y);
newRect.x += -Std.int(camera.scroll.x * scrollFactor.x) - offset.x + origin.x - _scaledOrigin.x;
newRect.y += -Std.int(camera.scroll.y * scrollFactor.y) - offset.y + origin.y - _scaledOrigin.y;
newRect.setSize(repeatWidth, repeatHeight);
newRect.setSize(repeatWidth, repeatHeight);

return newRect.getRotatedBounds(angle, _scaledOrigin, newRect);
}

Expand Down Expand Up @@ -202,10 +204,15 @@ class FlxRepeatSprite extends FlxSpriteExt {
if (tileRect != null) tileFrame = tileFrame.clipTo(tileRect);

var lastMatrix = __lastMatrix;
if (doDraw && (lastMatrix.x != _matrix.tx || lastMatrix.y != _matrix.ty)) {
lastMatrix.set(_matrix.tx, _matrix.ty);
var mTx = _matrix.tx;
var mTy = _matrix.ty;

if (doDraw && (lastMatrix.x != mTx || lastMatrix.y != mTy)) {
lastMatrix.set(mTx, mTy);
translateWithTrig(-tileOffset.x, -tileOffset.y);
if (!matrixOutOfBounds(_matrix, tileFrame.frame, __drawCam)) // dont draw stuff out of bounds

var frame = tileFrame.frame;
if (rectInBounds(mTx, mTy, frame.width, frame.height, __drawCam)) // dont draw stuff out of bounds
drawTileToCamera(tileFrame, bitmap, _matrix, __drawCam);

translateWithTrig(tileOffset.x, tileOffset.y);
Expand All @@ -217,14 +224,8 @@ class FlxRepeatSprite extends FlxSpriteExt {
camera.drawPixels(tileFrame, bitmap, tileMatrix, colorTransform, blend, antialiasing, shader);
}

public var boundsOffsetX:Float = 0.0;
public var boundsOffsetY:Float = 0.0;

inline function matrixOutOfBounds(matrix:FlxMatrix, frame:FlxRect, cam:FlxCamera):Bool {
return ((_matrix.ty + (frame.height * scale.y) - boundsOffsetY) < cam.viewY) ||
((_matrix.ty - (frame.height * scale.y) + boundsOffsetY) > cam.viewHeight) ||
((_matrix.tx + (frame.width * scale.x) - boundsOffsetX) < cam.viewX) ||
((_matrix.tx - (frame.width * scale.x) + boundsOffsetX) > cam.viewWidth);
inline function rectInBounds(x:Float, y:Float, w:Float, h:Float, cam:FlxCamera):Bool {
return cam.containsRect(CoolUtil.rect.set(x, y, w * Math.abs(scale.x), h * Math.abs(scale.y)));
}

function handleClipRect(tileFrame:FlxFrame, baseFrame:FlxFrame, tilePos:FlxPoint) {
Expand Down
18 changes: 16 additions & 2 deletions source/funkin/graphics/FlxSpriteExt.hx
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,25 @@ class FlxSpriteExt extends FlxSkewedSprite {
public override function getScreenBounds(?rect:FlxRect, ?cam:FlxCamera):FlxRect {
if (flippedOffsets) {
scale.x = -scale.x;
final bounds = super.getScreenBounds(rect, cam);
var bounds = __superGetScreenBounds(rect, cam);
scale.x = -scale.x;
return bounds;
}
return super.getScreenBounds(rect, cam);
return __superGetScreenBounds(rect, cam);
}

@:noCompletion
private inline function __superGetScreenBounds(?newRect:FlxRect, ?camera:FlxCamera):FlxRect {
if (newRect == null) newRect = CoolUtil.rect;
if (camera == null) camera = FlxG.camera;

newRect.setPosition(x, y);
_scaledOrigin.set(origin.x * scale.x, origin.y * scale.y);
newRect.x += -Std.int(camera.scroll.x * scrollFactor.x) - offset.x + origin.x - _scaledOrigin.x;
newRect.y += -Std.int(camera.scroll.y * scrollFactor.y) - offset.y + origin.y - _scaledOrigin.y;
newRect.setSize(frameWidth * Math.abs(scale.x), frameHeight * Math.abs(scale.y));

return newRect.getRotatedBounds(angle, _scaledOrigin, newRect);
}

public function switchAnim(anim1:String, anim2:String):Void {
Expand Down
1 change: 0 additions & 1 deletion source/funkin/states/editors/chart/ChartGridBase.hx
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,6 @@ class ChartSustain extends Sustain {
angle = 0;
flipX = false;
active = false;
boundsOffsetY = -FlxG.height;
}

public var chartData:Null<Array<Dynamic>> = null;
Expand Down
1 change: 1 addition & 0 deletions source/funkin/util/CoolUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class CoolUtil {
}

public static var point:FlxPoint = FlxPoint.get(); // Global FlxPoint for quick calculations
public static var rect:FlxRect = FlxRect.get(); // Global FlxRect for quick calculations

inline public static function openUrl(url:String) {
#if linux
Expand Down

0 comments on commit fa92383

Please sign in to comment.