[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Pygame for SDL 2, and how it may look.



Hello everyone,

I have written some proof-of-concept code on how SDL 2 windows and renderers may be incorporated into the Pygame Python API. Here is the latest incarnation of a previous post (http://archives.seul.org/pygame/users/Mar-2017/msg00139.html) as a pygame.example program:

"""Render an image to a window
This is adapted from the SDL 2 example at
<http://www.willusher.io/sdl2%20tutorials/2013/08/17/lesson-1-hello-world>.
"""

import pygame
from pygame.display import RendererWindow
import os


def get_resource_path():
    my_dir = os.path.dirname(os.path.abspath(__file__))
    return os.path.join(my_dir, 'data')


def run():
    pygame.display.init()
    title = "Hello World!"
    rect = (100, 100, 640, 480)
    flags = RendererWindow.SHOWN
    with RendererWindow(title, rect, flags) as win:
        ren = win.renderer
        flags = (ren.ACCELERATED  |
                 ren.PRESENTVSYNC   )
        ren.open(flags)
        image_path = os.path.join(get_resource_path(), 'hello.bmp')
        bmp = pygame.image.load(image_path)
        tex = ren.new_texture_from_surface(bmp)

        for i in range(3):
            ren.clear()
            tex.copy_to_renderer()
            ren.update()
            pygame.time.delay(1000)
    pygame.quit()

if __name__ == '__main__':
run()


A window with renderer and a window with surface are implemented as distinct subclasses of a standard Window extension type. Each Window instance is an open window on the display. Window updates are perform by methods on the corresponding renderer or surface. Window and renderer specific constants, such as SDL_WINDOW_SHOWN and SDL_RENDERER_ACCELERATED are class attributes, though they could also be included in pygame.locals.

This is preliminary design work, so feedback is not only welcome but also necessary.

Lenard Lindstrom