Advertisement
joseleeph

Untitled

Jan 4th, 2021
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.01 KB | None | 0 0
  1. 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
  2. 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
  3. 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
  4.  
  5. local sheetCounter = 1 -- everything is 1 indexed in lua
  6. 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
  7.  
  8. for y = 0, sheetHeight - 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
  9. for x = 0, sheetWidth - 1 do
  10. quads[sheetCounter] = love.graphics.newQuad(x*tilewidth, y*tileheight, tilewidth, tileheight, atlas:getDimentions())
  11. -- quads() a love.graphics function. it expects an x, y, width, height, and dimentions of texture atlas
  12. -- using x and y is 0 based. we need to find where the sub part of the texture's coordinates begin
  13. -- atlas:getDimentions() is a necessary requisite to use the quads function: it expects the dimentions of the texture following the declaration of the quad
  14. -- 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
  15. -- offset by counter
  16. end
  17. end
  18. 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
  19. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement