background.hpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License
  15. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. #ifndef HEADER_SUPERTUX_OBJECT_BACKGROUND_HPP
  17. #define HEADER_SUPERTUX_OBJECT_BACKGROUND_HPP
  18. #include "math/vector.hpp"
  19. #include "scripting/background.hpp"
  20. #include "squirrel/exposed_object.hpp"
  21. #include "supertux/game_object.hpp"
  22. #include "supertux/timer.hpp"
  23. #include "video/blend.hpp"
  24. #include "video/drawing_context.hpp"
  25. #include "video/flip.hpp"
  26. #include "video/surface_ptr.hpp"
  27. class ReaderMapping;
  28. class Background final : public GameObject,
  29. public ExposedObject<Background, scripting::Background>
  30. {
  31. public:
  32. Background();
  33. Background(const ReaderMapping& reader);
  34. ~Background() override;
  35. virtual void update(float dt_sec) override;
  36. virtual void draw(DrawingContext& context) override;
  37. static std::string class_name() { return "background"; }
  38. virtual std::string get_class_name() const override { return class_name(); }
  39. static std::string display_name() { return _("Background"); }
  40. virtual std::string get_display_name() const override { return display_name(); }
  41. virtual const std::string get_icon_path() const override {
  42. return "images/engine/editor/background.png";
  43. }
  44. virtual ObjectSettings get_settings() override;
  45. virtual void after_editor_set() override;
  46. virtual void on_flip(float height) override;
  47. void set_image(const std::string& name);
  48. void set_images(const std::string& name_top, const std::string& name_middle, const std::string& name_bottom);
  49. void set_speed(float bgd_speed);
  50. void draw_image(DrawingContext& context, const Vector& pos);
  51. std::string get_image() const { return m_imagefile; }
  52. float get_speed() const { return m_parallax_speed.x; }
  53. int get_layer() const { return m_layer; }
  54. Color get_color() const { return m_color; }
  55. void set_color(Color color) { m_color = color; }
  56. void fade_color(Color color, float time);
  57. private:
  58. enum Alignment {
  59. NO_ALIGNMENT,
  60. LEFT_ALIGNMENT,
  61. RIGHT_ALIGNMENT,
  62. TOP_ALIGNMENT,
  63. BOTTOM_ALIGNMENT
  64. };
  65. private:
  66. SurfacePtr load_background(const std::string& image_path);
  67. private:
  68. /** Backgrounds with NO_ALIGNMENT are repeated over the whole
  69. screen, backgrounds with left, right, top, bottom alignment are
  70. only repeated in one direction and attached to the level edge. */
  71. Alignment m_alignment;
  72. /** If fill is set, the background will not repeat and is instead
  73. stretched over the whole screen, alignment and top/bottom images
  74. are ignored in that case. */
  75. bool m_fill;
  76. int m_layer;
  77. std::string m_imagefile_top;
  78. std::string m_imagefile;
  79. std::string m_imagefile_bottom;
  80. Vector m_pos; /**< coordinates of upper-left corner of image */
  81. Vector m_parallax_speed;
  82. Vector m_scroll_speed;
  83. Vector m_scroll_offset;
  84. SurfacePtr m_image_top; /**< image to draw above pos */
  85. SurfacePtr m_image; /**< image to draw, anchored at pos */
  86. SurfacePtr m_image_bottom; /**< image to draw below pos+screenheight */
  87. Blend m_blend;
  88. Color m_color;
  89. DrawingTarget m_target;
  90. Timer m_timer_color;
  91. Color m_src_color, m_dst_color;
  92. Flip m_flip;
  93. private:
  94. Background(const Background&) = delete;
  95. Background& operator=(const Background&) = delete;
  96. };
  97. #endif
  98. /* EOF */