summaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorDusan Durech <dusan.d@Centrum.sk>2009-06-24 17:05:12 +0200
committerDusan Durech <dusan.d@Centrum.sk>2009-06-24 17:05:12 +0200
commit0f02f6efd455329fdcdd4737801b3c1e6cc536d3 (patch)
treed856c1ce299e734cb455c2246908318a39ee4875 /src/client
parentba961b3df79e19ab9313c43475aa30ddac31c112 (diff)
better code for control tux
Diffstat (limited to 'src/client')
-rw-r--r--src/client/control.c96
-rw-r--r--src/client/control.h28
2 files changed, 124 insertions, 0 deletions
diff --git a/src/client/control.c b/src/client/control.c
new file mode 100644
index 0000000..5d99c66
--- /dev/null
+++ b/src/client/control.c
@@ -0,0 +1,96 @@
+
+#include <SDL.h>
+#include <limits.h>
+#include <assert.h>
+
+#include "main.h"
+#include "control.h"
+
+control_t* control_new(SDLKey arg_up, SDLKey arg_right, SDLKey arg_left, SDLKey arg_down,
+ SDLKey arg_shot, SDLKey arg_switch)
+{
+ control_t *tmp;
+
+ tmp = malloc(sizeof(control_t));
+ memset(tmp, 0, sizeof(control_t));
+ tmp->key[CONTROL_UP] = arg_up;
+ tmp->key[CONTROL_RIGHT] = arg_right;
+ tmp->key[CONTROL_LEFT] = arg_left;
+ tmp->key[CONTROL_DOWN] = arg_down;
+ tmp->key[CONTROL_SHOT] = arg_shot;
+ tmp->key[CONTROL_SWITCH] = arg_switch;
+
+ return tmp;
+}
+
+int control_get_key_route(control_t *my_control)
+{
+ Uint8 *mapa;
+ SDLKey ret_key;
+ int z;
+ int i;
+
+ assert(my_control != NULL);
+
+ ret_key = CONTROL_NONE;
+
+ mapa = SDL_GetKeyState(NULL);
+
+ for (i = 0; i<CONTROL_KEY_COUNT_ROUTE; i++)
+ {
+ if (mapa[my_control->key[i]] == SDL_PRESSED)
+ {
+ my_control->count[i]++;
+ }
+ else
+ {
+ my_control->count[i] = 0;
+ }
+ }
+
+ z = INT_MAX;
+
+ for (i = 0; i<CONTROL_KEY_COUNT_ROUTE; i++)
+ {
+ if ( my_control->count[i] > 0 &&
+ my_control->count[i] < z &&
+ mapa[my_control->key[i]] == SDL_PRESSED )
+ {
+ z = my_control->count[i];
+ ret_key = i;
+ }
+ }
+
+ return ret_key;
+}
+
+int control_get_key_action(control_t *my_control)
+{
+ Uint8 *mapa;
+ SDLKey ret_key;
+
+ assert(my_control != NULL);
+
+ ret_key = CONTROL_NONE;
+
+ mapa = SDL_GetKeyState(NULL);
+
+ if (mapa[my_control->key[CONTROL_SHOT]] == SDL_PRESSED)
+ {
+ return CONTROL_SHOT;
+ }
+
+ if (mapa[my_control->key[CONTROL_SWITCH]] == SDL_PRESSED)
+ {
+ return CONTROL_SWITCH;
+ }
+
+ return CONTROL_NONE;
+}
+
+void control_destroy(control_t *my_control)
+{
+ assert(my_control != NULL);
+
+ free(my_control);
+}
diff --git a/src/client/control.h b/src/client/control.h
new file mode 100644
index 0000000..f36c9b5
--- /dev/null
+++ b/src/client/control.h
@@ -0,0 +1,28 @@
+#ifndef CONTROL_H
+
+#define CONTROL_H
+
+#define CONTROL_NONE -1
+#define CONTROL_UP 0
+#define CONTROL_RIGHT 1
+#define CONTROL_LEFT 2
+#define CONTROL_DOWN 3
+#define CONTROL_SHOT 4
+#define CONTROL_SWITCH 5
+
+#define CONTROL_KEY_COUNT_ROUTE 4
+#define CONTROL_KEY_COUNT 6
+
+typedef struct control_struct
+{
+ SDLKey key[CONTROL_KEY_COUNT];
+ int count[CONTROL_KEY_COUNT];
+} control_t;
+
+extern control_t* control_new(SDLKey arg_up, SDLKey arg_right, SDLKey arg_left, SDLKey arg_down,
+ SDLKey arg_shot, SDLKey arg_switch);
+extern int control_get_key_route(control_t *my_control);
+extern int control_get_key_action(control_t *my_control);
+extern void control_destroy(control_t *my_control);
+
+#endif