Esempio n. 1
0
    def test_Window(self):
        flags = video.SDL_WINDOW_BORDERLESS
        sizes = ((1, 1), (10, 10), (10, 20), (200, 17), (640, 480), (800, 600))
        for w, h in sizes:
            window = sdl2ext.Window("Window", size=(w, h), flags=flags)
            assert window.size == (w, h)
            window.close()

        with pytest.raises(TypeError):
            sdl2ext.Window("Test", None, None, None)
        with pytest.raises(TypeError):
            sdl2ext.Window("Test", None, None)
        with pytest.raises(TypeError):
            sdl2ext.Window("Test", None)
    def test_SpriteFactory(self):
        factory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE)
        assert isinstance(factory, sdl2ext.SpriteFactory)
        assert factory.default_args == {}

        factory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE, bananas="tasty")
        assert isinstance(factory, sdl2ext.SpriteFactory)
        assert factory.default_args == {"bananas": "tasty"}

        window = sdl2ext.Window("Test", size=(1, 1))
        renderer = sdl2ext.Renderer(window)

        factory = sdl2ext.SpriteFactory(sdl2ext.TEXTURE, renderer=renderer)
        assert isinstance(factory, sdl2ext.SpriteFactory)

        factory = sdl2ext.SpriteFactory(sdl2ext.TEXTURE, renderer=renderer)
        assert isinstance(factory, sdl2ext.SpriteFactory)
        assert factory.default_args == {"renderer": renderer}

        with pytest.raises(ValueError):
            sdl2ext.SpriteFactory("Test")
        with pytest.raises(ValueError):
            sdl2ext.SpriteFactory(-456)
        with pytest.raises(ValueError):
            sdl2ext.SpriteFactory(123)
        with pytest.raises(ValueError):
            sdl2ext.SpriteFactory(sdl2ext.TEXTURE)
        dogc()
Esempio n. 3
0
def run():
    # init sdl and get window up
    sdl2ext.init()
    window = sdl2ext.Window("Traction Edge RL", size=(800, 600))
    window.show()

    #create hardware accelerated context for drawing on
    context = sdl2ext.RenderContext(window, index=-1, flags=SDL_RENDERER_ACCELERATED)
    #create our custom renderer with the context
    renderer = systems.Renderer(context)

    # init world
    world = sdl2ext.World()
    world.add_system(renderer)

    # create our sprites
    factory = sdl2ext.SpriteFactory(sprite_type=sdl2ext.TEXTURE, renderer=context)
    sp_player = factory.from_color(WHITE, size=(32,32))

    #create player
    player = entities.Creature(world, sp_player, 100, 400)


    #main loop
    running = True
    while running:
        events = sdl2ext.get_events()
        for event in events:
            if event.type == SDL_QUIT:
                running = False
                break
        SDL_Delay(10)
        world.process()
    return 
Esempio n. 4
0
def main():
    #cause I don't want to pass these around
    global WINDOW, REN, SPRITE_FACTORY, SPRITE_RENDERER, MUSIC

    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)

    WINDOW = ext.Window("Tetromino", size=(WINDOWWIDTH, WINDOWHEIGHT))
    REN = ext.Renderer(WINDOW)
    WINDOW.show()

    font_file = sysfont.get_font("freesans", sysfont.STYLE_BOLD)
    font_manager = ext.FontManager(font_file, size=18)

    #fontmanager=font_manager will be default_args passed to every sprite creation method
    SPRITE_FACTORY = ext.SpriteFactory(renderer=REN,
                                       fontmanager=font_manager,
                                       free=True)
    SPRITE_RENDERER = SPRITE_FACTORY.create_sprite_render_system(WINDOW)

    sdlmixer.Mix_Init(sdlmixer.MIX_INIT_OGG)
    sdlmixer.Mix_OpenAudio(22050, sdlmixer.MIX_DEFAULT_FORMAT, 2, 1024)
    #BEEP1 = sdlmixer.Mix_LoadWAV(b"beep1.ogg")

    showTextScreen("Tetromino")
    while True:
        if random.randint(0, 1) == 0:
            MUSIC = sdlmixer.Mix_LoadMUS(b"tetrisb.mid")
        else:
            MUSIC = sdlmixer.Mix_LoadMUS(b"tetrisc.mid")
        sdlmixer.Mix_PlayMusic(MUSIC, -1)
        runGame()
        sdlmixer.Mix_HaltMusic()
        showTextScreen("Game Over")
    def test_SoftwareSpriteRenderSystem(self):
        with pytest.raises(TypeError):
            sdl2ext.SoftwareSpriteRenderSystem()
        with pytest.raises(TypeError):
            sdl2ext.SoftwareSpriteRenderSystem(None)
        with pytest.raises(TypeError):
            sdl2ext.SoftwareSpriteRenderSystem("Test")
        with pytest.raises(TypeError):
            sdl2ext.SoftwareSpriteRenderSystem(12345)

        window = sdl2ext.Window("Test", size=(1, 1))
        renderer = sdl2ext.SoftwareSpriteRenderSystem(window)
        assert isinstance(renderer, sdl2ext.SpriteRenderSystem)
        assert renderer.window == window.window
        assert isinstance(renderer.surface, SDL_Surface)

        renderer = sdl2ext.SoftwareSpriteRenderSystem(window.window)
        assert isinstance(renderer, sdl2ext.SpriteRenderSystem)
        assert renderer.window == window.window
        assert isinstance(renderer.surface, SDL_Surface)

        assert renderer.sortfunc is not None
        assert not (sdl2ext.Sprite in renderer.componenttypes)
        assert sdl2ext.SoftwareSprite in renderer.componenttypes
        dogc()
