Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function generateQuads(atlas, tilewidth, tileheight) -- a texture atlas is like a spritesheet, but sometimes has stored information where you may not need to rely on grid based layout
- local sheetWidth = atlas:getWidth()/tilewidth -- getwidth will be in pixels and so will the tile width and the sheetwideth will be how many tiles whide the sheet is
- local sheetHeight = atlas:getHeight()/tileheight -- same thing with tileheight, local means the variables won't be accesible anywhere outside the function, like in main.lua
- local sheetCounter = 1 -- everything is 1 indexed in lua
- local quads = {} -- a table that will store all the quads we create. a rectangle that represents a chunk of a texture. like an array or a list. we are storing data in the array chunk by chunk
- for y = 0, - 1 do -- iterate through the texture. goes from 0-3. we are starting at 0 because we want to specify the quad in pixels on the spritesheet. the pixel specification is 0 index based. array indexes are 1 based
- for x = 0, sheetWidth - 1 do
- quads[sheetCounter] = love.graphics.newQuad(x*tilewidth, y*tileheight, tilewidth, tileheight, atlas:getDimensions())
- -- quads() a love.graphics function. it expects an x, y, width, height, and dimentions of texture atlas
- -- using x and y is 0 based. we need to find where the sub part of the texture's coordinates begin
- -- atlas:getDimensions() is a necessary requisite to use the quads function: it expects the dimentions of the texture following the declaration of the quad
- -- we start at x = 1, y = 1, the first tile at the top left. it needs to start at 0, 0, and grab 16 pixels down and to the right
- -- offset by counter
- end
- end
- return quads-- the quads table has been assembled. it must now be returned. we can now call this from anywhere in the program as long as "require 'Util'" is included at the top of the file. we will use 'Util' in map so it must be called before map
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement