From cc1052ceb42870f87fafcf56be805c9f80e43a99 Mon Sep 17 00:00:00 2001 From: Luke Hubmayer-Werner Date: Fri, 28 Jul 2023 15:57:21 +0930 Subject: [PATCH] Add json loading --- project.godot | 1 + scripts/loaders/common.gd | 13 ++++++++++ scripts/loaders/sprite_loader.gd | 43 +++++++++++++++++++++++++++++++- 3 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 scripts/loaders/common.gd diff --git a/project.godot b/project.godot index 9d2146f..eced9ed 100644 --- a/project.godot +++ b/project.godot @@ -21,6 +21,7 @@ config/icon="res://icon.png" [autoload] globals="*res://globals.gd" +Common="*res://scripts/loaders/common.gd" CommonGBA="*res://scripts/loaders/common_gba.gd" SoundLoader="*res://scripts/loaders/sound_loader.gd" SpriteLoader="*res://scripts/loaders/sprite_loader.gd" diff --git a/scripts/loaders/common.gd b/scripts/loaders/common.gd new file mode 100644 index 0000000..da8cf9b --- /dev/null +++ b/scripts/loaders/common.gd @@ -0,0 +1,13 @@ +extends Node + +func load_json(filename: String) -> Dictionary: + var file := File.new() + var error := file.open(filename, File.READ) + if error == OK: + var result = JSON.parse(file.get_as_text()) + if result.error == OK: + return result.result + else: + print_debug(result.error_string) + print_debug(result.error_line) + return {} diff --git a/scripts/loaders/sprite_loader.gd b/scripts/loaders/sprite_loader.gd index f67f280..897aae7 100644 --- a/scripts/loaders/sprite_loader.gd +++ b/scripts/loaders/sprite_loader.gd @@ -274,7 +274,7 @@ func snes_load_worldmap(rom: File): MapLoader.update_worldmap_block_tile_ids(worldmap_block_tile_ids) -func load_snes_rom(rom: File): +func snes_load_battle_sprites(rom: File): # Load Battle sprites rom.seek(offset_Character_Battle_Sprite_Layouts) var battle_strip_layouts = rom.get_buffer(num_Character_Battle_Sprite_Layouts * 6) @@ -298,7 +298,48 @@ func load_snes_rom(rom: File): weapon_textures['Fist'] = texture_from_image(snes_get_tile(rom, offset_Tiles_Fist, 24)) + +var json_sprite_blocks := Common.load_json('res://data/sprite_blocks.json') +var sprite_blocks := {} +func snes_load_map_sprites(rom: File): + # Main palettes + var map_palettes = [] + for i in 4: + map_palettes.append(generate_palette(rom, 0x1FFC00 + 32*i)) + + for k in json_sprite_blocks.keys(): + var v = json_sprite_blocks[k] + var start := int(v['start']) + var bpp := int(v['bpp']) + var definitions = v['definitions'] + var total_tiles := 0 + for defn in definitions: + total_tiles += defn[0] * defn[1] * defn[2] + print_debug('Processing sprite block "%s" - starts at $%06X with %d %dbpp tiles' % [k, start, total_tiles, bpp]) + rom.seek(start) + var tiles := [] + match bpp: + 1: + for i in total_tiles: + tiles.append(snes_1plane_to_tile(rom.get_buffer(8))) + 2: + for i in total_tiles: + tiles.append(snes_2plane_to_tile(rom.get_buffer(16))) + 3: + for i in total_tiles: + tiles.append(snes_3plane_to_tile(rom.get_buffer(24))) + 4: + for i in total_tiles: + tiles.append(snes_4plane_to_tile(rom.get_buffer(32))) + _: + print_debug('Invalid bpp "%s" in sprite blocks json' % bpp) + sprite_blocks[k] = tiles + + +func load_snes_rom(rom: File): + snes_load_battle_sprites(rom) snes_load_worldmap(rom) + snes_load_map_sprites(rom) const gba_marker := 'FINAL FANTASY V ADVANCE SYGMAB'