Skip to content

Commit

Permalink
title screen works
Browse files Browse the repository at this point in the history
  • Loading branch information
Yonokid committed Sep 10, 2024
1 parent 5696c80 commit 03c85f6
Show file tree
Hide file tree
Showing 7 changed files with 290 additions and 70 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
Songs
__pycache__
__pycache__
attract_videos
File renamed without changes.
Binary file added Sounds/title/VO_ATTRACT_3.ogg
Binary file not shown.
3 changes: 1 addition & 2 deletions game.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,14 @@ def init_tja(self, song, difficulty):
self.song_music = ray.load_music_stream(self.tja.wave)
ray.play_music_stream(self.song_music)
self.start_ms = get_current_ms() - self.tja.offset*1000
self.profiler = Profiler()

def update(self):
ray.update_music_stream(self.song_music)
self.current_ms = get_current_ms() - self.start_ms
self.player_1.update(self)

def draw(self):
self.profiler.profile(self.player_1.draw, (self))
self.player_1.draw(self)

class Player:
def __init__(self, game_screen, player_number, difficulty):
Expand Down
160 changes: 113 additions & 47 deletions global_funcs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import time
import os
import pyray as ray
import cv2

from collections import deque

Expand Down Expand Up @@ -303,6 +304,8 @@ def notes_to_position(self, diff):
elif '#GOGOEND' in part:
self.gogo_time = False
continue
elif '#LYRIC' in part:
continue
#Unrecognized commands will be skipped for now
elif '#' in part:
continue
Expand Down Expand Up @@ -408,6 +411,12 @@ def update(self, current_ms):
elif self.type == 'text_stretch':
self.text_stretch(current_ms,
self.duration)
elif self.type == 'texture_resize':
self.texture_resize(current_ms,
self.duration,
initial_size=self.params.get('initial_size', 1.0),
final_size=self.params.get('final_size', 1.0),
delay=self.params.get('delay', 0.0))

def fade(self, current_ms, duration, initial_opacity, final_opacity, delay, ease_in, ease_out):
def ease_out_progress(progress, ease):
Expand Down Expand Up @@ -478,67 +487,124 @@ def text_stretch(self, current_ms, duration):
else:
self.attribute = 0
self.is_finished = True

import cv2
def texture_resize(self, current_ms, duration, initial_size, final_size, delay):
elapsed_time = current_ms - self.start_ms
if elapsed_time < delay:
self.attribute = initial_size
elapsed_time -= delay
if elapsed_time >= duration:
self.attribute = final_size
self.is_finished = True
elif elapsed_time < duration:
progress = elapsed_time / duration
self.attribute = initial_size + ((final_size - initial_size) * progress)
else:
self.attribute = final_size
self.is_finished = True

class VideoPlayer:
def __init__(self, path, loop_start=None):
video_path = path
audio_path = path[:-4] + '.ogg'
self.video_path = path
self.start_ms = None
self.loop_start = loop_start
self.cap = cv2.VideoCapture(video_path)
fps = self.cap.get(cv2.CAP_PROP_FPS)
self.frame_texture = None
self.frame_time = (1.0 / fps) * 1000
self.start_ms = get_current_ms()
self.audio = ray.load_music_stream(audio_path)
if loop_start is None:
self.audio.looping = False

self.current_frame = None
self.last_frame = self.current_frame
self.frame_index = 0
self.frames = []
self.cap = cv2.VideoCapture(self.video_path)
self.fps = self.cap.get(cv2.CAP_PROP_FPS)

self.is_finished = [False, False]
ray.play_music_stream(self.audio)
audio_path = path[:-4] + '.ogg'
self.audio = ray.load_music_stream(audio_path)

def convert_frames_background(self, index):
if not self.cap.isOpened():
print("Error: Could not open video file.")
return
raise ValueError("Error: Could not open video file.")

def update(self):
current_ms = get_current_ms()
elapsed_time = current_ms - self.start_ms
ray.update_music_stream(self.audio)
if not ray.is_music_stream_playing(self.audio):
self.is_finished[1] = True
total_frames = int(self.cap.get(cv2.CAP_PROP_FRAME_COUNT))
if len(self.frames) == total_frames:
return 0
self.cap.set(cv2.CAP_PROP_POS_FRAMES, index)

if elapsed_time >= self.frame_time:
ret, frame = self.cap.read()
success, frame = self.cap.read()