Esempio n. 6
0
 def __init__(self,
              title,
              data_size,
              initial_width=None,
              num_colors=16,
              grid_px=1):
     self.__init()
     self.data_sx, self.data_sy = data_size
     if initial_width:
         scaling = initial_width / self.data_sx
     else:
         scaling = 800 / max(data_size)
     size = (int(self.data_sx * scaling), int(self.data_sy * scaling))
     self.title = title
     self.sdl_window = sdl2ext.Window(title,
                                      position=self._find_position(*size),
                                      size=size,
                                      flags=SDL_WINDOW_RESIZABLE)
     self.surface = self.sdl_window.get_surface()
     self.running = True
     self.scaling = scaling
     self.colors = {
         val: _calc_color(val / num_colors)
         for val in range(num_colors + 1)
     }
     self.num_colors = num_colors
     self.grid_px = grid_px
     self.last_data = np.zeros(data_size)
     self.draw_lock = Lock()
     self.offset_x = self.offset_y = 0
     self.windows[self] = self
     self.update_thread = None
Esempio n. 7
0
 def __init__(self, título, tamanho):
     self.título = título
     self.win = sdlx.Window(título, tamanho)
     self.win.show()
     self.time = time.perf_counter()
     self.tempoTotal = 0
     self.i = 0
Esempio n. 8
0
def run():
    # You know those from the helloworld.py example.
    # Initialize the video subsystem, create a window and make it visible.
    sdl2ext.init()
    TTF_Init()
    window = sdl2ext.Window("2D drawing primitives", size=(800, 600))
    window.show()

    # As in colorpalettes.py, explicitly acquire the window's surface to
    # draw on.
    windowsurface = window.get_surface()

    fps = FPS(windowsurface)

    # We implement the functionality as it was done in colorpalettes.py and
    # utilise a mapping table to look up the function to be executed, together
    # with the arguments they should receive
    functions = ((draw_lines, (windowsurface, 800, 600)),
                 (draw_rects, (windowsurface, 800, 600)))

    # A storage variable for the function we are currently on, so that we know
    # which function to execute next.
    curindex = 0
    draw_lines(windowsurface, 800, 600)

    textSurface = TTF_RenderText_Shaded(font,
                                        repr(SDL_GetPerformanceFrequency()),
                                        SDL_Color(255, 255, 255),
                                        SDL_Color(40, 90, 120))

    # The event loop is nearly the same as we used in colorpalettes.py. If you
    # do not know, what happens here, take a look at colorpalettes.py for a
    # detailled description.
    running = True
    while running:
        events = sdl2ext.get_events()
        for event in events:
            if event.type == SDL_QUIT:
                running = False
                break
            if event.type == SDL_MOUSEBUTTONDOWN:
                curindex += 1
                if curindex >= len(functions):
                    curindex = 0
                # In contrast to colorpalettes.py, our mapping table consists
                # of functions and their arguments. Thus, we get the currently
                # requested function and argument tuple and execute the
                # function with the arguments.
                func, args = functions[curindex]
                func(*args)
                break
        textSurface = TTF_RenderText_Shaded(
            font, "FPS: " + str(SDL_GetPerformanceFrequency()),
            SDL_Color(255, 255, 255), SDL_Color(40, 90, 120))
        SDL_BlitSurface(textSurface, None, windowsurface, None)
        window.refresh()
    TTF_Quit()
    sdl2ext.quit()
    return 0
