diff options
Diffstat (limited to 'window.py')
| -rw-r--r-- | window.py | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/window.py b/window.py new file mode 100644 index 0000000..29d0522 --- /dev/null +++ b/window.py @@ -0,0 +1,110 @@ +from OpenGL.GL import * +from OpenGL.GLUT import * +from OpenGL.GLU import * +from time import time + +from transition import Transition +from texture import ImageTexture + + +class PixDisplay: + def __init__(self): + self.last_time = 0 + self.start_time = 0 + self.image_time = 0 + self.textures = [] + self.current_texture_index = 0 + self.transition = Transition() + self.window_width = 0 + self.window_height = 0 + + self.display_duration = 2.0 + self.transition_duration = 0.5 + + @property + def next_texture_index(self): return (self.current_texture_index + 1) % len(self.textures) + + @property + def texture_current(self): return self.textures[self.current_texture_index] + + @property + def texture_next(self): return self.textures[self.next_texture_index] + + # Main display function + def display(self): + # Calculate timings + current_time = time() + alive_time = current_time - self.start_time + delta_time = current_time - self.last_time + self.last_time = current_time + + if not self.textures: + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) + glLoadIdentity() + glClearColor(0.0, 0.0, 0.0, 1.0) # Set the background color to black + + glutSwapBuffers() + return + + # Ensure textures are initialized + self.texture_current.gl_init() + self.texture_next.gl_init() + + # Progress image time + self.image_time += delta_time + + # Get window size + window_width, window_height = glutGet(GLUT_WINDOW_WIDTH), glutGet(GLUT_WINDOW_HEIGHT) + + if self.image_time < self.display_duration: + if window_width != self.window_width or window_height != self.window_height: + self.window_width, self.window_height = window_width, window_height + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) + glLoadIdentity() + + self.transition.draw_image(self.texture_current, self.window_width, self.window_height, 1) + + glClearColor(0.0, 0.0, 0.0, 1.0) # Set the background color to black + glutSwapBuffers() + return + + self.window_width, self.window_height = window_width, window_height + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) + glLoadIdentity() + + # DRAW + transition_time = self.image_time - self.display_duration + complete = self.transition.draw(self.texture_current, self.texture_next, self.window_width, self.window_height, delta_time, transition_time, self.transition_duration) + + glClearColor(0.0, 0.0, 0.0, 1.0) # Set the background color to black + + glutSwapBuffers() + + if complete: + self.image_time = 0 + self.current_texture_index = self.next_texture_index + + # Initialization and main loop + def main(self): + # Initialize the window + glutInit(sys.argv) + glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) + glutCreateWindow("Image Viewer with Fade Transition") + glEnable(GL_TEXTURE_2D) + + self.image_time = 0 + self.start_time = time() + self.last_time = time() + + # Set up the OpenGL viewport and projection + glMatrixMode(GL_PROJECTION) + glLoadIdentity() + glOrtho(-1, 1, -1, 1, -1, 1) + glMatrixMode(GL_MODELVIEW) + + glutDisplayFunc(self.display) + glutIdleFunc(self.display) + glutMainLoop() + |
