Store joystick axis text in one allocation

This commit is contained in:
Kp 2013-11-09 23:17:28 +00:00
parent 25c731cc2c
commit 5354d05fbf
3 changed files with 43 additions and 9 deletions

View file

@ -141,7 +141,7 @@ void joy_init()
}
memset(&Joystick,0,sizeof(Joystick));
memset(joyaxis_text, 0, JOY_MAX_AXES * sizeof(char *));
joyaxis_text.clear();
memset(joybutton_text, 0, JOY_MAX_BUTTONS * sizeof(char *));
n = SDL_NumJoysticks();
@ -180,10 +180,10 @@ void joy_init()
con_printf(CON_NORMAL, "sdl-joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons);
con_printf(CON_NORMAL, "sdl-joystick: %d hats\n", SDL_Joysticks[num_joysticks].n_hats);
joyaxis_text.resize(joyaxis_text.size() + SDL_Joysticks[num_joysticks].n_axes);
for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++)
{
sprintf(temp, "J%d A%d", i + 1, j + 1);
joyaxis_text[Joystick.n_axes] = d_strdup(temp);
snprintf(&joyaxis_text[Joystick.n_axes][0], sizeof(joyaxis_text[Joystick.n_axes]), "J%d A%d", i + 1, j + 1);
SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++;
}
for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++)
@ -220,8 +220,7 @@ void joy_close()
{
SDL_JoystickClose(SDL_Joysticks[num_joysticks].handle);
while (Joystick.n_axes--)
d_free(joyaxis_text[Joystick.n_axes]);
joyaxis_text.clear();
while (Joystick.n_buttons--)
d_free(joybutton_text[Joystick.n_buttons]);
}

View file

@ -26,8 +26,13 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
#include "key.h"
#include "joy.h"
#include "mouse.h"
#include "dxxsconf.h"
#ifdef __cplusplus
#include <vector>
#define DXX_WANT_ARRAY
#include "compiler.h"
extern "C" {
#endif
@ -75,11 +80,41 @@ extern void kc_set_controls();
extern void reset_cruise(void);
extern char *joybutton_text[JOY_MAX_BUTTONS];
extern char *joyaxis_text[JOY_MAX_AXES];
extern fix Cruise_speed;
#ifdef __cplusplus
}
template <std::size_t N>
struct joystick_text_length
{
enum { value = ((N >= 10) ? (joystick_text_length<N / 10>::value + 1) : 1) };
};
template <>
struct joystick_text_length<0>
{
enum { value = 1 };
};
template <std::size_t N>
class joystick_text_t
{
typedef std::vector<array<char, N> > vector_type;
typedef typename vector_type::size_type size_type;
typedef typename vector_type::reference reference;
vector_type text;
public:
void clear() { text.clear(); }
size_type size() const { return text.size(); }
void resize(size_type s) { text.resize(s); }
reference operator[](size_type s) { return text.at(s); }
};
class joyaxis_text_t : public joystick_text_t<sizeof("J A") + joystick_text_length<MAX_JOYSTICKS>::value + joystick_text_length<MAX_AXES_PER_JOYSTICK>::value>
{
};
extern joyaxis_text_t joyaxis_text;
#endif
#endif /* _KCONFIG_H */

View file

@ -70,7 +70,7 @@ static const sbyte fades[64] = { 1,1,1,2,2,3,4,4,5,6,8,9,10,12,13,15,16,17,19,20
static const char invert_text[2][2] = { "N", "Y" };
char *joybutton_text[JOY_MAX_BUTTONS];
char *joyaxis_text[JOY_MAX_AXES];
joyaxis_text_t joyaxis_text;
static const char mouseaxis_text[][8] = { "L/R", "F/B", "WHEEL" };
static const char mousebutton_text[][8] = { "LEFT", "RIGHT", "MID", "M4", "M5", "M6", "M7", "M8", "M9", "M10","M11","M12","M13","M14","M15","M16" };
@ -589,8 +589,8 @@ static const char *get_item_text(const kc_item *item, char (&buf)[10])
}
break;
case BT_JOY_AXIS:
if (joyaxis_text[item->value])
return joyaxis_text[item->value];
if (joyaxis_text.size() > item->value)
return &joyaxis_text[item->value][0];
else
{
snprintf(buf, sizeof(buf), "AXIS%2d", item->value + 1);