1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2024-11-07 22:49:54 +00:00

fixed some more crashes related to SDL2 (e.g., right-click in shmup)

This commit is contained in:
Zeno Rogue 2024-03-27 16:28:37 +01:00
parent dea9fea67f
commit 6abb82174e

View File

@ -11,13 +11,14 @@ namespace hr {
EX namespace multi { EX namespace multi {
#if HDR #if HDR
static constexpr int SCANCODES = 512;
static constexpr int MAXJOY = 8; static constexpr int MAXJOY = 8;
static constexpr int MAXBUTTON = 64; static constexpr int MAXBUTTON = 64;
static constexpr int MAXAXE = 16; static constexpr int MAXAXE = 16;
static constexpr int MAXHAT = 4; static constexpr int MAXHAT = 4;
struct config { struct config {
char keyaction[512]; char keyaction[SCANCODES];
char joyaction[MAXJOY][MAXBUTTON]; char joyaction[MAXJOY][MAXBUTTON];
char axeaction[MAXJOY][MAXAXE]; char axeaction[MAXJOY][MAXAXE];
char hataction[MAXJOY][MAXHAT][4]; char hataction[MAXJOY][MAXHAT][4];
@ -171,7 +172,7 @@ int* dzconfigs[24];
string listkeys(config& scfg, int id) { string listkeys(config& scfg, int id) {
#if CAP_SDL #if CAP_SDL
string lk = ""; string lk = "";
for(int i=0; i<512; i++) for(int i=0; i<SCANCODES; i++)
if(scfg.keyaction[i] == id) if(scfg.keyaction[i] == id)
#if CAP_SDL2 #if CAP_SDL2
lk = lk + " " + SDL_GetScancodeName(SDL_Scancode(i)); lk = lk + " " + SDL_GetScancodeName(SDL_Scancode(i));
@ -265,7 +266,8 @@ struct key_configurer {
if(!setwhat) dialog::handleNavigation(sym, uni); if(!setwhat) dialog::handleNavigation(sym, uni);
if(sym) { if(sym) {
if(setwhat) { if(setwhat) {
which_config->keyaction[sym] = setwhat; int scan = key_to_scan(sym);
if(scan >= 0 && scan < SCANCODES) which_config->keyaction[scan] = setwhat;
setwhat = 0; setwhat = 0;
} }
else if(uni >= 'a' && uni < 'a' + isize(shmupcmdtable) && shmupcmdtable[uni-'a'][0]) else if(uni >= 'a' && uni < 'a' + isize(shmupcmdtable) && shmupcmdtable[uni-'a'][0])
@ -584,9 +586,19 @@ void pressaction(int id) {
actionspressed[id]++; actionspressed[id]++;
} }
EX int key_to_scan(int sym) {
#if CAP_SDL2
return SDL_GetScancodeFromKey(sym);
#else
return sym;
#endif
}
EX bool notremapped(int sym) { EX bool notremapped(int sym) {
auto& scfg = scfg_default; auto& scfg = scfg_default;
int k = scfg.keyaction[sym]; int sc = key_to_scan(sym);
if(sc < 0 || sc >= SCANCODES) return true;
int k = scfg.keyaction[sc];
if(k == 0) return true; if(k == 0) return true;
k /= 16; k /= 16;
if(k > 3) k--; else if(k==3) k = 0; if(k > 3) k--; else if(k==3) k = 0;
@ -595,7 +607,7 @@ EX bool notremapped(int sym) {
EX void sconfig_savers(config& scfg, string prefix) { EX void sconfig_savers(config& scfg, string prefix) {
// unfortunately we cannot use key names here because SDL is not yet initialized // unfortunately we cannot use key names here because SDL is not yet initialized
for(int i=0; i<512; i++) for(int i=0; i<SCANCODES; i++)
addsaver(scfg.keyaction[i], prefix + string("key:")+its(i)); addsaver(scfg.keyaction[i], prefix + string("key:")+its(i));
for(int i=0; i<MAXJOY; i++) { for(int i=0; i<MAXJOY; i++) {
@ -613,7 +625,7 @@ EX void sconfig_savers(config& scfg, string prefix) {
} }
EX void clear_config(config& scfg) { EX void clear_config(config& scfg) {
for(int i=0; i<512; i++) scfg.keyaction[i] = 0; for(int i=0; i<SCANCODES; i++) scfg.keyaction[i] = 0;
} }
EX void initConfig() { EX void initConfig() {