Skip to content
This repository has been archived by the owner on Dec 31, 2024. It is now read-only.

Commit

Permalink
ATTEMPTING OPTIMISATION IDK HOW TO OPTIMISE AND IDK HOW TO CHECK MEMORY
Browse files Browse the repository at this point in the history
  • Loading branch information
nebulazorua committed Jun 5, 2021
1 parent 54f5fd8 commit d1775d1
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 47 deletions.
12 changes: 11 additions & 1 deletion source/Cache.hx
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
package;
import flixel.graphics.frames.FlxFramesCollection;

import flixel.FlxG;
import LuaClass;
class Cache {
public static var offsetData = new Map<String,String>();
public static var animData = new Map<String,String>();
public static var charFrames = new Map<String,FlxFramesCollection>();
public static var xmlData = new Map<String,String>();

public static function Clear(){
offsetData.clear();
animData.clear();
charFrames.clear();
xmlData.clear();
LuaStorage.objectProperties.clear();
trace("CLEARED CACHE!");
}
}
14 changes: 12 additions & 2 deletions source/Character.hx
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ class Character extends FlxSprite
playAnim("idle");
default:
var xmlData:String = '';
if(Cache.xmlData[curCharacter]!=null){
/*if(Cache.xmlData[curCharacter]!=null){
xmlData=Cache.xmlData[curCharacter];
}else{
xmlData=File.getContent("assets/shared/images/characters/"+curCharacter+".xml");
Expand All @@ -439,9 +439,19 @@ class Character extends FlxSprite
}else{
bitmapData = BitmapData.fromFile("assets/shared/images/characters/"+curCharacter+".png");
FlxG.bitmap.add(bitmapData,true,curCharacter+"CharFrames");
}*/

if(Cache.charFrames[curCharacter]!=null){
frames=Cache.charFrames[curCharacter];
}else{
frames = FlxAtlasFrames.fromSparrow(BitmapData.fromFile("assets/shared/images/characters/"+curCharacter+".png"),File.getContent("assets/shared/images/characters/"+curCharacter+".xml"));
Cache.charFrames[curCharacter]=frames;
}
FlxG.bitmap.dumpCache();




frames = FlxAtlasFrames.fromSparrow(bitmapData,xmlData);

loadAnimations();
loadOffsets();
Expand Down
69 changes: 35 additions & 34 deletions source/LoadingState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ import haxe.io.Path;
class LoadingState extends MusicBeatState
{
inline static var MIN_TIME = 1.0;

var target:FlxState;
var stopMusic = false;
var callbacks:MultiCallback;

var logo:FlxSprite;
var gfDance:FlxSprite;
var danceLeft = false;

function new(target:FlxState, stopMusic:Bool)
{
super();
this.target = target;
this.stopMusic = stopMusic;
}

override function create()
{
logo = new FlxSprite(-150, -100);
Expand All @@ -52,7 +52,7 @@ class LoadingState extends MusicBeatState
gfDance.antialiasing = true;
add(gfDance);
add(logo);

initSongsManifest().onComplete
(
function (lib)
Expand All @@ -67,14 +67,14 @@ class LoadingState extends MusicBeatState
checkLibrary("week" + PlayState.storyWeek);
else
checkLibrary("tutorial");

var fadeTime = 0.5;
FlxG.camera.fade(FlxG.camera.bgColor, fadeTime, true);
new FlxTimer().start(fadeTime + MIN_TIME, function(_) introComplete());
}
);
}

function checkLoadSong(path:String)
{
if (!Assets.cache.hasSound(path))
Expand All @@ -89,7 +89,7 @@ class LoadingState extends MusicBeatState
Assets.loadSound(path).onComplete(function (_) { callback(); });
}
}

function checkLibrary(library:String)
{
trace(Assets.hasLibrary(library));
Expand All @@ -98,25 +98,25 @@ class LoadingState extends MusicBeatState
@:privateAccess
if (!LimeAssets.libraryPaths.exists(library))
throw "Missing library: " + library;

var callback = callbacks.add("library:" + library);
Assets.loadLibrary(library).onComplete(function (_) { callback(); });
}
}

override function beatHit()
{
super.beatHit();

logo.animation.play('bump');
danceLeft = !danceLeft;

if (danceLeft)
gfDance.animation.play('danceRight');
else
gfDance.animation.play('danceLeft');
}

override function update(elapsed:Float)
{
super.update(elapsed);
Expand All @@ -125,66 +125,67 @@ class LoadingState extends MusicBeatState
trace('fired: ' + callbacks.getFired() + " unfired:" + callbacks.getUnfired());
#end
}

function onLoad()
{
if (stopMusic && FlxG.sound.music != null)
FlxG.sound.music.stop();

FlxG.switchState(target);
}

static function getSongPath()
{
return Paths.inst(PlayState.SONG.song);
}

