FreeBASIC  0.91.0
makedata.bas
Go to the documentation of this file.
1 ''
2 '' Small tool to compress the font and palette data files for gfxlib2 and
3 '' write the resulting data bytes to inline.h to be compiled into gfxlib2.
4 '' Run/adjust this to update inline.h as needed.
5 ''
6 '' best compiled with -g -exx to catch assertions and file I/O errors
7 ''
8 
9 #define TRUE (-1)
10 #define FALSE 0
11 #define NULL 0
12 
13 #include once "crt.bi"
14 
15 declare function fb_hEncode lib "fbgfx" alias "fb_hEncode" _
16  ( _
17  byval as const ubyte ptr, _
18  byval as integer, _
19  byval as ubyte ptr, _
20  byval as integer ptr _
21  ) as integer
22 
23 type Entry
24  as zstring * 16 name
25  as zstring * 16 file
26  as ubyte ptr p
27  as integer size
28 end type
29 
30 dim shared as Entry entries(0 to ...) = _
31 { _
32  ( "FONT_8" , "fnt08x08.fnt" ), _
33  ( "FONT_14", "fnt08x14.fnt" ), _
34  ( "FONT_16", "fnt08x16.fnt" ), _
35  ( "PAL_2" , "pal002.pal" ), _
36  ( "PAL_16" , "pal016.pal" ), _
37  ( "PAL_64" , "pal064.pal" ), _
38  ( "PAL_256", "pal256.pal" ) _
39 }
40 
41 '' Load data files
42 for i as integer = 0 to ubound( entries )
43  with( entries(i) )
44  dim as string filename = exepath( ) + "/" + .file
45 
46  dim as integer f = freefile()
47  open filename for binary access read as #f
48 
49  .size = lof( f )
50  .p = allocate( .size )
51  get #f, , *.p, .size, .size
52  assert( .size > 0 )
53 
54  print "loading: '" + filename + "' (" & .size & " bytes)"
55 
56  close #f
57  end with
58 next
59 
60 '' Get size of all entries combined
61 dim as integer rawsize = 0
62 for i as integer = 0 to ubound( entries )
63  rawsize += entries(i).size
64 next
65 
66 '' Combine all the data files into one big buffer
67 dim as ubyte ptr raw = allocate( rawsize )
68 scope
69  dim as integer offset = 0
70  for i as integer = 0 to ubound( entries )
71  with( entries(i) )
72  memcpy( raw + offset, .p, .size )
73  offset += .size
74  end with
75  next
76 end scope
77 
78 '' Use this to store the raw data into a file
79 #if 0
80 scope
81  dim as integer f = freefile( )
82  open exepath( ) + "/data.dat" for binary access write as #f
83  put #f, , *raw, rawsize
84  close #f
85 end scope
86 #endif
87 
88 '' Compress the data
89 dim as integer compressedsize = rawsize
90 dim as ubyte ptr compressed = allocate( rawsize )
91 fb_hEncode( raw, rawsize, compressed, @compressedsize )
92 
93 print rawsize, compressedsize
94 
95 dim as string ccode
96 ccode += !"/* Automatically created by makedata, to be used by data.c */\n"
97 ccode += !"/* Compressed internal font/palette data for FB graphics */\n"
98 ccode += !"\n"
99 
100 '' Emit all the offset #defines
101 scope
102  dim as integer offset = 0
103  for i as integer = 0 to ubound( entries )
104  with( entries(i) )
105  ccode += "#define DATA_" + .name + " 0x" + hex( offset, 8 ) + !"\n"
106  offset += .size
107  end with
108  next
109 end scope
110 
111 '' Emit the compressed data
112 ccode += !"\nstatic const unsigned char compressed_data[] = {\n"
113 
114 for i as integer = 0 to compressedsize - 1
115  if( (i mod 16) = 0 ) then
116  ccode += " "
117  end if
118 
119  ccode += "0x" + hex( compressed[i], 2 )
120 
121  if( i < (compressedsize - 1) ) then
122  ccode += ","
123  '' Emit a newline every 16 bytes
124  if( ((i + 1) mod 16) = 0 ) then
125  ccode += !"\n"
126  else
127  ccode += " "
128  end if
129  else
130  ccode += !"\n"
131  end if
132 next
133 
134 ccode += !"};\n\n"
135 
136 ccode += !"#define DATA_SIZE " & rawsize & !"\n"
137 
138 '' Write out the emitted C code into the output file
139 scope
140  dim as integer f = freefile( )
141  open exepath( ) + "/../gfxdata_inline.h" for output as #f
142  print #f, ccode;
143  close #f
144 end scope
145 
146 print rawsize & " bytes in, " & compressedsize & " bytes out " & _
147  "(" & (rawsize / compressedsize) & ":1 ratio)"
148