예제 #1
0
def main():
    sdl2.sdl2_load(ctypes.util.find_library('SDL2')) # '/usr/local/lib/libSDL2.dylib'
    sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)

    fpsdelay = 100
    count = 0
    event = sdl2.SDL_Event()
    done = False
    while not done:
        while sdl2.SDL_PollEvent(ctypes.byref(event)) != 0:
            # 'type' and 'timestamp' are common members for all SDL Event structs.
            event_type = event.common.type
            event_timestamp = event.common.timestamp
            print("Event : type=0x%s, timestamp=%s" % (event_type, event_timestamp) )

            if event_type == sdl2.SDL_KEYDOWN:
                if event.key.keysym.sym == sdl2.SDLK_ESCAPE:
                    done = True

        count += 1
        if count >= 100:
            done = True
        sdl2.SDL_Delay(fpsdelay)

    sdl2.SDL_Quit()
예제 #2
0
def main():
    sdl2.sdl2_load(
        ctypes.util.find_library('SDL2'))  # '/usr/local/lib/libSDL2.dylib'

    sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)

    WINDOW_W = 320
    WINDOW_H = 240
    window = sdl2.SDL_CreateWindow(b"1st SDL Window via Python-SDL2", 0, 0,
                                   WINDOW_W, WINDOW_H, 0)

    fpsdelay = 100
    count = 0
    event = sdl2.SDL_Event()
    done = False
    while not done:
        while sdl2.SDL_PollEvent(ctypes.byref(event)) != 0:
            # 'type' and 'timestamp' are common members for all SDL Event structs.
            event_type = event.common.type
            event_timestamp = event.common.timestamp
            print("Event : type=0x%s, timestamp=%s" %
                  (event_type, event_timestamp))

            if event_type == sdl2.SDL_KEYDOWN:
                if event.key.keysym.sym == sdl2.SDLK_ESCAPE:
                    print("\tSPACE key pressed.")

        count += 1
        if count >= 100:
            done = True
        sdl2.SDL_Delay(fpsdelay)

    sdl2.SDL_Quit()
예제 #3
0
def main():
    sdl2.sdl2_load(
        ctypes.util.find_library('SDL2'))  # '/usr/local/lib/libSDL2.dylib'

    sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)
    # sdl2.SDL_SetMainReady()

    version = sdl2.SDL_version()
    sdl2.SDL_GetVersion(ctypes.byref(version))
    print('Major, Minor and Patch: ', version.major, version.minor,
          version.patch)
    print('Revision and its Number: ', sdl2.SDL_GetRevision(),
          sdl2.SDL_GetRevisionNumber())

    print('Base Path: ', sdl2.SDL_GetBasePath())
    print('Touch Devices: ', sdl2.SDL_GetNumTouchDevices())
    print('Power State: ', sdl2.SDL_GetPowerInfo(None, None))

    print('Have AVX: ', sdl2.SDL_HasAVX())
    print('System RAM: ', sdl2.SDL_GetSystemRAM())

    print('Platform: ', sdl2.SDL_GetPlatform())

    print('Pixel Format: ',
          sdl2.SDL_GetPixelFormatName(sdl2.SDL_PIXELFORMAT_RGBA32))

    # Test features added in 2.0.10
    # Ref.: https://discourse.libsdl.org/t/sdl-2-0-10-released/26429
    print('Touch Device (0) Type: ',
          sdl2.SDL_GetTouchDeviceType(sdl2.SDL_GetTouchDevice(0)))
    print('SIMD Alignment: ', sdl2.SDL_SIMDGetAlignment())

    sdl2.SDL_Quit()
