timer: improved timer_add_client - no longer has fixed-size storage. btw, name will change here - not happy with SUM_TIMER.

GUI/input code: moved g_active, g_keys etc. into ps/Globals; it has its
own input handler as well

all users of fopen(): add file_make_full_native_path to get correct path
even though current dir = system/

This was SVN commit r2954.
This commit is contained in:
janwas 2005-10-16 23:16:08 +00:00
parent 62c5e311f2
commit 953d2621dd
23 changed files with 163 additions and 119 deletions

View File

@ -77,7 +77,7 @@ void CTextureEntry::LoadTexture()
}
}
static TimerClient* tc_mipmap_basecolor = timer_add_client("mipmap BuildBaseColor");
TIMER_ADD_CLIENT(tc_mipmap_basecolor);
////////////////////////////////////////////////////////////////////////////////////////////////////////////
// BuildBaseColor: calculate the root colour of the texture, used for coloring minimap, and store

View File

@ -38,6 +38,7 @@ gee@pyro.nu
#include "scripting/ScriptingHost.h"
#include "Hotkey.h"
#include "ps/Globals.h"
// namespaces used
using namespace std;
@ -53,9 +54,6 @@ JSClass GUIClass = {
JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub,
};
// Globals used.
extern int g_xres, g_yres;
extern bool g_keys[];
//-------------------------------------------------------------------
// called from main loop when (input) events are received.

View File

@ -16,12 +16,10 @@ gee@pyro.nu
#include "lib/res/graphics/unifont.h"
#include "ps/Hotkey.h"
#include "ps/CLogger.h"
#define LOG_CATEGORY "gui"
#include "ps/Globals.h"
extern bool g_keys[];
extern bool g_mouse_buttons[];
#define LOG_CATEGORY "gui"
using namespace std;

View File

@ -16,11 +16,10 @@
#include "Terrain.h"
#include "Profile.h"
#include "LOSManager.h"
#include "ps/Globals.h"
extern bool g_mouse_buttons[5];
extern int g_mouse_x, g_mouse_y;
extern float g_MaxZoomHeight, g_YMinOffset;
bool HasClicked=false;

View File

