Skip to content

Commit

Permalink
simplify screen bounds math shit
Browse files Browse the repository at this point in the history
  • Loading branch information
MaybeMaru committed May 6, 2024
1 parent b7a60f8 commit bbcf9e2
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 96 deletions.
14 changes: 7 additions & 7 deletions source/funkin/graphics/FlxBackdropExt.hx
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ class FlxBackdropExt extends FlxBackdrop
}

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

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

if (flipY != frame.flipY) {
CoolUtil.matrixScale(mat, 1, -1);
FunkMath.scaleMatrix(mat, 1, -1);
mat.tx = (mat.tx + frame.sourceSize.y);
}
}
Expand All @@ -82,13 +82,13 @@ class FlxBackdropExt extends FlxBackdrop
(_frame.frame.height + spacing.y) * scale.y * lodScale,
);

CoolUtil.matrixScale(_matrix, scale.x, scale.y);
FunkMath.scaleMatrix(_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);
final rads:Float = angle * FunkMath.TO_RADS;
_cosAngle = FunkMath.cos(rads);
_sinAngle = FunkMath.sin(rads);
_angleChanged = false;
}
_matrix.rotateWithTrig(_cosAngle, _sinAngle);
Expand Down
4 changes: 2 additions & 2 deletions source/funkin/graphics/FlxFunkText.hx
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,11 @@ class FlxFunkText extends FlxSpriteExt {
point.copyFrom(offset);
size *= sizeMult();

final qualityDiv = CoolUtil.DOUBLE_PI / quality;
final qualityDiv = FunkMath.DOUBLE_PI / quality;
for (i in 0...quality) {
final rads = i * qualityDiv;
offset.set(point.x, point.y);
offset.add(CoolUtil.cos(rads) * size, CoolUtil.sin(rads) * size);
offset.add(FunkMath.cos(rads) * size, FunkMath.sin(rads) * size);
__superDrawComplex(camera);
}

Expand Down
18 changes: 10 additions & 8 deletions source/funkin/graphics/FlxRepeatSprite.hx
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,16 @@ class FlxRepeatSprite extends FlxSpriteExt
inline private function scaleX() return scale.x * lodScale;
inline private function scaleY() return scale.y * lodScale;

inline function rectInBounds(x:Float, y:Float, w:Float, h:Float, cam:FlxCamera):Bool {
var rect = CoolUtil.rect.set(
x,
y,
w * Math.abs(scaleX()),
h * Math.abs(scaleY())
);
return cam.containsRect(rect.getRotatedBounds(angle, null, rect));
inline function rectInBounds(x:Float, y:Float, w:Float, h:Float, cam:FlxCamera):Bool
{
var rect = _rect;
rect.x = x;
rect.y = y;
rect.width = w * Math.abs(scaleX());
rect.height = h * Math.abs(scaleY());

FunkMath.fastRotatedTrigRect(rect, _cosAngle, _sinAngle, angle);
return cam.containsRect(rect);
}

function handleClipRect(tileFrame:FlxFrame, baseFrame:FlxFrame, tilePos:FlxPoint):Bool
Expand Down
2 changes: 1 addition & 1 deletion source/funkin/graphics/FlxSkewRepeatSprite.hx
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class FlxSkewRepeatSprite extends FlxRepeatSprite
var lerpWiggle = FlxMath.lerp(0, scaledWiggleX, lerpValue);

var skewX = isLeftSkew() ? -lerpWiggle : lerpWiggle;
_matrix.c = Math.tan(skewX * CoolUtil.TO_RADS); // Set skew X
_matrix.c = Math.tan(skewX * FunkMath.TO_RADS); // Set skew X

if (clipRect == null)
offsetSkew(tileFrame, baseFrame);
Expand Down
50 changes: 27 additions & 23 deletions source/funkin/graphics/FlxSpriteExt.hx
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,9 @@ class FlxSpriteExt extends FlxSkewedSprite
@:noCompletion
private inline function __updateTrig():Void {
if (_angleChanged) {
final rads:Float = angle * CoolUtil.TO_RADS;
_cosAngle = CoolUtil.cos(rads);
_sinAngle = CoolUtil.sin(rads);
final rads:Float = angle * FunkMath.TO_RADS;
_cosAngle = FunkMath.cos(rads);
_sinAngle = FunkMath.sin(rads);
_angleChanged = false;

applyCurOffset(false); // Update display angle offset
Expand Down Expand Up @@ -282,13 +282,13 @@ class FlxSpriteExt extends FlxSkewedSprite

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

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

@:privateAccess {
Expand All @@ -308,15 +308,15 @@ class FlxSpriteExt extends FlxSkewedSprite
}

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

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

if (flipY != frame.flipY) {
CoolUtil.matrixScale(mat, 1, -1);
FunkMath.scaleMatrix(mat, 1, -1);
mat.tx = (mat.tx + frame.sourceSize.y);
}
}
Expand All @@ -335,7 +335,7 @@ class FlxSpriteExt extends FlxSkewedSprite
_matrix.tx = (_matrix.tx - origin.x);
_matrix.ty = (_matrix.ty - origin.y);

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

if (angle != 0) {
__updateTrig();
Expand All @@ -344,13 +344,12 @@ class FlxSpriteExt extends FlxSkewedSprite

if (skew.x != 0 || skew.y != 0) {
_skewMatrix.identity();
_skewMatrix.b = Math.tan(skew.y * CoolUtil.TO_RADS);
_skewMatrix.c = Math.tan(skew.x * CoolUtil.TO_RADS);
_skewMatrix.b = Math.tan(skew.y * FunkMath.TO_RADS);
_skewMatrix.c = Math.tan(skew.x * FunkMath.TO_RADS);
_matrix.concat(_skewMatrix);
}

final point = _point;
getScreenPosition(point, camera);
final point = getScreenPosition(_point, camera);
_matrix.tx = (_matrix.tx + point.x + origin.x - offset.x);
_matrix.ty = (_matrix.ty + point.y + origin.y - offset.y);
}
Expand All @@ -367,16 +366,21 @@ class FlxSpriteExt extends FlxSkewedSprite

@:noCompletion
private inline function __superGetScreenBounds(?newRect:FlxRect, ?camera:FlxCamera):FlxRect {
if (newRect == null) newRect = CoolUtil.rect;
if (camera == null) camera = FlxG.camera;
newRect ??= CoolUtil.rect;
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 * lodScale), frameHeight * Math.abs(scale.y * lodScale));
_scaledOrigin.x = origin.x * scale.x;
_scaledOrigin.y = origin.y * scale.y;

return newRect.getRotatedBounds(angle, _scaledOrigin, newRect);
newRect.setPosition(
x + (-Std.int(camera.scroll.x * scrollFactor.x) - offset.x + origin.x - _scaledOrigin.x),
y + (-Std.int(camera.scroll.y * scrollFactor.y) - offset.y + origin.y - _scaledOrigin.y)
);

newRect.width = frameWidth * Math.abs(scale.x * lodScale);
newRect.height = frameHeight * Math.abs(scale.y * lodScale);

return FunkMath.fastRotatedRect(newRect, _scaledOrigin, angle);
}

public function switchAnim(anim1:String, anim2:String):Void {
Expand Down
9 changes: 5 additions & 4 deletions source/funkin/objects/FunkCamera.hx
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,8 @@ class FunkCamera extends FlxCamera {
}
}

class AngledCamera extends FunkCamera {
class AngledCamera extends FunkCamera
{
@:noCompletion
private var _sin(default, null):Float = 0.0;

Expand All @@ -310,9 +311,9 @@ class AngledCamera extends FunkCamera {

override function set_angle(value:Float):Float {
if (value != angle) {
final rads:Float = value * CoolUtil.TO_RADS;
_sin = CoolUtil.sin(rads);
_cos = CoolUtil.cos(rads);
final rads:Float = value * FunkMath.TO_RADS;
_sin = FunkMath.sin(rads);
_cos = FunkMath.cos(rads);
}
return angle = value;
}
Expand Down
6 changes: 3 additions & 3 deletions source/funkin/objects/note/BasicNote.hx
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ class BasicNote extends SmartSprite implements INoteData
var _approachSin(default, null):Float = 0.0;

inline function calcApproachTrig(value:Float):Void {
final rads = value * CoolUtil.TO_RADS;
_approachCos = CoolUtil.cos(rads);
_approachSin = CoolUtil.sin(rads);
final rads = value * FunkMath.TO_RADS;
_approachCos = FunkMath.cos(rads);
_approachSin = FunkMath.sin(rads);
}

public var spawnMult:Float = 1.0;
Expand Down
6 changes: 3 additions & 3 deletions source/funkin/states/menus/TitleState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class TitleState extends MusicBeatState

if (initialized && !transitioning && titleText != null) {
titleSine += elapsed * 3;
var lerp:Float = FlxMath.remapToRange(FlxMath.fastSin(titleSine %= CoolUtil.DOUBLE_PI), -1, 1, 0, 1);
var lerp:Float = FlxMath.remapToRange(FlxMath.fastSin(titleSine %= FunkMath.DOUBLE_PI), -1, 1, 0, 1);
titleText.color = FlxColor.interpolate(0xFF3333CC, 0xFF33FFFF, lerp);
#if !mobile checkCode(); #end
}
Expand Down Expand Up @@ -205,13 +205,13 @@ class TitleState extends MusicBeatState
Shader.setFloat('colorSwap', 'iTime', shaderColor);
}

var musicPitch:Float = CoolUtil.PI + 0.148; // Shhhh
var musicPitch:Float = FunkMath.PI + 0.148; // Shhhh

function updatePitch(elapsed:Float) {
musicPitch += elapsed / FlxG.timeScale;

if (FlxG.sound.music != null) {
var pitch = FlxMath.remapToRange(CoolUtil.sin(musicPitch), -1, 1, 0.25, 2);
var pitch = FlxMath.remapToRange(FunkMath.sin(musicPitch), -1, 1, 0.25, 2);
FlxG.sound.music.pitch = pitch;
FlxG.timeScale = pitch;
}
Expand Down
40 changes: 2 additions & 38 deletions source/funkin/util/CoolUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -60,21 +60,6 @@ class CoolUtil {
return mat;
}

public static inline function matrixScale(mat:FlxMatrix, sx:Float, sy:Float):Void
{
if (sx != 1) {
mat.a = (mat.a * sx);
mat.c = (mat.c * sx);
mat.tx = (mat.tx * sx);
}

if (sy != 1) {
mat.b = (mat.b * sy);
mat.d = (mat.d * sy);
mat.ty = (mat.ty * sy);
}
}

inline public static function openUrl(url:String) {
#if linux
Sys.command('/usr/bin/xdg-open', [url, "&"]);
Expand Down Expand Up @@ -199,27 +184,6 @@ class CoolUtil {
resumeSoundsList.splice(0, resumeSoundsList.length);
}

public static inline var PI:Float = 3.14159265358979323846;
public static inline var DOUBLE_PI:Float = PI * 2;
public static inline var TO_RADS:Float = PI / 180;
public static inline var TO_DEGREES:Float = 180 / PI;

public static inline function sin(radians:Float) {
return #if FAST_MATH FlxMath.fastSin(radians); #else Math.sin(radians); #end
}

public static inline function cos(radians:Float) {
return #if FAST_MATH FlxMath.fastCos(radians); #else Math.cos(radians); #end
}

public static inline function sinAngle(angle:Float) {
return sin(angle * TO_RADS);
}

public static inline function cosAngle(angle:Float) {
return cos(angle * TO_RADS);
}

public static inline function positionInCenter(object:FlxObject, object2:FlxObject, setToPosition:Bool = false) {
object.x = (object2.width - object.width) * .5;
object.y = (object2.height - object.height) * .5;
Expand All @@ -244,8 +208,8 @@ class CoolUtil {
}

public static inline function translateWithAngle(object:FlxObject, x:Float = 0.0, y:Float = 0.0, angle:Float = 0.0) {
var rads:Float = angle * TO_RADS;
translateWithTrig(object, x, y, sin(rads), cos(rads));
final rads:Float = angle * FunkMath.TO_RADS;
translateWithTrig(object, x, y, FunkMath.sin(rads), FunkMath.cos(rads));
}

public static inline function offsetWithAngle(object:FlxObject, x:Float = 0.0, y:Float = 0.0, angle:Float = 0.0) {
Expand Down
Loading

0 comments on commit bbcf9e2

Please sign in to comment.