From ac243fec526b0612e488f89ddabc50f40e556733 Mon Sep 17 00:00:00 2001 From: Yoshman29 Date: Sat, 15 Oct 2022 15:26:29 +0200 Subject: [PATCH] cool stuff --- buildnumber.txt | 2 +- source/funkin/menus/MainMenuState.hx | 1 - source/funkin/menus/TitleState.hx | 3 +- source/funkin/system/AudioSwitchFix.hx | 69 ++++++++++++++++++++++ source/funkin/system/CoolUtil.hx | 11 ++++ source/funkin/system/FramerateField.hx | 79 ++++++++++++++++++++++++++ source/funkin/system/Main.hx | 49 ++-------------- source/native/WinAPI.hx | 2 +- 8 files changed, 167 insertions(+), 49 deletions(-) create mode 100644 source/funkin/system/AudioSwitchFix.hx create mode 100644 source/funkin/system/FramerateField.hx diff --git a/buildnumber.txt b/buildnumber.txt index 1e27e32d..755ab3c2 100644 --- a/buildnumber.txt +++ b/buildnumber.txt @@ -1 +1 @@ -478 \ No newline at end of file +495 \ No newline at end of file diff --git a/source/funkin/menus/MainMenuState.hx b/source/funkin/menus/MainMenuState.hx index 6b2f4f68..77cd676b 100644 --- a/source/funkin/menus/MainMenuState.hx +++ b/source/funkin/menus/MainMenuState.hx @@ -98,7 +98,6 @@ class MainMenuState extends MusicBeatState FlxG.camera.follow(camFollow, null, 0.06); - trace(funkin.macros.BuildCounterMacro.getBuildNumber()); var versionShit:FlxText = new FlxText(5, FlxG.height - 2, 0, 'Codename Engine v${Application.current.meta.get('version')}\nPre-Alpha: Build ${funkin.macros.BuildCounterMacro.getBuildNumber()}\n', 12); versionShit.scrollFactor.set(); versionShit.setFormat("VCR OSD Mono", 16, FlxColor.WHITE, LEFT, FlxTextBorderStyle.OUTLINE, FlxColor.BLACK); diff --git a/source/funkin/menus/TitleState.hx b/source/funkin/menus/TitleState.hx index 62190cbd..0aceaa8e 100644 --- a/source/funkin/menus/TitleState.hx +++ b/source/funkin/menus/TitleState.hx @@ -48,6 +48,8 @@ class TitleState extends MusicBeatState { curWacky = FlxG.random.getObject(getIntroTextShit()); + FlxTransitionableState.skipNextTransIn = true; + super.create(); startIntro(); @@ -73,7 +75,6 @@ class TitleState extends MusicBeatState FlxG.sound.playMusic(Paths.music('freakyMenu'), 0); FlxG.sound.music.fadeIn(4, 0, 0.7); } - FlxTransitionableState.skipNextTransIn = true; Conductor.changeBPM(102); persistentUpdate = true; diff --git a/source/funkin/system/AudioSwitchFix.hx b/source/funkin/system/AudioSwitchFix.hx new file mode 100644 index 00000000..f08e1564 --- /dev/null +++ b/source/funkin/system/AudioSwitchFix.hx @@ -0,0 +1,69 @@ +package funkin.system; + +import lime.media.AudioManager; +import flixel.system.FlxSound; +import flixel.FlxState; +import funkin.windows.WindowsAPI; +import flixel.FlxG; + +/** + * if youre stealing this keep this comment at least please lol + * + * hi gray itsa me yoshicrafter29 i fixed it hehe + */ +@:dox(hide) +class AudioSwitchFix { + @:noCompletion + private static function onStateSwitch(state:FlxState):Void { + #if windows + if (Main.audioDisconnected) { + + var playingList:Array = []; + for(e in FlxG.sound.list) { + if (e.playing) { + playingList.push({ + sound: e, + time: e.time + }); + e.stop(); + } + } + if (FlxG.sound.music != null) + FlxG.sound.music.stop(); + + AudioManager.shutdown(); + AudioManager.init(); + // #if !lime_doc_gen + // if (AudioManager.context.type == OPENAL) + // { + // var alc = AudioManager.context.openal; + + // var device = alc.openDevice(); + // var ctx = alc.createContext(device); + // alc.makeContextCurrent(ctx); + // alc.processContext(ctx); + // } + // #end + Main.changeID++; + + for(e in playingList) { + e.sound.play(e.time); + } + + Main.audioDisconnected = false; + } + #end + } + + public static function init() { + #if windows + WindowsAPI.registerAudio(); + FlxG.signals.preStateCreate.add(onStateSwitch); + #end + } +} + +typedef PlayingSound = { + var sound:FlxSound; + var time:Float; +} \ No newline at end of file diff --git a/source/funkin/system/CoolUtil.hx b/source/funkin/system/CoolUtil.hx index d9911951..73e7548c 100644 --- a/source/funkin/system/CoolUtil.hx +++ b/source/funkin/system/CoolUtil.hx @@ -23,6 +23,17 @@ class CoolUtil return v == null ? defaultValue : v; } + public static function getSizeString(size:UInt):String { + var labels = ["B", "KB", "MB", "GB", "TB"]; + var rSize:Float = size; + var label:Int = 0; + while(rSize > 1024 && label < labels.length-1) { + label++; + rSize /= 1024; + } + return '${FlxMath.roundDecimal(rSize, 2)}${labels[label]}'; + } + public static function fpsLerp(v1:Float, v2:Float, ratio:Float):Float { return FlxMath.lerp(v1, v2, getFPSRatio(ratio)); } diff --git a/source/funkin/system/FramerateField.hx b/source/funkin/system/FramerateField.hx new file mode 100644 index 00000000..42e9bdc2 --- /dev/null +++ b/source/funkin/system/FramerateField.hx @@ -0,0 +1,79 @@ +package funkin.system; + +import lime.ui.KeyCode; +import openfl.ui.Keyboard; +import openfl.events.KeyboardEvent; +import openfl.text.TextField; +import openfl.text.TextFormat; +import openfl.system.System; +import openfl.display.Application; +import flixel.math.FlxMath; +import flixel.FlxG; + +class FramerateField extends TextField { + public var showFPS:Bool = true; + public var showMemory:Bool = true; + public var showMemoryPeak:Bool = true; + + /** + * Press F3 to enable + */ + public var debugMode:Bool = false; + + public var peak:UInt = 0; + public var lastFPS:Float = 0; + + public function new(x:Float = 10, y:Float = 10, color:Int = 0x000000) + { + super(); + + this.x = x; + this.y = y; + + selectable = false; + mouseEnabled = false; + defaultTextFormat = new TextFormat("_sans", 12, color); + autoSize = LEFT; + + #if flash + addEventListener(Event.ENTER_FRAME, function(e) + { + var time = Lib.getTimer(); + __enterFrame(time - currentTime); + }); + #end + + FlxG.stage.addEventListener(KeyboardEvent.KEY_UP, function(e:KeyboardEvent) { + switch(e.keyCode) { + case 114: // F3 + debugMode = !debugMode; + } + }); + } + + public override function __enterFrame(t:Float) { + var text = []; + if (showFPS || debugMode) { + lastFPS = CoolUtil.fpsLerp(lastFPS, FlxG.elapsed == 0 ? 0 : (1 / FlxG.elapsed), 0.25); + text.push('FPS: ${Std.int(lastFPS)}'); + } + if (showMemory || debugMode) { + text.push('RAM: ${CoolUtil.getSizeString(System.totalMemory)}'); + } + if (showMemoryPeak || debugMode) { + if (peak < System.totalMemory) + peak = System.totalMemory; + text.push('RAM PEAK: (${CoolUtil.getSizeString(peak)})'); + } + #if !release + text.push('CODENAME ENGINE ALPHA - BUILD ${funkin.macros.BuildCounterMacro.getBuildNumber()}'); + #end + if (debugMode) { + text.push('System: ${lime.system.System.platformLabel} ${lime.system.System.platformVersion}'); + text.push('Objs in state: ${FlxG.state.members.length}'); + text.push('Nb cameras: ${FlxG.cameras.list.length}'); + text.push('Current state: ${Type.getClassName(Type.getClass(FlxG.state))}'); + } + this.text = text.join("\n"); + } +} \ No newline at end of file diff --git a/source/funkin/system/Main.hx b/source/funkin/system/Main.hx index 5d44b559..0f4b8152 100644 --- a/source/funkin/system/Main.hx +++ b/source/funkin/system/Main.hx @@ -90,7 +90,7 @@ class Main extends Sprite loadGameSettings(); #if !mobile - addChild(new FPS(10, 3, 0xFFFFFF)); + addChild(new FramerateField(10, 3, 0xFFFFFF)); #end } @@ -98,47 +98,7 @@ class Main extends Sprite public static var audioDisconnected:Bool = false; public static var changeID:Int = 0; - @:dox(hide) - @:noCompletion - private function onStateSwitch(state:FlxState):Void { - #if windows - if (audioDisconnected) { - // for(e in lime.media.openal.AL.buffers) { - // lime.media.openal.AL.deleteBuffer(e); - // } - // lime.media.openal.AL.buffers = []; - // var buffers = [for(e in lime._internal.backend.native.NativeAudioSource.initBuffers) e]; - // trace(buffers.length); - - for(e in FlxG.sound.list) { - e.stop(); - } - if (FlxG.sound.music != null) - FlxG.sound.music.stop(); - - #if !lime_doc_gen - if (lime.media.AudioManager.context.type == OPENAL) - { - var alc = lime.media.AudioManager.context.openal; - - var device = alc.openDevice(); - var ctx = alc.createContext(device); - alc.makeContextCurrent(ctx); - alc.processContext(ctx); - } - #end - changeID++; - - // for(e in buffers) { - // @:privateAccess - // e.init(); - // } - - // lime._internal.backend.native.NativeAudioSource.initBuffers = []; - audioDisconnected = false; - } - #end - } + public function loadGameSettings() { funkin.options.PlayerSettings.init(); @@ -165,11 +125,10 @@ class Main extends Sprite FlxG.fixedTimestep = false; FlxG.autoPause = false; - FlxG.signals.preStateCreate.add(onStateSwitch); - Options.load(); - WindowsAPI.registerAudio(); + AudioSwitchFix.init(); + // WindowsAPI.setAudioChangeCallback(function() { // trace("test"); // }); diff --git a/source/native/WinAPI.hx b/source/native/WinAPI.hx index 70730fc5..a4d7e937 100644 --- a/source/native/WinAPI.hx +++ b/source/native/WinAPI.hx @@ -114,7 +114,7 @@ class WinAPI { if (!curAudioFix) curAudioFix = new AudioFixClient(); ') public static function registerAudio() { - funkin.system.Main.audioDisconnected = true; + funkin.system.Main.audioDisconnected = false; } } #end \ No newline at end of file