From 9a785dfddad4215672c41396b8554477c18b4066 Mon Sep 17 00:00:00 2001 From: Tim Keller Date: Sat, 10 May 2025 18:22:40 -0500 Subject: transition using arrow keys + skip function --- window.py | 77 ++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 49 insertions(+), 28 deletions(-) (limited to 'window.py') diff --git a/window.py b/window.py index ef9e9ee..8b34989 100644 --- a/window.py +++ b/window.py @@ -19,8 +19,13 @@ class PixDisplay: self.win_h = 0 self.max_framerate = 30 - self.display_duration = 2.0 + self.image_duration = 2.0 self.transition_duration = 0.5 + self.auto_transition = True + self.transition_reverse = False + + self.tex = None + self.text_next = None @property def max_framerate(self): @@ -31,17 +36,19 @@ class PixDisplay: self._max_fps = max_fps # This is just for the getter since otherwise e.g. int(1000/int(1000/60)) would round to 62 self.frame_time = int(1000 / 60) # In ms - @property - def prev_texture_index(self): return self.current_texture_index - 1 + def increment_texture_index(self, increment): + if len(self.textures) < 2: + return - @property - def next_texture_index(self): return (self.current_texture_index + 1) % len(self.textures) + self.transition_reverse = increment < 0 - @property - def tex(self): return self.textures[self.current_texture_index] + self.tex_prev = self.textures[self.current_texture_index] + self.current_texture_index = (self.current_texture_index + increment) % len(self.textures) + self.tex = self.textures[self.current_texture_index] - @property - def tex_next(self): return self.textures[self.next_texture_index] + # Ensure textures are initialized + self.tex_prev.gl_init() + self.tex.gl_init() # Main display function def display(self): @@ -51,16 +58,14 @@ class PixDisplay: delta_time = current_time - self.last_time self.last_time = current_time - # Draw black window if no textures are available - if not self.textures: - glClearColor(0.0, 0.0, 0.0, 1.0) - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) - glutSwapBuffers() - return - - # Ensure textures are initialized - self.tex.gl_init() - self.tex_next.gl_init() + if not self.tex: + self.increment_texture_index(0) + # Draw black window if no textures are available + if not self.tex: + glClearColor(0.0, 0.0, 0.0, 1.0) + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) + glutSwapBuffers() + return # Progress image time self.image_time += delta_time @@ -70,30 +75,45 @@ class PixDisplay: self.win_w, self.win_h = glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) # Draw static image except during a transition - if self.image_time <= self.display_duration: + if self.image_time < self.image_duration: # Avoid redraw unless window size has changed if self.win_w != old_win_w or self.win_h != old_win_h: - self.renderer.draw_static(self.tex, self.win_w, self.win_h, 1) + self.renderer.draw_static(self.tex, self.win_w, self.win_h, 1.0) return - # Draw transition - transition_time = self.image_time - self.display_duration + # Start drawing transition once image_time >= image_duration + if self.auto_transition: + self.increment_texture_index(1) + self.auto_transition = False - # Draw frame of transition, function will return True if the transition is complete - self.renderer.draw_transition(self.tex, self.tex_next, self.win_w, self.win_h, delta_time, transition_time, self.transition_duration) + transition_time = self.image_time - self.image_duration + + self.renderer.draw_transition(self.tex_prev, self.tex, self.win_w, self.win_h, delta_time, transition_time, self.transition_duration, self.transition_reverse) if transition_time >= self.transition_duration: self.image_time = 0 - self.current_texture_index = self.next_texture_index + self.auto_transition = True + # Limit framerate def timer(self, value): glutPostRedisplay() glutTimerFunc(self.frame_time, self.timer, 0) # Schedule next frame + def skip(self, increment): + self.auto_transition = False + self.increment_texture_index(increment) + self.image_time = self.image_duration + + def handle_special_key(self, key, x, y): + if key == GLUT_KEY_LEFT: + self.skip(-1) + elif key == GLUT_KEY_RIGHT: + self.skip(1) + # Initialization and main loop - def main(self): + def main(self, glut_args): # Initialize the window - glutInit(sys.argv) + glutInit(glut_args) glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) glutCreateWindow("Image Viewer with Fade Transition") glEnable(GL_TEXTURE_2D) @@ -117,5 +137,6 @@ class PixDisplay: # Run display glutDisplayFunc(self.display) glutTimerFunc(self.frame_time, self.timer, 0) + glutSpecialFunc(self.handle_special_key) glutMainLoop() -- cgit v1.2.3