deepstone
diff src/dosemu/dosemu.c @ 38:17a5107b6fa4
- added perspective correct interpolation
- added recording functionality
- added video capture functionality
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 10 Mar 2014 17:24:42 +0200 |
parents | e234f2a4b6fa |
children |
line diff
1.1 --- a/src/dosemu/dosemu.c Mon Sep 23 07:42:56 2013 +0300 1.2 +++ b/src/dosemu/dosemu.c Mon Mar 10 17:24:42 2014 +0200 1.3 @@ -5,6 +5,7 @@ 1.4 #include <stdlib.h> 1.5 #include <assert.h> 1.6 #include <SDL.h> 1.7 +#include <imago2.h> 1.8 #include "wvga.h" 1.9 #include "conio.h" 1.10 #include "mouse.h" 1.11 @@ -12,6 +13,7 @@ 1.12 #include "timer.h" 1.13 1.14 static void proc_events(void); 1.15 +static void capture_frame(unsigned char *frame); 1.16 1.17 static void init_sdl() 1.18 { 1.19 @@ -29,6 +31,12 @@ 1.20 /* ----- graphics (wvga.c implementation) ----- */ 1.21 static SDL_Surface *fbsurf; 1.22 static int scale = 3; 1.23 +static int frames_to_capture; 1.24 + 1.25 +static struct { 1.26 + unsigned char r, g, b; 1.27 +} palette[256]; 1.28 + 1.29 1.30 int set_video_mode(int mode) 1.31 { 1.32 @@ -99,6 +107,10 @@ 1.33 if(SDL_SetPalette(fbsurf, SDL_LOGPAL | SDL_PHYSPAL, &col, idx, 1) != 1) { 1.34 fprintf(stderr, "set_palette failed to set the required color\n"); 1.35 } 1.36 + 1.37 + palette[idx].r = r; 1.38 + palette[idx].g = g; 1.39 + palette[idx].b = b; 1.40 } 1.41 1.42 void copy_frame(void *pixels) 1.43 @@ -130,6 +142,10 @@ 1.44 } 1.45 SDL_Flip(fbsurf); 1.46 1.47 + if(frames_to_capture > 0) { 1.48 + capture_frame(pixels); 1.49 + --frames_to_capture; 1.50 + } 1.51 1.52 /* also print fps every second ... */ 1.53 { 1.54 @@ -150,10 +166,77 @@ 1.55 } 1.56 } 1.57 1.58 +#define spin_delay(ms) \ 1.59 + do { \ 1.60 + unsigned int end = SDL_GetTicks() + ms; \ 1.61 + while((prev_msec = SDL_GetTicks()) < end); \ 1.62 + } while(0) 1.63 + 1.64 +#define FRAME_INTERVAL (1000/70) 1.65 void wait_vsync(void) 1.66 { 1.67 + static int prev_msec; 1.68 + int msec, dt, tleft; 1.69 + 1.70 + msec = SDL_GetTicks(); 1.71 + 1.72 + dt = msec - prev_msec; 1.73 + 1.74 + tleft = FRAME_INTERVAL - dt; 1.75 + if(tleft > 0) { 1.76 + int coarse = tleft & 0xfffffff8; 1.77 + tleft = tleft & 7; 1.78 + 1.79 + if(coarse) { 1.80 + SDL_Delay(coarse); 1.81 + } 1.82 + if(tleft) { 1.83 + spin_delay(tleft); 1.84 + } else { 1.85 + prev_msec = SDL_GetTicks(); 1.86 + } 1.87 + } else { 1.88 + prev_msec = msec; 1.89 + } 1.90 } 1.91 1.92 +static int cap_count = 0; 1.93 +void begin_capture(int frames) 1.94 +{ 1.95 + frames_to_capture = frames; 1.96 +} 1.97 + 1.98 +void end_capture(void) 1.99 +{ 1.100 + cap_count = 0; 1.101 + frames_to_capture = 0; 1.102 +} 1.103 + 1.104 +#define NUMPIX (320 * 200) 1.105 +static void capture_frame(unsigned char *frame) 1.106 +{ 1.107 + static unsigned char rgbpix[NUMPIX * 4]; 1.108 + char fname[32]; 1.109 + int i; 1.110 + unsigned char *src, *dest; 1.111 + 1.112 + sprintf(fname, "frame%04d.png", cap_count++); 1.113 + 1.114 + src = frame; 1.115 + dest = rgbpix; 1.116 + 1.117 + for(i=0; i<NUMPIX; i++) { 1.118 + unsigned char c = *src++; 1.119 + *dest++ = palette[c].r; 1.120 + *dest++ = palette[c].g; 1.121 + *dest++ = palette[c].b; 1.122 + *dest++ = 255; 1.123 + } 1.124 + 1.125 + img_save_pixels(fname, rgbpix, 320, 200, IMG_FMT_RGBA32); 1.126 +} 1.127 + 1.128 + 1.129 /* ----- event handling (conio.h) ----- */ 1.130 static SDL_Event *keybev; 1.131 static int mousex, mousey, bnmask;