94 lines
2.6 KiB
Python
94 lines
2.6 KiB
Python
import sys
|
|
|
|
import json
|
|
from importlib.resources import open_text, open_binary
|
|
from importlib.readers import FileReader
|
|
from importlib.machinery import SourcelessFileLoader
|
|
from io import BufferedReader
|
|
from pathlib import Path
|
|
from time import time
|
|
|
|
import yaml
|
|
from random import random, seed
|
|
from copy import deepcopy
|
|
|
|
from pygame.transform import scale
|
|
import pygame
|
|
|
|
from starsky_presenter.controller.window import ControlWindow
|
|
from starsky_presenter.projection.screen import Screen, Star
|
|
|
|
def dict_update(d1: dict, d2: dict) -> dict:
|
|
out = deepcopy(d1)
|
|
out.update(d2)
|
|
return out
|
|
|
|
def cli_main():
|
|
seed(43)
|
|
reader = FileReader(SourcelessFileLoader("resource_loader", f"{Path(__file__).parent}/resources"))
|
|
|
|
scenes = yaml.safe_load(reader.open_resource('resources/scenes.yml'))
|
|
objects = yaml.safe_load(reader.open_resource('resources/objects.yml'))
|
|
|
|
scene_data = [
|
|
dict_update(
|
|
{ obj['sid']: dict_update(obj, scenes[i][obj['sid']]) if scenes[i] and obj['sid'] in scenes[i] else deepcopy(obj) for obj in objects["stars"] },
|
|
{
|
|
'transition': scenes[i]['transition'],
|
|
}
|
|
)
|
|
if i < len(scenes) else { obj['sid']: deepcopy(obj) for obj in objects["stars"] }
|
|
for i in range(0,9)
|
|
]
|
|
|
|
star_references = {}
|
|
|
|
#print(json.dumps(scenes, indent=4))
|
|
#print(json.dumps(objects, indent=4))
|
|
#print(json.dumps(scene_data, indent=4))
|
|
|
|
def setup_star_instance(data: dict) -> Star:
|
|
image_path = f"resources/{data['image']}"
|
|
image = pygame.image.load(reader.open_resource(image_path))
|
|
data['image'] = image
|
|
|
|
star = Star(**data)
|
|
star_references[data['sid']] = star
|
|
return star
|
|
|
|
|
|
stars = [ setup_star_instance(star) for star in objects["stars"] ]
|
|
|
|
# load the background image
|
|
background = pygame.image.load(reader.open_resource(f"resources/{objects['background']}"))
|
|
|
|
screen = Screen(stars, background, objects['framerate'])
|
|
control_window = ControlWindow(screen, len(scenes))
|
|
|
|
last_time = time()
|
|
frame_count = 0
|
|
fps_update = objects['framerate']
|
|
star_time = time()
|
|
|
|
while control_window.is_running():
|
|
control_window.process_events()
|
|
screen.update(scene_data)
|
|
if frame_count % fps_update == 0:
|
|
now = time()
|
|
print(f"{time() - star_time:.3f}s: {1/((now - last_time) / fps_update):.02f} fps")
|
|
last_time = now
|
|
frame_count = 0
|
|
|
|
frame_count += 1
|
|
|
|
|
|
screen.close()
|
|
control_window.close()
|
|
pygame.quit()
|
|
|
|
exit(0)
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
cli_main() |