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

Commit

Permalink
the good, the bad and the gay
Browse files Browse the repository at this point in the history
  • Loading branch information
MaybeMaru committed Jan 31, 2024
1 parent c247a0e commit ec29336
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 46 deletions.
5 changes: 5 additions & 0 deletions example_mods/TemplateScript.hx
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,11 @@ function endWeek()
// Called when the week is finished on story mode
}

function exitFreeplay()
{
// Called when a freeplay song is finished
}

function switchSong(nextSongName:String, nextSongDifficulty:String)
{
// Called when the next song on the story mode playlist is loaded
Expand Down
1 change: 0 additions & 1 deletion source/funkin/Preferences.hx
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class Preferences {
addPref('resolution', 'resolution', {array:resolutions, value:default_resolution});
#end
#if !hl
addPref('clear-gpu', 'clear gpu cache', true);
addPref('preload', 'preload at start', true);
#end

Expand Down
3 changes: 2 additions & 1 deletion source/funkin/Preloader.hx
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ class Preloader extends FlxState {

final cacheStr = imageCache[0];
if (cacheStr != null) {
cacheImage(Paths.png(cacheStr));
var png = Paths.getPath('images/$cacheStr.png', IMAGE, null, false, false);
cacheImage(png);
imageCache.splice(imageCache.indexOf(cacheStr), 1);

cachePart.text = 'Preloading Sprites...\n' + cacheStr;
Expand Down
56 changes: 34 additions & 22 deletions source/funkin/graphics/FlxRepeatSprite.hx
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ class FlxRepeatSprite extends FlxSpriteExt {
// Fix bug of tiles duplicating
__lastMatrix.set(-1, -1);

final fw:Float = frameWidth * scale.x; // TODO: replace this shit same way as Height
var scaleX = scaleX();
var fw:Float = frameWidth * scaleX; // TODO: replace this shit same way as Height

switch (drawStyle) {
// Draw from left top to right bottom style
Expand All @@ -133,17 +134,17 @@ class FlxRepeatSprite extends FlxSpriteExt {
for (yi in 0...tilesY) {
setupTile(xi, yi, frame);

final addW = fw * (xi + 1);
var addW = fw * (xi + 1);
if (addW > repeatWidth) // Cut frame width
_frame.frame.width = (fw + (repeatWidth - addW)) / scale.x;
_frame.frame.width = (fw + (repeatWidth - addW)) / scaleX;

heightPos += tempPoint.y;
if (heightPos > repeatHeight) // Cut frame height
_frame.frame.height = (tempPoint.y + (repeatHeight - heightPos)) / scale.y;
_frame.frame.height = (tempPoint.y + (repeatHeight - heightPos)) / scaleY();

// Position and draw
final addX = addW - fw;
final addY = heightPos - tempPoint.y;
var addX = addW - fw;
var addY = heightPos - tempPoint.y;

_matrix.tx = point.x + (addX * _cosAngle) + (addY * -_sinAngle);
_matrix.ty = point.y + (addX * _sinAngle) + (addY * _cosAngle);
Expand All @@ -159,19 +160,20 @@ class FlxRepeatSprite extends FlxSpriteExt {
for (yi in 0...tilesY) {
setupTile(xi, yi, frame);

final addW = fw * (xi + 1);
var addW = fw * (xi + 1);
if (addW > repeatWidth) // Cut frame width
_frame.frame.width = (fw + (repeatWidth - addW)) / scale.x;
_frame.frame.width = (fw + (repeatWidth - addW)) / scaleX;

heightPos -= tempPoint.y;
if (heightPos < 0) {
_frame.frame.height += heightPos / scale.y;
_frame.frame.y -= heightPos / scale.y;
var scaleY = scaleY();
_frame.frame.height += heightPos / scaleY;
_frame.frame.y -= heightPos / scaleY;
heightPos = 0;
}

// Position and draw
final addX = addW - fw;
var addX = addW - fw;
_matrix.tx = point.x + (addX * _cosAngle) + (heightPos * -_sinAngle);
_matrix.ty = point.y + (addX * _sinAngle) + (heightPos * _cosAngle);

Expand All @@ -191,7 +193,7 @@ 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 * scale.y, frame.height * scale.y);
var point = __tempPoint.set(frame.width * scaleX(), frame.height * scaleY());
_frame.frame.copyFrom(frame);
_frame.angle = baseFrame.angle;
return point;
Expand Down Expand Up @@ -225,12 +227,18 @@ class FlxRepeatSprite extends FlxSpriteExt {
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 {
var rect = CoolUtil.rect.set(
x,
y,
w * Math.abs(scale.x),
h * Math.abs(scale.y)
w * Math.abs(scaleX()),
h * Math.abs(scaleY())
);
return cam.containsRect(rect.getRotatedBounds(angle, null, rect));
}
Expand All @@ -242,38 +250,42 @@ class FlxRepeatSprite extends FlxSpriteExt {
var frame = tileFrame.frame;
var baseFrame = baseFrame.frame;

var scaleX = scaleX();

// Cut if clipping left
if (tilePos.x < 0) {
var offX = tilePos.x / scale.x;
var offX = tilePos.x / scaleX;
frame.width += offX;
frame.x -= offX;
translateWithTrig(-offX * scale.x, 0);
translateWithTrig(-offX * scaleX, 0);
if (frame.width <= 0) return false; // Dont draw it
}

// Cut if clipping right
if ((clipRect.width - clipRect.x) < repeatWidth) {
var cutX = (tilePos.x + (baseFrame.width * scale.x)) - clipRect.width;
var cutX = (tilePos.x + (baseFrame.width * scaleX)) - clipRect.width;
if (cutX > 0) {
frame.width -= cutX / scale.x;
frame.width -= cutX / scaleX;
if (frame.width <= 0) return false; // Dont draw it
}
}

var scaleY = scaleY();

// Cut if clipping top
if (tilePos.y < 0) {
var offY = tilePos.y / scale.y;
var offY = tilePos.y / scaleY;
frame.height += offY;
frame.y -= offY;
translateWithTrig(0, -offY * scale.y);
translateWithTrig(0, -offY * scaleY);
if (frame.height <= 0) return false; // Dont draw it
}

// Cut if clipping bottom
if ((clipRect.height - clipRect.y) < repeatHeight) {
var cutY = (tilePos.y + (baseFrame.height * scale.y)) - clipRect.height;
var cutY = (tilePos.y + (baseFrame.height * scaleY)) - clipRect.height;
if (cutY > 0) {
frame.height -= cutY / scale.y;
frame.height -= cutY / scaleY;
if (frame.height <= 0) return false; // Dont draw it
}
}
Expand Down
10 changes: 6 additions & 4 deletions source/funkin/graphics/FlxSpriteExt.hx
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ class FlxSpriteExt extends FlxSkewedSprite {
if (flippedOffsets) {
flipX = !flipX;
scale.x = -scale.x;
lodScale = -lodScale;
__superDraw();
flipX = !flipX;
scale.x = -scale.x;
lodScale = -lodScale;
}
else __superDraw();
}
Expand Down Expand Up @@ -218,6 +220,10 @@ class FlxSpriteExt extends FlxSkewedSprite {
inline mat.translate(frame.sourceSize.y, frame.sourceSize.x);
}

if (lodScale != 1.0) {
inline mat.scale(lodScale, lodScale);
}

if (flipX != frame.flipX) {
inline mat.scale(-1, 1);
inline mat.translate(frame.sourceSize.x, 0);
Expand All @@ -227,10 +233,6 @@ class FlxSpriteExt extends FlxSkewedSprite {
inline mat.scale(1, -1);
inline mat.translate(0, frame.sourceSize.y);
}

if (lodScale != 1.0) {
inline mat.scale(lodScale, lodScale);
}
}

@:noCompletion
Expand Down
7 changes: 3 additions & 4 deletions source/funkin/states/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,8 @@ class PlayState extends MusicBeatState {
storyPlaylist.length <= 0 ? endWeek() : switchSong();
}
else {
trace('WENT BACK TO FREEPLAY??');
ModdingUtil.addCall('exitFreeplay');

clearCache = true;
switchState(new FreeplayState());
}
Expand Down Expand Up @@ -659,9 +660,7 @@ class PlayState extends MusicBeatState {
Conductor.stop();

clearCache = true;
clearCacheData = { // Clear last song audio and shaders
tempCache: false
}
clearCacheData = {tempCache: false, skins: false}
ModdingUtil.addCall('switchSong', [nextSong, curDifficulty]); // Could be used to change cache clear
switchState(new PlayState(), true);
}
Expand Down
7 changes: 5 additions & 2 deletions source/funkin/util/CoolUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import neko.vm.Gc;
typedef CacheClearing = {
?tempCache:Bool,
?staticCache:Bool,
?shaders:Bool
?shaders:Bool,
?skins:Bool
}

enum abstract SoundType(String) {
Expand Down Expand Up @@ -83,13 +84,15 @@ class CoolUtil {
static final DEFAULT_CACHE_CLEARING:CacheClearing = {
tempCache: true,
staticCache: false,
shaders: true
shaders: true,
skins: true
}

inline public static function clearCache(?cacheClear:CacheClearing, softClear:Bool = false) {
cacheClear = JsonUtil.checkJsonDefaults(DEFAULT_CACHE_CLEARING, cacheClear);

if (cacheClear.shaders) Shader.clearShaders();
if (cacheClear.skins) NoteUtil.clearSkinsData();

if (cacheClear.tempCache) AssetManager.clearTempCache(false);
if (cacheClear.staticCache) AssetManager.clearTempCache(false);
Expand Down
10 changes: 9 additions & 1 deletion source/funkin/util/NoteUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,15 @@ class NoteUtil {
skinSpriteMap.set(skin, skinData);
return skinData;
}

public static function clearSkinsData() {
for (key => data in skinSpriteMap) {
data.baseSprite = FlxDestroyUtil.destroy(data.baseSprite);
data.skinJson = null;
skinSpriteMap.remove(key);
data = null;
}
}

public static function getSkinSprites(skin:String):SkinSpriteData {
if (!skinSpriteMap.exists(skin)) setupSkinSprites(skin);
Expand Down Expand Up @@ -242,7 +251,6 @@ class NoteAtlas {

// And replace old bitmap
coloredParent.bitmap.dispose();
coloredParent.bitmap.disposeImage();
coloredParent.bitmap = strumBitmap;

// TODO: add default frames and optimize the ammount of stuff needed to add on the bitmap
Expand Down
45 changes: 38 additions & 7 deletions source/funkin/util/backend/AssetManager.hx
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,12 @@ class AssetManager
}

public static inline function clearStaticCache(runGc:Bool = true, clearGraphics:Bool = true, clearSounds:Bool = true):Void {
staticAssets = __clearCacheFromKeys(staticAssets, clearGraphics, clearSounds);
__clearCacheFromKeys(staticAssets, clearGraphics, clearSounds);
if (runGc) CoolUtil.gc(true);
}

public static inline function clearTempCache(runGc:Bool = true, clearGraphics:Bool = true, clearSounds:Bool = true):Void {
tempAssets = __clearCacheFromKeys(tempAssets, clearGraphics, clearSounds);
__clearCacheFromKeys(tempAssets, clearGraphics, clearSounds);
if (runGc) CoolUtil.gc(true);
}

Expand Down Expand Up @@ -305,8 +305,8 @@ class AssetManager
asset.dispose();
assetsMap.remove(key);

if (tempAssets.contains(key)) tempAssets.remove(key);
else if (staticAssets.contains(key)) staticAssets.remove(key);
if (tempAssets.contains(key)) tempAssets.splice(tempAssets.indexOf(key), 1);
else if (staticAssets.contains(key)) staticAssets.splice(staticAssets.indexOf(key), 1);

return true;
}
Expand All @@ -317,21 +317,52 @@ class AssetManager
return asset != null ? asset.asset : null;
}

/*inline static function __disposeKeyFromKeys(key:String, keys:Array<String>, clearGraphics:Bool, clearSounds:Bool):Void {
var asset = assetsMap.get(key);
if (asset != null) {
var dispose = (asset.isGraphicAsset && clearGraphics) || (asset.isSoundAsset && clearSounds);
if (dispose) {
keys.remove(key);
asset.dispose();
assetsMap.remove(key);
}
}
}*/

@:noCompletion
inline static function __clearCacheFromKeys(keys:Array<String>, clearGraphics:Bool, clearSounds:Bool) {
inline static function __clearCacheFromKeys(keys:Array<String>, clearGraphics:Bool, clearSounds:Bool):Array<String> {
var removeKeys:Array<String> = [];

for (i in 0...keys.length) {
var key = keys[i];
var asset = assetsMap.get(key);

if (asset != null) {
var dispose = (asset.isGraphicAsset && clearGraphics) || (asset.isSoundAsset && clearSounds);
if (dispose) {
removeKeys.push(key);
asset.dispose();
assetsMap.remove(key);
}
}
}

return FlxArrayUtil.clearArray(keys);
for (key in removeKeys)
keys.remove(key);

return keys;
}

/*
@:noCompletion
inline static function __clearCacheFromMod(keys:Array<String>, mod:String, clearGraphics:Bool, clearSounds:Bool):Array<String> {
for (i in 0...keys.length) {
var key = keys[i];
var keyMod = key.split("/")[1];
if (keyMod != mod) continue;
__disposeKeyFromKeys(key, keys, clearGraphics, clearSounds);
}
return keys;
}*/
}
4 changes: 2 additions & 2 deletions source/funkin/util/backend/AtlasFrames.hx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ import openfl.geom.Rectangle;

class AtlasFrames extends FlxAtlasFrames
{
public static function fromSparrow(source:FlxGraphicAsset, xml:FlxXmlAsset):FlxAtlasFrames
public static function fromSparrow(?graphic:LodGraphic, xml:FlxXmlAsset):FlxAtlasFrames
{
var graphic:FlxGraphic = FlxG.bitmap.add(source);
if (graphic == null)
return null;

// No need to parse data again
var frames:FlxAtlasFrames = FlxAtlasFrames.findFrame(graphic);
if (frames != null)
Expand Down
9 changes: 7 additions & 2 deletions source/funkin/util/modding/ModdingUtil.hx
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,13 @@ class ModdingUtil {
public static var modsMap:Map<String, ModFolder> = [];

static function set_curModFolder(?value:String) {
curModData = (value ?? "").length > 0 ? modsMap.get(value) : null;
return curModFolder = value;
if (value == null)
value = "";

curModFolder = value;
curModData = value.length > 0 ? modsMap.get(value) : null;

return value;
}

public static var activeMods:Map<String, Bool> = [];
Expand Down

0 comments on commit ec29336

Please sign in to comment.