-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTilemizer.lua
More file actions
402 lines (370 loc) · 9.61 KB
/
Tilemizer.lua
File metadata and controls
402 lines (370 loc) · 9.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
local byte = string.char
local binfile = nil
local map = {}
local images = { }
local colors = {}
local output_message = ""
local errors = 0
local checkXflips = false
local checkYflips = false
local cmdline = false
--
-- seperate platform from code
--
-- utility to swap nybbles
local function nswap(a)
return (((a & 0xf) << 4) | ((a>>4)&0xf))
end
--
-- 4bpp format with 444 palettes
-- Mega drive compatible
--
local BPP4 = {}
function BPP4.ExportTile( tile )
for y=0,7 do
for xp = 0, 7,2 do
local px0 = tile.pixels[1+((y*8) + xp)] -- stupid off by 1
local px1 = tile.pixels[2+((y*8) + xp)] -- stupid off by 1
px0 = px1 & 0xf | ((px0&0xf)<<4)
io.write(byte(px0))
end
end
end
function BPP4.ExportMap( map , offset )
print("BPP4 map")
for k,v in pairs(map) do
local mindex = (v - 1) + offset
io.write(byte(mindex))
end
end
function BPP4.ExportPal()
for c = 0, #colors-1 do
local index = 0
local color = colors:getColor(c)
index = (color.red>>4)
io.write(byte(index))
index = (color.green&0xf0)
index = index + (color.blue>>4)
io.write(byte(index))
end
end
--
-- Mega 65 format
-- 8bpp with palettes split into RGB channels with nybble swapped
--
local MEGA65 = {}
function MEGA65.ExportPal()
for c = 0, #colors-1 do
local color = colors:getColor(c)
io.write(byte(nswap(color.red)))
end
for c = 0, #colors-1 do
local color = colors:getColor(c)
io.write(byte(nswap(color.green)))
end
for c = 0, #colors-1 do
local color = colors:getColor(c)
io.write(byte(nswap(color.blue)))
end
end
function MEGA65.ExportTile(tile)
for y=0,7 do
for xp = 0, 7 do
local px = tile.pixels[1+((y*8) + xp)] -- stupid off by 1
io.write(byte(px))
end
end
end
function MEGA65.ExportMap(map,offset)
for k,v in pairs(map) do
local mindex = (v - 1) + offset
io.write(byte(mindex&0xff))
io.write(byte((mindex>>8)&0xff))
end
end
--
-- C64 MC mode
-- 2bpp wide pixels
--
local C64 = {
luminance = {0x0,0xf,0x3,0xb,0x5,0x9,0x1,0xd,0x6,0x2,0xa,0x4,0x8,0xe,0x7,0xc,0xff}
}
function C64.ExportMap(map,offset)
for k,v in pairs(map) do
local mindex = (v - 1) + offset
io.write(byte(mindex))
end
end
function C64.ExportTile(tile)
if #tile.colors<=4 then
-- sort the colors table by brightness
-- this seems to be quite nice
table.sort(tile.colors, function(a, b) return C64.luminance[1+(a&0xf)] < C64.luminance[1+(b&0xf)] end)
for y=0,7 do
-- clear the byte we write
local c = 0
-- for the bits we scan every 2 pixels ( MC mode )
-- we assume odd pixels are the same and don't care
for xp = 0, 7, 2 do
-- get the pixel from the tile data
local px = tile.pixels[1+((y*8) + xp)]
-- double check for nil
if px==nil then
px = 0
end
-- output color is an index between 0-#ncolors
local oc = 0
-- check the colors array for this color to find it's index
for ci = 1, #tile.colors do
if tile.colors[ci] == px then
oc = ci-1
break
end
end
-- output the color 0-3 into the byte for this line
c = c | ( oc << (6-xp))
end
-- write the byte out
io.write(byte(c))
end
else
-- kinda raster bar for chars with too many colors
errors = errors + 1
io.write(byte(0x00))
io.write(byte(0x55))
io.write(byte(0xaa))
io.write(byte(0xff))
io.write(byte(0xff))
io.write(byte(0xaa))
io.write(byte(0x55))
io.write(byte(0x00))
end
end
-- We create a string for each char for comparison later
-- note this is the raw 8bpp tile
local function ExtractTile(img, x, y ,tw , th, mask)
local res = {}
res.pixels = {}
res.colors = {}
res.string = ""
res.xflipstring = ""
res.yflipstring = ""
res.xyflipstring = ""
res.flag = 0
local xfpx = 0
local yfpx = 0
local xyfpx = 0
table.insert(res.colors,0)
for cy = 0, th-1 do
for cx = 0, tw-1 do
-- get the pixel
local px = img:getPixel(cx+x, cy+y)
-- store it in the string for comparison
res.string = res.string .. string.format("%02x",px)
-- if we need xflip check
if checkXflips == true then
xfpx = img:getPixel(x + ( tw-1 - cx), cy+y)
res.xflipstring = res.xflipstring .. string.format("%02x",xfpx)
end
-- if we need yflip check
if checkYflips == true then
yfpx = img:getPixel(x + cx , y + ( ( th-1 - cy)))
res.yflipstring = res.yflipstring .. string.format("%02x",yfpx)
end
-- if we need both
if checkXflips == true and checkYflips == true then
xyfpx = img:getPixel(x + ( tw-1 - cx) , y + ( ( th-1 - cy)))
res.xyflipstring = res.xyflipstring .. string.format("%02x",xyfpx)
end
-- mask
px = px & mask
-- check the colors for this tile
local found = -1
for i,v in ipairs(res.colors) do
if px==v then
found = i
break
end
end
-- if we didn't find that color, insert it
if found==-1 then
table.insert(res.colors,px)
found = #res.colors
end
-- insert the pixel itself
table.insert(res.pixels,px)
end
res.string = res.string .. "\n"
res.xflipstring = res.xflipstring .. "\n"
res.yflipstring = res.yflipstring .. "\n"
res.xyflipstring = res.xyflipstring .. "\n"
end
-- check for repeated tiles
for i,v in ipairs(images) do
local found = false
if res.string==v.string then
found = true
else
if checkXflips == true then
if res.string==v.xflipstring then
found = true
res.flag = res.flag | 1
end
end
if checkYflips == true then
if res.string==v.yflipstring then
found = true
res.flag = res.flag | 2
end
end
if checkXflips == true and checkYflips == true then
if res.string==v.xyflipstring then
found = true
res.flag = res.flag | 3
end
end
end
if found==true then
return i -- Return the existent tile index"
end
end
-- we add it and return the index of it.
table.insert(images, res)
return #images
end
--
-- encode the map into a normal x first grid
--
local function EncodeMap(input,tw,th,mask)
local sprite = input.sprite
local img = Image(sprite.spec)
output_message = output_message .. " map " .. (img.width//8) .. " " .. (img.height//8)
img:drawSprite(sprite, input)
colors = sprite.palettes[1]
-- collect the tiles this frame
for y = 0, img.height-1, tw do
for x = 0, img.width-1, th do
table.insert(map,ExtractTile(img, x, y , tw, th,mask))
end
end
end
--
-- functionality
-- encode map and tiles and palette
--
function Tilemizer(type,exportChars,exportMap,exportPal,OffsetTile,Xflips,Yflips)
checkXflips = Xflips
checkYflips = Yflips
if type=="C64" then
if cmdline==true then print("C64 mode") end
System = C64
EncodeMap(app.activeFrame,8,8,0xf)
end
if type=="BPP4" then
if cmdline==true then print("4bpp mode") end
System = BPP4
EncodeMap(app.activeFrame,8,8,0xf)
end
if type=="MEGA65" then
if cmdline==true then print("MEGA65 mode") end
System = MEGA65
EncodeMap(app.activeFrame,8,8,0xff)
end
-- save colors
if System.ExportPal~=nil then
if exportPal~=nil then
if cmdline==true then print("write "..exportPal) end
binfile = io.open(exportPal, "wb")
io.output(binfile)
System.ExportPal()
io.close(binfile)
end
end
-- tiles
if exportChars~=nil then
if cmdline==true then print("write "..exportChars) end
binfile = io.open(exportChars, "wb")
io.output(binfile)
for key,tile in pairs(images) do
System.ExportTile(tile)
end
io.close(binfile)
end
output_message = output_message .. " tiles " .. #images
-- for now no metatiles
-- map
if exportMap~=nil then
if cmdline==true then print("write "..exportMap) end
binfile = io.open(exportMap, "wb")
io.output(binfile)
System.ExportMap(map,OffsetTile)
io.close(binfile)
end
end
cmdline = (app.params["exportChars"]~=nil) or (app.params["exportMap"]~=nil)
if cmdline==true then
-- some default names
local format = app.params["format"] or "MEGA65"
local exportChars = app.params["exportChars"] or "cells.bin"
local exportMap = app.params["exportMap"] or "map.bin"
local exportPal = app.params["exportPal"] or "palette.bin"
local xflip = app.params["xflip"] or false
local yflip = app.params["yflip"] or false
local OffsetTile = app.params["OffsetTile"] or 0
print("Format =" .. format)
print("exportChars =" .. exportChars)
print("exportMap =" .. exportMap)
print("exportPal =" .. exportPal)
if xflip==true then print("xflip reduction") end
if yflip==true then print("yflip reduction") end
print("OffsetTile =" .. OffsetTile)
Tilemizer(format,exportChars,exportMap,exportPal,OffsetTile,xflip,yflip)
else
local dlg = Dialog("Tilemizer")
dlg:file{ id="exportChars",
label="Chars",
title="Char Export",
open=false,
save=true,
filename="chars.bin",
filetypes={"bin"}}
dlg:file{ id="exportMap",
label="Map",
title="Map Export",
open=false,
save=true,
filename="map.bin",
filetypes={"bin"}}
dlg:file{ id="exportPal",
label="Pal",
title="Palette Export",
open=false,
save=true,
filename="pal.bin",
filetypes={"bin"}}
dlg:combobox{ id="TileFormat",
label="TileFormat",
option="C64",
options={ "C64","MEGA65","BPP4"} }
dlg:number{ id="OffsetTile",
label="Offset#",
text="0",
decimals=0 }
dlg:check{ id="xflip",
text="Remove xflipped duplicates",
selected=false}
dlg:check{ id="yflip",
text="Remove yflipped duplicates",
selected=false}
dlg:button{ id="ok", text="OK" }
dlg:show()
local data = dlg.data
if data.ok then
-- ok was pressed
Tilemizer(data.TileFormat,data.exportChars,data.exportMap,data.exportPal,data.OffsetTile,data.xflip,data.yflip)
if errors~=0 then
output_message = output_message .. " Errors " .. errors
end
app.alert(output_message)
end
end