project_files/frontlib/model/map.c
changeset 7230 240620f46dd7
parent 7224 5143861c83bd
child 7271 5608ac657362
--- a/project_files/frontlib/model/map.c	Tue Jun 12 21:10:11 2012 +0200
+++ b/project_files/frontlib/model/map.c	Fri Jun 15 19:57:25 2012 +0200
@@ -3,9 +3,19 @@
 #include "../util/inihelper.h"
 #include "../util/util.h"
 #include "../util/logging.h"
+#include "../util/refcounter.h"
 
 #include <stdlib.h>
 
+static void flib_map_destroy(flib_map *map) {
+	if(map) {
+		free(map->drawData);
+		free(map->name);
+		free(map->theme);
+		free(map);
+	}
+}
+
 flib_map *flib_map_create_regular(const char *theme, int templateFilter) {
 	flib_map *result = NULL;
 	if(!theme) {
@@ -13,6 +23,7 @@
 	} else {
 		flib_map *newmap = flib_calloc(1, sizeof(flib_map));
 		if(newmap) {
+			newmap->_referenceCount = 1;
 			newmap->mapgen = MAPGEN_REGULAR;
 			newmap->templateFilter = templateFilter;
 			newmap->theme = flib_strdupnull(theme);
@@ -33,6 +44,7 @@
 	} else {
 		flib_map *newmap = flib_calloc(1, sizeof(flib_map));
 		if(newmap) {
+			newmap->_referenceCount = 1;
 			newmap->mapgen = MAPGEN_MAZE;
 			newmap->mazeSize = mazeSize;
 			newmap->theme = flib_strdupnull(theme);
@@ -53,6 +65,7 @@
 	} else {
 		flib_map *newmap = flib_calloc(1, sizeof(flib_map));
 		if(newmap) {
+			newmap->_referenceCount = 1;
 			newmap->mapgen = MAPGEN_NAMED;
 			newmap->name = flib_strdupnull(name);
 			if(newmap->name) {
@@ -72,6 +85,7 @@
 	} else {
 		flib_map *newmap = flib_calloc(1, sizeof(flib_map));
 		if(newmap) {
+			newmap->_referenceCount = 1;
 			newmap->mapgen = MAPGEN_DRAWN;
 			newmap->drawData = flib_bufdupnull(drawData, drawDataSize);
 			newmap->drawDataSize = drawDataSize;
@@ -85,11 +99,15 @@
 	return result;
 }
 
-void flib_map_destroy(flib_map *map) {
+flib_map *flib_map_retain(flib_map *map) {
 	if(map) {
-		free(map->drawData);
-		free(map->name);
-		free(map->theme);
-		free(map);
+		flib_retain(&map->_referenceCount, "flib_map");
+	}
+	return map;
+}
+
+void flib_map_release(flib_map *map) {
+	if(map && flib_release(&map->_referenceCount, "flib_map")) {
+		flib_map_destroy(map);
 	}
 }