Esempio n. 9
0
 def test_Window_position(self):
     window = sdl2ext.Window("Position",
                             size=(200, 200),
                             position=(100, 100))
     assert window.position == (100, 100)
     window.position = 70, 300
     assert window.position == (70, 300)
     window.close()
Esempio n. 10
0
def init(windowname='ZOMG FORGOT TO NAME IT', width=640, height=400):
    global renderer, window, window_width, window_height
    window_width = width
    window_height = height
    sdl.init()
    window = sdl.Window(windowname, size=(window_width, window_height))
    window.show()
    renderer = sdl.Renderer(window)
 def test_Window_position(self):
     window = sdl2ext.Window("Position",
                             size=(200, 200),
                             position=(100, 100))
     self.assertEqual(window.position, (100, 100))
     window.position = 70, 300
     self.assertEqual(window.position, (70, 300))
     window.close()
Esempio n. 12
0
def run():
    sdl2ext.init()
    window = sdl2ext.Window("The Pong Game", size=(800, 600))
    window.show()

    if "-hardware" in sys.argv:
        print("Using hardware acceleration")
        renderer = sdl2ext.RenderContext(window)
        factory = sdl2ext.SpriteFactory(sdl2ext.TEXTURE, renderer=renderer)
    else:
        print("Using software rendering")
        factory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE)

    # Create the paddles - we want white ones. To keep it easy enough for us,
    # we create a set of surfaces that can be used for Texture- and
    # Software-based sprites.
    sp_paddle1 = factory.from_color(WHITE, size=(20, 100))
    sp_paddle2 = factory.from_color(WHITE, size=(20, 100))
    sp_ball = factory.from_color(WHITE, size=(20, 20))

    world = World()

    movement = MovementSystem(0, 0, 800, 600)
    collision = CollisionSystem(0, 0, 800, 600)
    aicontroller = TrackingAIController(0, 600)
    if factory.sprite_type == sdl2ext.SOFTWARE:
        spriterenderer = SoftwareRenderer(window)
    else:
        spriterenderer = TextureRenderer(renderer)

    world.add_system(aicontroller)
    world.add_system(movement)
    world.add_system(collision)
    world.add_system(spriterenderer)

    player1 = Player(world, sp_paddle1, 0, 250)
    player2 = Player(world, sp_paddle2, 780, 250, True)
    ball = Ball(world, sp_ball, 390, 290)
    ball.velocity.vx = -BALL_SPEED
    collision.ball = ball
    aicontroller.ball = ball

    running = True
    while running:
        for event in sdl2ext.get_events():
            if event.type == sdlevents.SDL_QUIT:
                running = False
                break
            if event.type == sdlevents.SDL_KEYDOWN:
                if event.key.keysym.sym == sdlkc.SDLK_UP:
                    player1.velocity.vy = -PADDLE_SPEED
                elif event.key.keysym.sym == sdlkc.SDLK_DOWN:
                    player1.velocity.vy = PADDLE_SPEED
            elif event.type == sdlevents.SDL_KEYUP:
                if event.key.keysym.sym in (sdlkc.SDLK_UP, sdlkc.SDLK_DOWN):
                    player1.velocity.vy = 0
        sdltimer.SDL_Delay(10)
        world.process()
Esempio n. 13
0
 def test_Window_size(self):
     # This may fail for fullscreen WMs or Win10 tablet modes
     window = sdl2ext.Window("Size",
                             size=(200, 200),
                             flags=video.SDL_WINDOW_RESIZABLE)
     assert window.size == (200, 200)
     window.size = 150, 77
     assert window.size == (150, 77)
     window.close()
