summaryrefslogtreecommitdiff
path: root/examples/atlas_packer/main.lua
blob: 88367bfc360d9d3c9725329b3f9528b72f528f93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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