Mercurial > repos > blastem
comparison render_sdl.c @ 229:d60837a7d18a
Improve color rendering accuracy and optimize SDL renderer a bit
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sun, 21 Apr 2013 19:12:48 -0700 |
parents | 209a37eed3e7 |
children | d3266cee02c9 |
comparison
equal
deleted
inserted
replaced
228:1ed81ef2a3a2 | 229:d60837a7d18a |
---|---|
7 SDL_Surface *screen; | 7 SDL_Surface *screen; |
8 uint8_t render_dbg = 0; | 8 uint8_t render_dbg = 0; |
9 uint8_t debug_pal = 0; | 9 uint8_t debug_pal = 0; |
10 | 10 |
11 uint32_t last_frame = 0; | 11 uint32_t last_frame = 0; |
12 | |
13 int32_t color_map[1 << 12]; | |
14 uint8_t levels[] = {0, 27, 49, 71, 87, 103, 119, 130, 146, 157, 174, 190, 206, 228, 255}; | |
15 | |
16 uint32_t min_delay; | |
12 | 17 |
13 void render_init(int width, int height) | 18 void render_init(int width, int height) |
14 { | 19 { |
15 if (SDL_Init(SDL_INIT_VIDEO) < 0) { | 20 if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
16 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); | 21 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); |
25 } | 30 } |
26 if (screen->format->BytesPerPixel < 2) { | 31 if (screen->format->BytesPerPixel < 2) { |
27 fprintf(stderr, "BlastEm requires at least a 16-bit surface, SDL returned a %d-bit surface\n", screen->format->BytesPerPixel * 8); | 32 fprintf(stderr, "BlastEm requires at least a 16-bit surface, SDL returned a %d-bit surface\n", screen->format->BytesPerPixel * 8); |
28 exit(1); | 33 exit(1); |
29 } | 34 } |
35 uint8_t b,g,r; | |
36 for (uint16_t color = 0; color < (1 << 12); color++) { | |
37 b = levels[(color >> 8) & 0xE]; | |
38 g = levels[(color >> 4) & 0xE]; | |
39 r = levels[color & 0xE]; | |
40 color_map[color] = SDL_MapRGB(screen->format, r, g, b); | |
41 } | |
42 min_delay = 0; | |
43 for (int i = 0; i < 20; i++) { | |
44 uint32_t start = SDL_GetTicks(); | |
45 SDL_Delay(1); | |
46 uint32_t delay = SDL_GetTicks()-start; | |
47 if (delay > min_delay) { | |
48 min_delay = delay; | |
49 } | |
50 } | |
51 if (!min_delay) { | |
52 min_delay = 1; | |
53 } | |
54 printf("minimum delay: %d\n", min_delay); | |
30 } | 55 } |
31 | 56 |
32 void render_context(vdp_context * context) | 57 void render_context(vdp_context * context) |
33 { | 58 { |
34 uint8_t *buf_8; | 59 uint8_t *buf_8; |
52 case 2: | 77 case 2: |
53 buf_16 = (uint16_t *)screen->pixels; | 78 buf_16 = (uint16_t *)screen->pixels; |
54 for (int y = 0; y < 240; y++) { | 79 for (int y = 0; y < 240; y++) { |
55 for (int i = 0; i < repeat_y; i++,buf_16 += screen->pitch/2) { | 80 for (int i = 0; i < repeat_y; i++,buf_16 += screen->pitch/2) { |
56 uint16_t *line = buf_16; | 81 uint16_t *line = buf_16; |
82 uint16_t *src_line = context->framebuf + y * 320; | |
57 for (int x = 0; x < 320; x++) { | 83 for (int x = 0; x < 320; x++) { |
58 uint16_t gen_color = context->framebuf[y * 320 + x]; | 84 uint16_t color = color_map[*(src_line++) & 0xFFF]; |
59 b = ((gen_color >> 8) & 0xE) * 18; | |
60 g = ((gen_color >> 4) & 0xE) * 18; | |
61 r = (gen_color& 0xE) * 18; | |
62 for (int j = 0; j < repeat_x; j++) { | 85 for (int j = 0; j < repeat_x; j++) { |
63 *(line++) = SDL_MapRGB(screen->format, r, g, b); | 86 *(line++) = color; |
64 } | 87 } |
65 } | 88 } |
66 } | 89 } |
67 } | 90 } |
68 break; | 91 break; |
90 buf_32 = (uint32_t *)screen->pixels; | 113 buf_32 = (uint32_t *)screen->pixels; |
91 | 114 |
92 for (int y = 0; y < 240; y++) { | 115 for (int y = 0; y < 240; y++) { |
93 for (int i = 0; i < repeat_y; i++,buf_32 += screen->pitch/4) { | 116 for (int i = 0; i < repeat_y; i++,buf_32 += screen->pitch/4) { |
94 uint32_t *line = buf_32; | 117 uint32_t *line = buf_32; |
95 for (int x = 0; x < 320; x++) { | 118 uint16_t *src_line = context->framebuf + y * 320; |
96 uint16_t gen_color = context->framebuf[y * 320 + x]; | 119 for (int x = 0; x < 320; x++) { |
97 if (render_dbg == 1) { | 120 uint32_t color; |
121 if (!render_dbg) { | |
122 color = color_map[*(src_line++) & 0xFFF]; | |
123 } else if(render_dbg == 2) { | |
124 color = color_map[context->cram[(y/30)*8 + x/40]]; | |
125 } else if(render_dbg == 3) { | |
126 if (x & 1) { | |
127 color = color_map[context->cram[ (debug_pal << 4) | (context->vdpmem[(x/8)*32 + (y/8)*32*40 + (x%8)/2 + (y%8)*4] & 0xF) ]]; | |
128 } else { | |
129 color = color_map[context->cram[ (debug_pal << 4) | (context->vdpmem[(x/8)*32 + (y/8)*32*40 + (x%8)/2 + (y%8)*4] >> 4) ]]; | |
130 } | |
131 }else { | |
132 uint16_t gen_color = context->framebuf[y * 320 + x]; | |
98 r = g = b = 0; | 133 r = g = b = 0; |
99 switch(gen_color & FBUF_SRC_MASK) | 134 switch(gen_color & FBUF_SRC_MASK) |
100 { | 135 { |
101 case FBUF_SRC_A: | 136 case FBUF_SRC_A: |
102 g = 127; | 137 g = 127; |
118 if (gen_color & FBUF_BIT_PRIORITY) { | 153 if (gen_color & FBUF_BIT_PRIORITY) { |
119 b *= 2; | 154 b *= 2; |
120 g *= 2; | 155 g *= 2; |
121 r *= 2; | 156 r *= 2; |
122 } | 157 } |
123 } else { | 158 color = SDL_MapRGB(screen->format, r, g, b); |
124 if (render_dbg == 2) { | |
125 gen_color = context->cram[(y/30)*8 + x/40]; | |
126 } else if(render_dbg == 3) { | |
127 if (x & 1) { | |
128 gen_color = context->cram[ (debug_pal << 4) | (context->vdpmem[(x/8)*32 + (y/8)*32*40 + (x%8)/2 + (y%8)*4] & 0xF) ]; | |
129 } else { | |
130 gen_color = context->cram[ (debug_pal << 4) | (context->vdpmem[(x/8)*32 + (y/8)*32*40 + (x%8)/2 + (y%8)*4] >> 4) ]; | |
131 } | |
132 } | |
133 b = ((gen_color >> 8) & 0xE) * 18; | |
134 g = ((gen_color >> 4) & 0xE) * 18; | |
135 r = (gen_color& 0xE) * 18; | |
136 } | 159 } |
137 for (int j = 0; j < repeat_x; j++) { | 160 for (int j = 0; j < repeat_x; j++) { |
138 *(line++) = SDL_MapRGB(screen->format, r, g, b); | 161 *(line++) = color; |
139 } | 162 } |
140 } | 163 } |
141 } | 164 } |
142 } | 165 } |
143 break; | 166 break; |
185 #define BUTTON_B 0x10 | 208 #define BUTTON_B 0x10 |
186 #define BUTTON_START 0x20 | 209 #define BUTTON_START 0x20 |
187 #define BUTTON_C 0x20 | 210 #define BUTTON_C 0x20 |
188 | 211 |
189 #define FRAME_DELAY 16 | 212 #define FRAME_DELAY 16 |
190 #define MIN_DELAY 10 | 213 #define MIN_DELAY 5 |
191 uint32_t frame_counter = 0; | 214 uint32_t frame_counter = 0; |
192 uint32_t start = 0; | 215 uint32_t start = 0; |
193 int wait_render_frame(vdp_context * context) | 216 int wait_render_frame(vdp_context * context) |
194 { | 217 { |
195 FILE * outfile; | 218 FILE * outfile; |
323 uint32_t delay = last_frame + FRAME_DELAY - current; | 346 uint32_t delay = last_frame + FRAME_DELAY - current; |
324 //TODO: Calculate MIN_DELAY at runtime | 347 //TODO: Calculate MIN_DELAY at runtime |
325 if (delay > MIN_DELAY) { | 348 if (delay > MIN_DELAY) { |
326 SDL_Delay((delay/MIN_DELAY)*MIN_DELAY); | 349 SDL_Delay((delay/MIN_DELAY)*MIN_DELAY); |
327 } | 350 } |
328 while ((desired) < SDL_GetTicks()) { | 351 while ((desired) >= SDL_GetTicks()) { |
329 } | 352 } |
330 } | 353 } |
331 render_context(context); | 354 render_context(context); |
355 | |
332 /* | 356 /* |
333 //TODO: Figure out why this causes segfaults | 357 //TODO: Figure out why this causes segfaults |
334 frame_counter++; | 358 frame_counter++; |
335 if ((last_frame - start) > 1000) { | 359 if ((last_frame - start) > 1000) { |
336 if (start) { | 360 if (start) { |