]> icculus.org git repositories - crow/jumpnbump.git/blob - sdl/interrpt.c
More code cleanups. Separated some stuff into new functions.
[crow/jumpnbump.git] / sdl / interrpt.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <time.h>
4 #ifndef _MSC_VER
5 #include <unistd.h>
6 #endif
7 #include "globals.h"
8
9 char keyb[256];
10 char last_keys[50];
11
12 unsigned char scancode2ascii[256] = {
13         0, 0, 49, 50, 51, 52, 53, 54, 55, 56,           /* 0-9 */
14         57, 48, 45, 0, 0, 0, 113, 119, 101, 114,        /* 10-19 */
15         116, 121, 117, 105, 111, 112, 0, 0, 0, 0,       /* 20-29 */
16         97, 115, 100, 102, 103, 104, 106, 107, 108, 0,  /* 30-39 */
17         0, 0, 0, 0, 122, 120, 99, 118, 98, 110,         /* 40-49 */
18         109, 44, 46, 47, 0, 0, 0, 32, 0, 0,             /* 50-59 */
19         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
20         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
21         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
22         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
23         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
24         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
25         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
26         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
27         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
28         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
29         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
30         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
31         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
32         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
33         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
34         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
35         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
36         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
37         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
38         0, 0, 0, 0, 0, 0
39 };
40
41 int hook_keyb_handler(void)
42 {
43         SDL_EnableUNICODE(1);
44         memset((void *) last_keys, 0, sizeof(last_keys));
45         return 0;
46
47 }
48
49
50 void remove_keyb_handler(void)
51 {
52 }
53
54
55 int key_pressed(int key)
56 {
57         return keyb[(unsigned char) key];
58 }
59
60 int addkey(unsigned int key)
61 {
62         int c1;
63
64         if (!(key & 0x8000)) {
65                 keyb[key & 0x7fff] = 1;
66                 for (c1 = 48; c1 > 0; c1--)
67                         last_keys[c1] = last_keys[c1 - 1];
68                 last_keys[0] = key & 0x7fff;
69         } else
70                 keyb[key & 0x7fff] = 0;
71         return 0;
72 }
73
74 int intr_sysupdate()
75 {
76         SDL_Event e;
77         int i = 0;
78         static Uint32 now, then = 0;
79
80         while (SDL_PollEvent(&e)) {
81                 switch (e.type) {
82                 case SDL_MOUSEBUTTONDOWN:
83                         break;
84                 case SDL_KEYDOWN:
85                 case SDL_KEYUP:
86                         switch (e.key.keysym.sym) {
87                         case SDLK_F12:
88                                 if (e.type == SDL_KEYDOWN) {
89                                         SDL_Quit();
90                                         exit(1);
91                                 }
92                                 break;
93                         case SDLK_F10:
94                                 if (e.type == SDL_KEYDOWN) {
95                                         fs_toggle();
96                                 }
97                                 break;
98                         case SDLK_ESCAPE:
99                                 if (e.type == SDL_KEYUP)
100                                         addkey(1 | 0x8000);
101                                 else
102                                         addkey(1 & 0x7f);
103                                 break;
104                         default:
105                                 e.key.keysym.sym &= 0x7f;
106                                 if (e.type == SDL_KEYUP)
107                                         e.key.keysym.sym |= 0x8000;
108                                 addkey(e.key.keysym.sym);
109
110                                 break;
111                         }
112                         break;
113                 default:
114                         break;
115                 }
116                 i++;
117         }
118         //SDL_Delay(4);
119         now = SDL_GetTicks();
120         if (!then)
121                 SDL_Delay(1);
122         else {
123                 then = (1000 / 60) - (now - then);
124                 if (then > 0 && then < 1000)
125                         SDL_Delay(then);
126         }
127         then = now;
128
129         return i;
130 }