예제 #4
0
def main():
    sdl2.sdl2_load(ctypes.util.find_library('SDL2'), # '/usr/local/lib/libSDL2.dylib'
                   ttf_libpath = ctypes.util.find_library('SDL2_ttf'),
                   img_libpath = ctypes.util.find_library('SDL2_image')
                   )
    sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)

    WINDOW_W = 640
    WINDOW_H = 360
    window = sdl2.SDL_CreateWindow(b"Minimal SDL_Image Test via python-sdl2", 0, 0, WINDOW_W, WINDOW_H, sdl2.SDL_WINDOW_OPENGL)

    renderer = sdl2.SDL_CreateRenderer(window, -1, 0)

    sdl2.IMG_Init(sdl2.IMG_INIT_JPG|sdl2.IMG_INIT_PNG|sdl2.IMG_INIT_TIF|sdl2.IMG_INIT_WEBP)

    rwops_ptr = sdl2.SDL_RWFromFile(bytes(sys.argv[1], 'utf-8'), b"rb")

    print("PNG?: %s" % ('true' if sdl2.IMG_isPNG(rwops_ptr) else 'false'))

    texture = sdl2.IMG_LoadTexture_RW(renderer, rwops_ptr, 1)

    wh = 300
    pos = sdl2.SDL_Rect()
    pos.x = int((WINDOW_W - wh) / 2)
    pos.y = int((WINDOW_H - wh) / 2)
    pos.w = wh
    pos.h = wh

    sdl2.SDL_RenderCopy(renderer, texture, None, ctypes.byref(pos))

    sdl2.SDL_RenderPresent(renderer)

    fpsdelay = 100
    count = 0
    event = sdl2.SDL_Event()
    done = False
    while not done:
        while sdl2.SDL_PollEvent(ctypes.byref(event)) != 0:
            # 'type' and 'timestamp' are common members for all SDL Event structs.
            event_type = event.common.type
            event_timestamp = event.common.timestamp
            # print("Event : type=0x%s, timestamp=%s" % (event_type, event_timestamp) )

            if event_type == sdl2.SDL_KEYDOWN:
                if event.key.keysym.sym == sdl2.SDLK_ESCAPE:
                    done = True

        sdl2.SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF)
        sdl2.SDL_RenderClear(renderer)
        sdl2.SDL_RenderCopy(renderer, texture, None, ctypes.byref(pos))
        sdl2.SDL_RenderPresent(renderer)

        sdl2.SDL_Delay(fpsdelay)

    sdl2.IMG_Quit()
    sdl2.SDL_DestroyRenderer(renderer)
    sdl2.SDL_DestroyWindow(window)
    sdl2.SDL_Quit()
예제 #5
0
def main():
    sdl2.sdl2_load(ctypes.util.find_library('SDL2')) # '/usr/local/lib/libSDL2.dylib'

    sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)

    print(sdl2.SDL_HasClipboardText())
    print(sdl2.SDL_GetClipboardText())
    print(sdl2.SDL_SetClipboardText(b"Hello, world."))
    print(sdl2.SDL_HasClipboardText())
    print(sdl2.SDL_GetClipboardText())

    sdl2.SDL_Quit()
예제 #6
0
def main():
    sdl2.sdl2_load(
        ctypes.util.find_library('SDL2'))  # '/usr/local/lib/libSDL2.dylib'

    sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)
    # sdl2.SDL_SetMainReady()

    #rwops_ptr = sdl2.SDL_AllocRW()
    rwops_ptr = sdl2.SDL_RWFromFile(b'test_timer.py', b'rb')
    rwops = ctypes.cast(rwops_ptr, ctypes.POINTER(sdl2.SDL_RWops)).contents
    size_func = ctypes.CFUNCTYPE(ctypes.c_int64, ctypes.c_void_p)(rwops.size)
    print(sdl2.SDL_ReadU8(rwops_ptr))
    print(size_func(rwops_ptr))
    sdl2.SDL_FreeRW(rwops_ptr)

    sdl2.SDL_Quit()
예제 #7
0
def main():
    sdl2.sdl2_load(
        ctypes.util.find_library('SDL2'))  # '/usr/local/lib/libSDL2.dylib'
    sdl2.SDL_Init(sdl2.SDL_INIT_AUDIO)

    n = sdl2.SDL_GetNumAudioDrivers()
    if n == 0:
        print("No built-in audio drivers")
    else:
        print("Built-in audio drivers:")

    for i in range(0, n):
        print("  %s" % sdl2.SDL_GetAudioDriver(i))

    print("Using audio driver: %s" % sdl2.SDL_GetCurrentAudioDriver())

    print_devices(0)
    print_devices(1)

    sdl2.SDL_Quit()
