TextResource.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include "TextResource.h"
  2. #include "FileStream.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. FileStream* stream = archive.find(id);
  12. if (stream != NULL)
  13. {
  14. TextResource* resource = (TextResource*)allocator.allocate(sizeof(TextResource));
  15. stream->read(&resource->length, sizeof(uint32_t));
  16. resource->data = (char*)allocator.allocate(sizeof(char) * (resource->length + 1));
  17. stream->read(resource->data, (size_t)resource->length);
  18. resource->data[resource->length] = '\0';
  19. return resource;
  20. }
  21. return NULL;
  22. }
  23. //-----------------------------------------------------------------------------
  24. void TextResource::unload(Allocator& allocator, void* resource)
  25. {
  26. assert(resource != NULL);
  27. ((TextResource*)resource)->length = 0;
  28. allocator.deallocate(((TextResource*)resource)->data);
  29. allocator.deallocate(resource);
  30. }
  31. } // namespace crown