Procházet zdrojové kódy

Add allocator parameter to resource loading functions

Daniele Bartolini před 13 roky
rodič
revize
072b490ca3
4 změnil soubory, kde provedl 28 přidání a 35 odebrání
  1. 11 12
      src/TextResource.cpp
  2. 3 3
      src/TextResource.h
  3. 10 16
      src/TextureResource.cpp
  4. 4 4
      src/TextureResource.h

+ 11 - 12
src/TextResource.cpp

@@ -2,31 +2,26 @@
 #include "FileStream.h"
 #include "ResourceArchive.h"
 #include "Log.h"
-
-#include <cstdio>
+#include "Allocator.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
-TextResource* TextResource::load(ResourceArchive* archive, ResourceId id)
+TextResource* TextResource::load(Allocator& allocator, ResourceArchive* archive, ResourceId id)
 {
 	assert(archive != NULL);
 	
-	Log::D("TextResource::load called.");
-	
 	FileStream* stream = archive->find(id);
 
 	if (stream != NULL)
 	{
-		TextResource* resource = new TextResource;
+		TextResource* resource = (TextResource*)allocator.allocate(sizeof(TextResource));
 
 		stream->read(&resource->length, sizeof(uint32_t));
 		
-		resource->data = new char[resource->length + 1];
-		
-		printf("Resource length: %d\n", resource->length);
-		
+		resource->data = (char*)allocator.allocate(sizeof(char) * (resource->length + 1));
+
 		stream->read(resource->data, (size_t)resource->length);
 		
 		resource->data[resource->length] = '\0';
@@ -40,10 +35,14 @@ TextResource* TextResource::load(ResourceArchive* archive, ResourceId id)
 }
 
 //-----------------------------------------------------------------------------
-void TextResource::unload(TextResource* text)
+void TextResource::unload(Allocator& allocator, TextResource* text)
 {
+	assert(text != NULL);
+
+	text->length = 0;
 
+	allocator.deallocate(text->data);
+	allocator.deallocate(text);
 }
 
 } // namespace crown
-

+ 3 - 3
src/TextResource.h

@@ -7,17 +7,17 @@ namespace crown
 {
 
 class ResourceArchive;
+class Allocator;
 
 class TextResource
 {
 public:
 
-	static TextResource*		load(ResourceArchive* archive, ResourceId id);
-	static void					unload(TextResource* text);
+	static TextResource*		load(Allocator& allocator, ResourceArchive* archive, ResourceId id);
+	static void					unload(Allocator& allocator, TextResource* text);
 
 	uint32_t					length;
 	char*						data;
 };
 
 } // namespace crown
-

+ 10 - 16
src/TextureResource.cpp

@@ -3,22 +3,21 @@
 #include "Log.h"
 #include "FileStream.h"
 #include <cassert>
+#include "Allocator.h"
 
 namespace crown
 {
 
 //-----------------------------------------------------------------------------
-TextureResource* TextureResource::load(ResourceArchive* archive, ResourceId id)
+TextureResource* TextureResource::load(Allocator& allocator, ResourceArchive* archive, ResourceId id)
 {
 	assert(archive != NULL);
 	
-	Log::D("TextureResource::load called.");
-	
 	FileStream* stream = archive->find(id);
 
 	if (stream != NULL)
 	{
-		TextureResource* resource = new TextureResource;
+		TextureResource* resource = (TextureResource*)allocator.allocate(sizeof(TextureResource));
 	
 		stream->read(&resource->format, sizeof(PixelFormat));
 		stream->read(&resource->width, sizeof(uint16_t));
@@ -27,17 +26,10 @@ TextureResource* TextureResource::load(ResourceArchive* archive, ResourceId id)
 		stream->read(&resource->mode, sizeof(TextureMode));
 		stream->read(&resource->filter, sizeof(TextureFilter));
 		stream->read(&resource->wrap, sizeof(TextureWrap));
-		
-		printf("Debug: Format = %d\n", resource->format);
-		printf("Debug: Width  = %d\n", resource->width);
-		printf("Debug: Height = %d\n", resource->height);
-		printf("Debug: Mode   = %d\n", resource->mode);
-		printf("Debug: Filter = %d\n", resource->filter);
-		printf("Debug: Wrap   = %d\n", resource->wrap);
 	
 		size_t size = resource->width * resource->height * Pixel::GetBytesPerPixel(resource->format);
 
-		resource->data = new uint8_t[size];
+		resource->data = (uint8_t*)allocator.allocate(sizeof(uint8_t) * size);
 
 		stream->read(resource->data, size);
 
@@ -48,10 +40,12 @@ TextureResource* TextureResource::load(ResourceArchive* archive, ResourceId id)
 }
 
 //-----------------------------------------------------------------------------
-void TextureResource::unload(TextureResource* resource)
+void TextureResource::unload(Allocator& allocator, TextureResource* resource)
 {
-	// TODO
+	assert(resource != NULL);
+
+	allocator.deallocate(resource->data);
+	allocator.deallocate(resource);
 }
-	
-} // namespace crown
 
+} // namespace crown

+ 4 - 4
src/TextureResource.h

@@ -66,13 +66,14 @@ enum TextureWrap
 };
 
 class ResourceArchive;
+class Allocator;
 
 class TextureResource
 {
 public:
 
-	static TextureResource*		load(ResourceArchive* archive, ResourceId id);
-	static void					unload(TextureResource* texture);
+	static TextureResource*		load(Allocator& allocator, ResourceArchive* archive, ResourceId id);
+	static void					unload(Allocator& allocator, TextureResource* texture);
 
 private:
 
@@ -84,8 +85,7 @@ private:
 	TextureFilter				filter;
 	TextureWrap					wrap;
 
-	void*						data;
+	uint8_t*					data;
 };
 
 } // namespace crown
-