SpriteResource.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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 "ContainerTypes.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. namespace crown
  39. {
  40. const uint32_t SPRITE_VERSION = 1;
  41. struct SpriteHeader
  42. {
  43. VertexBufferId vb;
  44. IndexBufferId ib;
  45. uint32_t num_animations;
  46. uint32_t animations_offset;
  47. uint32_t num_vertices;
  48. uint32_t vertices_offset;
  49. uint32_t num_indices;
  50. uint32_t indices_offset;
  51. };
  52. struct SpriteAnimation
  53. {
  54. StringId32 name;
  55. float time;
  56. uint32_t num_frames;
  57. uint32_t start_frame;
  58. };
  59. //-----------------------------------------------------------------------------
  60. struct SpriteResource
  61. {
  62. //-----------------------------------------------------------------------------
  63. static void* load(Allocator& allocator, Bundle& bundle, ResourceId id)
  64. {
  65. File* file = bundle.open(id);
  66. const size_t file_size = file->size();
  67. void* res = allocator.allocate(file_size);
  68. file->read(res, file_size);
  69. bundle.close(file);
  70. return res;
  71. }
  72. //-----------------------------------------------------------------------------
  73. static void online(void* resource)
  74. {
  75. SpriteHeader* h = (SpriteHeader*) resource;
  76. const float* vertices = (float*) (((char*) resource) + h->vertices_offset);
  77. const uint16_t* indices = (uint16_t*) (((char*) resource) + h->indices_offset);
  78. h->vb = device()->renderer()->create_vertex_buffer(h->num_vertices * Vertex::bytes_per_vertex(VertexFormat::P2_T2), vertices, VertexFormat::P2_T2);
  79. h->ib = device()->renderer()->create_index_buffer(h->num_indices * sizeof(uint16_t), indices);
  80. }
  81. //-----------------------------------------------------------------------------
  82. static void unload(Allocator& allocator, void* resource)
  83. {
  84. CE_ASSERT_NOT_NULL(resource);
  85. allocator.deallocate(resource);
  86. }
  87. //-----------------------------------------------------------------------------
  88. static void offline(void* resource)
  89. {
  90. CE_ASSERT_NOT_NULL(resource);
  91. SpriteResource* sr = (SpriteResource*) resource;
  92. SpriteHeader* h = (SpriteHeader*) sr;
  93. device()->renderer()->destroy_vertex_buffer(h->vb);
  94. device()->renderer()->destroy_index_buffer(h->ib);
  95. }
  96. //-----------------------------------------------------------------------------
  97. const SpriteAnimation* get_animation(const char* name) const
  98. {
  99. SpriteHeader* h = (SpriteHeader*) this;
  100. SpriteAnimation* begin = (SpriteAnimation*) (((char*) this) + h->animations_offset);
  101. const StringId32 name_hash = string::murmur2_32(name, string::strlen(name));
  102. const uint32_t num = h->num_animations;
  103. for (uint32_t i = 0; i < num; i++)
  104. {
  105. if (begin[i].name == name_hash)
  106. return &begin[i];
  107. }
  108. return NULL;
  109. }
  110. uint32_t get_animation_frame(const SpriteAnimation* a, uint32_t frame) const
  111. {
  112. SpriteHeader* h = (SpriteHeader*) this;
  113. uint32_t* begin = (uint32_t*) (((char*) this) + h->animations_offset + sizeof(SpriteAnimation) * h->num_animations);
  114. return begin[a->start_frame + frame];
  115. }
  116. //-----------------------------------------------------------------------------
  117. VertexBufferId vertex_buffer() const
  118. {
  119. return ((SpriteHeader*) this)->vb;
  120. }
  121. //-----------------------------------------------------------------------------
  122. IndexBufferId index_buffer() const
  123. {
  124. return ((SpriteHeader*) this)->ib;
  125. }
  126. private:
  127. // Disable construction
  128. SpriteResource();
  129. };
  130. } // namespace crown