def test_Renderer_draw_rect(self): surface = SDL_CreateRGBSurface(0, 128, 128, 32, 0, 0, 0, 0).contents sdl2ext.fill(surface, 0x0) renderer = sdl2ext.Renderer(surface) renderer.draw_rect((40, 50, 32, 32), 0x0000FF) view = sdl2ext.PixelView(surface) self.check_lines(view, 128, 128, [ ((40, 50), (71, 50)), ((40, 50), (40, 81)), ((40, 81), (71, 81)), ((71, 50), (71, 81))], 0x0000FF, (0x0,)) del view sdl2ext.fill(surface, 0x0) renderer.draw_rect([(5, 5, 10, 10), (20, 15, 8, 10)], 0x0000FF) view = sdl2ext.PixelView(surface) self.check_lines(view, 128, 128, [ ((5, 5), (14, 5)), ((5, 5), (5, 14)), ((5, 14), (14, 14)), ((14, 5), (14, 14)), ((20, 15), (27, 15)), ((20, 15), (20, 24)), ((20, 24), (27, 24)), ((27, 15), (27, 24))], 0x0000FF, (0x0,)) del view
def test_pixels2d(self): factory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE) sprite = factory.create_sprite(size=(5, 10), bpp=32, masks=(0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF)) sdl2ext.fill(sprite, 0x01, (2, 2, 2, 2)) nparray = sdl2ext.pixels2d(sprite)
def test_fill(self): # TODO: add exceptions and more bounding tests. rects = ((0, 0, 3, 2), (2, 3, 4, 2), (5, -1, 2, 2), (1, 7, 4, 8) ) factory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE) sprite = factory.create_sprite(size=(10, 10), bpp=32) view = sdl2ext.PixelView(sprite) for rect in rects: sdl2ext.fill(sprite, 0) colorval = sdl2ext.prepare_color(0xAABBCCDD, sprite) sdl2ext.fill(sprite, 0xAABBCCDD, rect) for y, row in enumerate(view): for x, col in enumerate(row): if y >= rect[1] and y < (rect[1] + rect[3]): if x >= rect[0] and x < (rect[0] + rect[2]): self.assertEqual(col, colorval, "color mismatch at (x, y)") else: self.assertEqual(col, 0, "color mismatch at (x, y)") else: self.assertEqual(col, 0, "color mismatch at (x, y)")
def draw_palette(surface, palette): # Fill the entire surface with a black color. This is done by simply # passing a 0 value to the fill argument. We could also create a # Color(0, 0, 0) instance here, but this would be the same. sdl2ext.fill(surface, 0) # Calculate the average width (roughly cut) to be used for each palette # value. When running the example, you will notice a black gap on the # right for some palettes. This s caused by the implicit cut behaviour # of the // operator. Since we can only operate pixel-wise, there are # no fractions to be used. width, height = surface.w, surface.h rw = width // len(palette) # Create the area to be filled with the palette values. we always start # at the top-left corner, use the calculated width and the entire height # of the window surface. As you will see below, we then only advance # horizontically by the calculated width to draw stripes. # Play around with different height values and start offsets to see what # happens rect = [0, 0, rw, height] # Loop over all colors and fill a portion of the surface with them. As # above, we use fill() to fill the surface with the palette color, but now # we provide an area (the third argument) to avoid filling the whole # surface. Instead, the provided area makes sure, that we only fill a # certain part. for color in palette: sdl2ext.fill(surface, color, rect) rect[0] += rw
def draw_horizontal_stripes(surface, x1, x2, y1, y2): # Fill the entire surface with a black color. In contrast to # colorpalettes.py we use a Color() value here, just to demonstrate that # it really works. sdl2ext.fill(surface, BLACK) # Create a 2D view that allows us to directly access each individual pixel # of the surface. The PixelView class is quite slow, since it uses an non- # optimised read-write access to each individual pixel and offset. It works # on every platform, though. pixelview = sdl2ext.PixelView(surface) # Loop over the area bounds, considering each fourth line and every column # on the 2D view. The PixelView uses a y-x alignment to access pixels. # This mkeans that the first accessible dimension of the PixelView denotes # the horizontal lines of an image, and the second the vertical lines. for y in range(y1, y2, 4): for x in range(x1, x2): # Change the color of each individual pixel. We can assign any # color-like value here, since the assignment method of the # PixelView will implicitly check and convert the value to a # matching color for its target surface. pixelview[y][x] = WHITE # Explicitly delete the PixelView. Some surface types need to be locked # in order to access their pixels directly. The PixelView will do that # implicitly at creation time. Once we are done with all necessary # operations, we need to unlock the surface, which will be done # automatically at the time the PixelView is garbage-collected. del pixelview
def draw_vertical_stripes(surface, x1, x2, y1, y2): sdl2ext.fill(surface, BLACK) pixelview = sdl2ext.PixelView(surface) for x in range(x1, x2, 4): for y in range(y1, y2): pixelview[y][x] = WHITE del pixelview
def test_Renderer_draw_line(self): surface = SDL_CreateRGBSurface(0, 128, 128, 32, 0, 0, 0, 0).contents sdl2ext.fill(surface, 0x0) renderer = sdl2ext.Renderer(surface) renderer.draw_line((20, 10, 20, 86), 0x0000FF) view = sdl2ext.PixelView(surface) self.check_lines(view, 128, 128, [((20, 10), (20, 86))], 0x0000FF, (0x0, )) del view
def update(self): último = self.time self.time = time.perf_counter() # desenha cinza sdlx.fill(self.win.get_surface(), sdlx.string_to_color('#555')) sdl.SDL_UpdateWindowSurface(self.win.window) # e soma o tempo total self.tempoTotal += self.time - último self.i += 1
def test_Renderer_copy(self): surface = SDL_CreateRGBSurface(0, 128, 128, 32, 0, 0, 0, 0).contents sdl2ext.fill(surface, 0x0) renderer = sdl2ext.Renderer(surface) factory = sdl2ext.SpriteFactory(sdl2ext.TEXTURE, renderer=renderer) w, h = 32, 32 sp = factory.from_color(0xFF0000, (w, h)) sp.x, sp.y = 40, 50 renderer.copy(sp, (0, 0, w, h), (sp.x, sp.y, w, h)) view = sdl2ext.PixelView(surface) self.check_pixels(view, 128, 128, sp, 0xFF0000, (0x0, )) del view
def oncheck(button, event): if button.checked: color = GREEN else: color = RED if button.factory.sprite_type == sdl2ext.SOFTWARE: sdl2ext.fill(button.surface, color) else: # SDL textures do not support color manipulation operation as easy # as software surface (since the texture is ideally stored somwhere # on the GPU memory in a GPU-specific layout [or not]). To circumvent # this, we create a temporary sprite (texture) and exchange the button # texture with it. tmpsprite = button.factory.from_color(color, button.size) button.texture, tmpsprite.texture = tmpsprite.texture, button.texture del tmpsprite
def test_pixels3d(self): factory = sdl2ext.SpriteFactory(sdl2ext.SOFTWARE) sprite = factory.create_sprite(size=(5, 10), bpp=32, masks=(0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF)) sdl2ext.fill(sprite, 0xAABBCCDD, (1, 2, 3, 4)) nparray = sdl2ext.pixels3d(sprite) for x in range(1, 4): for y in range(2, 6): self.assertTrue(numpy.all([nparray[x, y], [0xAA, 0xBB, 0xCC, 0xDD]])) self.assertFalse(numpy.all([nparray[0, 0], [0xAA, 0xBB, 0xCC, 0xDD]])) self.assertFalse(numpy.all([nparray[1, 0], [0xAA, 0xBB, 0xCC, 0xDD]])) self.assertFalse(numpy.all([nparray[0, 1], [0xAA, 0xBB, 0xCC, 0xDD]])) self.assertFalse(numpy.all([nparray[3, 6], [0xAA, 0xBB, 0xCC, 0xDD]])) self.assertFalse(numpy.all([nparray[4, 6], [0xAA, 0xBB, 0xCC, 0xDD]]))
def draw(surface, width, height): blockw = width / W blockh = width / H for idy in range(H): for idx in range(W): x, y = idx * blockw, idy * blockw value = state[idy, idx] w = weights[idy, idx] if value == 0: r, g, b = (0, 0 ,0) elif 1 <= value <= EX: r, g, b = (0, 0xff//value ,0) else: r, g, b = (0, 0 ,0xff//(value-EX)) r = int(0xff*w/2) color = sdl2ext.Color(r, g, b) sdl2ext.fill(surface, color, (x, y, blockw, blockh))
def draw(window, points, polygons): surface = window.get_surface() sdl2e.fill(surface, sdl2e.string_to_color("#ffffff")) for polygon in polygons: n = len(polygon["points"]) ppoints = list(map(lambda p: points[p], polygon["points"])) edges = map(lambda pi: (ppoints[pi], ppoints[(pi + 1) % n]), range(n)) for edge, m in zip(edges, polygon["mask"]): if m == 0: continue try: # FIXME: fails on out-of-view lines, should do custom z-order canvas draws anyway sdl2e.line(surface, sdl2e.string_to_color("#000000"), (edge[0][0], edge[0][1], edge[1][0], edge[1][1])) except: pass window.refresh()
def draw_rects(surface, width, height): # Fill the whole surface with a black color. sdl2ext.fill(surface, 0) for k in range(15): # Create a set of four random points for the edges of the rectangle. x, y = randint(0, width), randint(0, height) w, h = randint(1, width // 2), randint(1, height // 2) # Create a random color. color = sdl2ext.Color(randint(0, 255), randint(0, 255), randint(0, 255)) # Draw the filled rect with the specified color on the surface. # We also could create a set of points to be passed to the function # in the form # # fill(surface, color, ((x1, y1, x2, y2), (x3, y3, x4, y4), ...)) # ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^ # first rect second rect sdl2ext.fill(surface, color, (x, y, w, h))
def draw_lines(surface, width, height): # Fill the whole surface with a black color. sdl2ext.fill(surface, 0) for x in range(15): # Create a set of four random points for drawing the line. x1, x2 = randint(0, width), randint(0, width) y1, y2 = randint(0, height), randint(0, height) # Create a random color. color = sdl2ext.Color(randint(0, 255), randint(0, 255), randint(0, 255)) # Draw the line with the specified color on the surface. # We also could create a set of points to be passed to the function # in the form # # line(surface, color, (x1, y1, x2, y2, x3, y3, x4, y4, ...)) # ^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^ # first line second line sdl2ext.line(surface, color, (x1, y1, x2, y2))
def make_score_board(window = None, yaku_list = ['rinshan', 'pinfu'], fusu = 20, hansu = 2 , tensu = 2000): if window == None: sdl2ext.init() window = sdl2ext.Window("The Pong Game", size=(800, 600)) window.show() img = print_yaku_list(yaku_list) window_surface = window.get_surface() surface = pilSurface(img) sdl2ext.fill(window_surface, sdl2ext.Color(0, 0, 0 )) rect_tes1 = rect.SDL_Rect(0, 0, 320, 500) rect_tes2 = rect.SDL_Rect(25, 0, 800, 600) SDL_BlitSurface(surface,rect_tes1,window_surface,rect_tes2) SDL_UpdateWindowSurface(window.window) rect_tes1 = rect.SDL_Rect(0, 0, 800, 600) rect_tes2 = rect.SDL_Rect(25, 550, 800, 600) img = PIL.Image.new("RGBA", (500, 35)) if hansu >= 0: text_hansu = str(hansu) + '飜 ' if hansu >= 13: text_hansu = '数え役満 ' if hansu >= 100: text_hansu = '役満 ' text = str(fusu) + '符 ' + text_hansu + str(tensu) + '点' draw_text(img, text, 0, 0) surface = pilSurface(img) SDL_BlitSurface(surface, rect_tes1, window_surface, rect_tes2) SDL_UpdateWindowSurface(window.window) while True: events = sdl2ext.get_events() for event in events: if event.type == SDL_QUIT: running = False break elif event.type == SDL_MOUSEBUTTONDOWN: return
def draw(window, robot): events = sdl.get_events() for event in events: if event.type == sdl2.SDL_QUIT: running = False break surface = window.get_surface() sdl.fill(surface, sdl.Color(0, 0, 0)) #Drawing the walls sdl.line(surface, sdl.Color(255, 255, 255), (-meters_to_pixels(0.4) + WINDOW_SIZE[0] // 2, 0, -meters_to_pixels(0.4) + WINDOW_SIZE[0] // 2, WINDOW_SIZE[1], meters_to_pixels(0.4) + WINDOW_SIZE[0] // 2, 0, meters_to_pixels(0.4) + WINDOW_SIZE[0] // 2, WINDOW_SIZE[1])) draw_robot(surface, robot) window.refresh()
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_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 render(self, components): sdl2ext.fill(self.surface, sdl2ext.Color(0, 0, 0)) super(SoftwareRenderer, self).render(components)
def fill(self, color, rect=None, special_flags=0): try: color = ext.string_to_color(color) except: color = ext.convert_to_color(color) ext.fill(self.surface.get_surface(), color)
def render(self, components): sdl2ext.fill(self.surface, BLACK) super(SoftwareRenderer, self).render(components)
def render(self, sprites, x=None, y=None): # Fill the screen with black every frame. fill(self.surface, Color(0, 0, 0)) super(SoftwareRenderer, self).render(sprites)
def render(self, sprites, x=None, y=None): sdlext.fill(self.surface, BLACK) super().render(sprites)
#!/usr/bin/env python import sdl2 as sdl import sdl2.ext as sdlx import time win = sdlx.Window ('Direto', (800, 600)) win.show () tempo = time.perf_counter () done = False tempoTotal = 0 i = 0 while not done: events = sdlx.get_events () for ev in events: if ev.type == sdl.SDL_QUIT: done = True break; # desenha =P sdlx.fill (win.get_surface (), sdlx.string_to_color ('#555')) sdl.SDL_UpdateWindowSurface (win.window) último = tempo tempo = time.perf_counter () tempoTotal += tempo - último i += 1 print ('Média de FPS:', 1 / (tempoTotal / i))