about summary refs log tree commit diff
path: root/src/game/editor
diff options
context:
space:
mode:
authorMagnus Auvinen <magnus.auvinen@gmail.com>2008-03-09 23:29:14 +0000
committerMagnus Auvinen <magnus.auvinen@gmail.com>2008-03-09 23:29:14 +0000
commit78155fa6d766d4529e8f602cb36457bce55cb17d (patch)
tree733044228da4c55a3e1438ed463cec6c4e7bd7fa /src/game/editor
parent61535a565a62d74654140af97ddd1cc853a54197 (diff)
downloadzcatch-78155fa6d766d4529e8f602cb36457bce55cb17d.tar.gz
zcatch-78155fa6d766d4529e8f602cb36457bce55cb17d.zip
fixed so you can append maps in the editor. broken the loading of old maps however :D
Diffstat (limited to 'src/game/editor')
-rw-r--r--src/game/editor/ed_editor.cpp82
-rw-r--r--src/game/editor/ed_editor.hpp45
-rw-r--r--src/game/editor/ed_io.cpp95
-rw-r--r--src/game/editor/ed_layer_quads.cpp4
-rw-r--r--src/game/editor/ed_layer_tiles.cpp8
-rw-r--r--src/game/editor/ed_popups.cpp4
6 files changed, 158 insertions, 80 deletions
diff --git a/src/game/editor/ed_editor.cpp b/src/game/editor/ed_editor.cpp
index 24df8b94..0ae82bfb 100644
--- a/src/game/editor/ed_editor.cpp
+++ b/src/game/editor/ed_editor.cpp
@@ -77,7 +77,7 @@ void LAYERGROUP::render()
 	
 	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();
 	}
 }
@@ -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 8312c65d..b2bc7f26 100644
--- a/src/game/editor/ed_editor.hpp
+++ b/src/game/editor/ed_editor.hpp
@@ -16,7 +16,7 @@ extern "C" {
 
 #include <game/client/gc_ui.h>
 
-typedef void (*index_modify_func)(int *index);
+typedef void (*INDEX_MODIFY_FUNC)(int *index);
 
 // EDITOR SPECIFIC
 template<typename T>
@@ -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<LAYERGROUP*> groups;
 	array<IMAGE*> images;
 	array<ENVELOPE*> 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 0db94c9b..76700781 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++)
 		{
@@ -190,6 +190,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);
 	if(!df)
@@ -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 da7e70b8..bf203ebb 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 f6a5dfe9..e12b2c63 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 4a1248b4..a03e4e9e 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);