diff options
Diffstat (limited to 'src/editor/editor.cpp')
| -rw-r--r-- | src/editor/editor.cpp | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/src/editor/editor.cpp b/src/editor/editor.cpp new file mode 100644 index 0000000..d97164e --- /dev/null +++ b/src/editor/editor.cpp @@ -0,0 +1,138 @@ +#include "editor.h" + +#include "../game.h" + +GAME_EDITOR* editor = 0; + +GAME_EDITOR* editor_create( GAME_DATA* game ) { + if( editor ) { + dlog( "editor_create() : attempted to create editor when one already exists\n" ); + return 0; + } + + GAME_EDITOR* e = new GAME_EDITOR(); + editor = e; + + e->grid = 1.f; + e->spritesize = EDITOR_DEFAULT_SPRITE_SIZE; + + e->game = game; + gui_init( game ); + + e->wnd = gui_editorwindow( 800, 600 ); + + gui_end(); + return e; +} + +STAT editor_close( GAME_EDITOR* e ) { + game_unload_map( e->game ); + e->map = 0; + + gui_push_callback( e, pfn( void* data ) { + GAME_EDITOR* e = (GAME_EDITOR*)data; + I32 w = e->wnd->w, h = e->wnd->h; + + gui_free( e->wnd ); + e->wnd = gui_editorwindow( w, h ); + } ); + + return STAT_OK; +} + +STAT editor_load_map( GAME_EDITOR* e, const char* mapname ) { + if( e->map ) { + dlog( "editor_load() : map already loaded\n" ); + return STAT_ERR; + } + + dlog( "editor_load() : loading map %s\n", mapname ); + + WORLD_MAP* m = game_load_map( e->game, mapname ); + if( !m ) + return STAT_ERR; + + e->game->state.map = e->map = m; + gui_push_callback( e, pfn( void* d ) { + editor_create_map_view( (GAME_EDITOR*)d ); + } ); + return STAT_OK; +} + +STAT editor_new_map( GAME_EDITOR* e, const char* mapname ) { + WORLD_MAP* m = new WORLD_MAP; + defer( map_free( e->game, m ) ); + + strcpy( m->name, mapname ); + strcat( m->name, ".hmap" ); + + m->startpos = { 10.f, 10.f, 0.f }; + m->props.push( { .clr = { 1.f, 1.f, 1.f, 1.f } } ); + m->walls.push( { + .start = { 0 , 0, -40.f }, + .end = { 10.f, 0, 80.f }, + .propid = 0 + } ); + + CFG_SECTION* map_cfg = map_serialize( m ); + defer( cfg_free( map_cfg ) ); + + char full_path[256]; + sprintf( full_path, "../assets/maps/%s.hmap", mapname ); + if( !OK( cfg_save( map_cfg, full_path ) ) ) { + dlog( "editor_new_map() : error saving file %s\n", full_path ); + return STAT_ERR; + } + + GUI_LIST_ENTRY ne; + ne.val = e->map_list.size; + strcpy( ne.title, mapname ); + strcat( ne.title, ".hmap" ); + e->map_list.push( ne ); + + return STAT_OK; +} + +STAT editor_save_map( GAME_EDITOR* e ) { + if( !e->map ) return STAT_ERR; + + CFG_SECTION* serialized = map_serialize( e->map ); + if( !serialized ) return STAT_ERR; + defer( cfg_free( serialized ) ); + + char full_path[256]; + sprintf( full_path, "../assets/maps/%s", e->map->name ); + + if( !OK( cfg_save( serialized, full_path ) ) ) { + dlog( "failed to save map %s", full_path ); + return STAT_ERR; + } + + return STAT_OK; +} + +LIST<GUI_LIST_ENTRY>* editor_get_map_list( GAME_EDITOR* ge ) { + const char* mapsdir = "../assets/maps"; + const char* ext[] = { "hmap", 0 }; + LIST<FILE_ENTRY> files = assets_get_files_by_ext_dir( ext, mapsdir ); + LIST<GUI_LIST_ENTRY> list; + + I32 i = 0; + files.each( fn( FILE_ENTRY* e ) { + if( e->dir ) return; + + GUI_LIST_ENTRY ne; + ne.val = i++; + strcpy( ne.title, file_path_last_of( e->name ) ); + + list.push( ne ); + } ); + + ge->map_list = list; + return &ge->map_list; +} + +void editor_destroy( GAME_EDITOR* e ) { + delete e; + editor = 0; +} |
