Skip to content

Commit

Permalink
squashed commits
Browse files Browse the repository at this point in the history
extend treshold + createThread in threadUtils

add threadutil

use threads to fix a small bug
  • Loading branch information
KoloInDaCrib committed Jan 23, 2025
1 parent 0d8e4a5 commit ef26a62
Show file tree
Hide file tree
Showing 2 changed files with 202 additions and 0 deletions.
62 changes: 62 additions & 0 deletions source/funkin/play/PlayState.hx
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ import haxe.Int64;
#if FEATURE_DISCORD_RPC
import funkin.api.discord.DiscordClient;
#end
#if sys
import funkin.util.ThreadUtil;
#end

/**
* Parameters used to initialize the PlayState.
Expand Down Expand Up @@ -707,6 +710,10 @@ class PlayState extends MusicBeatSubState

initPreciseInputs();

#if sys
initThreads();
#end

FlxG.worldBounds.set(0, 0, FlxG.width, FlxG.height);

// The song is loaded and in the process of starting.
Expand Down Expand Up @@ -1764,6 +1771,55 @@ class PlayState extends MusicBeatSubState
}
}

#if sys
/**
* Whether the main thread of this application is frozen by a prolonged lag or the window's title bar being dragged.
*/
var isMainThreadFrozen:Bool = false;

/**
* Setups the Thread to check if the game has been dragged by the window's title bar.
*/
function initThreads()
{
ThreadUtil.createLoopingThread("playStateWindow", function() {
var lostFocus:Bool = false;
@:privateAccess
lostFocus = FlxG.game._lostFocus;

if (!initialized || health <= Constants.HEALTH_MIN || isGamePaused || !generatedMusic || criticalFailure || lostFocus || justUnpaused) return;

if (isMainThreadFrozen)
{
trace("The game is frozen! Pausing.");

persistentUpdate = false;
persistentDraw = true;

var pauseSubState:FlxSubState = new PauseSubState({mode: isChartingMode ? Charting : Standard});

FlxTransitionableState.skipNextTransIn = true;
FlxTransitionableState.skipNextTransOut = true;
pauseSubState.camera = camCutscene;
openSubState(pauseSubState);
}
else
{
// trace("Not frozen! We resetting and checking again!");
}

isMainThreadFrozen = true;
}, 1, 0.5);

FlxG.signals.preUpdate.add(checkMainThreadActivity);
}

function checkMainThreadActivity()
{
isMainThreadFrozen = false;
}
#end

/**
* Constructs the strumlines for each player.
*/
Expand Down Expand Up @@ -3108,6 +3164,12 @@ class PlayState extends MusicBeatSubState
*/
function performCleanup():Void
{
#if sys
// If we have a thread running, stop it.
FlxG.signals.preUpdate.remove(checkMainThreadActivity);
ThreadUtil.stopThread("playStateWindow");
#end

// If the camera is being tweened, stop it.
cancelAllCameraTweens();

Expand Down
140 changes: 140 additions & 0 deletions source/funkin/util/ThreadUtil.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
package funkin.util;

#if sys
import sys.thread.Thread;
#end

/**
* Utilities for creating and managing Threads. Only available on desktop devices.
*/
class ThreadUtil
{
#if sys
/**
* Data regarding every looping Thread.
*/
public static var loopingThreads:Map<String, ThreadParams> = [];
#end

/**
* Creates a Thread that executes `job` once outside of the Main Thread.
* @param job The function to execute.
*/
public static function createThread(job:Void->Void)
{
#if sys
return Thread.create(job);
#else
throw "Threads aren't supported on this device.";
return null;
#end
}

/**
* Creates a Thread that starts after a set time.
* @param job The function to execute after the delay.
* @param delay The delay itself.
*/
public static function createDelayedThread(job:Void->Void, delay:Float = 1.0)
{
#if sys
return Thread.create(function() {
Sys.sleep(delay);

job();
});
#else
throw "Threads aren't supported on this device.";
return null;
#end
}

/**
* Creates a Thread that loops infinitely until destroyed.
* @param id The ID of the Thread, used for pausing, resuming and stopping.
* @param job The function to execute.
* @param startDelay The delay before the loop begins.
* @param loopDelay The delay between every loop.
*/
public static function createLoopingThread(id:String, job:Void->Void, startDelay:Float = 1.0, loopDelay:Float = 0.0)
{
#if sys
var threadInfo:ThreadParams = {thread: null, isPaused: false, isDestroyed: false}
loopingThreads.set(id, threadInfo);

var thread:Thread = Thread.create(function() {
Sys.sleep(startDelay);

while (!threadInfo.isDestroyed)
{
if (threadInfo.isPaused) continue;

job();

Sys.sleep(loopDelay);
}

// Clean-up.
if (threadInfo.isDestroyed)
{
threadInfo.thread = null;
loopingThreads.remove(id);
}
});

threadInfo.thread = thread;
return thread;
#else
throw "Threads aren't supported on this device.";
return null;
#end
}

/**
* Pauses a looping Thread.
* @param id The ID of the Thread to pause.
*/
public static function pauseThread(id:String)
{
#if sys
if (loopingThreads.exists(id)) loopingThreads[id].isPaused = true;
#else
throw "Threads aren't supported on this device.";
#end
}

/**
* Resumes a looping Thread.
* @param id The ID of the Thread to resume.
*/
public static function resumeThread(id:String)
{
#if sys
if (loopingThreads.exists(id)) loopingThreads[id].isPaused = false;
#else
throw "Threads aren't supported on this device.";
#end
}

/**
* Stops a looping Thread and destroys it afterwards.
* @param id The ID of the Thread to stop and destroy.
*/
public static function stopThread(id:String)
{
#if sys
if (loopingThreads.exists(id)) loopingThreads[id].isDestroyed = true;
#else
throw "Threads aren't supported on this device.";
#end
}
}

#if sys
typedef ThreadParams =
{
var thread:Thread;
var isPaused:Bool;
var isDestroyed:Bool;
}
#end

0 comments on commit ef26a62

Please sign in to comment.