rayzor
view src/dosemu/dosemu.c @ 5:5fcf72837b69
fixed the dosemu bit
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 06 Apr 2014 02:43:24 +0300 |
parents | |
children | fcd06a15dfdd |
line source
1 /* This file implements all calls made to dos-specific code using SDL */
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <SDL/SDL.h>
5 #ifdef DOSEMU_CAPTURE
6 #include <imago2.h>
7 #endif
8 #include "gfx.h"
9 #include "mouse.h"
10 #include "keyb.h"
11 #include "timer.h"
13 static void proc_events(void);
14 static void capture_frame(unsigned char *frame);
16 static void init_sdl()
17 {
18 const SDL_version *ver;
20 if(!SDL_WasInit(SDL_INIT_EVERYTHING)) {
21 SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER);
23 if((ver = SDL_Linked_Version())) {
24 printf("SDL %d.%d.%d initialized\n", ver->major, ver->minor, ver->patch);
25 }
26 }
27 }
29 /* ----- graphics (gfx.c (vbe) implementation) ----- */
30 static SDL_Surface *fbsurf;
31 static int scale = 1;
32 static int frames_to_capture;
34 static struct {
35 unsigned char r, g, b;
36 } palette[256];
39 void *set_video_mode(int xsz, int ysz, int bpp)
40 {
41 unsigned int sdl_flags = bpp <= 8 ? SDL_HWPALETTE : 0;
42 char *env;
44 if(getenv("DOSEMU_DOUBLESIZE")) {
45 scale = 2;
46 }
48 if((env = getenv("DOSEMU_SCALE"))) {
49 int n = atoi(env);
50 if(n > 0) {
51 scale = n;
52 }
53 }
54 xsz *= scale;
55 ysz *= scale;
57 if(getenv("DOSEMU_FULLSCREEN")) {
58 sdl_flags |= SDL_FULLSCREEN;
59 }
61 init_sdl();
63 if(!(fbsurf = SDL_SetVideoMode(xsz, ysz, bpp, sdl_flags))) {
64 fprintf(stderr, "failed to set video mode\n");
65 abort();
66 }
67 SDL_WM_SetCaption("Rayzor", 0);
68 /*SDL_ShowCursor(0);*/
70 return fbsurf->pixels;
71 }
73 int set_text_mode(void)
74 {
75 SDL_ShowCursor(1);
76 SDL_EnableKeyRepeat(0, 0);
77 SDL_Quit();
78 return 0;
79 }
81 int get_color_depth(void)
82 {
83 return fbsurf->format->BitsPerPixel;
84 }
86 static int count_bits(unsigned int x)
87 {
88 int i, count = 0;
89 for(i=0; i<32; i++) {
90 if(x & 1) {
91 count++;
92 }
93 x >>= 1;
94 }
95 return count;
96 }
98 int get_color_bits(int *rbits, int *gbits, int *bbits)
99 {
100 *rbits = count_bits(fbsurf->format->Rmask);
101 *gbits = count_bits(fbsurf->format->Gmask);
102 *bbits = count_bits(fbsurf->format->Bmask);
103 return *rbits + *gbits + *bbits;
104 }
106 int get_color_shift(int *rshift, int *gshift, int *bshift)
107 {
108 *rshift = fbsurf->format->Rshift;
109 *gshift = fbsurf->format->Gshift;
110 *bshift = fbsurf->format->Bshift;
111 return 0;
112 }
114 int get_color_mask(unsigned int *rmask, unsigned int *gmask, unsigned int *bmask)
115 {
116 *rmask = fbsurf->format->Rmask;
117 *gmask = fbsurf->format->Gmask;
118 *bmask = fbsurf->format->Bmask;
119 return 0;
120 }
123 void set_palette(int idx, int r, int g, int b)
124 {
125 SDL_Color col;
126 col.r = r;
127 col.g = g;
128 col.b = b;
130 if(SDL_SetPalette(fbsurf, SDL_LOGPAL | SDL_PHYSPAL, &col, idx, 1) != 1) {
131 fprintf(stderr, "set_palette failed to set the required color\n");
132 }
134 palette[idx].r = r;
135 palette[idx].g = g;
136 palette[idx].b = b;
137 }
139 static int copy_frame_called;
141 void copy_frame(void *pixels)
142 {
143 unsigned char *frame = (unsigned char*)pixels;
144 copy_frame_called = 1;
146 if(SDL_MUSTLOCK(fbsurf)) {
147 SDL_LockSurface(fbsurf);
148 }
150 if(pixels) {
151 if(scale > 1) {
152 int i, j, xsz, ysz;
153 unsigned char *dest = fbsurf->pixels;
155 xsz = fbsurf->w * scale;
156 ysz = fbsurf->h * scale;
158 for(i=0; i<ysz; i++) {
159 for(j=0; j<xsz; j++) {
160 *dest++ = frame[(i / scale) * fbsurf->w + (j / scale)];
161 }
162 }
163 } else {
164 int num_pixels = fbsurf->w * fbsurf->h;
165 memcpy(fbsurf->pixels, frame, num_pixels * fbsurf->format->BytesPerPixel);
166 }
167 }
169 if(SDL_MUSTLOCK(fbsurf)) {
170 SDL_UnlockSurface(fbsurf);
171 }
173 SDL_Flip(fbsurf);
175 if(frames_to_capture > 0) {
176 capture_frame(pixels);
177 --frames_to_capture;
178 }
180 /* also print fps every second ... */
181 {
182 static long prev_fps, num_frames;
183 long msec, dt;
185 msec = get_msec();
186 dt = msec - prev_fps;
187 if(dt >= 1000) {
188 float fps = (float)num_frames / ((float)dt / 1000.0f);
189 printf("framerate: %.1f \r", fps);
190 fflush(stdout);
191 num_frames = 0;
192 prev_fps = msec;
193 } else {
194 num_frames++;
195 }
196 }
197 }
199 #define spin_delay(ms) \
200 do { \
201 unsigned int end = SDL_GetTicks() + ms; \
202 while((prev_msec = SDL_GetTicks()) < end); \
203 } while(0)
205 #define FRAME_INTERVAL (1000/70)
207 void wait_vsync(void)
208 {
209 static int prev_msec;
210 int msec, dt, tleft;
212 if(!copy_frame_called) {
213 copy_frame(0);
214 copy_frame_called = 0;
215 }
217 msec = SDL_GetTicks();
219 dt = msec - prev_msec;
221 tleft = FRAME_INTERVAL - dt;
222 if(tleft > 0) {
223 int coarse = tleft & 0xfffffff8;
224 tleft = tleft & 7;
226 if(coarse) {
227 SDL_Delay(coarse);
228 }
229 if(tleft) {
230 spin_delay(tleft);
231 } else {
232 prev_msec = SDL_GetTicks();
233 }
234 } else {
235 prev_msec = msec;
236 }
237 }
239 static int cap_count = 0;
240 void begin_capture(int frames)
241 {
242 frames_to_capture = frames;
243 }
245 void end_capture(void)
246 {
247 cap_count = 0;
248 frames_to_capture = 0;
249 }
251 #define NUMPIX (fbsurf->w * fbsurf->h)
252 static void capture_frame(unsigned char *frame)
253 {
254 #ifdef DOSEMU_CAPTURE
255 static unsigned char rgbpix[NUMPIX * 4];
256 char fname[32];
257 int i;
258 unsigned char *src, *dest;
260 sprintf(fname, "frame%04d.png", cap_count++);
262 src = frame;
263 dest = rgbpix;
265 for(i=0; i<NUMPIX; i++) {
266 unsigned char c = *src++;
267 *dest++ = palette[c].r;
268 *dest++ = palette[c].g;
269 *dest++ = palette[c].b;
270 *dest++ = 255;
271 }
273 img_save_pixels(fname, rgbpix, fbsurf->w, fbsurf->h, IMG_FMT_RGBA32);
274 #endif
275 }
278 /* ----- event handling (conio.h) ----- */
279 static SDL_Event *keybev;
280 static int mousex, mousey, bnmask;
282 static int keystate[256];
283 static int num_pressed;
285 int kbhit(void)
286 {
287 if(!keybev) {
288 proc_events();
289 }
290 return keybev != 0;
291 }
293 int getch(void)
294 {
295 int res;
297 while(!keybev) {
298 SDL_Event ev;
299 SDL_WaitEvent(&ev);
300 SDL_PushEvent(&ev);
301 proc_events();
302 }
303 res = keybev->key.keysym.sym;
304 keybev = 0;
305 return res;
306 }
308 /* ----- improved event handling (keyb.h) ---- */
310 int kb_init(int bufsz)
311 {
312 init_sdl();
314 return 0;
315 }
317 void kb_shutdown(void)
318 {
319 }
321 int kb_getkey(void)
322 {
323 int res = -1;
325 proc_events();
326 if(keybev) {
327 res = keybev->key.keysym.sym;
328 keybev = 0;
329 }
330 return res;
331 }
333 int kb_isdown(int key)
334 {
335 if(key == KB_ANY) {
336 return num_pressed;
337 }
338 return keystate[key];
339 }
341 /* mouse handling (mouse.c implementation) */
342 static unsigned long last_mouse_hide_time;
344 int have_mouse(void)
345 {
346 return 1;
347 }
349 void set_mouse(int x, int y)
350 {
351 SDL_ShowCursor(0);
352 last_mouse_hide_time = get_msec();
354 SDL_WarpMouse(x * scale, y * scale);
355 mousex = x;
356 mousey = y;
357 }
359 int read_mouse(int *xp, int *yp)
360 {
361 if(xp) *xp = mousex;
362 if(yp) *yp = mousey;
363 return bnmask;
364 }
366 static void proc_events(void)
367 {
368 static SDL_Event ev;
370 if(last_mouse_hide_time > 0 && get_msec() - last_mouse_hide_time > 3000) {
371 last_mouse_hide_time = 0;
372 SDL_ShowCursor(1);
373 }
375 while(SDL_PollEvent(&ev)) {
376 switch(ev.type) {
377 case SDL_KEYDOWN:
378 {
379 int key = ev.key.keysym.sym;
381 if(!keybev) {
382 keybev = &ev;
383 }
385 if(!keystate[key]) {
386 keystate[key] = 1;
387 num_pressed++;
388 }
389 }
390 break;
392 case SDL_KEYUP:
393 {
394 int key = ev.key.keysym.sym;
396 if(keystate[key]) {
397 keystate[key] = 0;
398 if(--num_pressed < 0) {
399 num_pressed = 0;
400 }
401 }
402 }
403 break;
405 case SDL_MOUSEMOTION:
406 mousex = ev.motion.x / scale;
407 mousey = ev.motion.y / scale;
408 break;
410 case SDL_MOUSEBUTTONDOWN:
411 case SDL_MOUSEBUTTONUP:
412 {
413 int mask = 0;
414 switch(ev.button.button) {
415 case SDL_BUTTON_LEFT:
416 mask = MOUSE_LEFT;
417 break;
418 case SDL_BUTTON_MIDDLE:
419 mask = MOUSE_MIDDLE;
420 break;
421 case SDL_BUTTON_RIGHT:
422 mask = MOUSE_RIGHT;
423 default:
424 break;
425 }
426 if(!mask) {
427 break;
428 }
430 if(ev.button.state == SDL_PRESSED) {
431 bnmask |= mask;
432 } else {
433 bnmask &= ~mask;
434 }
435 }
436 break;
438 default:
439 break;
440 }
441 }
442 }
444 /* ---- timer.c implementation ---- */
445 static Uint32 start_time;
447 void init_timer(int res_hz)
448 {
449 init_sdl();
450 reset_timer();
451 }
453 void reset_timer(void)
454 {
455 start_time = SDL_GetTicks();
456 printf("resetting timer: %u, %lu\n", start_time, get_msec());
457 }
459 unsigned long get_msec(void)
460 {
461 Uint32 ticks = SDL_GetTicks();
462 return (unsigned long)(ticks - start_time);
463 }