From c2b8c4445746e076b248030967926d49d4108b2b Mon Sep 17 00:00:00 2001 From: divverent Date: Wed, 20 May 2009 05:53:12 +0000 Subject: [PATCH] improve abstraction of the console buffers; console drawing still needs to be changed to not use the conbuffer_t struct directly git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@8979 d7cf8633-e32d-0410-b094-e92efae38249 --- console.c | 66 +++++++++++++++++++++++++++++++++++-------------------- console.h | 15 ++++++++----- host.c | 1 + keys.c | 8 ++++--- 4 files changed, 57 insertions(+), 33 deletions(-) diff --git a/console.c b/console.c index 67618ab9..5648a491 100644 --- a/console.c +++ b/console.c @@ -36,12 +36,14 @@ int con_backscroll; conbuffer_t con; -#define CON_LINES_IDX(i) CONBUF_LINES_IDX(&con, i) -#define CON_LINES_UNIDX(i) CONBUF_LINES_UNIDX(&con, i) -#define CON_LINES_LAST CONBUF_LINES_LAST(&con) -#define CON_LINES(i) CONBUF_LINES(&con, i) -#define CON_LINES_PRED(i) CONBUF_LINES_PRED(&con, i) -#define CON_LINES_SUCC(i) CONBUF_LINES_SUCC(&con, i) +#define CON_LINES_IDX(i) CONBUFFER_LINES_IDX(&con, i) +#define CON_LINES_UNIDX(i) CONBUFFER_LINES_UNIDX(&con, i) +#define CON_LINES_LAST CONBUFFER_LINES_LAST(&con) +#define CON_LINES(i) CONBUFFER_LINES(&con, i) +#define CON_LINES_PRED(i) CONBUFFER_LINES_PRED(&con, i) +#define CON_LINES_SUCC(i) CONBUFFER_LINES_SUCC(&con, i) +#define CON_LINES_FIRST CONBUFFER_LINES_FIRST(&con) +#define CON_LINES_COUNT CONBUFFER_LINES_COUNT(&con) cvar_t con_notifytime = {CVAR_SAVE, "con_notifytime","3", "how long notify lines last, in seconds"}; cvar_t con_notify = {CVAR_SAVE, "con_notify","4", "how many notify lines to show"}; @@ -124,6 +126,8 @@ void ConBuffer_Shutdown(conbuffer_t *buf) { Mem_Free(buf->text); Mem_Free(buf->lines); + buf->text = NULL; + buf->lines = NULL; } /* @@ -140,11 +144,11 @@ void ConBuffer_FixTimes(conbuffer_t *buf) int i; if(buf->lines_count >= 1) { - double diff = cl.time - (buf->lines + CONBUF_LINES_LAST(buf))->addtime; + double diff = cl.time - (buf->lines + CONBUFFER_LINES_LAST(buf))->addtime; if(diff < 0) { for(i = 0; i < buf->lines_count; ++i) - CONBUF_LINES(buf, i).addtime += diff; + CONBUFFER_LINES(buf, i).addtime += diff; } } } @@ -161,7 +165,7 @@ void ConBuffer_DeleteLine(conbuffer_t *buf) if(buf->lines_count == 0) return; --buf->lines_count; - buf->lines_first = CONBUF_LINES_IDX(buf, 1); + buf->lines_first = CONBUFFER_LINES_IDX(buf, 1); } /* @@ -195,7 +199,7 @@ static char *ConBuffer_BytesLeft(conbuffer_t *buf, int len) else { char *firstline_start = buf->lines[buf->lines_first].start; - char *lastline_onepastend = buf->lines[CONBUF_LINES_LAST(buf)].start + buf->lines[CONBUF_LINES_LAST(buf)].len; + char *lastline_onepastend = buf->lines[CONBUFFER_LINES_LAST(buf)].start + buf->lines[CONBUFFER_LINES_LAST(buf)].len; // the buffer is cyclic, so we first have two cases... if(firstline_start < lastline_onepastend) // buffer is contiguous { @@ -247,7 +251,7 @@ void ConBuffer_AddLine(conbuffer_t *buf, const char *line, int len, int mask) //fprintf(stderr, "Now have %d lines (%d -> %d).\n", buf->lines_count, buf->lines_first, CON_LINES_LAST); - p = buf->lines + CONBUF_LINES_LAST(buf); + p = buf->lines + CONBUFFER_LINES_LAST(buf); p->start = putpos; p->len = len; p->addtime = cl.time; @@ -262,7 +266,7 @@ int ConBuffer_FindPrevLine(conbuffer_t *buf, int mask_must, int mask_mustnot, in start = buf->lines_count; for(i = start - 1; i >= 0; --i) { - con_lineinfo_t *l = &CONBUF_LINES(buf, i); + con_lineinfo_t *l = &CONBUFFER_LINES(buf, i); if((l->mask & mask_must) != mask_must) continue; @@ -280,7 +284,7 @@ int Con_FindNextLine(conbuffer_t *buf, int mask_must, int mask_mustnot, int star int i; for(i = start + 1; i < buf->lines_count; ++i) { - con_lineinfo_t *l = &CONBUF_LINES(buf, i); + con_lineinfo_t *l = &CONBUFFER_LINES(buf, i); if((l->mask & mask_must) != mask_must) continue; @@ -296,7 +300,7 @@ int Con_FindNextLine(conbuffer_t *buf, int mask_must, int mask_mustnot, int star const char *ConBuffer_GetLine(conbuffer_t *buf, int i) { static char copybuf[MAX_INPUTLINE]; - con_lineinfo_t *l = &CONBUF_LINES(buf, i); + con_lineinfo_t *l = &CONBUFFER_LINES(buf, i); size_t sz = l->len+1 > sizeof(copybuf) ? sizeof(copybuf) : l->len+1; strlcpy(copybuf, l->start, sz); return copybuf; @@ -597,7 +601,7 @@ Clear all notify lines. void Con_ClearNotify (void) { int i; - for(i = 0; i < con.lines_count; ++i) + for(i = 0; i < CON_LINES_COUNT; ++i) CON_LINES(i).mask |= CON_MASK_HIDENOTIFY; } @@ -658,13 +662,14 @@ void Con_CheckResize (void) Cvar_SetValueQuick(&con_textsize, f); width = (int)floor(vid_conwidth.value / con_textsize.value); width = bound(1, width, con.textsize/4); + // FIXME uses con in a non abstracted way if (width == con_linewidth) return; con_linewidth = width; - for(i = 0; i < con.lines_count; ++i) + for(i = 0; i < CON_LINES_COUNT; ++i) CON_LINES(i).height = -1; // recalculate when next needed Con_ClearNotify(); @@ -701,7 +706,7 @@ void Con_ConDump_f (void) Con_Printf("condump: unable to write file \"%s\"\n", Cmd_Argv(1)); return; } - for(i = 0; i < con.lines_count; ++i) + for(i = 0; i < CON_LINES_COUNT; ++i) { FS_Write(file, CON_LINES(i).start, CON_LINES(i).len); FS_Write(file, "\n", 1); @@ -773,6 +778,10 @@ void Con_Init (void) Con_DPrint("Console initialized.\n"); } +void Con_Shutdown (void) +{ + ConBuffer_Shutdown(&con); +} /* ================ @@ -793,6 +802,9 @@ void Con_PrintToHistory(const char *txt, int mask) static char buf[CON_TEXTSIZE]; static int bufpos = 0; + if(!con.text) // FIXME uses a non-abstracted property of con + return; + for(; *txt; ++txt) { if(cr_pending) @@ -815,7 +827,7 @@ void Con_PrintToHistory(const char *txt, int mask) break; default: buf[bufpos++] = *txt; - if(bufpos >= con.textsize - 1) + if(bufpos >= con.textsize - 1) // FIXME uses a non-abstracted property of con { ConBuffer_AddLine(&con, buf, bufpos, mask); bufpos = 0; @@ -1463,8 +1475,8 @@ int Con_DrawNotifyRect(int mask_must, int mask_mustnot, float maxage, float x, f continuationWidth = (int) Con_WordWidthFunc(&ti, continuationString, &l, -1); // first find the first line to draw by backwards iterating and word wrapping to find their length... - startidx = con.lines_count; - for(i = con.lines_count - 1; i >= 0; --i) + startidx = CON_LINES_COUNT; + for(i = CON_LINES_COUNT - 1; i >= 0; --i) { con_lineinfo_t *l = &CON_LINES(i); int mylines; @@ -1495,7 +1507,7 @@ int Con_DrawNotifyRect(int mask_must, int mask_mustnot, float maxage, float x, f ti.y = y + alignment_y * (height - lines * fontsize) - nskip * fontsize; // then actually draw - for(i = startidx; i < con.lines_count; ++i) + for(i = startidx; i < CON_LINES_COUNT; ++i) { con_lineinfo_t *l = &CON_LINES(i); @@ -1625,6 +1637,7 @@ int Con_MeasureConsoleLine(int lineno) ti.font = FONT_CONSOLE; return COM_Wordwrap(con.lines[lineno].start, con.lines[lineno].len, 0, width, Con_WordWidthFunc, &ti, Con_CountLineFunc, NULL); + // FIXME uses con in a non abstracted way } /* @@ -1640,6 +1653,7 @@ int Con_LineHeight(int i) if(h != -1) return h; return con.lines[i].height = Con_MeasureConsoleLine(i); + // FIXME uses con in a non abstracted way } /* @@ -1670,6 +1684,7 @@ int Con_DrawConsoleLine(float y, int lineno, float ymin, float ymax) ti.width = width; return COM_Wordwrap(con.lines[lineno].start, con.lines[lineno].len, 0, width, Con_WordWidthFunc, &ti, Con_DisplayLineFunc, &ti); + // FIXME uses con in a non abstracted way } /* @@ -1689,9 +1704,9 @@ void Con_LastVisibleLine(int *last, int *limitlast) con_backscroll = 0; // now count until we saw con_backscroll actual lines - for(ic = 0; ic < con.lines_count; ++ic) + for(ic = 0; ic < CON_LINES_COUNT; ++ic) { - int i = CON_LINES_IDX(con.lines_count - 1 - ic); + int i = CON_LINES_IDX(CON_LINES_COUNT - 1 - ic); int h = Con_LineHeight(i); // line is the last visible line? @@ -1709,6 +1724,7 @@ void Con_LastVisibleLine(int *last, int *limitlast) // visible then. con_backscroll = lines_seen - 1; *last = con.lines_first; + // FIXME uses con in a non abstracted way *limitlast = 1; } @@ -1735,7 +1751,7 @@ void Con_DrawConsole (int lines) DrawQ_String_Font(vid_conwidth.integer - DrawQ_TextWidth_Font(engineversion, 0, false, FONT_CONSOLE) * con_textsize.value, lines - con_textsize.value, engineversion, 0, con_textsize.value, con_textsize.value, 1, 0, 0, 1, 0, NULL, true, FONT_CONSOLE); // draw the text - if(con.lines_count > 0) + if(CON_LINES_COUNT > 0) { float ymax = con_vislines - 2 * con_textsize.value; Con_LastVisibleLine(&last, &limitlast); @@ -1743,12 +1759,14 @@ void Con_DrawConsole (int lines) if(limitlast) y += (con.lines[last].height - limitlast) * con_textsize.value; + // FIXME uses con in a non abstracted way i = last; for(;;) { y -= Con_DrawConsoleLine(y, i, 0, ymax) * con_textsize.value; if(i == con.lines_first) + // FIXME uses con in a non abstracted way break; // top of console buffer if(y < 0) break; // top of console window diff --git a/console.h b/console.h index 4cce55ab..54b75310 100644 --- a/console.h +++ b/console.h @@ -35,6 +35,7 @@ void Con_Rcon_Redirect_Abort(); void Con_CheckResize (void); void Con_Init (void); void Con_Init_Commands (void); +void Con_Shutdown (void); void Con_DrawConsole (int lines); void Con_Print(const char *txt); void Con_Printf(const char *fmt, ...) DP_FUNC_PRINTF(1); @@ -95,12 +96,14 @@ typedef struct conbuffer_s } conbuffer_t; -#define CONBUF_LINES_IDX(buf, i) (((buf)->lines_first + (i)) % (buf)->maxlines) -#define CONBUF_LINES_UNIDX(buf, i) (((i) - (buf)->lines_first + (buf)->maxlines) % (buf)->maxlines) -#define CONBUF_LINES_LAST(buf) CONBUF_LINES_IDX(buf, (buf)->lines_count - 1) -#define CONBUF_LINES(buf, i) (buf)->lines[CONBUF_LINES_IDX(buf, i)] -#define CONBUF_LINES_PRED(buf, i) (((i) + (buf)->maxlines - 1) % (buf)->maxlines) -#define CONBUF_LINES_SUCC(buf, i) (((i) + 1) % (buf)->maxlines) +#define CONBUFFER_LINES_IDX(buf, i) (((buf)->lines_first + (i)) % (buf)->maxlines) +#define CONBUFFER_LINES_UNIDX(buf, i) (((i) - (buf)->lines_first + (buf)->maxlines) % (buf)->maxlines) +#define CONBUFFER_LINES_LAST(buf) CONBUFFER_LINES_IDX(buf, (buf)->lines_count - 1) +#define CONBUFFER_LINES(buf, i) (buf)->lines[CONBUFFER_LINES_IDX(buf, i)] +#define CONBUFFER_LINES_PRED(buf, i) (((i) + (buf)->maxlines - 1) % (buf)->maxlines) +#define CONBUFFER_LINES_SUCC(buf, i) (((i) + 1) % (buf)->maxlines) +#define CONBUFFER_LINES_FIRST(buf) ((buf)->lines_first) +#define CONBUFFER_LINES_COUNT(buf) ((buf)->lines_count) void ConBuffer_Init(conbuffer_t *buf, int textsize, int maxlines, mempool_t *mempool); void ConBuffer_Clear (conbuffer_t *buf); diff --git a/host.c b/host.c index afce873d..7e0cdbc3 100644 --- a/host.c +++ b/host.c @@ -1220,6 +1220,7 @@ void Host_Shutdown(void) Sys_Shutdown(); Log_Close(); FS_Shutdown(); + Con_Shutdown(); Memory_Shutdown(); } diff --git a/keys.c b/keys.c index f5b9b74f..2d7431fa 100644 --- a/keys.c +++ b/keys.c @@ -89,10 +89,12 @@ static void Key_History_Shutdown() if(historyfile) { int i; - for(i = 0; i < history.lines_count; ++i) + for(i = 0; i < CONBUFFER_LINES_COUNT(&history); ++i) FS_Printf(historyfile, "%s\n", ConBuffer_GetLine(&history, i)); FS_Close(historyfile); } + + ConBuffer_Shutdown(&history); } static void Key_History_Push() @@ -112,7 +114,7 @@ static void Key_History_Up() if(history_line == -1) { - history_line = history.lines_count - 1; + history_line = CONBUFFER_LINES_COUNT(&history) - 1; if(history_line != -1) { strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1); @@ -132,7 +134,7 @@ static void Key_History_Down() if(history_line == -1) // editing the "new" line return; - if(history_line < history.lines_count - 1) + if(history_line < CONBUFFER_LINES_COUNT(&history) - 1) { ++history_line; strlcpy(key_line + 1, ConBuffer_GetLine(&history, history_line), sizeof(key_line) - 1); -- 2.39.2