예제 #8
0
def main():
    sdl2.sdl2_load(
        ctypes.util.find_library('SDL2'))  # '/usr/local/lib/libSDL2.dylib'

    sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO)

    WINDOW_W = 640
    WINDOW_H = 360
    window = sdl2.SDL_CreateWindow(
        b"RenderDrawPoint/RenderDrawLine/RenderFillRect", 0, 0, WINDOW_W,
        WINDOW_H, sdl2.SDL_WINDOW_OPENGL)

    renderer = sdl2.SDL_CreateRenderer(window, -1, 0)

    event = sdl2.SDL_Event()

    done = False
    while not done:
        while sdl2.SDL_PollEvent(ctypes.byref(event)) != 0:
            # 'type' and 'timestamp' are common members for all SDL Event structs.
            event_type = event.common.type
            event_timestamp = event.common.timestamp
            print("Event : type=0x%s, timestamp=%s" %
                  (event_type, event_timestamp))

            if event_type == sdl2.SDL_KEYDOWN:
                if event.key.keysym.sym == sdl2.SDLK_ESCAPE:
                    done = True

        sdl2.SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF)
        sdl2.SDL_RenderClear(renderer)

        draw_points(renderer)
        draw_lines(renderer)
        draw_rects(renderer)

        sdl2.SDL_RenderPresent(renderer)

    sdl2.SDL_Quit()
예제 #9
0
def main():
    sdl2.sdl2_load(ctypes.util.find_library('SDL2')) # '/usr/local/lib/libSDL2.dylib'

    sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)

    # SDL_ShowSimpleMessageBox
    success = sdl2.SDL_ShowSimpleMessageBox(sdl2.SDL_MESSAGEBOX_ERROR,
                                            b"Simple MessageBox",
                                            'これが SDL_ShowSimpleMessageBox です。'.encode('utf8'),
                                            None)

    # SDL_ShowMessageBox
    button = (sdl2.SDL_MessageBoxButtonData * 2)() # _main__.SDL_MessageBoxButtonData_Array_2

    button[0].flags = sdl2.SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT
    button[0].buttonid = 0
    button[0].text = b"OK"

    button[1].flags = sdl2.SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT
    button[1].buttonid = 1
    button[1].text = b"Cancel"

    data = sdl2.SDL_MessageBoxData()
    data.flags = sdl2.SDL_MESSAGEBOX_INFORMATION
    data.window = None
    data.title = b"Custom MessageBox"
    data.message = "カスタマイズされたメッセージボックスの利用例です。".encode('utf8')
    data.numbuttons = 2
    data.buttons = ctypes.cast(button, ctypes.c_void_p)
    data.colorScheme = None

    buttonid = ctypes.c_int()
    retval = sdl2.SDL_ShowMessageBox(ctypes.byref(data), ctypes.byref(buttonid))
    print(retval, buttonid)

    sdl2.SDL_Quit()
