Renamed Rect to Rectf
[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/joystickkeyboardcontroller.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 static const float DEFAULT_SPEED = 20;
31 static const float LEFT_BORDER = 50;
32 static const float SCROLL = 60;
33
34 TextScroller::TextScroller(const std::string& filename) :
35   defaultspeed(),
36   speed(),
37   music(),
38   background(),
39   lines(),
40   scroll(),
41   fading()
42 {
43   defaultspeed = DEFAULT_SPEED;
44   speed = defaultspeed;
45
46   std::string text;
47   std::string background_file;
48
49   lisp::Parser parser;
50   try {
51     const lisp::Lisp* root = parser.parse(filename);
52
53     const lisp::Lisp* text_lisp = root->get_lisp("supertux-text");
54     if(!text_lisp)
55       throw std::runtime_error("File isn't a supertux-text file");
56
57     if(!text_lisp->get("text", text))
58       throw std::runtime_error("file doesn't contain a text field");
59     if(!text_lisp->get("background", background_file))
60       throw std::runtime_error("file doesn't contain a background file");
61     text_lisp->get("speed", defaultspeed);
62     text_lisp->get("music", music);
63   } catch(std::exception& e) {
64     std::ostringstream msg;
65     msg << "Couldn't load file '" << filename << "': " << e.what() << std::endl;
66     throw std::runtime_error(msg.str());
67   }
68
69   // Split text string lines into a vector
70   lines = InfoBoxLine::split(text, SCREEN_WIDTH - 2*LEFT_BORDER);
71
72   // load background image
73   background = Surface::create("images/background/" + background_file);
74
75   scroll = 0;
76   fading = false;
77 }
78
79 TextScroller::~TextScroller()
80 {
81   for(std::vector<InfoBoxLine*>::iterator i = lines.begin(); i != lines.end(); i++) delete *i;
82 }
83
84 void
85 TextScroller::setup()
86 {
87   sound_manager->play_music(music);
88 }
89
90 void
91 TextScroller::update(float elapsed_time)
92 {
93   if(g_main_controller->hold(Controller::UP)) {
94     speed = -defaultspeed*5;
95   } else if(g_main_controller->hold(Controller::DOWN)) {
96     speed = defaultspeed*5;
97   } else {
98     speed = defaultspeed;
99   }
100   if(g_main_controller->pressed(Controller::JUMP)
101      || g_main_controller->pressed(Controller::ACTION)
102      || g_main_controller->pressed(Controller::MENU_SELECT))
103     scroll += SCROLL;
104   if(g_main_controller->pressed(Controller::PAUSE_MENU)) {
105     g_screen_manager->exit_screen(new FadeOut(0.5));
106   }
107
108   scroll += speed * elapsed_time;
109
110   if(scroll < 0)
111     scroll = 0;
112 }
113
114 void
115 TextScroller::draw(DrawingContext& context)
116 {
117   context.draw_filled_rect(Vector(0, 0), Vector(SCREEN_WIDTH, SCREEN_HEIGHT),
118                            Color(0.6f, 0.7f, 0.8f, 0.5f), 0);
119   context.draw_surface(background.get(), Vector(SCREEN_WIDTH/2 - background->get_width()/2 , SCREEN_HEIGHT/2 - background->get_height()/2), 0);
120
121   float y = SCREEN_HEIGHT - scroll;
122   for(size_t i = 0; i < lines.size(); i++) {
123     if (y + lines[i]->get_height() >= 0 && SCREEN_HEIGHT - y >= 0) {
124       lines[i]->draw(context, Rectf(LEFT_BORDER, y, SCREEN_WIDTH - 2*LEFT_BORDER, y), LAYER_GUI);
125     }
126
127     y += lines[i]->get_height();
128   }
129
130   if(y < 0 && !fading ) {
131     fading = true;
132     g_screen_manager->exit_screen(new FadeOut(0.5));
133   }
134 }
135
136 /* EOF */