summaryrefslogtreecommitdiff
path: root/examples/atlas_packer/main.lua
diff options
context:
space:
mode:
Diffstat (limited to 'examples/atlas_packer/main.lua')
-rw-r--r--examples/atlas_packer/main.lua69
1 files changed, 69 insertions, 0 deletions
diff --git a/examples/atlas_packer/main.lua b/examples/atlas_packer/main.lua
new file mode 100644
index 0000000..88367bf
--- /dev/null
+++ b/examples/atlas_packer/main.lua
@@ -0,0 +1,69 @@
+package.path = package.path..";"..RL.GetBasePath().."../resources/lib/?.lua"
+
+Util = require( "utillib" )
+Vector2 = require( "vector2" )
+Rectangle = require( "rectangle" )
+
+local atlasTexture = nil
+local atlasRects = {}
+local atlasSize = Vector2:new()
+local atlasPadding = 1
+local imgPaths = {
+ "tile.png",
+ "apple.png",
+ "snake.png",
+ "wabbit_alpha.png",
+ "nPatch.png",
+ "open-folder.png",
+ "cat.png",
+ "tiles.png",
+ "monkey_tex.png",
+}
+
+local function makeAtlas()
+ local images = {}
+ local pathPrefix = RL.GetBasePath().."../resources/images/"
+
+ for i, path in ipairs( imgPaths ) do
+ images[i] = RL.LoadImage( pathPrefix..path )
+ local imgSize = Vector2:newT( RL.GetImageSize( images[i] ) )
+ atlasRects[i] = Rectangle:new( 0, 0, imgSize.x, imgSize.y )
+ end
+
+ atlasSize:setT( RL.GetScreenSize() )
+ atlasRects = RL.RectPack( atlasRects, atlasSize, atlasPadding )
+
+ local atlasImg = RL.GenImageColor( atlasSize, RL.BLANK )
+
+ for i, rect in ipairs( atlasRects ) do
+ print( "Rect", i, Rectangle:tempT( rect ) )
+ RL.ImageDraw( atlasImg, images[i],
+ { 0, 0, rect[3], rect[4] },
+ rect,
+ RL.WHITE
+ )
+ end
+
+ RL.ImageAlphaCrop( atlasImg, 0 )
+ print( "Cropped size", Vector2:tempT( RL.GetImageSize( atlasImg ) ) )
+
+ atlasTexture = RL.LoadTextureFromImage( atlasImg )
+end
+
+function RL.init()
+ RL.SetWindowTitle( "Texture Atlas" )
+ RL.SetWindowState( RL.FLAG_VSYNC_HINT )
+
+ makeAtlas()
+end
+
+function RL.update( delta )
+end
+
+function RL.draw()
+ RL.ClearBackground( RL.DARKBLUE )
+
+ if atlasTexture then
+ RL.DrawTexture( atlasTexture, { 0, 0 }, RL.WHITE )
+ end
+end