LoginSignup
11
14

More than 5 years have passed since last update.

pysdl2やってみる

Posted at

PyGameがなんかpython3とか64bit対応してないなぁと思ったら開発終了しているらしい。
で、サイトを見に行ってみると一見生きているように見えるのだが、
最新版が
1.9.1 Packages (August 6th 2009)
なので、まぁそうなのかという感じである。

後継らしいPySDL2をやってみる。
SDL2のctypesラッパーらしいのでビルドしなくてもさくっと動くのではないかと期待。

実行環境

DownloadしてExamples

環境は、Anaconda版のPython3.5(64bit) on Windows。

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

SDL2が見つからないエラーが出る。

https://www.libsdl.org/download-2.0.php
からSDL2のdllを入手。
環境変数PYSDL2_DLL_PATHがdllのディレクトリを指すようにセット。

再度

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

動いた。
しかし、他のSDL_gfxを使っているサンプルが動かぬ。

SDL_gfx

dll無いな・・・。
よし、全部ビルドしよう。

SDL2一式ビルドする

ビルドした。
ビルドスクリプト。

helloworld.pyとsdl2hello.py

examplesに実行結果が同じhelloworld.pyとsdl2hello.pyがある。

import sdl2.ext
def ext_hello():
    '''
    helloworld.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.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()

という感じだった。
なるほど。extはctypesの隠蔽とちょっとしたフレームワークの層のようだ。
sdl2を直接使うことにしよう。

SDL_Renderer

https://wiki.libsdl.org/SDL_CreateRenderer
を移植してみる。

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();

あっさり動いた。

  • byref sdl2.SDL_PollEvent(ctypes.byref(e))
  • nullptr NULL -> None
  • 文字列は b"bytesの方で"

後は、Cの例から情報を得ればいけそう。

11
14
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
14