gd3asset 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #!/usr/bin/python
  2. import gameduino2 as gd2
  3. import os
  4. import array
  5. import Image
  6. import wave
  7. import audioop
  8. def cname(s):
  9. """ make name s C-friendly """
  10. for c in "-+.":
  11. s = s.replace(c, "_")
  12. return s.upper()
  13. class GD2Assets(gd2.prep.AssetBin):
  14. def __init__(self, cmdline, opts, args):
  15. self.opts = opts
  16. self.args = args
  17. gd2.prep.AssetBin.__init__(self)
  18. self.header = opts.get('-o', 'default_assets.h')
  19. self.header_intro = "// This file was generated with the command-line:\n// " + cmdline + "\n\n"
  20. if '-f' in opts:
  21. self.asset_file = opts['-f']
  22. if '-3' in opts:
  23. self.target_810()
  24. self.handlers = {
  25. 'png' : (self.image, "PNG image file (options: format)"),
  26. 'jpg' : (self.image, "JPEG image file (options: format)"),
  27. 'bmp' : (self.image, "BMP image file (options: format)"),
  28. 'gif' : (self.image, "GIF image file (options: format)"),
  29. 'ttf' : (self.ttf, "TrueType font file (options: format, size)"),
  30. 'wav' : (self.sample, "Audio sample, mono 16-bit (no options)"),
  31. }
  32. def parse_format(self, format):
  33. formats = ('ARGB1555', 'L1', 'L2', 'L4', 'L8', 'RGB332', 'ARGB2', 'ARGB4', 'RGB565')
  34. if format not in formats:
  35. print 'ERROR: unknown format "%s"' % format
  36. print
  37. print 'Formats are: %s' % " ".join(formats)
  38. sys.exit(1)
  39. return eval("gd2." + format)
  40. def image(self, suffix, ff, format = 'ARGB4'):
  41. name = cname(os.path.basename(ff[0])[:-1 - len(suffix)])
  42. self.load_handle(name,
  43. [Image.open(f) for f in ff],
  44. self.parse_format(format),
  45. dither = '-d' in self.opts)
  46. def ttf(self, suffix, f, size = '12', format = 'L4'):
  47. name = cname(os.path.basename(f[0])[:-1 - len(suffix)])
  48. self.load_ttf(name, f[0], int(size), self.parse_format(format))
  49. def sample(self, suffix, f):
  50. name = os.path.basename(f[0])[:-1 - len(suffix)].upper()
  51. f = wave.open(f[0], "rb")
  52. if f.getnchannels() != 1:
  53. print "Sorry - .wav file must be mono"
  54. sys.exit(1)
  55. if f.getsampwidth() != 2:
  56. print "Sorry - .wav file must be 16-bit"
  57. sys.exit(1)
  58. freq = f.getframerate()
  59. pcm16 = f.readframes(f.getnframes())
  60. (adpcm, _) = audioop.lin2adpcm(pcm16, f.getsampwidth(), (0,0))
  61. adpcm = adpcm[:len(adpcm) & ~7]
  62. da = array.array('B', [((ord(c) >> 4) | ((15 & ord(c)) << 4)) for c in adpcm])
  63. self.align(8)
  64. self.add(name, da.tostring())
  65. self.define(name + "_LENGTH", len(da))
  66. self.define(name + "_FREQ", freq)
  67. def error(self, suffix, f, **_):
  68. print 'ERROR: cannot identify type of file "%s"' % f
  69. print
  70. print 'recognized file types are:'
  71. for suffix,(_, doc) in sorted(self.handlers.items()):
  72. print ' %s %s' % (suffix, doc)
  73. sys.exit(1)
  74. def addall(self):
  75. for a in self.args:
  76. a = a.split(',')
  77. f = []
  78. vars = {}
  79. for part in a:
  80. if '=' in part:
  81. varval = part.split('=')
  82. if len(varval) != 2:
  83. print 'ERROR: syntax error in asset specification "%s"' % setting
  84. sys.exit(1)
  85. (var, val) = varval
  86. vars[var] = val
  87. else:
  88. f.append(part)
  89. suffix = f[0].split('.')[-1].lower()
  90. (handler, _) = self.handlers.get(suffix, (self.error, ''))
  91. handler(suffix, f, **vars)
  92. if __name__ == '__main__':
  93. import sys, getopt
  94. try:
  95. optlist, args = getopt.getopt(sys.argv[1:], "3do:f:")
  96. except getopt.GetoptError:
  97. print 'usage: gd2asset <options> <assets>'
  98. print
  99. print ' -3 target GD3 (FT810 series)'
  100. print ' -d dither all pixel conversions'
  101. print ' -f <name> output asset file (default is header file)'
  102. print ' -o <name> output header file'
  103. print
  104. print 'If no output header file is given, then "default_assets.h" is used'
  105. print
  106. print 'Each asset is a filename, optionally followed by some var=val'
  107. print 'assignments. For example:'
  108. print ' pic1.png image, format ARGB4'
  109. print ' pic2.jpg,format=L8 image, format L8'
  110. print ' serif.ttf,size=16 font, 16 pixels high'
  111. print
  112. print 'The assets are compiled into flash, or if the "-f" option is given'
  113. print 'into a file. In this case the file should be copied to the'
  114. print 'microSD card.'
  115. print 'In either case, calling LOAD_ASSETS() from the program loads all'
  116. print 'assets.'
  117. sys.exit(1)
  118. optdict = dict(optlist)
  119. if 'gd3' in sys.argv[0].lower():
  120. optdict['-3'] = ''
  121. GD2Assets(" ".join(sys.argv), optdict, args).make()