Esempio n. 14
0
def run():
    # print sdl2ext.get_image_formats()
    # return
    sdl2ext.init()
    window = sdl2ext.Window("The Pong Game", size=(800, 600))
    window.show()

    world = sdl2ext.World()

    factory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE)
    sp_ball = factory.from_color(WHITE, size=(20, 20))
    sp_paddle1 = factory.from_color(WHITE, size=(20, 100))
    sp_paddle2 = factory.from_color(WHITE, size=(20, 100))

    movement = MovementSystem(0, 0, 800, 600)
    collision = CollisionSystem(0, 0, 800, 600, 390, 290)
    aicontroller = TrackingAIController(0, 600)

    spriterenderer = SoftwareRenderer(window)

    world.add_system(aicontroller)
    world.add_system(movement)
    world.add_system(collision)
    world.add_system(spriterenderer)

    player1 = Player(world, sp_paddle1, 0, 250)
    player2 = Player(world, sp_paddle2, 780, 250, True)

    ball = Ball(world, sp_ball, 390, 290)
    ball.velocity.vx = -3

    collision.ball = ball
    aicontroller.ball = ball

    collision.player1 = player1
    collision.player2 = player2

    running = True
    while running:
        events = sdl2ext.get_events()
        for event in events:
            if event.type == SDL_QUIT:
                running = False
                break
            if event.type == SDL_KEYDOWN:
                if event.key.keysym.sym == SDLK_UP:
                    player1.velocity.vy = -3
                elif event.key.keysym.sym == SDLK_DOWN:
                    player1.velocity.vy = 3
            elif event.type == SDL_KEYUP:
                if event.key.keysym.sym in (SDLK_UP, SDLK_DOWN):
                    player1.velocity.vy = 0
        SDL_Delay(10)
        world.process()
    return 0
Esempio n. 15
0
 def __init__(self, entities, timer):
     self.entities = entities
     self.timer = timer
     self.window = sdle.Window("Game", size=(self.WINDOW_X, self.WINDOW_Y))
     self.renderer = sdle.Renderer(self.window,
                                   flags=sdl.SDL_RENDERER_ACCELERATED)
     self.texture = sdl.SDL_CreateTexture(self.renderer.renderer,
                                          sdl.SDL_PIXELFORMAT_ARGB8888,
                                          sdl.SDL_TEXTUREACCESS_STREAMING,
                                          self.WINDOW_X, self.WINDOW_Y)
     self.window.show()
Esempio n. 16
0
    def test_Window(self):
        flags = video.SDL_WINDOW_BORDERLESS
        sizes = ((1, 1), (10, 10), (10, 20), (200, 17), (640, 480), (800, 600))
        for w, h in sizes:
            window = sdl2ext.Window("Window", size=(w, h), flags=flags)
            self.assertEqual(window.size, (w, h))

        #self.assertRaises(TypeError, video.Window, None, None, None, None)
        #self.assertRaises(TypeError, video.Window, None, None, None)
        #self.assertRaises(TypeError, video.Window, None, None)
        self.assertRaises(TypeError, sdl2ext.Window, "Test", None, None, None)
        self.assertRaises(TypeError, sdl2ext.Window, "Test", None, None)
        self.assertRaises(TypeError, sdl2ext.Window, "Test", None)
Esempio n. 17
0
def run():
    # You know those from the helloworld.py example.
    # Initialize the video subsystem, create a window and make it visible.
    sdl2ext.init()
    window = sdl2ext.Window("sdlgfx drawing examples", size=(800, 600))
    window.show()

    # Create a rendering context for the window. The sdlgfx module requires it.
    context = sdl2ext.RenderContext(window)

    # We implement the functionality as it was done in colorpalettes.py and
    # utilise a mapping table to look up the function to be executed, together
    # with the arguments they should receive
    functions = ((draw_lines, (context, 800, 600)),
                 (draw_circles, (context, 800, 600)),
                 (draw_ellipsis, (context, 800, 600)),
                 (draw_rects, (context, 800, 600)),
                 (draw_trigons, (context, 800, 600)),
                 (draw_polygons, (context, 800, 600)),
                 (draw_mixed, (context, 800, 600))
                 )

    # A storage variable for the function we are currently on, so that we know
    # which function to execute next.
    curindex = 0
    draw_lines(context, 800, 600)

    # The event loop is nearly the same as we used in colorpalettes.py. If you
    # do not know, what happens here, take a look at colorpalettes.py for a
    # detailed description.
    running = True
    while running:
        events = sdl2ext.get_events()
        for event in events:
            if event.type == SDL_QUIT:
                running = False
                break
            if event.type == SDL_MOUSEBUTTONDOWN:
                curindex += 1
                if curindex >= len(functions):
                    curindex = 0
                # In contrast to colorpalettes.py, our mapping table consists
                # of functions and their arguments. Thus, we get the currently
                # requested function and argument tuple and execute the
                # function with the arguments.
                func, args = functions[curindex]
                func(*args)
                break
        context.present()
    sdl2ext.quit()
    return 0
