Пример #1
0
    def __init__(self, parent, length=10):
        """@parent is the layer where this object is added."""
        ui.String.__init__(self, parent, u"CPU:")
        self.values = [0.0] * length
        self.idx = 0
        self.sum = 0
        self.clock = sf.Clock()
        self._sprite.SetSize(12)
        self.set_center_rel(1.0, 0.0)
        self.set_position_rel(0.97, 0.05)

        self.ticks = 0

        self.add_action(uiactions.CallInfinitely0(self.tick_once))
Пример #2
0
def main():

    # Create main window
    App = sf.RenderWindow(sf.VideoMode(800, 600), "SFML OpenGL")
    App.SetActive()

    # Create a sprite for the background
    BackgroundImage = sf.Image()
    if not BackgroundImage.LoadFromFile(
            "../../samples/bin/datas/opengl/background.jpg"):
        return
    Background = sf.Sprite(BackgroundImage)

    # Load an OpenGL texture.
    # We could directly use a sf.Image as an OpenGL texture (with its Bind() member function),
    # but here we want more control on it (generate mipmaps, ...) so we create a new one

    Image = sf.Image()
    if not Image.LoadFromFile("../../samples/bin/datas/opengl/texture.jpg"):
        return
    # The next line is a bit different from the C++ version
    Texture = glGenTextures(1)  # instead of glGenTextures(1, &Texture);
    glBindTexture(GL_TEXTURE_2D, Texture)
    # It is almost the same line there, except in C++, the last argument was Image.GetPixelsPtr().
    # In python, GetPixels simply returns a string.
    gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, Image.GetWidth(),
                      Image.GetHeight(), GL_RGBA, GL_UNSIGNED_BYTE,
                      Image.GetPixels())
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
                    GL_LINEAR_MIPMAP_LINEAR)

    # Enable Z-buffer read and write
    glEnable(GL_DEPTH_TEST)
    glDepthMask(GL_TRUE)
    glClearDepth(1.)

    # Setup a perspective projection
    glMatrixMode(GL_PROJECTION)
    glLoadIdentity()
    gluPerspective(90., 1., 1., 500.)

    # Bind our texture
    glEnable(GL_TEXTURE_2D)
    glBindTexture(GL_TEXTURE_2D, Texture)
    glColor4f(1., 1., 1., 1.)

    # Create a clock for measuring the time elapsed
    Clock = sf.Clock()

    # Start game loop
    while App.IsOpened():
        # Process events
        Event = sf.Event()
        while App.GetEvent(Event):
            # Close window : exit
            if Event.Type == sf.Event.Closed:
                App.Close()

            # Escape key : exit
            if (Event.Type == sf.Event.KeyPressed) and (Event.Key.Code
                                                        == sf.Key.Escape):
                App.Close()

            # Adjust the viewport when the window is resized
            if Event.Type == sf.Event.Resized:
                glViewport(0, 0, Event.Size.Width, Event.Size.Height)

        # Draw background
        App.Draw(Background)

        # Active window to be able to perform OpenGL commands.
        App.SetActive()

        # Clear depth buffer
        glClear(GL_DEPTH_BUFFER_BIT)

        # Apply some transformations
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()
        glTranslatef(0, 0, -200)
        glRotatef(Clock.GetElapsedTime() * 50, 1, 0, 0)
        glRotatef(Clock.GetElapsedTime() * 30, 0, 1, 0)
        glRotatef(Clock.GetElapsedTime() * 90, 0, 0, 1)

        # Draw a cube
        glBegin(GL_QUADS)

        glTexCoord2f(0, 0)
        glVertex3f(-50., -50., -50.)
        glTexCoord2f(0, 1)
        glVertex3f(-50., 50., -50.)
        glTexCoord2f(1, 1)
        glVertex3f(50., 50., -50.)
        glTexCoord2f(1, 0)
        glVertex3f(50., -50., -50.)

        glTexCoord2f(0, 0)
        glVertex3f(-50., -50., 50.)
        glTexCoord2f(0, 1)
        glVertex3f(-50., 50., 50.)
        glTexCoord2f(1, 1)
        glVertex3f(50., 50., 50.)
        glTexCoord2f(1, 0)
        glVertex3f(50., -50., 50.)

        glTexCoord2f(0, 0)
        glVertex3f(-50., -50., -50.)
        glTexCoord2f(0, 1)
        glVertex3f(-50., 50., -50.)
        glTexCoord2f(1, 1)
        glVertex3f(-50., 50., 50.)
        glTexCoord2f(1, 0)
        glVertex3f(-50., -50., 50.)

        glTexCoord2f(0, 0)
        glVertex3f(50., -50., -50.)
        glTexCoord2f(0, 1)
        glVertex3f(50., 50., -50.)
        glTexCoord2f(1, 1)
        glVertex3f(50., 50., 50.)
        glTexCoord2f(1, 0)
        glVertex3f(50., -50., 50.)

        glTexCoord2f(0, 1)
        glVertex3f(-50., -50., 50.)
        glTexCoord2f(0, 0)
        glVertex3f(-50., -50., -50.)
        glTexCoord2f(1, 0)
        glVertex3f(50., -50., -50.)
        glTexCoord2f(1, 1)
        glVertex3f(50., -50., 50.)

        glTexCoord2f(0, 1)
        glVertex3f(-50., 50., 50.)
        glTexCoord2f(0, 0)
        glVertex3f(-50., 50., -50.)
        glTexCoord2f(1, 0)
        glVertex3f(50., 50., -50.)
        glTexCoord2f(1, 1)
        glVertex3f(50., 50., 50.)

        glEnd()

        # Draw some text on top of our OpenGL object
        Text = sf.Text("This is a rotating cube")
        Text.SetPosition(230., 300.)
        Text.SetColor(sf.Color(128, 0, 128))
        App.Draw(Text)

        # Finally, display the rendered frame on screen
        App.Display()

# Don't forget to destroy our texture
    # In C++, the call to this function was a bit different
    glDeleteTextures(Texture)  # instead of glDeleteTextures(1, &Texture);

    return
Пример #3
0
# Load a fancy font.
cheese = sf.Font()
cheese.LoadFromFile("data/cheeseburger.ttf")

# Create a text.
text = sf.Text(u"Hello SFML from Python!", cheese, 50)
text.SetOrigin(text.GetRect().GetSize()[0] / 2,
               text.GetRect().GetSize()[1] / 2)
text.SetPosition(400, 300)
text.SetColor(sf.Color(0, 100, 0, 100))

# Create a text for FPS display.
fpstext = sf.Text(u"FPS: --", cheese)
fpstext.SetColor(sf.Color(0, 0, 0))
currentfps = 0
fpsclock = sf.Clock()

# Load apple image from file.
appleimage = sf.Image()
appleimage.LoadFromFile("data/apple.png")

# Create some apples with random position, speed, rotation and color.
apples = [Apple(appleimage) for num in range(0, 100)]
for apple in apples:
    apple.sprite.SetOrigin(appleimage.GetWidth() / 2,
                           appleimage.GetHeight() / 2)
    apple.sprite.SetPosition(
        random.randint(apple.sprite.GetOrigin()[0],
                       Resolution[0] - apple.sprite.GetOrigin()[0]),
        random.randint(apple.sprite.GetOrigin()[1],
                       Resolution[1] - apple.sprite.GetOrigin()[1]))