Skip to content

Commit

Permalink
SDL2 --> SDL3 4th pass
Browse files Browse the repository at this point in the history
  • Loading branch information
misl6 committed Apr 6, 2024
1 parent 877ce44 commit 1f71b9b
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 31 deletions.
2 changes: 1 addition & 1 deletion examples/audio/pitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
class Test(App):
def build(self):
self.sound = SoundLoader.load(
'/usr/lib64/python{}.{}/test/audiodata/pluck-pcm32.wav'
'examples/audio/12908_sweet_trip_mm_clap_hi.wav'
.format(*version_info[0:2])
)
root = BoxLayout()
Expand Down
3 changes: 2 additions & 1 deletion kivy/core/audio/audio_sdl2.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -194,10 +194,11 @@ class SoundSDL2(Sound):
dst_spec.channels = src_spec.channels

if SDL_ConvertAudioSamples(&src_spec, cc.original_chunk.abuf, cc.original_chunk.alen, &dst_spec, &dst_data, &dst_len) < 0:
print("Error converting audio samples: %s" % SDL_GetError())
Logger.warning("SoundSDL2: Error converting audio samples: %s" % SDL_GetError())
return

cc.chunk = Mix_QuickLoad_RAW(dst_data, dst_len)
SDL_free(dst_data)

def play(self):
cdef ChunkContainer cc = self.cc
Expand Down
57 changes: 37 additions & 20 deletions kivy/core/image/_img_sdl2.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,39 @@ from libc.stdlib cimport malloc

cdef int _is_init = 0

"""
cdef size_t rwops_bytesio_write(SDL_IOStream *context, const void *ptr, size_t size, size_t num) noexcept:

cdef struct BytesIODataContainer:
void* data


cdef size_t rwops_bytesio_write(void *userdata, const void *ptr, size_t size, SDL_IOStatus *status) noexcept:
cdef char *c_string = <char *>ptr
byteio = <object>context.hidden.unknown.data1
byteio.write(c_string[:size * num])
return size * num
byteio = <object>(<BytesIODataContainer*>userdata).data
byteio.write(c_string[:size * 1])
return size * 1


cdef int rwops_bytesio_close(SDL_IOStream *context) noexcept:
byteio = <object>context.hidden.unknown.data1
cdef int rwops_bytesio_close(void *userdata) noexcept:
byteio = <object>(<BytesIODataContainer*>userdata).data
byteio.seek(0)


cdef SDL_IOStream *rwops_bridge_to_bytesio(byteio):
# works only for write.
cdef SDL_IOStream *rwops = SDL_AllocRW()
rwops.hidden.unknown.data1 = <void *>byteio
rwops.seek = NULL
rwops.read = NULL
rwops.write = &rwops_bytesio_write
rwops.close =&rwops_bytesio_close
cdef SDL_IOStreamInterface io_interface
cdef SDL_IOStream *rwops
cdef BytesIODataContainer *bytesiocontainer

# works only for write.
bytesiocontainer.data = <void *>byteio
io_interface.seek = NULL
io_interface.read = NULL
io_interface.write = &rwops_bytesio_write
io_interface.close = &rwops_bytesio_close

rwops = SDL_OpenIO(&io_interface, bytesiocontainer)

return rwops
"""


def init():
global _is_init
Expand Down Expand Up @@ -86,23 +96,30 @@ def save(filename, w, h, pixelfmt, pixels, flipped, imagefmt, quality=90):

cdef char *c_pixels = pixels
cdef SDL_Surface *image = NULL
cdef SDL_PixelFormatEnum fmt
cdef int depth

if pixelfmt == "rgba":
image = SDL_CreateSurfaceFrom(c_pixels, w, h, 32,
SDL_GetPixelFormatEnumForMasks(pitch, 0x00000000ff, 0x0000ff00, 0x00ff0000, 0xff000000)
fmt = SDL_GetPixelFormatEnumForMasks(
pitch, 0x00000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
)
depth = 32

elif pixelfmt == "rgb":
image = SDL_CreateSurfaceFrom(c_pixels, w, h, 24,
SDL_GetPixelFormatEnumForMasks(pitch, 0x0000ff, 0x00ff00, 0xff0000, 0)
fmt = SDL_GetPixelFormatEnumForMasks(
pitch, 0x0000FF, 0x00FF00, 0xFF0000, 0
)
depth = 24

image = SDL_CreateSurfaceFrom(c_pixels, w, h, depth, fmt)

if c_filename is not None:
if imagefmt == "png":
IMG_SavePNG(image, c_filename)
elif imagefmt == "jpg":
IMG_SaveJPG(image, c_filename, quality)
else:
# rwops = rwops_bridge_to_bytesio(filename)
rwops = rwops_bridge_to_bytesio(filename)
if imagefmt == "png":
IMG_SavePNG_IO(image, rwops, 1)
elif imagefmt == "jpg":
Expand Down
8 changes: 5 additions & 3 deletions kivy/core/text/_text_sdl2.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ cdef class _SurfaceContainer:
def __init__(self, w, h):
# XXX check on OSX to see if little endian/big endian make a difference
# here.
self.surface = SDL_CreateSurface(w, h,
self.surface = SDL_CreateSurface(
w,
h,
SDL_GetPixelFormatEnumForMasks(
32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
)
32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000
),
)

def __dealloc__(self):
Expand Down
25 changes: 19 additions & 6 deletions kivy/lib/sdl2.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -436,10 +436,25 @@ cdef extern from "SDL.h":
int refresh_rate
void *driverdata

cdef enum SDL_IOStatus:
SDL_IO_STATUS_READY
SDL_IO_STATUS_ERROR
SDL_IO_STATUS_EOF
SDL_IO_STATUS_NOT_READY
SDL_IO_STATUS_READONLY
SDL_IO_STATUS_WRITEONLY

cdef struct SDL_IOStreamInterface:
Sint64 (*size)(void *userdata)
Sint64 (*seek)(void *userdata, Sint64 offset, int whence)
size_t (*read)(void *userdata, void *ptr, size_t size, SDL_IOStatus *status)
size_t (*write)(void *userdata, const void *ptr, size_t size, SDL_IOStatus *status)
int (*close)(void *userdata)

cdef struct SDL_IOStream:
# SDL_IOStreamInterface iface;
SDL_IOStreamInterface iface;
void *userdata;
# SDL_IOStatus status;
SDL_IOStatus status;
# SDL_PropertiesID props;

cdef enum SDL_Keymod:
Expand Down Expand Up @@ -529,10 +544,8 @@ cdef extern from "SDL.h":
cdef Uint8 SDL_SetEventEnabled(Uint32 type, SDL_bool enabled)
cdef int SDL_PollEvent(SDL_Event * event) nogil
cdef void SDL_SetEventFilter(SDL_EventFilter *filter, void* userdata)
cdef SDL_IOStream * SDL_RWFromFile(char *file, char *mode)
cdef SDL_IOStream * SDL_IOFromMem(void *mem, int size)
cdef SDL_IOStream * SDL_RWFromConstMem(void *mem, int size)
cdef SDL_IOStream * SDL_AllocRW()
cdef SDL_IOStream * SDL_OpenIO(const SDL_IOStreamInterface *iface, void *userdata)
cdef void SDL_CloseIO(SDL_IOStream *area)
cdef int SDL_GetRendererInfo(SDL_Renderer *renderer, SDL_RendererInfo *info)
cdef int SDL_RenderSetViewport(SDL_Renderer * renderer, SDL_Rect * rect)
Expand Down Expand Up @@ -656,7 +669,7 @@ cdef extern from "SDL.h":
cdef void* SDL_Metal_GetLayer(SDL_MetalView view)
cdef void SDL_GetProperty(SDL_PropertiesID props, const char *name, void *default_value)
cdef SDL_PropertiesID SDL_GetWindowProperties(SDL_Window *window)

cdef void SDL_free(void *mem)

# Sound audio formats
Uint16 AUDIO_U8 #0x0008 /**< Unsigned 8-bit samples */
Expand Down

0 comments on commit 1f71b9b

Please sign in to comment.