Skip to content
Snippets Groups Projects
Tiles.lua 1.77 KiB
Newer Older
  • Learn to ignore specific revisions
  • dg's avatar
    dg committed
    require "engine.class"
    
    
    dg's avatar
    dg committed
    --- Handles tiles
    -- Used by engine.Map to reduce processing needed. Module authors wont use it directly mostly.
    
    dg's avatar
    dg committed
    module(..., package.seeall, class.make)
    
    
    dg's avatar
    dg committed
    prefix = "/data/gfx/"
    use_images = true
    
    
    dg's avatar
    dg committed
    function _M:init(w, h, fontname, fontsize, texture)
    	self.texture = texture
    
    dg's avatar
    dg committed
    	self.w, self.h = w, h
    
    dg's avatar
    dg committed
    	self.font = core.display.newFont(fontname or "/data/font/VeraMoBd.ttf", fontsize or 14)
    
    dg's avatar
    dg committed
    	self.repo = {}
    end
    
    
    dg's avatar
    dg committed
    function _M:get(char, fr, fg, fb, br, bg, bb, image, alpha)
    
    dg's avatar
    dg committed
    	alpha = alpha or 0
    	local dochar = char
    
    dg's avatar
    dg committed
    	local fgidx = 65536 * fr + 256 * fg + fb
    
    dg's avatar
    dg committed
    	local bgidx
    	if br >= 0 and bg >= 0 and bb >= 0 then
    
    dg's avatar
    dg committed
    		bgidx = 65536 * br + 256 * bg + bb
    
    dg's avatar
    dg committed
    	else
    		bgidx = "none"
    	end
    
    dg's avatar
    dg committed
    
    	if self.use_images and image then char = image end
    
    
    dg's avatar
    dg committed
    	if self.repo[char] and self.repo[char][fgidx] and self.repo[char][fgidx][bgidx] then
    		return self.repo[char][fgidx][bgidx]
    	else
    
    dg's avatar
    dg committed
    		local s
    		if self.use_images and image then
    
    dg's avatar
    dg committed
    			print("Loading tile", image)
    
    dg's avatar
    dg committed
    			s = core.display.loadImage(self.prefix..image)
    
    dg's avatar
    dg committed
    		end
    		if not s then
    			local w, h = self.font:size(dochar)
    --[[
    
    dg's avatar
    dg committed
    			s = core.display.newSurface(self.w, self.h)
    			if br >= 0 then
    
    dg's avatar
    dg committed
    				s:erase(br, bg, bb, alpha)
    			else
    				s:erase(0, 0, 0, alpha)
    
    dg's avatar
    dg committed
    			end
    
    dg's avatar
    dg committed
    
    
    dg's avatar
    dg committed
    			s:drawString(self.font, char, (self.w - w) / 2, (self.h - h) / 2, fr, fg, fb)
    
    dg's avatar
    dg committed
    ]]
    			if br < 0 then br = nil end
    			if bg < 0 then bg = nil end
    			if bb < 0 then bb = nil end
    			s = core.display.newTile(self.w, self.h, self.font, dochar, (self.w - w) / 2, (self.h - h) / 2, fr, fg, fb, br or 0, bg or 0, bb or 0, alpha)
    --			s = core.display.drawStringNewSurface(self.font, char, fr, fg, fb)
    
    dg's avatar
    dg committed
    		end
    
    dg's avatar
    dg committed
    
    
    dg's avatar
    dg committed
    		if self.texture then s = s:glTexture() end
    
    
    dg's avatar
    dg committed
    		self.repo[char] = self.repo[char] or {}
    		self.repo[char][fgidx] = self.repo[char][fgidx] or {}
    		self.repo[char][fgidx][bgidx] = s
    		return s
    	end
    end