-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathMain.hx
More file actions
199 lines (169 loc) · 4.75 KB
/
Main.hx
File metadata and controls
199 lines (169 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package backend;
import flixel.addons.transition.FlxTransitionableState;
import states.MainMenuState;
import flixel.input.keyboard.FlxKey;
import debug.codename.Framerate;
import flixel.graphics.FlxGraphic;
import flixel.FlxGame;
import haxe.io.Path;
import openfl.Assets;
import openfl.system.System;
import openfl.Lib;
import openfl.display.Sprite;
import openfl.events.Event;
import openfl.display.StageScaleMode;
import lime.system.System as LimeSystem;
import lime.app.Application;
#if native
import lime.ui.WindowVSyncMode;
#end
import states.TitleState;
import openfl.events.KeyboardEvent;
class Main extends Sprite
{
public static final game = {
width: 1280, // game width
height: 720, // game height
initialState: TitleState, // initial game state
zoom: -1.0, // game state bounds
framerate: 60, // default framerate
skipSplash: true, // if the flixel splash screen should be skipped
startFullscreen: false // if the game should start at fullscreen mode
};
public static var fpsVar:Framerate;
public static function main():Void
{
Lib.current.addChild(new Main());
#if cpp
cpp.NativeGc.enable(true);
#elseif hl
hl.Gc.enable(true);
#end
}
public function new()
{
backend.CrashHandler.init();
#if mobile
Sys.setCwd(StorageUtil.getStorageDirectory());
#if android
StorageUtil.requestPermissions();
#end
mobile.backend.io.Assets.init();
#end
super();
if (stage != null)
{
init();
}
else
{
addEventListener(Event.ADDED_TO_STAGE, init);
}
}
private function init(?E:Event):Void
{
if (hasEventListener(Event.ADDED_TO_STAGE))
{
removeEventListener(Event.ADDED_TO_STAGE, init);
}
#if android
if (!FileSystem.exists(haxe.io.Path.addTrailingSlash(lime.system.System.applicationStorageDirectory) + "useExternal.txt"))
{
File.saveContent(haxe.io.Path.addTrailingSlash(lime.system.System.applicationStorageDirectory) + "useExternal.txt", 'false');
Sys.setCwd(StorageUtil.getStorageDirectory());
}
#end
setupGame();
}
private function setupGame():Void
{
if (game.zoom == -1.0)
game.zoom = 1.0;
#if FEATURE_VIDEOS
hxvlc.util.Handle.init();
#end
Controls.instance = new Controls();
ClientPrefs.loadDefaultKeys();
untyped FlxG.cameras = new backend.rendering.ShadowCameraFrontEnd();
final funkinGame:FlxGame = new FlxGame(game.width, game.height, game.initialState, #if (flixel < "5.0.0") game.zoom, #end game.framerate,
game.framerate, game.skipSplash, game.startFullscreen);
@:privateAccess
{
final soundFrontEnd:flixel.system.frontEnds.SoundFrontEnd = new objects.CustomSoundTray.CustomSoundFrontEnd();
FlxG.sound = soundFrontEnd;
funkinGame._customSoundTray = objects.CustomSoundTray.CustomSoundTray;
}
addChild(funkinGame);
FlxG.game.addChild(fpsVar = new Framerate());
debug.codename.SystemInfo.init();
Lib.current.stage.align = "tl";
Lib.current.stage.scaleMode = StageScaleMode.NO_SCALE;
#if mobile
//FlxG.game.stage.quality = openfl.display.StageQuality.LOW;
#end
if (fpsVar != null)
fpsVar.visible = true;
#if desktop
FlxG.stage.addEventListener(KeyboardEvent.KEY_UP, toggleFullScreen);
FlxG.stage.addEventListener(KeyboardEvent.KEY_UP, emergencyEject);
FlxG.stage.addEventListener(KeyboardEvent.KEY_UP, hotReload);
#end
#if FEATURE_DISCORD_RPC
DiscordClient.prepare();
#end
#if mobile
LimeSystem.allowScreenTimeout = ClientPrefs.data.screensaver;
#end
#if android
FlxG.android.preventDefaultKeys = [flixel.input.android.FlxAndroidKey.BACK];
#end
#if native
FlxG.stage.application.window.setVSyncMode(ClientPrefs.data.vsync ? WindowVSyncMode.ON : WindowVSyncMode.OFF);
#end
FlxSprite.defaultAntialiasing = ClientPrefs.data.antialiasing;
// 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);
}
}
if (FlxG.game != null)
resetSpriteCache(FlxG.game);
});
}
static function resetSpriteCache(sprite:Sprite):Void
{
@:privateAccess
{
sprite.__cacheBitmap = null;
sprite.__cacheBitmapData = null;
}
}
function toggleFullScreen(event:KeyboardEvent):Void
{
if (Controls.instance.justReleased('fullscreen'))
FlxG.fullscreen = !FlxG.fullscreen;
}
function emergencyEject(event:KeyboardEvent):Void
{
if (event.shiftKey && event.keyCode == FlxKey.F4)
{
FlxTransitionableState.skipNextTransIn = FlxTransitionableState.skipNextTransOut = true;
FlxG.switchState(new MainMenuState());
Paths.clearStoredMemory();
}
}
function hotReload(event:KeyboardEvent):Void
{
if (event.shiftKey && event.keyCode == FlxKey.F5)
{
// SHADOW TODO: maybe do some real hot reloading in the future...
FlxG.resetState();
}
}
}