1 | /* |
---|
2 | orxonox - the future of 3D-vertical-scrollers |
---|
3 | |
---|
4 | Copyright (C) 2004 orx |
---|
5 | |
---|
6 | This program is free software; you can redistribute it and/or modify |
---|
7 | it under the terms of the GNU General Public License as published by |
---|
8 | the Free Software Foundation; either version 2, or (at your option) |
---|
9 | any later version. |
---|
10 | |
---|
11 | ### File Specific: |
---|
12 | main-programmer: Benjamin Grauer |
---|
13 | co-programmer: ... |
---|
14 | */ |
---|
15 | |
---|
16 | //#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_ |
---|
17 | |
---|
18 | #include "shell.h" |
---|
19 | #include "shell_command.h" |
---|
20 | #include "shell_buffer.h" |
---|
21 | #include "shell_input.h" |
---|
22 | |
---|
23 | |
---|
24 | #include "text_engine.h" |
---|
25 | #include "list.h" |
---|
26 | #include "graphics_engine.h" |
---|
27 | #include "event_handler.h" |
---|
28 | #include "debug.h" |
---|
29 | #include "class_list.h" |
---|
30 | |
---|
31 | #include "key_names.h" |
---|
32 | #include <stdarg.h> |
---|
33 | #include <stdio.h> |
---|
34 | |
---|
35 | using namespace std; |
---|
36 | |
---|
37 | SHELL_COMMAND(clear, Shell, clear)->describe("Clears the shell from unwanted lines (empties all buffers)"); |
---|
38 | SHELL_COMMAND(deactivate, Shell, deactivate)->describe("Deactivates the Shell. (moves it into background)"); |
---|
39 | |
---|
40 | /** |
---|
41 | * standard constructor |
---|
42 | */ |
---|
43 | Shell::Shell () |
---|
44 | { |
---|
45 | this->setClassID(CL_SHELL, "Shell"); |
---|
46 | this->setName("Shell"); |
---|
47 | |
---|
48 | // Element2D and generals |
---|
49 | this->setAbsCoor2D(3, -400); |
---|
50 | this->textSize = 15; |
---|
51 | this->lineSpacing = 5; |
---|
52 | this->bActive = false; |
---|
53 | |
---|
54 | // BUFFER |
---|
55 | this->bufferText = NULL; |
---|
56 | this->setBufferDisplaySize(10); |
---|
57 | |
---|
58 | // INPUT LINE |
---|
59 | this->shellInput = new ShellInput; |
---|
60 | //this->commandList = new tList<ShellCommand>; |
---|
61 | |
---|
62 | this->rebuildText(); |
---|
63 | |
---|
64 | // EVENT-Handler subscription of '`' to all States. |
---|
65 | EventHandler::getInstance()->subscribe(this, ES_ALL, SDLK_BACKQUOTE); |
---|
66 | |
---|
67 | } |
---|
68 | |
---|
69 | Shell* Shell::singletonRef = NULL; |
---|
70 | |
---|
71 | /** |
---|
72 | * standard deconstructor |
---|
73 | */ |
---|
74 | Shell::~Shell () |
---|
75 | { |
---|
76 | // delete the displayable Buffers |
---|
77 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
78 | delete this->bufferText[i]; |
---|
79 | delete[] this->bufferText; |
---|
80 | |
---|
81 | // delete the inputLine |
---|
82 | delete this->shellInput; |
---|
83 | |
---|
84 | Shell::singletonRef = NULL; |
---|
85 | } |
---|
86 | |
---|
87 | /** |
---|
88 | * activates the shell |
---|
89 | * |
---|
90 | * This also feeds the Last few lines from the main buffers into the displayBuffer |
---|
91 | */ |
---|
92 | void Shell::activate() |
---|
93 | { |
---|
94 | if (this->bActive == true) |
---|
95 | PRINTF(3)("The shell is already active\n"); |
---|
96 | this->bActive = true; |
---|
97 | |
---|
98 | EventHandler::getInstance()->setState(ES_SHELL); |
---|
99 | this->setRelCoorSoft2D(0, 0, 1, 5); |
---|
100 | |
---|
101 | ShellBuffer::getInstance()->getBufferIterator()->lastElement(); |
---|
102 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
103 | this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), true); |
---|
104 | } |
---|
105 | |
---|
106 | /** |
---|
107 | * deactiveates the Shell. |
---|
108 | */ |
---|
109 | void Shell::deactivate() |
---|
110 | { |
---|
111 | if (this->bActive == false) |
---|
112 | PRINTF(3)("The shell is already inactive\n"); |
---|
113 | this->bActive = false; |
---|
114 | |
---|
115 | EventHandler::getInstance()->setState(ES_GAME); |
---|
116 | this->setRelCoorSoft2D(0, -400, 1, 5); |
---|
117 | |
---|
118 | ShellBuffer::getInstance()->getBufferIterator()->lastElement(); |
---|
119 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
120 | this->bufferText[i]->setText(ShellBuffer::getInstance()->getBufferIterator()->prevElement(), false); |
---|
121 | } |
---|
122 | |
---|
123 | |
---|
124 | /** |
---|
125 | * sets the size of the text and spacing |
---|
126 | * @param textSize the size of the Text in Pixels |
---|
127 | * @param lineSpacing the size of the Spacing between two lines in pixels |
---|
128 | * |
---|
129 | * this also rebuilds the entire Text, inputLine and displayBuffer, |
---|
130 | * to be accurate again. |
---|
131 | */ |
---|
132 | void Shell::setTextSize(unsigned int textSize, unsigned int lineSpacing) |
---|
133 | { |
---|
134 | this->textSize = textSize; |
---|
135 | this->lineSpacing = lineSpacing; |
---|
136 | |
---|
137 | this->rebuildText(); |
---|
138 | } |
---|
139 | |
---|
140 | /** |
---|
141 | * rebuilds the Text's |
---|
142 | * |
---|
143 | * use this function, if you changed the Font/Size or something else. |
---|
144 | */ |
---|
145 | void Shell::rebuildText() |
---|
146 | { |
---|
147 | this->shellInput->setFont("fonts/Aniron_Bold.ttf", this->textSize); |
---|
148 | this->shellInput->setColor(1, 0, 0); |
---|
149 | this->shellInput->setAlignment(TEXT_ALIGN_LEFT); |
---|
150 | this->shellInput->setText(NULL); |
---|
151 | this->shellInput->setParent2D(this); |
---|
152 | this->shellInput->setRelCoor2D(5, (this->textSize + this->lineSpacing)*this->bufferDisplaySize + this->textSize); |
---|
153 | |
---|
154 | this->setBufferDisplaySize(this->bufferDisplaySize); |
---|
155 | } |
---|
156 | |
---|
157 | /** |
---|
158 | * sets The count of Lines to display in the buffer. |
---|
159 | * @param bufferDisplaySize the count of lines to display in the Shell-Buffer. |
---|
160 | */ |
---|
161 | void Shell::setBufferDisplaySize(unsigned int bufferDisplaySize) |
---|
162 | { |
---|
163 | if (this->bufferText != NULL) |
---|
164 | { |
---|
165 | for (unsigned int i = 0; i < this->bufferDisplaySize; i++) |
---|
166 | delete this->bufferText[i]; |
---|
167 | delete[] this->bufferText; |
---|
168 | } |
---|
169 | |
---|
170 | this->bufferText = new Text*[bufferDisplaySize]; |
---|
171 | for (unsigned int i = 0; i < bufferDisplaySize; i++) |
---|
172 | { |
---|
173 | this->bufferText[i] = TextEngine::getInstance()->createText("fonts/Aniron_Bold.ttf", this->textSize, TEXT_RENDER_DYNAMIC); |
---|
174 | this->bufferText[i]->setColor(1, 0, 0); |
---|
175 | this->bufferText[i]->setAlignment(TEXT_ALIGN_LEFT); |
---|
176 | this->bufferText[i]->setRelCoor2D(calculateLinePosition(i)); |
---|
177 | this->bufferText[i]->setText(NULL); |
---|
178 | this->bufferText[i]->setParent2D(this); |
---|
179 | } |
---|
180 | this->bufferDisplaySize = bufferDisplaySize; |
---|
181 | |
---|
182 | this->shellHeight = (this->textSize + this->lineSpacing) * (bufferDisplaySize+1); |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | * deletes all the Buffers |
---|
187 | */ |
---|
188 | void Shell::flush() |
---|
189 | { |
---|
190 | // remove all chars from the BufferTexts. |
---|
191 | if (this->bufferText) |
---|
192 | for (int i = 0; i < this->bufferDisplaySize; i++) |
---|
193 | { |
---|
194 | this->bufferText[i]->setText(NULL, true); |
---|
195 | } |
---|
196 | |
---|
197 | |
---|
198 | // BUFFER FLUSHING |
---|
199 | } |
---|
200 | |
---|
201 | /** |
---|
202 | * prints out some text to the input-buffers |
---|
203 | * @param text the text to output. |
---|
204 | */ |
---|
205 | void Shell::printToDisplayBuffer(const char* text) |
---|
206 | { |
---|
207 | if(likely(bufferText != NULL)) |
---|
208 | { |
---|
209 | Text* lastText = this->bufferText[this->bufferDisplaySize-1]; |
---|
210 | |
---|
211 | Text* swapText; |
---|
212 | Text* moveText = this->bufferText[0]; |
---|
213 | this->bufferText[0]->setRelCoorSoft2D(this->calculateLinePosition(1),10); |
---|
214 | for (unsigned int i = 1; i < this->bufferDisplaySize; i++) |
---|
215 | { |
---|
216 | if ( i < this->bufferDisplaySize-1) |
---|
217 | this->bufferText[i]->setRelCoorSoft2D(this->calculateLinePosition(i+1),5); |
---|
218 | swapText = this->bufferText[i]; |
---|
219 | this ->bufferText[i] = moveText; |
---|
220 | moveText = swapText; |
---|
221 | } |
---|
222 | lastText->setRelCoor2D(this->calculateLinePosition(0)); |
---|
223 | this->bufferText[0] = lastText; |
---|
224 | |
---|
225 | this->bufferText[0]->setText(text, true); |
---|
226 | } |
---|
227 | } |
---|
228 | |
---|
229 | /** |
---|
230 | * clears the Shell (empties all buffers) |
---|
231 | */ |
---|
232 | void Shell::clear() |
---|
233 | { |
---|
234 | this->flush(); |
---|
235 | ShellBuffer::addBufferLineStatic("orxonox - shell\n ==================== \n", NULL); |
---|
236 | } |
---|
237 | |
---|
238 | |
---|
239 | |
---|
240 | /** |
---|
241 | * listens for some event |
---|
242 | * @param event the Event happened |
---|
243 | */ |
---|
244 | void Shell::process(const Event &event) |
---|
245 | { |
---|
246 | if (event.bPressed) |
---|
247 | { |
---|
248 | if (event.type == SDLK_BACKQUOTE) |
---|
249 | { |
---|
250 | if (EventHandler::getInstance()->getState() == ES_GAME) |
---|
251 | this->activate(); |
---|
252 | else |
---|
253 | this->deactivate(); |
---|
254 | } |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | /** |
---|
259 | * displays the Shell |
---|
260 | */ |
---|
261 | void Shell::draw() const |
---|
262 | { |
---|
263 | glPushMatrix(); |
---|
264 | // transform for alignment. |
---|
265 | // setting the Blending effects |
---|
266 | |
---|
267 | glColor4f(0.0f, 0.0f, 0.8f, .4); |
---|
268 | glEnable(GL_BLEND); |
---|
269 | glDisable(GL_TEXTURE_2D); |
---|
270 | glBlendFunc(GL_SRC_ALPHA, GL_ONE); |
---|
271 | |
---|
272 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
273 | glBegin(GL_TRIANGLE_STRIP); |
---|
274 | |
---|
275 | glTexCoord2f(0, 0); |
---|
276 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y ); |
---|
277 | |
---|
278 | glTexCoord2f(1, 0); |
---|
279 | glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y ); |
---|
280 | |
---|
281 | glTexCoord2f(0, 1); |
---|
282 | glVertex2f(this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight); |
---|
283 | |
---|
284 | glTexCoord2f(1, 1); |
---|
285 | glVertex2f(GraphicsEngine::getInstance()->getResolutionX() - this->getAbsCoor2D().x, this->getAbsCoor2D().y + this->shellHeight); |
---|
286 | |
---|
287 | glEnd(); |
---|
288 | } |
---|
289 | |
---|
290 | /////////////////////// |
---|
291 | // HELPER FUNCTIONS // |
---|
292 | /////////////////////// |
---|
293 | |
---|
294 | /** |
---|
295 | * calculates the position of a Buffer-Display Line |
---|
296 | * @param lineNumber the lineNumber from the bottom to calculate the position from |
---|
297 | * @returns the Position of the Line. |
---|
298 | */ |
---|
299 | Vector Shell::calculateLinePosition(unsigned int lineNumber) |
---|
300 | { |
---|
301 | return Vector(5, (this->textSize + this->lineSpacing)*(this->bufferDisplaySize - lineNumber -1) + this->textSize, 0); |
---|
302 | } |
---|
303 | |
---|
304 | |
---|
305 | |
---|
306 | /** |
---|
307 | * displays some nice output from the Shell |
---|
308 | */ |
---|
309 | void Shell::debug() const |
---|
310 | { |
---|
311 | PRINT(3)("Debugging output to console (not this shell)\n"); |
---|
312 | |
---|
313 | // if (this->pressedKey != SDLK_FIRST) |
---|
314 | // printf("%s::%f %f\n", SDLKToKeyname(this->pressedKey), this->delayed, this->repeatDelay); |
---|
315 | |
---|
316 | |
---|
317 | ShellBuffer::getInstance()->debug(); |
---|
318 | } |
---|
319 | |
---|
320 | |
---|
321 | |
---|
322 | // void Shell::testI (int i) |
---|
323 | // { |
---|
324 | // PRINTF(3)("This is the Test for one Int '%d'\n", i); |
---|
325 | // } |
---|
326 | // |
---|
327 | // void Shell::testS (const char* s) |
---|
328 | // { |
---|
329 | // PRINTF(3)("This is the Test for one String '%s'\n", s); |
---|
330 | // } |
---|
331 | // |
---|
332 | // void Shell::testB (bool b) |
---|
333 | // { |
---|
334 | // PRINTF(3)("This is the Test for one Bool: "); |
---|
335 | // if (b) |
---|
336 | // PRINTF(3)("true\n"); |
---|
337 | // else |
---|
338 | // PRINTF(3)("false\n"); |
---|
339 | // } |
---|
340 | // |
---|
341 | // void Shell::testF (float f) |
---|
342 | // { |
---|
343 | // PRINTF(3)("This is the Test for one Float '%f'\n", f); |
---|
344 | // } |
---|
345 | // |
---|
346 | // void Shell::testSF (const char* s, float f) |
---|
347 | // { |
---|
348 | // PRINTF(3)("This is the Test for one String '%s' and one Float '%f'\n",s , f); |
---|
349 | // } |
---|