Esempio n. 1
0
    def __init__(self, display, mountCWDData=True, **kwargs):
        super(PythonicUniverse, self).__init__(display, **kwargs)
        vfs = XDGFileSystem("pyuniverse")
        if mountCWDData:
            vfs.mount("/data", MountDirectory(os.path.join(os.getcwd(), "data")), MountPriority.FileSystem)

        ResourceManager(vfs)

        self.theme = Theme()
        self.theme.addRules(ResourceManager().require("ui.css"))

        mainScreen = self._primaryWidget

        scene = Scene(mainScreen)
        self.addSceneWidget(scene)

        window = WindowWidget(self._windowLayer)
        window.Title.Text = "Test"
        window.AbsoluteRect.XYWH = (32, 32, 128, 128)

        self.theme.applyStyles(self)

        self._shader = ResourceManager().require("/data/shaders/ui.shader")
        self._upsideDownHelper = np.asarray([-1.0, self.AbsoluteRect.Height], dtype=np.float32)
        self._shader.cacheShaders(
            [
                {"texturing": True, "upsideDown": True},
                {"texturing": True, "upsideDown": False},
                {"texturing": False, "upsideDown": False},
            ]
        )
        shader = self._shader.bind(texturing=False)

        shader = self._shader.bind(texturing=True)
        glUniform1i(shader["texture"], 0)

        Shader.unbind()
Esempio n. 2
0
class PythonicUniverse(Application):
    def __init__(self, display, mountCWDData=True, **kwargs):
        super(PythonicUniverse, self).__init__(display, **kwargs)
        vfs = XDGFileSystem("pyuniverse")
        if mountCWDData:
            vfs.mount("/data", MountDirectory(os.path.join(os.getcwd(), "data")), MountPriority.FileSystem)

        ResourceManager(vfs)

        self.theme = Theme()
        self.theme.addRules(ResourceManager().require("ui.css"))

        mainScreen = self._primaryWidget

        scene = Scene(mainScreen)
        self.addSceneWidget(scene)

        window = WindowWidget(self._windowLayer)
        window.Title.Text = "Test"
        window.AbsoluteRect.XYWH = (32, 32, 128, 128)

        self.theme.applyStyles(self)

        self._shader = ResourceManager().require("/data/shaders/ui.shader")
        self._upsideDownHelper = np.asarray([-1.0, self.AbsoluteRect.Height], dtype=np.float32)
        self._shader.cacheShaders(
            [
                {"texturing": True, "upsideDown": True},
                {"texturing": True, "upsideDown": False},
                {"texturing": False, "upsideDown": False},
            ]
        )
        shader = self._shader.bind(texturing=False)

        shader = self._shader.bind(texturing=True)
        glUniform1i(shader["texture"], 0)

        Shader.unbind()

    def clearCairoSurface(self):
        ctx = self._cairoContext
        ctx.set_source_rgba(0.0, 0.0, 0.0, 0.0)
        ctx.set_operator(cairo.OPERATOR_SOURCE)
        ctx.paint()
        ctx.set_operator(cairo.OPERATOR_OVER)
        ctx.set_line_cap(cairo.LINE_CAP_SQUARE)

    def onKeyDown(self, symbol, modifiers):
        if symbol == key.Escape:
            print("bye!")
            self._eventLoop.terminate()
        elif symbol == key.f:
            if self.fullscreen:
                self._window.setWindowed(0, 800, 600)
                self.fullscreen = False
            else:
                self._window.setFullscreen(0, 0, 0, 0)
                self.fullscreen = True

    def doAlign(self):
        super(PythonicUniverse, self).doAlign()

        mainScreen = self._primaryWidget

        w, h = mainScreen.AbsoluteRect.Width, mainScreen.AbsoluteRect.Height
        if (
            hasattr(self, "_cairoSurface")
            and w == self._cairoSurface.get_width()
            and h == self._cairoSurface.get_height()
        ):
            return
        potW, potH = makePOT(w), makePOT(h)

        self.cairoTexCoords = (w / potW, h / potH)

        self.cairoTex = Texture2D(potW, potH, format=GL_RGBA, data=(GL_RGBA, GL_UNSIGNED_BYTE, None))
        self._cairoSurface = cairo.ImageSurface(cairo.FORMAT_ARGB32, w, h)
        self._cairoContext = cairo.Context(self._cairoSurface)
        self._pangoContext = Pango.PangoCairoContext(self._cairoContext)
        self.updateRenderingContext()

    def frameUnsynced(self, deltaT):
        window = self._screens[0][0]
        window.switchTo()

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
        glLoadIdentity()

        wx, wy, ww, wh = self._primaryWidget.AbsoluteRect.XYWH

        for sceneWidget in window._sceneWidgets:
            glViewport(*sceneWidget.AbsoluteRect.XYWH)
            sceneWidget.update(deltaT)
            sceneWidget.renderScene()

        glViewport(0, 0, ww, wh)
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()
        glOrtho(wx, ww, wh, wy, -1.0, 1.0)
        glMatrixMode(GL_MODELVIEW)
        ctx = self._cairoContext
        self.clearCairoSurface()

        self.render()

        self.cairoTex.bind()
        s, t = self.cairoTexCoords
        CGL.glTexCairoSurfaceSubImage2D(GL_TEXTURE_2D, 0, 0, 0, self._cairoSurface)
        glEnable(GL_TEXTURE_2D)
        glEnable(GL_BLEND)
        glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA)
        glBegin(GL_QUADS)
        glTexCoord2f(0, 0)
        glVertex2f(0, 0)
        glTexCoord2f(0, t)
        glVertex2f(0, wh)
        glTexCoord2f(s, t)
        glVertex2f(ww, wh)
        glTexCoord2f(s, 0)
        glVertex2f(ww, 0)
        glEnd()
        Texture2D.unbind()

        window.flip()