Fix coverity #29357
[supertux.git] / src / supertux / textscroller.cpp
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
17 #include "supertux/textscroller.hpp"
18
19 #include "audio/sound_manager.hpp"
20 #include "control/input_manager.hpp"
21 #include "lisp/parser.hpp"
22 #include "supertux/fadeout.hpp"
23 #include "supertux/info_box_line.hpp"
24 #include "supertux/globals.hpp"
25 #include "supertux/screen_manager.hpp"
26 #include "supertux/resources.hpp"
27 #include "util/reader.hpp"
28 #include "video/drawing_context.hpp"
29
30 #include <sstream>
31 #include <stdexcept>
32
33 static const float DEFAULT_SPEED = 20;
34 static const float LEFT_BORDER = 50;
35 static const float SCROLL = 60;
36
37 TextScroller::TextScroller(const std::string& filename) :
38   defaultspeed(),
39   speed(),
40   music(),
41   background(),
42   lines(),
43   scroll(),
44   fading()
45 {
46   defaultspeed = DEFAULT_SPEED;
47   speed = defaultspeed;
48
49   std::string text;
50   std::string background_file;
51
52   lisp::Parser parser;
53   try {
54     const lisp::Lisp* root = parser.parse(filename);
55
56     const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
57     if(!text_lisp)
58       throw std::runtime_error("File isn't a supertux-text file");
59
60     if(!text_lisp->get("text", text))
61       throw std::runtime_error("file doesn't contain a text field");
62     if(!text_lisp->get("background", background_file))
63       throw std::runtime_error("file doesn't contain a background file");
64     text_lisp->get("speed", defaultspeed);
65     text_lisp->get("music", music);
66   } catch(std::exception& e) {
67     std::ostringstream msg;
68     msg << "Couldn't load file '" << filename << "': " << e.what() << std::endl;
69     throw std::runtime_error(msg.str());
70   }
71
72   // Split text string lines into a vector
73   lines = InfoBoxLine::split(text, SCREEN_WIDTH - 2*LEFT_BORDER);
74
75   // load background image
76   background = Surface::create("images/background/" + background_file);
77
78   scroll = 0;
79   fading = false;
80 }
81
82 TextScroller::~TextScroller()
83 {
84   for(std::vector<InfoBoxLine*>::iterator i = lines.begin(); i != lines.end(); ++i) delete *i;
85 }
86
87 void
88 TextScroller::setup()
89 {
90   SoundManager::current()->play_music(music);
91 }
92
93 void
94 TextScroller::update(float elapsed_time)
95 {
96   Controller* controller = InputManager::current()->get_controller();
97   if(controller->hold(Controller::UP)) {
98     speed = -defaultspeed*5;
99   } else if(controller->hold(Controller::DOWN)) {
100     speed = defaultspeed*5;
101   } else {
102     speed = defaultspeed;
103   }
104   if((controller->pressed(Controller::JUMP)
105      || controller->pressed(Controller::ACTION)
106      || controller->pressed(Controller::MENU_SELECT)
107      )&& !(controller->pressed(Controller::UP))) // prevent skipping if jump with up is enabled
108     scroll += SCROLL;
109   if(controller->pressed(Controller::START) ||
110      controller->pressed(Controller::ESCAPE)) {
111     ScreenManager::current()->pop_screen(std::unique_ptr<ScreenFade>(new FadeOut(0.5)));
112   }
113
114   scroll += speed * elapsed_time;
115
116   if(scroll < 0)
117     scroll = 0;
118 }
119
120 void
121 TextScroller::draw(DrawingContext& context)
122 {
123   context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
124                            Color(0.6f, 0.7f, 0.8f, 0.5f), 0);
125   context.draw_surface_part(background, Rectf(0, 0, background->get_width(), background->get_height()),
126                             Rectf(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT), 0);
127
128
129   float y = SCREEN_HEIGHT - scroll;
130   for(size_t i = 0; i < lines.size(); i++) {
131     if (y + lines[i]->get_height() >= 0 && SCREEN_HEIGHT - y >= 0) {
132       lines[i]->draw(context, Rectf(LEFT_BORDER, y, SCREEN_WIDTH - 2*LEFT_BORDER, y), LAYER_GUI);
133     }
134
135     y += lines[i]->get_height();
136   }
137
138   if(y < 0 && !fading ) {
139     fading = true;
140     ScreenManager::current()->pop_screen(std::unique_ptr<ScreenFade>(new FadeOut(0.5)));
141   }
142 }
143
144 /* EOF */