TextureResource.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include "TextureResource.h"
  2. #include "ResourceArchive.h"
  3. #include "Log.h"
  4. #include "FileStream.h"
  5. #include <cassert>
  6. #include "Allocator.h"
  7. #include "Device.h"
  8. #include "Renderer.h"
  9. namespace crown
  10. {
  11. //-----------------------------------------------------------------------------
  12. TextureResource* TextureResource::load(Allocator& allocator, ResourceArchive* archive, ResourceId id)
  13. {
  14. assert(archive != NULL);
  15. FileStream* stream = archive->find(id);
  16. if (stream != NULL)
  17. {
  18. TextureResource* resource = (TextureResource*)allocator.allocate(sizeof(TextureResource));
  19. stream->read(&resource->m_format, sizeof(PixelFormat));
  20. stream->read(&resource->m_width, sizeof(uint16_t));
  21. stream->read(&resource->m_height, sizeof(uint16_t));
  22. size_t size = resource->m_width * resource->m_height * Pixel::GetBytesPerPixel(resource->m_format);
  23. resource->m_data = (uint8_t*)allocator.allocate(sizeof(uint8_t) * size);
  24. stream->read(resource->m_data, size);
  25. return resource;
  26. }
  27. return NULL;
  28. }
  29. //-----------------------------------------------------------------------------
  30. void TextureResource::online(TextureResource* texture)
  31. {
  32. assert(texture != NULL);
  33. texture->m_render_texture = GetDevice()->renderer()->load_texture(texture);
  34. }
  35. //-----------------------------------------------------------------------------
  36. void TextureResource::unload(Allocator& allocator, TextureResource* resource)
  37. {
  38. assert(resource != NULL);
  39. allocator.deallocate(resource->m_data);
  40. allocator.deallocate(resource);
  41. }
  42. //-----------------------------------------------------------------------------
  43. void TextureResource::offline()
  44. {
  45. }
  46. } // namespace crown