SNES-like engine in JavaScript.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

278 lines
7.5 KiB

  1. #!/usr/bin/env python3
  2. import glob
  3. import json
  4. import os
  5. import pygame
  6. import re
  7. import sys
  8. # The guide in tiles/guide.png is quite helpful.
  9. # TODO: sort the stuff in free/ and noncommercial/
  10. # TODO: in future/100/characters there are some doors, conveyor belts, screens
  11. SPRITE_FILES = [
  12. 'animals/sheets/*.png',
  13. 'beasttribes/100/*.png',
  14. 'characters/sheets/*.png',
  15. 'christmas/1x/gnome*.png',
  16. 'christmas/1x/reindeer*.png',
  17. 'christmas/1x/rudolph*.png',
  18. 'christmas/1x/xmas*.png',
  19. 'dwarvesvselves/regularsize/*.png',
  20. 'future/100/characters/cars.png',
  21. 'future/100/characters/future*.png',
  22. 'future/100/characters/military*.png',
  23. 'future/100/characters/modern*.png',
  24. 'halloween/ghost1.png',
  25. 'halloween/horseman/*1.png',
  26. 'halloween/reaper/*1.png',
  27. 'halloween/witch/1x/*.png',
  28. 'lichcrusades/100/*.png',
  29. 'monsters/1x/*.png',
  30. 'mythicalbosses/100/*.png',
  31. 'mythicalbosses/dinosaurs/*.png',
  32. 'npcanimations/rpgmaker/1/*.png',
  33. 'ship/100/char/airship*.png',
  34. 'ship/100/char/boat*.png',
  35. 'ship/100/char/pirates_100.png',
  36. 'ship/100/char/ship*.png',
  37. ]
  38. SPRITE_SIDEVIEW_FILES = [
  39. 'beasttribes/100/sv_battler/*.png',
  40. 'future/100/svbattler/*.png',
  41. # TODO: these need to get scaled down 2x before they can be used.
  42. 'sv_battle/RMMV/sv_actors/*.png',
  43. ]
  44. TILESET_FILES = [
  45. 'ashlands/ashlands_tileset.png',
  46. 'atlantis/tf_atlantis_tiles.png',
  47. 'beach/beach_tileset.png',
  48. 'christmas/1x/addon_igloo_1.png',
  49. 'christmas/1x/christmas*.png',
  50. 'cloud/cloud_tileset.png',
  51. 'darkdimension/tf_darkdimension_sheet.png',
  52. 'farmandfort/ff_master_tile_sheet.png',
  53. 'future/100/tilesets/*.png',
  54. 'gianttree/tf_gianttree_tiles.png',
  55. 'halloween/tiles/*1.png',
  56. 'jungle/tf_jungle_tileset.png',
  57. 'patron/train_sheet_1.png',
  58. 'ruindungeons/ruindungeons_sheet_full.png',
  59. 'ship/ship_big_tileset.png',
  60. 'tiles/TILESETS/*.png',
  61. 'winter/tiles/*.png',
  62. ]
  63. ANIMATION_FILES = [
  64. 'patron/fireworks*_1.png',
  65. 'pixelanimations/animationsheets/*.png',
  66. 'ship/100/char/!$ship_wave*.png',
  67. 'sv_battle/RMMV/system/States.png',
  68. 'tiles/TILESETS/animated/*.png',
  69. ]
  70. ICON_FILES = [
  71. 'farmandfort/IconSet/tf_icon_16.png',
  72. 'halloween/hallowicons_1.png',
  73. ]
  74. BACKGROUND_FILES = [
  75. 'cloud/bg_*.png',
  76. 'future/100/other/spacebg.png',
  77. 'ship/100/parallax/*.png'
  78. ]
  79. def unglob(list_of_globs):
  80. result = []
  81. for file in list_of_globs:
  82. globbed_files = glob.glob(file)
  83. assert len(globbed_files) > 0, 'glob for %s should be non-empty' % file
  84. result.extend(globbed_files)
  85. result.sort()
  86. return result
  87. def input_wh(prompt):
  88. while True:
  89. geometry = input(prompt).strip()
  90. try:
  91. cols, rows = [int(x) for x in geometry.split(' ')]
  92. return cols, rows
  93. except:
  94. pass
  95. # Returns True or False.
  96. def input_ok(prompt):
  97. while True:
  98. ok = input(prompt).strip()
  99. if ok.startswith('y'):
  100. return True
  101. if ok.startswith('n'):
  102. return False
  103. def draw_checkerboard(size):
  104. surface = pygame.display.get_surface()
  105. surface.fill((224, 224, 224))
  106. for i in range(surface.get_width() // size + 1):
  107. for j in range(surface.get_height() // size + 1):
  108. if (i + j) % 2 == 0:
  109. continue
  110. rect = pygame.Rect(i * size, j * size, size, size)
  111. surface.fill((192, 192, 192), rect)
  112. def show_splits(image_width, image_height, cols, rows):
  113. surface = pygame.display.get_surface()
  114. split_width = image_width / cols
  115. split_height = image_height / rows
  116. for i in range(cols):
  117. for j in range(rows):
  118. rect = pygame.Rect(
  119. i * split_width, j * split_height, split_width + 1, split_height + 1)
  120. pygame.draw.rect(surface, (255, 0, 255), rect, 1)
  121. pygame.display.flip()
  122. def render_text(text, pos, color):
  123. surface = pygame.display.get_surface()
  124. font = pygame.font.SysFont('notomono', 16)
  125. image = font.render(text, True, color)
  126. surface.blit(image, pos)
  127. pygame.display.flip()
  128. def render_sprite(metadata):
  129. line_color = (255, 0, 255)
  130. surface = pygame.display.get_surface()
  131. draw_checkerboard(8)
  132. image = pygame.image.load(metadata['filename'])
  133. surface.blit(image, (0, 0))
  134. if metadata.get('chunks'):
  135. for chunk in metadata['chunks']:
  136. rect = pygame.Rect(
  137. chunk['x'], chunk['y'], chunk['width'] + 1, chunk['height'] + 1)
  138. pygame.draw.rect(surface, line_color, rect, 1)
  139. label_pos = (chunk['x'] + 4, chunk['y'])
  140. render_text(str(chunk['index']), label_pos, line_color)
  141. caption_pos = (4, 4 + metadata['image_height'] + chunk['index'] * 20)
  142. caption = '%d: %s' % (chunk['index'], chunk.get('name', ''))
  143. render_text(caption, caption_pos, (0, 0, 0))
  144. pygame.display.flip()
  145. def set_sprite_chunk_size(metadata):
  146. cols, rows = input_wh('how many columns & rows of sprites? ')
  147. metadata['chunk_columns'] = cols
  148. metadata['chunk_rows'] = rows
  149. metadata['chunk_width'] = metadata['image_width'] // cols
  150. metadata['chunk_height'] = metadata['image_height'] // rows
  151. metadata['chunks'] = []
  152. for i in range(cols * rows):
  153. x = i % cols
  154. y = i // cols
  155. chunk_md = {
  156. 'index': i,
  157. 'x': x * metadata['chunk_width'],
  158. 'y': y * metadata['chunk_height'],
  159. 'width': metadata['chunk_width'],
  160. 'height': metadata['chunk_height']
  161. }
  162. metadata['chunks'].append(chunk_md)
  163. render_sprite(metadata)
  164. def edit_sprite_chunk_metadata(chunk):
  165. while True:
  166. name = input('name for chunk #%d: ' % chunk['index']).strip()
  167. print(name)
  168. if re.fullmatch(r'\w+', name):
  169. chunk['name'] = name
  170. return
  171. def edit_sprite_metadata(filename, metadata=None):
  172. if metadata is None:
  173. image = pygame.image.load(filename)
  174. metadata = {
  175. 'filename': filename,
  176. 'image_width': image.get_width(),
  177. 'image_height': image.get_height(),
  178. }
  179. print('\nprocessing %s (%dx%d)' % (
  180. filename, metadata['image_width'], metadata['image_height']))
  181. render_sprite(metadata)
  182. if not metadata.get('chunk_width'):
  183. set_sprite_chunk_size(metadata)
  184. while True:
  185. render_sprite(metadata)
  186. prompt = 'edit (c)hunk sizes, type a chunk #, (n)ext, or (q)uit: '
  187. choice = input(prompt).strip()
  188. if choice == 'n':
  189. return metadata, False
  190. elif choice == 'q':
  191. return metadata, True
  192. elif choice == 'c':
  193. set_sprite_chunk_size(metadata)
  194. elif re.fullmatch(r'\d+', choice):
  195. chunk_num = int(choice)
  196. if 0 <= chunk_num < len(metadata['chunks']):
  197. edit_sprite_chunk_metadata(metadata['chunks'][chunk_num])
  198. else:
  199. print('invalid chunk #')
  200. else:
  201. print('invalid choice')
  202. def annotate_sprites(sprite_files, all_metadata):
  203. pygame.init()
  204. surface = pygame.display.set_mode((1200, 900), pygame.RESIZABLE)
  205. for filename in sprite_files:
  206. sprite_metadata, quit = edit_sprite_metadata(
  207. filename, all_metadata.get(filename))
  208. all_metadata[filename] = sprite_metadata
  209. with open('metadata.json', 'w') as f:
  210. json.dump(all_metadata, f, sort_keys=True, indent=2)
  211. if quit:
  212. return
  213. def main(args):
  214. os.chdir(os.path.expanduser('~/time_fantasy'))
  215. with open('metadata.json') as f:
  216. all_metadata = json.load(f)
  217. sprite_files = unglob(SPRITE_FILES)
  218. tileset_files = unglob(TILESET_FILES)
  219. animation_files = unglob(ANIMATION_FILES)
  220. icon_files = unglob(ICON_FILES)
  221. background_files = unglob(BACKGROUND_FILES)
  222. print('sprites: %d tilesets: %d animations: %d icons: %d backgrounds: %d' %
  223. (len(sprite_files), len(tileset_files), len(animation_files),
  224. len(icon_files), len(background_files)))
  225. if len(args) < 1:
  226. return
  227. command = args[0]
  228. if command == 'annotate-sprites':
  229. annotate_sprites(sprite_files, all_metadata)
  230. if __name__ == '__main__':
  231. main(sys.argv[1:])