rev |
line source |
nuclear@0
|
1 /* This file implements all calls made to dos-specific code using SDL
|
nuclear@0
|
2 * Don't ask why ...
|
nuclear@0
|
3 */
|
nuclear@0
|
4
|
nuclear@0
|
5 #include <stdlib.h>
|
nuclear@0
|
6 #include <assert.h>
|
nuclear@0
|
7 #include <SDL.h>
|
nuclear@24
|
8 #include "wvga.h"
|
nuclear@0
|
9 #include "conio.h"
|
nuclear@0
|
10 #include "mouse.h"
|
nuclear@34
|
11 #include "keyb.h"
|
nuclear@0
|
12 #include "timer.h"
|
nuclear@0
|
13
|
nuclear@0
|
14 static void proc_events(void);
|
nuclear@0
|
15
|
nuclear@34
|
16 static void init_sdl()
|
nuclear@34
|
17 {
|
nuclear@34
|
18 if(!SDL_WasInit(SDL_INIT_EVERYTHING)) {
|
nuclear@34
|
19 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
|
nuclear@34
|
20 }
|
nuclear@34
|
21 }
|
nuclear@34
|
22
|
nuclear@24
|
23 /* ----- graphics (wvga.c implementation) ----- */
|
nuclear@0
|
24 static SDL_Surface *fbsurf;
|
nuclear@28
|
25 static int scale = 1;
|
nuclear@0
|
26
|
nuclear@24
|
27 int set_video_mode(int mode)
|
nuclear@0
|
28 {
|
nuclear@0
|
29 int resx = 320, resy = 200;
|
nuclear@9
|
30 unsigned int sdl_flags = SDL_HWPALETTE;
|
nuclear@28
|
31 char *env;
|
nuclear@0
|
32
|
nuclear@0
|
33 if(getenv("DOSEMU_DOUBLESIZE")) {
|
nuclear@28
|
34 scale = 2;
|
nuclear@0
|
35 }
|
nuclear@0
|
36
|
nuclear@28
|
37 if((env = getenv("DOSEMU_SCALE"))) {
|
nuclear@28
|
38 int n = atoi(env);
|
nuclear@28
|
39 if(n > 0) {
|
nuclear@28
|
40 scale = n;
|
nuclear@28
|
41 }
|
nuclear@28
|
42 }
|
nuclear@28
|
43 resx *= scale;
|
nuclear@28
|
44 resy *= scale;
|
nuclear@28
|
45
|
nuclear@9
|
46 if(getenv("DOSEMU_FULLSCREEN")) {
|
nuclear@9
|
47 sdl_flags |= SDL_FULLSCREEN;
|
nuclear@9
|
48 }
|
nuclear@9
|
49
|
nuclear@34
|
50 init_sdl();
|
nuclear@34
|
51
|
nuclear@0
|
52 switch(mode) {
|
nuclear@0
|
53 case 0x13:
|
nuclear@9
|
54 if(!(fbsurf = SDL_SetVideoMode(resx, resy, 8, sdl_flags))) {
|
nuclear@0
|
55 fprintf(stderr, "failed to set video mode\n");
|
nuclear@0
|
56 abort();
|
nuclear@0
|
57 }
|
nuclear@25
|
58 SDL_WM_SetCaption("Deepstone", 0);
|
nuclear@28
|
59 /*SDL_ShowCursor(0);*/
|
nuclear@34
|
60 /*SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);*/
|
nuclear@0
|
61 break;
|
nuclear@0
|
62
|
nuclear@0
|
63 case 3:
|
nuclear@0
|
64 SDL_ShowCursor(1);
|
nuclear@11
|
65 SDL_EnableKeyRepeat(0, 0);
|
nuclear@0
|
66 SDL_Quit();
|
nuclear@0
|
67 break;
|
nuclear@0
|
68
|
nuclear@0
|
69 default:
|
nuclear@0
|
70 break;
|
nuclear@0
|
71 }
|
nuclear@24
|
72
|
nuclear@24
|
73 return 0;
|
nuclear@0
|
74 }
|
nuclear@0
|
75
|
nuclear@24
|
76 void set_palette(int idx, int *col, int count)
|
nuclear@24
|
77 {
|
nuclear@24
|
78 int i;
|
nuclear@24
|
79
|
nuclear@24
|
80 for(i=0; i<count; i++) {
|
nuclear@24
|
81 set_pal_entry(idx + i, col[0], col[1], col[2]);
|
nuclear@24
|
82 col += 3;
|
nuclear@24
|
83 }
|
nuclear@24
|
84 }
|
nuclear@24
|
85
|
nuclear@24
|
86 void set_pal_entry(int idx, int r, int g, int b)
|
nuclear@0
|
87 {
|
nuclear@0
|
88 SDL_Color col;
|
nuclear@0
|
89 col.r = r;
|
nuclear@0
|
90 col.g = g;
|
nuclear@0
|
91 col.b = b;
|
nuclear@0
|
92
|
nuclear@24
|
93 if(SDL_SetPalette(fbsurf, SDL_LOGPAL | SDL_PHYSPAL, &col, idx, 1) != 1) {
|
nuclear@0
|
94 fprintf(stderr, "set_palette failed to set the required color\n");
|
nuclear@0
|
95 }
|
nuclear@0
|
96 }
|
nuclear@0
|
97
|
nuclear@24
|
98 void copy_frame(void *pixels)
|
nuclear@0
|
99 {
|
nuclear@24
|
100 unsigned char *frame = (unsigned char*)pixels;
|
nuclear@24
|
101
|
nuclear@0
|
102 if(SDL_MUSTLOCK(fbsurf)) {
|
nuclear@0
|
103 SDL_LockSurface(fbsurf);
|
nuclear@0
|
104 }
|
nuclear@0
|
105
|
nuclear@28
|
106 if(scale > 1) {
|
nuclear@28
|
107 int i, j, xsz, ysz;
|
nuclear@28
|
108 unsigned char *dest = fbsurf->pixels;
|
nuclear@0
|
109
|
nuclear@28
|
110 xsz = 320 * scale;
|
nuclear@28
|
111 ysz = 200 * scale;
|
nuclear@28
|
112
|
nuclear@28
|
113 for(i=0; i<ysz; i++) {
|
nuclear@28
|
114 for(j=0; j<xsz; j++) {
|
nuclear@28
|
115 *dest++ = frame[(i / scale) * 320 + (j / scale)];
|
nuclear@0
|
116 }
|
nuclear@0
|
117 }
|
nuclear@0
|
118 } else {
|
nuclear@0
|
119 memcpy(fbsurf->pixels, frame, 64000);
|
nuclear@0
|
120 }
|
nuclear@0
|
121
|
nuclear@0
|
122 if(SDL_MUSTLOCK(fbsurf)) {
|
nuclear@0
|
123 SDL_UnlockSurface(fbsurf);
|
nuclear@0
|
124 }
|
nuclear@0
|
125 SDL_Flip(fbsurf);
|
nuclear@34
|
126
|
nuclear@34
|
127
|
nuclear@34
|
128 /* also print fps every second ... */
|
nuclear@34
|
129 {
|
nuclear@34
|
130 static long prev_fps, num_frames;
|
nuclear@34
|
131 long msec, dt;
|
nuclear@34
|
132
|
nuclear@34
|
133 msec = get_msec();
|
nuclear@34
|
134 dt = msec - prev_fps;
|
nuclear@34
|
135 if(dt >= 1000) {
|
nuclear@34
|
136 float fps = (float)num_frames / ((float)dt / 1000.0f);
|
nuclear@34
|
137 printf("framerate: %.1f \r", fps);
|
nuclear@34
|
138 fflush(stdout);
|
nuclear@34
|
139 num_frames = 0;
|
nuclear@34
|
140 prev_fps = msec;
|
nuclear@34
|
141 } else {
|
nuclear@34
|
142 num_frames++;
|
nuclear@34
|
143 }
|
nuclear@34
|
144 }
|
nuclear@0
|
145 }
|
nuclear@0
|
146
|
nuclear@0
|
147 void wait_vsync(void)
|
nuclear@0
|
148 {
|
nuclear@0
|
149 }
|
nuclear@0
|
150
|
nuclear@0
|
151 /* ----- event handling (conio.h) ----- */
|
nuclear@0
|
152 static SDL_Event *keybev;
|
nuclear@0
|
153 static int mousex, mousey, bnmask;
|
nuclear@0
|
154
|
nuclear@34
|
155 static int keystate[256];
|
nuclear@34
|
156 static int num_pressed;
|
nuclear@34
|
157
|
nuclear@0
|
158 int kbhit(void)
|
nuclear@0
|
159 {
|
nuclear@0
|
160 if(!keybev) {
|
nuclear@0
|
161 proc_events();
|
nuclear@0
|
162 }
|
nuclear@0
|
163 return keybev != 0;
|
nuclear@0
|
164 }
|
nuclear@0
|
165
|
nuclear@9
|
166 int getch(void)
|
nuclear@0
|
167 {
|
nuclear@9
|
168 int res;
|
nuclear@0
|
169
|
nuclear@0
|
170 while(!keybev) {
|
nuclear@0
|
171 SDL_Event ev;
|
nuclear@0
|
172 SDL_WaitEvent(&ev);
|
nuclear@0
|
173 SDL_PushEvent(&ev);
|
nuclear@0
|
174 proc_events();
|
nuclear@0
|
175 }
|
nuclear@0
|
176 res = keybev->key.keysym.sym;
|
nuclear@0
|
177 keybev = 0;
|
nuclear@0
|
178 return res;
|
nuclear@0
|
179 }
|
nuclear@0
|
180
|
nuclear@34
|
181 /* ----- improved event handling (keyb.h) ---- */
|
nuclear@34
|
182
|
nuclear@34
|
183 int kb_init(int bufsz)
|
nuclear@34
|
184 {
|
nuclear@34
|
185 init_sdl();
|
nuclear@34
|
186
|
nuclear@34
|
187 return 0;
|
nuclear@34
|
188 }
|
nuclear@34
|
189
|
nuclear@34
|
190 void kb_shutdown(void)
|
nuclear@34
|
191 {
|
nuclear@34
|
192 }
|
nuclear@34
|
193
|
nuclear@34
|
194 int kb_getkey(void)
|
nuclear@34
|
195 {
|
nuclear@34
|
196 int res = -1;
|
nuclear@34
|
197
|
nuclear@34
|
198 proc_events();
|
nuclear@34
|
199 if(keybev) {
|
nuclear@34
|
200 res = keybev->key.keysym.sym;
|
nuclear@34
|
201 keybev = 0;
|
nuclear@34
|
202 }
|
nuclear@34
|
203 return res;
|
nuclear@34
|
204 }
|
nuclear@34
|
205
|
nuclear@34
|
206 int kb_isdown(int key)
|
nuclear@34
|
207 {
|
nuclear@34
|
208 if(key == KB_ANY) {
|
nuclear@34
|
209 return num_pressed;
|
nuclear@34
|
210 }
|
nuclear@34
|
211 return keystate[key];
|
nuclear@34
|
212 }
|
nuclear@34
|
213
|
nuclear@0
|
214 /* mouse handling (mouse.c implementation) */
|
nuclear@34
|
215 static unsigned long last_mouse_hide_time;
|
nuclear@34
|
216
|
nuclear@0
|
217 int have_mouse(void)
|
nuclear@0
|
218 {
|
nuclear@0
|
219 return 1;
|
nuclear@0
|
220 }
|
nuclear@0
|
221
|
nuclear@34
|
222 void set_mouse(int x, int y)
|
nuclear@34
|
223 {
|
nuclear@34
|
224 SDL_ShowCursor(0);
|
nuclear@34
|
225 last_mouse_hide_time = get_msec();
|
nuclear@34
|
226
|
nuclear@34
|
227 SDL_WarpMouse(x * scale, y * scale);
|
nuclear@34
|
228 mousex = x;
|
nuclear@34
|
229 mousey = y;
|
nuclear@34
|
230 }
|
nuclear@34
|
231
|
nuclear@0
|
232 int read_mouse(int *xp, int *yp)
|
nuclear@0
|
233 {
|
nuclear@0
|
234 if(xp) *xp = mousex;
|
nuclear@0
|
235 if(yp) *yp = mousey;
|
nuclear@0
|
236 return bnmask;
|
nuclear@0
|
237 }
|
nuclear@0
|
238
|
nuclear@0
|
239 static void proc_events(void)
|
nuclear@0
|
240 {
|
nuclear@0
|
241 static SDL_Event ev;
|
nuclear@0
|
242
|
nuclear@34
|
243 if(last_mouse_hide_time > 0 && get_msec() - last_mouse_hide_time > 3000) {
|
nuclear@34
|
244 last_mouse_hide_time = 0;
|
nuclear@34
|
245 SDL_ShowCursor(1);
|
nuclear@34
|
246 }
|
nuclear@34
|
247
|
nuclear@0
|
248 while(SDL_PollEvent(&ev)) {
|
nuclear@0
|
249 switch(ev.type) {
|
nuclear@0
|
250 case SDL_KEYDOWN:
|
nuclear@34
|
251 {
|
nuclear@34
|
252 int key = ev.key.keysym.sym;
|
nuclear@34
|
253
|
nuclear@34
|
254 if(!keybev) {
|
nuclear@34
|
255 keybev = &ev;
|
nuclear@34
|
256 }
|
nuclear@34
|
257
|
nuclear@34
|
258 if(!keystate[key]) {
|
nuclear@34
|
259 keystate[key] = 1;
|
nuclear@34
|
260 num_pressed++;
|
nuclear@34
|
261 }
|
nuclear@34
|
262 }
|
nuclear@34
|
263 break;
|
nuclear@34
|
264
|
nuclear@34
|
265 case SDL_KEYUP:
|
nuclear@34
|
266 {
|
nuclear@34
|
267 int key = ev.key.keysym.sym;
|
nuclear@34
|
268
|
nuclear@34
|
269 if(keystate[key]) {
|
nuclear@34
|
270 keystate[key] = 0;
|
nuclear@34
|
271 if(--num_pressed < 0) {
|
nuclear@34
|
272 num_pressed = 0;
|
nuclear@34
|
273 }
|
nuclear@34
|
274 }
|
nuclear@34
|
275 }
|
nuclear@34
|
276 break;
|
nuclear@0
|
277
|
nuclear@0
|
278 case SDL_MOUSEMOTION:
|
nuclear@28
|
279 mousex = ev.motion.x / scale;
|
nuclear@28
|
280 mousey = ev.motion.y / scale;
|
nuclear@0
|
281 break;
|
nuclear@0
|
282
|
nuclear@0
|
283 case SDL_MOUSEBUTTONDOWN:
|
nuclear@0
|
284 case SDL_MOUSEBUTTONUP:
|
nuclear@0
|
285 {
|
nuclear@0
|
286 int mask = 0;
|
nuclear@0
|
287 switch(ev.button.button) {
|
nuclear@0
|
288 case SDL_BUTTON_LEFT:
|
nuclear@0
|
289 mask = MOUSE_LEFT;
|
nuclear@0
|
290 break;
|
nuclear@0
|
291 case SDL_BUTTON_MIDDLE:
|
nuclear@0
|
292 mask = MOUSE_MIDDLE;
|
nuclear@0
|
293 break;
|
nuclear@0
|
294 case SDL_BUTTON_RIGHT:
|
nuclear@0
|
295 mask = MOUSE_RIGHT;
|
nuclear@0
|
296 default:
|
nuclear@0
|
297 break;
|
nuclear@0
|
298 }
|
nuclear@0
|
299 if(!mask) {
|
nuclear@0
|
300 break;
|
nuclear@0
|
301 }
|
nuclear@0
|
302
|
nuclear@0
|
303 if(ev.button.state == SDL_PRESSED) {
|
nuclear@0
|
304 bnmask |= mask;
|
nuclear@0
|
305 } else {
|
nuclear@0
|
306 bnmask &= ~mask;
|
nuclear@0
|
307 }
|
nuclear@0
|
308 }
|
nuclear@0
|
309 break;
|
nuclear@0
|
310
|
nuclear@0
|
311 default:
|
nuclear@0
|
312 break;
|
nuclear@0
|
313 }
|
nuclear@0
|
314 }
|
nuclear@0
|
315 }
|
nuclear@0
|
316
|
nuclear@0
|
317 /* ---- timer.c implementation ---- */
|
nuclear@0
|
318 static Uint32 start_time;
|
nuclear@0
|
319
|
nuclear@0
|
320 void init_timer(int res_hz)
|
nuclear@0
|
321 {
|
nuclear@34
|
322 init_sdl();
|
nuclear@0
|
323 reset_timer();
|
nuclear@0
|
324 }
|
nuclear@0
|
325
|
nuclear@0
|
326 void reset_timer(void)
|
nuclear@0
|
327 {
|
nuclear@0
|
328 start_time = SDL_GetTicks();
|
nuclear@34
|
329 printf("resetting timer: %u, %lu\n", start_time, get_msec());
|
nuclear@0
|
330 }
|
nuclear@0
|
331
|
nuclear@0
|
332 unsigned long get_msec(void)
|
nuclear@0
|
333 {
|
nuclear@34
|
334 Uint32 ticks = SDL_GetTicks();
|
nuclear@34
|
335 return (unsigned long)(ticks - start_time);
|
nuclear@0
|
336 }
|