Add json loading

This commit is contained in:
Luke Hubmayer-Werner 2023-07-28 15:57:21 +09:30
parent e0266998b9
commit cc1052ceb4
3 changed files with 56 additions and 1 deletions

View File

@ -21,6 +21,7 @@ config/icon="res://icon.png"
[autoload] [autoload]
globals="*res://globals.gd" globals="*res://globals.gd"
Common="*res://scripts/loaders/common.gd"
CommonGBA="*res://scripts/loaders/common_gba.gd" CommonGBA="*res://scripts/loaders/common_gba.gd"
SoundLoader="*res://scripts/loaders/sound_loader.gd" SoundLoader="*res://scripts/loaders/sound_loader.gd"
SpriteLoader="*res://scripts/loaders/sprite_loader.gd" SpriteLoader="*res://scripts/loaders/sprite_loader.gd"

13
scripts/loaders/common.gd Normal file
View File

@ -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 {}

View File

@ -274,7 +274,7 @@ func snes_load_worldmap(rom: File):
MapLoader.update_worldmap_block_tile_ids(worldmap_block_tile_ids) 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 # Load Battle sprites
rom.seek(offset_Character_Battle_Sprite_Layouts) rom.seek(offset_Character_Battle_Sprite_Layouts)
var battle_strip_layouts = rom.get_buffer(num_Character_Battle_Sprite_Layouts * 6) 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)) 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_worldmap(rom)
snes_load_map_sprites(rom)
const gba_marker := 'FINAL FANTASY V ADVANCE SYGMAB' const gba_marker := 'FINAL FANTASY V ADVANCE SYGMAB'