Esempio n. 18
0
def run():
    # You know those from the helloworld.py example.
    # Initialize the video subsystem, create a window and make it visible.
    sdl2ext.init()
    window = sdl2ext.Window("Pixel Access", size=(800, 600))
    window.show()

    # As in colorpalettes.py, explicitly acquire the window's surface to
    # draw on.
    windowsurface = window.get_surface()

    # We implement the functionality as it was done in colorpalettes.py and
    # utilise a mapping table to look up the function to be executed, together
    # with the arguments they should receive
    functions = (
        (draw_horizontal_stripes, (windowsurface, 300, 500, 200, 400)),
        (draw_vertical_stripes, (windowsurface, 300, 500, 200, 400)),
    )

    # A storage variable for the function we are currently on, so that we know
    # which function to execute next.
    curindex = 0
    draw_horizontal_stripes(windowsurface, 300, 500, 200, 400)

    # The event loop is nearly the same as we used in colorpalettes.py. If you
    # do not know, what happens here, take a look at colorpalettes.py for a
    # detailled description.
    running = True
    while running:
        events = sdl2ext.get_events()
        for event in events:
            if event.type == SDL_QUIT:
                running = False
                break
            if event.type == SDL_MOUSEBUTTONDOWN:
                curindex += 1
                if curindex >= len(functions):
                    curindex = 0
                # In contrast to colorpalettes.py, our mapping table consists
                # of functions and their arguments. Thus, we get the currently
                # requested function and argument tuple and execute the
                # function with the arguments.
                func, args = functions[curindex]
                func(*args)
                break
        window.refresh()
    sdl2ext.quit()
    return 0
Esempio n. 19
0
    def test_SpriteFactory_create_texture_sprite(self):
        window = sdl2ext.Window("Test", size=(1, 1))
        renderer = sdl2ext.Renderer(window)
        factory = sdl2ext.SpriteFactory(sdl2ext.TEXTURE, renderer=renderer)
        for w in range(1, 100):
            for h in range(1, 100):
                sprite = factory.create_texture_sprite(renderer, size=(w, h))
                self.assertIsInstance(sprite, sdl2ext.TextureSprite)
                del sprite

        # Test different access flags
        for flag in (SDL_TEXTUREACCESS_STATIC, SDL_TEXTUREACCESS_STREAMING,
                     SDL_TEXTUREACCESS_TARGET):
            sprite = factory.create_texture_sprite(renderer, size=(64, 64),
                                                   access=flag)
            self.assertIsInstance(sprite, sdl2ext.TextureSprite)
            del sprite
        dogc()
Esempio n. 20
0
    def test_SpriteFactory_create_sprite(self):
        window = sdl2ext.Window("Test", size=(1, 1))
        renderer = sdl2ext.Renderer(window)
        tfactory = sdl2ext.SpriteFactory(sdl2ext.TEXTURE, renderer=renderer)
        sfactory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE)

        for w in range(0, 100):
            for h in range(0, 100):
                for bpp in (1, 4, 8, 12, 15, 16, 24, 32):
                    sprite = sfactory.create_sprite(size=(w, h), bpp=bpp)
                    assert isinstance(sprite, sdl2ext.SoftwareSprite)

                if w == 0 or h == 0:
                    with pytest.raises(sdl2ext.SDLError):
                        tfactory.create_sprite(size=(w, h))
                    continue
                sprite = tfactory.create_sprite(size=(w, h))
                assert isinstance(sprite, sdl2ext.TextureSprite)
        dogc()
