Created
March 25, 2015 13:43
-
-
Save G4MR/327074ceb2b8c7a04826 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sdl2, sdl2.image as img | |
import helpers | |
import typetraits | |
type Image* = ref object | |
rw*: RWopsPtr | |
format: string | |
loaded: bool | |
surface: SurfacePtr | |
texture: TexturePtr | |
src_rect: Rect | |
dst_rect: Rect | |
method getFormat*(self: Image): string = | |
if isNil(self.format): | |
"none" | |
else: | |
self.format | |
method isLoaded*(self: Image): bool = | |
self.loaded | |
method setDimensions*(self: Image; src, dst: Rect) = | |
self.src_rect = src | |
self.dst_rect = dst | |
method setDimensions*(self: Image; src, dst: tuple[x, y: int; w, h: int]) = | |
self.setDimensions(toRect(src), toRect(dst)) | |
method setSrc*(self: Image, src: Rect) = | |
self.src_rect = src | |
method setSrc*(self: Image, src: tuple[x, y: int; w, h: int]) = | |
self.setSrc(toRect(src)) | |
method setDst*(self: Image, dst: Rect) = | |
self.dst_rect = dst | |
method setDst*(self: Image, dst: tuple[x, y: int; w, h: int]) = | |
self.setDst(toRect(dst)) | |
method load*(self: Image, file: cstring, render: RendererPtr) = | |
self.loaded = false | |
self.rw = sdl2.rwFromFile(file, "rb") | |
if not isNIl(self.rw): | |
if img.isGIF(self.rw) == 1: | |
self.format = "gif" | |
elif img.isPNG(self.rw) == 1: | |
self.format = "png" | |
elif img.isJPG(self.rw) == 1: | |
self.format = "jpg" | |
elif img.isBMP(self.rw) == 1: | |
self.format = "bmp" | |
elif img.isWEBP(self.rw) == 1: | |
self.format = "webp" | |
else: | |
self.format = nil | |
if isNil(self.rw): | |
echo "yes..." | |
if not isNil(self.format): | |
# try loading surface | |
self.surface = img.load_RW(self.rw, 1) | |
if isNil(self.surface): | |
#throw exception | |
return | |
#try loading texture | |
self.texture = sdl2.createTextureFromSurface(render, self.surface) | |
if isNil(self.texture): | |
#throw exception | |
return | |
self.loaded = true | |
method draw*(self: Image, render: RendererPtr) = | |
if isNil(self.texture): | |
return | |
# draw font to screen | |
sdl2.copy(render, self.texture, addr(self.src_rect), addr(self.dst_rect)) | |
# Release any resources used by the stream and free RWopsPtr memory | |
method close*(self: Image) = | |
if not isNil(self.rw): | |
let close_rw = self.rw.close | |
echo "closed successfully" | |
if not isNil(self.surface): | |
destroy self.surface | |
if not isNil(self.texture): | |
destroy self.texture |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment