Преглед изворни кода

Update TextureResource and FileBundle to reflect new data formats

Daniele Bartolini пре 12 година
родитељ
комит
8ca984c518
4 измењених фајлова са 36 додато и 26 уклоњено
  1. 8 7
      src/FileBundle.cpp
  2. 7 5
      src/FileBundle.h
  3. 6 8
      src/TextureResource.cpp
  4. 15 6
      src/TextureResource.h

+ 8 - 7
src/FileBundle.cpp

@@ -51,19 +51,20 @@ DiskFile* FileBundle::open(ResourceId name)
 {
 	// Convert name/type into strings
 	char resource_name[512];
-
-	// Fixme
 	snprintf(resource_name, 512, "%.8X%.8X", name.name, name.type);
 
 	// Search the resource in the filesystem
-	if (m_filesystem.exists(resource_name) == false)
-	{
-		return NULL;
-	}
+	bool exists = m_filesystem.exists(resource_name);
+	CE_ASSERT(exists == true, "Resource does not exist: %s", resource_name);
 
+	// Open the resource and check magic number/version
 	DiskFile* file = (DiskFile*)m_filesystem.open(resource_name, FOM_READ);
 
-	file->skip(sizeof(ResourceHeader));
+	ResourceHeader header;
+	file->read(&header, sizeof(ResourceHeader));
+
+	CE_ASSERT(header.magic == RESOURCE_MAGIC_NUMBER, "Resource is not valid: %s", resource_name);
+	CE_ASSERT(header.version == RESOURCE_VERSION, "Resource version mismatch: %s", resource_name);
 
 	return file;
 }

+ 7 - 5
src/FileBundle.h

@@ -35,15 +35,17 @@ namespace crown
 class Filesystem;
 class DiskFile;
 
-// The header of every compiled resource file.
-// KEEP IN SYNC WITH CompiledResource struct in Compiler.h!
+const uint32_t	RESOURCE_MAGIC_NUMBER		= 0xCE010101;
+const uint32_t	RESOURCE_VERSION			= 2;
+
+/// Contains the header data common to all
+/// types of resources passing through the
+/// standard Compiler mechanics.
 struct ResourceHeader
 {
 	uint32_t	magic;		// Magic number used to identify the file
 	uint32_t	version;	// Version of the compiler used to compile the resource
-	uint32_t	name;		// Name of the resource (murmur2_32 hash)
-	uint32_t	type;		// Type of the resource (murmur2_32 hash)
-	uint32_t	size;		// Size of the resource data _not_ including header (in bytes)
+	uint32_t	size;		// Size of the resource data _not_ including this header in bytes
 };
 
 /// Source of resources

+ 6 - 8
src/TextureResource.cpp

@@ -39,23 +39,21 @@ namespace crown
 //-----------------------------------------------------------------------------
 void* TextureResource::load(Allocator& allocator, Bundle& bundle, ResourceId id)
 {
-	DiskFile* stream = bundle.open(id);
+	DiskFile* file = bundle.open(id);
 
-	CE_ASSERT(stream != NULL, "Resource does not exist: %.8X%.8X", id.name, id.type);
+	CE_ASSERT(file != NULL, "Resource does not exist: %.8X%.8X", id.name, id.type);
 
 	TextureResource* resource = (TextureResource*)allocator.allocate(sizeof(TextureResource));
 
-	stream->read(&resource->m_format, sizeof(PixelFormat));
-	stream->read(&resource->m_width, sizeof(uint16_t));
-	stream->read(&resource->m_height, sizeof(uint16_t));
+	file->read(&resource->m_header, sizeof(TextureHeader));
 
-	size_t size = resource->m_width * resource->m_height * Pixel::bytes_per_pixel(resource->m_format);
+	size_t size = resource->width() * resource->height() * Pixel::bytes_per_pixel(resource->format());
 
 	resource->m_data = (uint8_t*)allocator.allocate(sizeof(uint8_t) * size);
 
-	stream->read(resource->m_data, size);
+	file->read(resource->m_data, size);
 
-	bundle.close(stream);
+	bundle.close(file);
 
 	return resource;
 }

+ 15 - 6
src/TextureResource.h

@@ -34,6 +34,17 @@ OTHER DEALINGS IN THE SOFTWARE.
 namespace crown
 {
 
+// Bump the version whenever a change in the header is made
+const uint32_t TEXTURE_VERSION = 1;
+
+struct TextureHeader
+{
+	uint32_t	version;	// Texture file version
+	uint32_t	format;		// Format of the pixels
+	uint32_t	width;		// Width in pixels
+	uint32_t	height;		// Height in pixels
+};
+
 class Bundle;
 class Allocator;
 
@@ -48,16 +59,14 @@ public:
 
 public:
 
-	PixelFormat			format() const { return m_format; }
-	uint16_t			width() const { return m_width; }
-	uint16_t			height() const { return m_height; }
+	PixelFormat			format() const { return (PixelFormat) m_header.format; }
+	uint32_t			width() const { return m_header.width; }
+	uint32_t			height() const { return m_header.height; }
 	const uint8_t*		data() const { return m_data; }
 
 private:
 
-	PixelFormat			m_format;
-	uint16_t			m_width;
-	uint16_t			m_height;
+	TextureHeader		m_header;
 	uint8_t*			m_data;
 };