Created
January 18, 2017 16:22
-
-
Save treeform/8a750e51f85cb5c4c9af862682676e4e to your computer and use it in GitHub Desktop.
make atlas
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 | |
import sdl2.image | |
import os | |
import strutils | |
if not sdl2.init(INIT_EVERYTHING): | |
quit "Failed start SDL2" | |
if image.init(IMG_INIT_PNG) == -1: | |
quit "Failed start SDL2_Image" | |
const size = 1024 | |
var rmask = uint32(0xff000000) | |
var gmask = uint32(0x00ff0000) | |
var bmask = uint32(0x0000ff00) | |
var amask = uint32(0x000000ff) | |
var atlasImage = createRGBSurface( | |
0, | |
cint(size), | |
cint(size), | |
32, | |
rmask, | |
gmask, | |
bmask, | |
amask | |
) | |
var r = Rect((x:cint(0), y:cint(0), w:cint(size), h:cint(size))) | |
fillRect( | |
atlasImage, | |
addr r, | |
uint32(0) | |
) | |
var heights : array[size, uint16] | |
var margin = 8 | |
proc putImage(file: string): void = | |
echo file | |
var image: SurfacePtr = load(file) | |
if image == nil: | |
echo "Failed to load ", file | |
var imgWidth = image.w + margin * 2 | |
var imgHeight = image.h + margin * 2 | |
echo imgWidth, imgHeight | |
var lowest = size | |
var at = 0 | |
for i in 0..size - 1: | |
var v = int(heights[i]) | |
if v < lowest: | |
# found low point, is it consecutive? | |
var fit = true | |
for j in 0 .. imgWidth: | |
if i + j >= size: | |
fit = false | |
break | |
if int(heights[i + j]) > v: | |
fit = false | |
break | |
if fit: | |
# found! | |
lowest = v | |
at = i | |
if lowest + imgHeight > size: | |
echo "Atlas is full!!!" | |
for j in at..at + imgWidth - 1: | |
heights[j] = uint16(lowest + imgHeight + margin * 2) | |
echo "lowest", lowest, at | |
var srcRect = Rect((x:cint(0), y:cint(0), w:cint(image.w), h:cint(image.h))) | |
var dstRect = Rect((x:cint(at + margin), y:cint(lowest + margin), w:cint(image.w), h:cint(image.h))) | |
blitSurface( | |
image, | |
addr srcRect, | |
atlasImage, | |
addr dstRect | |
) | |
for file in walkDirRec("data"): | |
if file.endsWith(".png"): | |
putImage(file) | |
# proc savePNG*(surface: SurfacePtr, file: cstring): cint {.importc: "IMG_SavePNG".} | |
discard savePNG(atlasImage, "bin/atlas.png") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment