Skip to content
Snippets Groups Projects
Commit c4b83607 authored by dg's avatar dg
Browse files

Added support for grouping images into tilesets for faster loading

git-svn-id: http://svn.net-core.org/repos/t-engine4@3674 51575b47-30f0-44d4-a5cc-537603b46e54
parent a2ba1a51
No related branches found
No related tags found
No related merge requests found
Showing
with 51 additions and 21 deletions
......@@ -253,7 +253,8 @@ function _M:makeMapObject(tiles, idx)
self._mo:tint(self.tint_r, self.tint_g, self.tint_b)
-- Texture 0 is always the normal image/ascii tile
self._mo:texture(0, tiles:get(self.display, self.color_r, self.color_g, self.color_b, self.color_br, self.color_bg, self.color_bb, self.image, self._noalpha and 255, self.ascii_outline))
local tex, texx, texy, pos_x, pos_y = tiles:get(self.display, self.color_r, self.color_g, self.color_b, self.color_br, self.color_bg, self.color_bb, self.image, self._noalpha and 255, self.ascii_outline, true)
self._mo:texture(0, tex, false, texx, texy, pos_x, pos_y)
-- Additional MO chained to the same Z order
if tiles.use_images and self.add_mos then
......@@ -262,7 +263,8 @@ function _M:makeMapObject(tiles, idx)
local amo = self.add_mos[i]
-- Create a simple additional chained MO
local mo = core.map.newObject(self.uid, 1, false, false, false, amo.display_x or 0, amo.display_y or 0, amo.display_w or 1, amo.display_h or 1, amo.display_scale or 1)
mo:texture(0, tiles:get("", 0, 0, 0, 0, 0, 0, amo.image, false, false))
tex, texx, texy, pos_x, pos_y = tiles:get("", 0, 0, 0, 0, 0, 0, amo.image, false, false, true)
mo:texture(0, tex, false, texx, texy, pos_x, pos_y)
cmo:chain(mo)
cmo = mo
end
......@@ -272,9 +274,9 @@ function _M:makeMapObject(tiles, idx)
if tiles.use_images and self.textures then
for i = 1, #self.textures do
local t = self.textures[i]
if type(t) == "function" then local tex, is3d = t(self, tiles); if tex then self._mo:texture(i, tex, is3d) tiles.texture_store[tex] = true end
if type(t) == "function" then local tex, is3d = t(self, tiles); if tex then self._mo:texture(i, tex, is3d, 1, 1) tiles.texture_store[tex] = true end
elseif type(t) == "table" then
if t[1] == "image" then local tex = tiles:get('', 0, 0, 0, 0, 0, 0, t[2]); self._mo:texture(i, tex, false) tiles.texture_store[tex] = true
if t[1] == "image" then local tex = tiles:get('', 0, 0, 0, 0, 0, 0, t[2]); self._mo:texture(i, tex, false, 1, 1) tiles.texture_store[tex] = true
end
end
end
......
......@@ -28,6 +28,17 @@ base_prefix = "/data/gfx/"
use_images = true
force_back_color = nil
tilesets = {}
tilesets_texs = {}
function _M:loadTileset(file)
local f, err = loadfile(file)
if err then error(err) end
local env = {}
setfenv(f, setmetatable(self.tilesets, {__index={_G=self.tilesets}}))
local ok, err = pcall(f)
if not ok then error(err) end
end
function _M:init(w, h, fontname, fontsize, texture, allow_backcolor)
self.allow_backcolor = allow_backcolor
self.texture = texture
......@@ -43,7 +54,20 @@ function _M:loadImage(image)
return s
end
function _M:get(char, fr, fg, fb, br, bg, bb, image, alpha, do_outline)
function _M:checkTileset(f)
if not self.tilesets[f] then return end
local d = self.tilesets[f]
-- print("Loading tile from tileset", f)
local tex = self.tilesets_texs[d.set]
if not tex then
tex = core.display.loadImage(d.set):glTexture()
self.tilesets_texs[d.set] = tex
print("Loading tileset", d.set)
end
return tex, d.factorx, d.factory, d.x, d.y
end
function _M:get(char, fr, fg, fb, br, bg, bb, image, alpha, do_outline, allow_tileset)
if self.force_back_color then br, bg, bb, alpha = self.force_back_color.r, self.force_back_color.g, self.force_back_color.b, self.force_back_color.a end
alpha = alpha or 0
......@@ -59,11 +83,21 @@ function _M:get(char, fr, fg, fb, br, bg, bb, image, alpha, do_outline)
if (self.use_images or not dochar) and image then char = image end
if self.repo[char] and self.repo[char][fgidx] and self.repo[char][fgidx][bgidx] then
return self.repo[char][fgidx][bgidx]
local s = self.repo[char][fgidx][bgidx]
return s[1], s[2], s[3], s[4], s[5]
else
local s
local s, sw, sh
local is_image = false
if (self.use_images or not dochar) and image then
if allow_tileset and self.texture then
local ts, fx, fy, tsx, tsy = self:checkTileset(self.prefix..image)
if ts then
self.repo[char] = self.repo[char] or {}
self.repo[char][fgidx] = self.repo[char][fgidx] or {}
self.repo[char][fgidx][bgidx] = {ts, fx, fy, tsx, tsy}
return ts, fx, fy, tsx, tsy
end
end
print("Loading tile", image)
s = core.display.loadImage(self.prefix..image)
if not s then s = core.display.loadImage(self.base_prefix..image) end
......@@ -71,26 +105,17 @@ function _M:get(char, fr, fg, fb, br, bg, bb, image, alpha, do_outline)
end
if not s then
local w, h = self.font:size(dochar)
--[[
s = core.display.newSurface(self.w, self.h)
if br >= 0 then
s:erase(br, bg, bb, alpha)
else
s:erase(0, 0, 0, alpha)
end
s:drawString(self.font, char, (self.w - w) / 2, (self.h - h) / 2, fr, fg, fb)
]]
if not self.allow_backcolor or br < 0 then br = nil end
if not self.allow_backcolor or bg < 0 then bg = nil end
if not self.allow_backcolor or bb < 0 then bb = nil end
if not self.allow_backcolor then alpha = 0 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, self.use_images)
-- s = core.display.drawStringNewSurface(self.font, char, fr, fg, fb)
end
if self.texture then
s = s:glTexture()
local w, h = s:getSize()
s, sw, sh = s:glTexture()
sw, sh = w / sw, h / sh
if not is_image and do_outline then
if type(do_outline) == "boolean" then
s = s:makeOutline(2, 2, self.w, self.h, 0, 0, 0, 1) or s
......@@ -98,12 +123,14 @@ function _M:get(char, fr, fg, fb, br, bg, bb, image, alpha, do_outline)
s = s:makeOutline(do_outline.x, do_outline.y, self.w, self.h, do_outline.r, do_outline.g, do_outline.b, do_outline.a) or s
end
end
else
sw, sh = s:getSize()
end
self.repo[char] = self.repo[char] or {}
self.repo[char][fgidx] = self.repo[char][fgidx] or {}
self.repo[char][fgidx][bgidx] = s
return s
self.repo[char][fgidx][bgidx] = {s, sw, sh}
return s, sw, sh
end
end
......
Source diff could not be displayed: it is too large. Options to address this: view the blob.
game/modules/tome/data/gfx/ts-shockbolt-all1.png

