console.c

来自「quake1 dos源代码最新版本」· C语言 代码 · 共 799 行 · 第 1/2 页

C
799
字号
/*
Copyright (C) 1996-1997 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
// console.c

#ifdef NeXT
#include <libc.h>
#endif
#ifndef _MSC_VER
// 2001-12-10 Compilable with LCC-Win32 by Jeff Ford  start
#ifdef __LCC__
#include <io.h>
#else
// 2001-12-10 Compilable with LCC-Win32 by Jeff Ford  end
#include <unistd.h>
#endif	// 2001-12-10 Compilable with LCC-Win32 by Jeff Ford
#endif
#include <fcntl.h>
#include "quakedef.h"

int 		con_linewidth;

float		con_cursorspeed = 4;

// 2000-01-05 Console scrolling fix by Maddes  start
//#define		CON_TEXTSIZE	16384
#define		CON_TEXTSIZE	64*1024
// 2000-01-05 Console scrolling fix by Maddes  end

qboolean 	con_forcedup;		// because no entities to refresh

int			con_totallines;		// total lines in console scrollback
int			con_backscroll;		// lines up from bottom to display
int			con_current;		// where next message will be printed
int			con_x;				// offset in current line for next print
char		*con_text=0;

cvar_t	*con_notifytime;		//seconds
cvar_t	*con_alpha;				// 2000-08-04 "Transparent" console background for software renderer by Norberto Alfredo Bensa/Maddes
								// 2000-01-11 Transparent console by Radix

#define	NUM_CON_TIMES 4
float		con_times[NUM_CON_TIMES];	// realtime time the line was generated
								// for transparent notify lines

int			con_vislines;

qboolean	con_debuglog;

#define		MAXCMDLINE	256
extern	char	key_lines[32][MAXCMDLINE];
extern	int		edit_line;
extern	int		key_linepos;
extern	int		key_insert;	// 2000-01-05 Console typing enhancement by Radix
					// insert key toggle


qboolean	con_initialized;

int			con_notifylines;		// scan lines to clear for notify lines

extern void M_Menu_Main_f (void);

/*
================
Con_ToggleConsole_f
================
*/
void Con_ToggleConsole_f (void)
{
	if (key_dest == key_console)
	{
		if (cls.state == ca_connected)
		{
			key_dest = key_game;
			key_lines[edit_line][1] = 0;	// clear any typing
			key_linepos = 1;
		}
		else
		{
			M_Menu_Main_f ();
		}
	}
	else
		key_dest = key_console;

	SCR_EndLoadingPlaque ();
	memset (con_times, 0, sizeof(con_times));
}

/*
================
Con_Clear_f
================
*/
void Con_Clear_f (void)
{
	if (con_text)
		Q_memset (con_text, ' ', CON_TEXTSIZE);
	con_current = 0;	// 2000-01-05 Console scrolling fix by Maddes
}


/*
================
Con_ClearNotify
================
*/
void Con_ClearNotify (void)
{
	int		i;

	for (i=0 ; i<NUM_CON_TIMES ; i++)
		con_times[i] = 0;
}


/*
================
Con_MessageMode_f
================
*/
extern qboolean team_message;

void Con_MessageMode_f (void)
{
	key_dest = key_message;
	team_message = false;
}


/*
================
Con_MessageMode2_f
================
*/
void Con_MessageMode2_f (void)
{
	key_dest = key_message;
	team_message = true;
}


/*
================
Con_CheckResize

If the line width has changed, reformat the buffer.
================
*/
void Con_CheckResize (void)
{
	int		i, j, width, oldwidth, oldtotallines, numlines, numchars;
	char	tbuf[CON_TEXTSIZE];

	width = (vid.width >> 3) - 2;

	if (width == con_linewidth)
		return;

	if (width < 1)			// video hasn't been initialized yet
	{
		width = 38;
		con_linewidth = width;
		con_totallines = CON_TEXTSIZE / con_linewidth;
		Q_memset (con_text, ' ', CON_TEXTSIZE);
		con_current = 0;	// 2000-01-05 Console scrolling fix by Maddes
	}
	else
	{
		oldwidth = con_linewidth;
		oldtotallines = con_totallines;

		con_linewidth = width;
		con_totallines = CON_TEXTSIZE / con_linewidth;

// 2000-01-05 Console scrolling fix by Maddes  start
//		numlines = oldtotallines;
		numlines = (oldtotallines < con_current) ? oldtotallines : con_current;	// available lines in old buffer
// 2000-01-05 Console scrolling fix by Maddes  end
		if (numlines > con_totallines)	// 2000-01-05 Console scrolling fix by Maddes
			numlines = con_totallines;

		numchars = oldwidth;
		if (numchars > con_linewidth)	// 2000-01-05 Console scrolling fix by Maddes
			numchars = con_linewidth;

		Q_memcpy (tbuf, con_text, CON_TEXTSIZE);
		Q_memset (con_text, ' ', CON_TEXTSIZE);

		for (i=0 ; i<numlines ; i++)
		{
			for (j=0 ; j<numchars ; j++)
			{
// 2000-01-05 Console scrolling fix by Maddes  start
/*
				con_text[(con_totallines - 1 - i) * con_linewidth + j] =
						tbuf[((con_current - i + oldtotallines) %
							  oldtotallines) * oldwidth + j];
*/
				con_text[i*con_linewidth + j] =
					tbuf[((con_current-numlines+i) % oldtotallines) * oldwidth + j];
// 2000-01-05 Console scrolling fix by Maddes  end
			}
		}
		con_current = numlines;	// 2000-01-05 Console scrolling fix by Maddes

		Con_ClearNotify ();
	}

	con_backscroll = 0;
//	con_current = con_totallines - 1;	// 2000-01-05 Console scrolling fix by Maddes
}