Esempio n. 21
0
    def test_Renderer(self):
        sf = SDL_CreateRGBSurface(0, 10, 10, 32, 0, 0, 0, 0).contents

        renderer = sdl2ext.Renderer(sf)
        assert renderer.rendertarget == sf
        assert isinstance(renderer.sdlrenderer.contents, SDL_Renderer)
        del renderer

        # Deprecated
        renderer = sdl2ext.Renderer(sf)
        assert renderer.rendertarget == sf
        assert isinstance(renderer.sdlrenderer.contents, SDL_Renderer)
        del renderer

        sprite = sdl2ext.SoftwareSprite(sf, True)
        renderer = sdl2ext.Renderer(sprite)
        assert renderer.rendertarget == sprite
        assert isinstance(renderer.sdlrenderer.contents, SDL_Renderer)
        del renderer
        dogc()

        window = sdl2ext.Window("Test", size=(1, 1))
        renderer = sdl2ext.Renderer(window)
        assert renderer.rendertarget == window
        assert isinstance(renderer.sdlrenderer.contents, SDL_Renderer)
        del renderer
        dogc()

        sdlwindow = window.window
        renderer = sdl2ext.Renderer(sdlwindow)
        assert renderer.rendertarget == sdlwindow
        assert isinstance(renderer.sdlrenderer.contents, SDL_Renderer)
        del renderer
        del window

        with pytest.raises(TypeError):
            sdl2ext.Renderer(None)
        with pytest.raises(TypeError):
            sdl2ext.Renderer(1234)
        with pytest.raises(TypeError):
            sdl2ext.Renderer("test")
        dogc()
    def test_SpriteFactory_from_image(self):
        window = sdl2ext.Window("Test", size=(1, 1))
        renderer = sdl2ext.Renderer(window)
        tfactory = sdl2ext.SpriteFactory(sdl2ext.TEXTURE, renderer=renderer)
        sfactory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE)

        for suffix in ("tif", "png", "jpg"):
            imgname = RESOURCES.get_path("surfacetest.%s" % suffix)
            tsprite = tfactory.from_image(imgname)
            self.assertIsInstance(tsprite, sdl2ext.TextureSprite)
            ssprite = sfactory.from_image(imgname)
            self.assertIsInstance(ssprite, sdl2ext.SoftwareSprite)

        for factory in (tfactory, sfactory):
            self.assertRaises((ArgumentError, ValueError), factory.from_image,
                              None)
            #self.assertRaises((IOError, SDLError),
            #                  factory.from_image, "banana")
            if not _ISPYPY:
                self.assertRaises(ArgumentError, factory.from_image, 12345)
        dogc()
Esempio n. 23
0
    def test_Renderer(self):
        sf = SDL_CreateRGBSurface(0, 10, 10, 32, 0, 0, 0, 0).contents

        renderer = sdl2ext.Renderer(sf)
        self.assertEqual(renderer.rendertarget, sf)
        self.assertIsInstance(renderer.sdlrenderer.contents, SDL_Renderer)
        del renderer

        # Deprecated
        renderer = sdl2ext.Renderer(sf)
        self.assertEqual(renderer.rendertarget, sf)
        self.assertIsInstance(renderer.renderer.contents, SDL_Renderer)
        del renderer


        sprite = sdl2ext.SoftwareSprite(sf, True)
        renderer = sdl2ext.Renderer(sprite)
        self.assertEqual(renderer.rendertarget, sprite)
        self.assertIsInstance(renderer.sdlrenderer.contents, SDL_Renderer)
        del renderer
        dogc()

        window = sdl2ext.Window("Test", size=(1, 1))
        renderer = sdl2ext.Renderer(window)
        self.assertEqual(renderer.rendertarget, window)
        self.assertIsInstance(renderer.sdlrenderer.contents, SDL_Renderer)
        del renderer
        dogc()

        sdlwindow = window.window
        renderer = sdl2ext.Renderer(sdlwindow)
        self.assertEqual(renderer.rendertarget, sdlwindow)
        self.assertIsInstance(renderer.sdlrenderer.contents, SDL_Renderer)
        del renderer
        del window

        self.assertRaises(TypeError, sdl2ext.Renderer, None)
        self.assertRaises(TypeError, sdl2ext.Renderer, 1234)
        self.assertRaises(TypeError, sdl2ext.Renderer, "test")
        dogc()
Esempio n. 24
0
    def test_SpriteFactory_from_surface(self):
        window = sdl2ext.Window("Test", size=(1, 1))
        renderer = sdl2ext.Renderer(window)
        tfactory = sdl2ext.SpriteFactory(sdl2ext.TEXTURE, renderer=renderer)
        sfactory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE)

        sf = SDL_CreateRGBSurface(0, 10, 10, 32, 0, 0, 0, 0)
        tsprite = tfactory.from_surface(sf.contents)
        self.assertIsInstance(tsprite, sdl2ext.TextureSprite)
        ssprite = sfactory.from_surface(sf.contents)
        self.assertIsInstance(ssprite, sdl2ext.SoftwareSprite)
        SDL_FreeSurface(sf)

        for factory in (tfactory, sfactory):
            self.assertRaises((sdl2ext.SDLError, AttributeError, ArgumentError,
                               TypeError), factory.from_surface, None)
            self.assertRaises((AttributeError, ArgumentError, TypeError),
                              factory.from_surface, "test")
            # TODO: crashes pypy 2.0
            #self.assertRaises((AttributeError, ArgumentError, TypeError),
            #                  factory.from_surface, 1234)
        dogc()
