💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
3.6 KiB

  1. /*
  2. * Copyright (C) 2020 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "render-system.hpp"
  20. #include "game/components/transform-component.hpp"
  21. #include "renderer/renderer.hpp"
  22. using namespace ecs;
  23. render_system::render_system(entt::registry& registry):
  24. entity_system(registry),
  25. renderer(nullptr)
  26. {
  27. registry.on_construct<model_component>().connect<&render_system::on_model_construct>(this);
  28. registry.on_replace<model_component>().connect<&render_system::on_model_replace>(this);
  29. registry.on_destroy<model_component>().connect<&render_system::on_model_destroy>(this);
  30. }
  31. void render_system::update(double t, double dt)
  32. {
  33. registry.view<transform_component, model_component>().each
  34. (
  35. [this](auto entity, auto& transform, auto& model)
  36. {
  37. model_instance* instance = model_instances[entity];
  38. instance->set_transform(transform.transform);
  39. if (transform.warp)
  40. {
  41. instance->get_transform_tween().update();
  42. instance->update_tweens();
  43. transform.warp = false;
  44. }
  45. }
  46. );
  47. }
  48. void render_system::render(double alpha)
  49. {
  50. if (renderer)
  51. {
  52. for (const scene* scene: layers)
  53. {
  54. renderer->render(alpha, *scene);
  55. }
  56. }
  57. }
  58. void render_system::add_layer(::scene* layer)
  59. {
  60. layers.push_back(layer);
  61. }
  62. void render_system::remove_layers()
  63. {
  64. layers.clear();
  65. }
  66. void render_system::set_renderer(::renderer* renderer)
  67. {
  68. this->renderer = renderer;
  69. }
  70. void render_system::update_model_and_materials(entt::entity entity, model_component& model)
  71. {
  72. if (auto model_it = model_instances.find(entity); model_it != model_instances.end())
  73. {
  74. model_it->second->set_model(model.model);
  75. model_it->second->set_instanced((model.instance_count > 0), model.instance_count);
  76. for (auto material_it = model.materials.begin(); material_it != model.materials.end(); ++material_it)
  77. {
  78. model_it->second->set_material(material_it->first, material_it->second);
  79. }
  80. // Add model instance to its specified layers
  81. for (std::size_t i = 0; i < std::min<std::size_t>(layers.size(), (sizeof(model.layers) << 3)); ++i)
  82. {
  83. layers[i]->remove_object(model_it->second);
  84. if ((model.layers >> i) & 1)
  85. {
  86. layers[i]->add_object(model_it->second);
  87. }
  88. }
  89. }
  90. }
  91. void render_system::on_model_construct(entt::registry& registry, entt::entity entity, model_component& model)
  92. {
  93. ::model_instance* model_instance = new ::model_instance();
  94. model_instances[entity] = model_instance;
  95. update_model_and_materials(entity, model);
  96. }
  97. void render_system::on_model_replace(entt::registry& registry, entt::entity entity, model_component& model)
  98. {
  99. update_model_and_materials(entity, model);
  100. }
  101. void render_system::on_model_destroy(entt::registry& registry, entt::entity entity)
  102. {
  103. if (auto it = model_instances.find(entity); it != model_instances.end())
  104. {
  105. // Remove model instance from all layers
  106. for (std::size_t i = 0; i < layers.size(); ++i)
  107. {
  108. layers[i]->remove_object(it->second);
  109. }
  110. delete it->second;
  111. model_instances.erase(it);
  112. }
  113. }