if not ret:
# Reset to the loop start frame
if self.loop_start == None:
self.is_finished[0] = True
else:
self.cap.set(cv2.CAP_PROP_POS_FRAMES, self.loop_start)
ret, frame = self.cap.read() # Read the frame at loop start
if not ret:
return # If still not successful, return
timestamp = (index / self.fps * 1000)
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

new_frame = ray.Image(frame_rgb.tobytes(), frame_rgb.shape[1], frame_rgb.shape[0], 1, ray.PixelFormat.PIXELFORMAT_UNCOMPRESSED_R8G8B8)

self.frames.append((timestamp, new_frame))
print(len(self.frames), total_frames)

def convert_frames(self):
'''
if ray.file_exists(path[:-4] + '.pkl'):
self.object_file = path[:-4] + '.pkl'
with open(self.object_file, 'rb') as f:
self.frames = pickle.load(f)
print(f"Loaded {path[:-4] + '.pkl'}")
else:
self.object_file = None
if self.object_file is None:
'''
if not self.cap.isOpened():
raise ValueError("Error: Could not open video file.")

frame_count = 0
success, frame = self.cap.read()

while success:
timestamp = (frame_count / self.fps * 1000)
frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
image = ray.Image(frame_rgb.tobytes(), frame_rgb.shape[1], frame_rgb.shape[0], 1, ray.PIXELFORMAT_UNCOMPRESSED_R8G8B8)
new_texture = ray.load_texture_from_image(image)
new_frame = ray.Image(frame_rgb.tobytes(), frame_rgb.shape[1], frame_rgb.shape[0], 1, ray.PixelFormat.PIXELFORMAT_UNCOMPRESSED_R8G8B8)

# Unload the previous texture if it exists
if self.frame_texture:
ray.unload_texture(self.frame_texture)
self.frames.append((timestamp, new_frame))

# Assign the new texture to the instance variable
self.frame_texture = new_texture
self.start_ms = current_ms
success, frame = self.cap.read()
frame_count += 1

self.cap.release()
print(f"Extracted {len(self.frames)} frames.")
'''
with open(path[:-4] + '.pkl', 'wb') as f:
pickle.dump(self.frames, f)
'''
self.start_ms = get_current_ms()

def update(self):
if self.start_ms is None:
self.start_ms = get_current_ms()
ray.play_music_stream(self.audio)
if self.frames == []:
self.convert_frames()
ray.update_music_stream(self.audio)
time_played = ray.get_music_time_played(self.audio) / ray.get_music_time_length(self.audio)
if time_played > 0.95:
self.is_finished[1] = True

if self.frame_index == len(self.frames)-1:
self.is_finished[0] = True
return
timestamp, frame = self.frames[self.frame_index][0], self.frames[self.frame_index][1]

if self.start_ms is not None:
elapsed_time = get_current_ms() - self.start_ms
if elapsed_time >= timestamp:
self.current_frame = ray.load_texture_from_image(frame)
if self.last_frame != self.current_frame and self.last_frame is not None:
ray.unload_texture(self.last_frame)
self.frame_index += 1
self.last_frame = self.current_frame

def draw(self):
if self.frame_texture:
ray.draw_texture(self.frame_texture, 0, 0, ray.WHITE)
if self.current_frame is not None:
ray.draw_texture(self.current_frame, 0, 0, ray.WHITE)

def __del__(self):
# Ensure resources are cleaned up when the instance is deleted
if self.frame_texture:
ray.unload_texture(self.frame_texture)
self.cap.release()
if hasattr(self, 'current_frame') and self.current_frame:
ray.unload_texture(self.current_frame)
if hasattr(self, 'last_frame') and self.last_frame:
ray.unload_texture(self.last_frame)
7 changes: 5 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ def main():
start_song = False
while not ray.window_should_close():
ray.begin_drawing()
ray.clear_background(ray.WHITE)
screen = screen_mapping[current_screen]
if screen == title_screen:
ray.clear_background(ray.BLACK)
else:
ray.clear_background(ray.WHITE)

if ray.is_key_pressed(ray.KeyboardKey.KEY_F11):
ray.toggle_fullscreen()

screen = screen_mapping[current_screen]
if screen == game_screen and not start_song:
game_screen.init_tja(sys.argv[1], sys.argv[2])
start_song = True
Expand Down
Loading

0 comments on commit 03c85f6

Please sign in to comment.