resource_registry.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * Copyright (c) 2012-2014 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. uint64_t type;
  42. ResourceCompileCallback on_compile;
  43. ResourceLoadCallback on_load;
  44. ResourceUnloadCallback on_unload;
  45. ResourceOnlineCallback on_online;
  46. ResourceOfflineCallback on_offline;
  47. };
  48. static const ResourceCallback RESOURCE_CALLBACK_REGISTRY[] =
  49. {
  50. { LUA_TYPE, lur::compile, lur::load, lur::unload, lur::online, lur::offline },
  51. { TEXTURE_TYPE, txr::compile, txr::load, txr::unload, txr::online, txr::offline },
  52. { MESH_TYPE, mhr::compile, mhr::load, mhr::unload, mhr::online, mhr::offline },
  53. { SOUND_TYPE, sdr::compile, sdr::load, sdr::unload, sdr::online, sdr::offline },
  54. { UNIT_TYPE, utr::compile, utr::load, utr::unload, utr::online, utr::offline },
  55. { SPRITE_TYPE, spr::compile, spr::load, spr::unload, spr::online, spr::offline },
  56. { PACKAGE_TYPE, pkr::compile, pkr::load, pkr::unload, pkr::online, pkr::offline },
  57. { PHYSICS_TYPE, phr::compile, phr::load, phr::unload, phr::online, phr::offline },
  58. { MATERIAL_TYPE, mtr::compile, mtr::load, mtr::unload, mtr::online, mtr::offline },
  59. { PHYSICS_CONFIG_TYPE, pcr::compile, pcr::load, pcr::unload, pcr::online, pcr::offline },
  60. { FONT_TYPE, ftr::compile, ftr::load, ftr::unload, ftr::online, ftr::offline },
  61. { LEVEL_TYPE, lvr::compile, lvr::load, lvr::unload, lvr::online, lvr::offline },
  62. { SHADER_TYPE, shr::compile, shr::load, shr::unload, shr::online, shr::offline },
  63. { SPRITE_ANIMATION_TYPE, sar::compile, sar::load, sar::unload, sar::online, sar::offline },
  64. { 0, NULL, NULL, NULL, NULL, NULL }
  65. };
  66. static const ResourceCallback* find_callback(uint64_t type)
  67. {
  68. const ResourceCallback* c = RESOURCE_CALLBACK_REGISTRY;
  69. while (c->type != 0 && c->type != type)
  70. {
  71. c++;
  72. }
  73. CE_ASSERT(c->type != 0, "Compiler not found");
  74. return c;
  75. }
  76. void resource_on_compile(uint64_t type, const char* path, CompileOptions& opts)
  77. {
  78. return find_callback(type)->on_compile(path, opts);
  79. }
  80. void* resource_on_load(uint64_t type, File& file, Allocator& a)
  81. {
  82. return find_callback(type)->on_load(file, a);
  83. }
  84. void resource_on_unload(uint64_t type, Allocator& allocator, void* resource)
  85. {
  86. return find_callback(type)->on_unload(allocator, resource);
  87. }
  88. void resource_on_online(uint64_t type, StringId64 id, ResourceManager& rm)
  89. {
  90. return find_callback(type)->on_online(id, rm);
  91. }
  92. void resource_on_offline(uint64_t type, StringId64 id, ResourceManager& rm)
  93. {
  94. return find_callback(type)->on_offline(id, rm);
  95. }
  96. } // namespace crown