Commit f522f687ac5f3bd76c46133649fff0e01333665c

Authored by DarkGod
1 parent c77d4379

check if tilesets are usable

... ... @@ -36,7 +36,13 @@ function _M:loadTileset(file)
36 36 local f, err = loadfile(file)
37 37 if err then error(err) end
38 38 local env = {}
39   - setfenv(f, setmetatable(self.tilesets, {__index={_G=self.tilesets}}))
  39 + local ts = {}
  40 + setfenv(f, setmetatable(self.tilesets, {__index={_G=ts}}))
  41 + if ts.__width > core.display.glMaxTextureSize() or ts.__height > core.display.glMaxTextureSize() then
  42 + print("[TILESET] Refusing tileset "..file.." due to texture size "..ts.__width.."x"..ts.__height.." over max of "..core.display.glMaxTextureSize())
  43 + return
  44 + end
  45 + for k, e in pairs(ts) do self.tilesets[k] = e end
40 46 local ok, err = pcall(f)
41 47 if not ok then error(err) end
42 48 end
... ...
... ... @@ -260,12 +260,11 @@ os.exit()
260 260 --]]
261 261
262 262 -- Load tilesets, to speed up image loads
263   -Tiles:loadTileset("/data/gfx/ts-shockbolt-npc.lua")
264   -Tiles:loadTileset("/data/gfx/ts-shockbolt-object.lua")
265   -Tiles:loadTileset("/data/gfx/ts-shockbolt-trap.lua")
266   -Tiles:loadTileset("/data/gfx/ts-shockbolt-terrain.lua")
267   -Tiles:loadTileset("/data/gfx/ts-talents.lua")
268   -Tiles:loadTileset("/data/gfx/ts-effects.lua")
  263 +Tiles:loadTileset("/data/gfx/ts-npc.lua")
  264 +Tiles:loadTileset("/data/gfx/ts-object.lua")
  265 +Tiles:loadTileset("/data/gfx/ts-trap.lua")
  266 +Tiles:loadTileset("/data/gfx/ts-terrain.lua")
  267 +Tiles:loadTileset("/data/gfx/ts-talents-effects.lua")
269 268
270 269 -- Factions
271 270 dofile("/data/factions.lua")
... ...
... ... @@ -3093,6 +3093,10 @@ static int display_pause_anims(lua_State *L) {
3093 3093 return 0;
3094 3094 }
3095 3095
  3096 +static int gl_get_max_texture_size(lua_State *L) {
  3097 + lua_pushnumber(L, max_texture_size);
  3098 + return 1;
  3099 +}
