示例#1
0
def DrawChessBoard(renderer):
    row = 0
    coloum = 0
    x = 0
    rect = ffi.new("SDL_Rect *")
    darea = ffi.new("SDL_Rect *")

    # Get the Size of drawing surface
    lib.SDL_RenderGetViewport(renderer, darea)

    for row in range(8):
        coloum = row % 2
        x = x + coloum

        for coloum in range(coloum, 4 + (row % 2)):

            lib.SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0xFF)

            rect.w = darea.w // 8
            rect.h = darea.h // 8
            rect.x = x * rect.w
            rect.y = row * rect.h
            x = x + 2
            lib.SDL_RenderFillRect(renderer, rect)

        x = 0
示例#2
0
 def __init__(self, data=None, ffi=ffi):
     if isinstance(data, ffi.CData):
         self.cdata = data
     elif isinstance(data, Struct):
         self.cdata = data.cdata
     else:
         self.cdata = ffi.new("%s *" % (self.__class__.__ctype__), data)
示例#3
0
def joystickGetGUIDString(a0):
    """
    ``void SDL_JoystickGetGUIDString(SDL_JoystickGUID, char *, int)``

    Return a string representation for this guid. pszGUID must point to at
    least 33 bytes (32 for the string plus a NULL terminator).
    """
    buf = ffi.new('char *', 33)
    lib.SDL_JoystickGetGUIDString(unbox(a0), buf, 33)
    return ffi.string(buffer)
示例#4
0
def calculateGammaRamp(a0, a1=ffi.NULL):
    """void SDL_CalculateGammaRamp(float, uint16_t *)

    :param gamma: a gamma value where 0.0 is black and 1.0 is identity
    :param ramp: an array of 256 values filled in with the gamma ramp
    :return: ramp
    """
    if a1 == ffi.NULL:
        a1 = ffi.new("uint16_t[]", 256)
    lib.SDL_CalculateGammaRamp(a0, a1)
    return a1
示例#5
0
def unbox(data, c_type=None, ffi=ffi, nullable=False):
    """
    Try to return something to pass to low-level ffi calls.
    For a cffi type, return data.
    For a Struct, return the wrapper's cdata.
    Else try to use data as a ffi initializer for c_type.
    """
    if not isinstance(data, ffi.CData):
        try:
            return data.cdata
        except AttributeError:
            if data is None and nullable:
                return ffi.NULL
            if c_type:
                return ffi.new(c_type, data)
    return data
示例#6
0
def main():
    # Enable standard application logging
    lib.SDL_LogSetPriority(lib.SDL_LOG_CATEGORY_APPLICATION,
                           lib.SDL_LOG_PRIORITY_INFO)

    # Initialize SDL
    if lib.SDL_Init(lib.SDL_INIT_VIDEO) != 0:
        lib.SDL_LogError(lib.SDL_LOG_CATEGORY_APPLICATION,
                         "lib.SDL_Init fail : %s\n" % (lib.SDL_GetError()))
        return 1

    # Create window and renderer for given surface
    window = lib.SDL_CreateWindow(b"Chess Board", lib.SDL_WINDOWPOS_UNDEFINED,
                                  lib.SDL_WINDOWPOS_UNDEFINED, 640, 640,
                                  lib.SDL_WINDOW_SHOWN)
    if not window:
        lib.SDL_LogError(lib.SDL_LOG_CATEGORY_APPLICATION,
                         "Window creation fail : %s\n" % (lib.SDL_GetError()))
        return 1
    surface = lib.SDL_GetWindowSurface(window)
    renderer = lib.SDL_CreateSoftwareRenderer(surface)
    if not renderer:
        lib.SDL_LogError(
            lib.SDL_LOG_CATEGORY_APPLICATION,
            "Render creation for surface fail : %s\n" % (lib.SDL_GetError()))
        return 1

    # Clear the rendering surface with the specified color
    lib.SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF)
    lib.SDL_RenderClear(renderer)

    # Draw the Image on rendering surface
    e = ffi.new("SDL_Event *")
    while True:
        if lib.SDL_PollEvent(e):
            if e.type == lib.SDL_QUIT:
                break

            if e.key.keysym.sym == lib.SDLK_ESCAPE:
                break

        DrawChessBoard(renderer)

        # Got everything on rendering surface,
        # now Update the drawing image on window screen
        lib.SDL_UpdateWindowSurface(window)
示例#7
0
def main():
    audio_format = None
    audio_volume = __sdl_mixer.lib.MIX_MAX_VOLUME
    looping = 0
    interactive = 0
    rwops = 0

    # Initialize variables
    audio_rate = 22050 * 2
    audio_format = lib.AUDIO_S16
    audio_channels = 2
    audio_buffers = 4096

    # Initialize the SDL library
    if SDL_Init(SDL_INIT_AUDIO) < 0:
        sys.stderr.write("Couldn't initialize SDL: %s\n" % (SDL_GetError()))
        return 255

    # Open the audio device
    if mix.Mix_OpenAudio(audio_rate, audio_format, audio_channels,
                         audio_buffers) < 0:
        sys.stderr.write("Couldn't open audio: %s\n" % (SDL_GetError()))
        return (2)
    else:
        audio_rate = ffi.new("int *")
        audio_format = ffi.new("uint16_t *")
        audio_channels = ffi.new("int *")
        mix.Mix_QuerySpec(audio_rate, audio_format, audio_channels)
        sys.stdout.write(
            "Opened audio at %d Hz %d bit %s (%s), %d bytes audio buffer\n" %
            (audio_rate[0], (audio_format[0] & 0xFF), "surround" if
             (audio_channels[0] > 2) else "stereo" if
             (audio_channels[0] > 1) else "mono", "BE" if
             (audio_format[0] & 0x1000) else "LE", audio_buffers))
    audio_open = 1

    # Set the music volume
    mix.Mix_VolumeMusic(audio_volume)

    # Set the external music player, if any
    if os.getenv("MUSIC_CMD"):
        mix.Mix_SetMusicCMD(os.getenv("MUSIC_CMD"))

    next_track = 0

    i = 1

    filename = sys.argv[i].encode('utf-8')

    # Load the requested music file
    if rwops:
        music = mix.Mix_LoadMUS_RW(SDL_RWFromFile(filename, "rb"), SDL_TRUE)
    else:
        music = mix.Mix_LoadMUS(filename)
    if music == ffi.NULL:
        sys.stderr.write("Couldn't load %s: %s\n" %
                         (sys.argv[i], SDL_GetError()))
        CleanUp(2)

    # Play and then exit
    sys.stdout.write("Playing %s\n" % (sys.argv[i]))
    mix.Mix_FadeInMusic(music, looping, 2000)
    while not next_track and (mix.Mix_PlayingMusic() or mix.Mix_PausedMusic()):
        if interactive:
            Menu()
        else:
            SDL_Delay(100)
    mix.Mix_FreeMusic(music)
    music = ffi.NULL

    # If the user presses Ctrl-C more than once, exit.
    SDL_Delay(500)
    if next_track > 1: return

    i += 1

    CleanUp(0)

    # Not reached, but fixes compiler warnings
    return 0
示例#8
0
class wave():
    spec = ffi.new("SDL_AudioSpec *")
    sound_p = ffi.new("uint8_t **")
    soundlen_p = ffi.new("uint32_t *")
    soundpos = 0
    soundlen = 0