PS. The Linux version isn’t working yet - and I need some help from
you Linux/SDL-gurus. Check the Formido’s page for details.
As for the crash in the options, that’s easily fixed. The problem is
that you allocate a “buffer” of one character, and then use strcpy()
to copy a string into it. Obviously, this overflows the buffer, and
I don’t know why Windows is letting you get away with it.
Anyway, I rewrote the code (menu.cpp:148) to:
if(key != -1) {
// Get the key name from SDL
char *buf;
buf = strdup(SDL_GetKeyName((SDLKey)key));
// Convert it to upper case
for(unsigned int c=0; c<strlen(buf); c++) {
buf[c] = toupper(buf[c]);
}
return buf;
}
Sadly, I don’t think this is much better than the original. The original
code was indeed writing into a 1-byte ‘buffer’, which it probably isn’t
allowed to write to anyway. (AFAIK, stating “” in C gets you a constant
string literal, and thus this shouldn’t be written into. It might not
even be writable memory at the time of execution.) But in avoiding this
problem., this new code contains a memory leak, in that it allocates
memory with that strdup call each time and never frees it. At a couple
of bytes a time, this is unlikely to be a big problem, but it’s still
Wrong! 
Assuming this application is single-threaded, the best solution is
probably to allocate a static char buffer inside the function, copy the
text into that, and return its address from the function. Make the
buffer a nice safe 128 characters long or so, and use strncpy (or is
that _strncpy?) to ensure that you never overflow that buffer even so.
If you’re multithreading, you don’t really want to use some sort of
static buffer; instead, add relevant ‘free’ calls after each call to
key_name() and make sure that it’s always returning a freeable pointer
(ie. strdup the constants too).
Or lastly, you could just set up your own array of keynames, populating
them at the start of the program by calling SDL_GetKeyName for
everything up to SDLK_LAST, changing them to upper case yourself, and
returning pointers to them instead.From: sdl@lists.rapacity.dyndns.org (Michael Alger)
Sent: Friday, February 07, 2003 7:45 PM
Subject: Re: [SDL] [ANN] Formido - a shooting game
On Fri, Feb 07, 2003 at 04:04:16PM +0200, Mika Halttunen wrote:
–
Kylotan
http://pages.eidosnet.co.uk/kylotan