💿🐜 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.

139 lines
3.8 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. model_instance* render_system::get_model_instance(entt::entity entity)
  71. {
  72. if (auto it = model_instances.find(entity); it != model_instances.end())
  73. return it->second;
  74. return nullptr;
  75. }
  76. void render_system::update_model_and_materials(entt::entity entity, model_component& model)
  77. {
  78. if (auto model_it = model_instances.find(entity); model_it != model_instances.end())
  79. {
  80. model_it->second->set_model(model.model);
  81. model_it->second->set_instanced((model.instance_count > 0), model.instance_count);
  82. for (auto material_it = model.materials.begin(); material_it != model.materials.end(); ++material_it)
  83. {
  84. model_it->second->set_material(material_it->first, material_it->second);
  85. }
  86. // Add model instance to its specified layers
  87. for (std::size_t i = 0; i < std::min<std::size_t>(layers.size(), (sizeof(model.layers) << 3)); ++i)
  88. {
  89. layers[i]->remove_object(model_it->second);
  90. if ((model.layers >> i) & 1)
  91. {
  92. layers[i]->add_object(model_it->second);
  93. }
  94. }
  95. }
  96. }
  97. void render_system::on_model_construct(entt::registry& registry, entt::entity entity, model_component& model)
  98. {
  99. ::model_instance* model_instance = new ::model_instance();
  100. model_instances[entity] = model_instance;
  101. update_model_and_materials(entity, model);
  102. }
  103. void render_system::on_model_replace(entt::registry& registry, entt::entity entity, model_component& model)
  104. {
  105. update_model_and_materials(entity, model);
  106. }
  107. void render_system::on_model_destroy(entt::registry& registry, entt::entity entity)
  108. {
  109. if (auto it = model_instances.find(entity); it != model_instances.end())
  110. {
  111. // Remove model instance from all layers
  112. for (std::size_t i = 0; i < layers.size(); ++i)
  113. {
  114. layers[i]->remove_object(it->second);
  115. }
  116. delete it->second;
  117. model_instances.erase(it);
  118. }
  119. }