1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
#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->tool.wallshape = EDITOR_WALLSHAPE_LINE;
e->tool.polysides = EDITOR_DEFAULT_POLY_SIDES;
e->tool.wallheight = EDITOR_DEFAULT_WALL_HEIGHT;
e->tool.placementheight = EDITOR_DEFAULT_PLACEMENT_HEIGHT;
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;
}
|