Esempio n. 25
0
    def test_SoftwareSpriteRenderSystem_render(self):
        sf1 = SDL_CreateRGBSurface(0, 12, 7, 32, 0, 0, 0, 0)
        sp1 = sdl2ext.SoftwareSprite(sf1.contents, True)
        sdl2ext.fill(sp1, 0xFF0000)

        sf2 = SDL_CreateRGBSurface(0, 3, 9, 32, 0, 0, 0, 0)
        sp2 = sdl2ext.SoftwareSprite(sf2.contents, True)
        sdl2ext.fill(sp2, 0x00FF00)
        sprites = [sp1, sp2]

        window = sdl2ext.Window("Test", size=(20, 20))
        renderer = sdl2ext.SoftwareSpriteRenderSystem(window)
        assert isinstance(renderer, sdl2ext.SpriteRenderSystem)

        with pytest.raises(AttributeError):
            renderer.render(None, None, None)
        with pytest.raises(AttributeError):
            renderer.render([None, None], None, None)

        for x, y in ((0, 0), (3, 3), (20, 20), (1, 12), (5, 6)):
            sp1.position = x, y
            renderer.render(sp1)
            view = sdl2ext.PixelView(renderer.surface)
            self.check_pixels(view, 20, 20, sp1, 0xFF0000, (0x0, ))
            del view
            sdl2ext.fill(renderer.surface, 0x0)
        sp1.position = 0, 0
        sp2.position = 14, 1
        renderer.render(sprites)
        view = sdl2ext.PixelView(renderer.surface)
        self.check_pixels(view, 20, 20, sp1, 0xFF0000, (0x0, 0x00FF00))
        self.check_pixels(view, 20, 20, sp2, 0x00FF00, (0x0, 0xFF0000))
        del view
        sdl2ext.fill(renderer.surface, 0x0)
        renderer.render(sprites, 1, 2)
        view = sdl2ext.PixelView(renderer.surface)
        self.check_pixels(view, 20, 20, sp1, 0xFF0000, (0x0, 0x00FF00), 1, 2)
        self.check_pixels(view, 20, 20, sp2, 0x00FF00, (0x0, 0xFF0000), 1, 2)
        del view
    def test_SoftwareSpriteRenderSystem(self):
        self.assertRaises(TypeError, sdl2ext.SoftwareSpriteRenderSystem)
        self.assertRaises(TypeError, sdl2ext.SoftwareSpriteRenderSystem, None)
        self.assertRaises(TypeError, sdl2ext.SoftwareSpriteRenderSystem,
                          "Test")
        self.assertRaises(TypeError, sdl2ext.SoftwareSpriteRenderSystem, 12345)

        window = sdl2ext.Window("Test", size=(1, 1))
        renderer = sdl2ext.SoftwareSpriteRenderSystem(window)
        self.assertIsInstance(renderer, sdl2ext.SpriteRenderSystem)
        self.assertEqual(renderer.window, window.window)
        self.assertIsInstance(renderer.surface, SDL_Surface)

        renderer = sdl2ext.SoftwareSpriteRenderSystem(window.window)
        self.assertIsInstance(renderer, sdl2ext.SpriteRenderSystem)
        self.assertEqual(renderer.window, window.window)
        self.assertIsInstance(renderer.surface, SDL_Surface)

        self.assertIsNotNone(renderer.sortfunc)
        self.assertFalse(sdl2ext.Sprite in renderer.componenttypes)
        self.assertTrue(sdl2ext.SoftwareSprite in renderer.componenttypes)
        dogc()
    def test_SoftwareSpriteRenderSystem_process(self):
        sf1 = SDL_CreateRGBSurface(0, 5, 10, 32, 0, 0, 0, 0)
        sp1 = sdl2ext.SoftwareSprite(sf1.contents, True)
        sp1.depth = 0
        sdl2ext.fill(sp1, 0xFF0000)

        sf2 = SDL_CreateRGBSurface(0, 5, 10, 32, 0, 0, 0, 0)
        sp2 = sdl2ext.SoftwareSprite(sf2.contents, True)
        sp2.depth = 99
        sdl2ext.fill(sp2, 0x00FF00)
        sprites = [sp1, sp2]

        window = sdl2ext.Window("Test", size=(20, 20))
        renderer = sdl2ext.SoftwareSpriteRenderSystem(window)

        renderer.process("fakeworld", sprites)
        view = sdl2ext.PixelView(renderer.surface)
        # Only sp2 wins, since its depth is higher
        self.check_pixels(view, 20, 20, sp1, 0x00FF00, (0x0, ))
        self.check_pixels(view, 20, 20, sp2, 0x00FF00, (0x0, ))
        del view

        self.assertRaises(TypeError, renderer.process, None, None)
    def test_SpriteFactory_from_text(self):
        sfactory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE)
        fm = sdl2ext.FontManager(RESOURCES.get_path("tuffy.ttf"))

        # No Fontmanager passed
        self.assertRaises(KeyError, sfactory.from_text, "Test")

        # Passing various keywords arguments
        sprite = sfactory.from_text("Test", fontmanager=fm)
        self.assertIsInstance(sprite, sdl2ext.SoftwareSprite)

        sprite = sfactory.from_text("Test", fontmanager=fm, alias="tuffy")
        self.assertIsInstance(sprite, sdl2ext.SoftwareSprite)

        # Get text from a texture sprite factory
        window = sdl2ext.Window("Test", size=(1, 1))
        renderer = sdl2ext.Renderer(window)
        tfactory = sdl2ext.SpriteFactory(sdl2ext.TEXTURE,
                                         renderer=renderer,
                                         fontmanager=fm)
        sprite = tfactory.from_text("Test", alias="tuffy")
        self.assertIsInstance(sprite, sdl2ext.TextureSprite)
        dogc()
