[PYTHON] try pysdl2

If you think that PyGame does not support python3 or 64bit, it seems that development finished. So, when I go to see the site, it looks alive at first glance. The latest version 1.9.1 Packages (August 6th 2009) So, I feel like that is the case.

Try PySDL2 which seems to be the successor. It seems to be a ctypes wrapper for SDL2, so I expect it to work quickly without building.

Execution environment

Download and Examples

The environment is Anaconda version of Python 3.5 (64bit) on Windows.

> cd PySDL2-0.9.3
> C:\Anaconda3\python.exe setup.py install
> cd examples
> C:\Anaconda3\python.exe sdl2hello.py

I get an error that SDL2 cannot be found.

https://www.libsdl.org/download-2.0.php Get the SDL2 dll from. Set the environment variable PYSDL2_DLL_PATH to point to the dll directory.

again

> C:\Anaconda3\python.exe sdl2hello.py

moved. However, the samples using other SDL_gfx do not work.

SDL_gfx

There is no dll ... Alright, let's build everything.

Build a set of SDL2

I built it. Build script.

helloworld.pyとsdl2hello.py

Examples have helloworld.py and sdl2hello.py with the same execution result.

import sdl2.ext
def ext_hello():
    '''
    helloworld.Excerpt from py
    '''
    sdl2.ext.init()
    window = sdl2.ext.Window("Hello World!", size=(592, 460))
    window.show()
    processor = sdl2.ext.TestEventProcessor()
    processor.run(window)
    sdl2.ext.quit()

import ctypes
import sdl2
def hello():
    '''
    helloworld.Excerpt from py
    '''
    sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO)
    window = sdl2.SDL_CreateWindow(b"Hello World",
                                   sdl2.SDL_WINDOWPOS_CENTERED,
                                   sdl2.SDL_WINDOWPOS_CENTERED,
                                   592, 460, sdl2.SDL_WINDOW_SHOWN)
    running = True
    event = sdl2.SDL_Event()
    while running:
        while sdl2.SDL_PollEvent(ctypes.byref(event)) != 0:
            if event.type == sdl2.SDL_QUIT:
                running = False
                break
        sdl2.SDL_Delay(10)

    sdl2.SDL_DestroyWindow(window)
    sdl2.SDL_Quit()


if __name__=='__main__':
    hello()
    #ext_hello()

I had such feeling. I see. ext seems to be a layer of ctypes hiding and a little framework. Let's use sdl2 directly.

SDL_Renderer https://wiki.libsdl.org/SDL_CreateRenderer Try to transplant.

def hello_renderer():
    print("hello_renderer")

    posX = 100
    posY = 100
    width = 320
    height = 240

    sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO);

    win = sdl2.SDL_CreateWindow(b"Hello World", posX, posY, width, height, 0);

    renderer = sdl2.SDL_CreateRenderer(win, -1, sdl2.SDL_RENDERER_ACCELERATED);

    bitmapSurface = sdl2.SDL_LoadBMP(b"hello.bmp");
    bitmapTex = sdl2.SDL_CreateTextureFromSurface(renderer, bitmapSurface);
    sdl2.SDL_FreeSurface(bitmapSurface);

    while True:
        e=sdl2.SDL_Event()
        if sdl2.SDL_PollEvent(ctypes.byref(e)):
            if e.type == sdl2.SDL_QUIT:
                break;

        sdl2.SDL_RenderClear(renderer);
        sdl2.SDL_RenderCopy(renderer, bitmapTex, None, None);
        sdl2.SDL_RenderPresent(renderer);

    sdl2.SDL_DestroyTexture(bitmapTex);
    sdl2.SDL_DestroyRenderer(renderer);
    sdl2.SDL_DestroyWindow(win);

    sdl2.SDL_Quit();

It moved easily.

sdl2.SDL_PollEvent(ctypes.byref(e))
NULL -> None
b"By bytes"

After that, I think I should get information from the example of C.

Recommended Posts

try pysdl2
Try python
Try PyOpenGL
Try touching libtaxii
Try using Tkinter
Try using docker-py
Try numpy.random Generator
Try Distributed TensorFlow
Try using cookiecutter
Try using PDFMiner
Try using geopandas
Try using Selenium
Try using scipy
Python> try: / except:
Try installing Polynote
Try Standard Scaler
Try using pandas.DataFrame
Try type hints
Try using django-swiftbrowser
Try using matplotlib
Try using tf.metrics
Try installing chainer-gogh
Try moving deepdream
Try using PyODE
Try running Pyston 0.1
Try building PySide2