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 |
|
---|
21 | function 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
|
---|
27 | end
|
---|
28 |
|
---|
29 |
|
---|
30 | function 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
|
---|
39 | end
|
---|
40 |
|
---|
41 | ------------------------------------------------------------------------------
|
---|
42 |
|
---|
43 | w, h = getpicturesize()
|
---|
44 |
|
---|
45 | tail="_8bit"
|
---|
46 |
|
---|
47 | filename, filepath = getfilename()
|
---|
48 | path_seperator=package.config:sub(1,1);
|
---|
49 | filename_without_ext=string.gsub(filename, "%..-$", "")
|
---|
50 | local file = io.open(filepath..path_seperator..filename_without_ext..tail..".raw.s", "w")
|
---|
51 | size=h*w
|
---|
52 |
|
---|
53 | const_name_prefix=replace_all_chars(filename_without_ext, "-", "_")
|
---|
54 | const_name_prefix=replace_all_chars(const_name_prefix, "%(", "")
|
---|
55 | const_name_prefix=replace_all_chars(const_name_prefix, "%)", "")
|
---|
56 | const_name_prefix=replace_all_chars(const_name_prefix, "+", "")
|
---|
57 | first_char = const_name_prefix:sub(1,1)
|
---|
58 | if string.match (first_char, "%d") ~= nil then
|
---|
59 | const_name_prefix="const_"..const_name_prefix
|
---|
60 | end
|
---|
61 |
|
---|
62 | const_name_prefix=const_name_prefix..tail
|
---|
63 |
|
---|
64 | -- messagebox("var", const_name_prefix)
|
---|
65 |
|
---|
66 | file:write("@===============================================================================\n")
|
---|
67 | file:write("@ "..const_name_prefix.."_Tiles_Size\n")
|
---|
68 | file:write("@===============================================================================\n")
|
---|
69 | file:write(".global "..const_name_prefix.."_Tiles_Size\n")
|
---|
70 | file:write(".type "..const_name_prefix.."_Tiles_Size, %object\n")
|
---|
71 | file:write(".section .rodata\n")
|
---|
72 | file:write(".align 1\n")
|
---|
73 | file:write(const_name_prefix.."_Tiles_Size:\n")
|
---|
74 | file:write(".hword "..size.."\n")
|
---|
75 | file:write("\n")
|
---|
76 | file:write("@===============================================================================\n")
|
---|
77 | file:write("@ "..const_name_prefix.."_Tiles_SizeX\n")
|
---|
78 | file:write("@===============================================================================\n")
|
---|
79 | file:write(".global "..const_name_prefix.."_Tiles_SizeX\n")
|
---|
80 | file:write(".type "..const_name_prefix.."_Tiles_SizeX, %object\n")
|
---|
81 | file:write(".section .rodata\n")
|
---|
82 | file:write(".align 1\n")
|
---|
83 | file:write(const_name_prefix.."_Tiles_SizeX:\n")
|
---|
84 | file:write(".hword "..w.."\n")
|
---|
85 | file:write("\n")
|
---|
86 | file:write("@===============================================================================\n")
|
---|
87 | file:write("@ "..const_name_prefix.."_Tiles_SizeY\n")
|
---|
88 | file:write("@===============================================================================\n")
|
---|
89 | file:write(".global "..const_name_prefix.."_Tiles_SizeY\n")
|
---|
90 | file:write(".type "..const_name_prefix.."_Tiles_SizeY, %object\n")
|
---|
91 | file:write(".section .rodata\n")
|
---|
92 | file:write(".align 1\n")
|
---|
93 | file:write(const_name_prefix.."_Tiles_SizeY:\n")
|
---|
94 | file:write(".hword "..h.."\n")
|
---|
95 | file:write("\n")
|
---|
96 | file:write("@===============================================================================\n")
|
---|
97 | file:write("@ "..const_name_prefix.."_Tiles\n")
|
---|
98 | file:write("@===============================================================================\n")
|
---|
99 | file:write(".global "..const_name_prefix.."_Tiles\n")
|
---|
100 | file:write(".type "..const_name_prefix.."_Tiles, %object\n")
|
---|
101 | file:write(".section .rodata\n")
|
---|
102 | file:write(".align 2\n")
|
---|
103 | file:write(const_name_prefix.."_Tiles:\n")
|
---|
104 |
|
---|
105 | colscounter=1
|
---|
106 | const_type_written=0
|
---|
107 | for 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
|
---|
135 | end
|
---|
136 |
|
---|
137 | file:close()
|
---|