Esempio n. 29
0
def main():
    sdl2ext.init()
    TTF_Init()

    window = sdl2ext.Window("Text display", size=(800, 600))
    window.show()

    renderer = sdl2ext.RenderContext(window)
    factory = sdl2ext.SpriteFactory(sdl2ext.TEXTURE, renderer=renderer)
    world = sdl2ext.World()

    fps = FPSCounter(world, renderer=renderer)

    spriteRenderer = factory.create_sprite_renderer()
    fpsController = FPSController()

    world.add_system(fpsController)
    world.add_system(spriteRenderer)

    running = True

    while running:
        for event in sdl2ext.get_events():
            if event.type == sdlevents.SDL_QUIT:
                running = False
                break
            elif event.type == sdlevents.SDL_USEREVENT:
                entity = cast(event.user.data1,
                              POINTER(py_object)).contents.value
                entity.textsprite.text = "FPS: " + str(entity.fps.counter)
                entity.fps.counter = 0
        renderer.clear()
        world.process()

    TTF_Quit()
    sdl2ext.quit()
    return 0
    def test_SpriteFactory(self):
        factory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE)
        self.assertIsInstance(factory, sdl2ext.SpriteFactory)
        self.assertEqual(factory.default_args, {})

        factory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE, bananas="tasty")
        self.assertIsInstance(factory, sdl2ext.SpriteFactory)
        self.assertEqual(factory.default_args, {"bananas": "tasty"})

        window = sdl2ext.Window("Test", size=(1, 1))
        renderer = sdl2ext.Renderer(window)

        factory = sdl2ext.SpriteFactory(sdl2ext.TEXTURE, renderer=renderer)
        self.assertIsInstance(factory, sdl2ext.SpriteFactory)

        factory = sdl2ext.SpriteFactory(sdl2ext.TEXTURE, renderer=renderer)
        self.assertIsInstance(factory, sdl2ext.SpriteFactory)
        self.assertEqual(factory.default_args, {"renderer": renderer})

        self.assertRaises(ValueError, sdl2ext.SpriteFactory, "Test")
        self.assertRaises(ValueError, sdl2ext.SpriteFactory, -456)
        self.assertRaises(ValueError, sdl2ext.SpriteFactory, 123)
        self.assertRaises(ValueError, sdl2ext.SpriteFactory, sdl2ext.TEXTURE)
        dogc()