ref: c213b65f962b9a0a22274974fa5b213eb9f810c4
parent: b5757a8ba1128b07998610fb856d393d1942bd27
author: LTCHIPS <ltchips994@gmail.com>
date: Mon Mar 19 13:32:32 EDT 2018
Fixed keycode to scancode mapping issue. However special keyboard keys now crash the game when pressed...
--- a/rott/HashTable.c
+++ b/rott/HashTable.c
@@ -1,6 +1,8 @@
#include <stdlib.h>
#include <stdio.h>
+#include <string.h>
#include "HashTable.h"
+#include "LinkedList.h"
#if !defined(ARRAY_SIZE)
#define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0]))
@@ -9,82 +11,69 @@
void InitHashTable(HashTable * hashTable, int initSize)
{
hashTable->totalSize = initSize;
- hashTable->table = malloc(sizeof(int) * initSize);
- hashTable->keys = malloc(sizeof(int) * initSize);
+ hashTable->table = malloc(sizeof(LinkedList) * initSize);
- memset(hashTable->table,(int) NULL, sizeof(int) * initSize);
- memset(hashTable->keys,(int) NULL, sizeof(int) * initSize);
+ memset(hashTable->table,0, sizeof(LinkedList) * initSize);
}
-int HashFunc(HashTable * hashTable, int key, int item)
+int HashFunc(HashTable * hashTable, int key)
{
- return (key * (item)) % hashTable->totalSize;
+ return abs((key)) % 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);
- }
+ int index = HashFunc(hashTable,key);
- hashTable->table[hashTable->keys[key]] = (int) NULL;
- free(&hashTable->table[hashTable->keys[key]]);
- free(&hashTable->keys[key]);
+ LinkedList * list = hashTable->table[index];
+
+ DeleteWithKey(list, key);
+ free(&hashTable->table[index]);
}
void ClearHashTable (HashTable * hashTable)
-{
+{
+ int x = 0;
+ for (x; x < ARRAY_SIZE(hashTable->table); x++)
+ {
+ if (hashTable->table[x] != 0)
+ {
+ DestroyList(hashTable->table[x]);
+ }
+
+ }
free(hashTable->table);
- free(hashTable->keys);
-}
+}
void Insert(HashTable * hashTable, int key, int item)
{
- int index = HashFunc(hashTable,key,item);
+ int index = HashFunc(hashTable,key);
int found = 0;
- if (hashTable->table[index] != (int) NULL)
+ if (hashTable->table[index] != 0)
{
- //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);
- }
- }
+ InsertInList(hashTable->table[index], key, item);
}
else
{
- hashTable->keys[key] = index;
- hashTable->table[index] = item;
+ LinkedList * newList = malloc(sizeof(LinkedList));
+
+ InitLinkedList(newList);
+
+ InsertInList(newList, key, item);
+
+ hashTable->table[index] = newList;
}
}
int Lookup(HashTable * hashTable, int key)
{
- return hashTable->table[hashTable->keys[key]];
+ int index = HashFunc(hashTable,key);
+
+ if (hashTable->table == 0)
+ {
+ printf("ERROR: HashTable Lookup lead to a NULL Entry.");
+ exit(1);
+ }
+
+ return SearchWithKey(hashTable->table[index], key);
}
\ No newline at end of file
--- a/rott/HashTable.h
+++ b/rott/HashTable.h
@@ -1,12 +1,7 @@
-/*
- * 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
+ * Author: Steven LeVesque
*
* Created on March 13, 2018, 5:10 PM
*/
@@ -21,9 +16,15 @@
int * table;
} HashTable;
+typedef struct Key
+{
+ int IndexInTable;
+ int IndexInLinkedList;
+} Key;
+
void InitHashTable(HashTable * hashTable, int initSize);
-int HashFunc(HashTable * hashTable, int key, int item);
+int HashFunc(HashTable * hashTable, int key);
void Delete(HashTable * hashTable, int key);
--- /dev/null
+++ b/rott/LinkedList.c
@@ -1,0 +1,129 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include "LinkedList.h"
+
+void InitLinkedList(LinkedList * linkedList)
+{
+ linkedList->NumOfItems = 0;
+ linkedList->head = NULL;
+}
+
+void InsertInList(LinkedList * linkedList, int key, int item)
+{
+ if (linkedList->head == NULL)
+ {
+ listNode * newNode = malloc(sizeof(listNode));
+ newNode->data = item;
+ newNode->key = key;
+
+ linkedList->head = malloc(sizeof(listNode));
+ linkedList->head = newNode;
+
+ linkedList->head->next = NULL;
+
+ linkedList->NumOfItems++;
+ return;
+ }
+ else
+ {
+ listNode * x;
+ int counter = 0;
+
+ for (x = linkedList->head; counter < linkedList->NumOfItems; counter++, x = x->next )
+ {
+ if (x->next == NULL)
+ {
+ listNode * newNode = malloc(sizeof(listNode));
+ newNode->data = item;
+ newNode->key = key;
+
+ x->next = malloc(sizeof(listNode));
+ x->next = newNode;
+
+ newNode->next = NULL;
+
+ linkedList->NumOfItems++;
+ break;
+ }
+ }
+ }
+}
+
+int SearchWithKey(LinkedList * linkedList, int key)
+{
+ listNode * x;
+ int counter = 0;
+
+ for (x = linkedList->head; counter < linkedList->NumOfItems; counter++, x = x->next )
+ {
+ if (x->key == key)
+ {
+ return x->data;
+ }
+ }
+ return 0;
+
+}
+
+void DeleteAtIndex(LinkedList* linkedList,int index)
+{
+ listNode * x;
+ int counter = 0;
+
+ for (x = linkedList->head; counter < linkedList->NumOfItems; counter++, x = x->next )
+ {
+ if (counter == index)
+ {
+ if (counter == 0)
+ {
+ linkedList->head = linkedList->head->next;
+ }
+ free(x);
+ linkedList->NumOfItems--;
+ break;
+ }
+ }
+ if (counter == linkedList->NumOfItems)
+ {
+ printf("ERROR: LinkedList could not find item at %d \n", index);
+ exit(1);
+ }
+}
+void DeleteWithKey(LinkedList* linkedList,int key)
+{
+ listNode * x = linkedList->head;
+ int counter = 0;
+
+ for (counter; counter < linkedList->NumOfItems; counter++, x = x->next )
+ {
+ if (x->key == key)
+ {
+ if (counter == 0)
+ {
+ linkedList->head = linkedList->head->next;
+ }
+
+ free(x);
+ linkedList->NumOfItems--;
+ break;
+ }
+ }
+ if (counter == linkedList->NumOfItems)
+ {
+ printf("ERROR: LinkedList could not find item with key %d", key);
+ exit(1);
+ }
+}
+
+void DestroyList(LinkedList* linkedList)
+{
+ listNode *x = linkedList->head;
+ int counter = 0;
+ for (counter; counter < linkedList->NumOfItems; counter++ )
+ {
+ printf("%d ", x->data);
+ listNode * temp = x->next;
+ free(x);
+ x = temp;
+ }
+}
--- /dev/null
+++ b/rott/LinkedList.h
@@ -1,0 +1,46 @@
+/*
+ * File: LinkedList.h
+ * Author: LTCHIPS
+ *
+ * Created on March 19, 2018, 3:40 PM
+ */
+
+#ifndef LINKEDLIST_H
+#define LINKEDLIST_H
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+typedef struct listNode
+{
+ int key;
+ int data;
+ struct listNode * next;
+} listNode;
+
+typedef struct LinkedList
+{
+ int NumOfItems;
+ struct listNode * head;
+} LinkedList;
+
+int SearchWithKey(LinkedList *, int);
+
+void InitLinkedList(LinkedList*);
+
+void InsertInList(LinkedList*,int, int);
+
+void DeleteAtIndex(LinkedList*,int);
+
+void DeleteWithKey(LinkedList*,int);
+
+void DestroyList(LinkedList*);
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* LINKEDLIST_H */
+
--- a/rott/Makefile
+++ b/rott/Makefile
@@ -84,6 +84,7 @@
OBJS += winrott.o
OBJS += queue.o
OBJS += HashTable.o
+OBJS += LinkedList.o
AUDIOLIB := audiolib/audiolib.a
--- a/rott/rt_in.c
+++ b/rott/rt_in.c
@@ -357,15 +357,15 @@
k = handle_keypad_enter_hack(event);
if (!k)
{
- if (event->key.keysym.sym == SDLK_a)
- {
- k = 0x1e;
- }
- else
- {
+ //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);
@@ -822,13 +822,6 @@
if (IN_Started==true)
return;
-#if USE_SDL
-
-#if PLATFORM_WIN32
-// 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.
@@ -901,7 +894,7 @@
Insert(scancodes, SDLK_LALT, sc_Alt);
Insert(scancodes, SDLK_RALT, sc_Alt);
- Insert(scancodes, SDLK_MODE, 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);
@@ -909,6 +902,8 @@
Insert(scancodes, SDLK_F2, sc_F2);
Insert(scancodes, SDLK_F3, sc_F3);
Insert(scancodes, SDLK_F4, sc_F4);
+ Insert(scancodes, SDLK_KP_PLUS, sc_Plus);
+ Insert(scancodes, SDLK_PLUS, sc_Plus);
Insert(scancodes, SDLK_F5, sc_F5);
Insert(scancodes, SDLK_F6, sc_F6);
Insert(scancodes, SDLK_F7, sc_F7);
@@ -934,7 +929,6 @@
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);
@@ -946,7 +940,6 @@
Insert(scancodes, SDLK_KP_0, sc_Insert);
Insert(scancodes, SDLK_INSERT, sc_Insert);
Insert(scancodes, SDLK_KP_ENTER, sc_Return);
-#endif
checkjoys = true;
checkmouse = true;