@ -24,10 +24,12 @@
#include "debug.h"
#include "debug_stl.h"
#include "posix.h"
// some functions here are called from within mmgr; disable its hooks
// so that our allocations don't cause infinite recursion.
#include "nommgr.h"
// some functions here are called from within mmgr; disable its hooks
// so that our allocations don't cause infinite recursion.
#include "self_test.h"
// file_make_full_native_path is needed when bundling game data files.
#include "lib/res/file/file.h"
// needed when writing crashlog
static const size_t LOG_CHARS = 16384;
@ -84,7 +86,7 @@ static void cat_atow(FILE* out, const char* in_filename)
while(!feof(in))
{
size_t bytes_read = fread(buf, 1,buf_size, in);
size_t bytes_read = fread(buf, 1, buf_size, in);
if(!bytes_read)
break;
buf[bytes_read] = 0; // 0-terminate
@ -109,15 +111,15 @@ int debug_write_crashlog(const wchar_t* text)
fwprintf(f, L"%ls\n", text);
WRITE_DIVIDER
// for user convenience, bundle all logs into this file:
char N_path[PATH_MAX];
fwprintf(f, L"System info:\n\n");
cat_atow(f, "../logs/system_info.txt");
(void)file_make_full_native_path("../logs/system_info.txt", N_path);
cat_atow(f, N_path);
WRITE_DIVIDER
fwprintf(f, L"Main log:\n\n");
cat_atow(f, "../logs/mainlog.html");
(void)file_make_full_native_path("../logs/mainlog.html", N_path);
cat_atow(f, N_path);
WRITE_DIVIDER
fwprintf(f, L"Last known activity:\n\n %ls\n", debug_log);

View File

@ -26,7 +26,6 @@
#include "adts.h"
#include "sysdep/sysdep.h"
#include "byte_order.h"
#include "timer.h"
#include "lib/allocators.h"
#include <vector>
@ -431,8 +430,6 @@ static bool dirent_less(const DirEnt* d1, const DirEnt* d2)
}
static TimerClient* tc_file_enum_malloc = timer_add_client("file_enum_malloc");
// call <cb> for each file and subdirectory in <dir> (alphabetical order),
// passing the entry name (not full path!), stat info, and <user>.
//
@ -476,9 +473,7 @@ int file_enum(const char* P_path, const FileCB cb, const uintptr_t user)
stat_err = ret;
const size_t size = sizeof(DirEnt)+strlen(ent.name)+1;
DirEnt* p_ent;
{SUM_TIMER(tc_file_enum_malloc);
p_ent = (DirEnt*)malloc(size);}
DirEnt* p_ent = (DirEnt*)malloc(size);
if(!p_ent)
{
stat_err = ERR_NO_MEM;

View File

@ -279,7 +279,9 @@ static void file_listing_add(const char* v_fn)
if(!file_list)
{
file_list = fopen("../logs/filelist.txt", "w");
char N_path[PATH_MAX];
(void)file_make_full_native_path("../logs/filelist.txt", N_path);
file_list = fopen(N_path, "w");
if(!file_list)
return;
}

View File

@ -781,8 +781,8 @@ static void dump()
}
static TimerClient* tc_zip_inflate = timer_add_client("zip inflate");
static TimerClient* tc_zip_memcpy = timer_add_client("zip memcpy");
TIMER_ADD_CLIENT(tc_zip_inflate);
TIMER_ADD_CLIENT(tc_zip_memcpy);
// unzip into output buffer. returns bytes written
// (may be 0, if not enough data is passed in), or < 0 on error.
@ -822,7 +822,7 @@ double t0 = get_time();
else
{
SUM_TIMER(tc_zip_memcpy);
memcpy(zs->next_out, zs->next_in, zs->avail_in);
memcpy2(zs->next_out, zs->next_in, zs->avail_in);
uInt size = MIN(zs->avail_in, zs->avail_out);
zs->avail_out -= size;
zs->avail_in -= size; // => = 0

View File

@ -654,7 +654,7 @@ int tex_free(Tex* t)
//-----------------------------------------------------------------------------
static TimerClient* tc_transform = timer_add_client("tex_transform");
TIMER_ADD_CLIENT(tc_transform);
// change <t>'s pixel format by flipping the state of all TEX_* flags
// that are set in transforms.

View File

@ -106,7 +106,7 @@ static void remove_alloc(void* raw_p)
{
size_t num_removed = ptr_to_h.erase(raw_p);
if(num_removed != 1)
debug_assert(num_removed == 1 && "remove_alloc: not in map");
debug_warn("remove_alloc: not in map");
}

View File

@ -242,40 +242,30 @@ void calc_fps()
// this supplements in-game profiling by providing low-overhead,
// high resolution time accounting.
struct TimerClient
{
double sum; // total bill [s]
// only store a pointer for efficiency.
const char* name;
};
// use static storage to ensure clients can be added at any time,
// especially before the heap has been initialized.
// that means there's a fixed limit, but that's not a problem since
// these timers are added manually for performance measuring.
static uint num_clients;
static const uint MAX_CLIENTS = 16;
static TimerClient clients[MAX_CLIENTS];
static TimerClient* clients;
// allocate a new TimerClient whose total (added to by timer_bill_client)
// will be displayed by timer_display_client_totals.
// notes:
// - uses static data; there is a fixed limit. rationale: see clients[].
// - may be called at any time;
// - always succeeds (there's no fixed limit);
// - free() is not needed nor possible.
// - name must remain valid until exit; passing a string literal is safest.
TimerClient* timer_add_client(const char* name)
// - description must remain valid until exit; a string literal is safest.
TimerClient* timer_add_client(TimerClient* tc, const char* description)
{
if(num_clients == MAX_CLIENTS)
{
debug_warn("timer: increase MAX_CLIENTS");
return 0;
}
TimerClient* tc = &clients[num_clients++];
tc->name = name;
// note: tc->sum is already 0 (static data)
tc->sum = 0.0;
tc->description = description;
// insert at front of list
tc->next = clients;
clients = tc;
num_clients++;
return tc;
}
@ -293,9 +283,16 @@ void timer_display_client_totals()
{
debug_printf("TIMER TOTALS (%d clients)\n", num_clients);
debug_printf("-----------------------------------------------------\n");
for(uint i = 0; i < num_clients; i++)
while(clients)
{
const double sum = clients[i].sum;
// (make sure list and count are consistent)
debug_assert(num_clients != 0);
TimerClient* tc = clients;
clients = tc->next;
num_clients--;
const double sum = tc->sum;
// determine scale factor for pretty display
double scale = 1e6;
const char* unit = "us";
@ -304,7 +301,8 @@ void timer_display_client_totals()
else if(sum > 1e-3)
scale = 1e3, unit = "ms";
debug_printf(" %s: %g %s\n", clients[i].name, sum*scale, unit);
debug_printf(" %s: %g %s\n", tc->description, sum*scale, unit);
}
debug_printf("-----------------------------------------------------\n");
}

View File

@ -45,16 +45,34 @@ extern void calc_fps(void);
// cumulative timer API
//
struct TimerClient; // opaque
// this is to be considered opaque - do not access its fields!
// note: must be defined here because clients instantiate them;
// fields cannot be made private due to C compatibility requirement.
struct TimerClient
{
double sum; // total bill [s]
// only store a pointer for efficiency.
const char* description;
TimerClient* next;
int dummy;
};
// allocate a new TimerClient whose total (added to by timer_bill_client)
// will be displayed by timer_display_client_totals.
// notes:
// - uses static data; there is a fixed limit. rationale: see clients[].
// - may be called at any time;
// - always succeeds (there's no fixed limit);
// - free() is not needed nor possible.
// - name must remain valid until exit; passing a string literal is safest.
extern TimerClient* timer_add_client(const char* name);
// - description must remain valid until exit; a string literal is safest.
extern TimerClient* timer_add_client(TimerClient* tc, const char* description);
#define TIMER_ADD_CLIENT(id)\
static TimerClient UID__;\
TimerClient* id = timer_add_client(&UID__, #id);
// add <dt> [s] to the client's total.
extern void timer_bill_client(TimerClient* tc, double dt);

View File

@ -43,60 +43,15 @@ that of Atlas depending on commandline parameters.
void kill_mainloop();
//
// main app message handler
//
bool g_active = true;
bool g_keys[SDLK_LAST];
int g_mouse_x = 50, g_mouse_y = 50;
// left, right, middle, wheel up, wheel down
// (order is given by SDL_BUTTON_* constants).
bool g_mouse_buttons[5];
static int MainInputHandler(const SDL_Event* ev)
{
int c;
switch(ev->type)
{
case SDL_QUIT:
kill_mainloop();
break;
case SDL_ACTIVEEVENT:
g_active = (ev->active.gain != 0);
break;
case SDL_MOUSEMOTION:
g_mouse_x = ev->motion.x;
g_mouse_y = ev->motion.y;
break;
case SDL_KEYDOWN:
case SDL_KEYUP:
c = ev->key.keysym.sym;
if(c < ARRAY_SIZE(g_keys))
g_keys[c] = (ev->type == SDL_KEYDOWN);
else
{
// don't complain: this happens when the hotkey system
// spoofs keys (it assigns values starting from SDLK_LAST)
//debug_warn("MainInputHandler: invalid key");
}
break;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
c = ev->button.button;
if(c < ARRAY_SIZE(g_mouse_buttons))
g_mouse_buttons[c] = (ev->type == SDL_MOUSEBUTTONDOWN);
else
debug_warn("MainInputHandler: invalid mouse button");
break;
case SDL_HOTKEYDOWN:
switch(ev->user.code)
{

View File

@ -17,8 +17,8 @@
#include "lib/res/file/vfs.h"
#include "Interact.h"
#include "ps/Globals.h"
extern bool g_keys[];
CConsole* g_Console = 0;

View File

@ -351,7 +351,7 @@ bool CConfigDB::WriteFile(EConfigNamespace ns, bool useVFS, CStr path)
}
filepath=realpath;
}
file_make_native_path(filepath, nativepath);
file_make_full_native_path(filepath, nativepath);
if ((fp = fopen(nativepath, "w")) == NULL)
{
LOG(ERROR, LOG_CATEGORY, "CConfigDB::WriteFile(): fopen for path \"%s\" failed (error: %d)", filepath, errno);

View File

@ -42,7 +42,9 @@ void CFilePacker::Write(const char* filename)
throw CFileWriteError();
#else
FILE* fp=fopen(filename,"wb");
char N_path[PATH_MAX];
(void)file_make_full_native_path(filename, N_path);
FILE* fp=fopen(N_path,"wb");
if (!fp) {
throw CFileOpenError();
}

View File

@ -94,7 +94,9 @@ void CFileUnpacker::Read(const char* filename,const char magicstr[4])
}
else
{
FILE* fp=fopen(filename,"rb");
char N_path[PATH_MAX];
(void)file_make_full_native_path(filename, N_path);
FILE* fp=fopen(N_path,"rb");
if (!fp)
throw CFileOpenError();

View File

@ -27,6 +27,7 @@
#include "ps/i18n.h"
#include "ps/Overlay.h"
#include "ps/StringConvert.h"
#include "ps/Globals.h"
#include "graphics/MapReader.h"
#include "graphics/Terrain.h"
@ -523,6 +524,8 @@ static void InitInput()
in_add_handler(profilehandler);
in_add_handler(hotkeyInputHandler);
in_add_handler(GlobalsInputHandler);
}

60
source/ps/Globals.cpp Normal file
View File

@ -0,0 +1,60 @@
#include "precompiled.h"
#include "lib/input.h"
#include "Globals.h"
bool g_active = true;
bool g_keys[SDLK_LAST];
int g_mouse_x = 50, g_mouse_y = 50;
// left, right, middle, wheel up, wheel down
// (order is given by SDL_BUTTON_* constants).
bool g_mouse_buttons[5];
// updates the state of the above; never swallows messages.
int GlobalsInputHandler(const SDL_Event* ev)
{
int c;
switch(ev->type)
{
case SDL_ACTIVEEVENT:
g_active = (ev->active.gain != 0);
return EV_PASS;
case SDL_MOUSEMOTION:
g_mouse_x = ev->motion.x;
g_mouse_y = ev->motion.y;
return EV_PASS;
case SDL_KEYDOWN:
case SDL_KEYUP:
c = ev->key.keysym.sym;
if(c < ARRAY_SIZE(g_keys))
g_keys[c] = (ev->type == SDL_KEYDOWN);
else
{
// don't complain: this happens when the hotkey system
// spoofs keys (it assigns values starting from SDLK_LAST)
//debug_warn("MainInputHandler: invalid key");
}
return EV_PASS;
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
c = ev->button.button;
if(c < ARRAY_SIZE(g_mouse_buttons))
g_mouse_buttons[c] = (ev->type == SDL_MOUSEBUTTONDOWN);
else
debug_warn("MainInputHandler: invalid mouse button");
return EV_PASS;
default:
return EV_PASS;
}
UNREACHABLE;
}

10
source/ps/Globals.h Normal file
View File

@ -0,0 +1,10 @@
extern bool g_active;
extern bool g_keys[SDLK_LAST];
extern int g_mouse_x, g_mouse_y;
// { left, right, middle, wheel up, wheel down }
// (order is given by SDL_BUTTON_* constants).
extern bool g_mouse_buttons[5];
extern int GlobalsInputHandler(const SDL_Event* ev);

View File

@ -6,11 +6,11 @@
#include "CLogger.h"
#include "CConsole.h"
#include "CStr.h"
#include "ps/Globals.h"
extern CConsole* g_Console;
extern bool g_keys[];
extern bool g_mouse_buttons[];
bool unified[5];
static bool unified[5];
/* SDL-type */

View File

@ -6,6 +6,7 @@
#include <stdarg.h>
#include <time.h>
#include "lib/timer.h"
#include "lib/res/file/file.h"
CNetLog g_NetLog;
@ -35,16 +36,15 @@ void CNetLog::Initialize()
if (m_Initialized)
return;
char filename[256];
snprintf(filename, sizeof(filename), "../logs/net_log.txt");
filename[sizeof(filename)-1]=0;
Open(filename);
char N_path[PATH_MAX];
(void)file_make_full_native_path("../logs/net_log.txt", N_path);
Open(N_path);
m_Initialized=true;
}
void CNetLog::Open(const char *filename)
void CNetLog::Open(const char *N_filename)
{
m_pFile=fopen(filename, "a");
m_pFile=fopen(N_filename, "a");
if (m_pFile)
{
time_t t = time(NULL);

View File

@ -43,7 +43,9 @@ void WriteSystemInfo()
struct utsname un;
uname(&un);
FILE* f = fopen("../logs/system_info.txt", "w");
char N_path[PATH_MAX];
(void)file_make_full_native_path("../logs/system_info.txt", N_path);
FILE* f = fopen(N_path, "w");
if(!f)
return;