예제 #10
0
def main():
    sdl2.sdl2_load(
        ctypes.util.find_library('SDL2'),  # '/usr/local/lib/libSDL2.dylib'
        ttf_libpath=ctypes.util.find_library('SDL2_ttf'))
    sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)

    WINDOW_W = 640
    WINDOW_H = 360
    window = sdl2.SDL_CreateWindow(b"Minimal SDL_TTF Test via python-sdl2", 0,
                                   0, WINDOW_W, WINDOW_H,
                                   sdl2.SDL_WINDOW_OPENGL)

    renderer = sdl2.SDL_CreateRenderer(window, -1, 0)

    rect = sdl2.SDL_Rect()
    rect.x = 0
    rect.y = 0
    rect.w = WINDOW_W
    rect.h = WINDOW_H

    sdl2.TTF_Init()
    rwops_ptr = sdl2.SDL_RWFromFile(
        bytes(sys.argv[1], 'utf-8'), b"rb"
    )  # rwops = ctypes.cast(rwops_ptr, ctypes.POINTER(sdl2.SDL_RWops)).contents
    font = sdl2.TTF_OpenFontRW(rwops_ptr, 0, 42)

    fg = sdl2.SDL_Color()
    fg.r = 96
    fg.g = 96
    fg.b = 96
    fg.a = 255

    bg = sdl2.SDL_Color()
    bg.r = 0xE0
    bg.g = 0xE0
    bg.b = 0xE0
    bg.a = 255

    pos = [
        sdl2.SDL_Rect(),
        sdl2.SDL_Rect(),
    ]
    pos[0].x = 20
    pos[0].y = 120
    pos[0].w = 600
    pos[0].h = 60

    pos[1].x = 20
    pos[1].y = 180
    pos[1].w = 600
    pos[1].h = 60

    surfaces = [
        sdl2.TTF_RenderUTF8_Shaded(font, bytes("志於道、據於徳、依於仁、游於藝", 'utf-8'), fg,
                                   bg),
        sdl2.TTF_RenderUTF8_Shaded(font, bytes("SCORE 123456780", 'utf-8'), fg,
                                   bg),
    ]

    texture = [
        sdl2.SDL_CreateTextureFromSurface(renderer, surfaces[0]),
        sdl2.SDL_CreateTextureFromSurface(renderer, surfaces[1]),
    ]

    sdl2.SDL_SetRenderDrawColor(renderer, bg.r, bg.g, bg.b, bg.a)
    sdl2.SDL_RenderFillRect(renderer, ctypes.byref(rect))
    sdl2.SDL_RenderCopy(renderer, texture[0], None, ctypes.byref(pos[0]))
    sdl2.SDL_RenderCopy(renderer, texture[1], None, ctypes.byref(pos[1]))

    sdl2.SDL_RenderPresent(renderer)

    sdl2.SDL_FreeSurface(surfaces[0])
    sdl2.SDL_FreeSurface(surfaces[1])

    fpsdelay = 100
    count = 0
    event = sdl2.SDL_Event()
    done = False
    while not done:
        while sdl2.SDL_PollEvent(ctypes.byref(event)) != 0:
            # 'type' and 'timestamp' are common members for all SDL Event structs.
            event_type = event.common.type
            event_timestamp = event.common.timestamp
            # print("Event : type=0x%s, timestamp=%s" % (event_type, event_timestamp) )

            if event_type == sdl2.SDL_KEYDOWN:
                if event.key.keysym.sym == sdl2.SDLK_ESCAPE:
                    done = True

        sdl2.SDL_Delay(fpsdelay)

    sdl2.TTF_Quit()
    sdl2.SDL_DestroyRenderer(renderer)
    sdl2.SDL_DestroyWindow(window)
    sdl2.SDL_Quit()
예제 #11
0
def main():

    nodes = None
    edges = None

    if len(sys.argv) >= 2 and Path(sys.argv[1]).exists():
        map_file = Path(sys.argv[1])
        with map_file.open() as roadmap_input:
            nodes, edges = read_map(roadmap_input)
            # print(nodes, edges)
    else:
        nodes = [
            Vector(100.0, 100.0),
            Vector(200.0, 200.0),
            Vector(300.0, 100.0),
            Vector(400, 200)
        ]
        edges = [[0, 1], [1, 2], [2, 0], [1, 3], [2, 3]]

    wps = []
    for n in nodes:
        wps.append(WayPoint(n.x, n.y))

    for e in edges:
        edge = WayPointEdge(wps[e[0]], wps[e[1]])
        wps[e[0]].neighbors.append(edge)
        wps[e[1]].neighbors.append(edge)
        # print(Vector.distance(wps[e[0]].pos, wps[e[1]].pos))

    rp = RoutePlanner(wps)
    start = Vector(0.0, 0.0)
    goal = Vector(500.0, 200.0)
    route_plan = rp.plan(start, goal)

    sdl2.sdl2_load(
        ctypes.util.find_library('SDL2'),  # '/usr/local/lib/libSDL2.dylib'
        gfx_libpath=ctypes.util.find_library('SDL2_gfx'))
    sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)

    window = sdl2.SDL_CreateWindow(b"A* demonstration", 0, 0, WINDOW_W,
                                   WINDOW_H, sdl2.SDL_WINDOW_OPENGL)

    renderer = sdl2.SDL_CreateRenderer(window, -1, 0)

    mouse_x = ctypes.c_int()
    mouse_y = ctypes.c_int()

    fps_delay = 100
    event = sdl2.SDL_Event()
    done = False
    while not done:
        while sdl2.SDL_PollEvent(ctypes.byref(event)) != 0:
            # 'type' and 'timestamp' are common members for all SDL Event structs.
            event_type = event.common.type
            # event_timestamp = event.common.timestamp
            # print("Event : type=0x%s, timestamp=%s" % (event_type, event_timestamp) )

            if event_type == sdl2.SDL_KEYDOWN:
                if event.key.keysym.sym == sdl2.SDLK_ESCAPE:
                    done = True
                if event.key.keysym.sym == sdl2.SDLK_SPACE:
                    route_plan = rp.plan(start, goal)

        sdl2.SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF)
        sdl2.SDL_RenderClear(renderer)

        mouse_state = sdl2.SDL_GetMouseState(ctypes.byref(mouse_x),
                                             ctypes.byref(mouse_y))
        if mouse_state == (1 << (sdl2.SDL_BUTTON_LEFT - 1)):
            prev_x = start.x
            prev_y = start.y
            start.x = float(mouse_x.value)
            start.y = float(mouse_y.value)
            if start.x != prev_x or start.y != prev_y:
                route_plan = rp.plan(start, goal)
        elif mouse_state == (1 << (sdl2.SDL_BUTTON_RIGHT - 1)):
            prev_x = goal.x
            prev_y = goal.y
            goal.x = float(mouse_x.value)
            goal.y = float(mouse_y.value)
            if goal.x != prev_x or goal.y != prev_y:
                route_plan = rp.plan(start, goal)

        map_renderer.render_waypoints(renderer, wps)
        map_renderer.render_route_plan(renderer, start, goal, route_plan)
        sdl2.SDL_RenderPresent(renderer)

        sdl2.SDL_Delay(fps_delay)

    sdl2.SDL_DestroyRenderer(renderer)
    sdl2.SDL_DestroyWindow(window)
    sdl2.SDL_Quit()
