Initial level export functionality

stinkhead7ds
Ashley N. 2023-09-14 10:11:40 -04:00
parent c62573e1ed
commit de5868305d
2 changed files with 37 additions and 5 deletions

View File

@ -1,6 +1,6 @@
use std::{error::Error, fs::File, io::Write, path::Path}; use std::{error::Error, fs::File, io::Write, path::Path};
use clap::Parser; use clap::Parser;
use crate::reskit::{tileset, soundtrack::{formats::dmf::DmfModule, engines::echo::engine::{EchoFormat, EchoArtifact}}, utility::{print_good, print_error, print_info}, level::converter::get_tiled_tilemap}; use crate::reskit::{tileset, soundtrack::{formats::dmf::DmfModule, engines::echo::engine::{EchoFormat, EchoArtifact}}, utility::{print_good, print_error, print_info}, level::{converter::get_tiled_tilemap, system::{get_tiles, get_code}}};
use super::settings::{Args, Tools, TileOutputFormat, TileOrder}; use super::settings::{Args, Tools, TileOutputFormat, TileOrder};
pub fn run_command() -> Result<(), Box<dyn Error>> { pub fn run_command() -> Result<(), Box<dyn Error>> {
@ -67,9 +67,21 @@ pub fn run_command() -> Result<(), Box<dyn Error>> {
} }
Tools::Level { input_file, output_directory, console, tile_size } => { Tools::Level { input_file, output_directory, console, tile_size } => {
let tiled_file = get_tiled_tilemap( &input_file )?; let tiled_file = get_tiled_tilemap( &input_file )?;
//print_info( &format!( "{:#?}", tiled_file ) );
print_good( "file opened without errors" ); // Get tile and palette files
print_error( "unimplemented" ); let ( all_tiles, palettes ) = get_tiles( &tiled_file )?;
let mut tiles_bin = File::create( format!( "{}tiles.bin", output_directory ) )?;
tiles_bin.write_all( &all_tiles )?;
print_good( "exported tiles.bin" );
let mut palettes_bin = File::create( format!( "{}palettes.pal", output_directory ) )?;
palettes_bin.write_all( &palettes )?;
print_good( "exported palettes.pal" );
let mut code_asm = File::create( format!( "{}level.asm", output_directory ) )?;
code_asm.write_all( &get_code( "testlevel", "levels/" )?.as_bytes() )?;
print_good( "exported level.asm" );
} }
}; };

View File

@ -1,6 +1,6 @@
use std::{error::Error, convert::TryInto}; use std::{error::Error, convert::TryInto};
use image::GenericImageView; use image::GenericImageView;
use crate::reskit::tileset::image_to_tiles; use crate::reskit::{tileset::image_to_tiles, utility::symbol_to_pascal};
use super::converter::{TiledTilemap, Layer, SystemPlane}; use super::converter::{TiledTilemap, Layer, SystemPlane};
/** /**
@ -163,4 +163,24 @@ pub fn get_collision_map( tilemap: &TiledTilemap ) -> Result<Vec<u8>, Box<dyn Er
} }
Ok( result ) Ok( result )
}
/**
* Get a helper .asm or .c file that ties all the level components together
*/
pub fn get_code( level_name: &str, path_prefix: &str ) -> Result<String, Box<dyn Error>> {
let version = env!( "CARGO_PKG_VERSION" );
let level_label = symbol_to_pascal( level_name );
let file = format!( r#"; Level definition file
; Generated by reskit v{version}
{level_label}Tiles:
incbin '{path_prefix}{level_name}/tiles.bin'
{level_label}Palettes:
incbin '{path_prefix}{level_name}/palettes.pal'
"# );
Ok( file )
} }