DOOM-3-BFG

DOOM 3 BFG Edition
Log | Files | Refs

hu_lib.cpp (7243B)


      1 /*
      2 ===========================================================================
      3 
      4 Doom 3 BFG Edition GPL Source Code
      5 Copyright (C) 1993-2012 id Software LLC, a ZeniMax Media company. 
      6 
      7 This file is part of the Doom 3 BFG Edition GPL Source Code ("Doom 3 BFG Edition Source Code").  
      8 
      9 Doom 3 BFG Edition Source Code is free software: you can redistribute it and/or modify
     10 it under the terms of the GNU General Public License as published by
     11 the Free Software Foundation, either version 3 of the License, or
     12 (at your option) any later version.
     13 
     14 Doom 3 BFG Edition Source Code is distributed in the hope that it will be useful,
     15 but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 GNU General Public License for more details.
     18 
     19 You should have received a copy of the GNU General Public License
     20 along with Doom 3 BFG Edition Source Code.  If not, see <http://www.gnu.org/licenses/>.
     21 
     22 In addition, the Doom 3 BFG Edition Source Code is also subject to certain additional terms. You should have received a copy of these additional terms immediately following the terms and conditions of the GNU General Public License which accompanied the Doom 3 BFG Edition Source Code.  If not, please request a copy in writing from id Software at the address below.
     23 
     24 If you have questions concerning this license or the applicable additional terms, you may contact in writing id Software LLC, c/o ZeniMax Media Inc., Suite 120, Rockville, Maryland 20850 USA.
     25 
     26 ===========================================================================
     27 */
     28 
     29 #include "Precompiled.h"
     30 #include "globaldata.h"
     31 
     32 #include <ctype.h>
     33 
     34 #include "doomdef.h"
     35 
     36 #include "v_video.h"
     37 #include "m_swap.h"
     38 
     39 #include "hu_lib.h"
     40 #include "r_local.h"
     41 #include "r_draw.h"
     42 
     43 // qboolean : whether the screen is always erased
     44 
     45 
     46 void HUlib_init(void)
     47 {
     48 }
     49 
     50 void HUlib_clearTextLine(hu_textline_t* t)
     51 {
     52     t->len = 0;
     53     t->l[0] = 0;
     54     t->needsupdate = true;
     55 }
     56 
     57 void
     58 HUlib_initTextLine
     59 ( hu_textline_t*	t,
     60   int			x,
     61   int			y,
     62   patch_t**		f,
     63   int			sc )
     64 {
     65     t->x = x;
     66     t->y = y;
     67     t->f = f;
     68     t->sc = sc;
     69     HUlib_clearTextLine(t);
     70 }
     71 
     72 qboolean
     73 HUlib_addCharToTextLine
     74 ( hu_textline_t*	t,
     75   char			ch )
     76 {
     77 
     78     if (t->len == HU_MAXLINELENGTH)
     79 	return false;
     80     else
     81     {
     82 	t->l[t->len++] = ch;
     83 	t->l[t->len] = 0;
     84 	t->needsupdate = 4;
     85 	return true;
     86     }
     87 
     88 }
     89 
     90 qboolean HUlib_delCharFromTextLine(hu_textline_t* t)
     91 {
     92 
     93     if (!t->len) return false;
     94     else
     95     {
     96 	t->l[--t->len] = 0;
     97 	t->needsupdate = 4;
     98 	return true;
     99     }
    100 
    101 }
    102 
    103 void
    104 HUlib_drawTextLine
    105 ( hu_textline_t*	l,
    106   qboolean		drawcursor )
    107 {
    108 
    109     int			i;
    110     int			w;
    111     int			x;
    112     unsigned char	c;
    113 
    114     // draw the new stuff
    115     x = l->x;
    116     for (i=0;i<l->len;i++)
    117     {
    118 	c = toupper(l->l[i]);
    119 	if (c != ' '
    120 	    && c >= l->sc
    121 	    && c <= '_')
    122 	{
    123 	    w = SHORT(l->f[c - l->sc]->width);
    124 	    if (x+w > SCREENWIDTH)
    125 		break;
    126 	    V_DrawPatchDirect(x, l->y, FG, l->f[c - l->sc]);
    127 	    x += w;
    128 	}
    129 	else
    130 	{
    131 	    x += 4;
    132 	    if (x >= SCREENWIDTH)
    133 		break;
    134 	}
    135     }
    136 
    137     // draw the cursor if requested
    138     if (drawcursor
    139 	&& x + SHORT(l->f['_' - l->sc]->width) <= SCREENWIDTH)
    140     {
    141 	V_DrawPatchDirect(x, l->y, FG, l->f['_' - l->sc]);
    142     }
    143 }
    144 
    145 
    146 // sorta called by HU_Erase and just better darn get things straight
    147 void HUlib_eraseTextLine(hu_textline_t* l)
    148 {
    149     int			lh;
    150     int			y;
    151     int			yoffset;
    152 
    153     // Only erases when NOT in automap and the screen is reduced,
    154     // and the text must either need updating or refreshing
    155     // (because of a recent change back from the automap)
    156 
    157     if (!::g->automapactive &&
    158 	::g->viewwindowx && l->needsupdate)
    159     {
    160 	lh = SHORT(l->f[0]->height) + 1;
    161 	for (y=l->y,yoffset=y*SCREENWIDTH ; y<l->y+lh ; y++,yoffset+=SCREENWIDTH)
    162 	{
    163 	    if (y < ::g->viewwindowy || y >= ::g->viewwindowy + ::g->viewheight)
    164 		R_VideoErase(yoffset, SCREENWIDTH); // erase entire line
    165 	    else
    166 	    {
    167 		R_VideoErase(yoffset, ::g->viewwindowx); // erase left border
    168 		R_VideoErase(yoffset + ::g->viewwindowx + ::g->viewwidth, ::g->viewwindowx);
    169 		// erase right border
    170 	    }
    171 	}
    172     }
    173 
    174     ::g->lastautomapactive = ::g->automapactive;
    175     if (l->needsupdate) l->needsupdate--;
    176 
    177 }
    178 
    179 void
    180 HUlib_initSText
    181 ( hu_stext_t*	s,
    182   int		x,
    183   int		y,
    184   int		h,
    185   patch_t**	font,
    186   int		startchar,
    187   qboolean*	on )
    188 {
    189 
    190     int i;
    191 
    192     s->h = h;
    193     s->on = on;
    194     s->laston = true;
    195     s->cl = 0;
    196     for (i=0;i<h;i++)
    197 	HUlib_initTextLine(&s->l[i],
    198 			   x, y - i*(SHORT(font[0]->height)+1),
    199 			   font, startchar);
    200 
    201 }
    202 
    203 void HUlib_addLineToSText(hu_stext_t* s)
    204 {
    205 
    206     int i;
    207 
    208     // add a clear line
    209     if (++s->cl == s->h)
    210 	s->cl = 0;
    211     HUlib_clearTextLine(&s->l[s->cl]);
    212 
    213     // everything needs updating
    214     for (i=0 ; i<s->h ; i++)
    215 	s->l[i].needsupdate = 4;
    216 
    217 }
    218 
    219 void
    220 HUlib_addMessageToSText
    221 ( hu_stext_t*	s,
    222   const char*		prefix,
    223   const char*		msg )
    224 {
    225     HUlib_addLineToSText(s);
    226     if (prefix)
    227 	while (*prefix)
    228 	    HUlib_addCharToTextLine(&s->l[s->cl], *(prefix++));
    229 
    230     while (*msg)
    231 	HUlib_addCharToTextLine(&s->l[s->cl], *(msg++));
    232 }
    233 
    234 void HUlib_drawSText(hu_stext_t* s)
    235 {
    236     int i, idx;
    237     hu_textline_t *l;
    238 
    239     if (!*s->on)
    240 	return; // if not on, don't draw
    241 
    242     // draw everything
    243     for (i=0 ; i<s->h ; i++)
    244     {
    245 	idx = s->cl - i;
    246 	if (idx < 0)
    247 	    idx += s->h; // handle queue of ::g->lines
    248 	
    249 	l = &s->l[idx];
    250 
    251 	// need a decision made here on whether to skip the draw
    252 	HUlib_drawTextLine(l, false); // no cursor, please
    253     }
    254 
    255 }
    256 
    257 void HUlib_eraseSText(hu_stext_t* s)
    258 {
    259 
    260     int i;
    261 
    262     for (i=0 ; i<s->h ; i++)
    263     {
    264 	if (s->laston && !*s->on)
    265 	    s->l[i].needsupdate = 4;
    266 	HUlib_eraseTextLine(&s->l[i]);
    267     }
    268     s->laston = *s->on;
    269 
    270 }
    271 
    272 void
    273 HUlib_initIText
    274 ( hu_itext_t*	it,
    275   int		x,
    276   int		y,
    277   patch_t**	font,
    278   int		startchar,
    279   qboolean*	on )
    280 {
    281     it->lm = 0; // default left margin is start of text
    282     it->on = on;
    283     it->laston = true;
    284     HUlib_initTextLine(&it->l, x, y, font, startchar);
    285 }
    286 
    287 
    288 // The following deletion routines adhere to the left margin restriction
    289 void HUlib_delCharFromIText(hu_itext_t* it)
    290 {
    291     if (it->l.len != it->lm)
    292 	HUlib_delCharFromTextLine(&it->l);
    293 }
    294 
    295 void HUlib_eraseLineFromIText(hu_itext_t* it)
    296 {
    297     while (it->lm != it->l.len)
    298 	HUlib_delCharFromTextLine(&it->l);
    299 }
    300 
    301 // Resets left margin as well
    302 void HUlib_resetIText(hu_itext_t* it)
    303 {
    304     it->lm = 0;
    305     HUlib_clearTextLine(&it->l);
    306 }
    307 
    308 void
    309 HUlib_addPrefixToIText
    310 ( hu_itext_t*	it,
    311   char*		str )
    312 {
    313     while (*str)
    314 	HUlib_addCharToTextLine(&it->l, *(str++));
    315     it->lm = it->l.len;
    316 }
    317 
    318 // wrapper function for handling general keyed input.
    319 // returns true if it ate the key
    320 qboolean
    321 HUlib_keyInIText
    322 ( hu_itext_t*	it,
    323   unsigned char ch )
    324 {
    325 
    326     if (ch >= ' ' && ch <= '_') 
    327   	HUlib_addCharToTextLine(&it->l, (char) ch);
    328     else 
    329 	if (ch == KEY_BACKSPACE) 
    330 	    HUlib_delCharFromIText(it);
    331 	else 
    332 	    if (ch != KEY_ENTER) 
    333 		return false; // did not eat key
    334 
    335     return true; // ate the key
    336 
    337 }
    338 
    339 void HUlib_drawIText(hu_itext_t* it)
    340 {
    341 
    342     hu_textline_t *l = &it->l;
    343 
    344     if (!*it->on)
    345 	return;
    346     HUlib_drawTextLine(l, true); // draw the line w/ cursor
    347 
    348 }
    349 
    350 void HUlib_eraseIText(hu_itext_t* it)
    351 {
    352     if (it->laston && !*it->on)
    353 	it->l.needsupdate = 4;
    354     HUlib_eraseTextLine(&it->l);
    355     it->laston = *it->on;
    356 }
    357 
    358