def test_SDL_RenderDrawPoint(self): points = ((-4, -3), (-4, 3), (4, -3), (0, 0), (1, 1), (10, 10), (99, 99), (4, 22), (57, 88), (45, 15), (100, 100)) r, g, b, a = 0xAA, 0xBB, 0xCC, 0xDD w, h = 100, 100 sf = surface.SDL_CreateRGBSurface(0, w, h, 32, 0xFF000000, 0x00FF0000, 0x0000FF00, 0x000000FF) color = pixels.SDL_MapRGBA(sf.contents.format, r, g, b, a) renderer = render.SDL_CreateSoftwareRenderer(sf) self.assertIsInstance(renderer.contents, render.SDL_Renderer) ret = render.SDL_SetRenderDrawColor(renderer, r, g, b, a) self.assertEqual(ret, 0) for x, y in points: ret = render.SDL_RenderDrawPoint(renderer, x, y) self.assertEqual(ret, 0) render.SDL_RenderPresent(renderer) view = PixelView(sf.contents) for x, y in points: npx = max(x + 1, w) npy = max(y + 1, h) ppx = max(x - 1, 0) ppy = max(y - 1, 0) if x < 0 or x >= w or y < 0 or y >= h: continue self.assertEqual(hex(view[y][x]), hex(color)) if (npx, npy) not in points: self.assertNotEqual(hex(view[npy][npx]), hex(color)) if (ppx, ppy) not in points: self.assertNotEqual(hex(view[ppy][ppx]), hex(color)) render.SDL_DestroyRenderer(renderer) del view surface.SDL_FreeSurface(sf)
def render(self, sprites, x=None, y=None): """Overrides the render method of sdl2.ext.TextureSpriteRenderSystem to use "SDL_RenderCopyEx" instead of "SDL_RenderCopy" to allow sprite rotation: http://wiki.libsdl.org/SDL_RenderCopyEx """ r = rect.SDL_Rect(0, 0, 0, 0) if isiterable(sprites): rcopy = render.SDL_RenderCopyEx renderer = self.sdlrenderer x = x or 0 y = y or 0 for sp in sprites: r.x = x + int(sp.x) r.y = y + int(sp.y) r.w, r.h = sp.size if rcopy(renderer, sp.texture, None, r, sp.angle, None, render.SDL_FLIP_NONE) == -1: raise sdl2.ext.error() else: r.x = sprites.x r.y = sprites.y r.w, r.h = sprites.size if x is not None and y is not None: r.x = x r.y = y render.SDL_RenderCopyEx(self.sdlrenderer, sprites.texture, None, r, sprites.angle, None, render.SDL_FLIP_NONE) render.SDL_RenderPresent(self.sdlrenderer)
def render(self): unsized = node.unsized_nodes() resize = False if self._last_w_size != self._window.size: self._dirty = True self._update_base_scale() resize = True w = min(self.window_size) image_scale = self._base_scale * self._zoom image_scale = image_scale, image_scale for nd in unsized: if isinstance(nd, node.TextNode) and not nd.text: continue self._load_sprite(nd, image_scale, w) if not self.root_node.traverse() and not self._dirty \ and not self.uiroot.traverse(): return if self._dirty: self._update_view_aabb() x = int(self._view_pos.x * w * self._zoom) y = int(self._view_pos.y * w * self._zoom) self._renderer.clear() for nd in self.root_node.query(self._view_aabb): if isinstance(nd, node.ImageNode): if nd.tiled and resize: self._load_sprite(nd, image_scale, w) self._render_image(nd, w, image_scale, x, y) elif isinstance(nd, node.TextNode): self._render_text(nd, w, image_scale, x, y) ui_aabb = aabb.AABB(*self._view_aabb.size, *self._view_aabb.size) ui_nodes = self.uiroot.query(ui_aabb) for nd in ui_nodes: if isinstance(nd, uinode.UINode): nd.update() for nd in ui_nodes: if isinstance(nd, node.ImageNode): self._render_image(nd, w, image_scale, x, y) elif isinstance(nd, node.TextNode): self._render_text(nd, w, image_scale, x, y) self._dirty = False render.SDL_RenderPresent(self.renderer)
def run_sdl(): sdl2.ext.init() window = sdl2.ext.Window("SDL Cairo Example", size=(WIDTH, 300), flags=sdl2.SDL_WINDOW_ALLOW_HIGHDPI) window.show() running = True renderer = render.SDL_CreateRenderer(window.window, -1, 0) render.SDL_RenderClear(renderer) port = rect.SDL_Rect() render.SDL_RenderGetViewport(renderer, byref(port)) cairo_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, port.w, port.h) scale = port.w * 1.0 / WIDTH texture = render.SDL_CreateTexture(renderer, sdl2.pixels.SDL_PIXELFORMAT_ARGB32, render.SDL_TEXTUREACCESS_TARGET, port.w, port.h) while running: draw(cairo_surface, scale) buf = cairo_surface.get_data().tobytes() render.SDL_UpdateTexture(texture, port, buf, port.w * 4) render.SDL_RenderCopy(renderer, texture, port, port) render.SDL_RenderPresent(renderer) events = sdl2.ext.get_events() for event in events: if event.type == sdl2.SDL_QUIT: running = False break time.sleep(0.2) render.SDL_DestroyTexture(texture)