fixed so you can append maps in the editor. broken the loading of old maps however :D

This commit is contained in:
Magnus Auvinen 2008-03-09 23:29:14 +00:00
parent 61535a565a
commit 78155fa6d7
7 changed files with 158 additions and 80 deletions

Binary file not shown.

View file

@ -77,7 +77,7 @@ void LAYERGROUP::render()
for(int i = 0; i < layers.len(); i++) for(int i = 0; i < layers.len(); i++)
{ {
if(layers[i]->visible && layers[i] != editor.game_layer) if(layers[i]->visible && layers[i] != editor.map.game_layer)
layers[i]->render(); layers[i]->render();
} }
} }
@ -782,10 +782,10 @@ static void do_map_editor(RECT view, RECT toolbar)
} }
// render the game above everything else // render the game above everything else
if(editor.game_group->visible && editor.game_layer->visible) if(editor.map.game_group->visible && editor.map.game_layer->visible)
{ {
editor.game_group->mapscreen(); editor.map.game_group->mapscreen();
editor.game_layer->render(); editor.map.game_layer->render();
} }
} }
@ -1075,7 +1075,7 @@ static void do_map_editor(RECT view, RECT toolbar)
// render screen sizes // render screen sizes
if(editor.proof_borders) if(editor.proof_borders)
{ {
LAYERGROUP *g = editor.game_group; LAYERGROUP *g = editor.map.game_group;
g->mapscreen(); g->mapscreen();
gfx_texture_set(-1); gfx_texture_set(-1);
@ -2036,15 +2036,9 @@ static void render_envelopeeditor(RECT view)
} }
} }
static void callback_open_map(const char *filename) static void callback_open_map(const char *filename) { editor.load(filename); }
{ static void callback_append_map(const char *filename) { editor.append(filename); }
editor.load(filename); static void callback_save_map(const char *filename) { editor.save(filename); }
}
static void callback_save_map(const char *filename)
{
editor.save(filename);
}
static int popup_menu_file(RECT view) static int popup_menu_file(RECT view)
{ {
@ -2052,6 +2046,7 @@ static int popup_menu_file(RECT view)
static int save_button = 0; static int save_button = 0;
static int save_as_button = 0; static int save_as_button = 0;
static int open_button = 0; static int open_button = 0;
static int append_button = 0;
RECT slot; RECT slot;
ui_hsplit_t(&view, 2.0f, &slot, &view); ui_hsplit_t(&view, 2.0f, &slot, &view);
@ -2072,7 +2067,15 @@ static int popup_menu_file(RECT view)
ui_hsplit_t(&view, 10.0f, &slot, &view); ui_hsplit_t(&view, 10.0f, &slot, &view);
ui_hsplit_t(&view, 12.0f, &slot, &view); ui_hsplit_t(&view, 12.0f, &slot, &view);
if(do_editor_button(&save_button, "Save", 0, &slot, draw_editor_button_menuitem, 0, "Saves the current map")) if(do_editor_button(&append_button, "Append", 0, &slot, draw_editor_button_menuitem, 0, "Opens a map and adds everything from that map to the current one"))
{
editor.invoke_file_dialog("Append Map", "Append", "data/maps/", "", callback_append_map);
return 1;
}
ui_hsplit_t(&view, 10.0f, &slot, &view);
ui_hsplit_t(&view, 12.0f, &slot, &view);
if(do_editor_button(&save_button, "Save (NOT IMPL)", 0, &slot, draw_editor_button_menuitem, 0, "Saves the current map"))
{ {
return 1; return 1;
} }
@ -2203,20 +2206,15 @@ void EDITOR::render()
void EDITOR::reset(bool create_default) void EDITOR::reset(bool create_default)
{ {
editor.map.groups.deleteall(); editor.map.clean();
editor.map.envelopes.deleteall();
editor.map.images.deleteall();
editor.game_layer = 0;
editor.game_group = 0;
// create default layers // create default layers
if(create_default) if(create_default)
editor.map.create_default(entities_texture);
/*
{ {
editor.make_game_group(editor.map.new_group()); }*/
editor.make_game_layer(new LAYER_GAME(50, 50));
editor.game_group->add_layer(editor.game_layer);
}
selected_layer = 0; selected_layer = 0;
selected_group = 0; selected_group = 0;
@ -2226,21 +2224,39 @@ void EDITOR::reset(bool create_default)
selected_image = 0; selected_image = 0;
} }
void EDITOR::make_game_layer(LAYER *layer) void MAP::make_game_layer(LAYER *layer)
{ {
editor.game_layer = (LAYER_GAME *)layer; game_layer = (LAYER_GAME *)layer;
editor.game_layer->tex_id = entities_texture; game_layer->tex_id = entities_texture;
editor.game_layer->readonly = true; game_layer->readonly = true;
} }
void EDITOR::make_game_group(LAYERGROUP *group) void MAP::make_game_group(LAYERGROUP *group)
{ {
editor.game_group = group; game_group = group;
editor.game_group->game_group = true; game_group->game_group = true;
editor.game_group->name = "Game"; game_group->name = "Game";
} }
void MAP::clean()
{
groups.deleteall();
envelopes.deleteall();
images.deleteall();
game_layer = 0x0;
game_group = 0x0;
}
void MAP::create_default(int entities_texture)
{
make_game_group(new_group());
make_game_layer(new LAYER_GAME(50, 50));
game_group->add_layer(game_layer);
}
extern "C" void editor_init() extern "C" void editor_init()
{ {
checker_texture = gfx_load_texture("data/editor/checker.png", IMG_AUTO); checker_texture = gfx_load_texture("data/editor/checker.png", IMG_AUTO);

View file

@ -16,7 +16,7 @@ extern "C" {
#include <game/client/gc_ui.h> #include <game/client/gc_ui.h>
typedef void (*index_modify_func)(int *index); typedef void (*INDEX_MODIFY_FUNC)(int *index);
// EDITOR SPECIFIC // EDITOR SPECIFIC
template<typename T> template<typename T>
@ -152,8 +152,8 @@ public:
virtual void render() {} virtual void render() {}
virtual int render_properties(RECT *toolbox) { return 0; } virtual int render_properties(RECT *toolbox) { return 0; }
virtual void modify_image_index(index_modify_func func) {} virtual void modify_image_index(INDEX_MODIFY_FUNC func) {}
virtual void modify_envelope_index(index_modify_func func) {} virtual void modify_envelope_index(INDEX_MODIFY_FUNC func) {}
virtual void get_size(float *w, float *h) { *w = 0; *h = 0;} virtual void get_size(float *w, float *h) { *w = 0; *h = 0;}
@ -196,13 +196,13 @@ public:
void delete_layer(int index); void delete_layer(int index);
int swap_layers(int index0, int index1); int swap_layers(int index0, int index1);
void modify_image_index(index_modify_func func) void modify_image_index(INDEX_MODIFY_FUNC func)
{ {
for(int i = 0; i < layers.len(); i++) for(int i = 0; i < layers.len(); i++)
layers[i]->modify_image_index(func); layers[i]->modify_image_index(func);
} }
void modify_envelope_index(index_modify_func func) void modify_envelope_index(INDEX_MODIFY_FUNC func)
{ {
for(int i = 0; i < layers.len(); i++) for(int i = 0; i < layers.len(); i++)
layers[i]->modify_envelope_index(func); layers[i]->modify_envelope_index(func);
@ -235,11 +235,21 @@ public:
class MAP class MAP
{ {
void make_game_group(LAYERGROUP *group);
void make_game_layer(LAYER *layer);
public: public:
MAP()
{
clean();
}
array<LAYERGROUP*> groups; array<LAYERGROUP*> groups;
array<IMAGE*> images; array<IMAGE*> images;
array<ENVELOPE*> envelopes; array<ENVELOPE*> envelopes;
class LAYER_GAME *game_layer;
LAYERGROUP *game_group;
ENVELOPE *new_envelope(int channels) ENVELOPE *new_envelope(int channels)
{ {
ENVELOPE *e = new ENVELOPE(channels); ENVELOPE *e = new ENVELOPE(channels);
@ -270,17 +280,24 @@ public:
groups.removebyindex(index); groups.removebyindex(index);
} }
void modify_image_index(index_modify_func func) void modify_image_index(INDEX_MODIFY_FUNC func)
{ {
for(int i = 0; i < groups.len(); i++) for(int i = 0; i < groups.len(); i++)
groups[i]->modify_image_index(func); groups[i]->modify_image_index(func);
} }
void modify_envelope_index(index_modify_func func) void modify_envelope_index(INDEX_MODIFY_FUNC func)
{ {
for(int i = 0; i < groups.len(); i++) for(int i = 0; i < groups.len(); i++)
groups[i]->modify_envelope_index(func); groups[i]->modify_envelope_index(func);
} }
void clean();
void create_default(int entities_texture);
// io
int save(const char *filename);
int load(const char *filename);
}; };
@ -339,9 +356,6 @@ public:
const char *basepath, const char *default_name, const char *basepath, const char *default_name,
void (*func)(const char *filename)); void (*func)(const char *filename));
void make_game_group(LAYERGROUP *group);
void make_game_layer(LAYER *layer);
void reset(bool create_default=true); void reset(bool create_default=true);
int save(const char *filename); int save(const char *filename);
int load(const char *filename); int load(const char *filename);
@ -353,9 +367,6 @@ public:
LAYER *get_selected_layer(int index); LAYER *get_selected_layer(int index);
LAYERGROUP *get_selected_group(); LAYERGROUP *get_selected_group();
class LAYER_GAME *game_layer;
LAYERGROUP *game_group;
int do_properties(RECT *toolbox, PROPERTY *props, int *ids, int *new_val); int do_properties(RECT *toolbox, PROPERTY *props, int *ids, int *new_val);
int mode; int mode;
@ -425,8 +436,8 @@ public:
virtual int render_properties(RECT *toolbox); virtual int render_properties(RECT *toolbox);
virtual void modify_image_index(index_modify_func func); virtual void modify_image_index(INDEX_MODIFY_FUNC func);
virtual void modify_envelope_index(index_modify_func func); virtual void modify_envelope_index(INDEX_MODIFY_FUNC func);
void get_size(float *w, float *h) { *w = width*32.0f; *h = height*32.0f; } void get_size(float *w, float *h) { *w = width*32.0f; *h = height*32.0f; }
@ -456,8 +467,8 @@ public:
virtual int render_properties(RECT *toolbox); virtual int render_properties(RECT *toolbox);
virtual void modify_image_index(index_modify_func func); virtual void modify_image_index(INDEX_MODIFY_FUNC func);
virtual void modify_envelope_index(index_modify_func func); virtual void modify_envelope_index(INDEX_MODIFY_FUNC func);
void get_size(float *w, float *h); void get_size(float *w, float *h);

View file

@ -98,7 +98,7 @@ void editor_load_old(DATAFILE *df, MAP *map)
// move game layer to correct position // move game layer to correct position
for(int i = 0; i < map->groups[0]->layers.len()-1; i++) for(int i = 0; i < map->groups[0]->layers.len()-1; i++)
{ {
if(map->groups[0]->layers[i] == editor.game_layer) if(map->groups[0]->layers[i] == editor.map.game_layer)
map->groups[0]->swap_layers(i, i+1); map->groups[0]->swap_layers(i, i+1);
} }
@ -142,7 +142,7 @@ void editor_load_old(DATAFILE *df, MAP *map)
img->data = mem_alloc(img->width*img->height*4, 1); img->data = mem_alloc(img->width*img->height*4, 1);
mem_copy(img->data, data, img->width*img->height*4); mem_copy(img->data, data, img->width*img->height*4);
img->tex_id = gfx_load_texture_raw(img->width, img->height, img->format, img->data, IMG_AUTO); img->tex_id = gfx_load_texture_raw(img->width, img->height, img->format, img->data, IMG_AUTO);
editor.map.images.add(img); map->images.add(img);
// unload image // unload image
datafile_unload_data(df, imgres->image_data); datafile_unload_data(df, imgres->image_data);
@ -151,7 +151,7 @@ void editor_load_old(DATAFILE *df, MAP *map)
// load entities // load entities
{ {
LAYER_GAME *g = editor.game_layer; LAYER_GAME *g = map->game_layer;
g->resize(game_width, game_height); g->resize(game_width, game_height);
for(int t = MAPRES_ENTS_START; t < MAPRES_ENTS_END; t++) for(int t = MAPRES_ENTS_START; t < MAPRES_ENTS_END; t++)
{ {
@ -189,6 +189,11 @@ void editor_load_old(DATAFILE *df, MAP *map)
} }
int EDITOR::save(const char *filename) int EDITOR::save(const char *filename)
{
return map.save(filename);
}
int MAP::save(const char *filename)
{ {
dbg_msg("editor", "saving to '%s'...", filename); dbg_msg("editor", "saving to '%s'...", filename);
DATAFILE_OUT *df = datafile_create(filename); DATAFILE_OUT *df = datafile_create(filename);
@ -206,9 +211,9 @@ int EDITOR::save(const char *filename)
} }
// save images // save images
for(int i = 0; i < map.images.len(); i++) for(int i = 0; i < images.len(); i++)
{ {
IMAGE *img = map.images[i]; IMAGE *img = images[i];
MAPITEM_IMAGE item; MAPITEM_IMAGE item;
item.version = 1; item.version = 1;
@ -225,9 +230,9 @@ int EDITOR::save(const char *filename)
// save layers // save layers
int layer_count = 0; int layer_count = 0;
for(int g = 0; g < map.groups.len(); g++) for(int g = 0; g < groups.len(); g++)
{ {
LAYERGROUP *group = map.groups[g]; LAYERGROUP *group = groups[g];
MAPITEM_GROUP gitem; MAPITEM_GROUP gitem;
gitem.version = 1; gitem.version = 1;
@ -298,13 +303,13 @@ int EDITOR::save(const char *filename)
// save envelopes // save envelopes
int point_count = 0; int point_count = 0;
for(int e = 0; e < map.envelopes.len(); e++) for(int e = 0; e < envelopes.len(); e++)
{ {
MAPITEM_ENVELOPE item; MAPITEM_ENVELOPE item;
item.version = 1; item.version = 1;
item.channels = map.envelopes[e]->channels; item.channels = envelopes[e]->channels;
item.start_point = point_count; item.start_point = point_count;
item.num_points = map.envelopes[e]->points.len(); item.num_points = envelopes[e]->points.len();
item.name = -1; item.name = -1;
datafile_add_item(df, MAPITEMTYPE_ENVELOPE, e, sizeof(item), &item); datafile_add_item(df, MAPITEMTYPE_ENVELOPE, e, sizeof(item), &item);
@ -316,10 +321,10 @@ int EDITOR::save(const char *filename)
ENVPOINT *points = (ENVPOINT *)mem_alloc(totalsize, 1); ENVPOINT *points = (ENVPOINT *)mem_alloc(totalsize, 1);
point_count = 0; point_count = 0;
for(int e = 0; e < map.envelopes.len(); e++) for(int e = 0; e < envelopes.len(); e++)
{ {
int count = map.envelopes[e]->points.len(); int count = envelopes[e]->points.len();
mem_copy(&points[point_count], map.envelopes[e]->points.getptr(), sizeof(ENVPOINT)*count); mem_copy(&points[point_count], envelopes[e]->points.getptr(), sizeof(ENVPOINT)*count);
point_count += count; point_count += count;
} }
@ -331,29 +336,34 @@ int EDITOR::save(const char *filename)
return 1; return 1;
} }
void load_into_map(DATAFILE *df, MAP *map) int EDITOR::load(const char *filename)
{ {
reset();
return map.load(filename);
} }
int EDITOR::load(const char *filename) int MAP::load(const char *filename)
{ {
DATAFILE *df = datafile_load(filename); DATAFILE *df = datafile_load(filename);
if(!df) if(!df)
return 0; return 0;
clean();
// check version // check version
MAPITEM_VERSION *item = (MAPITEM_VERSION *)datafile_find_item(df, MAPITEMTYPE_VERSION, 0); MAPITEM_VERSION *item = (MAPITEM_VERSION *)datafile_find_item(df, MAPITEMTYPE_VERSION, 0);
if(!item) if(!item)
{ {
// import old map // import old map
/*
MAP old_mapstuff; MAP old_mapstuff;
editor.reset(); editor.reset();
editor_load_old(df, &old_mapstuff); editor_load_old(df, &old_mapstuff);
*/
} }
else if(item->version == 1) else if(item->version == 1)
{ {
editor.reset(false); //editor.reset(false);
// load images // load images
{ {
@ -399,7 +409,7 @@ int EDITOR::load(const char *filename)
if(name) if(name)
strncpy(img->name, name, 128); strncpy(img->name, name, 128);
editor.map.images.add(img); images.add(img);
// unload image // unload image
datafile_unload_data(df, item->image_data); datafile_unload_data(df, item->image_data);
@ -417,7 +427,7 @@ int EDITOR::load(const char *filename)
for(int g = 0; g < num; g++) for(int g = 0; g < num; g++)
{ {
MAPITEM_GROUP *gitem = (MAPITEM_GROUP *)datafile_get_item(df, start+g, 0, 0); MAPITEM_GROUP *gitem = (MAPITEM_GROUP *)datafile_get_item(df, start+g, 0, 0);
LAYERGROUP *group = map.new_group(); LAYERGROUP *group = new_group();
group->parallax_x = gitem->parallax_x; group->parallax_x = gitem->parallax_x;
group->parallax_y = gitem->parallax_y; group->parallax_y = gitem->parallax_y;
group->offset_x = gitem->offset_x; group->offset_x = gitem->offset_x;
@ -437,7 +447,7 @@ int EDITOR::load(const char *filename)
if(tilemap_item->flags&1) if(tilemap_item->flags&1)
{ {
tiles = new LAYER_GAME(tilemap_item->width, tilemap_item->height); tiles = new LAYER_GAME(tilemap_item->width, tilemap_item->height);
editor.make_game_layer(tiles); make_game_layer(tiles);
make_game_group(group); make_game_group(group);
} }
else else
@ -466,7 +476,7 @@ int EDITOR::load(const char *filename)
MAPITEM_LAYER_QUADS *quads_item = (MAPITEM_LAYER_QUADS *)layer_item; MAPITEM_LAYER_QUADS *quads_item = (MAPITEM_LAYER_QUADS *)layer_item;
LAYER_QUADS *layer = new LAYER_QUADS; LAYER_QUADS *layer = new LAYER_QUADS;
layer->image = quads_item->image; layer->image = quads_item->image;
if(layer->image < -1 || layer->image >= map.images.len()) if(layer->image < -1 || layer->image >= images.len())
layer->image = -1; layer->image = -1;
void *data = datafile_get_data_swapped(df, quads_item->data); void *data = datafile_get_data_swapped(df, quads_item->data);
group->add_layer(layer); group->add_layer(layer);
@ -497,7 +507,7 @@ int EDITOR::load(const char *filename)
ENVELOPE *env = new ENVELOPE(item->channels); ENVELOPE *env = new ENVELOPE(item->channels);
env->points.setsize(item->num_points); env->points.setsize(item->num_points);
mem_copy(env->points.getptr(), &points[item->start_point], sizeof(ENVPOINT)*item->num_points); mem_copy(env->points.getptr(), &points[item->start_point], sizeof(ENVPOINT)*item->num_points);
map.envelopes.add(env); envelopes.add(env);
} }
} }
} }
@ -507,8 +517,49 @@ int EDITOR::load(const char *filename)
return 0; return 0;
} }
static int modify_add_amount = 0;
static void modify_add(int *index)
{
if(*index >= 0)
*index += modify_add_amount;
}
int EDITOR::append(const char *filename) int EDITOR::append(const char *filename)
{ {
MAP new_map;
int err;
err = new_map.load(filename);
if(err)
return err;
// modify indecies
modify_add_amount = map.images.len();
new_map.modify_image_index(modify_add);
modify_add_amount = map.envelopes.len();
new_map.modify_envelope_index(modify_add);
// transfer images
for(int i = 0; i < new_map.images.len(); i++)
map.images.add(new_map.images[i]);
new_map.images.clear();
// transfer envelopes
for(int i = 0; i < new_map.envelopes.len(); i++)
map.envelopes.add(new_map.envelopes[i]);
new_map.envelopes.clear();
// transfer groups
for(int i = 0; i < new_map.groups.len(); i++)
{
if(new_map.groups[i] == new_map.game_group)
delete new_map.groups[i];
else
map.groups.add(new_map.groups[i]);
}
new_map.groups.clear();
// all done \o/
return 0; return 0;
} }

View file

@ -232,12 +232,12 @@ int LAYER_QUADS::render_properties(RECT *toolbox)
} }
void LAYER_QUADS::modify_image_index(index_modify_func func) void LAYER_QUADS::modify_image_index(INDEX_MODIFY_FUNC func)
{ {
func(&image); func(&image);
} }
void LAYER_QUADS::modify_envelope_index(index_modify_func func) void LAYER_QUADS::modify_envelope_index(INDEX_MODIFY_FUNC func)
{ {
for(int i = 0; i < quads.len(); i++) for(int i = 0; i < quads.len(); i++)
{ {

View file

@ -188,11 +188,11 @@ int LAYER_TILES::render_properties(RECT *toolbox)
{ {
RECT button; RECT button;
ui_hsplit_b(toolbox, 12.0f, toolbox, &button); ui_hsplit_b(toolbox, 12.0f, toolbox, &button);
bool in_gamegroup = editor.game_group->layers.find(this) != -1; bool in_gamegroup = editor.map.game_group->layers.find(this) != -1;
static int col_button = 0; static int col_button = 0;
if(do_editor_button(&col_button, "Make Collision", in_gamegroup?0:-1, &button, draw_editor_button, 0, "Constructs collision from the this layer")) if(do_editor_button(&col_button, "Make Collision", in_gamegroup?0:-1, &button, draw_editor_button, 0, "Constructs collision from the this layer"))
{ {
LAYER_TILES *gl = editor.game_layer; LAYER_TILES *gl = editor.map.game_layer;
int w = min(gl->width, width); int w = min(gl->width, width);
int h = min(gl->height, height); int h = min(gl->height, height);
for(int y = 0; y < h; y++) for(int y = 0; y < h; y++)
@ -235,11 +235,11 @@ int LAYER_TILES::render_properties(RECT *toolbox)
} }
void LAYER_TILES::modify_image_index(index_modify_func func) void LAYER_TILES::modify_image_index(INDEX_MODIFY_FUNC func)
{ {
func(&image); func(&image);
} }
void LAYER_TILES::modify_envelope_index(index_modify_func func) void LAYER_TILES::modify_envelope_index(INDEX_MODIFY_FUNC func)
{ {
} }

View file

@ -77,7 +77,7 @@ int popup_group(RECT view)
static int delete_button = 0; static int delete_button = 0;
// don't allow deletion of game group // don't allow deletion of game group
if(editor.game_group != editor.get_selected_group() && if(editor.map.game_group != editor.get_selected_group() &&
do_editor_button(&delete_button, "Delete Group", 0, &button, draw_editor_button, 0, "Delete group")) do_editor_button(&delete_button, "Delete Group", 0, &button, draw_editor_button, 0, "Delete group"))
{ {
editor.map.delete_group(editor.selected_group); editor.map.delete_group(editor.selected_group);
@ -162,7 +162,7 @@ int popup_layer(RECT view)
static int delete_button = 0; static int delete_button = 0;
// don't allow deletion of game layer // don't allow deletion of game layer
if(editor.game_layer != editor.get_selected_layer(0) && if(editor.map.game_layer != editor.get_selected_layer(0) &&
do_editor_button(&delete_button, "Delete Layer", 0, &button, draw_editor_button, 0, "Deletes the layer")) do_editor_button(&delete_button, "Delete Layer", 0, &button, draw_editor_button, 0, "Deletes the layer"))
{ {
editor.map.groups[editor.selected_group]->delete_layer(editor.selected_layer); editor.map.groups[editor.selected_group]->delete_layer(editor.selected_layer);