Exemple #1
0
    def __init__(self,
                 width,
                 height,
                 title='',
                 samples=1,
                 monitor=0,
                 fullscreen=False):
        self._width = width
        self._height = height
        self._title = title

        if not glfw.Init():
            log.error('Glfw Init failed!')
            raise RuntimeError('Glfw Init failed!')
        else:
            log.debug('Glfw Initialized.')

        glfw.WindowHint(glfw.SAMPLES, samples)

        self._window = glfw.CreateWindow(
            self._width, self._height, self._title,
            glfw.GetMonitors()[monitor] if fullscreen else None, None)

        glfw.MakeContextCurrent(self._window)
        if not glInitBindlessTextureNV():
            log.error('Bindless textures not supported!')
            raise RuntimeError('Bindless textures not supported!')
        else:
            log.debug('Bindless textures supported.')

        self.center_pos(monitor)

        self._previous_second = glfw.GetTime()
        self._frame_count = 0.0
Exemple #2
0
 def center_pos(self, monitor_id):
     m = glfw.GetMonitors()
     assert monitor_id >= 0 and monitor_id < len(m)
     monitor_pos = glfw.GetMonitorPos(m[monitor_id])
     video_mode = glfw.GetVideoMode(m[monitor_id])
     xpos = monitor_pos[0] + video_mode.width / 2 - self._width / 2
     ypos = monitor_pos[1] + video_mode.height / 2 - self._height / 2
     glfw.SetWindowPos(self._window, xpos, ypos)
Exemple #3
0
 def test_get_monitors_test_fullscreen(self):
     monitors = glfw.GetMonitors()
     for monitor in monitors:
         self.assertIsInstance(glfw.GetMonitorName(monitor), str)
         x, y = glfw.GetMonitorPos(monitor)
         width, height = glfw.GetMonitorPhysicalSize(monitor)
         window = glfw.CreateWindow(width, height, 'Monitors', monitor)
         glfw.DestroyWindow(window)
def center_pos(monitor_id, W, H):
    # W, H: window dimensions
    mon = glfw.GetMonitors()
    xpos = glfw.GetMonitorPos(mon[monitor_id])[0]+glfw.GetVideoMode(mon[monitor_id]).width/2-W/2
    ypos = glfw.GetMonitorPos(mon[monitor_id])[1]+glfw.GetVideoMode(mon[monitor_id]).height/2-H/2
    glfw.SetWindowPos(window, xpos, ypos)
    def __init__(self,
                 window_width,
                 window_height,
                 samples=1,
                 window_title="",
                 monitor=1,
                 show_at_center=True,
                 offscreen=False):
        self.window_title = window_title
        assert glfw.Init(), "Glfw Init failed!"
        glfw.WindowHint(glfw.SAMPLES, samples)
        if offscreen:
            glfw.WindowHint(glfw.VISIBLE, False)
        mon = glfw.GetMonitors()[monitor] if monitor != None else None
        self.windowID = glfw.CreateWindow(window_width, window_height,
                                          self.window_title, mon)
        assert self.windowID, "Could not create Window!"
        glfw.MakeContextCurrent(self.windowID)

        if not glInitBindlessTextureNV():
            raise RuntimeError("Bindless Textures not supported")

        self.framebuf_width, self.framebuf_height = glfw.GetFramebufferSize(
            self.windowID)
        self.framebuffer_size_callback = []

        def framebuffer_size_callback(window, w, h):
            self.framebuf_width, self.framebuf_height = w, h
            for callback in self.framebuffer_size_callback:
                callback(w, h)

        glfw.SetFramebufferSizeCallback(self.windowID,
                                        framebuffer_size_callback)

        self.key_callback = []

        def key_callback(window, key, scancode, action, mode):
            if action == glfw.PRESS:
                if key == glfw.KEY_ESCAPE:
                    glfw.SetWindowShouldClose(window, True)
            for callback in self.key_callback:
                callback(key, scancode, action, mode)

        glfw.SetKeyCallback(self.windowID, key_callback)

        self.mouse_callback = []

        def mouse_callback(window, xpos, ypos):
            for callback in self.mouse_callback:
                callback(xpos, ypos)

        glfw.SetCursorPosCallback(self.windowID, mouse_callback)

        self.mouse_button_callback = []

        def mouse_button_callback(window, button, action, mods):
            for callback in self.mouse_button_callback:
                callback(button, action, mods)

        glfw.SetMouseButtonCallback(self.windowID, mouse_button_callback)

        self.scroll_callback = []

        def scroll_callback(window, xoffset, yoffset):
            for callback in self.scroll_callback:
                callback(xoffset, yoffset)

        glfw.SetScrollCallback(self.windowID, scroll_callback)

        self.previous_second = glfw.GetTime()
        self.frame_count = 0.0

        if show_at_center:
            monitors = glfw.GetMonitors()
            assert monitor >= 0 and monitor < len(
                monitors), "Invalid monitor selected."
            vidMode = glfw.GetVideoMode(monitors[monitor])
            glfw.SetWindowPos(
                self.windowID,
                vidMode.width / 2 - self.framebuf_width / 2,
                vidMode.height / 2 - self.framebuf_height / 2,
            )
    #glfw.API_UNAVAILABLE
    #glfw.VERSION_UNAVAILABLE
    #glfw.PLATFORM_ERROR
    #glfw.FORMAT_UNAVAILABLE
    #glfw.NO_WINDOW_CONTEXT


glfw.SetErrorCallback(glfw_error_callback)

if not glfw.Init():
    exit(-1)
    # Handle initialization failure

monitor_id = 0

mon = glfw.GetMonitors()
for m in mon:
    mode = glfw.GetVideoMode(m)
    W, H = glfw.GetMonitorPhysicalSize(m)
    print mode.width, mode.height
    print glfw.GetMonitorPos(m)
    print glfw.GetMonitorName(m)

W = 1024
H = int(W / (16. / 9.))

window = glfw.CreateWindow(W, H, "My Title", None, None)
glfw.MakeContextCurrent(window)


def center_pos(monitor_id):