dxx-rebirth/main/console.c

221 lines
4.6 KiB
C
Raw Normal View History

2006-03-20 17:12:09 +00:00
/*
*
* Game console
2006-03-20 17:12:09 +00:00
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <SDL/SDL.h>
2006-03-20 17:12:09 +00:00
#include "console.h"
#include "args.h"
2006-03-20 17:12:09 +00:00
#include "gr.h"
#include "physfsx.h"
2006-03-20 17:12:09 +00:00
#include "gamefont.h"
#include "key.h"
#include "vers_id.h"
#include "timer.h"
2006-03-20 17:12:09 +00:00
PHYSFS_file *gamelog_fp=NULL;
struct console_buffer con_buffer[CON_LINES_MAX];
int con_render=0;
static int con_scroll_offset=0;
2006-03-20 17:12:09 +00:00
void con_add_buffer_line(int priority, char *buffer)
2006-03-20 17:12:09 +00:00
{
int i=0;
2006-03-20 17:12:09 +00:00
/* shift con_buffer for one line */
for (i=1; i<CON_LINES_MAX; i++)
{
con_buffer[i-1].priority=con_buffer[i].priority;
memcpy(&con_buffer[i-1].line,&con_buffer[i].line,CON_LINE_LENGTH);
}
memset(con_buffer[CON_LINES_MAX-1].line,'\0',sizeof(CON_LINE_LENGTH));
con_buffer[CON_LINES_MAX-1].priority=priority;
2006-03-20 17:12:09 +00:00
memcpy(&con_buffer[CON_LINES_MAX-1].line,buffer,CON_LINE_LENGTH);
for (i=0; i<CON_LINE_LENGTH; i++)
{
con_buffer[CON_LINES_MAX-1].line[i]=buffer[i];
if (buffer[strlen(buffer)-1] == '\n')
con_buffer[CON_LINES_MAX-1].line[strlen(buffer)-1]='\0';
}
2006-03-20 17:12:09 +00:00
}
void con_printf(int priority, char *fmt, ...)
{
va_list arglist;
char buffer[CON_LINE_LENGTH];
memset(buffer,'\0',CON_LINE_LENGTH);
2006-03-20 17:12:09 +00:00
if (priority <= ((int)GameArg.DbgVerbose))
2006-03-20 17:12:09 +00:00
{
char *p1, *p2;
2006-03-20 17:12:09 +00:00
va_start (arglist, fmt);
vsprintf (buffer, fmt, arglist);
va_end (arglist);
/* Produce a sanitised version and send it to the console */
p1 = p2 = buffer;
do
switch (*p1)
{
2006-03-20 17:12:09 +00:00
case CC_COLOR:
case CC_LSPACING:
p1++;
case CC_UNDERLINE:
p1++;
break;
default:
*p2++ = *p1++;
}
while (*p1);
*p2 = 0;
2006-03-20 17:12:09 +00:00
/* add given string to con_buffer */
con_add_buffer_line(priority, buffer);
2006-03-20 17:12:09 +00:00
/* Print output to stdout */
printf("%s",buffer);
2006-03-20 17:12:09 +00:00
/* Print output to gamelog.txt */
if (gamelog_fp)
{
struct tm *lt;
time_t t;
t=time(NULL);
lt=localtime(&t);
PHYSFSX_printf(gamelog_fp,"%02i:%02i:%02i ",lt->tm_hour,lt->tm_min,lt->tm_sec);
#ifdef _WIN32 // stupid hack to force DOS-style newlines
if (buffer[strlen(buffer)-1] == '\n' && strlen(buffer) <= CON_LINE_LENGTH)
{
buffer[strlen(buffer)-1]='\r';
buffer[strlen(buffer)]='\n';
}
#endif
PHYSFSX_printf(gamelog_fp,"%s",buffer);
}
}
2006-03-20 17:12:09 +00:00
}
void con_show(void)
2006-03-20 17:12:09 +00:00
{
int i=0, y;
static int con_size=0;
static fix64 last_scroll_time = 0;
int done=0;
2006-03-20 17:12:09 +00:00
if (con_render)
2006-03-20 17:12:09 +00:00
{
if (con_size < CON_LINES_ONSCREEN && timer_query() >= last_scroll_time+(F1_0/30))
{
last_scroll_time = timer_query();
con_size++;
}
}
else
2006-03-20 17:12:09 +00:00
{
if (con_size > 0 && timer_query() >= last_scroll_time+(F1_0/30))
{
last_scroll_time = timer_query();
con_size--;
}
2006-03-20 17:12:09 +00:00
}
if (!con_size)
return;
gr_set_current_canvas(NULL);
gr_set_curfont(GAME_FONT);
gr_setcolor(0);
Gr_scanline_darkening_level = 1*7;
gr_rect(0,0,SWIDTH,(LINE_SPACING*(con_size))+FSPACY(1));
Gr_scanline_darkening_level = GR_FADE_LEVELS;
y=FSPACY(1)+(LINE_SPACING*con_size);
i+=con_scroll_offset;
while (!done)
{
int w,h,aw;
2006-03-20 17:12:09 +00:00
switch (con_buffer[CON_LINES_MAX-1-i].priority)
{
case CON_CRITICAL:
gr_set_fontcolor(BM_XRGB(28,0,0),-1);
break;
case CON_URGENT:
gr_set_fontcolor(BM_XRGB(54,54,0),-1);
break;
case CON_DEBUG:
case CON_VERBOSE:
gr_set_fontcolor(BM_XRGB(14,14,14),-1);
break;
case CON_HUD:
gr_set_fontcolor(BM_XRGB(0,28,0),-1);
break;
default:
gr_set_fontcolor(255,-1);
break;
}
gr_get_string_size(con_buffer[CON_LINES_MAX-1-i].line,&w,&h,&aw);
y-=h+FSPACY(1);
gr_printf(FSPACX(1),y,"%s",con_buffer[CON_LINES_MAX-1-i].line);
i++;
2006-03-20 17:12:09 +00:00
if (y<=0 || CON_LINES_MAX-1-i <= 0 || i < 0)
done=1;
}
gr_setcolor(0);
gr_rect(0,0,SWIDTH,LINE_SPACING);
gr_set_fontcolor(255,-1);
gr_printf(FSPACX(1),FSPACY(1),"%s LOG", DESCENT_VERSION);
gr_printf(SWIDTH-FSPACX(110),FSPACY(1),"PAGE-UP/DOWN TO SCROLL");
2006-03-20 17:12:09 +00:00
}
int con_events(int key)
2006-03-20 17:12:09 +00:00
{
switch (key)
2006-03-20 17:12:09 +00:00
{
case KEY_PAGEUP:
con_scroll_offset+=CON_SCROLL_OFFSET;
if (con_scroll_offset >= CON_LINES_MAX-1)
con_scroll_offset = CON_LINES_MAX-1;
while (con_buffer[CON_LINES_MAX-1-con_scroll_offset].line[0]=='\0')
con_scroll_offset--;
return 1;
case KEY_PAGEDOWN:
con_scroll_offset-=CON_SCROLL_OFFSET;
if (con_scroll_offset<0)
con_scroll_offset=0;
return 1;
case KEY_SHIFTED + KEY_ESC:
con_render=!con_render;
return 1;
2006-03-20 17:12:09 +00:00
}
return 0;
2006-03-20 17:12:09 +00:00
}
void con_close(void)
2006-03-20 17:12:09 +00:00
{
if (gamelog_fp)
PHYSFS_close(gamelog_fp);
gamelog_fp = NULL;
2006-03-20 17:12:09 +00:00
}
void con_init(void)
2006-03-20 17:12:09 +00:00
{
memset(con_buffer,0,sizeof(con_buffer));
if (GameArg.DbgVerbose >= CON_VERBOSE)
gamelog_fp = PHYSFS_openWrite("gamelog.txt");
else
gamelog_fp = PHYSFSX_openWriteBuffered("gamelog.txt");
atexit(con_close);
}