basic key handling
This commit is contained in:
@@ -1,29 +1,102 @@
|
|||||||
#include "cat_api.h"
|
#include "cat_api.h"
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
|
||||||
#define NS_PER_TICK 30000000
|
|
||||||
|
|
||||||
void bye(void)
|
void bye(void)
|
||||||
{
|
{
|
||||||
printf("bye!\n");
|
printf("bye!\n");
|
||||||
exit(0);
|
exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct keydowns
|
||||||
|
{
|
||||||
|
char k;
|
||||||
|
struct keydowns* next;
|
||||||
|
};
|
||||||
|
|
||||||
|
struct keydowns* first_keydown = NULL;
|
||||||
|
|
||||||
|
void add_keydown(char k)
|
||||||
|
{
|
||||||
|
struct keydowns* new = malloc(sizeof(struct keydowns));
|
||||||
|
new->k = k;
|
||||||
|
new->next = NULL;
|
||||||
|
if (first_keydown == NULL) first_keydown = new;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
struct keydowns* head;
|
||||||
|
for (head = first_keydown; head->next != NULL; head = head->next)
|
||||||
|
if (head->k == k) return;
|
||||||
|
head->next = new;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void lit_remove_keydown(struct keydowns* entry)
|
||||||
|
{
|
||||||
|
if (first_keydown == entry) first_keydown = entry->next;
|
||||||
|
else
|
||||||
|
{
|
||||||
|
struct keydowns* head;
|
||||||
|
for (head = first_keydown; head->next != entry; head = head->next) ;
|
||||||
|
head->next = entry->next;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void remove_keydown(char k)
|
||||||
|
{
|
||||||
|
struct keydowns* head;
|
||||||
|
for (head = first_keydown; head != NULL; head = head->next)
|
||||||
|
if (head->k == k) lit_remove_keydown(head);
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_held(char k)
|
||||||
|
{
|
||||||
|
struct keydowns* head;
|
||||||
|
for (head = first_keydown; head != NULL; head = head->next)
|
||||||
|
if (head->k == k) return 1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
void key_down(char k)
|
||||||
|
{
|
||||||
|
add_keydown(k);
|
||||||
|
printf("%c pressed!\n", k);
|
||||||
|
}
|
||||||
|
|
||||||
|
void key_up(char k)
|
||||||
|
{
|
||||||
|
remove_keydown(k);
|
||||||
|
printf("%c un-pressed\n", k);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouse_move(int x, int y)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouse_down(unsigned char button)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouse_up(unsigned char button)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
void process_input(void)
|
void process_input(void)
|
||||||
{
|
{
|
||||||
struct cat_event* e = cat_get_event();
|
struct cat_event* e = cat_get_event();
|
||||||
if (e == NULL) return;
|
if (e == NULL) return;
|
||||||
if (e->type == QUIT) bye();
|
if (e->type == QUIT) bye();
|
||||||
else if (e->type == KEYDOWN) printf("keypress! %c\n", e->key);
|
else if (e->type == KEYDOWN) key_down(e->key);
|
||||||
else if (e->type == KEYUP) printf("key up! %c\n", e->key);
|
else if (e->type == KEYUP) key_up(e->key);
|
||||||
else if (e->type == MOUSEMOVE) printf("mouse move! (%d, %d)\n", e->mousex, e->mousey);
|
else if (e->type == MOUSEMOVE) mouse_move(e->mousex, e->mousey);
|
||||||
else if (e->type == MOUSEDOWN) printf("mouse down! %02x\n", e->mouse_button);
|
else if (e->type == MOUSEDOWN) mouse_down(e->mouse_button);
|
||||||
else if (e->type == MOUSEUP) printf("mouse up! %02x\n", e->mouse_button);
|
else if (e->type == MOUSEUP) mouse_up(e->mouse_button);
|
||||||
}
|
}
|
||||||
|
|
||||||
static long last_sec = 0;
|
static long last_sec = 0;
|
||||||
static long last_nsec = 0;
|
static long last_nsec = 0;
|
||||||
|
|
||||||
|
#define NS_PER_TICK 30000000
|
||||||
|
|
||||||
void tick(void)
|
void tick(void)
|
||||||
{
|
{
|
||||||
/* run every N milliseconds... nanoseconds? idk */
|
/* run every N milliseconds... nanoseconds? idk */
|
||||||
@@ -35,6 +108,8 @@ void tick(void)
|
|||||||
else return;
|
else return;
|
||||||
yes: last_sec = t.tv_sec;
|
yes: last_sec = t.tv_sec;
|
||||||
last_nsec = t.tv_nsec;
|
last_nsec = t.tv_nsec;
|
||||||
|
|
||||||
|
/* code to run every tick goes here */
|
||||||
}
|
}
|
||||||
|
|
||||||
void game_loop(void)
|
void game_loop(void)
|
||||||
|
|||||||
Reference in New Issue
Block a user