unit_resource.cpp 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2012-2020 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. #include "config.h"
  6. #include "core/containers/array.inl"
  7. #include "core/memory/globals.h"
  8. #include "core/memory/memory.inl"
  9. #include "resource/compile_options.inl"
  10. #include "resource/unit_compiler.h"
  11. #include "resource/unit_resource.h"
  12. namespace crown
  13. {
  14. #if CROWN_CAN_COMPILE
  15. namespace unit_resource_internal
  16. {
  17. s32 compile(CompileOptions& opts)
  18. {
  19. Buffer unit_data(default_allocator());
  20. UnitCompiler uc(opts);
  21. s32 err = uc.compile_unit(opts.source_path());
  22. DATA_COMPILER_ENSURE(err == 0, opts);
  23. opts.write(uc.blob());
  24. return 0;
  25. }
  26. } // namespace unit_resource_internal
  27. #endif // CROWN_CAN_COMPILE
  28. namespace unit_resource
  29. {
  30. const ComponentData* component_type_data(const UnitResource* ur, const ComponentData* component)
  31. {
  32. if (component == NULL)
  33. return (ComponentData*)(&ur[1]);
  34. else
  35. return (ComponentData*)memory::align_top(component_payload(component) + component->data_size, alignof(ComponentData));
  36. }
  37. const u32* component_unit_index(const ComponentData* component)
  38. {
  39. return (u32*)(&component[1]);
  40. }
  41. const char* component_payload(const ComponentData* component)
  42. {
  43. const u32* unit_index = component_unit_index(component);
  44. return (char*)memory::align_top(unit_index + component->num_instances, 16);
  45. }
  46. } // namespace unit_resource
  47. } // namespace crown