resource_registry.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "resource_registry.h"
  6. #include "lua_resource.h"
  7. #include "texture_resource.h"
  8. #include "mesh_resource.h"
  9. #include "sound_resource.h"
  10. #include "sprite_resource.h"
  11. #include "package_resource.h"
  12. #include "unit_resource.h"
  13. #include "physics_resource.h"
  14. #include "material_resource.h"
  15. #include "font_resource.h"
  16. #include "level_resource.h"
  17. #include "shader.h"
  18. namespace crown
  19. {
  20. namespace pcr = physics_config_resource;
  21. namespace phr = physics_resource;
  22. namespace pkr = package_resource;
  23. namespace sdr = sound_resource;
  24. namespace mhr = mesh_resource;
  25. namespace utr = unit_resource;
  26. namespace txr = texture_resource;
  27. namespace mtr = material_resource;
  28. namespace lur = lua_resource;
  29. namespace ftr = font_resource;
  30. namespace lvr = level_resource;
  31. namespace spr = sprite_resource;
  32. namespace shr = shader_resource;
  33. namespace sar = sprite_animation_resource;
  34. typedef void (*ResourceCompileCallback)(const char* path, CompileOptions& opts);
  35. typedef void* (*ResourceLoadCallback)(File& file, Allocator& a);
  36. typedef void (*ResourceOnlineCallback)(StringId64 id, ResourceManager& rm);
  37. typedef void (*ResourceOfflineCallback)(StringId64 id, ResourceManager& rm);
  38. typedef void (*ResourceUnloadCallback)(Allocator& a, void* resource);
  39. struct ResourceCallback
  40. {
  41. StringId64 type;
  42. ResourceCompileCallback on_compile;
  43. ResourceLoadCallback on_load;
  44. ResourceUnloadCallback on_unload;
  45. ResourceOnlineCallback on_online;
  46. ResourceOfflineCallback on_offline;
  47. };
  48. #define NULL_RESOURCE_TYPE StringId64(uint64_t(0))
  49. static const ResourceCallback RESOURCE_CALLBACK_REGISTRY[] =
  50. {
  51. { LUA_TYPE, lur::compile, lur::load, lur::unload, lur::online, lur::offline },
  52. { TEXTURE_TYPE, txr::compile, txr::load, txr::unload, txr::online, txr::offline },
  53. { MESH_TYPE, mhr::compile, mhr::load, mhr::unload, mhr::online, mhr::offline },
  54. { SOUND_TYPE, sdr::compile, sdr::load, sdr::unload, sdr::online, sdr::offline },
  55. { UNIT_TYPE, utr::compile, utr::load, utr::unload, utr::online, utr::offline },
  56. { SPRITE_TYPE, spr::compile, spr::load, spr::unload, spr::online, spr::offline },
  57. { PACKAGE_TYPE, pkr::compile, pkr::load, pkr::unload, pkr::online, pkr::offline },
  58. { PHYSICS_TYPE, phr::compile, phr::load, phr::unload, phr::online, phr::offline },
  59. { MATERIAL_TYPE, mtr::compile, mtr::load, mtr::unload, mtr::online, mtr::offline },
  60. { PHYSICS_CONFIG_TYPE, pcr::compile, pcr::load, pcr::unload, pcr::online, pcr::offline },
  61. { FONT_TYPE, ftr::compile, ftr::load, ftr::unload, ftr::online, ftr::offline },
  62. { LEVEL_TYPE, lvr::compile, lvr::load, lvr::unload, lvr::online, lvr::offline },
  63. { SHADER_TYPE, shr::compile, shr::load, shr::unload, shr::online, shr::offline },
  64. { SPRITE_ANIMATION_TYPE, sar::compile, sar::load, sar::unload, sar::online, sar::offline },
  65. { NULL_RESOURCE_TYPE, NULL, NULL, NULL, NULL, NULL }
  66. };
  67. static const ResourceCallback* find_callback(StringId64 type)
  68. {
  69. const ResourceCallback* c = RESOURCE_CALLBACK_REGISTRY;
  70. while (c->type != NULL_RESOURCE_TYPE && c->type != type)
  71. {
  72. c++;
  73. }
  74. CE_ASSERT(c->type != NULL_RESOURCE_TYPE, "Compiler not found");
  75. return c;
  76. }
  77. void resource_on_compile(StringId64 type, const char* path, CompileOptions& opts)
  78. {
  79. return find_callback(type)->on_compile(path, opts);
  80. }
  81. void* resource_on_load(StringId64 type, File& file, Allocator& a)
  82. {
  83. return find_callback(type)->on_load(file, a);
  84. }
  85. void resource_on_unload(StringId64 type, Allocator& allocator, void* resource)
  86. {
  87. return find_callback(type)->on_unload(allocator, resource);
  88. }
  89. void resource_on_online(StringId64 type, StringId64 id, ResourceManager& rm)
  90. {
  91. return find_callback(type)->on_online(id, rm);
  92. }
  93. void resource_on_offline(StringId64 type, StringId64 id, ResourceManager& rm)
  94. {
  95. return find_callback(type)->on_offline(id, rm);
  96. }
  97. } // namespace crown