summaryrefslogtreecommitdiff
path: root/window.py
blob: 4c51bd147051ece74503c7c4c275bec17c50839b (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
111
112
113
114
115
116
117
118
119
120
121
122
123
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from time import time

from renderer import ImageRenderer, TransitionMix
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.renderer = None
        self.window_width = 0
        self.window_height = 0
        # TODO
        self.max_framerate = 60
        self.frame_time = int(1000 / self.max_framerate)  # In ms

        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:
            glClearColor(0.0, 0.0, 0.0, 1.0)  # Set the background color to black
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
            #glLoadIdentity()

            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

                glClearColor(0.0, 0.0, 0.0, 1.0)  # Set the background color to black
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
                #glLoadIdentity()

                self.renderer.draw_static(self.texture_current, self.window_width, self.window_height, 1)

                glutSwapBuffers()
            return

        self.window_width, self.window_height = window_width, window_height

        glClearColor(0.0, 0.0, 0.0, 1.0)  # Set the background color to black
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        #glLoadIdentity()

        # DRAW
        transition_time = self.image_time - self.display_duration
        complete = self.renderer.draw_transition(self.texture_current, self.texture_next, self.window_width, self.window_height, delta_time, transition_time, self.transition_duration)

        glutSwapBuffers()

        if complete:
            self.image_time = 0
            self.current_texture_index = self.next_texture_index

    def timer(self, value):
        glutPostRedisplay()
        glutTimerFunc(self.frame_time, self.timer, 0)  # Schedule next frame

    # 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.renderer = ImageRenderer()
        self.renderer.set_transition(TransitionMix)
        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)

        # Enable alpha blending
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

        # Run display
        glutDisplayFunc(self.display)
        glutTimerFunc(self.frame_time, self.timer, 0)
        glutMainLoop()