def loadImage(filename, transparentColor=None, rotate=0, scale=1, flipHorizontal=False, flipVertical=False): freeable_images = [] image_out = None try: image = sdl2.sdlimage.IMG_Load(filename.encode()) if image is None: raise IOError("Could not load image: %s" % sdl2.sdlimage.IMG_GetError()) freeable_images.append(image) # pf = _disp.getPixelFormat() # TODO: do we still need to convert the surface? I don't think so... #image = sdl2.SDL_ConvertSurface(image_base, pf, 0) #assert image is not None, "Could not convert image: %s" % sdl2.SDL_GetError() #freeable_images.append(image) # TODO: do something differently if transparentColor is None versus False? if transparentColor is not None and transparentColor is not False: r, g, b = colors.lookupColor(transparentColor) pix = sdl2.SDL_MapRGB(r, g, b) assert sdl2.SDL_SetColorKey(image, True, pix) == 0, "Could not set color key: %s" % sdl2.SDL_GetError() if rotate != 0 or scale != 1 or flipHorizontal or flipVertical: hscale = -scale if flipHorizontal else scale vscale = -scale if flipVertical else scale image = sdl2.sdlgfx.rotozoomSurfaceXY(image, rotate, hscale, vscale, sdl2.sdlgfx.SMOOTHING_ON) assert image is not None, "Could not transform image: %s" % sdl2.SDL_GetError() freeable_images.append(image) image_out = image finally: for freeable in freeable_images: if freeable is not image_out and freeable is not None: sdl2.SDL_FreeSurface(freeable) return Image(image_out)
def drawEllipse(self, x, y, width, height, color=None, thickness=1): color = color or self.foreground thickness = int(thickness) r, g, b, a = colors.lookupColor(color) if thickness <= 0: assert sdl2.sdlgfx.filledEllipseRGBA(self.renderer, int(x), int(y), int(width), int(height), r, g, b, a) == 0, \ "Could not fill ellipse: %s" % sdl2.SDL_GetError() else: # what on earth was pygame doing??? for loop in range(thickness): assert sdl2.sdlgfx.ellipseRGBA(self.renderer, int(x), int(y), int(width / 2) - loop, int(height / 2) - loop, r, g, b, a) == 0, \ "Could not draw ellipse: %s" % sdl2.SDL_GetError()
def drawPolygon(self, pointlist, color=None, thickness=1): color = color or self.foreground thickness = int(thickness) if thickness > 0: for i, (x1, y1) in enumerate(pointlist): x2, y2 = pointlist[(i + 1) % len(pointlist)] self.drawLine(x1, y1, x2, y2, color, thickness) else: r, g, b = colors.lookupColor(color) xlist, ylist = (sdl2.Sint16 * len(pointlist))(), (sdl2.Sint16 * len(pointlist))() for k, (x, y) in enumerate(pointlist): xlist[k], ylist[k] = x, y xptr = ctypes.cast(xlist, ctypes.POINTER(sdl2.Sint16)) yptr = ctypes.cast(ylist, ctypes.POINTER(sdl2.Sint16)) assert sdl2.sdlgfx.filledPolygonRGBA(self.renderer, xptr, yptr, len(pointlist), int(r), int(g), int(b), 255) \ == 0, "Could not fill polygon: %s" % sdl2.SDL_GetError()
def drawString(self, text, x, y, size=30, color=None, bold=False, italic=False, font=None): color = color or self.foreground color = self.convertColor(colors.lookupColor(color)) font = self.getCachedFont(bold, font, italic, size) # TODO: do we want to change the quality? textimage = sdl2.sdlttf.TTF_RenderUTF8_Solid(font, str(text).encode("utf-8"), color) assert textimage is not None, "Could not render font: %s" % sdl2.SDL_GetError() try: texture = sdl2.SDL_CreateTextureFromSurface(self.renderer, textimage) assert texture is not None, "Could not convert surface: %s" % sdl2.SDL_GetError() try: target_rect = sdl2.SDL_Rect(int(x), int(y), textimage.contents.w, textimage.contents.h) assert sdl2.SDL_RenderCopy(self.renderer, texture, None, target_rect) == 0, \ "Could not render text: %s" % sdl2.SDL_GetError() return textimage.contents.w, textimage.contents.h finally: sdl2.SDL_DestroyTexture(texture) finally: sdl2.SDL_FreeSurface(textimage)
def drawLine(self, x1, y1, x2, y2, color=None, thickness=1): color = color or self.foreground assert int(thickness) >= 1, "invalid thickness - gfx will fail" r, g, b, a = colors.lookupColor(color) assert sdl2.sdlgfx.thickLineRGBA(self.renderer, int(x1), int(y1), int(x2), int(y2), int(thickness), r, g, b, a) == 0, "Could not draw line: %s" % sdl2.SDL_GetError()
def drawPixel(self, x, y, color=None): color = color or self.foreground self.set_render_color(colors.lookupColor(color)) assert sdl2.SDL_RenderDrawPoint(self.renderer, int(x), int(y)) == 0, \ "Could not draw pixel: %s" % sdl2.SDL_GetError()
def setForeground(self, foreground): self.foreground = colors.lookupColor(foreground)
def setBackground(self, background): self.background = colors.lookupColor(background)