3096 3100
3097 3101 /**************************************************************
3098 3102 * Vertex Objects
... ... @@ -3287,6 +3291,7 @@ static const struct luaL_Reg displaylib[] =
3287 3291 {"glDepthTest", gl_depth_test},
3288 3292 {"glScissor", gl_scissor},
3289 3293 {"getScreenshot", sdl_get_png_screenshot},
  3294 + {"glMaxTextureSize", gl_get_max_texture_size},
3290 3295 {NULL, NULL},
3291 3296 };
3292 3297
... ...
  1 +function table.print(src, offset, ret)
  2 + if type(src) ~= "table" then print("table.print has no table:", src) return end
  3 + offset = offset or ""
  4 + for k, e in pairs(src) do
  5 + -- Deep copy subtables, but not objects!
  6 + if type(e) == "table" and not e.__ATOMIC then
  7 + print(("%s[%s] = {"):format(offset, tostring(k)))
  8 + table.print(e, offset.." ")
  9 + print(("%s}"):format(offset))
  10 + else
  11 + print(("%s[%s] = %s"):format(offset, tostring(k), tostring(e)))
  12 + end
  13 + end
  14 +end
  15 +--- This is a really naive algorithm, it will not handle objects and such.
  16 +-- Use only for small tables
  17 +function table.serialize(src, sub, no_G)
  18 + local str = ""
  19 + if sub then str = "{" end
  20 + for k, e in pairs(src) do
  21 + local nk, ne = k, e
  22 + local tk, te = type(k), type(e)
  23 +
  24 + if no_G then
  25 + if tk == "table" then nk = "["..table.serialize(nk, true).."]"
  26 + elseif tk == "string" then -- nothing
  27 + else nk = "["..nk.."]"
  28 + end
  29 + else
  30 + if tk == "table" then nk = "["..table.serialize(nk, true).."]"
  31 + elseif tk == "string" then nk = string.format("[%q]", nk)
  32 + else nk = "["..nk.."]"
  33 + end
  34 + if not sub then nk = "_G"..nk end
  35 + end
  36 +
  37 + if te == "table" then
  38 + str = str..string.format("%s=%s ", nk, table.serialize(ne, true))
  39 + elseif te == "number" then
  40 + str = str..string.format("%s=%f ", nk, ne)
  41 + elseif te == "string" then
  42 + str = str..string.format("%s=%q ", nk, ne)
  43 + elseif te == "boolean" then
  44 + str = str..string.format("%s=%s ", nk, tostring(ne))
  45 + end
  46 + if sub then str = str..", " end
  47 + end
  48 + if sub then str = str.."}" end
  49 + return str
  50 +end
  51 +
  52 +
  53 +local gd = require "gd"
  54 +
  55 +local ts_size = 512
  56 +
  57 +function makeSet(w, h)
  58 + local used = {}
  59 + local im = gd.createTrueColor(w, h)
  60 + im:alphaBlending(false)
  61 + im:saveAlpha(true)
  62 + im:filledRectangle(0, 0, w, h, im:colorAllocateAlpha(0, 0, 0, 127))
  63 +
  64 + for i = 0, ts_size-1, 1 do
  65 + used[i] = {}
  66 + for j = 0, ts_size-1, 1 do
  67 + used[i][j] = false
  68 + end
  69 + end
  70 +
  71 + return im, used
  72 +end
  73 +
  74 +local w, h = ts_size, ts_size
  75 +local id = 1
  76 +
  77 +local pos = {}
  78 +
  79 +local list = {...}
  80 +local basename = table.remove(list, 1)
  81 +local prefix = table.remove(list, 1)
  82 +
  83 +function findPlace(used, d)
  84 + for i = 0, #used do
  85 + for j = 0, #used[i] do if not used[i][j] then
  86 + -- print("Spot", i, j)
  87 + local ok = true
  88 + for a = i, i + d.sw - 1 do for b = j, j + d.sh - 1 do
  89 + -- print("SubSpot", a, b)
  90 + if not used[a] or used[a][b] == true or used[a][b] == nil then ok = false end
  91 + end end
  92 + if ok then return i, j end
  93 + end end
  94 + end
  95 +end
  96 +
  97 +function fillSet(rlist)
  98 + local im, used = makeSet(w, h)
  99 + local i, j = 0, 0
  100 + while #rlist > 0 do
  101 + local d = table.remove(rlist)
  102 + print("SRC", d.file, d.mw, d.mh, d.sw, d.sh)
  103 +
  104 + local i, j = findPlace(used, d)
  105 + if not i then
  106 + im:png(basename..id..".png")
  107 + im, used = makeSet(w, h)
  108 + i, j = findPlace(used, d)
  109 + id = id + 1
  110 + end
  111 +
  112 + local ri, rj = i, j
  113 + im:copyResampled(d.src, ri, rj, 0, 0, d.mw, d.mh, d.mw, d.mh)
  114 + pos[prefix..d.file] = {x=ri/w, y=rj/h, factorx=d.mw/w, factory=d.mh/h, w=d.mw, h=d.mh, set=prefix..basename..id..".png"}
  115 +
  116 + for x = i, i - 1 + d.sw do for y = j, j - 1 + d.sh do
  117 + used[x][y] = true
  118 + end end
  119 +
  120 + i = i + d.mw
  121 + end
  122 + -- table.print(used)
  123 + im:png(basename..id..".png")
  124 +end
  125 +
  126 +local total = 0
  127 +
  128 +local rlist = {}
  129 +for i = #list, 1, -1 do
  130 + local file = list[i]
  131 + if file:sub(1, 2) == "./" then file = file:sub(3) end
  132 +
  133 + local src = gd.createFromPng(file)
  134 + local mw, mh = src:sizeXY()
  135 + rlist[#rlist+1] = {file=file, src=src, mw=mw, mh=mh, sw=mw+4, sh=mh+4}
  136 + table.remove(list, i)
  137 +end
  138 +table.sort(rlist, function(a,b)
  139 + return a.file < b.file
  140 + -- local ai, bi = a.mw + a.mh, b.mw + b.mh
  141 + -- if ai == bi then return a.file < b.file end
  142 + -- return ai < bi
  143 +end)
  144 +
  145 +total = total + #rlist
  146 +fillSet(rlist)
  147 +
  148 +-- Missing ones ?
  149 +for i = #list, 1, -1 do
  150 + local file = list[i]
  151 + if file:sub(1, 2) == "./" then file = file:sub(3) end
  152 +
  153 + local src = gd.createFromPng(file)
  154 + local mw, mh = src:sizeXY()
  155 + print("Missed: ", mw, mh, file)
  156 +end
  157 +
  158 +local f = io.open(basename..".lua", "w")
  159 +f:write(table.serialize(pos))
  160 +f:close()
  161 +
  162 +print("Total for", basename, " contains ", total, "images")
... ...
... ... @@ -52,7 +52,10 @@ end
52 52
53 53 local gd = require "gd"
54 54
55   -local ts_size_w, ts_size_h = 1024 * 2, 1024 * 2
  55 +local list = {...}
  56 +local ts_size_w, ts_size_h = tonumber(table.remove(list, 1)) or 1024, tonumber(table.remove(list, 1)) or 1024
  57 +local basename = table.remove(list, 1)
  58 +local prefix = table.remove(list, 1)
56 59
57 60 function makeSet(w, h)
58 61 local used = {}
... ... @@ -75,10 +78,8 @@ local w, h = ts_size_w, ts_size_h
75 78 local id = 1
76 79
77 80 local pos = {}
78   -
79   -local list = {...}
80   -local basename = table.remove(list, 1)
81   -local prefix = table.remove(list, 1)
  81 +pos.__width = w
  82 +pos.__height = h
82 83
83 84 function findPlace(used, d)
84 85 for i = 0, #used do
... ...
1 1 #!/bin/bash
2 2
3 3 cd game/modules/tome/data/gfx/
4   -rm -f ts-shockbolt*
5   -lua ../../../../../utils/tileset-maker.lua ts-shockbolt-terrain /data/gfx/ shockbolt/invis.png `find shockbolt/terrain/ -name '*png'`
6   -lua ../../../../../utils/tileset-maker.lua ts-shockbolt-npc /data/gfx/ `find shockbolt/npc/ -name '*png'`
7   -lua ../../../../../utils/tileset-maker.lua ts-shockbolt-object /data/gfx/ `find shockbolt/object/ -name '*png'`
8   -lua ../../../../../utils/tileset-maker.lua ts-shockbolt-trap /data/gfx/ `find shockbolt/trap/ -name '*png'`
9   -lua ../../../../../utils/tileset-maker.lua ts-talents /data/gfx/ `find talents/ -name '*png'`
10   -lua ../../../../../utils/tileset-maker.lua ts-effects /data/gfx/ `find effects/ stats/ -name '*png'`
  4 +rm -f ts-*
  5 +lua ../../../../../utils/tileset-maker.lua 4096 4096 ts-terrain /data/gfx/ shockbolt/invis.png `find shockbolt/terrain/ -name '*png'`
  6 +lua ../../../../../utils/tileset-maker.lua 2048 2048 ts-npc /data/gfx/ `find shockbolt/npc/ -name '*png'`
  7 +lua ../../../../../utils/tileset-maker.lua 2048 2048 ts-object /data/gfx/ `find shockbolt/object/ -name '*png'`
  8 +lua ../../../../../utils/tileset-maker.lua 2048 2048 ts-trap /data/gfx/ `find shockbolt/trap/ -name '*png'`
  9 +lua ../../../../../utils/tileset-maker.lua 4096 2048 ts-talents-effects /data/gfx/ `find talents/ -name '*png'` `find effects/ stats/ -name '*png'`
... ...
  1 +#!/bin/bash
  2 +
  3 +cd game/engines/default/data/gfx
  4 +rm -f ts-*
  5 +lua ../../../../../utils/tileset-maker-precise.lua ts-metal-ui /data/gfx/ `find metal-ui/ -name '*png'`
... ...