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

TitleState Cleanup #15256

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
95 changes: 22 additions & 73 deletions source/Main.hx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package;

import flixel.input.keyboard.FlxKey;
#if android
import android.content.Context;
#end
Expand Down Expand Up @@ -40,25 +41,11 @@ import backend.Highscore;

class Main extends Sprite
{
var game = {
width: 1280, // WINDOW width
height: 720, // WINDOW height
initialState: TitleState, // initial game state
zoom: -1.0, // game state bounds
framerate: 60, // default framerate
skipSplash: true, // if the default flixel splash screen should be skipped
startFullscreen: false // if the game should start at fullscreen mode
};

public static var muteKeys:Array<FlxKey> = [FlxKey.ZERO];
public static var volumeDownKeys:Array<FlxKey> = [FlxKey.NUMPADMINUS, FlxKey.MINUS];
public static var volumeUpKeys:Array<FlxKey> = [FlxKey.NUMPADPLUS, FlxKey.PLUS];
public static var fpsVar:FPSCounter;

// You can pretty much ignore everything from here on - your code should go in your states.

public static function main():Void
{
Lib.current.addChild(new Main());
}

public function new()
{
super();
Expand All @@ -70,43 +57,6 @@ class Main extends Sprite
Sys.setCwd(lime.system.System.applicationStorageDirectory);
#end

if (stage != null)
{
init();
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
#if VIDEOS_ALLOWED
hxvlc.util.Handle.init(#if (hxvlc >= "1.8.0") ['--no-lua'] #end);
#end
}

private function init(?E:Event):Void
{
if (hasEventListener(Event.ADDED_TO_STAGE))
{
removeEventListener(Event.ADDED_TO_STAGE, init);
}

setupGame();
}

private function setupGame():Void
{
var stageWidth:Int = Lib.current.stage.stageWidth;
var stageHeight:Int = Lib.current.stage.stageHeight;

if (game.zoom == -1.0)
{
var ratioX:Float = stageWidth / game.width;
var ratioY:Float = stageHeight / game.height;
game.zoom = Math.min(ratioX, ratioY);
game.width = Math.ceil(stageWidth / game.zoom);
game.height = Math.ceil(stageHeight / game.zoom);
}

#if LUA_ALLOWED
Mods.pushGlobalMods();
#end
Expand All @@ -120,22 +70,15 @@ class Main extends Sprite
Controls.instance = new Controls();
ClientPrefs.loadDefaultKeys();
#if ACHIEVEMENTS_ALLOWED Achievements.load(); #end
addChild(new FlxGame(game.width, game.height, game.initialState, #if (flixel < "5.0.0") game.zoom, #end game.framerate, game.framerate, game.skipSplash, game.startFullscreen));
addChild(new FlxGame(1280, 720, TitleState, #if (flixel < "5.0.0") -1.0, #end 60, 60, true, false));

#if !mobile
fpsVar = new FPSCounter(10, 3, 0xFFFFFF);
addChild(fpsVar);
Lib.current.stage.align = "tl";
Lib.current.stage.scaleMode = StageScaleMode.NO_SCALE;
if(fpsVar != null) {
addChild(fpsVar = new FPSCounter(10, 3, 0xFFFFFF));
if (fpsVar != null)
fpsVar.visible = ClientPrefs.data.showFPS;
}
#end

#if linux
var icon = Image.fromFile("icon.png");
Lib.current.stage.window.setIcon(icon);
#end
#if linux Lib.current.stage.window.setIcon(Image.fromFile("icon.png")); #end

#if html5
FlxG.autoPause = false;
Expand All @@ -145,7 +88,7 @@ class Main extends Sprite
FlxG.fixedTimestep = false;
FlxG.game.focusLostFramerate = 60;
FlxG.keys.preventDefaultKeys = [TAB];

#if CRASH_HANDLER
Lib.current.loaderInfo.uncaughtErrorEvents.addEventListener(UncaughtErrorEvent.UNCAUGHT_ERROR, onCrash);
#end
Expand All @@ -155,17 +98,23 @@ class Main extends Sprite
#end

// shader coords fix
FlxG.signals.gameResized.add(function (w, h) {
if (FlxG.cameras != null) {
for (cam in FlxG.cameras.list) {
if (cam != null && cam.filters != null)
resetSpriteCache(cam.flashSprite);
}
FlxG.signals.gameResized.add(function(w, h)
{
if (FlxG.cameras != null)
{
for (cam in FlxG.cameras.list)
{
if (cam != null && cam.filters != null)
resetSpriteCache(cam.flashSprite);
}
}

if (FlxG.game != null)
resetSpriteCache(FlxG.game);
resetSpriteCache(FlxG.game);
});
#if VIDEOS_ALLOWED
hxvlc.util.Handle.init(#if (hxvlc >= "1.8.0") ['--no-lua'] #end);
#end
}

static function resetSpriteCache(sprite:Sprite):Void {
Expand Down
12 changes: 6 additions & 6 deletions source/backend/ClientPrefs.hx
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,16 @@ class ClientPrefs {

public static function reloadVolumeKeys()
{
TitleState.muteKeys = keyBinds.get('volume_mute').copy();
TitleState.volumeDownKeys = keyBinds.get('volume_down').copy();
TitleState.volumeUpKeys = keyBinds.get('volume_up').copy();
Main.muteKeys = keyBinds.get('volume_mute').copy();
Main.volumeDownKeys = keyBinds.get('volume_down').copy();
Main.volumeUpKeys = keyBinds.get('volume_up').copy();
toggleVolumeKeys(true);
}
public static function toggleVolumeKeys(?turnOn:Bool = true)
{
final emptyArray = [];
FlxG.sound.muteKeys = turnOn ? TitleState.muteKeys : emptyArray;
FlxG.sound.volumeDownKeys = turnOn ? TitleState.volumeDownKeys : emptyArray;
FlxG.sound.volumeUpKeys = turnOn ? TitleState.volumeUpKeys : emptyArray;
FlxG.sound.muteKeys = turnOn ? Main.muteKeys : emptyArray;
FlxG.sound.volumeDownKeys = turnOn ? Main.volumeDownKeys : emptyArray;
FlxG.sound.volumeUpKeys = turnOn ? Main.volumeUpKeys : emptyArray;
}
}
Loading
Loading