Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fps setting #36

Merged
merged 20 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 2 additions & 7 deletions source/Main.hx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package;

import flixel.FlxGame;
import flixel.FlxState;
import funkin.Preferences;
import funkin.util.logging.CrashHandler;
import funkin.ui.debug.MemoryCounter;
import funkin.save.Save;
Expand All @@ -22,12 +23,6 @@ class Main extends Sprite
var gameHeight:Int = 720; // Height of the game in pixels (might be less / more in actual pixels depending on your zoom).
var initialState:Class<FlxState> = funkin.InitState; // The FlxState the game starts with.
var zoom:Float = -1; // If -1, zoom is automatically calculated to fit the window dimensions.
#if web
var framerate:Int = 60; // How many frames per second the game should run at.
#else
// TODO: This should probably be in the options menu?
var framerate:Int = 144; // How many frames per second the game should run at.
#end
var skipSplash:Bool = true; // Whether to skip the flixel splash screen that appears in release mode.
var startFullscreen:Bool = false; // Whether to start the game in fullscreen on desktop targets

Expand Down Expand Up @@ -103,7 +98,7 @@ class Main extends Sprite

// George recommends binding the save before FlxGame is created.
Save.load();
var game:FlxGame = new FlxGame(gameWidth, gameHeight, initialState, framerate, framerate, skipSplash, startFullscreen);
var game:FlxGame = new FlxGame(gameWidth, gameHeight, initialState, Preferences.framerate, Preferences.framerate, skipSplash, startFullscreen);

// FlxG.game._customSoundTray wants just the class, it calls new from
// create() in there, which gets called when it's added to stage
Expand Down
29 changes: 29 additions & 0 deletions source/funkin/Preferences.hx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,35 @@ import funkin.save.Save;
*/
class Preferences
{
/**
* FPS
* @default `60`
*/
public static var framerate(get, set):Int;

static function get_framerate():Int
{
#if web
return 60;
#else
return Save?.instance?.options?.framerate ?? 60;
#end
}

static function set_framerate(value:Int):Int
{
#if web
return 60;
#else
var save:Save = Save.instance;
save.options.framerate = value;
save.flush();
FlxG.updateFramerate = value;
FlxG.drawFramerate = value;
return value;
#end
}

/**
* Whether some particularly fowl language is displayed.
* @default `true`
Expand Down
7 changes: 7 additions & 0 deletions source/funkin/save/Save.hx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class Save
options:
{
// Reasonable defaults.
framerate: 60,
naughtyness: true,
downscroll: false,
flashingLights: true,
Expand Down Expand Up @@ -1055,6 +1056,12 @@ typedef SaveScoreTallyData =
*/
typedef SaveDataOptions =
{
/**
* FPS
* @default `60`
*/
var framerate:Int;

/**
* Whether some particularly fowl language is displayed.
* @default `true`
Expand Down
26 changes: 26 additions & 0 deletions source/funkin/ui/AtlasText.hx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,32 @@ class AtlasText extends FlxTypedSpriteGroup<AtlasChar>
}
}

public function getWidth():Int
{
var width = 0;
for (char in this.text.split(""))
{
switch (char)
{
case " ":
{
width += 40;
}
case "\n":
{}
case char:
{
var sprite = new AtlasChar(atlas, char);
sprite.revive();
sprite.char = char;
sprite.alpha = 1;
width += Std.int(sprite.width);
}
}
}
return width;
}

override function toString()
{
return "InputItem, " + FlxStringUtil.getDebugString([
Expand Down
95 changes: 41 additions & 54 deletions source/funkin/ui/options/PreferencesMenu.hx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import flixel.FlxSprite;
import flixel.group.FlxSpriteGroup.FlxTypedSpriteGroup;
import funkin.ui.AtlasText.AtlasFont;
import funkin.ui.options.OptionsState.Page;
import funkin.ui.options.items.CheckboxPreferenceItem;
import funkin.ui.options.items.NumberPreferenceItem;
import funkin.graphics.FunkinCamera;
import funkin.ui.TextMenuList.TextMenuItem;

Expand Down Expand Up @@ -49,6 +51,11 @@ class PreferencesMenu extends Page
*/
function createPrefItems():Void
{
#if !web
createPrefItemNumber('FPS', 'The framerate that the game is running on', function(value:Float) {
Preferences.framerate = Std.int(value);
}, Preferences.framerate, 60, 360, 1, 0);
#end
createPrefItemCheckbox('Naughtyness', 'Toggle displaying raunchy content', function(value:Bool):Void {
Preferences.naughtyness = value;
}, Preferences.naughtyness);
Expand All @@ -69,75 +76,55 @@ class PreferencesMenu extends Page
}, Preferences.autoPause);
}

