TextResource.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "TextResource.h"
  2. #include "DiskFile.h"
  3. #include "ResourceArchive.h"
  4. #include "Log.h"
  5. #include "Allocator.h"
  6. namespace crown
  7. {
  8. //-----------------------------------------------------------------------------
  9. void* TextResource::load(Allocator& allocator, ResourceArchive& archive, ResourceId id)
  10. {
  11. DiskFile* stream = archive.open(id);
  12. CE_ASSERT(stream != NULL, "Resource does not exist: %.8X%.8X", id.name, id.type);
  13. TextResource* resource = (TextResource*)allocator.allocate(sizeof(TextResource));
  14. stream->read(&resource->length, sizeof(uint32_t));
  15. resource->data = (char*)allocator.allocate(sizeof(char) * (resource->length + 1));
  16. stream->read(resource->data, (size_t)resource->length);
  17. resource->data[resource->length] = '\0';
  18. archive.close(stream);
  19. return resource;
  20. }
  21. //-----------------------------------------------------------------------------
  22. void TextResource::unload(Allocator& allocator, void* resource)
  23. {
  24. CE_ASSERT(resource != NULL, "Resource not loaded");
  25. ((TextResource*)resource)->length = 0;
  26. allocator.deallocate(((TextResource*)resource)->data);
  27. allocator.deallocate(resource);
  28. }
  29. //-----------------------------------------------------------------------------
  30. void TextResource::online(void* resource)
  31. {
  32. (void) resource;
  33. }
  34. //-----------------------------------------------------------------------------
  35. void TextResource::offline()
  36. {
  37. }
  38. } // namespace crown