-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.lua
More file actions
97 lines (74 loc) · 2.49 KB
/
Copy pathmain.lua
File metadata and controls
97 lines (74 loc) · 2.49 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
LoadLibrary("Renderer")
LoadLibrary("Sprite")
LoadLibrary("System")
LoadLibrary("Texture")
LoadLibrary("Asset")
Asset.Run("example_map.lua")
gTiledMap = CreateMap1()
function GenerateUVs(tileWidth, tileHeight, texture)
-- This is the table we'll fill with uvs and return.
local uvs = {}
local textureWidth = texture:GetWidth()
local textureHeight = texture:GetHeight()
local width = tileWidth / textureWidth
local height = tileHeight / textureHeight
local cols = textureWidth / tileWidth
local rows = textureHeight / tileHeight
local ux = 0
local uy = 0
local vx = width
local vy = height
for j = 0, rows - 1 do
for i = 0, cols -1 do
table.insert(uvs, {ux, uy, vx, vy})
-- Advance the UVs to the next column
ux = ux + width
vx = vx + width
end
-- Put the UVs back to the start of the next row
ux = 0
vx = width
uy = uy + height
vy = vy + height
end
return uvs
end
-- Create the UVs from the first tileset
gTextureAtlas = Texture.Find(gTiledMap.tilesets[1].image)
gUVs = GenerateUVs(
gTiledMap.tilesets[1].tilewidth,
gTiledMap.tilesets[1].tileheight,
gTextureAtlas)
-- Print out the UVs, for checking
-- for k, v in ipairs(gUVs) do
-- print(k, string.format("{%.f, %.f, %.f, %.f}", v[1], v[2], v[3], v[4]))
-- end
gDisplayWidth = System.ScreenWidth()
gDisplayHeight = System.ScreenHeight()
function GetTile(map, rowsize, x, y)
x = x + 1 -- change from 1 -> rowsize
-- to 0 -> rowsize - 1
return map[x + y * rowsize]
end
gRenderer = Renderer.Create()
gTileSprite = Sprite.Create()
gTileSprite:SetTexture(gTextureAtlas)
gMap = gTiledMap.layers[1]
gMapHeight = gMap.height
gMapWidth = gMap.width
gTileWidth = gTiledMap.tilesets[1].tilewidth
gTileHeight = gTiledMap.tilesets[1].tileheight
gTiles = gMap.data
gTop = gDisplayHeight / 2 - gTileHeight / 2
gLeft = -gDisplayWidth / 2 + gTileWidth / 2
function update()
for j = 0, gMapHeight - 1 do
for i = 0, gMapWidth - 1 do
local tile = GetTile(gTiles, gMapWidth, i, j)
local uvs = gUVs[tile]
gTileSprite:SetUVs(unpack(uvs))
gTileSprite:SetPosition(gLeft + i * gTileWidth, gTop - j * gTileHeight)
gRenderer:DrawSprite(gTileSprite)
end
end
end