forked from sapphire-arches/PyOOGL
/
windowmanagement.py
68 lines (56 loc) · 1.96 KB
/
windowmanagement.py
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
from OpenGL.GLUT import glutInit, glutInitDisplayMode, glutInitWindowSize, glutCreateWindow
from OpenGL.GLUT import GLUT_DOUBLE, GLUT_RGB, GLUT_DEPTH
from OpenGL.GLUT import glutDisplayFunc, glutReshapeFunc, glutIdleFunc
from OpenGL.GLUT import glutMainLoop
from OpenGL.GLUT import glutSwapBuffers
from OpenGL.GL import glClear, glClearColor, GL_COLOR_BUFFER_BIT, GL_DEPTH_BUFFER_BIT
from time import time, sleep
from sys import argv
class Window:
"""
represents a GLUT window
"""
def __init__(self, winname, size=(800, 600), display_func=None, resize_func=None):
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
glutInitWindowSize(size[0], size[1])
self._win_id = glutCreateWindow(winname.encode('ascii'))
self.display_func = display_func
self.resize_func = resize_func
self.width = size[0]
self.height = size[1]
if display_func is not None:
display_func = _display_wrapper(display_func)
glutDisplayFunc(display_func)
glutIdleFunc(display_func)
if resize_func is not None:
resize_func = _resize_wrapper(self, resize_func)
glutReshapeFunc(resize_func)
glClearColor(0, 0, 0, 0)
def get_height(self):
return self.height
def get_width(self):
return self.width
def clear(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
def main_loop():
""" starts the glut main loop """
glutMainLoop()
# internal stuffs - do not touch
_lastTime = 0
def _display_wrapper(display):
def disp():
global _lastTime
display()
tick = time()
required_delta = (1/60) - (tick - _lastTime)
if (required_delta > 0):
sleep(required_delta)
_lastTime = tick
glutSwapBuffers()
return disp
def _resize_wrapper(window, func):
def resize(w, h):
window.width = w
window.height = h
func(w, h)
return resize