textscroller_screen.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SuperTux
  2. // Copyright (C) 2006 Matthias Braun <matze@braunis.de>
  3. // Copyright (C) 2016 M. Teufel <mteufel@supertux.org>
  4. //
  5. // This program is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. #include "supertux/textscroller_screen.hpp"
  18. #include <sstream>
  19. #include "audio/sound_manager.hpp"
  20. #include "control/input_manager.hpp"
  21. #include "object/textscroller.hpp"
  22. #include "supertux/fadetoblack.hpp"
  23. #include "supertux/globals.hpp"
  24. #include "supertux/info_box_line.hpp"
  25. #include "supertux/screen_manager.hpp"
  26. #include "util/log.hpp"
  27. #include "util/reader.hpp"
  28. #include "util/reader_document.hpp"
  29. #include "util/reader_mapping.hpp"
  30. #include "video/compositor.hpp"
  31. #include "video/drawing_context.hpp"
  32. #include "video/surface.hpp"
  33. #include "video/video_system.hpp"
  34. #include "video/viewport.hpp"
  35. namespace {
  36. const float DEFAULT_SPEED = 20;
  37. } // namespace
  38. TextScrollerScreen::TextScrollerScreen(const std::string& filename) :
  39. m_defaultspeed(DEFAULT_SPEED),
  40. m_music(),
  41. m_background(),
  42. m_text_scroller()
  43. {
  44. std::string background_file;
  45. try {
  46. auto doc = ReaderDocument::from_file(filename);
  47. auto root = doc.get_root();
  48. m_text_scroller = std::make_unique<TextScroller>(root);
  49. if (root.get_name() != "supertux-text") {
  50. throw std::runtime_error("File isn't a supertux-text file");
  51. } else {
  52. auto text_mapping = root.get_mapping();
  53. if (!text_mapping.get("background", background_file)) {
  54. throw std::runtime_error("File doesn't contain a background file");
  55. }
  56. text_mapping.get("speed", m_defaultspeed);
  57. text_mapping.get("music", m_music);
  58. m_text_scroller->set_default_speed(m_defaultspeed);
  59. }
  60. } catch (std::exception& e) {
  61. std::ostringstream msg;
  62. msg << "Couldn't load file '" << filename << "': " << e.what() << std::endl;
  63. throw std::runtime_error(msg.str());
  64. }
  65. // load background image
  66. m_background = Surface::from_file("images/background/" + background_file);
  67. }
  68. TextScrollerScreen::~TextScrollerScreen()
  69. {
  70. }
  71. void
  72. TextScrollerScreen::setup()
  73. {
  74. SoundManager::current()->play_music(m_music);
  75. ScreenManager::current()->set_screen_fade(std::make_unique<FadeToBlack>(FadeToBlack::FADEIN, 0.5f));
  76. }
  77. void
  78. TextScrollerScreen::update(float dt_sec, const Controller& controller)
  79. {
  80. // NOTE: Keyboard input is handled by the TextScroller class.
  81. m_text_scroller->update(dt_sec);
  82. }
  83. void
  84. TextScrollerScreen::draw(Compositor& compositor)
  85. {
  86. auto& context = compositor.make_context();
  87. const float ctx_w = context.get_width();
  88. const float ctx_h = context.get_height();
  89. { // draw background
  90. const float bg_w = static_cast<float>(m_background->get_width());
  91. const float bg_h = static_cast<float>(m_background->get_height());
  92. const float bg_ratio = bg_w / bg_h;
  93. const float ctx_ratio = ctx_w / ctx_h;
  94. if (bg_ratio > ctx_ratio)
  95. {
  96. const float new_bg_w = ctx_h * bg_ratio;
  97. context.color().draw_surface_scaled(m_background,
  98. Rectf::from_center(Vector(ctx_w / 2.0f, ctx_h / 2.0f),
  99. Sizef(new_bg_w, ctx_h)),
  100. 0);
  101. }
  102. else
  103. {
  104. const float new_bg_h = ctx_w / bg_ratio;
  105. context.color().draw_surface_scaled(m_background,
  106. Rectf::from_center(Vector(ctx_w / 2.0f, ctx_h / 2.0f),
  107. Sizef(ctx_w, new_bg_h)),
  108. 0);
  109. }
  110. }
  111. m_text_scroller->draw(context);
  112. }
  113. IntegrationStatus
  114. TextScrollerScreen::get_status() const
  115. {
  116. IntegrationStatus status;
  117. status.m_details.push_back("Watching a cutscene");
  118. return status;
  119. }
  120. /* EOF */