python - PySDL2 issue with SDL_Surface / LP_SDL_Surface -
im running win7 python 3.3 , pysdl2 0.5. when creating surfaces (no matter method) lp_sdl_surface instead of sdl_surface. lp_sdl_surface lacks of methods , attribute expect have. here issue using example code documentation:
import os os.environ["pysdl2_dll_path"] = os.path.dirname(os.path.abspath(__file__)) import sys import ctypes sdl2 import * def main(): sdl_init(sdl_init_video) window = sdl_createwindow(b"hello world", sdl_windowpos_centered, sdl_windowpos_centered, 592, 460, sdl_window_shown) windowsurface = sdl_getwindowsurface(window) image = sdl_loadbmp(b"exampleimage.bmp") sdl_blitsurface(image, none, windowsurface, none) print(image.h) sdl_updatewindowsurface(window) sdl_freesurface(image) running = true event = sdl_event() while running: while sdl_pollevent(ctypes.byref(event)) != 0: if event.type == sdl_quit: running = false break sdl_destroywindow(window) sdl_quit() return 0 if __name__ == "__main__": sys.exit(main())
and traceback is:
traceback (most recent call last): file "c:/.../test.py", line 35, in <module> sys.exit(main()) file "c:/.../test.py", line 17, in main print(image.h) attributeerror: 'lp_sdl_surface' object has no attribute 'h'
a google search "lp_sdl_surface" brings 0 (!) results.
if work lower level sdl methods (e.g. sdl2.sdl_loadbmp
) have deal ctypes conversions, referencing(byref
) , dereferencing of pointers (.contents
, .value
).
so specific question, you've commented, using print(image.contents.h)
enough.
there higher level classes , methods provided pysdl2 (sdl2.ext
), however, of conversions you, if desired. code below achieves same goal without having touch ctypes:
import os os.environ["pysdl2_dll_path"] = os.path.dirname(os.path.abspath(__file__)) import sys import sdl2 import sdl2.ext def main(): sdl2.ext.init() window = sdl2.ext.window( title="hello world!", size=(592, 460), flags=sdl2.sdl_window_shown, position=(sdl2.sdl_windowpos_centered, sdl2.sdl_windowpos_centered)) window.show() renderer = sdl2.ext.renderer(window) factory = sdl2.ext.spritefactory(sdl2.ext.texture, renderer=renderer) spriterenderer = factory.create_sprite_render_system(window) image = factory.from_image("exampleimage.bmp") print(image.size[1]) # image.size = (w, h) running = true while running: event in sdl2.ext.get_events(): if event.type == sdl2.sdl_quit: running = false break spriterenderer.render(image) sdl2.ext.quit() return 0 if __name__ == '__main__': sys.exit(main())
it makes use of texture rendering, using hardware acceleration, instead of surface blitting (software based).
finally, using higher level sdl2.ext
instantiate classes (instead of having write whole new sprite class yourself) sdl2.ext.sprite.texturesprite
, , implement h
property:
class texturesprite(sdl2.ext.texturesprite): @property def h(self): """the height of texturesprite.""" return self.size[1] @property def w(self): """the width of texturesprite.""" return self.size[0]
Comments
Post a Comment