2014-06-01 17:55:23 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the DXX-Rebirth project <http://www.dxx-rebirth.com/>.
|
|
|
|
* It is copyright by its individual contributors, as recorded in the
|
|
|
|
* project's Git history. See COPYING.txt at the top level for license
|
|
|
|
* terms and a link to the Git history.
|
|
|
|
*/
|
2006-03-20 17:12:09 +00:00
|
|
|
/*
|
|
|
|
*
|
2008-04-06 20:23:28 +00:00
|
|
|
* Game console
|
2006-03-20 17:12:09 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-12-17 23:30:39 +00:00
|
|
|
#include <algorithm>
|
2006-03-20 17:12:09 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <string.h>
|
2008-04-06 20:23:28 +00:00
|
|
|
#include <time.h>
|
2013-06-30 02:22:56 +00:00
|
|
|
#include <SDL.h>
|
2011-01-14 21:56:00 +00:00
|
|
|
#include "window.h"
|
2006-03-20 17:12:09 +00:00
|
|
|
#include "console.h"
|
2008-04-06 20:23:28 +00:00
|
|
|
#include "args.h"
|
2006-03-20 17:12:09 +00:00
|
|
|
#include "gr.h"
|
2008-04-06 20:23:28 +00:00
|
|
|
#include "physfsx.h"
|
2006-03-20 17:12:09 +00:00
|
|
|
#include "gamefont.h"
|
2012-11-11 00:14:30 +00:00
|
|
|
#include "game.h"
|
2008-04-06 20:23:28 +00:00
|
|
|
#include "key.h"
|
|
|
|
#include "vers_id.h"
|
2010-12-10 23:18:17 +00:00
|
|
|
#include "timer.h"
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2014-07-05 03:32:07 +00:00
|
|
|
#include "dxxsconf.h"
|
|
|
|
#include "compiler-array.h"
|
|
|
|
|
2012-11-02 17:24:51 +00:00
|
|
|
static PHYSFS_file *gamelog_fp=NULL;
|
2014-07-05 03:32:07 +00:00
|
|
|
static array<console_buffer, CON_LINES_MAX> con_buffer;
|
2011-01-14 21:56:00 +00:00
|
|
|
static int con_state = CON_STATE_CLOSED, con_scroll_offset = 0, con_size = 0;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2013-12-17 23:30:39 +00:00
|
|
|
static void con_add_buffer_line(int priority, const char *buffer, size_t len)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2008-04-06 20:23:28 +00:00
|
|
|
/* shift con_buffer for one line */
|
2014-07-05 03:32:07 +00:00
|
|
|
std::move(std::next(con_buffer.begin()), con_buffer.end(), con_buffer.begin());
|
|
|
|
console_buffer &c = con_buffer.back();
|
|
|
|
c.priority=priority;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2013-12-17 23:30:39 +00:00
|
|
|
size_t copy = std::min(len, CON_LINE_LENGTH - 1);
|
2014-07-05 03:32:07 +00:00
|
|
|
memcpy(&c.line,buffer, copy);
|
|
|
|
c.line[copy] = 0;
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2013-12-07 01:01:27 +00:00
|
|
|
void (con_printf)(int priority, const char *fmt, ...)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
|
|
|
va_list arglist;
|
2008-04-06 20:23:28 +00:00
|
|
|
char buffer[CON_LINE_LENGTH];
|
|
|
|
|
|
|
|
if (priority <= ((int)GameArg.DbgVerbose))
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
|
|
|
va_start (arglist, fmt);
|
2013-12-22 17:18:44 +00:00
|
|
|
size_t len = vsnprintf (buffer, sizeof(buffer), fmt, arglist);
|
2006-03-20 17:12:09 +00:00
|
|
|
va_end (arglist);
|
2013-12-22 17:18:44 +00:00
|
|
|
con_puts(priority, buffer, len);
|
2013-12-07 01:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2013-12-22 17:18:44 +00:00
|
|
|
static void con_scrub_markup(char *buffer)
|
2013-12-07 01:01:27 +00:00
|
|
|
{
|
2013-12-22 17:18:44 +00:00
|
|
|
char *p1 = buffer, *p2 = p1;
|
|
|
|
do
|
|
|
|
switch (*p1)
|
|
|
|
{
|
|
|
|
case CC_COLOR:
|
|
|
|
case CC_LSPACING:
|
|
|
|
if (!*p1++)
|
|
|
|
break;
|
|
|
|
case CC_UNDERLINE:
|
|
|
|
p1++;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*p2++ = *p1++;
|
|
|
|
}
|
|
|
|
while (*p1);
|
|
|
|
*p2 = 0;
|
|
|
|
}
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2013-12-22 17:18:44 +00:00
|
|
|
static void con_print_file(const char *buffer)
|
|
|
|
{
|
|
|
|
/* Print output to stdout */
|
|
|
|
puts(buffer);
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2013-12-22 17:18:44 +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);
|
2008-04-21 23:03:18 +00:00
|
|
|
#ifdef _WIN32 // stupid hack to force DOS-style newlines
|
2013-12-07 00:47:27 +00:00
|
|
|
#define DXX_LF "\r\n"
|
|
|
|
#else
|
|
|
|
#define DXX_LF "\n"
|
2008-04-21 23:03:18 +00:00
|
|
|
#endif
|
2013-12-22 17:18:44 +00:00
|
|
|
PHYSFSX_printf(gamelog_fp,"%s" DXX_LF,buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void con_puts(int priority, char *buffer, size_t len)
|
|
|
|
{
|
|
|
|
if (priority <= ((int)GameArg.DbgVerbose))
|
|
|
|
{
|
|
|
|
con_add_buffer_line(priority, buffer, len);
|
|
|
|
con_scrub_markup(buffer);
|
|
|
|
/* Produce a sanitised version and send it to the console */
|
|
|
|
con_print_file(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void con_puts(int priority, const char *buffer, size_t len)
|
|
|
|
{
|
|
|
|
if (priority <= ((int)GameArg.DbgVerbose))
|
|
|
|
{
|
|
|
|
/* add given string to con_buffer */
|
|
|
|
con_add_buffer_line(priority, buffer, len);
|
|
|
|
con_print_file(buffer);
|
2008-04-06 20:23:28 +00:00
|
|
|
}
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2012-11-02 17:24:51 +00:00
|
|
|
static void con_draw(void)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2011-01-14 21:56:00 +00:00
|
|
|
int i = 0, y = 0, done = 0;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2011-01-14 21:56:00 +00:00
|
|
|
if (con_size <= 0)
|
2008-04-06 20:23:28 +00:00
|
|
|
return;
|
|
|
|
|
2008-11-17 23:28:59 +00:00
|
|
|
gr_set_current_canvas(NULL);
|
2008-04-06 20:23:28 +00:00
|
|
|
gr_set_curfont(GAME_FONT);
|
2011-01-14 21:56:00 +00:00
|
|
|
gr_setcolor(BM_XRGB(0,0,0));
|
2011-02-23 16:46:39 +00:00
|
|
|
gr_settransblend(7, GR_BLEND_NORMAL);
|
2008-04-06 20:23:28 +00:00
|
|
|
gr_rect(0,0,SWIDTH,(LINE_SPACING*(con_size))+FSPACY(1));
|
2011-02-23 16:46:39 +00:00
|
|
|
gr_settransblend(GR_FADE_OFF, GR_BLEND_NORMAL);
|
2008-04-06 20:23:28 +00:00
|
|
|
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
|
|
|
|
2008-04-06 20:23:28 +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:
|
2011-01-14 21:56:00 +00:00
|
|
|
gr_set_fontcolor(BM_XRGB(255,255,255),-1);
|
2008-04-06 20:23:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
gr_get_string_size(con_buffer[CON_LINES_MAX-1-i].line,&w,&h,&aw);
|
|
|
|
y-=h+FSPACY(1);
|
2012-06-24 20:52:25 +00:00
|
|
|
gr_string(FSPACX(1),y,con_buffer[CON_LINES_MAX-1-i].line);
|
2008-04-06 20:23:28 +00:00
|
|
|
i++;
|
2006-03-20 17:12:09 +00:00
|
|
|
|
2008-04-06 20:23:28 +00:00
|
|
|
if (y<=0 || CON_LINES_MAX-1-i <= 0 || i < 0)
|
|
|
|
done=1;
|
|
|
|
}
|
2011-01-14 21:56:00 +00:00
|
|
|
gr_setcolor(BM_XRGB(0,0,0));
|
2008-04-06 20:23:28 +00:00
|
|
|
gr_rect(0,0,SWIDTH,LINE_SPACING);
|
2011-01-14 21:56:00 +00:00
|
|
|
gr_set_fontcolor(BM_XRGB(255,255,255),-1);
|
2008-04-06 20:23:28 +00:00
|
|
|
gr_printf(FSPACX(1),FSPACY(1),"%s LOG", DESCENT_VERSION);
|
2012-06-24 20:53:52 +00:00
|
|
|
gr_string(SWIDTH-FSPACX(110),FSPACY(1),"PAGE-UP/DOWN TO SCROLL");
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2014-10-16 02:30:29 +00:00
|
|
|
static window_event_result con_handler(window *wind,const d_event &event, const unused_window_userdata_t *)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2011-01-14 21:56:00 +00:00
|
|
|
int key;
|
|
|
|
static fix64 last_scroll_time = 0;
|
|
|
|
|
2014-10-04 21:47:13 +00:00
|
|
|
switch (event.type)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2011-01-14 21:56:00 +00:00
|
|
|
case EVENT_WINDOW_ACTIVATED:
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVENT_WINDOW_DEACTIVATED:
|
|
|
|
con_size = 0;
|
|
|
|
con_state = CON_STATE_CLOSED;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case EVENT_KEY_COMMAND:
|
|
|
|
key = event_key_get(event);
|
|
|
|
switch (key)
|
|
|
|
{
|
2011-01-16 18:33:41 +00:00
|
|
|
case KEY_SHIFTED + KEY_ESC:
|
2011-01-14 21:56:00 +00:00
|
|
|
switch (con_state)
|
|
|
|
{
|
|
|
|
case CON_STATE_OPEN:
|
|
|
|
case CON_STATE_OPENING:
|
|
|
|
con_state = CON_STATE_CLOSING;
|
|
|
|
break;
|
|
|
|
case CON_STATE_CLOSED:
|
|
|
|
case CON_STATE_CLOSING:
|
|
|
|
con_state = CON_STATE_OPENING;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
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--;
|
|
|
|
break;
|
|
|
|
case KEY_PAGEDOWN:
|
|
|
|
con_scroll_offset-=CON_SCROLL_OFFSET;
|
|
|
|
if (con_scroll_offset<0)
|
|
|
|
con_scroll_offset=0;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2014-08-06 02:10:49 +00:00
|
|
|
return window_event_result::handled;
|
2011-01-14 21:56:00 +00:00
|
|
|
|
|
|
|
case EVENT_WINDOW_DRAW:
|
2011-01-22 12:30:20 +00:00
|
|
|
timer_delay2(50);
|
2011-01-14 21:56:00 +00:00
|
|
|
if (con_state == CON_STATE_OPENING)
|
|
|
|
{
|
|
|
|
if (con_size < CON_LINES_ONSCREEN && timer_query() >= last_scroll_time+(F1_0/30))
|
|
|
|
{
|
|
|
|
last_scroll_time = timer_query();
|
|
|
|
con_size++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (con_state == CON_STATE_CLOSING)
|
|
|
|
{
|
|
|
|
if (con_size > 0 && timer_query() >= last_scroll_time+(F1_0/30))
|
|
|
|
{
|
|
|
|
last_scroll_time = timer_query();
|
|
|
|
con_size--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (con_size >= CON_LINES_ONSCREEN)
|
|
|
|
con_state = CON_STATE_OPEN;
|
|
|
|
else if (con_size <= 0)
|
|
|
|
con_state = CON_STATE_CLOSED;
|
2014-08-06 02:10:49 +00:00
|
|
|
con_draw();
|
2011-01-14 21:56:00 +00:00
|
|
|
if (con_state == CON_STATE_CLOSED && wind)
|
2014-08-06 02:10:49 +00:00
|
|
|
{
|
2011-01-14 21:56:00 +00:00
|
|
|
window_close(wind);
|
2014-08-06 02:10:49 +00:00
|
|
|
return window_event_result::close;
|
|
|
|
}
|
2011-01-14 21:56:00 +00:00
|
|
|
break;
|
|
|
|
case EVENT_WINDOW_CLOSE:
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
2011-01-14 21:56:00 +00:00
|
|
|
|
2014-08-06 02:10:49 +00:00
|
|
|
return window_event_result::ignored;
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2011-01-14 21:56:00 +00:00
|
|
|
void con_showup(void)
|
|
|
|
{
|
|
|
|
window *wind;
|
|
|
|
|
|
|
|
game_flush_inputs();
|
|
|
|
con_state = CON_STATE_OPENING;
|
2013-12-01 05:21:47 +00:00
|
|
|
wind = window_create(&grd_curscreen->sc_canvas, 0, 0, SWIDTH, SHEIGHT, con_handler, unused_window_userdata);
|
2011-01-14 21:56:00 +00:00
|
|
|
|
|
|
|
if (!wind)
|
|
|
|
{
|
|
|
|
d_event event = { EVENT_WINDOW_CLOSE };
|
2014-10-04 21:47:13 +00:00
|
|
|
con_handler(NULL, event, NULL);
|
2011-01-14 21:56:00 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-02 17:24:51 +00:00
|
|
|
static void con_close(void)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2008-04-06 20:23:28 +00:00
|
|
|
if (gamelog_fp)
|
|
|
|
PHYSFS_close(gamelog_fp);
|
2011-01-14 21:56:00 +00:00
|
|
|
|
2008-04-27 05:06:30 +00:00
|
|
|
gamelog_fp = NULL;
|
2006-03-20 17:12:09 +00:00
|
|
|
}
|
|
|
|
|
2008-04-06 20:23:28 +00:00
|
|
|
void con_init(void)
|
2006-03-20 17:12:09 +00:00
|
|
|
{
|
2014-07-05 03:32:07 +00:00
|
|
|
con_buffer = {};
|
2012-01-09 14:21:09 +00:00
|
|
|
if (GameArg.DbgSafelog)
|
2010-11-26 12:15:56 +00:00
|
|
|
gamelog_fp = PHYSFS_openWrite("gamelog.txt");
|
|
|
|
else
|
|
|
|
gamelog_fp = PHYSFSX_openWriteBuffered("gamelog.txt");
|
2008-04-06 20:23:28 +00:00
|
|
|
atexit(con_close);
|
2008-10-28 17:58:54 +00:00
|
|
|
}
|
2011-01-14 21:56:00 +00:00
|
|
|
|