Debian Codegraph @

src/include/codriver.h

raw
1 //
2 // Copyright (C) 2015-2016 Andrei Bondor, [email protected]
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (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, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 //
18
19 #pragma once
20
21 #include <cstddef>
22 #include <forward_list>
23 #include <iterator>
24 #include <sstream>
25 #include <string>
26 #include <thread>
27 #include <unordered_map>
28 #include <vector>
29
30 // maximum number of characters for a note
31 // e.g.: "hard left over jump" has 19 characters
32 //
33 // NOTE: this isn't a hard limit and it can be exceeded by the game safely
34 // but at the cost of one or more memory reallocations
35 const std::size_t note_maxlength = 128;
36
37 struct PCodriverUserConfig
38 {
39 float life = 3.00f; ///< How many seconds until the codriver signs start fading.
40 float scale = 0.20f; ///< Scale of the codriver signs.
41 float posx = 0.00f; ///< X scale centering.
42 float posy = 0.45f; ///< Y scale centering.
43 };
44
45 ///
46 /// @brief Draws the codriver signs for the driver.
47 ///
48 class PCodriverSigns
49 {
50 public:
51
52 PCodriverSigns() = delete;
53
54 PCodriverSigns(
55 const std::unordered_map<std::string, PTexture *> &signs,
56 const PCodriverUserConfig &uc
57 ):
58 signs(signs),
59 uc(uc)
60 {
61 cpsigns.reserve(8); // FIXME: magic number
62 tempnote.reserve(note_maxlength);
63 }
64
65 ///
66 /// @brief Sets the current codriver signs.
67 /// @param [in] notes Original notes.
68 /// @param time Codriver checkpoint time.
69 ///
70 void set(const std::string &notes, float time)
71 {
72 std::istringstream ssnotes(notes);
73 std::istream_iterator<std::string> itnotes_begin(ssnotes);
74 std::istream_iterator<std::string> itnotes_end;
75 std::forward_list<std::string> flnotes(itnotes_begin, itnotes_end);
76
77 cpsigns.clear();
78 cptime = time;
79
80 while (!flnotes.empty())
81 {
82 PTexture *cptex = nullptr;
83 auto cut_end = flnotes.cbegin();
84
85 tempnote.clear();
86
87 for (auto ci = flnotes.cbegin(); ci != flnotes.cend(); ++ci)
88 {
89 tempnote += *ci;
90
91 if (signs.count(tempnote) != 0)
92 {
93 cptex = signs.at(tempnote);
94 cut_end = ci;
95 }
96 }
97
98 if (cptex != nullptr)
99 cpsigns.push_back(cptex);
100
101 flnotes.erase_after(flnotes.cbefore_begin(), std::next(cut_end));
102 }
103 }
104
105 ///
106 /// @brief Draws the current codriver signs.
107 /// @param coursetime The current time of the course.
108 ///
109 void render(float coursetime)
110 {
111 if (cpsigns.empty())
112 return;
113
114 float alpha;
115
116 if (coursetime - cptime < uc.life)
117 {
118 alpha = 1.0f;
119 }
120 else
121 if (coursetime - cptime < uc.life + 1.0f)
122 {
123 alpha = (uc.life + 1.0f) - (coursetime - cptime);
124 }
125 else
126 {
127 cpsigns.clear();
128 return;
129 }
130
131 glPushMatrix();
132 glLoadIdentity();
133 glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
134 glTranslatef(uc.posx, uc.posy, 0.0f);
135 glScalef(uc.scale, uc.scale, 1.0f);
136 glTranslatef(-0.5f * cpsigns.size() * 2 + 1.0f, 0.0f, 0.0f);
137 glColor4f(1.0f, 1.0f, 1.0f, alpha);
138
139 for (PTexture *cptex: cpsigns)
140 {
141 cptex->bind();
142 glBegin(GL_QUADS);
143 glTexCoord2f( 1.0f, 1.0f);
144 glVertex2f( 1.0f, 1.0f);
145 glTexCoord2f( 0.0f, 1.0f);
146 glVertex2f( -1.0f, 1.0f);
147 glTexCoord2f( 0.0f, 0.0f);
148 glVertex2f( -1.0f, -1.0f);
149 glTexCoord2f( 1.0f, 0.0f);
150 glVertex2f( 1.0f, -1.0f);
151 glEnd();
152 glTranslatef(2.0f, 0.0f, 0.0f);
153 }
154
155 glPopMatrix();
156 }
157
158 private:
159
160 std::unordered_map<std::string, PTexture *> signs; ///< MainApp::tex_codriversigns
161 PCodriverUserConfig uc; ///< User configuration.
162 std::vector<PTexture *> cpsigns;
163 std::string tempnote; ///< Temporary string for performance.
164 float cptime;
165 };
166
167 ///
168 /// @brief Gives a voice to the codriver.
169 /// @todo Re-code without making use of C++11 threads to be in line with
170 /// the rest of the game logic, which is implemented with `tick()` functions.
171 ///
172 class PCodriverVoice
173 {
174 public:
175
176 PCodriverVoice() = delete;
177
178 PCodriverVoice(const std::unordered_map<std::string, PAudioSample *> &words, float volume):
179 words(words),
180 volume(volume)
181 {
182 tempnote.reserve(note_maxlength);
183 }
184
185 void say(const std::string &notes)
186 {
187 if (words.empty())
188 return;
189
190 std::vector<PAudioSample *> cpwords;
191 std::istringstream ssnotes(notes);
192 std::istream_iterator<std::string> itnotes_begin(ssnotes);
193 std::istream_iterator<std::string> itnotes_end;
194 std::forward_list<std::string> flnotes(itnotes_begin, itnotes_end);
195
196 cpwords.reserve(note_maxlength);
197
198 while (!flnotes.empty())
199 {
200 PAudioSample *cpaud = nullptr;
201 auto cut_end = flnotes.cbegin();
202
203 tempnote.clear();
204
205 for (auto ci = flnotes.cbegin(); ci != flnotes.cend(); ++ci)
206 {
207 tempnote += *ci;
208
209 if (words.count(tempnote) != 0)
210 {
211 cpaud = words.at(tempnote);
212 cut_end = ci;
213 }
214 }
215
216 if (cpaud != nullptr)
217 cpwords.push_back(cpaud);
218
219 flnotes.erase_after(flnotes.cbefore_begin(), std::next(cut_end));
220 }
221
222 std::thread(&PCodriverVoice::asyncSay, this, cpwords).detach();
223 }
224
225 private:
226
227 void asyncSay(std::vector<PAudioSample *> cpwords) const
228 {
229 for (auto w: cpwords)
230 {
231 PAudioInstance voice(w);
232
233 voice.setGain(volume);
234 voice.play();
235
236 while (voice.isPlaying())
237 std::this_thread::sleep_for(std::chrono::milliseconds(50));
238 }
239 }
240
241 std::unordered_map<std::string, PAudioSample *> words; ///< MainApp::aud_codriverwords
242 std::string tempnote; ///< Temporary string for performance.
243 float volume;
244 };
245