From 78155fa6d766d4529e8f602cb36457bce55cb17d Mon Sep 17 00:00:00 2001 From: Magnus Auvinen Date: Sun, 9 Mar 2008 23:29:14 +0000 Subject: [PATCH] fixed so you can append maps in the editor. broken the loading of old maps however :D --- data/maps/dm2.map | Bin 6493 -> 7580 bytes src/game/editor/ed_editor.cpp | 82 +++++++++++++++---------- src/game/editor/ed_editor.hpp | 45 ++++++++------ src/game/editor/ed_io.cpp | 95 ++++++++++++++++++++++------- src/game/editor/ed_layer_quads.cpp | 4 +- src/game/editor/ed_layer_tiles.cpp | 8 +-- src/game/editor/ed_popups.cpp | 4 +- 7 files changed, 158 insertions(+), 80 deletions(-) diff --git a/data/maps/dm2.map b/data/maps/dm2.map index 70ffbdbf0cb7cf457c44494e3c8006a01e0fe67f..062e579776f5e4e5bf725d7c2aadd88eaef2ee01 100644 GIT binary patch delta 1842 zcmZ{j3slTm7{Kqpc{GzuGtE>pO=Z#~Hc_4Os;$POrp6=8N`#VFOGN9eO$-{lv0?Qu z$0!o1wz&NOw4qOJ9P*DkFVPzOXU<42ZL;$$} z2mC^SJq)5EKZ4^kpcq=b&LzYF+H0@6 z`3NK+BUl%Gtmr+IP%s=M1T$JXqVG~tZf_kX`p|qU4i~Qr?JU~eN~*C22{m|ySc4GR zq~5;K3&9+?H|irke6VA`=wtN}AIyVwFgN49kLL%)up!(O6P$u!#)h&P0Ri~ZSnmTr zh=XghG{Ej3m(66_TK7@We5S7mo58#1^f!oT9Ye;q1p~LgA%KtX+Gd~be8 zJA3uqmlGJ-01rs%Nn^>69rWc1Tn4Op{gf2_J2TA2d(`zrC^CqOp@pny%`Z7Cl(!N@ zHACvBs~U8MQR=OenUPLIudU+Tn`ZCN_kTN!Em=?(J$BhJ<4C9DNlsBmZ`%7USl%M) z%or9s*GAEKMOn8aXX|B7qt19swN2giwb^Om!H$cEHU&S{6~;M{|BB`t($@J_G`4>} zwkoDhSS-)<;|cS5Lj%hccarBl$NC3_KBCoUVhgML*mk|7`KtawxH&bqNlL#>NISXG znJ$vMK1%j#U2n|Szlh={8Y6#hv+jPpIUWB;mCjmQW^~OSOiE~%WDBpNRNzB^vtgiI?UY{-Fyd9ray>#O8PfhBOg2;^X zyEE6QULU1gSJWiMrpYG!D>$VY>~~Kcs-NTMe>>#t@|zK<)A+mGboLe3v^6sim*(*4 z`jp!vo-VmC>(v=0T*(FbxiD$NGM{6>&YAdzP ztG@X=IHB^EIc~<#&Z5Y++Fx&kojg6gHZ@mheOg?Zteuo6ZgSC{U)TM3?J@S^T9>Uk z2V?I9dx<9Xs8R99aP+_$Ycl);!H=FPo% zC;H70PS`|WXOhTpk19`KB~z0+I4$v|UpXv3S5~iWR%i2#!qGf-Xh;&-9_&!n{92x;^*yoCR6$0vVyKq{$_1_0LhGYV3p6X=Gp%bSwqW` delta 808 zcmaKpOGq1G5XWaXo0qN{V$=pp3FtvVaE&d zp9c@|aTS8%qdgR%Xd$)qpx#7O6i*&J2p*J3*Z-_J7@-~b<(tem|C#w_lMN^SXsETj zRUx7hFHr)Hm3>eMHZ4Sx9ynQ(aLRyBToLOS)-X;n%v^@Aih1C{6)0V(bbD+bk|0mQ*ExPfjAx%HT;*G{yMN3@9Ub8ez< z5b$vPFobE&1ed$3L>h#@|K;s!N=NIHddcg zTPxtE1;89u%-dSTrLAM*%sDJYGe6GvjC6Za{eEQ_!yhj`=zDh=!@OOV*9k`^Le_dcvisible && layers[i] != editor.game_layer) + if(layers[i]->visible && layers[i] != editor.map.game_layer) layers[i]->render(); } } @@ -782,10 +782,10 @@ static void do_map_editor(RECT view, RECT toolbar) } // 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.game_layer->render(); + editor.map.game_group->mapscreen(); + editor.map.game_layer->render(); } } @@ -1075,7 +1075,7 @@ static void do_map_editor(RECT view, RECT toolbar) // render screen sizes if(editor.proof_borders) { - LAYERGROUP *g = editor.game_group; + LAYERGROUP *g = editor.map.game_group; g->mapscreen(); gfx_texture_set(-1); @@ -2036,15 +2036,9 @@ static void render_envelopeeditor(RECT view) } } -static void callback_open_map(const char *filename) -{ - editor.load(filename); -} - -static void callback_save_map(const char *filename) -{ - editor.save(filename); -} +static void callback_open_map(const char *filename) { editor.load(filename); } +static void callback_append_map(const char *filename) { editor.append(filename); } +static void callback_save_map(const char *filename) { editor.save(filename); } 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_as_button = 0; static int open_button = 0; + static int append_button = 0; RECT slot; 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, 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; } @@ -2203,20 +2206,15 @@ void EDITOR::render() void EDITOR::reset(bool create_default) { - editor.map.groups.deleteall(); - editor.map.envelopes.deleteall(); - editor.map.images.deleteall(); - - editor.game_layer = 0; - editor.game_group = 0; + editor.map.clean(); // create default layers 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_group = 0; @@ -2226,21 +2224,39 @@ void EDITOR::reset(bool create_default) selected_image = 0; } -void EDITOR::make_game_layer(LAYER *layer) +void MAP::make_game_layer(LAYER *layer) { - editor.game_layer = (LAYER_GAME *)layer; - editor.game_layer->tex_id = entities_texture; - editor.game_layer->readonly = true; + game_layer = (LAYER_GAME *)layer; + game_layer->tex_id = entities_texture; + game_layer->readonly = true; } -void EDITOR::make_game_group(LAYERGROUP *group) +void MAP::make_game_group(LAYERGROUP *group) { - editor.game_group = group; - editor.game_group->game_group = true; - editor.game_group->name = "Game"; + game_group = group; + game_group->game_group = true; + 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() { checker_texture = gfx_load_texture("data/editor/checker.png", IMG_AUTO); diff --git a/src/game/editor/ed_editor.hpp b/src/game/editor/ed_editor.hpp index 8312c65d2..b2bc7f26f 100644 --- a/src/game/editor/ed_editor.hpp +++ b/src/game/editor/ed_editor.hpp @@ -16,7 +16,7 @@ extern "C" { #include -typedef void (*index_modify_func)(int *index); +typedef void (*INDEX_MODIFY_FUNC)(int *index); // EDITOR SPECIFIC template @@ -152,8 +152,8 @@ public: virtual void render() {} virtual int render_properties(RECT *toolbox) { return 0; } - virtual void modify_image_index(index_modify_func func) {} - virtual void modify_envelope_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 get_size(float *w, float *h) { *w = 0; *h = 0;} @@ -196,13 +196,13 @@ public: void delete_layer(int index); 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++) 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++) layers[i]->modify_envelope_index(func); @@ -235,11 +235,21 @@ public: class MAP { + void make_game_group(LAYERGROUP *group); + void make_game_layer(LAYER *layer); public: + MAP() + { + clean(); + } + array groups; array images; array envelopes; + class LAYER_GAME *game_layer; + LAYERGROUP *game_group; + ENVELOPE *new_envelope(int channels) { ENVELOPE *e = new ENVELOPE(channels); @@ -270,17 +280,24 @@ public: 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++) 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++) 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, void (*func)(const char *filename)); - void make_game_group(LAYERGROUP *group); - void make_game_layer(LAYER *layer); - void reset(bool create_default=true); int save(const char *filename); int load(const char *filename); @@ -353,9 +367,6 @@ public: LAYER *get_selected_layer(int index); 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 mode; @@ -425,8 +436,8 @@ public: virtual int render_properties(RECT *toolbox); - virtual void modify_image_index(index_modify_func func); - virtual void modify_envelope_index(index_modify_func func); + virtual void modify_image_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; } @@ -456,8 +467,8 @@ public: virtual int render_properties(RECT *toolbox); - virtual void modify_image_index(index_modify_func func); - virtual void modify_envelope_index(index_modify_func func); + virtual void modify_image_index(INDEX_MODIFY_FUNC func); + virtual void modify_envelope_index(INDEX_MODIFY_FUNC func); void get_size(float *w, float *h); diff --git a/src/game/editor/ed_io.cpp b/src/game/editor/ed_io.cpp index 0db94c9b6..767007811 100644 --- a/src/game/editor/ed_io.cpp +++ b/src/game/editor/ed_io.cpp @@ -98,7 +98,7 @@ void editor_load_old(DATAFILE *df, MAP *map) // move game layer to correct position 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); } @@ -142,7 +142,7 @@ void editor_load_old(DATAFILE *df, MAP *map) img->data = mem_alloc(img->width*img->height*4, 1); 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); - editor.map.images.add(img); + map->images.add(img); // unload image datafile_unload_data(df, imgres->image_data); @@ -151,7 +151,7 @@ void editor_load_old(DATAFILE *df, MAP *map) // load entities { - LAYER_GAME *g = editor.game_layer; + LAYER_GAME *g = map->game_layer; g->resize(game_width, game_height); 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) +{ + return map.save(filename); +} + +int MAP::save(const char *filename) { dbg_msg("editor", "saving to '%s'...", filename); DATAFILE_OUT *df = datafile_create(filename); @@ -206,9 +211,9 @@ int EDITOR::save(const char *filename) } // 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; item.version = 1; @@ -225,9 +230,9 @@ int EDITOR::save(const char *filename) // save layers 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; gitem.version = 1; @@ -298,13 +303,13 @@ int EDITOR::save(const char *filename) // save envelopes int point_count = 0; - for(int e = 0; e < map.envelopes.len(); e++) + for(int e = 0; e < envelopes.len(); e++) { MAPITEM_ENVELOPE item; item.version = 1; - item.channels = map.envelopes[e]->channels; + item.channels = envelopes[e]->channels; item.start_point = point_count; - item.num_points = map.envelopes[e]->points.len(); + item.num_points = envelopes[e]->points.len(); item.name = -1; 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); 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(); - mem_copy(&points[point_count], map.envelopes[e]->points.getptr(), sizeof(ENVPOINT)*count); + int count = envelopes[e]->points.len(); + mem_copy(&points[point_count], envelopes[e]->points.getptr(), sizeof(ENVPOINT)*count); point_count += count; } @@ -331,29 +336,34 @@ int EDITOR::save(const char *filename) 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); if(!df) return 0; + + clean(); // check version MAPITEM_VERSION *item = (MAPITEM_VERSION *)datafile_find_item(df, MAPITEMTYPE_VERSION, 0); if(!item) { // import old map + /* MAP old_mapstuff; editor.reset(); editor_load_old(df, &old_mapstuff); + */ } else if(item->version == 1) { - editor.reset(false); + //editor.reset(false); // load images { @@ -399,7 +409,7 @@ int EDITOR::load(const char *filename) if(name) strncpy(img->name, name, 128); - editor.map.images.add(img); + images.add(img); // unload image datafile_unload_data(df, item->image_data); @@ -417,7 +427,7 @@ int EDITOR::load(const char *filename) for(int g = 0; g < num; g++) { 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_y = gitem->parallax_y; group->offset_x = gitem->offset_x; @@ -437,7 +447,7 @@ int EDITOR::load(const char *filename) if(tilemap_item->flags&1) { tiles = new LAYER_GAME(tilemap_item->width, tilemap_item->height); - editor.make_game_layer(tiles); + make_game_layer(tiles); make_game_group(group); } else @@ -466,7 +476,7 @@ int EDITOR::load(const char *filename) MAPITEM_LAYER_QUADS *quads_item = (MAPITEM_LAYER_QUADS *)layer_item; LAYER_QUADS *layer = new LAYER_QUADS; 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; void *data = datafile_get_data_swapped(df, quads_item->data); group->add_layer(layer); @@ -497,7 +507,7 @@ int EDITOR::load(const char *filename) ENVELOPE *env = new ENVELOPE(item->channels); env->points.setsize(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; } +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) { + 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; } diff --git a/src/game/editor/ed_layer_quads.cpp b/src/game/editor/ed_layer_quads.cpp index da7e70b87..bf203ebbd 100644 --- a/src/game/editor/ed_layer_quads.cpp +++ b/src/game/editor/ed_layer_quads.cpp @@ -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); } -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++) { diff --git a/src/game/editor/ed_layer_tiles.cpp b/src/game/editor/ed_layer_tiles.cpp index f6a5dfe91..e12b2c63f 100644 --- a/src/game/editor/ed_layer_tiles.cpp +++ b/src/game/editor/ed_layer_tiles.cpp @@ -188,11 +188,11 @@ int LAYER_TILES::render_properties(RECT *toolbox) { RECT 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; 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 h = min(gl->height, height); 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); } -void LAYER_TILES::modify_envelope_index(index_modify_func func) +void LAYER_TILES::modify_envelope_index(INDEX_MODIFY_FUNC func) { } diff --git a/src/game/editor/ed_popups.cpp b/src/game/editor/ed_popups.cpp index 4a1248b49..a03e4e9e8 100644 --- a/src/game/editor/ed_popups.cpp +++ b/src/game/editor/ed_popups.cpp @@ -77,7 +77,7 @@ int popup_group(RECT view) static int delete_button = 0; // 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")) { editor.map.delete_group(editor.selected_group); @@ -162,7 +162,7 @@ int popup_layer(RECT view) static int delete_button = 0; // 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")) { editor.map.groups[editor.selected_group]->delete_layer(editor.selected_layer);