import sdl2
import sdl2.ext

def run():
    sdl2.ext.init()
    window = sdl2.ext.Window("SDL2 Texture Example", size=(640, 480))
    renderer = sdl2.ext.Renderer(window)
    target_surface = sdl2.SDL_CreateRGBSurface(0, 640, 480, 32, 0, 0, 0, 0)
    # Crea texture target usando il metodo del renderer
    target_tex = sdl2.ext.Texture(renderer, surface=target_surface)

    # Disegna sulla texture
    sdl2.SDL_SetRenderTarget(renderer.renderer, target_tex.tx)
    renderer.clear()
    square_rect = sdl2.SDL_Rect(270, 190, 100, 100)
    renderer.draw_rect([square_rect], sdl2.ext.Color(255, 0, 0))
    renderer.present()
    sdl2.SDL_SetRenderTarget(renderer.renderer, None)
    # Main loop
    running = True
    while running:
        for event in sdl2.ext.get_events():
            if event.type == sdl2.SDL_QUIT:
                running = False
                break
        
        renderer.clear()
        renderer.copy(target_tex)  # Ora usa l'oggetto Texture corretto
        renderer.present()
        sdl2.SDL_Delay(10)

    sdl2.ext.quit()

if __name__ == "__main__":
    run()