static function getVocalPath()
{
return Paths.voices(PlayState.SONG.song);
}

inline static public function loadAndSwitchState(target:FlxState, stopMusic = false)
{
Cache.Clear();
FlxG.switchState(getNextState(target, stopMusic));
}

static function getNextState(target:FlxState, stopMusic = false):FlxState
{
Paths.setCurrentLevel("week" + PlayState.storyWeek);
#if NO_PRELOAD_ALL
var loaded = isSoundLoaded(getSongPath())
&& (!PlayState.SONG.needsVoices || isSoundLoaded(getVocalPath()))
&& isLibraryLoaded("shared");

if (!loaded)
return new LoadingState(target, stopMusic);
#end
if (stopMusic && FlxG.sound.music != null)
FlxG.sound.music.stop();

return target;
}

#if NO_PRELOAD_ALL
static function isSoundLoaded(path:String):Bool
{
return Assets.cache.hasSound(path);
}

static function isLibraryLoaded(library:String):Bool
{
return Assets.getLibrary(library) != null;
}
#end

override function destroy()
{
super.destroy();

callbacks = null;
}

static function initSongsManifest()
{
var id = "songs";
Expand Down Expand Up @@ -258,16 +259,16 @@ class MultiCallback
public var logId:String = null;
public var length(default, null) = 0;
public var numRemaining(default, null) = 0;

var unfired = new Map<String, Void->Void>();
var fired = new Array<String>();

public function new (callback:Void->Void, logId:String = null)
{
this.callback = callback;
this.logId = logId;
}

public function add(id = "untitled")
{
id = '$length:$id';
Expand All @@ -281,10 +282,10 @@ class MultiCallback
unfired.remove(id);
fired.push(id);
numRemaining--;

if (logId != null)
log('fired $id, $numRemaining remaining');

if (numRemaining == 0)
{
if (logId != null)
Expand All @@ -298,13 +299,13 @@ class MultiCallback
unfired[id] = func;
return func;
}

inline function log(msg):Void
{
if (logId != null)
trace('$logId: $msg');
}

public function getFired() return fired.copy();
public function getUnfired() return [for (id in unfired.keys()) id];
}
}
15 changes: 9 additions & 6 deletions source/LuaClass.hx
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,20 @@ typedef LuaProperty = {
var setter:State->Int;
}

class LuaStorage {
public static var objectProperties:Map<String,Map<String,LuaProperty>> = [];
}

class LuaClass {
public var properties:Map<String,LuaProperty> = [];
public var methods:Map<String,cpp.Callable<StatePointer->Int> > = [];
public var className:String = "BaseClass";
private static var objectProperties:Map<String,Map<String,LuaProperty>> = [];
private static var state:State;
public var addToGlobal:Bool=true;
public function Register(l:State){
Lua.newtable(l);
state=l;
objectProperties[className]=this.properties;
LuaStorage.objectProperties[className]=this.properties;

var classIdx = Lua.gettop(l);
Lua.pushvalue(l,classIdx);
Expand Down Expand Up @@ -83,8 +86,8 @@ class LuaClass {
Lua.pushstring(l,"_CLASSNAME");
Lua.rawget(l,mtIdx);
var clName = Lua.tostring(l,-1);
if(objectProperties[clName]!=null && objectProperties[clName][index]!=null){
return objectProperties[clName][index].getter(l,data);
if(LuaStorage.objectProperties[clName]!=null && LuaStorage.objectProperties[clName][index]!=null){
return LuaStorage.objectProperties[clName][index].getter(l,data);
}
};
}else{
Expand All @@ -105,9 +108,9 @@ class LuaClass {
Lua.pushstring(l,"_CLASSNAME");
Lua.rawget(l,mtIdx);
var clName = Lua.tostring(l,-1);
if(objectProperties[clName]!=null && objectProperties[clName][index]!=null){
if(LuaStorage.objectProperties[clName]!=null && LuaStorage.objectProperties[clName][index]!=null){
Lua.pop(l,2);
return objectProperties[clName][index].setter(l);
return LuaStorage.objectProperties[clName][index].setter(l);
}
};
}else{
Expand Down
4 changes: 2 additions & 2 deletions source/Note.hx
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ class Note extends FlxSprite

//off -= width / 2;
//x -= width / 2;
if (PlayState.curStage.startsWith('school'))
off += 30;
// if (PlayState.curStage.startsWith('school'))
//off += 30;

offset.x = off;

Expand Down
1 change: 1 addition & 0 deletions source/PauseSubState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ class PauseSubState extends MusicBeatSubstate
FlxG.resetState();
case "Exit to menu":
FlxG.switchState(new MainMenuState());
Cache.Clear();
}
}

Expand Down
Loading

0 comments on commit d1775d1

Please sign in to comment.