function createPrefItemCheckbox(prefName:String, prefDesc:String, onChange:Bool->Void, defaultValue:Bool):Void
{
var checkbox:CheckboxPreferenceItem = new CheckboxPreferenceItem(0, 120 * (items.length - 1 + 1), defaultValue);

items.createItem(120, (120 * items.length) + 30, prefName, AtlasFont.BOLD, function() {
var value = !checkbox.currentValue;
onChange(value);
checkbox.currentValue = value;
});

preferenceItems.add(checkbox);
}

override function update(elapsed:Float)
override function update(elapsed:Float):Void
{
super.update(elapsed);

// Indent the selected item.
// TODO: Only do this on menu change?
items.forEach(function(daItem:TextMenuItem) {
if (items.selectedItem == daItem) daItem.x = 150;
var thyOffset:Int = 0;
// Initializing thy text width (if thou text present)
var thyTextWidth:Int = 0;
if (Std.isOfType(daItem, NumberPreferenceItem)) thyTextWidth = cast(daItem, NumberPreferenceItem).lefthandText.getWidth();

if (thyTextWidth != 0)
{
// Magic number because of the weird offset thats being added by default
thyOffset += thyTextWidth - 75;
}

if (items.selectedItem == daItem)
{
thyOffset += 150;
}
else
daItem.x = 120;
{
thyOffset += 120;
}

daItem.x = thyOffset;
});
}
}

class CheckboxPreferenceItem extends FlxSprite
{
public var currentValue(default, set):Bool;

public function new(x:Float, y:Float, defaultValue:Bool = false)
function createPrefItemCheckbox(prefName:String, prefDesc:String, onChange:Bool->Void, defaultValue:Bool):Void
{
super(x, y);

frames = Paths.getSparrowAtlas('checkboxThingie');
animation.addByPrefix('static', 'Check Box unselected', 24, false);
animation.addByPrefix('checked', 'Check Box selecting animation', 24, false);

setGraphicSize(Std.int(width * 0.7));
updateHitbox();

this.currentValue = defaultValue;
}
var checkbox:CheckboxPreferenceItem = new CheckboxPreferenceItem(0, 120 * (items.length - 1 + 1), defaultValue);

override function update(elapsed:Float)
{
super.update(elapsed);
items.createItem(0, (120 * items.length) + 30, prefName, AtlasFont.BOLD, function() {
var value = !checkbox.currentValue;
onChange(value);
checkbox.currentValue = value;
});

switch (animation.curAnim.name)
{
case 'static':
offset.set();
case 'checked':
offset.set(17, 70);
}
preferenceItems.add(checkbox);
}

function set_currentValue(value:Bool):Bool
function createPrefItemNumber(prefName:String, prefDesc:String, onChange:Float->Void, defaultValue:Float, min:Float, max:Float, step:Float,
precision:Int):Void
{
if (value)
{
animation.play('checked', true);
}
else
{
animation.play('static');
}

return currentValue = value;
var item = new NumberPreferenceItem(0, (120 * items.length) + 30, prefName, defaultValue, min, max, step, precision, onChange);
items.addItem(prefName, item);
preferenceItems.add(item.lefthandText);
}
}
49 changes: 49 additions & 0 deletions source/funkin/ui/options/items/CheckboxPreferenceItem.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package funkin.ui.options.items;

import flixel.FlxSprite.FlxSprite;

class CheckboxPreferenceItem extends FlxSprite
{
public var currentValue(default, set):Bool;

public function new(x:Float, y:Float, defaultValue:Bool = false)
{
super(x, y);

frames = Paths.getSparrowAtlas('checkboxThingie');
animation.addByPrefix('static', 'Check Box unselected', 24, false);
animation.addByPrefix('checked', 'Check Box selecting animation', 24, false);

setGraphicSize(Std.int(width * 0.7));
updateHitbox();

this.currentValue = defaultValue;
}

override function update(elapsed:Float)
{
super.update(elapsed);

switch (animation.curAnim.name)
{
case 'static':
offset.set();
case 'checked':
offset.set(17, 70);
}
}

function set_currentValue(value:Bool):Bool
{
if (value)
{
animation.play('checked', true);
}
else
{
animation.play('static');
}

return currentValue = value;
}
}
Loading
Loading