Skip to content

Commit

Permalink
added a threshold option to the catspeak manager
Browse files Browse the repository at this point in the history
  • Loading branch information
katsaii committed Jun 27, 2021
1 parent 7f8ab44 commit 311caeb
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 14 deletions.
4 changes: 2 additions & 2 deletions objects/obj_catspeak_examples/CleanUp_0.gml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/// @desc Clean up Catspeak sessions.
catspeak_session_destroy(jsonSession);
catspeak_session_destroy(configSession);
catspeak_session_destroy(configSession);
catspeak_session_destroy(processingSession);
37 changes: 27 additions & 10 deletions objects/obj_catspeak_examples/Create_0.gml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/// @desc Initialise examples.
frameStartTime = 0;
show_debug_overlay(true);
catspeak_set_frame_allocation(0.5); // compute for 50% of the frame if possible
catspeak_set_frame_threshold(0.75); // do not surpass 75% of the current frame
// JSON session
jsonObj = catspeak_ext_json_parse(@'
{
Expand Down Expand Up @@ -39,19 +41,34 @@ catspeak_session_add_function(configSession, "world", function(_value) {
worldData = _value;
});
catspeak_session_set_source(configSession, @'
player {
.name : "Angie"
.x : -12
.y : 8
}
player {
.name : "Angie"
.x : -12
.y : 8
}
world {
.height : 1000
.width : 500
.background_colour : 0x00FF00
}
world {
.height : 1000
.width : 500
.background_colour : 0x00FF00
}
');
catspeak_session_create_process(configSession, function(_) {
show_message("player data:\n" + string(playerData));
show_message("world data:\n" + string(worldData));
});
// heavy processing example
processingSession = catspeak_session_create();
catspeak_ext_session_add_gml_operators(configSession);
catspeak_session_set_source(configSession, @'
set countdown 15000
while countdown {
if (countdown % 5000 == 0) {
print countdown
}
set countdown : countdown - 1
}
');
catspeak_session_create_process(configSession, function(_) {
show_message("countdown complete!");
});
19 changes: 17 additions & 2 deletions scripts/scr_catspeak/scr_catspeak.gml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ function __catspeak_manager() {
runtimeProcessID : 0,
errorScript : undefined,
maxIterations : -1,
frameAllocation : 0.5 // compute for 50% of frame time
frameAllocation : 0.5, // compute for 50% of frame time
frameThreshold : 1 // do not surpass 1 frame when processing
};
return catspeak;
}
Expand Down Expand Up @@ -69,7 +70,7 @@ function __catspeak_next_runtime_process() {
function catspeak_update(_frame_start) {
var catspeak = __catspeak_manager();
// update active processes
var frame_time = game_get_speed(gamespeed_microseconds);
var frame_time = catspeak.frameThreshold * game_get_speed(gamespeed_microseconds);
var time_limit = min(_frame_start + frame_time,
get_timer() + catspeak.frameAllocation * frame_time);
var runtime_processes = catspeak.runtimeProcesses;
Expand Down Expand Up @@ -128,6 +129,20 @@ function catspeak_set_max_iterations(_iteration_count) {
catspeak.maxIterations = is_numeric(_iteration_count) && _iteration_count >= 0 ? _iteration_count : -1;
}

/// @desc Sets the maximum percentage of a game frame to spend processing.
/// @param {real} amount The amount in the range 0-1.
function catspeak_set_frame_allocation(_amount) {
var catspeak = __catspeak_manager();
catspeak.frameAllocation = is_numeric(_amount) ? clamp(_amount, 0, 1) : 0.5;
}

/// @desc Sets the threshold for processing in the current frame. Processing will not continue beyond this point.
/// @param {real} amount The amount in the range 0-1.
function catspeak_set_frame_threshold(_amount) {
var catspeak = __catspeak_manager();
catspeak.frameThreshold = is_numeric(_amount) ? clamp(_amount, 0, 1) : 1.0;
}

/// @desc Creates a new Catspeak session and returns its ID.
function catspeak_session_create() {
var catspeak = __catspeak_manager();
Expand Down

0 comments on commit 311caeb

Please sign in to comment.