Debian Codegraph @

src/plan/SubTitleAgent.cpp

raw
1 /*
2 * Copyright (C) 2004 Ivo Danihelka ([email protected])
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 2 of the License, or
7 * (at your option) any later version.
8 */
9 #include "SubTitleAgent.h"
10
11 #include "Title.h"
12 #include "Font.h"
13 #include "ResColorPack.h"
14
15 #include "Path.h"
16 #include "OptionAgent.h"
17 #include "minmax.h"
18
19 //-----------------------------------------------------------------
20 void
21 SubTitleAgent::own_init()
22 {
23 m_limitY = TITLE_LIMIT_Y;
24 m_colors = new ResColorPack();
25
26 m_font = NULL;
27 m_font = new Font(Path::dataReadPath("font/font_subtitle.ttf"), 20);
28 }
29 //-----------------------------------------------------------------
30 /**
31 * Shift all titles up.
32 * Try remove the oldest subtitle.
33 */
34 void
35 SubTitleAgent::own_update()
36 {
37 if (!m_titles.empty()) {
38 shiftTitlesUp(TITLE_SPEED);
39
40 if (m_titles.front()->isGone()) {
41 delete m_titles.front();
42 m_titles.pop_front();
43 }
44 }
45 }
46 //-----------------------------------------------------------------
47 void
48 SubTitleAgent::own_shutdown()
49 {
50 removeAll();
51 delete m_colors;
52 if (m_font) {
53 delete m_font;
54 }
55 }
56
57 //-----------------------------------------------------------------
58 void
59 SubTitleAgent::addFont(const std::string &fontname, Color *new_color)
60 {
61 m_colors->addRes(fontname, new_color);
62 }
63 //-----------------------------------------------------------------
64 /**
65 * Create new subtitle.
66 * Shift all existing subtitles up.
67 */
68 void
69 SubTitleAgent::newSubtitle(const std::string &original,
70 const std::string &fontname)
71 {
72 const Color *color = m_colors->getRes(fontname);
73
74 std::string subtitle = original;
75 while (!subtitle.empty()) {
76 subtitle = splitAndCreate(subtitle, color);
77 }
78 }
79 //-----------------------------------------------------------------
80 /**
81 * Split subtitle, create title and return rest.
82 * @return rest or empty string
83 */
84 std::string
85 SubTitleAgent::splitAndCreate(const std::string &original,
86 const Color *color)
87 {
88 std::string subtitle = original;
89 int screen_width = OptionAgent::agent()->getAsInt("screen_width");
90
91 int text_width = m_font->calcTextWidth(subtitle);
92 while (text_width > screen_width - 2 * TITLE_BORDER) {
93 trimRest(subtitle);
94 text_width = m_font->calcTextWidth(subtitle);
95 }
96
97 std::string rest = "";
98 if (!subtitle.empty()) {
99 newShortSubtitle(subtitle, color);
100
101 if (original.size() > subtitle.size()) {
102 rest = original.substr(subtitle.size());
103 }
104 }
105 return rest;
106 }
107 //-----------------------------------------------------------------
108 /**
109 * Break long string.
110 * String is trimed at ' '
111 * but not at " . " (single char surrounded with spaces).
112 *
113 * @param buffer buffer to change
114 */
115 void
116 SubTitleAgent::trimRest(std::string &buffer)
117 {
118 int i;
119 for (i = buffer.size() - 1; i >= 0; --i) {
120 if (buffer[i] == ' ' &&
121 !(i - 2 >= 0 && buffer[i - 2] == ' '))
122 {
123 break;
124 }
125 }
126
127 if (i <= 0) {
128 LOG_WARNING(ExInfo("unbreakable string")
129 .addInfo("string", buffer));
130 if (buffer.size() > 4) {
131 buffer.erase(buffer.size() - 4);
132 }
133 else {
134 buffer = "";
135 }
136 }
137 else {
138 buffer.erase(i);
139 }
140 }
141 //-----------------------------------------------------------------
142 void
143 SubTitleAgent::newShortSubtitle(const std::string &subtitle,
144 const Color *color)
145 {
146 int startY = lowestY();
147 int finalY = TITLE_BASE + TITLE_ROW;
148 int bonusTime = (TITLE_BASE - startY + m_limitY - TITLE_LIMIT_Y)
149 / TITLE_SPEED;
150 Title *title = new Title(startY, finalY, bonusTime, m_limitY,
151 subtitle, m_font, color);
152 shiftFinalsUp(TITLE_ROW);
153 m_titles.push_back(title);
154 }
155 //-----------------------------------------------------------------
156 /**
157 * Increase Y for all existing titles.
158 */
159 void
160 SubTitleAgent::shiftTitlesUp(int rate)
161 {
162 t_titles::iterator end = m_titles.end();
163 for (t_titles::iterator i = m_titles.begin(); i != end; ++i) {
164 (*i)->shiftUp(rate);
165 }
166 }
167 //-----------------------------------------------------------------
168 /**
169 * Increase finalY for all existing titles.
170 */
171 void
172 SubTitleAgent::shiftFinalsUp(int rate)
173 {
174 t_titles::iterator end = m_titles.end();
175 for (t_titles::iterator i = m_titles.begin(); i != end; ++i) {
176 (*i)->shiftFinalUp(rate);
177 }
178 }
179 //-----------------------------------------------------------------
180 /**
181 * Get lowest possible Y.
182 * It can be negative.
183 */
184 int
185 SubTitleAgent::lowestY()
186 {
187 int lowest = TITLE_BASE;
188 if (!m_titles.empty()) {
189 int lastest = m_titles.back()->getY() - TITLE_ROW;
190 lowest = min(lowest, lastest);
191 }
192 return lowest;
193 }
194 //-----------------------------------------------------------------
195 /**
196 * Kill all running subtitles.
197 */
198 void
199 SubTitleAgent::killTalks()
200 {
201 t_titles::iterator end = m_titles.end();
202 for (t_titles::iterator i = m_titles.begin(); i != end; ++i) {
203 delete *i;
204 }
205 m_titles.clear();
206 }
207 //-----------------------------------------------------------------
208 /**
209 * Kill all subtitles and remove fonts.
210 */
211 void
212 SubTitleAgent::removeAll()
213 {
214 killTalks();
215 m_colors->removeAll();
216 }
217
218 //-----------------------------------------------------------------
219 /**
220 * Draw all subtitles.
221 */
222 void
223 SubTitleAgent::drawOn(SDL_Surface *screen)
224 {
225 if (OptionAgent::agent()->getAsBool("subtitles", true)) {
226 t_titles::iterator end = m_titles.end();
227 for (t_titles::iterator i = m_titles.begin(); i != end; ++i) {
228 (*i)->drawOn(screen);
229 }
230 }
231 }