Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
TextuePack Enhancement — Gideros Forum

TextuePack Enhancement

I've created the following class which is a simple TexturePack which you can draw into. It is handy for puzzle games where you might want to generate the blocks using one tile.
--[[
name:   TPack
vers:   1.0.0
desc:   A basic TexturePack that you can modify
auth:   Cliff Earl, Antix Development
date:   July 2020
help:   antix.development@gmail.com
legal:  (C) 2020 Antix Development.
        Permission is hereby granted, free of charge, to any person obtaining a copy of this source code and associated documentation files (the "source code"), to deal in the source code without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the source code, and to permit persons to whom the source code is furnished to do so, subject to the following conditions:
        The above copyright notice and this permission notice shall be included in all copies or substantial portions of the source code.
        THIS SOURCE CODE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOURCE CODE OR THE USE OR OTHER DEALINGS IN THE SOURCE CODE.
--]]
 
--- A basic TexturePack that you can modify
-- <a href="https://forum.gideros.rocks/profile/classmod" rel="nofollow">@classmod</a> TPack
-- <a href="https://forum.gideros.rocks/profile/warning" rel="nofollow">@warning</a> Does not support offsets created by "Remove Alpha Borders" in Gideros TexturePacker.
TPack = Core.class()
 
--- init constructor
-- Create a new TPack
-- <a href="https://forum.gideros.rocks/profile/param" rel="nofollow">@param</a> textFile (string) ascii file containing sub-image file names and coordinates within the texture atlas
-- <a href="https://forum.gideros.rocks/profile/param" rel="nofollow">@param</a> textureName (string) Name of texture atlas
function TPack:init(textFile, textureFile)
 
  local function split(s, d) local f = {} local pattern = string.format("([^%s]+)", d) string.gsub(s, pattern, function(c) f[#f + 1] = c end) return f end
 
  local regions = {}
  self.regions = regions
 
  -- Load and draw texture into new RenderTarget
  local texture = Texture.new(textureFile)
  local canvas = RenderTarget.new(texture:getWidth(), texture:getHeight())
  local brush = Bitmap.new(texture)
  brush:setPosition(0, 0)
  canvas:draw(brush)
  brush = nil
  self.canvas = canvas
  self.texture = texture
 
  -- Load text file and process all lines (sub-image files)
  local file = io.open(textFile)
  while true do
    local line = file:read()
    if line == nil then break end
      local f = split(line, ',') -- Split line into component parts
      --       name                                x     y     w     h
      regions[f[1]] = TextureRegion.new(texture, f[2], f[3], f[4], f[5]) -- Add new region
  end
end
 
--- getTextureRegion
-- Get TextureRegion for given sub-image name.
-- <a href="https://forum.gideros.rocks/profile/param" rel="nofollow">@param</a> name (string) sub-image name
-- <a href="https://forum.gideros.rocks/profile/return" rel="nofollow">@return</a> TextureRegion
function TPack:getTextureRegion(name)
  local regions = self.regions
  return regions[name]
end
 
--- draw
-- draw a pre-positioned Sprite to the canvas
-- <a href="https://forum.gideros.rocks/profile/param" rel="nofollow">@param</a> brush (Sprite) Sprite to be drawn
function TPack:draw(brush)
  local canvas = self.canvas
  canvas:clear(0, 0, brush:getX(), brush:getY(), brush:getWidth(), brush:getHeight())
  canvas:draw(brush)
end
Would it be possible for Gideros to have this kind of functionality built in?

Comments

Sign In or Register to comment.