#!python # -*- mode: python; Encoding: utf-8; coding: utf-8 -*- # Last updated: <2017/04/24 12:36:06 +0900> u""" PySDL2 のテスト. ウインドウを表示して画像(スプライト)を表示 sdl2.ext は使わず、sdl2 を使って処理をしてみる。 ソフトウェア的に画像転送してるので処理としては遅いらしい。 png画像の読み込みには sdl2.sdlimage が利用できるらしい? 動作確認環境: Windows10 x64 + Python 2.7.13 32bit + PySDL2 0.9.5 Hello World — PySDL2 0.9.5 documentation http://pysdl2.readthedocs.io/en/latest/tutorial/helloworld.html """ import sdl2 import sdl2.sdlimage import ctypes import os # PySDL2 の初期化 sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO) # ウインドウを作成して表示。 # タイトル文字列、表示位置、ウインドウサイズを指定している window = sdl2.SDL_CreateWindow(b"Hello World", sdl2.SDL_WINDOWPOS_CENTERED, sdl2.SDL_WINDOWPOS_CENTERED, 640, 480, sdl2.SDL_WINDOW_SHOWN) # ウインドウのサーフェイスを取得 windowsurface = sdl2.SDL_GetWindowSurface(window) # 画像ファイルのパスを取得 filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), "res", "hello.png") # 画像を読み込み # image = sdl2.SDL_LoadBMP(filepath.encode("utf-8")) image = sdl2.sdlimage.IMG_Load(filepath.encode("utf-8")) # 画像サイズを取得してみる # ctypes を利用しているため、 # image には SDL_Surface ではなく LP_SDL_Surface が入ってる # 故に image.w では取得できないが image.contents.w にすれば取得できる w = image.contents.w h = image.contents.h print("w, h = %d, %d" % (w, h)) # 画像の転送先領域を指定 x = 24 y = 36 w = image.contents.w h = image.contents.h dst_rect = sdl2.rect.SDL_Rect(x, y, w, h) # 画像を、ウインドウサーフェイスに転送 sdl2.SDL_BlitSurface(image, None, windowsurface, dst_rect) # ウインドウサーフェイスを更新 sdl2.SDL_UpdateWindowSurface(window) # RGBサーフェイスを解放 sdl2.SDL_FreeSurface(image) # イベントループ。閉じるボタンが押されるまで待つ(ループする) event = sdl2.SDL_Event() running = True 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()