384 KiB

game/modules/tome/data/gfx/ts-shockbolt-all10.png

305 KiB

game/modules/tome/data/gfx/ts-shockbolt-all11.png

334 KiB

game/modules/tome/data/gfx/ts-shockbolt-all12.png

370 KiB

game/modules/tome/data/gfx/ts-shockbolt-all13.png

479 KiB

game/modules/tome/data/gfx/ts-shockbolt-all14.png

467 KiB

game/modules/tome/data/gfx/ts-shockbolt-all15.png

470 KiB

game/modules/tome/data/gfx/ts-shockbolt-all16.png

451 KiB

game/modules/tome/data/gfx/ts-shockbolt-all17.png

513 KiB

game/modules/tome/data/gfx/ts-shockbolt-all18.png

396 KiB

game/modules/tome/data/gfx/ts-shockbolt-all19.png

313 KiB

game/modules/tome/data/gfx/ts-shockbolt-all2.png

325 KiB

game/modules/tome/data/gfx/ts-shockbolt-all20.png

427 KiB

game/modules/tome/data/gfx/ts-shockbolt-all21.png

228 KiB

game/modules/tome/data/gfx/ts-shockbolt-all3.png

350 KiB

game/modules/tome/data/gfx/ts-shockbolt-all4.png

324 KiB

game/modules/tome/data/gfx/ts-shockbolt-all5.png

232 KiB

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment