organize main file

This commit is contained in:
Yonokid
2025-12-27 15:52:59 -05:00
parent 327c48aa1a
commit fbcd181667

View File

@@ -193,14 +193,7 @@ def update_camera_for_window_size(camera, virtual_width, virtual_height):
camera.rotation = global_data.camera.rotation camera.rotation = global_data.camera.rotation
def main(): def setup_logging():
force_dedicated_gpu()
global_data.config = get_config()
match global_data.config["general"]["score_method"]:
case ScoreMethod.GEN3:
global_data.score_db = 'scores_gen3.db'
case ScoreMethod.SHINUCHI:
global_data.score_db = 'scores.db'
log_level = global_data.config["general"]["log_level"] log_level = global_data.config["general"]["log_level"]
if sys.platform == 'win32': if sys.platform == 'win32':
import io import io
@@ -219,12 +212,8 @@ def main():
handlers=[console_handler, file_handler] handlers=[console_handler, file_handler]
) )
sys.excepthook = handle_exception sys.excepthook = handle_exception
logger.info("Starting PyTaiko")
logger.debug(f"Loaded config: {global_data.config}")
screen_width = global_tex.screen_width
screen_height = global_tex.screen_height
def set_config_flags():
if global_data.config["video"]["vsync"]: if global_data.config["video"]["vsync"]:
ray.set_config_flags(ray.ConfigFlags.FLAG_VSYNC_HINT) ray.set_config_flags(ray.ConfigFlags.FLAG_VSYNC_HINT)
logger.info("VSync enabled") logger.info("VSync enabled")
@@ -235,25 +224,21 @@ def main():
ray.set_config_flags(ray.ConfigFlags.FLAG_WINDOW_RESIZABLE) ray.set_config_flags(ray.ConfigFlags.FLAG_WINDOW_RESIZABLE)
ray.set_trace_log_level(ray.TraceLogLevel.LOG_WARNING) ray.set_trace_log_level(ray.TraceLogLevel.LOG_WARNING)
ray.init_window(screen_width, screen_height, "PyTaiko") def init_audio():
logger.info(f"Window initialized: {screen_width}x{screen_height}") audio.set_log_level((logger.level-1)//10)
global_tex.load_screen_textures('global') old_stderr = os.dup(2)
logger.info("Global screen textures loaded") devnull = os.open(os.devnull, os.O_WRONLY)
global_tex.load_zip('chara', 'chara_0') os.dup2(devnull, 2)
global_tex.load_zip('chara', 'chara_1') os.close(devnull)
logger.info("Chara textures loaded") audio.init_audio_device()
if global_data.config["video"]["borderless"]: os.dup2(old_stderr, 2)
ray.toggle_borderless_windowed() os.close(old_stderr)
logger.info("Borderless window enabled") logger.info("Audio device initialized")
if global_data.config["video"]["fullscreen"]:
ray.toggle_fullscreen()
logger.info("Fullscreen enabled")
current_screen = Screens.LOADING
def check_args():
if len(sys.argv) == 1: if len(sys.argv) == 1:
pass return Screens.LOADING
else:
parser = argparse.ArgumentParser(description='Launch game with specified song file') parser = argparse.ArgumentParser(description='Launch game with specified song file')
parser.add_argument('song_path', type=str, help='Path to the TJA song file') parser.add_argument('song_path', type=str, help='Path to the TJA song file')
parser.add_argument('difficulty', type=int, nargs='?', default=None, parser.add_argument('difficulty', type=int, nargs='?', default=None,
@@ -279,19 +264,70 @@ def main():
global_data.session_data[PlayerNum.P1].selected_song = path global_data.session_data[PlayerNum.P1].selected_song = path
global_data.session_data[PlayerNum.P1].selected_difficulty = selected_difficulty global_data.session_data[PlayerNum.P1].selected_difficulty = selected_difficulty
global_data.modifiers[PlayerNum.P1].auto = args.auto global_data.modifiers[PlayerNum.P1].auto = args.auto
return current_screen
def check_discord_heartbeat(current_screen):
if global_data.session_data[global_data.player_num].selected_song != Path():
details = f"Playing Song: {global_data.session_data[global_data.player_num].song_title}"
else:
details = "Idling"
RPC.update(
state=f"In Screen {current_screen}",
details=details,
large_text="PyTaiko",
start=get_current_ms()/1000,
buttons=[{"label": "Play Now", "url": "https://github.com/Yonokid/PyTaiko"}]
)
def draw_fps(last_fps: int):
curr_fps = ray.get_fps()
if curr_fps != 0 and curr_fps != last_fps:
last_fps = curr_fps
if last_fps < 30:
ray.draw_text(f'{last_fps} FPS', 20, 20, 20, ray.RED)
elif last_fps < 60:
ray.draw_text(f'{last_fps} FPS', 20, 20, 20, ray.YELLOW)
else:
ray.draw_text(f'{last_fps} FPS', 20, 20, 20, ray.LIME)
def draw_outer_border(screen_width: int, screen_height: int, last_color: ray.Color):
ray.draw_rectangle(-screen_width, 0, screen_width, screen_height, last_color)
ray.draw_rectangle(screen_width, 0, screen_width, screen_height, last_color)
ray.draw_rectangle(0, -screen_height, screen_width, screen_height, last_color)
ray.draw_rectangle(0, screen_height, screen_width, screen_height, last_color)
def main():
force_dedicated_gpu()
global_data.config = get_config()
match global_data.config["general"]["score_method"]:
case ScoreMethod.GEN3:
global_data.score_db = 'scores_gen3.db'
case ScoreMethod.SHINUCHI:
global_data.score_db = 'scores.db'
setup_logging()
logger.info("Starting PyTaiko")
logger.debug(f"Loaded config: {global_data.config}")
screen_width = global_tex.screen_width
screen_height = global_tex.screen_height
ray.init_window(screen_width, screen_height, "PyTaiko")
logger.info(f"Window initialized: {screen_width}x{screen_height}")
global_tex.load_screen_textures('global')
global_tex.load_zip('chara', 'chara_0')
global_tex.load_zip('chara', 'chara_1')
if global_data.config["video"]["borderless"]:
ray.toggle_borderless_windowed()
logger.info("Borderless window enabled")
if global_data.config["video"]["fullscreen"]:
ray.toggle_fullscreen()
logger.info("Fullscreen enabled")
current_screen = check_args()
logger.info(f"Initial screen: {current_screen}") logger.info(f"Initial screen: {current_screen}")
audio.set_log_level((log_level-1)//10)
old_stderr = os.dup(2)
devnull = os.open(os.devnull, os.O_WRONLY)
os.dup2(devnull, 2)
os.close(devnull)
audio.init_audio_device()
os.dup2(old_stderr, 2)
os.close(old_stderr)
logger.info("Audio device initialized")
create_song_db() create_song_db()
title_screen = TitleScreen('title') title_screen = TitleScreen('title')
@@ -346,17 +382,7 @@ def main():
while not ray.window_should_close(): while not ray.window_should_close():
if discord_connected: if discord_connected:
if global_data.session_data[global_data.player_num].selected_song != Path(): check_discord_heartbeat(current_screen)
details = f"Playing Song: {global_data.session_data[global_data.player_num].song_title}"
else:
details = "Idling"
RPC.update(
state=f"In Screen {current_screen}",
details=details,
large_text="PyTaiko",
start=get_current_ms()/1000,
buttons=[{"label": "Play Now", "url": "https://github.com/Yonokid/PyTaiko"}]
)
if ray.is_key_pressed(global_data.config["keys"]["fullscreen_key"]): if ray.is_key_pressed(global_data.config["keys"]["fullscreen_key"]):
ray.toggle_fullscreen() ray.toggle_fullscreen()
@@ -388,20 +414,9 @@ def main():
global_data.input_locked = 0 global_data.input_locked = 0
if global_data.config["general"]["fps_counter"]: if global_data.config["general"]["fps_counter"]:
curr_fps = ray.get_fps() draw_fps(last_fps)
if curr_fps != 0 and curr_fps != last_fps:
last_fps = curr_fps
if last_fps < 30:
ray.draw_text(f'{last_fps} FPS', 20, 20, 20, ray.RED)
elif last_fps < 60:
ray.draw_text(f'{last_fps} FPS', 20, 20, 20, ray.YELLOW)
else:
ray.draw_text(f'{last_fps} FPS', 20, 20, 20, ray.LIME)
ray.draw_rectangle(-screen_width, 0, screen_width, screen_height, last_color) draw_outer_border(screen_width, screen_height, last_color)
ray.draw_rectangle(screen_width, 0, screen_width, screen_height, last_color)
ray.draw_rectangle(0, -screen_height, screen_width, screen_height, last_color)
ray.draw_rectangle(0, screen_height, screen_width, screen_height, last_color)
ray.end_blend_mode() ray.end_blend_mode()
ray.end_mode_2d() ray.end_mode_2d()