comparison pytouhou/lib/glfw.pyx @ 636:4fa0a8e7d941

Add a GLFW implementation of gui.Window.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Sun, 14 May 2017 20:14:03 +0100
parents
children f953ae5b3732
comparison
equal deleted inserted replaced
635:80687f258001 636:4fa0a8e7d941
1 # -*- encoding: utf-8 -*-
2 ##
3 ## Copyright (C) 2016 Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
4 ##
5 ## This program is free software; you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published
7 ## by the Free Software Foundation; version 3 only.
8 ##
9 ## This program is distributed in the hope that it will be useful,
10 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 ## GNU General Public License for more details.
13 ##
14
15 from .gui cimport SHOOT, BOMB, FOCUS, UP, DOWN, LEFT, RIGHT, SKIP
16
17 CLIENT_API = GLFW_CLIENT_API
18 OPENGL_PROFILE = GLFW_OPENGL_PROFILE
19 CONTEXT_VERSION_MAJOR = GLFW_CONTEXT_VERSION_MAJOR
20 CONTEXT_VERSION_MINOR = GLFW_CONTEXT_VERSION_MINOR
21 DEPTH_BITS = GLFW_DEPTH_BITS
22 ALPHA_BITS = GLFW_ALPHA_BITS
23 RESIZABLE = GLFW_RESIZABLE
24 DOUBLEBUFFER = GLFW_DOUBLEBUFFER
25
26 OPENGL_API = GLFW_OPENGL_API
27 OPENGL_ES_API = GLFW_OPENGL_ES_API
28 OPENGL_CORE_PROFILE = GLFW_OPENGL_CORE_PROFILE
29
30 cdef void error_callback(int a, const char* b):
31 print('GLFW error 0x%x: %s' % (a, b.decode('utf-8')))
32
33 cdef list _global_events = []
34
35 cdef void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods):
36 if action != GLFW_PRESS:
37 return
38 if key == GLFW_KEY_ESCAPE:
39 _global_events.append((gui.PAUSE, None))
40 elif key in (GLFW_KEY_P, GLFW_KEY_HOME):
41 _global_events.append((gui.SCREENSHOT, None))
42 elif key == GLFW_KEY_DOWN:
43 _global_events.append((gui.DOWN, None))
44 elif key == GLFW_KEY_F11:
45 _global_events.append((gui.FULLSCREEN, None))
46 elif key == GLFW_KEY_ENTER:
47 if mods & GLFW_MOD_ALT:
48 _global_events.append((gui.FULLSCREEN, None))
49
50 cdef void size_callback(GLFWwindow* window, int width, int height):
51 _global_events.append((gui.RESIZE, (width, height)))
52
53 cdef void close_callback(GLFWwindow* window):
54 _global_events.append((gui.EXIT, None))
55
56 cdef void init() except *:
57 glfwSetErrorCallback(<GLFWerrorfun>error_callback)
58 ret = glfwInit()
59 if not ret:
60 raise Exception('TODO')
61
62 cdef void terminate() nogil:
63 glfwTerminate()
64
65 cdef void window_hint(int hint, int value) nogil:
66 glfwWindowHint(hint, value)
67
68 cdef class Window:
69 def __init__(self, int width, int height, str title, Monitor monitor=None, Window share=None):
70 cdef GLFWmonitor* c_monitor = NULL
71 cdef GLFWwindow* c_share = NULL
72 if monitor is not None:
73 c_monitor = monitor.monitor
74 if share is not None:
75 c_share = share.window
76 self.window = glfwCreateWindow(width, height, title.encode('utf-8'), c_monitor, c_share)
77 if self.window == NULL:
78 raise Exception('TODO')
79 glfwSetFramebufferSizeCallback(self.window, <GLFWframebuffersizefun>size_callback)
80 glfwSetWindowCloseCallback(self.window, <GLFWwindowclosefun>close_callback)
81 glfwSetKeyCallback(self.window, <GLFWkeyfun>key_callback)
82
83 def __del__(self):
84 glfwDestroyWindow(self.window)
85
86 cdef void create_gl_context(self) except *:
87 glfwMakeContextCurrent(self.window)
88
89 cdef void present(self) nogil:
90 glfwSwapBuffers(self.window)
91
92 cdef void set_window_size(self, int width, int height) nogil:
93 pass
94
95 cdef list get_events(self):
96 glfwPollEvents()
97 events = _global_events[:]
98 _global_events.clear()
99 return events
100
101 cdef void toggle_fullscreen(self) nogil:
102 monitor = glfwGetWindowMonitor(self.window)
103 if monitor == NULL:
104 monitor = glfwGetPrimaryMonitor()
105 else:
106 monitor = NULL
107 # TODO: save the previous size.
108 glfwSetWindowMonitor(self.window, monitor, 0, 0, 640, 480, 60)
109
110 cdef int get_keystate(self) nogil:
111 cdef int keystate = 0
112 if glfwGetKey(self.window, GLFW_KEY_Z):
113 keystate |= SHOOT
114 if glfwGetKey(self.window, GLFW_KEY_X):
115 keystate |= BOMB
116 if glfwGetKey(self.window, GLFW_KEY_LEFT_SHIFT):
117 keystate |= FOCUS
118 if glfwGetKey(self.window, GLFW_KEY_UP):
119 keystate |= UP
120 if glfwGetKey(self.window, GLFW_KEY_DOWN):
121 keystate |= DOWN
122 if glfwGetKey(self.window, GLFW_KEY_LEFT):
123 keystate |= LEFT
124 if glfwGetKey(self.window, GLFW_KEY_RIGHT):
125 keystate |= RIGHT
126 if glfwGetKey(self.window, GLFW_KEY_LEFT_CONTROL):
127 keystate |= SKIP
128 return keystate