예제 #12
0
def main():
    sdl2.sdl2_load(
        ctypes.util.find_library('SDL2'),  # '/usr/local/lib/libSDL2.dylib'
        ttf_libpath=ctypes.util.find_library('SDL2_ttf'),
        img_libpath=ctypes.util.find_library('SDL2_image'),
        gfx_libpath=ctypes.util.find_library('SDL2_gfx'))
    sdl2.SDL_Init(sdl2.SDL_INIT_EVERYTHING)

    WINDOW_W = 640
    WINDOW_H = 360
    window = sdl2.SDL_CreateWindow(b"Minimal SDL2_gfx Test via python-sdl2", 0,
                                   0, WINDOW_W, WINDOW_H,
                                   sdl2.SDL_WINDOW_OPENGL)

    renderer = sdl2.SDL_CreateRenderer(window, -1, 0)

    fpsdelay = 100
    count = 0
    event = sdl2.SDL_Event()
    done = False
    while not done:
        while sdl2.SDL_PollEvent(ctypes.byref(event)) != 0:
            # 'type' and 'timestamp' are common members for all SDL Event structs.
            event_type = event.common.type
            # event_timestamp = event.common.timestamp
            # print("Event : type=0x%s, timestamp=%s" % (event_type, event_timestamp) )

            if event_type == sdl2.SDL_KEYDOWN:
                if event.key.keysym.sym == sdl2.SDLK_ESCAPE:
                    done = True

        sdl2.SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF)
        sdl2.SDL_RenderClear(renderer)

        sdl2.pixelColor(renderer, 100, 100,
                        0xFFFFFFFF)  # Uint32 color = 0x[AA][BB][GG][RR]
        sdl2.pixelRGBA(renderer, 101, 100, 0xFF, 0x00, 0x00, 0xFF)

        sdl2.hlineColor(renderer, 0, 100, 50, 0xFFFFFFFF)
        sdl2.vlineColor(renderer, 50, 0, 100, 0xFFFF00FF)

        sdl2.rectangleColor(renderer, 5, 5, 95, 95, 0xFF00FFFF)
        sdl2.rectangleRGBA(renderer, 10, 10, 90, 90, 0, 0, 0xFF, 0xFF)

        sdl2.circleColor(renderer, 150, 150, 50, 0xFF00FF00)
        sdl2.filledCircleRGBA(renderer, 150, 150, 45, 0x00, 0xFF, 0x00, 0xFF)

        sdl2.aalineColor(renderer, 200, 200, 300, 300, 0xFFFFFFFF)
        sdl2.aalineColor(renderer, 300, 200, 200, 300, 0xFFFFFFFF)

        sdl2.aacircleColor(renderer, 300, 200, 25, 0xFF00FF00)
        sdl2.pieColor(renderer, 200, 300, 25, 0, 270, 0xFF00FF00)

        sdl2.SDL_RenderPresent(renderer)

        sdl2.SDL_Delay(fpsdelay)

    sdl2.IMG_Quit()
    sdl2.SDL_DestroyRenderer(renderer)
    sdl2.SDL_DestroyWindow(window)
    sdl2.SDL_Quit()