forked from marcan/blitzloop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathglfw.py
More file actions
184 lines (162 loc) · 6.08 KB
/
glfw.py
File metadata and controls
184 lines (162 loc) · 6.08 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2012-2018 Hector Martin "marcan" <hector@marcansoft.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 or version 3.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
import os, platform
from blitzloop import util
import blitzloop.backend.gles_fixes
import OpenGL.GLES2 as gl
import OpenGL.EGL as egl
import glfw
import sys
from ctypes import c_void_p
from blitzloop.backend.common import *
GLFW_CONTEXT_CREATION_API = 0x0002200B
GLFW_EGL_CONTEXT_API = 0x00036002
class Display(BaseDisplay):
KEYMAP = {
glfw.KEY_ESCAPE: "KEY_ESCAPE",
glfw.KEY_ENTER: "KEY_ENTER",
glfw.KEY_UP: "KEY_UP",
glfw.KEY_DOWN: "KEY_DOWN",
glfw.KEY_LEFT: "KEY_LEFT",
glfw.KEY_RIGHT: "KEY_RIGHT",
glfw.KEY_KP_0: "0",
glfw.KEY_KP_1: "1",
glfw.KEY_KP_2: "2",
glfw.KEY_KP_3: "3",
glfw.KEY_KP_4: "4",
glfw.KEY_KP_5: "5",
glfw.KEY_KP_6: "6",
glfw.KEY_KP_7: "7",
glfw.KEY_KP_8: "8",
glfw.KEY_KP_9: "9",
glfw.KEY_KP_DECIMAL: ".",
glfw.KEY_KP_DIVIDE: "/",
glfw.KEY_KP_MULTIPLY: "*",
glfw.KEY_KP_SUBTRACT: "-",
glfw.KEY_KP_ADD: "+",
glfw.KEY_KP_ENTER: "KEY_ENTER",
}
def __init__(self, width=640, height=480, fullscreen=False, aspect=None):
self.gl = gl
if not glfw.init():
raise Exception("GLFW init failed")
glfw.window_hint(glfw.CLIENT_API, glfw.OPENGL_ES_API)
glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, 2)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 0)
glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, 0)
glfw.window_hint(glfw.DOUBLEBUFFER, True)
glfw.window_hint(glfw.DEPTH_BITS, 24)
glfw.window_hint(glfw.ALPHA_BITS, 0)
if platform.system() == "Linux":
try:
glfw.window_hint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API)
except:
pass
monitor = glfw.get_primary_monitor() if fullscreen else None
self.window = glfw.create_window(width, height, "BlitzLoop Karaoke",
monitor, None)
self.x = 0
self.y = 0
glfw.make_context_current(self.window)
BaseDisplay.__init__(self, width, height, fullscreen, aspect)
self._on_reshape(self.window, width, height)
if fullscreen:
self.saved_size = (0, 0, width, height)
glfw.set_input_mode(self.window, glfw.CURSOR, glfw.CURSOR_HIDDEN)
glfw.set_key_callback(self.window, self._on_keyboard)
glfw.set_window_pos_callback(self.window, self._on_move)
glfw.set_window_size_callback(self.window, self._on_reshape)
self._initialize()
def toggle_fullscreen(self):
if self.fullscreen:
glfw.set_window_monitor(self.window, None, *self.saved_size,
util.get_opts().fps)
else:
self.saved_size = self.x, self.y, self.win_width, self.win_height
monitor = glfw.get_primary_monitor()
mode = glfw.get_video_mode(monitor)
glfw.set_window_monitor(self.window, monitor, 0, 0,
mode.size.width, mode.size.height,
mode.refresh_rate)
self.fullscreen = not self.fullscreen
def _on_move(self, window, x, y):
self.x = x
self.y = y
def _on_reshape(self, window, width, height):
self.win_width = width
self.win_height = height
self.gl.glViewport(0, 0, width, height)
self.set_aspect(self.aspect)
def _on_keyboard(self, window, key, scancode, action, mods):
if action != glfw.PRESS:
return
if self.kbd_handler:
if key in self.KEYMAP:
key = self.KEYMAP[key]
else:
key = chr(key).lower()
self.kbd_handler(key)
elif key == glfw.KEY_ESCAPE:
if self.exit_handler:
self.exit_handler()
os._exit(0)
def _render(self):
try:
BaseDisplay._render(self)
except BaseException as e:
sys.excepthook(*sys.exc_info())
os._exit(0)
self.swap_buffers()
def swap_buffers(self):
glfw.swap_buffers(self.window)
def main_loop(self):
while True:
glfw.poll_events()
if glfw.window_should_close(self.window) or self.should_exit:
self.do_exit()
return
try:
self._render()
except StopIteration:
self.do_exit()
return
def get_proc_address(self, s):
return glfw.get_proc_address(s.decode("ascii"))
def get_mpv_params(self):
params = {}
try:
glfw._glfw.glfwGetX11Display.restype = c_void_p
params["x11_display"] = glfw._glfw.glfwGetX11Display()
except AttributeError:
pass
try:
glfw._glfw.glfwGetWaylandDisplay.restype = c_void_p
params["wl_display"] = glfw._glfw.glfwGetWaylandDisplay()
except AttributeError:
pass
return params
if __name__ == "__main__":
d = Display()
def render():
while True:
for color in (1.,0.,0.,1.), (0.,1.,0.,1.), (0.,0.,1.,1.):
gl.glClearColor(*color)
gl.glClear(gl.GL_COLOR_BUFFER_BIT | gl.GL_DEPTH_BUFFER_BIT)
yield None
time.sleep(0.2)
d.set_render_gen(render)
d.main_loop()