comparison pytouhou/lib/sdl.pyx @ 786:7e940ebeb5fd

Replace SDL2_image with the image crate
author Link Mauve <linkmauve@linkmauve.fr>
date Mon, 01 Dec 2025 17:05:48 +0100
parents f73e8524c045
children
comparison
equal deleted inserted replaced
785:f73e8524c045 786:7e940ebeb5fd
69 global keyboard_state 69 global keyboard_state
70 70
71 IF UNAME_SYSNAME == "Windows": 71 IF UNAME_SYSNAME == "Windows":
72 SDL_SetMainReady() 72 SDL_SetMainReady()
73 init(SDL_INIT_VIDEO if self.video else 0) 73 init(SDL_INIT_VIDEO if self.video else 0)
74 img_init(IMG_INIT_PNG)
75 ttf_init() 74 ttf_init()
76 75
77 keyboard_state = SDL_GetKeyboardState(NULL) 76 keyboard_state = SDL_GetKeyboardState(NULL)
78 77
79 def __exit__(self, *args): 78 def __exit__(self, *args):
80 TTF_Quit() 79 TTF_Quit()
81 IMG_Quit()
82 SDL_Quit() 80 SDL_Quit()
83 81
84 82
85 cdef class Window(gui.Window): 83 cdef class Window(gui.Window):
86 def __init__(self, str title, int x, int y, int w, int h, Uint32 flags): 84 def __init__(self, str title, int x, int y, int w, int h, Uint32 flags):
237 self.color.b = b 235 self.color.b = b
238 self.color.a = a 236 self.color.a = a
239 237
240 238
241 cdef class Surface: 239 cdef class Surface:
240 def __init__(self, bytes data=None):
241 self.data = data
242
242 def __dealloc__(self): 243 def __dealloc__(self):
243 if self.surface != NULL: 244 if self.surface != NULL:
244 SDL_FreeSurface(self.surface) 245 SDL_FreeSurface(self.surface)
245 246
246 property pixels: 247 property pixels:
286 cdef bint init(Uint32 flags) except True: 287 cdef bint init(Uint32 flags) except True:
287 if SDL_Init(flags) < 0: 288 if SDL_Init(flags) < 0:
288 raise SDLError() 289 raise SDLError()
289 290
290 291
291 cdef bint img_init(int flags) except True:
292 if IMG_Init(flags) != flags:
293 raise SDLError()
294
295
296 cdef bint ttf_init() except True: 292 cdef bint ttf_init() except True:
297 if TTF_Init() < 0: 293 if TTF_Init() < 0:
298 raise SDLError() 294 raise SDLError()
299 295
300 296
301 cdef bint gl_set_attribute(SDL_GLattr attr, int value) except True: 297 cdef bint gl_set_attribute(SDL_GLattr attr, int value) except True:
302 if SDL_GL_SetAttribute(attr, value) < 0: 298 if SDL_GL_SetAttribute(attr, value) < 0:
303 raise SDLError() 299 raise SDLError()
304 300
305 301
306 cdef Surface load_png(file_): 302 cdef Surface create_rgba_surface(bytes data, int width, int height):
307 data = file_.read() 303 surface = Surface(data)
308 rwops = SDL_RWFromConstMem(<char*>data, len(data)) 304 cdef char *pixels = <char*>data
309 surface = Surface() 305 depth = 32
310 surface.surface = IMG_LoadPNG_RW(rwops) 306 pitch = width * 4
311 SDL_RWclose(rwops) 307 fmt = SDL_PIXELFORMAT_ABGR8888
312 if surface.surface == NULL: 308 surface.surface = SDL_CreateRGBSurfaceWithFormatFrom(pixels, width, height, depth, pitch, fmt)
313 raise SDLError()
314 return surface
315
316
317 cdef Surface create_rgb_surface(int width, int height, int depth, Uint32 rmask=0, Uint32 gmask=0, Uint32 bmask=0, Uint32 amask=0):
318 surface = Surface()
319 surface.surface = SDL_CreateRGBSurface(0, width, height, depth, rmask, gmask, bmask, amask)
320 if surface.surface == NULL: 309 if surface.surface == NULL:
321 raise SDLError() 310 raise SDLError()
322 return surface 311 return surface
323 312
324 313