1 /* oglconsole -- gpl license here */
3 /* This strategy seems to offer the convenience of zero-configuration, but
4 * obviously it also offers defining GLHEADERINCLUDE */
6 # include GLHEADERINCLUDE
9 # include <OpenGL/gl.h>
15 #include "oglconsole.h"
23 #define C ((_OGLCONSOLE_Console*)console)
26 #include "ConsoleFont.c"
27 #define FIRST_CHARACTER ' '
28 #define LAST_CHARACTER '~'
30 #define CHAR_PIXEL_W 6
31 #define CHAR_PIXEL_H 13
32 #define CHAR_WIDTH 0.0234375 /* ogl tex coords */
33 #define CHAR_HEIGHT 0.203125 /* ogl tex coords */
35 /* This is how long the animation should take to make the transition between
36 * "hidden" and "visible" console visibility modes (expressed in milliseconds) */
39 /* If we don't know how to retrieve the time then we can just use a number of
40 * frames to divide up the time it takes to transition between "hidden" and
41 * "visible" console visibility modes */
44 GLuint OGLCONSOLE_glFontHandle
= 0;
46 OGLCONSOLE_CreateFont(void)
49 int err
= glGetError();
51 printf("GL ERROR: %i\n", err
);
54 puts("Creating OGLCONSOLE font");
57 /* Get a font index from OpenGL */
58 glGenTextures(1, &OGLCONSOLE_glFontHandle
);
60 int err
= glGetError();
62 printf("glGenTextures() error: %i\n", err
);
66 glBindTexture(GL_TEXTURE_2D
, OGLCONSOLE_glFontHandle
);
68 int err
= glGetError();
70 printf("glBindTexture() error: %i\n", err
);
73 /* Set some parameters i guess */
74 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MIN_FILTER
, GL_LINEAR
);
75 glTexParameteri(GL_TEXTURE_2D
, GL_TEXTURE_MAG_FILTER
, GL_LINEAR
);
78 glTexImage2D(GL_TEXTURE_2D
, 0, GL_RGB
,
79 OGLCONSOLE_FontData
.width
, OGLCONSOLE_FontData
.height
, 0,
80 GL_RGB
, GL_UNSIGNED_BYTE
, OGLCONSOLE_FontData
.pixel_data
);
83 int err
= glGetError();
85 printf("glTexImage2D() error: %i\n", err
);
89 puts("Created OGLCONSOLE font");
94 /* TODO: Expose these macros to the user? */
96 /* This is the longest command line that the user can enter TODO: Make dynamic
97 * so that the user can enter any length line */
98 #define MAX_INPUT_LENGTH 256
100 /* This is the number of command line entries that the console will remember (so
101 * that the user can use the up/down keys to see and easily re-execute his past
103 #define MAX_HISTORY_COUNT 25
105 #define MAX_COMMAND_ARGS 20
107 /* This is the default number of lines for the console to remember (that is to
108 * say, the user can scroll up and down to see what has been printed to the
109 * console in the past, and this is the number of those lines, plus the number
110 * of lines shown on the screen at all times) */
111 #define DEFAULT_MAX_LINES 100
113 /* OGLCONSOLE console structure */
115 GLdouble mvMatrix
[16];
118 GLdouble pMatrix
[16];
121 /* Screen+scrollback lines (console output) */
124 int maxLines
, lineQueueIndex
, lineScrollIndex
;
126 /* History scrollback (command input) */
127 char history
[MAX_HISTORY_COUNT
][MAX_INPUT_LENGTH
];
128 int historyQueueIndex
, historyScrollIndex
;
130 /* Current input line */
131 char inputLine
[MAX_INPUT_LENGTH
];
132 int inputCursorPos
, inputLineLength
;
134 /* Rows and columns of text to display */
135 int textWidth
, textHeight
;
137 /* Current column of text where new text will be inserted */
141 /* Width and height of a single character for the GL */
142 GLdouble characterWidth
, characterHeight
;
147 /* Various callback functions defined by the user */
148 void (*enterKeyCallback
) (OGLCONSOLE_Console console
, int argc
, char **argv
);
150 /* When not visible, render the last line at the top of the screen
152 struct timeval expire
;
155 } _OGLCONSOLE_Console
;
157 /* To save code, I've gone with an imperative "modal" kind of interface */
158 _OGLCONSOLE_Console
*programConsole
= NULL
;
160 /* This console is the console currently receiving user input */
161 _OGLCONSOLE_Console
*userConsole
= NULL
;
163 /* Set the callback for a console */
165 OGLCONSOLE_EnterKey(void (*cbfun
) (OGLCONSOLE_Console console
, int argc
, char **argv
))
167 programConsole
->enterKeyCallback
= cbfun
;
171 OGLCONSOLE_DefaultEnterKeyCallback(OGLCONSOLE_Console console
, int __attribute__((unused
)) argc
, char __attribute__((unused
)) **argv
)
173 OGLCONSOLE_Output(console
,
174 "No enter key callback is registered for this console!\n");
179 OGLCONSOLE_InitText(void *c
, int res_x
, int res_y
)
181 _OGLCONSOLE_Console
*console
= c
;
186 console
= userConsole
;
191 /* Textual dimensions */
193 console
->textWidth
= res_x
/ CHAR_PIXEL_W
;
194 console
->textHeight
= res_y
/ CHAR_PIXEL_H
;
195 console
->characterWidth
= 1.0 / console
->textWidth
;
196 console
->characterHeight
= 1.0 / console
->textHeight
;
198 /* Different values have different meanings for xMatrixUse:
199 0) Do not change the matrix before rendering
200 1) Upload the console's matrix before rendering
201 2) Multiply the console's matrix before rendering */
203 /* Initialize its projection matrix */
204 console
->pMatrixUse
= 1;
205 glMatrixMode(GL_PROJECTION
);
207 glOrtho(0, console
->textWidth
, 0, console
->textHeight
, -1, 1);
208 glGetDoublev(GL_MODELVIEW_MATRIX
, console
->pMatrix
);
211 /* Initialize its modelview matrix */
212 console
->mvMatrixUse
= 1;
213 glMatrixMode(GL_MODELVIEW
);
216 glTranslated(-1, -1, 0);
218 glGetDoublev(GL_MODELVIEW_MATRIX
, console
->mvMatrix
);
221 /* Screen and scrollback lines */
222 /* This is the total number of screen lines in memory (start blank) */
223 console
->maxLines
= DEFAULT_MAX_LINES
;
225 /* (re)allocate space for text */
226 new_lines_size
= console
->maxLines
* console
->textWidth
+ 1;
227 new_lines
= malloc(new_lines_size
);
228 /* XXX return value */
229 memset(new_lines
, 0, new_lines_size
);
231 if (console
->lines
) {
233 if (new_lines_size
>= console
->lines_size
) {
234 memcpy(new_lines
, console
->lines
, console
->lines_size
);
237 console
->lines
+ (console
->lines_size
-new_lines_size
),
242 free(console
->lines
);
245 console
->lines_size
= new_lines_size
;
246 console
->lines
= new_lines
;
248 /* This cursor points to the X pos where console output is next destined */
249 console
->outputCursor
= console
->lines
;
251 /* This variable represents whether or not a newline has been left */
252 console
->outputNewline
= 0;
253 /* This cursor points to the X pos where console output is next destined */
254 console
->outputCursor
= console
->lines
;
255 /* This cursor points to what line console output is next destined for */
256 console
->lineQueueIndex
= 0;
257 /* This cursor points to what line the console view is scrolled to */
258 console
->lineScrollIndex
= console
->maxLines
- console
->textHeight
+ 1;
260 /* Initialize the user's input (command line) */
261 console
->inputLineLength
= 0;
262 console
->inputCursorPos
= 0;
263 console
->inputLine
[0] = '\0';
265 OGLCONSOLE_Output((void *) console
,
266 "Console Resolution: %i x %i\n"
267 "Text Size: %i x %i\n",
269 console
->textWidth
, console
->textHeight
);
275 _OGLCONSOLE_Console
*console
;
278 /* If font hasn't been created, we create it */
279 if (!glIsTexture(OGLCONSOLE_glFontHandle
))
280 OGLCONSOLE_CreateFont();
282 /* Allocate memory for our console */
283 console
= (void *) malloc(sizeof (_OGLCONSOLE_Console
));
284 memset(console
, 0, sizeof(*console
));
286 console
->maxLines
= DEFAULT_MAX_LINES
;
288 glGetIntegerv(GL_VIEWPORT
, viewport
);
289 OGLCONSOLE_InitText(console
, viewport
[2], viewport
[3]);
292 console
->enterKeyCallback
= OGLCONSOLE_DefaultEnterKeyCallback
;
294 /* The console starts life invisible */
295 console
->visibility
= 0;
297 /* If no consoles existed before, we select this one for convenience */
299 programConsole
= console
;
301 userConsole
= console
;
304 OGLCONSOLE_Output((void *) console
,
305 "Console input length: %i\n", MAX_INPUT_LENGTH
);
306 OGLCONSOLE_Output((void *) console
, "Console initialized\n");
308 /* Return the opaque pointer to the programmer */
309 return (OGLCONSOLE_Console
) console
;
312 /* This functoin is only used internally; the user ultimately invokes this
313 * function through either a call to Destroy() or Quit(); the purpose of this
314 * mechanism is to warn the user if he has explicitly destroyed a console that
315 * was engaged in operation at the time they destroyed it (the only two
316 * operations a console can be engaged in are receiving programmer interaction,
317 * or receiving end-user interaction. fyi, "user" always refers to the
318 * programmer, end-user refers to the real end-user) */
320 OGLCONSOLE_DestroyReal(OGLCONSOLE_Console console
, int warn
)
325 if (programConsole
== C
) {
327 "Warning: OGLCONSOLE you just destroyed the programConsole!\n");
328 programConsole
= NULL
;
331 if (userConsole
== C
) {
333 "Warning: OGLCONSOLE you just destroyed the userConsole!\n");
339 /* The user can call this function to free a console. A warning is printed to
340 * stderr if the user has destroyed a console destroyed while it was receiving
341 * input, see DestroyReal() for details TODO: there are currently no semantics
342 * under consideration for explicitly destroying an 'engaged' console WITHOUT
343 * having a warning issued, nor is there a way to deselect a console without
344 * also selecting a new one as of yet */
346 OGLCONSOLE_Destroy(OGLCONSOLE_Console console
)
348 OGLCONSOLE_DestroyReal(console
, 1);
351 /* This function frees all of the consoles that the library is actively aware
352 * of, and is intended to be used by programs that have only a single console;
353 * technically this particular function will free up to two consoles, but don't
354 * count on it because that may change; no warnings are issued by this function */
358 OGLCONSOLE_DestroyReal((void *) programConsole
, 0);
359 if (programConsole
!= userConsole
)
360 OGLCONSOLE_DestroyReal((void *) userConsole
, 0);
361 programConsole
= NULL
;
365 /* Select a console to receive input from the user (read: programmer); this
366 * function is intended for use by applications which create MORE THAN ONE
367 * console, since every newly created console is automatically selected for
368 * programmer input ('programmer input' refers to calling any function which
369 * does not specify a console explicitly in its parameter list. the console
370 * which is 'engaged' by 'programmer input' at the time of calling one of these
371 * functions is the console that function will operate on */
373 OGLCONSOLE_EditConsole(OGLCONSOLE_Console console
)
378 /* Show or hide a console */
380 OGLCONSOLE_SetVisibility(int __attribute__ ((unused
)) visible
)
384 /* Get current configuration information about a console */
386 OGLCONSOLE_Info(void)
388 puts("TODO: print some console info here");
391 /* This routine is meant for applications with a single console, if you use
392 * multiple consoles in your program, use Render() instead */
396 OGLCONSOLE_Render((void *) userConsole
);
399 /* Internal functions for drawing text. You don't want these, do you? */
400 void OGLCONSOLE_DrawString(char *s
, double x
, double y
, double w
, double h
,
402 void OGLCONSOLE_DrawWrapString(char *s
, double x
, double y
, double w
, double h
,
404 void OGLCONSOLE_DrawCharacter(int c
, double x
, double y
, double w
, double h
,
408 OGLCONSOLE_RenderText(OGLCONSOLE_Console console
)
410 /* Graphical line, and line in lines[] */
411 int gLine
, tLine
= C
->lineScrollIndex
;
413 /* Iterate through each line being displayed */
414 for (gLine
= 0; gLine
< C
->textHeight
; gLine
++) {
415 /* Draw this line of text adjusting for user scrolling up/down */
416 OGLCONSOLE_DrawString(C
->lines
+ (tLine
* C
->textWidth
),
419 gLine
) * C
->characterHeight
,
421 C
->characterHeight
, 0);
423 /* Grab next line of text using wheel-queue wrapping */
424 if (++tLine
>= C
->maxLines
)
428 /* Here we draw the current commandline, it will either be a line from
429 * the command history or the line being edited atm */
430 if (C
->historyScrollIndex
>= 0) {
432 OGLCONSOLE_DrawString(C
->history
[C
->historyScrollIndex
],
435 C
->characterHeight
, 0);
437 /* Draw input line cyan */
439 OGLCONSOLE_DrawString(C
->inputLine
,
442 C
->characterHeight
, 0);
444 /* Draw cursor beige */
446 OGLCONSOLE_DrawCharacter('_' - ' ',
448 C
->characterWidth
, 0,
450 C
->characterHeight
, 0);
455 OGLCONSOLE_RenderLast(OGLCONSOLE_Console console
)
459 if (!C
->expire
.tv_sec
)
462 /* don't do this time of day thing too often */
463 if (++C
->frame_skip
% 8) {
464 gettimeofday(&tv
, NULL
);
465 if (tv
.tv_sec
> C
->expire
.tv_sec
||
466 (tv
.tv_sec
== C
->expire
.tv_sec
&&
467 tv
.tv_usec
>= C
->expire
.tv_usec
)) {
468 C
->expire
.tv_sec
= 0;
474 /* Graphical line, and line in lines[] */
475 OGLCONSOLE_DrawString(C
->lines
+ (C
->textWidth
* C
->lineQueueIndex
),
476 0, 0, C
->characterWidth
,
477 C
->characterHeight
, 0);
480 /* This function draws a single specific console; if you only use one console in
481 * your program, use Draw() instead */
483 OGLCONSOLE_Render(OGLCONSOLE_Console console
)
487 /* Don't render hidden console */
488 if (C
->visibility
== 0 && !C
->expire
.tv_sec
)
491 glMatrixMode(GL_PROJECTION
);
493 glLoadMatrixd(C
->pMatrix
);
495 glMatrixMode(GL_MODELVIEW
);
497 glLoadMatrixd(C
->mvMatrix
);
499 glPushAttrib(GL_ALL_ATTRIB_BITS
);
501 /* glEnable(GL_BLEND);
502 glBlendFunc(GL_ONE, GL_ONE);*/
504 /* TODO: This SHOULD become an option at some point because the
505 * infrastructure for "real" consoles in the game (like you could walk up to
506 * a computer terminal and manipulate a console on a computer using
507 * oglconsole) already exists; you'd want depth testing in that case */
508 glDisable(GL_DEPTH_TEST
);
510 /* Render hiding / showing console in a special manner. Zero means hidden. 1
511 * means visible. All other values are traveling toward zero or one. TODO:
512 * Make this time dependent */
513 if (C
->visibility
!= 1 && C
->visibility
!= 0) {
514 int v
= C
->visibility
;
516 /* Count down in both directions */
525 d
= (1.0 / (double)SLIDE_STEPS
) * v
;
526 glTranslated(0, 1.0 - d
, 0);
527 } else if (C
->visibility
== 0) {
528 /* Draw the last line */
529 glTranslated(0, 1 - (1 / (double)C
->textHeight
), 0);
533 /* First we draw our console's background TODO: Add something fancy? */
534 glDisable(GL_TEXTURE_2D
);
536 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
537 // glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_COLOR);
538 glColor4d(.1, 0, 0, 0.5);
547 // Change blend mode for drawing text
548 glBlendFunc(GL_ONE
, GL_ONE
);
550 /* Select the console font */
551 glEnable(GL_TEXTURE_2D
);
552 glBindTexture(GL_TEXTURE_2D
, OGLCONSOLE_glFontHandle
);
557 /* Render console contents */
560 OGLCONSOLE_RenderText(console
);
562 OGLCONSOLE_RenderLast(console
);
566 /* Relinquish our rendering settings */
567 glMatrixMode(GL_PROJECTION
);
570 glMatrixMode(GL_MODELVIEW
);
576 /* Issue rendering commands for a single a string */
578 OGLCONSOLE_DrawString(char *s
, double x
, double y
, double w
, double h
, double z
)
581 OGLCONSOLE_DrawCharacter(*s
- ' ', x
, y
, w
, h
, z
);
587 /* Issue rendering commands for a single a string */
589 OGLCONSOLE_DrawWrapString(char *s
, double x
, double y
, double w
, double h
,
596 OGLCONSOLE_DrawCharacter(*s
- ' ', X
, y
, w
, h
, z
);
608 /* Issue rendering commands for a single character */
610 OGLCONSOLE_DrawCharacter(int c
, double x
, double y
, double w
, double h
,
613 // static int message = 0;
614 double cx
, cy
, cX
, cY
;
616 // if (c < FIRST_CHARACTER || c > LAST_CHARACTER)
617 // c = (c - FIRST_CHARACTER) % (LAST_CHARACTER - FIRST_CHARACTER);
618 // else c -= FIRST_CHARACTER;
620 cx
= (c
% 42) * CHAR_WIDTH
;
621 cy
= 1.0 - (c
/ 42) * CHAR_HEIGHT
;
622 cX
= cx
+ CHAR_WIDTH
;
623 cY
= 1.0 - (c
/ 42 + 1) * CHAR_HEIGHT
;
627 printf("For %i we got %f, %f\n", c, x, y);
631 /* This should occur outside of this function for optimiation TODO: MOVE IT */
632 glTexCoord2d(cx
, cy
);
634 glTexCoord2d(cX
, cy
);
635 glVertex3d(x
+ w
, y
, z
);
636 glTexCoord2d(cX
, cY
);
637 glVertex3d(x
+ w
, y
+ h
, z
);
638 glTexCoord2d(cx
, cY
);
639 glVertex3d(x
, y
+ h
, z
);
642 /* This is the final, internal function for printing text to a console */
644 OGLCONSOLE_Output(OGLCONSOLE_Console console
, const char *s
, ...)
648 /* cache some console properties */
649 int lineQueueIndex
= C
->lineQueueIndex
;
650 int lineScrollIndex
= C
->lineScrollIndex
;
651 int textWidth
= C
->textWidth
;
652 int maxLines
= C
->maxLines
;
657 /* string copy cursors */
658 char *consoleCursor
, *outputCursor
= output
;
660 /* Acrue arguments in argument list */
661 va_start(argument
, s
);
662 vsnprintf(output
, 4096, s
, argument
);
665 /* This cursor tells us where in the console display we are currently
666 * copying text into from the "output" string */
667 consoleCursor
= C
->outputCursor
;
669 while (*outputCursor
) {
670 /* Here we check to see if any conditions require console line
671 * advancement. These two conditions are:
672 1) Hitting the end of the screen
673 2) Getting a newlien character */
674 if ((C
->outputNewline
) ||
675 (consoleCursor
- (C
->lines
+ lineQueueIndex
* textWidth
))
676 >= (textWidth
- 1)) {
677 C
->outputNewline
= 0;
679 //puts("incrementing to the next line");
681 /* Inrement text-line index, with wrapping */
682 if (++lineQueueIndex
>= maxLines
)
685 /* Scroll the console display one line TODO: Don't scroll if the console is
686 * currently scrolled away from the end of output? */
687 if (++lineScrollIndex
>= maxLines
)
690 /* Reposition the cursor at the beginning of the new line */
692 C
->lines
+ lineQueueIndex
* C
->textWidth
;
695 /* If we encounter a newline character, we set the newline flag, which
696 * tells the console to advance one line before it prints the next
697 * character. The reason we do it this way is to defer line-advancement,
698 * and thus we needn't suffer through a needless blank line between
699 * console output and the command line, wasting precious screen
701 if (*outputCursor
== '\n') {
702 C
->outputNewline
= 1;
707 /* copy a single character */
708 *(consoleCursor
++) = *(outputCursor
++);
711 /* Unless we're at the very end of our current line, we finish up by capping
712 * a NULL terminator on the current line */
713 if (consoleCursor
!= C
->lines
+ (lineQueueIndex
+ 1) * C
->textWidth
- 1)
714 *consoleCursor
= '\0';
716 /* Restore cached values */
717 C
->lineQueueIndex
= lineQueueIndex
;
718 C
->lineScrollIndex
= lineScrollIndex
;
719 C
->outputCursor
= consoleCursor
; // TODO: confusing variable names
721 /* old way of copying the text into the console */
722 //strcpy(C->lines[C->lineQueueIndex], output);
724 printf("Copied \"%s\" into line %i\n", output
, C
->lineQueueIndex
);
726 gettimeofday(&C
->expire
, NULL
);
727 C
->expire
.tv_sec
+= 3;
731 /* Mono-Console Users: print text to the console; multi-console users use
734 OGLCONSOLE_Print(char *s
, ...)
739 /* Acrue arguments in argument list */
740 va_start(argument
, s
);
741 vsnprintf(output
, 4096, s
, argument
);
744 /* TODO: Find some way to pass the va_list arguments to OGLCONSOLE_Output
745 * so that we don't waste extra time with the "%s" bullshit */
746 OGLCONSOLE_Output((OGLCONSOLE_Console
) userConsole
, "%s", output
);
750 /* Multi-Console Users: print text to a specific console; mono-console users use
753 OGLCONSOLE_Output(OGLCONSOLE_Console console
, char *s
)
755 /* TODO: Chop up output to wrap text */
757 /* TODO: Add auto-scroll (the commented out code here is a failed attempt,
758 * my brain is too scattered to do it) */
759 /* If the console isn't scrolled up, then we move the scroll point */
760 /* if (C->lineQueueIndex - C->lineScrollIndex == C->textHeight)
761 if (++C->lineScrollIndex - C->lineScrollIndex >= MAX_LINE_COUNT)
762 C->lineScrollIndex = 0;*/
767 /* Internal encapsulation of the act for adding a command the user executed to
768 * their command history for that particular console */
770 OGLCONSOLE_AddHistory(_OGLCONSOLE_Console
* console
, char *s
)
772 if (++C
->historyQueueIndex
> MAX_HISTORY_COUNT
)
773 C
->historyQueueIndex
= 0;
775 strcpy(C
->history
[C
->historyQueueIndex
], s
);
779 OGLCONSOLE_YankHistory(_OGLCONSOLE_Console
* console
)
781 /* First we have to see if we are browsing our command history */
782 if (console
->historyScrollIndex
!= -1) {
783 /* Copy the selected command into the current input line */
784 strcpy(console
->inputLine
,
785 console
->history
[console
->historyScrollIndex
]);
787 /* Set up this shite */
788 console
->inputCursorPos
=
789 console
->inputLineLength
= strlen(console
->inputLine
);
791 /* Drop out of history browsing mode */
792 console
->historyScrollIndex
= -1;
796 #ifndef OGLCONSOLE_USE_SDL
797 #error **************************************************************************
801 #error Only SDL is supported so far: you must define the OGLCONSOLE_USE_SDL macro
805 #error **************************************************************************
808 /* This function tries to handle the incoming SDL event. In the future there may
809 * be non-SDL analogs for input systems such as GLUT. Returns true if the event
810 * was handled by the console. If console is hidden, no events are handled. */
811 #ifdef OGLCONSOLE_USE_SDL
813 #define KEY_BACKSPACE SDLK_BACKSPACE
814 #define KEY_DELETE SDLK_DELETE
815 #define KEY_RETURN SDLK_RETURN
816 #define KEY_UP SDLK_UP
817 #define KEY_DOWN SDLK_DOWN
818 #define KEY_LEFT SDLK_LEFT
819 #define KEY_RIGHT SDLK_RIGHT
820 #define KEY_PAGEUP SDLK_PAGEUP
821 #define KEY_PAGEDOWN SDLK_PAGEDOWN
822 #define KMOD_CAPITALIZE (KMOD_LSHIFT|KMOD_RSHIFT|KMOD_CAPS)
824 OGLCONSOLE_SDLEvent(SDL_Event
* e
)
826 /* If the terminal is hidden we only check for show/hide key */
827 if (userConsole
->visibility
< 1) {
828 if (e
->type
== SDL_KEYDOWN
&& e
->key
.keysym
.sym
== '`') {
829 // TODO: Fetch values from OS?
830 // TODO: Expose them to the program
831 SDL_EnableKeyRepeat(250, 30);
832 userConsole
->visibility
+= SLIDE_STEPS
;
839 /* TODO: SDL_KEYPRESS? ENABLE KEY REPEAT USING THE HIDE/SHOW FUNCTION? */
840 if (e
->type
== SDL_KEYDOWN
) {
841 /* Reject most modifier keys TODO: Add some accelerator keys? */
842 if (e
->key
.keysym
.mod
& ~(KMOD_CAPITALIZE
| KMOD_NUM
))
845 /* Check for hide key */
846 if (e
->key
.keysym
.sym
== '`' || e
->key
.keysym
.sym
== SDLK_ESCAPE
) {
847 /* Tell console to slide into closing */
848 userConsole
->visibility
-= SLIDE_STEPS
;
850 /* Disable key repeat */
851 SDL_EnableKeyRepeat(0, 0);
855 /* TODO: Find out how to handle CAPSLOCK */
856 if (e
->key
.keysym
.sym
>= ' ' && e
->key
.keysym
.sym
<= '~') {
857 int k
= e
->key
.keysym
.sym
;
860 /* Yank the command history if necessary */
861 OGLCONSOLE_YankHistory(userConsole
);
863 /* Capitalize if necessary */
864 if (e
->key
.keysym
.mod
& KMOD_CAPITALIZE
) {
866 const int capital
[] =
867 { (int) ' ', (int) '!', (int) '"',
869 (int) '$', (int) '%', (int) '&',
870 (int) '"', (int) '(', (int) ')',
871 (int) '*', (int) '+', (int) '<',
872 (int) '_', (int) '>', (int) '?',
873 (int) ')', (int) '!', (int) '@',
874 (int) '#', (int) '$', (int) '%',
875 (int) '^', (int) '&', (int) '*',
876 (int) '(', (int) ':', (int) ':',
877 (int) '<', (int) '+', (int) '>',
878 (int) '?', (int) '@', (int) 'A',
879 (int) 'B', (int) 'C', (int) 'D',
880 (int) 'E', (int) 'F', (int) 'G',
881 (int) 'H', (int) 'I', (int) 'J',
882 (int) 'K', (int) 'L', (int) 'M',
883 (int) 'N', (int) 'O', (int) 'P',
884 (int) 'Q', (int) 'R', (int) 'S',
885 (int) 'T', (int) 'U', (int) 'V',
886 (int) 'W', (int) 'X', (int) 'Y',
887 (int) 'Z', (int) '{', (int) '|',
888 (int) '}', (int) '^', (int) '_',
889 (int) '~', (int) 'A', (int) 'B',
890 (int) 'C', (int) 'D', (int) 'E',
891 (int) 'F', (int) 'G', (int) 'H',
892 (int) 'I', (int) 'J', (int) 'K',
893 (int) 'L', (int) 'M', (int) 'N',
894 (int) 'O', (int) 'P', (int) 'Q',
895 (int) 'R', (int) 'S', (int) 'T',
896 (int) 'U', (int) 'V', (int) 'W',
897 (int) 'X', (int) 'Y', (int) 'Z',
898 (int) '{', (int) '|', (int) '}',
902 /* If we're not explicitly holding a shift key, that means just
903 * capslock, which means we only capitalize letters */
904 if ((k
>= 'a' && k
<= 'z')
905 || (e
->key
.keysym
.mod
& KMOD_SHIFT
))
906 k
= capital
[k
- ' '];
909 /* Point to the cursor position and the end of the string */
910 c
= userConsole
->inputLine
+
911 userConsole
->inputCursorPos
;
912 d
= userConsole
->inputLine
+
913 userConsole
->inputLineLength
+ 1;
915 /* Slide some of the string to the right */
919 /* Insert new character */
922 /* Increment input line length counter */
923 userConsole
->inputLineLength
++;
925 /* Advance input cursor position */
926 userConsole
->inputCursorPos
++;
931 else if (e
->key
.keysym
.sym
== KEY_DELETE
932 || e
->key
.keysym
.sym
== KEY_BACKSPACE
) {
933 /* Yank the command history if necessary */
934 OGLCONSOLE_YankHistory(userConsole
);
936 /* If string is not empty */
937 if (userConsole
->inputLineLength
) {
940 /* Backspace is like pressing LEFT and then DELETE */
941 if (e
->key
.keysym
.sym
== KEY_BACKSPACE
)
942 userConsole
->inputCursorPos
--;
944 /* With delete we much check more than just inputLineLength */
945 else if (userConsole
->inputCursorPos
==
946 userConsole
->inputLineLength
)
949 c
= userConsole
->inputLine
+
950 userConsole
->inputCursorPos
;
952 userConsole
->inputLine
+
953 --userConsole
->inputLineLength
;
964 else if (e
->key
.keysym
.sym
== KEY_RETURN
) {
965 char *argv
[MAX_COMMAND_ARGS
];
968 /* Yank the command history if necessary */
969 OGLCONSOLE_YankHistory(userConsole
);
971 /* Add user's command to history */
972 OGLCONSOLE_AddHistory(userConsole
,
973 userConsole
->inputLine
);
975 /* Print user's command to the console */
976 OGLCONSOLE_Output((void *) userConsole
, "%s\n",
977 userConsole
->inputLine
);
979 /* Invoke console's enter-key callback function */
980 if (command_split(userConsole
->inputLine
, &argc
, argv
,
982 OGLCONSOLE_Output((void *)userConsole
,
983 "Warning: Command line too long!\n");
985 userConsole
->enterKeyCallback((void *) userConsole
,
988 /* Erase command line */
989 userConsole
->inputCursorPos
= 0;
990 userConsole
->inputLineLength
= 0;
991 userConsole
->inputLine
[0] = '\0';
996 else if (e
->key
.keysym
.sym
== SDLK_PAGEUP
) {
997 userConsole
->lineScrollIndex
-=
998 userConsole
->textHeight
/ 2;
1000 if (userConsole
->lineScrollIndex
< 0)
1001 userConsole
->lineScrollIndex
+=
1002 userConsole
->maxLines
;
1005 printf("scroll index = %i\n",
1006 userConsole
->lineScrollIndex
);
1010 else if (e
->key
.keysym
.sym
== SDLK_PAGEDOWN
) {
1011 userConsole
->lineScrollIndex
+=
1012 userConsole
->textHeight
/ 2;
1014 if (userConsole
->lineScrollIndex
>=
1015 userConsole
->maxLines
)
1016 userConsole
->lineScrollIndex
-=
1017 userConsole
->maxLines
;
1020 printf("scroll index = %i\n",
1021 userConsole
->lineScrollIndex
);
1025 else if (e
->key
.keysym
.sym
== KEY_UP
) {
1026 // Shift key is for scrolling the output display
1027 if (e
->key
.keysym
.mod
& (KMOD_LSHIFT
| KMOD_RSHIFT
)) {
1028 if (--userConsole
->lineScrollIndex
< 0)
1029 userConsole
->lineScrollIndex
=
1030 userConsole
->maxLines
- 1;
1032 // No shift key is for scrolling through command history
1034 // -1 means we aren't look at history yet
1035 if (userConsole
->historyScrollIndex
== -1) {
1036 userConsole
->historyScrollIndex
=
1037 userConsole
->historyQueueIndex
;
1039 // Wrap our history scrolling
1040 if (--userConsole
->historyScrollIndex
<
1043 historyScrollIndex
=
1047 // If we've returned to our current position in the command
1048 // history, we'll just drop out of history mode
1049 if (userConsole
->historyScrollIndex
==
1050 userConsole
->historyQueueIndex
+ 1)
1051 userConsole
->historyScrollIndex
= -1;
1057 else if (e
->key
.keysym
.sym
== KEY_DOWN
) {
1058 // Shift key is for scrolling the output display
1059 if (e
->key
.keysym
.mod
& (KMOD_LSHIFT
| KMOD_RSHIFT
)) {
1060 if (++userConsole
->lineScrollIndex
>=
1061 userConsole
->maxLines
)
1062 userConsole
->lineScrollIndex
= 0;
1064 // No shift key is for scrolling through command history
1066 // -1 means we aren't look at history yet
1067 if (userConsole
->historyScrollIndex
!= -1) {
1068 // Wrap our history scrolling
1069 if (++userConsole
->historyScrollIndex
>=
1072 historyScrollIndex
= 0;
1074 // If we've returned to our current position in the command
1075 // history, we'll just drop out of history mode
1076 if (userConsole
->historyScrollIndex
==
1077 userConsole
->historyQueueIndex
+ 1)
1079 historyScrollIndex
= -1;
1081 // TODO: be like, no bitch, there's no history down there
1087 else if (e
->key
.keysym
.sym
== KEY_LEFT
) {
1088 /* Yank the command history if necessary */
1089 OGLCONSOLE_YankHistory(userConsole
);
1091 if (userConsole
->inputCursorPos
> 0)
1092 userConsole
->inputCursorPos
--;
1097 else if (e
->key
.keysym
.sym
== KEY_RIGHT
) {
1098 /* Yank the command history if necessary */
1099 OGLCONSOLE_YankHistory(userConsole
);
1101 if (userConsole
->inputCursorPos
<
1102 userConsole
->inputLineLength
)
1103 userConsole
->inputCursorPos
++;