DecoratorStarfield.cpp 4.6 KB

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