ref: 2efe52fd31a27925a381f948958129f63051743b
parent: f9d1d15dbdda82faaca58d7d4a3ccd0c74f10da2
author: LTCHIPS <ltchips994@gmail.com>
date: Wed Mar 14 21:04:55 EDT 2018
Replaced Scancode array with a hash table that links SDL keycodes to ROTT scancodes
--- /dev/null
+++ b/rott/HashTable.c
@@ -1,0 +1,95 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+#include <stdlib.h>
+#include <stdio.h>
+#include "HashTable.h"
+
+#if !defined(ARRAY_SIZE)
+ #define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0]))
+#endif
+
+void InitHashTable(HashTable * hashTable, int initSize)
+{
+ hashTable->totalSize = initSize;
+ hashTable->table = malloc(sizeof(int) * initSize);
+ hashTable->keys = malloc(sizeof(int) * initSize);
+
+ memset(hashTable->table,(int) NULL, sizeof(int) * initSize);
+ memset(hashTable->keys,(int) NULL, sizeof(int) * initSize);
+}
+
+int HashFunc(HashTable * hashTable, int key, int item)
+{
+ return (key * (item)) % hashTable->totalSize;
+}
+
+void Delete(HashTable * hashTable, int key)
+{
+ if (hashTable->keys[key] == -1)
+ {
+ printf("ERROR: Tried to delete something that doesn't exist in the hash table!");
+ exit(1);
+ }
+
+ hashTable->table[hashTable->keys[key]] = (int) NULL;
+ free(&hashTable->table[hashTable->keys[key]]);
+ free(&hashTable->keys[key]);
+}
+
+void ClearHashTable (HashTable * hashTable)
+{
+ free(hashTable->table);
+ free(hashTable->keys);
+}
+
+void Insert(HashTable * hashTable, int key, int item)
+{
+ int index = HashFunc(hashTable,key,item);
+ int found = 0;
+ if (hashTable->table[index] != (int) NULL)
+ {
+ //printf("COLLISION \n");
+ int x;
+ for (x = index; x < 0; x-- )
+ {
+ if (hashTable->table[x] == (int) NULL)
+ {
+ hashTable->table[x] = item;
+ hashTable->keys[key] = x;
+ found++;
+ break;
+ }
+ }
+ if (!found)
+ {
+ for (x = index; x > ARRAY_SIZE(hashTable->table); x++ )
+ {
+ if (hashTable->table[x] == (int) NULL)
+ {
+ hashTable->table[x] = item;
+ hashTable->keys[key] = x;
+ found++;
+ break;
+ }
+ }
+ if(!found)
+ {
+ printf("ERROR: Hash Table was unable to find a blank entry! \n");
+ exit(1);
+ }
+ }
+ }
+ else
+ {
+ hashTable->keys[key] = index;
+ hashTable->table[index] = item;
+ }
+}
+
+int Lookup(HashTable * hashTable, int key)
+{
+ return hashTable->table[hashTable->keys[key]];
+}
\ No newline at end of file
--- /dev/null
+++ b/rott/HashTable.h
@@ -1,0 +1,39 @@
+/*
+ * To change this license header, choose License Headers in Project Properties.
+ * To change this template file, choose Tools | Templates
+ * and open the template in the editor.
+ */
+
+/*
+ * File: HashTable.h
+ * Author: LTCHIPS
+ *
+ * Created on March 13, 2018, 5:10 PM
+ */
+
+#ifndef HASHTABLE_H
+#define HASHTABLE_H
+
+typedef struct HashTable
+{
+ int totalSize;
+ int * keys;
+ int * table;
+} HashTable;
+
+void InitHashTable(HashTable * hashTable, int initSize);
+
+int HashFunc(HashTable * hashTable, int key, int item);
+
+void Delete(HashTable * hashTable, int key);
+
+void ClearHashTable (HashTable * hashTable);
+
+void Insert(HashTable * hashTable, int key, int item);
+
+int Lookup(HashTable * hashTable, int key);
+
+
+#endif /* HASHTABLE_H */
+
+
--- a/rott/Makefile
+++ b/rott/Makefile
@@ -83,6 +83,7 @@
OBJS += dukemusc.o
OBJS += winrott.o
OBJS += queue.o
+OBJS += HashTable.o
AUDIOLIB := audiolib/audiolib.a
--- a/rott/rt_in.c
+++ b/rott/rt_in.c
@@ -20,6 +20,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <SDL2/SDL_scancode.h>
#if PLATFORM_DOS
#include <conio.h>
@@ -49,6 +50,7 @@
//MED
#include "memcheck.h"
#include "keyb.h"
+#include "HashTable.h"
#define MAXMESSAGELENGTH (COM_MAXTEXTSTRINGLENGTH-1)
@@ -103,7 +105,8 @@
static word *sdl_stick_button_state = NULL;
static word sdl_sticks_joybits = 0;
static int sdl_mouse_grabbed = 0;
-static unsigned int scancodes[SDL_NUM_SCANCODES]; //TODO: replace with a hashtable if possible
+//static unsigned int scancodes[SDL_NUM_SCANCODES]; //TODO: replace with a hashtable if possible
+static HashTable * scancodes;
extern boolean sdl_fullscreen;
#endif
@@ -290,7 +293,8 @@
if (event->key.keysym.mod & KMOD_SHIFT)
{
kp_enter_hack = 1;
- retval = scancodes[SDLK_KP_ENTER];
+ retval = Lookup(scancodes, SDLK_KP_ENTER);
+ //retval = scancodes[SDLK_KP_ENTER];
} /* if */
} /* if */
@@ -299,7 +303,8 @@
if (kp_enter_hack)
{
kp_enter_hack = 0;
- retval = scancodes[SDLK_KP_ENTER];
+ retval = scancodes = Lookup(scancodes, SDLK_KP_ENTER);
+ //retval = scancodes[SDLK_KP_ENTER];
} /* if */
} /* if */
} /* if */
@@ -315,7 +320,8 @@
int strippedkey;
//SDL_GrabMode grab_mode = SDL_GRAB_OFF;
int extended;
-
+
+
if ( (event->key.keysym.sym == SDLK_g) &&
(event->key.state == SDL_PRESSED) &&
(event->key.keysym.mod & KMOD_CTRL) )
@@ -351,7 +357,16 @@
k = handle_keypad_enter_hack(event);
if (!k)
{
- k = scancodes[event->key.keysym.sym];
+ if (event->key.keysym.sym == SDLK_a)
+ {
+ k = 0x1e;
+ }
+ else
+ {
+ k = Lookup(scancodes, event->key.keysym.sym);
+ //k = scancodes[event->key.keysym.sym];
+ }
+
if (!k) /* No DOS equivalent defined. */
return(0);
} /* if */
@@ -376,7 +391,8 @@
{
KeyboardQueue[ Keytail ] = extended;
Keytail = ( Keytail + 1 )&( KEYQMAX - 1 );
- k = scancodes[event->key.keysym.sym] & 0xFF;
+ k = Lookup(scancodes, event->key.keysym.sym) & 0xFF;
+ //k = scancodes[event->key.keysym.sym] & 0xFF;
if (event->key.state == SDL_RELEASED)
k += 128; /* +128 signifies that the key is released in DOS. */
}
@@ -454,29 +470,11 @@
{
IN_PumpEvents();
-#ifdef PLATFORM_DOS
- union REGS inregs;
- union REGS outregs;
-
- if (!MousePresent)
- *x = *y = 0;
- else
- {
- inregs.w.ax = MDelta;
- int386 (MouseInt, &inregs, &outregs);
- *x = outregs.w.cx;
- *y = outregs.w.dx;
- }
-
-#elif USE_SDL
*x = sdl_mouse_delta_x;
*y = sdl_mouse_delta_y;
sdl_mouse_delta_x = sdl_mouse_delta_y = 0;
-#else
-#error please define for your platform.
-#endif
}
@@ -498,33 +496,8 @@
IN_PumpEvents();
-#if USE_SDL
buttons = sdl_mouse_button_mask;
-#elif PLATFORM_DOS
- union REGS inregs;
- union REGS outregs;
-
- if (!MousePresent || !mouseenabled)
- return (0);
-
- inregs.w.ax = MButtons;
- int386 (MouseInt, &inregs, &outregs);
-
- buttons = outregs.w.bx;
-
-#else
-# error please define for your platform.
-#endif
-
-// Used by menu routines that need to wait for a button release.
-// Sometimes the mouse driver misses an interrupt, so you can't wait for
-// a button to be released. Instead, you must ignore any buttons that
-// are pressed.
-
- IgnoreMouse &= buttons;
- buttons &= ~IgnoreMouse;
-
return (buttons);
}
@@ -661,20 +634,9 @@
{
word result = 0;
-#if USE_SDL
if (joy < sdl_total_sticks)
result = sdl_stick_button_state[joy];
-#elif PLATFORM_DOS
- result = inp (0x201); // Get all the joystick buttons
- result >>= joy? 6 : 4; // Shift into bits 0-1
- result &= 3; // Mask off the useless bits
- result ^= 3;
-
-#else
-#error please define for your platform.
-#endif
-
return result;
}
@@ -715,23 +677,9 @@
boolean retval = false;
-#if USE_SDL
/* no-op. */
retval = true;
-#elif PLATFORM_DOS
- union REGS inregs;
- union REGS outregs;
-
- inregs.w.ax = 0;
- int386 (MouseInt, &inregs, &outregs);
-
- retval = ((outregs.w.ax == 0xffff) ? true : false);
-
-#else
-#error please define your platform.
-#endif
-
return (retval);
}
@@ -882,130 +830,124 @@
// fixme: remove this.
sdl_mouse_grabbed = 1;
#endif
-
+
/*
all keys are now mapped to the wolf3d-style names,
except where no such name is available.
*/
- memset(scancodes, '\0', sizeof (scancodes));
- scancodes[SDLK_ESCAPE] = sc_Escape;
- scancodes[SDLK_1] = sc_1;
- scancodes[SDLK_2] = sc_2;
- scancodes[SDLK_3] = sc_3;
- scancodes[SDLK_4] = sc_4;
- scancodes[SDLK_5] = sc_5;
- scancodes[SDLK_6] = sc_6;
- scancodes[SDLK_7] = sc_7;
- scancodes[SDLK_8] = sc_8;
- scancodes[SDLK_9] = sc_9;
- scancodes[SDLK_0] = sc_0;
-
- //scancodes[SDLK_EQUALS] = 0x4E;
- scancodes[SDLK_EQUALS] = sc_Equals;
-
- scancodes[SDLK_BACKSPACE] = sc_BackSpace;
- scancodes[SDLK_TAB] = sc_Tab;
- scancodes[SDLK_q] = sc_Q;
- scancodes[SDLK_w] = sc_W;
- scancodes[SDLK_e] = sc_E;
- scancodes[SDLK_r] = sc_R;
- scancodes[SDLK_t] = sc_T;
- scancodes[SDLK_y] = sc_Y;
- scancodes[SDLK_u] = sc_U;
- scancodes[SDLK_i] = sc_I;
- scancodes[SDLK_o] = sc_O;
- scancodes[SDLK_p] = sc_P;
- scancodes[SDLK_LEFTBRACKET] = sc_OpenBracket;
- scancodes[SDLK_RIGHTBRACKET] = sc_CloseBracket;
- scancodes[SDLK_RETURN] = sc_Return;
- scancodes[SDLK_LCTRL] = sc_Control;
- scancodes[SDLK_a] = sc_A;
- scancodes[SDLK_s] = sc_S;
- scancodes[SDLK_d] = sc_D;
- scancodes[SDLK_f] = sc_F;
- scancodes[SDLK_g] = sc_G;
- scancodes[SDLK_h] = sc_H;
- scancodes[SDLK_j] = sc_J;
- scancodes[SDLK_k] = sc_K;
- scancodes[SDLK_l] = sc_L;
- scancodes[SDLK_SEMICOLON] = 0x27;
- scancodes[SDLK_QUOTE] = 0x28;
- scancodes[SDLK_BACKQUOTE] = 0x29;
-
- /* left shift, but ROTT maps it to right shift in isr.c */
- scancodes[SDLK_LSHIFT] = sc_RShift; /* sc_LShift */
-
- scancodes[SDLK_BACKSLASH] = 0x2B;
- /* Accept the German eszett as a backslash key */
- //scancodes[SDLK_WORLD_63] = 0x2B;
- scancodes[SDLK_z] = sc_Z;
- scancodes[SDLK_x] = sc_X;
- scancodes[SDLK_c] = sc_C;
- scancodes[SDLK_v] = sc_V;
- scancodes[SDLK_b] = sc_B;
- scancodes[SDLK_n] = sc_N;
- scancodes[SDLK_m] = sc_M;
- scancodes[SDLK_COMMA] = sc_Comma;
- scancodes[SDLK_PERIOD] = sc_Period;
- scancodes[SDLK_SLASH] = 0x35;
- scancodes[SDLK_RSHIFT] = sc_RShift;
- scancodes[SDLK_KP_DIVIDE] = 0x35;
-
- /* 0x37 is printscreen */
- //scancodes[SDLK_KP_MULTIPLY] = 0x37;
-
- scancodes[SDLK_LALT] = sc_Alt;
- scancodes[SDLK_RALT] = sc_Alt;
- scancodes[SDLK_MODE] = sc_Alt;
- scancodes[SDLK_RCTRL] = sc_Control;
- scancodes[SDLK_SPACE] = sc_Space;
- scancodes[SDLK_CAPSLOCK] = sc_CapsLock;
- scancodes[SDLK_F1] = sc_F1;
- scancodes[SDLK_F2] = sc_F2;
- scancodes[SDLK_F3] = sc_F3;
- scancodes[SDLK_F4] = sc_F4;
- scancodes[SDLK_F5] = sc_F5;
- scancodes[SDLK_F6] = sc_F6;
- scancodes[SDLK_F7] = sc_F7;
- scancodes[SDLK_F8] = sc_F8;
- scancodes[SDLK_F9] = sc_F9;
- scancodes[SDLK_F10] = sc_F10;
- scancodes[SDLK_F11] = sc_F11;
- scancodes[SDLK_F12] = sc_F12;
- scancodes[SDLK_NUMLOCKCLEAR] = 0x45;
- scancodes[SDLK_SCROLLLOCK] = 0x46;
-
- //scancodes[SDLK_MINUS] = 0x4A;
- scancodes[SDLK_MINUS] = sc_Minus;
-
- scancodes[SDLK_KP_7] = sc_Home;
- scancodes[SDLK_KP_8] = sc_UpArrow;
- scancodes[SDLK_KP_9] = sc_PgUp;
- scancodes[SDLK_HOME] = sc_Home;
- scancodes[SDLK_UP] = sc_UpArrow;
- scancodes[SDLK_PAGEUP] = sc_PgUp;
- // Make this a normal minus, for viewport changing
- //scancodes[SDLK_KP_MINUS] = 0xE04A;
- scancodes[SDLK_KP_MINUS] = sc_Minus;
- scancodes[SDLK_KP_4] = sc_LeftArrow;
- scancodes[SDLK_KP_5] = 0x4C;
- scancodes[SDLK_KP_6] = sc_RightArrow;
- scancodes[SDLK_LEFT] = sc_LeftArrow;
- scancodes[SDLK_RIGHT] = sc_RightArrow;
-
- //scancodes[SDLK_KP_PLUS] = 0x4E;
- scancodes[SDLK_KP_PLUS] = sc_Plus;
-
- scancodes[SDLK_KP_1] = sc_End;
- scancodes[SDLK_KP_2] = sc_DownArrow;
- scancodes[SDLK_KP_3] = sc_PgDn;
- scancodes[SDLK_END] = sc_End;
- scancodes[SDLK_DOWN] = sc_DownArrow;
- scancodes[SDLK_PAGEDOWN] = sc_PgDn;
- scancodes[SDLK_DELETE] = sc_Delete;
- scancodes[SDLK_KP_0] = sc_Insert;
- scancodes[SDLK_INSERT] = sc_Insert;
- scancodes[SDLK_KP_ENTER] = sc_Return;
+ //calloc(scancodes, sizeof(unsigned int) * 513);
+
+ scancodes = malloc(sizeof(HashTable));
+
+ InitHashTable(scancodes, SDL_NUM_SCANCODES);
+
+ Insert(scancodes, SDLK_ESCAPE, sc_Escape);
+ Insert(scancodes, SDLK_1, sc_1);
+ Insert(scancodes, SDLK_2, sc_2);
+ Insert(scancodes, SDLK_3, sc_3);
+ Insert(scancodes, SDLK_4, sc_4);
+ Insert(scancodes, SDLK_5, sc_5);
+ Insert(scancodes, SDLK_6, sc_6);
+ Insert(scancodes, SDLK_7, sc_7);
+ Insert(scancodes, SDLK_8, sc_8);
+ Insert(scancodes, SDLK_9, sc_9);
+ Insert(scancodes, SDLK_0, sc_0);
+
+ Insert(scancodes, SDLK_EQUALS, sc_Equals);
+
+ Insert(scancodes, SDLK_BACKSPACE, sc_BackSpace);
+ Insert(scancodes, SDLK_TAB, sc_Tab);
+ Insert(scancodes, SDLK_q, sc_Q);
+ Insert(scancodes, SDLK_w, sc_W);
+ Insert(scancodes, SDLK_e, sc_E);
+ Insert(scancodes, SDLK_r, sc_R);
+ Insert(scancodes, SDLK_t, sc_T);
+ Insert(scancodes, SDLK_y, sc_Y);
+ Insert(scancodes, SDLK_u, sc_U);
+ Insert(scancodes, SDLK_i, sc_I);
+ Insert(scancodes, SDLK_o, sc_O);
+ Insert(scancodes, SDLK_p, sc_P);
+ Insert(scancodes, SDLK_LEFTBRACKET, sc_OpenBracket);
+ Insert(scancodes, SDLK_RIGHTBRACKET, sc_CloseBracket);
+ Insert(scancodes, SDLK_RETURN, sc_Return);
+ Insert(scancodes, SDLK_LCTRL, sc_Control);
+
+ Insert(scancodes, SDLK_PAGEUP, sc_PgUp);
+ Insert(scancodes, SDLK_s, sc_S);
+ Insert(scancodes, SDLK_d, sc_D);
+ Insert(scancodes, SDLK_f, sc_F);
+ Insert(scancodes, SDLK_g, sc_G);
+ Insert(scancodes, SDLK_h, sc_H);
+ Insert(scancodes, SDLK_j, sc_J);
+ Insert(scancodes, SDLK_k, sc_K);
+ Insert(scancodes, SDLK_l, sc_L);
+ Insert(scancodes, SDLK_SEMICOLON, 0x27);
+ Insert(scancodes, SDLK_QUOTE, 0x28);
+ Insert(scancodes, SDLK_BACKQUOTE, 0x29);
+
+ Insert(scancodes, SDLK_LSHIFT, sc_RShift);
+
+ Insert(scancodes, SDLK_BACKSLASH, 0x2B);
+ Insert(scancodes, SDLK_z, sc_Z);
+ Insert(scancodes, SDLK_x, sc_X);
+ Insert(scancodes, SDLK_c, sc_C);
+ Insert(scancodes, SDLK_v, sc_V);
+ Insert(scancodes, SDLK_b, sc_B);
+ Insert(scancodes, SDLK_n, sc_N);
+ Insert(scancodes, SDLK_m, sc_M);
+ Insert(scancodes, SDLK_COMMA, sc_Comma);
+ Insert(scancodes, SDLK_PERIOD, sc_Period);
+ Insert(scancodes, SDLK_SLASH, 0x35);
+ Insert(scancodes, SDLK_RSHIFT, sc_RShift);
+ Insert(scancodes, SDLK_KP_DIVIDE, 0x35);
+
+ Insert(scancodes, SDLK_LALT, sc_Alt);
+ Insert(scancodes, SDLK_RALT, sc_Alt);
+ Insert(scancodes, SDLK_MODE, sc_Alt);
+ Insert(scancodes, SDLK_RCTRL, sc_Control);
+ Insert(scancodes, SDLK_SPACE, sc_Space);
+ Insert(scancodes, SDLK_CAPSLOCK, sc_CapsLock);
+ Insert(scancodes, SDLK_F1, sc_F1);
+ Insert(scancodes, SDLK_F2, sc_F2);
+ Insert(scancodes, SDLK_F3, sc_F3);
+ Insert(scancodes, SDLK_F4, sc_F4);
+ Insert(scancodes, SDLK_F5, sc_F5);
+ Insert(scancodes, SDLK_F6, sc_F6);
+ Insert(scancodes, SDLK_F7, sc_F7);
+ Insert(scancodes, SDLK_F8, sc_F8);
+ Insert(scancodes, SDLK_F9, sc_F9);
+ Insert(scancodes, SDLK_F10, sc_F10);
+ Insert(scancodes, SDLK_F11, sc_F11);
+ Insert(scancodes, SDLK_F12, sc_F12);
+ Insert(scancodes, SDLK_NUMLOCKCLEAR, 0x45);
+ Insert(scancodes, SDLK_SCROLLLOCK, 0x46);
+
+ Insert(scancodes, SDLK_MINUS, sc_Minus);
+
+ Insert(scancodes, SDLK_KP_7, sc_Home);
+ Insert(scancodes, SDLK_KP_8, sc_UpArrow);
+ Insert(scancodes, SDLK_KP_9, sc_PgUp);
+ Insert(scancodes, SDLK_HOME, sc_Home);
+ Insert(scancodes, SDLK_UP, sc_UpArrow);
+ Insert(scancodes, SDLK_a, sc_A);
+ Insert(scancodes, SDLK_KP_MINUS, sc_Minus);
+ Insert(scancodes, SDLK_KP_4, sc_LeftArrow);
+ Insert(scancodes, SDLK_KP_5, 0x4C);
+ Insert(scancodes, SDLK_KP_6, sc_RightArrow);
+ Insert(scancodes, SDLK_LEFT, sc_LeftArrow);
+ Insert(scancodes, SDLK_RIGHT, sc_RightArrow);
+ Insert(scancodes, SDLK_KP_PLUS, sc_Plus);
+
+ Insert(scancodes, SDLK_KP_1, sc_End);
+ Insert(scancodes, SDLK_KP_2, sc_DownArrow);
+ Insert(scancodes, SDLK_KP_3, sc_PgDn);
+ Insert(scancodes, SDLK_END, sc_End);
+ Insert(scancodes, SDLK_DOWN, sc_DownArrow);
+ Insert(scancodes, SDLK_PAGEDOWN, sc_PgDn);
+ Insert(scancodes, SDLK_DELETE, sc_Delete);
+ Insert(scancodes, SDLK_KP_0, sc_Insert);
+ Insert(scancodes, SDLK_INSERT, sc_Insert);
+ Insert(scancodes, SDLK_KP_ENTER, sc_Return);
#endif
checkjoys = true;
--- a/rott/rt_in.h
+++ b/rott/rt_in.h
@@ -28,6 +28,7 @@
#include "develop.h"
#include "rottnet.h"
+#include "HashTable.h"
//***************************************************************************
//