From b1c90e16bb134c43fb3fe78f4da40d83da09a50d Mon Sep 17 00:00:00 2001 From: Lon Hohberger Date: Fri, 17 Oct 2008 10:57:38 -0400 Subject: [PATCH] Reinitialize console text / font on resolution change --- main.c | 1 + oglconsole.c | 100 +++++++++++++++++++++++++++++++++++++++++------------------ oglconsole.h | 1 + 3 files changed, 71 insertions(+), 31 deletions(-) diff --git a/main.c b/main.c index a91e159..b3c160c 100644 --- a/main.c +++ b/main.c @@ -111,6 +111,7 @@ static void console_input(OGLCONSOLE_Console console, char *cmd) } screen_set_resolution(width, height); screen_switch_resolution(); + OGLCONSOLE_InitText(console, width, height); return; } diff --git a/oglconsole.c b/oglconsole.c index bd23d24..055ec8a 100644 --- a/oglconsole.c +++ b/oglconsole.c @@ -117,6 +117,7 @@ typedef struct { /* Screen+scrollback lines (console output) */ char *lines; + int lines_size; int maxLines, lineQueueIndex, lineScrollIndex; /* History scrollback (command input) */ @@ -165,23 +166,24 @@ OGLCONSOLE_DefaultEnterKeyCallback(OGLCONSOLE_Console console, char __attribute_ "No enter key callback is registered for this console!\n"); } -OGLCONSOLE_Console -OGLCONSOLE_Create() -{ - _OGLCONSOLE_Console *console; - GLint viewport[4]; - - /* If font hasn't been created, we create it */ - if (!glIsTexture(OGLCONSOLE_glFontHandle)) - OGLCONSOLE_CreateFont(); - /* Allocate memory for our console */ - console = (void *) malloc(sizeof (_OGLCONSOLE_Console)); +void +OGLCONSOLE_InitText(void *c, int res_x, int res_y) +{ + _OGLCONSOLE_Console *console = c; + char *new_lines; + int new_lines_size; + + if (!console) { + console = userConsole; + if (!userConsole) + return; + } /* Textual dimensions */ - glGetIntegerv(GL_VIEWPORT, viewport); - console->textWidth = viewport[2] / CHAR_PIXEL_W; - console->textHeight = viewport[3] / CHAR_PIXEL_H; + + console->textWidth = res_x / CHAR_PIXEL_W; + console->textHeight = res_y / CHAR_PIXEL_H; console->characterWidth = 1.0 / console->textWidth; console->characterHeight = 1.0 / console->textHeight; @@ -211,11 +213,33 @@ OGLCONSOLE_Create() /* Screen and scrollback lines */ /* This is the total number of screen lines in memory (start blank) */ console->maxLines = DEFAULT_MAX_LINES; - /* Allocate space for text */ - console->lines = - (char *) malloc(console->maxLines * (console->textWidth + 1)); - /* Initialize to empty strings */ - memset(console->lines, 0, console->maxLines * (console->textWidth + 1)); + + /* (re)allocate space for text */ + new_lines_size = console->maxLines * console->textWidth + 1; + new_lines = malloc(new_lines_size); + /* XXX return value */ + memset(new_lines, 0, new_lines_size); + + if (console->lines) { +#if 0 + if (new_lines_size >= console->lines_size) { + memcpy(new_lines, console->lines, console->lines_size); + } else { + memcpy(new_lines, + console->lines + (console->lines_size-new_lines_size), + new_lines_size); + } +#endif + + free(console->lines); + } + + console->lines_size = new_lines_size; + console->lines = new_lines; + + /* This cursor points to the X pos where console output is next destined */ + console->outputCursor = console->lines; + /* This variable represents whether or not a newline has been left */ console->outputNewline = 0; /* This cursor points to the X pos where console output is next destined */ @@ -230,10 +254,31 @@ OGLCONSOLE_Create() console->inputCursorPos = 0; console->inputLine[0] = '\0'; - /* History lines */ - memset(console->history, 0, MAX_INPUT_LENGTH * MAX_HISTORY_COUNT); - console->historyQueueIndex = 0; - console->historyScrollIndex = -1; + OGLCONSOLE_Output((void *) console, + "Console Resolution: %i x %i\n" + "Text Size: %i x %i\n", + res_x, res_y, + console->textWidth, console->textHeight); +} + +OGLCONSOLE_Console +OGLCONSOLE_Create() +{ + _OGLCONSOLE_Console *console; + GLint viewport[4]; + + /* If font hasn't been created, we create it */ + if (!glIsTexture(OGLCONSOLE_glFontHandle)) + OGLCONSOLE_CreateFont(); + + /* Allocate memory for our console */ + console = (void *) malloc(sizeof (_OGLCONSOLE_Console)); + memset(console, 0, sizeof(*console)); + + console->maxLines = DEFAULT_MAX_LINES; + + glGetIntegerv(GL_VIEWPORT, viewport); + OGLCONSOLE_InitText(console, viewport[2], viewport[3]); /* Callbacks */ console->enterKeyCallback = OGLCONSOLE_DefaultEnterKeyCallback; @@ -248,16 +293,9 @@ OGLCONSOLE_Create() userConsole = console; /* Temporary shit */ - OGLCONSOLE_Output((void *) console, "Console initialized\n"); - - OGLCONSOLE_Output((void *) console, - "Console display lines: %i\n", console->textHeight); - - OGLCONSOLE_Output((void *) console, - "Console display columns: %i\n", console->textWidth); - OGLCONSOLE_Output((void *) console, "Console input length: %i\n", MAX_INPUT_LENGTH); + OGLCONSOLE_Output((void *) console, "Console initialized\n"); /* Return the opaque pointer to the programmer */ return (OGLCONSOLE_Console) console; diff --git a/oglconsole.h b/oglconsole.h index 363a2a5..cd26910 100644 --- a/oglconsole.h +++ b/oglconsole.h @@ -14,6 +14,7 @@ typedef struct _OGLCONSOLE_Console *OGLCONSOLE_Console; /* Initialize/uninitialize OGLConsole */ OGLCONSOLE_Console OGLCONSOLE_Create(void); +void OGLCONSOLE_InitText(void *console, int res_x, int res_y); void OGLCONSOLE_Destroy(OGLCONSOLE_Console console); void OGLCONSOLE_Quit(void); -- 2.11.4.GIT