// 2001-09-18 New cvar system by Maddes (Init)  start
/*
================
Con_Init_Cvars
================
*/
void Con_Init_Cvars (void)
{
	con_notifytime = Cvar_Get ("con_notifytime", "3", CVAR_ORIGINAL);

// 2000-08-04 "Transparent" console background for software renderer by Norberto Alfredo Bensa/Maddes  start
// 2000-01-11 Transparent console by Radix  start
	con_alpha = Cvar_Get ("con_alpha", "1", CVAR_ARCHIVE);
	Cvar_SetRangecheck (con_alpha, Cvar_RangecheckFloat, 0, 1);
	Cvar_Set(con_alpha, con_alpha->string);	// do rangecheck
// 2000-01-11 Transparent console by Radix  end
// 2000-08-04 "Transparent" console background for software renderer by Norberto Alfredo Bensa/Maddes  end
}
// 2001-09-18 New cvar system by Maddes (Init)  end

/*
================
Con_Init
================
*/
void Con_Init (void)
{
#define MAXGAMEDIRLEN	1000
	char	temp[MAXGAMEDIRLEN+1];
	char	*t2 = "/qconsole.log";

	con_debuglog = COM_CheckParm("-condebug");

	if (con_debuglog)
	{
		if (strlen (com_gamedir) < (MAXGAMEDIRLEN - strlen (t2)))
		{
			sprintf (temp, "%s%s", com_gamedir, t2);
			unlink (temp);
		}
	}

	con_text = Hunk_AllocName (CON_TEXTSIZE, "context");
// 2000-01-05 Console scrolling fix by Maddes  start
//	Q_memset (con_text, ' ', CON_TEXTSIZE);
	Con_Clear_f();
// 2000-01-05 Console scrolling fix by Maddes  end
	con_linewidth = -1;
	Con_CheckResize ();

//	Con_Printf ("Console initialized.\n");	// 2000-01-05 Console scrolling fix by Maddes

//
// register our commands
//
// 2001-09-18 New cvar system by Maddes (Init)  start
/*
	con_notifytime = Cvar_Get ("con_notifytime", "3", CVAR_ORIGINAL);

// 2000-08-04 "Transparent" console background for software renderer by Norberto Alfredo Bensa/Maddes  start
// 2000-01-11 Transparent console by Radix  start
	con_alpha = Cvar_Get ("con_alpha", "1", CVAR_ARCHIVE);
	Cvar_SetRangecheck (con_alpha, Cvar_RangecheckFloat, 0, 1);
	Cvar_Set(con_alpha, con_alpha->string);	// do rangecheck
// 2000-01-11 Transparent console by Radix  end
// 2000-08-04 "Transparent" console background for software renderer by Norberto Alfredo Bensa/Maddes  end
*/
// 2001-09-18 New cvar system by Maddes (Init)  end

	Cmd_AddCommand ("toggleconsole", Con_ToggleConsole_f);
	Cmd_AddCommand ("messagemode", Con_MessageMode_f);
	Cmd_AddCommand ("messagemode2", Con_MessageMode2_f);
	Cmd_AddCommand ("clear", Con_Clear_f);

	con_initialized = true;
	Con_Printf ("Console initialized.\n");	// 2000-01-05 Console scrolling fix by Maddes
}


/*
===============
Con_Linefeed
===============
*/
void Con_Linefeed (void)
{
	con_x = 0;
// 2001-12-15 Avoid automatic console scrolling by Fett  start
	if (con_backscroll)
		con_backscroll++;
// 2001-12-15 Avoid automatic console scrolling by Fett  end
	con_current++;
	Q_memset (&con_text[((con_current-1)%con_totallines)*con_linewidth]
	, ' ', con_linewidth);	// 2000-01-05 Console scrolling fix by Maddes
}

/*
================
Con_Print

Handles cursor positioning, line wrapping, etc
All console printing must go through this in order to be logged to disk
If no console is visible, the notify window will pop up.
================
*/
void Con_Print (char *txt)
{
	int		y;
	int		c, l;
	static int	cr;
	int		mask;

//	con_backscroll = 0;	// 2001-12-15 Avoid automatic console scrolling by Fett

	if (txt[0] == 1)
	{
		mask = 128;		// go to colored text
		S_LocalSound ("misc/talk.wav");
	// play talk wav
		txt++;
	}
	else if (txt[0] == 2)
	{
		mask = 128;		// go to colored text
		txt++;
	}
	else
		mask = 0;


	while ( (c = *txt) )
	{
	// count word length
		for (l=0 ; l< con_linewidth ; l++)
			if ( txt[l] <= ' ')
				break;

	// word wrap
		if (l != con_linewidth && (con_x + l > con_linewidth) )
			con_x = 0;

		txt++;

		if (cr)
		{
			con_current--;
			cr = false;
		}


		if (!con_x)
		{
			Con_Linefeed ();
		// mark time for transparent overlay
// 2000-01-05 Console scrolling fix by Maddes  start
/*
			if (con_current >= 0)
				con_times[con_current % NUM_CON_TIMES] = realtime;
*/
			if (con_current > 0)
				con_times[(con_current-1) % NUM_CON_TIMES] = realtime;
// 2000-01-05 Console scrolling fix by Maddes  end
		}

		switch (c)
		{
		case '\n':
			con_x = 0;
			break;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?