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.

280 lines
7.6 KiB

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