def __init__(self, *, size=None, title=None): super().__init__() # Handle inputs if not size: size = 640, 480 title = str(title or "") # Set window hints glfw.window_hint(glfw.CLIENT_API, glfw.NO_API) glfw.window_hint(glfw.RESIZABLE, True) # see https://github.com/FlorianRhiem/pyGLFW/issues/42 # Alternatively, from pyGLFW 1.10 one can set glfw.ERROR_REPORTING='warn' if sys.platform.startswith("linux"): if "wayland" in os.getenv("XDG_SESSION_TYPE", "").lower(): glfw.window_hint(glfw.FOCUSED, False) # prevent Wayland focus error # Create the window (the initial size may not be in logical pixels) self._window = glfw.create_window(int(size[0]), int(size[1]), title, None, None) # Register ourselves self._need_draw = True all_glfw_canvases.add(self) # Register callbacks. We may get notified too often, but that's # ok, they'll result in a single draw. glfw.set_window_content_scale_callback(self._window, self._on_pixelratio_change) glfw.set_framebuffer_size_callback(self._window, self._on_size_change) glfw.set_window_close_callback(self._window, self._on_close) glfw.set_window_refresh_callback(self._window, self._on_window_dirty) glfw.set_window_focus_callback(self._window, self._on_window_dirty) glfw.set_window_maximize_callback(self._window, self._on_window_dirty) # Initialize the size self.set_logical_size(*size)
def __init__(self, **kwargs): super().__init__(**kwargs) if not glfw.init(): raise ValueError("Failed to initialize glfw") # Configure the OpenGL context glfw.window_hint(glfw.CONTEXT_CREATION_API, glfw.NATIVE_CONTEXT_API) glfw.window_hint(glfw.CLIENT_API, glfw.OPENGL_API) glfw.window_hint(glfw.CONTEXT_VERSION_MAJOR, self.gl_version[0]) glfw.window_hint(glfw.CONTEXT_VERSION_MINOR, self.gl_version[1]) glfw.window_hint(glfw.OPENGL_PROFILE, glfw.OPENGL_CORE_PROFILE) glfw.window_hint(glfw.OPENGL_FORWARD_COMPAT, True) glfw.window_hint(glfw.RESIZABLE, self.resizable) glfw.window_hint(glfw.DOUBLEBUFFER, True) glfw.window_hint(glfw.DEPTH_BITS, 24) glfw.window_hint(glfw.SAMPLES, self.samples) glfw.window_hint(glfw.SCALE_TO_MONITOR, glfw.TRUE) monitor = None if self.fullscreen: self._set_fullscreen(True) self._window = glfw.create_window(self.width, self.height, self.title, monitor, None) self._has_focus = True if not self._window: glfw.terminate() raise ValueError("Failed to create window") self.cursor = self._cursor self._buffer_width, self._buffer_height = glfw.get_framebuffer_size( self._window) glfw.make_context_current(self._window) if self.vsync: glfw.swap_interval(1) else: glfw.swap_interval(0) glfw.set_key_callback(self._window, self.glfw_key_event_callback) glfw.set_cursor_pos_callback(self._window, self.glfw_mouse_event_callback) glfw.set_mouse_button_callback(self._window, self.glfw_mouse_button_callback) glfw.set_scroll_callback(self._window, self.glfw_mouse_scroll_callback) glfw.set_window_size_callback(self._window, self.glfw_window_resize_callback) glfw.set_char_callback(self._window, self.glfw_char_callback) glfw.set_window_focus_callback(self._window, self.glfw_window_focus) glfw.set_cursor_enter_callback(self._window, self.glfw_cursor_enter) glfw.set_window_iconify_callback(self._window, self.glfw_window_iconify) glfw.set_window_close_callback(self._window, self.glfw_window_close) self.init_mgl_context() self.set_default_viewport()
def set_callbacks(self): def resize_clb(glfw_window, width, height): self.config["screen_width"] = width self.config["screen_height"] = height self.config.store() def frame_resize_clb(glfw_window, width, height): self.set_size(width, height) def mouse_look_clb(glfw_window, x_pos, y_pos): if not self.focused or not self.mouse_captured: return if not self.mouse_set: self.last_mouse_pos = (x_pos, y_pos) self.mouse_set = True x_offset: float = x_pos - self.last_mouse_pos[0] y_offset: float = self.last_mouse_pos[1] - y_pos self.last_mouse_pos = (x_pos, y_pos) self.cam.process_mouse_movement(x_offset, y_offset) def mouse_button_clb(glfw_window, button: int, action: int, mods: int): if not self.focused: return if button == glfw.MOUSE_BUTTON_RIGHT and action == glfw.PRESS: self.toggle_mouse_capture() def focus_clb(glfw_window, focused: int): if focused: self.focused = True else: self.focused = False def window_pos_clb(glfw_window, x_pos: int, y_pos: int): if len(glfw.get_monitors()) >= 1: for monitor_id, monitor in enumerate(glfw.get_monitors()): m_x, m_y, width, height = glfw.get_monitor_workarea( monitor) if m_x <= x_pos < m_x + width and m_y <= y_pos < m_y + height: self.config["monitor_id"] = monitor_id self.config["screen_x"] = x_pos self.config["screen_y"] = y_pos self.config.store() def key_input_clb(glfw_window, key, scancode, action, mode): if not self.focused: return if key == glfw.KEY_ESCAPE and action == glfw.PRESS: glfw.set_window_should_close(glfw_window, True) if key == glfw.KEY_W and action == glfw.PRESS: self.cam.move(Vector3([0, 0, 1])) elif key == glfw.KEY_W and action == glfw.RELEASE: self.cam.stop(Vector3([0, 0, 1])) if key == glfw.KEY_S and action == glfw.PRESS: self.cam.move(Vector3([0, 0, -1])) elif key == glfw.KEY_S and action == glfw.RELEASE: self.cam.stop(Vector3([0, 0, -1])) if key == glfw.KEY_A and action == glfw.PRESS: self.cam.move(Vector3([-1, 0, 0])) elif key == glfw.KEY_A and action == glfw.RELEASE: self.cam.stop(Vector3([-1, 0, 0])) if key == glfw.KEY_D and action == glfw.PRESS: self.cam.move(Vector3([1, 0, 0])) elif key == glfw.KEY_D and action == glfw.RELEASE: self.cam.stop(Vector3([1, 0, 0])) if key == glfw.KEY_F and action == glfw.RELEASE: self.freeze = not self.freeze if key == glfw.KEY_G and action == glfw.RELEASE: self.gradient = not self.gradient if key == glfw.KEY_H and action == glfw.RELEASE: self.cam.rotate_around_base = not self.cam.rotate_around_base self.config["camera_rotation"] = self.cam.rotate_around_base self.config.store() if key == glfw.KEY_K and action == glfw.RELEASE: self.screenshot = True if key == glfw.KEY_R and action == glfw.RELEASE: self.record = not self.record if key == glfw.KEY_0 and action == glfw.RELEASE: self.cam.set_position(CameraPose.LEFT) if key == glfw.KEY_1 and action == glfw.RELEASE: self.cam.set_position(CameraPose.FRONT) if key == glfw.KEY_2 and action == glfw.RELEASE: self.cam.set_position(CameraPose.RIGHT) if key == glfw.KEY_3 and action == glfw.RELEASE: self.cam.set_position(CameraPose.BACK_RIGHT) if key == glfw.KEY_4 and action == glfw.RELEASE: self.cam.set_position(CameraPose.BACK_RIGHT) if key == glfw.KEY_5 and action == glfw.RELEASE: self.cam.set_position(CameraPose.UPPER_BACK_LEFT) if key == glfw.KEY_6 and action == glfw.RELEASE: self.cam.set_position(CameraPose.UPPER_BACK_RIGHT) if key == glfw.KEY_7 and action == glfw.RELEASE: self.cam.set_position(CameraPose.LOWER_BACK_RIGHT) if key == glfw.KEY_8 and action == glfw.RELEASE: self.cam.set_position(CameraPose.BACK) if key == glfw.KEY_9 and action == glfw.RELEASE: self.cam.set_position(CameraPose.LEFT) glfw.set_window_size_callback(self.window_handle, resize_clb) glfw.set_framebuffer_size_callback(self.window_handle, frame_resize_clb) glfw.set_cursor_pos_callback(self.window_handle, mouse_look_clb) glfw.set_key_callback(self.window_handle, key_input_clb) glfw.set_mouse_button_callback(self.window_handle, mouse_button_clb) glfw.set_window_focus_callback(self.window_handle, focus_clb) glfw.set_window_pos_callback(self.window_handle, window_pos_clb)
def __init__(self, specification: WindowSpecs, startImgui: bool = False): start = time.perf_counter() self.isActive = True self.baseTitle = specification.title self.frameTime = 1.0 if not glfw.init(): raise GraphicsException("Cannot initialize GLFW.") glfw.set_error_callback(self.__GlfwErrorCb) ver = ".".join(str(x) for x in glfw.get_version()) Debug.Log(f"GLFW version: {ver}", LogLevel.Info) self.__SetDefaultWindowFlags(specification) if specification.fullscreen: self.__handle = self.__CreateWindowFullscreen(specification) Debug.Log("Window started in fulscreen mode.", LogLevel.Info) else: self.__handle = self.__CreateWindowNormal(specification) glfw.make_context_current(self.__handle) self.__GetScreenInfo(specification) # enginePreview.RenderPreview() # glfw.swap_buffers(self.__handle) glfw.set_input_mode( self.__handle, glfw.CURSOR, glfw.CURSOR_NORMAL if specification.cursorVisible else glfw.CURSOR_HIDDEN) glfw.set_framebuffer_size_callback(self.__handle, self.__ResizeCb) glfw.set_cursor_pos_callback(self.__handle, self.__CursorPosCb) glfw.set_window_iconify_callback(self.__handle, self.__IconifyCb) glfw.set_mouse_button_callback(self.__handle, self.__MouseCb) glfw.set_scroll_callback(self.__handle, self.__MouseScrollCb) glfw.set_key_callback(self.__handle, self.__KeyCb) glfw.set_window_pos_callback(self.__handle, self.__WindowPosCallback) glfw.set_window_focus_callback(self.__handle, self.__WindowFocusCallback) #set icon if specification.iconFilepath: if not os.path.endswith(".ico"): raise SpykeException( f"Invalid icon extension: {os.path.splitext(specification.iconFilepath)}." ) self.__LoadIcon(specification.iconFilepath) else: self.__LoadIcon(DEFAULT_ICON_FILEPATH) self.SetVsync(specification.vsync) self.positionX, self.positionY = glfw.get_window_pos(self.__handle) Renderer.Initialize(Renderer.screenStats.width, Renderer.screenStats.height, specification.samples) self.OnLoad() if startImgui: Imgui.Initialize() atexit.register(Imgui.Close) gc.collect() Debug.Log( f"GLFW window initialized in {time.perf_counter() - start} seconds.", LogLevel.Info)