]> icculus.org git repositories - crow/jumpnbump.git/blob - sdl/interrpt.c
Updated documentation.
[crow/jumpnbump.git] / sdl / interrpt.c
1 /*
2  * interrpt.c
3  * Copyright (C) 1998 Brainchild Design - http://brainchilddesign.com/
4  * 
5  * Copyright (C) 2001 Chuck Mason <cemason@users.sourceforge.net>
6  *
7  * Copyright (C) 2002 Florian Schulze <crow@icculus.org>
8  *
9  * Portions of this code are from the MPEG software simulation group
10  * idct implementation. This code will be replaced with a new
11  * implementation soon.
12  *
13  * This file is part of Jump'n'Bump.
14  *
15  * Jump'n'Bump is free software; you can redistribute it and/or modify
16  * it under the terms of the GNU General Public License as published by
17  * the Free Software Foundation; either version 2 of the License, or
18  * (at your option) any later version.
19  *
20  * Jump'n'Bump is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  * GNU General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <time.h>
33 #ifndef _MSC_VER
34 #include <unistd.h>
35 #endif
36 #include "globals.h"
37
38 char keyb[256];
39 char last_keys[50];
40
41 unsigned char scancode2ascii[256] = {
42         0, 0, 49, 50, 51, 52, 53, 54, 55, 56,           /* 0-9 */
43         57, 48, 45, 0, 0, 0, 113, 119, 101, 114,        /* 10-19 */
44         116, 121, 117, 105, 111, 112, 0, 0, 0, 0,       /* 20-29 */
45         97, 115, 100, 102, 103, 104, 106, 107, 108, 0,  /* 30-39 */
46         0, 0, 0, 0, 122, 120, 99, 118, 98, 110,         /* 40-49 */
47         109, 44, 46, 47, 0, 0, 0, 32, 0, 0,             /* 50-59 */
48         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
49         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
50         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
51         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
52         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
53         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
54         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
55         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
56         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
57         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
58         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
59         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
60         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
61         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
62         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
63         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
64         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
65         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
66         0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
67         0, 0, 0, 0, 0, 0
68 };
69
70 int hook_keyb_handler(void)
71 {
72         SDL_EnableUNICODE(1);
73         memset((void *) last_keys, 0, sizeof(last_keys));
74         return 0;
75
76 }
77
78
79 void remove_keyb_handler(void)
80 {
81 }
82
83
84 int key_pressed(int key)
85 {
86         return keyb[(unsigned char) key];
87 }
88
89 int addkey(unsigned int key)
90 {
91         int c1;
92
93         if (!(key & 0x8000)) {
94                 keyb[key & 0x7fff] = 1;
95                 for (c1 = 48; c1 > 0; c1--)
96                         last_keys[c1] = last_keys[c1 - 1];
97                 last_keys[0] = key & 0x7fff;
98         } else
99                 keyb[key & 0x7fff] = 0;
100         return 0;
101 }
102
103 int intr_sysupdate()
104 {
105         SDL_Event e;
106         int i = 0;
107         static int last_time = 0;
108         int now, time_diff;
109
110         while (SDL_PollEvent(&e)) {
111                 switch (e.type) {
112                 case SDL_MOUSEBUTTONDOWN:
113                         break;
114                 case SDL_KEYDOWN:
115                 case SDL_KEYUP:
116                         switch (e.key.keysym.sym) {
117                         case SDLK_F12:
118                                 if (e.type == SDL_KEYDOWN) {
119                                         SDL_Quit();
120                                         exit(1);
121                                 }
122                                 break;
123                         case SDLK_F10:
124                                 if (e.type == SDL_KEYDOWN) {
125                                         fs_toggle();
126                                 }
127                                 break;
128                         case SDLK_ESCAPE:
129                                 if (e.type == SDL_KEYUP)
130                                         addkey(1 | 0x8000);
131                                 else
132                                         addkey(1 & 0x7f);
133                                 break;
134                         default:
135                                 e.key.keysym.sym &= 0x7f;
136                                 if (e.type == SDL_KEYUP)
137                                         e.key.keysym.sym |= 0x8000;
138                                 addkey(e.key.keysym.sym);
139
140                                 break;
141                         }
142                         break;
143                 default:
144                         break;
145                 }
146                 i++;
147         }
148         SDL_Delay(1);
149         now = SDL_GetTicks();
150         time_diff = now - last_time;
151         if (time_diff>0) {
152                 i = time_diff / (1000 / 60);
153                 if (i) {
154                         last_time = now;
155                 } else {
156                         int tmp;
157
158                         tmp = (1000/60) - i - 10;
159                         if (tmp>0)
160                                 SDL_Delay(tmp);
161                 }
162         }
163 /*
164         if (!then)
165                 SDL_Delay(1);
166         else {
167                 then = (1000 / 60) - (now - then);
168                 if (then > 0 && then < 1000)
169                         SDL_Delay(then);
170         }
171         then = now;
172 */
173
174         return i;
175 }