SpriteResource.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #pragma once
  24. #include <cstring>
  25. #include <inttypes.h>
  26. #include "Types.h"
  27. #include "Allocator.h"
  28. #include "File.h"
  29. #include "OS.h"
  30. #include "StringUtils.h"
  31. #include "List.h"
  32. #include "Bundle.h"
  33. #include "Device.h"
  34. #include "ResourceManager.h"
  35. #include "RendererTypes.h"
  36. #include "Renderer.h"
  37. #include "TextureResource.h"
  38. #define MAX_SPRITE_ANIM_FRAMES 60
  39. #define SPRITE_FRAME_SIZE 4 * 4 * 4
  40. namespace crown
  41. {
  42. const uint32_t SPRITE_VERSION = 1;
  43. //-----------------------------------------------------------------------------
  44. struct SpriteHeader
  45. {
  46. char name[128];
  47. ResourceId texture;
  48. uint32_t num_frames;
  49. uint32_t frame_rate;
  50. uint32_t playback_mode;
  51. };
  52. //-----------------------------------------------------------------------------
  53. class SpriteResource
  54. {
  55. public:
  56. //-----------------------------------------------------------------------------
  57. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  58. {
  59. File* file = bundle.open(id);
  60. const size_t file_size = file->size() - 12;
  61. SpriteResource* res = (SpriteResource*) allocator.allocate(sizeof(SpriteResource));
  62. res->m_data = (uint8_t*) allocator.allocate(file_size);
  63. res->m_data_size = file_size;
  64. file->read(res->m_data, res->m_data_size);
  65. bundle.close(file);
  66. return res;
  67. }
  68. //-----------------------------------------------------------------------------
  69. static void online(void* resource)
  70. {
  71. SpriteResource* sr = (SpriteResource*)resource;
  72. static uint16_t t_indices[] = {0, 1, 2, 0, 2, 3};
  73. sr->m_vb = device()->renderer()->create_vertex_buffer(4, VertexFormat::P2_T2, sr->frame(0));
  74. sr->m_ib = device()->renderer()->create_index_buffer(6, t_indices);
  75. }
  76. //-----------------------------------------------------------------------------
  77. static void unload(Allocator& allocator, void* resource)
  78. {
  79. SpriteResource* res = (SpriteResource*)resource;
  80. allocator.deallocate(res->m_data);
  81. allocator.deallocate(res);
  82. }
  83. //-----------------------------------------------------------------------------
  84. static void offline(void* resource)
  85. {
  86. SpriteResource* sprite = (SpriteResource*) resource;
  87. Renderer* r = device()->renderer();
  88. r->destroy_vertex_buffer(sprite->m_vb);
  89. r->destroy_index_buffer(sprite->m_ib);
  90. }
  91. //-----------------------------------------------------------------------------
  92. const char* name()
  93. {
  94. SpriteHeader* header = (SpriteHeader*)m_data;
  95. return header->name;
  96. }
  97. //-----------------------------------------------------------------------------
  98. ResourceId texture()
  99. {
  100. SpriteHeader* header = (SpriteHeader*)m_data;
  101. return header->texture;
  102. }
  103. //-----------------------------------------------------------------------------
  104. uint32_t num_frames()
  105. {
  106. SpriteHeader* header = (SpriteHeader*)m_data;
  107. return header->num_frames;
  108. }
  109. //-----------------------------------------------------------------------------
  110. uint32_t frame_rate()
  111. {
  112. SpriteHeader* header = (SpriteHeader*)m_data;
  113. return header->frame_rate;
  114. }
  115. //-----------------------------------------------------------------------------
  116. uint32_t playback_mode()
  117. {
  118. SpriteHeader* header = (SpriteHeader*)m_data;
  119. return header->playback_mode;
  120. }
  121. //-----------------------------------------------------------------------------
  122. float* animation()
  123. {
  124. return (float*)(m_data + sizeof(SpriteHeader));
  125. }
  126. float* frame(uint32_t index)
  127. {
  128. return (float*)(m_data + sizeof(SpriteHeader) + SPRITE_FRAME_SIZE * index);
  129. }
  130. public:
  131. uint8_t* m_data;
  132. size_t m_data_size;
  133. VertexBufferId m_vb;
  134. IndexBufferId m_ib;
  135. };
  136. } // namespace crown