summaryrefslogtreecommitdiff
path: root/window.py
blob: 29d052257e7f05f18c6ce8e6f1f945f73c03e492 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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()