Ticket #123: pic_db_Export_Pic2asm_8bit_Tiles_SP2D_GBA.lua

File pic_db_Export_Pic2asm_8bit_Tiles_SP2D_GBA.lua, 4.8 KB (added by HoraK-FDF@…, 5 years ago)
Line 
1------------------------------------------------------------------------------------------
2-- PICTURE: Export picture to assembler array --
3-- (8-bit GBA SP 2D) --
4-- by HoraK-FDF --
5-- pic_db_Export_Pic2asm_8bit_Tiles_SP2D_GBA.lua --
6------------------------------------------------------------------------------------------
7
8 --- Returns HEX representation of num
9 function num2hex(num)
10 local hexstr = '0123456789abcdef'
11 local s = ''
12 while num > 0 do
13 local mod = math.fmod(num, 16)
14 s = string.sub(hexstr, mod+1, mod+1) .. s
15 num = math.floor(num / 16)
16 end
17 if s == '' then s = '0' end
18 return s
19 end
20
21function replace_char(pos, str, char)
22 if char == nil or char == "" then
23 return str:sub(1, pos-1) .. str:sub(pos+1)
24 else
25 return str:sub(1, pos-1) .. char .. str:sub(pos+1)
26 end
27end
28
29
30function replace_all_chars(str, char, replacechar)
31 tmpstr=str
32 repeat
33 charstart, charend=string.find(tmpstr, char)
34 if charstart ~= nil then
35 tmpstr=replace_char(charstart, tmpstr, replacechar)
36 end
37 until charstart == nil
38 return tmpstr
39end
40
41------------------------------------------------------------------------------
42
43w, h = getpicturesize()
44
45tail="_8bit"
46
47filename, filepath = getfilename()
48path_seperator=package.config:sub(1,1);
49filename_without_ext=string.gsub(filename, "%..-$", "")
50local file = io.open(filepath..path_seperator..filename_without_ext..tail..".raw.s", "w")
51size=h*w
52
53const_name_prefix=replace_all_chars(filename_without_ext, "-", "_")
54const_name_prefix=replace_all_chars(const_name_prefix, "%(", "")
55const_name_prefix=replace_all_chars(const_name_prefix, "%)", "")
56const_name_prefix=replace_all_chars(const_name_prefix, "+", "")
57first_char = const_name_prefix:sub(1,1)
58if string.match (first_char, "%d") ~= nil then
59 const_name_prefix="const_"..const_name_prefix
60end
61
62const_name_prefix=const_name_prefix..tail
63
64-- messagebox("var", const_name_prefix)
65
66file:write("@===============================================================================\n")
67file:write("@ "..const_name_prefix.."_Tiles_Size\n")
68file:write("@===============================================================================\n")
69file:write(".global "..const_name_prefix.."_Tiles_Size\n")
70file:write(".type "..const_name_prefix.."_Tiles_Size, %object\n")
71file:write(".section .rodata\n")
72file:write(".align 1\n")
73file:write(const_name_prefix.."_Tiles_Size:\n")
74file:write(".hword "..size.."\n")
75file:write("\n")
76file:write("@===============================================================================\n")
77file:write("@ "..const_name_prefix.."_Tiles_SizeX\n")
78file:write("@===============================================================================\n")
79file:write(".global "..const_name_prefix.."_Tiles_SizeX\n")
80file:write(".type "..const_name_prefix.."_Tiles_SizeX, %object\n")
81file:write(".section .rodata\n")
82file:write(".align 1\n")
83file:write(const_name_prefix.."_Tiles_SizeX:\n")
84file:write(".hword "..w.."\n")
85file:write("\n")
86file:write("@===============================================================================\n")
87file:write("@ "..const_name_prefix.."_Tiles_SizeY\n")
88file:write("@===============================================================================\n")
89file:write(".global "..const_name_prefix.."_Tiles_SizeY\n")
90file:write(".type "..const_name_prefix.."_Tiles_SizeY, %object\n")
91file:write(".section .rodata\n")
92file:write(".align 1\n")
93file:write(const_name_prefix.."_Tiles_SizeY:\n")
94file:write(".hword "..h.."\n")
95file:write("\n")
96file:write("@===============================================================================\n")
97file:write("@ "..const_name_prefix.."_Tiles\n")
98file:write("@===============================================================================\n")
99file:write(".global "..const_name_prefix.."_Tiles\n")
100file:write(".type "..const_name_prefix.."_Tiles, %object\n")
101file:write(".section .rodata\n")
102file:write(".align 2\n")
103file:write(const_name_prefix.."_Tiles:\n")
104
105colscounter=1
106const_type_written=0
107for y = 0, h - 1 , 8 do
108 for x = 0, w - 1 , 8 do
109 for ty = y, y + 7 , 1 do
110 for tx = x, x + 7 , 1 do
111 col = getpicturepixel(tx, ty)
112 colhex=num2hex(col)
113 ZEROS=""
114 strlen=string.len(colhex)
115 for i = 1, 2-strlen, 1 do --2 = byte
116 ZEROS=ZEROS.."0"
117 end
118 if colscounter == 8 then
119 colscounter=1
120 col2write="0x"..ZEROS..colhex.."\n"
121 const_type_written=0
122 else
123 colscounter=colscounter+1
124 if const_type_written == 0 then
125 const_type_written=1
126 file:write(".byte ")
127 end
128 col2write="0x"..ZEROS..colhex..", "
129 end
130-- messagebox("nOp", col2write)
131 file:write(col2write)
132 end
133 end
134 end
135end
136
137file:close()