DecoratorStarfield.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "DecoratorStarfield.h"
  29. #include <RmlUi/Core/Math.h>
  30. #include <RmlUi/Core/Element.h>
  31. #include <Shell.h>
  32. #include <ShellOpenGL.h>
  33. #include "GameDetails.h"
  34. float last_update_time = 0.0f;
  35. DecoratorStarfield::~DecoratorStarfield()
  36. {
  37. }
  38. bool DecoratorStarfield::Initialise(int _num_layers, const Rml::Core::Colourb& _top_colour, const Rml::Core::Colourb& _bottom_colour, float _top_speed, float _bottom_speed, int _top_density, int _bottom_density)
  39. {
  40. num_layers = _num_layers;
  41. top_colour = _top_colour;
  42. bottom_colour = _bottom_colour;
  43. top_speed = _top_speed;
  44. bottom_speed = _bottom_speed;
  45. top_density = _top_density;
  46. bottom_density = _bottom_density;
  47. return true;
  48. }
  49. /// Called on a decorator to generate any required per-element data for a newly decorated element.
  50. Rml::Core::DecoratorDataHandle DecoratorStarfield::GenerateElementData(Rml::Core::Element* element) const
  51. {
  52. StarField* star_field = new StarField();
  53. star_field->star_layers.resize(num_layers);
  54. for (int i = 0; i < num_layers; i++)
  55. {
  56. float layer_depth = i / (float)num_layers;
  57. int density = Rml::Core::Math::RealToInteger((top_density * layer_depth) + (bottom_density * (1.0f - layer_depth)));
  58. star_field->star_layers[i].stars.resize(density);
  59. Rml::Core::Colourb colour = (top_colour * layer_depth) + (bottom_colour * (1.0f - layer_depth));
  60. star_field->star_layers[i].colour = colour;
  61. float speed = (top_speed * layer_depth) + (bottom_speed * (1.0f - layer_depth));
  62. star_field->star_layers[i].speed = speed;
  63. star_field->dimensions = element->GetBox().GetSize(Rml::Core::Box::PADDING);
  64. if (star_field->dimensions.x > 0)
  65. {
  66. for (int j = 0; j < density; j++)
  67. {
  68. star_field->star_layers[i].stars[j].x = (float) Rml::Core::Math::RandomReal(star_field->dimensions.x);
  69. star_field->star_layers[i].stars[j].y = (float) Rml::Core::Math::RandomReal(star_field->dimensions.y);
  70. }
  71. }
  72. star_field->last_update = Shell::GetElapsedTime();
  73. }
  74. return (Rml::Core::DecoratorDataHandle)star_field;
  75. }
  76. // Called to release element data generated by this decorator.
  77. void DecoratorStarfield::ReleaseElementData(Rml::Core::DecoratorDataHandle element_data) const
  78. {
  79. delete (StarField*)element_data;
  80. }
  81. // Called to render the decorator on an element.
  82. void DecoratorStarfield::RenderElement(Rml::Core::Element* RMLUI_UNUSED_PARAMETER(element), Rml::Core::DecoratorDataHandle element_data) const
  83. {
  84. RMLUI_UNUSED(element);
  85. StarField* star_field = (StarField*)element_data;
  86. star_field->Update();
  87. glDisable(GL_TEXTURE_2D);
  88. glPointSize(2);
  89. glBegin(GL_POINTS);
  90. for (size_t i = 0; i < star_field->star_layers.size(); i++)
  91. {
  92. glColor4ubv(star_field->star_layers[i].colour);
  93. for (size_t j = 0; j < star_field->star_layers[i].stars.size(); j++)
  94. {
  95. glVertex2f(star_field->star_layers[i].stars[j].x, star_field->star_layers[i].stars[j].y);
  96. }
  97. }
  98. glEnd();
  99. glColor4ub(255, 255, 255, 255);
  100. }
  101. void DecoratorStarfield::StarField::Update()
  102. {
  103. double time = Shell::GetElapsedTime();
  104. float delta_time = float(time - last_update);
  105. last_update = time;
  106. if (!GameDetails::GetPaused())
  107. {
  108. for (size_t i = 0; i < star_layers.size(); i++)
  109. {
  110. float movement = star_layers[i].speed * delta_time;
  111. for (size_t j = 0; j < star_layers[i].stars.size(); j++)
  112. {
  113. star_layers[i].stars[j].y += movement;
  114. if (star_layers[i].stars[j].y > dimensions.y)
  115. {
  116. star_layers[i].stars[j].y = 0;
  117. star_layers[i].stars[j].x = Rml::Core::Math::RandomReal(dimensions.x);
  118. }
  119. }
  120. }
  121. }
  122. }