2005-10-20 17:27:39 +02:00
|
|
|
// input layer (dispatch events to multiple handlers; record/playback events)
|
|
|
|
//
|
|
|
|
// Copyright (c) 2002 Jan Wassenberg
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// Contact info:
|
|
|
|
// Jan.Wassenberg@stud.uni-karlsruhe.de
|
|
|
|
// http://www.stud.uni-karlsruhe.de/~urkt/
|
2003-11-03 17:22:45 +01:00
|
|
|
|
2004-05-08 03:11:51 +02:00
|
|
|
#include "precompiled.h"
|
2003-11-03 17:22:45 +01:00
|
|
|
|
2004-06-24 16:05:39 +02:00
|
|
|
#include "lib.h"
|
2005-10-20 19:44:56 +02:00
|
|
|
#include "input.h"
|
2003-11-03 17:22:45 +01:00
|
|
|
|
2004-06-04 14:41:53 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2003-11-03 17:22:45 +01:00
|
|
|
|
2005-10-20 17:27:39 +02:00
|
|
|
const uint MAX_HANDLERS = 8;
|
|
|
|
static InHandler handler_stack[MAX_HANDLERS];
|
|
|
|
static uint handler_stack_top = 0;
|
2003-11-03 17:22:45 +01:00
|
|
|
|
2005-10-20 17:27:39 +02:00
|
|
|
void in_add_handler(InHandler handler)
|
2003-11-03 17:22:45 +01:00
|
|
|
{
|
2005-10-19 05:06:54 +02:00
|
|
|
debug_assert(handler);
|
|
|
|
|
|
|
|
if(handler_stack_top >= MAX_HANDLERS)
|
2004-06-24 16:05:39 +02:00
|
|
|
{
|
2005-10-19 08:29:55 +02:00
|
|
|
debug_warn("increase MAX_HANDLERS");
|
2005-10-20 17:27:39 +02:00
|
|
|
return;
|
2004-06-24 16:05:39 +02:00
|
|
|
}
|
2003-11-03 17:22:45 +01:00
|
|
|
|
|
|
|
handler_stack[handler_stack_top++] = handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-20 17:27:39 +02:00
|
|
|
// send event to each handler until one returns IN_HANDLED
|
|
|
|
static void dispatch_event(const SDL_Event* event)
|
2003-11-03 17:22:45 +01:00
|
|
|
{
|
2005-10-20 17:27:39 +02:00
|
|
|
for(int i = (int)handler_stack_top-1; i >= 0; i--)
|
2004-06-24 16:05:39 +02:00
|
|
|
{
|
2005-10-20 17:27:39 +02:00
|
|
|
debug_assert(handler_stack[i] && event);
|
|
|
|
InReaction ret = handler_stack[i](event);
|
2004-06-24 16:05:39 +02:00
|
|
|
// .. done, return
|
2005-10-20 19:44:56 +02:00
|
|
|
if(ret == IN_HANDLED)
|
2003-11-03 17:22:45 +01:00
|
|
|
return;
|
2004-06-24 16:05:39 +02:00
|
|
|
// .. next handler
|
2005-10-20 19:44:56 +02:00
|
|
|
else if(ret == IN_PASS)
|
2004-06-24 16:05:39 +02:00
|
|
|
continue;
|
|
|
|
// .. invalid return value
|
|
|
|
else
|
2005-10-19 08:29:55 +02:00
|
|
|
debug_warn("invalid handler return value");
|
2004-06-24 16:05:39 +02:00
|
|
|
}
|
2003-11-03 17:22:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-10-20 17:27:39 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2003-11-03 17:22:45 +01:00
|
|
|
|
|
|
|
static enum
|
|
|
|
{
|
2005-01-23 18:54:20 +01:00
|
|
|
INIT, // first call to in_record() or in_playback(): register cleanup routine
|
2003-11-03 17:22:45 +01:00
|
|
|
IDLE,
|
|
|
|
RECORD,
|
|
|
|
PLAYBACK
|
|
|
|
}
|
|
|
|
state = INIT;
|
|
|
|
|
|
|
|
static FILE* f;
|
|
|
|
|
2005-01-07 01:52:05 +01:00
|
|
|
u32 game_ticks;
|
2003-11-03 17:22:45 +01:00
|
|
|
|
|
|
|
static u32 time_adjust = 0;
|
|
|
|
static u32 next_event_time;
|
|
|
|
|
|
|
|
|
|
|
|
void in_stop()
|
|
|
|
{
|
|
|
|
if(f)
|
|
|
|
{
|
|
|
|
fclose(f);
|
|
|
|
f = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
state = IDLE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-11 23:23:55 +01:00
|
|
|
LibError in_record(const char* fn)
|
2003-11-03 17:22:45 +01:00
|
|
|
{
|
|
|
|
if(state == INIT)
|
|
|
|
atexit(in_stop);
|
|
|
|
|
|
|
|
in_stop();
|
|
|
|
|
|
|
|
f = fopen(fn, "wb");
|
|
|
|
if(!f)
|
2005-12-11 23:23:55 +01:00
|
|
|
return ERR_FILE_ACCESS;
|
2003-11-03 17:22:45 +01:00
|
|
|
|
|
|
|
fwrite(&game_ticks, sizeof(u32), 1, f);
|
|
|
|
|
|
|
|
state = RECORD;
|
|
|
|
|
2005-12-11 23:23:55 +01:00
|
|
|
return ERR_OK;
|
2003-11-03 17:22:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-12-11 23:23:55 +01:00
|
|
|
LibError in_playback(const char* fn)
|
2003-11-03 17:22:45 +01:00
|
|
|
{
|
|
|
|
if(state == INIT)
|
|
|
|
atexit(in_stop);
|
|
|
|
|
|
|
|
in_stop();
|
|
|
|
|
|
|
|
f = fopen(fn, "rb");
|
|
|
|
if(!f)
|
2005-12-11 23:23:55 +01:00
|
|
|
return ERR_FILE_NOT_FOUND;
|
2003-11-03 17:22:45 +01:00
|
|
|
|
|
|
|
u32 rec_start_time;
|
|
|
|
fread(&rec_start_time, sizeof(u32), 1, f);
|
|
|
|
time_adjust = game_ticks-rec_start_time;
|
|
|
|
|
|
|
|
fread(&next_event_time, sizeof(u32), 1, f);
|
|
|
|
next_event_time += time_adjust;
|
|
|
|
|
|
|
|
state = PLAYBACK;
|
|
|
|
|
2005-12-11 23:23:55 +01:00
|
|
|
return ERR_OK;
|
2003-11-03 17:22:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-08-15 01:34:37 +02:00
|
|
|
|
|
|
|
void in_dispatch_event(const SDL_Event* event)
|
|
|
|
{
|
|
|
|
if(state == RECORD)
|
|
|
|
{
|
|
|
|
fwrite(&game_ticks, sizeof(u32), 1, f);
|
|
|
|
fwrite(event, sizeof(SDL_Event), 1, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch_event(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void in_dispatch_recorded_events()
|
2003-11-03 17:22:45 +01:00
|
|
|
{
|
|
|
|
SDL_Event event;
|
|
|
|
|
|
|
|
while(state == PLAYBACK && next_event_time <= game_ticks)
|
|
|
|
{
|
|
|
|
fread(&event, sizeof(SDL_Event), 1, f);
|
|
|
|
|
2005-01-23 18:54:20 +01:00
|
|
|
// do this before dispatch_event(),
|
|
|
|
// in case a handler calls in_stop() (setting f to 0)
|
2003-11-03 17:22:45 +01:00
|
|
|
if(!fread(&next_event_time, sizeof(u32), 1, f))
|
|
|
|
{
|
|
|
|
in_stop();
|
|
|
|
exit(0x73c07d);
|
|
|
|
// TODO: 'disconnect'?
|
|
|
|
}
|
|
|
|
next_event_time += time_adjust;
|
|
|
|
|
2005-08-15 01:34:37 +02:00
|
|
|
in_dispatch_event(&event);
|
2003-11-03 17:22:45 +01:00
|
|
|
}
|
|
|
|
}
|