Mercurial > repos > blastem
comparison render_sdl.c @ 20:f664eeb55cb4
Mostly broken VDP core and savestate viewer
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 08 Dec 2012 02:00:54 -0800 |
parents | |
children | 037963b4c92d |
comparison
equal
deleted
inserted
replaced
19:4717146a7606 | 20:f664eeb55cb4 |
---|---|
1 #include <SDL.h> | |
2 #include <stdlib.h> | |
3 #include <stdio.h> | |
4 #include "render.h" | |
5 | |
6 SDL_Surface *screen; | |
7 | |
8 void render_init() | |
9 { | |
10 if (SDL_Init(SDL_INIT_VIDEO) < 0) { | |
11 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); | |
12 exit(1); | |
13 } | |
14 atexit(SDL_Quit); | |
15 screen = SDL_SetVideoMode(320, 240, 32, SDL_SWSURFACE | SDL_ANYFORMAT); | |
16 if (!screen) { | |
17 fprintf(stderr, "Unable to get SDL surface: %s\n", SDL_GetError()); | |
18 exit(1); | |
19 } | |
20 if (screen->format->BytesPerPixel < 2) { | |
21 fprintf(stderr, "BlastEm requires at least a 16-bit surface, SDL returned a %d-bit surface\n", screen->format->BytesPerPixel * 8); | |
22 exit(1); | |
23 } | |
24 } | |
25 | |
26 void render_context(vdp_context * context) | |
27 { | |
28 uint8_t *buf_8; | |
29 uint16_t *buf_16; | |
30 uint32_t *buf_32; | |
31 uint8_t b,g,r; | |
32 if (SDL_MUSTLOCK(screen)) { | |
33 if (SDL_LockSurface(screen) < 0) { | |
34 return; | |
35 } | |
36 } | |
37 switch (screen->format->BytesPerPixel) { | |
38 case 2: | |
39 buf_16 = (uint16_t *)screen->pixels; | |
40 for (int y = 0; y < 240; y++, buf_16 += (screen->pitch/2 - 320)) { | |
41 for (int x = 0; x < 320; x++, buf_16++) { | |
42 uint16_t gen_color = context->framebuf[y * 320 + x]; | |
43 b = ((gen_color >> 8) & 0xE) * 18; | |
44 g = ((gen_color >> 4) & 0xE) * 18; | |
45 r = (gen_color& 0xE) * 18; | |
46 *buf_16 = SDL_MapRGB(screen->format, r, g, b); | |
47 } | |
48 } | |
49 break; | |
50 case 3: | |
51 buf_8 = (uint8_t *)screen->pixels; | |
52 for (int y = 0; y < 240; y++, buf_8 += (screen->pitch - 320)) { | |
53 for (int x = 0; x < 320; x++, buf_8 += 3) { | |
54 uint16_t gen_color = context->framebuf[y * 320 + x]; | |
55 b = ((gen_color >> 8) & 0xE) * 18; | |
56 g = ((gen_color >> 4) & 0xE) * 18; | |
57 r = (gen_color& 0xE) * 18; | |
58 *(buf_8+screen->format->Rshift/8) = r; | |
59 *(buf_8+screen->format->Gshift/8) = g; | |
60 *(buf_8+screen->format->Bshift/8) = b; | |
61 } | |
62 } | |
63 break; | |
64 case 4: | |
65 buf_32 = (uint32_t *)screen->pixels; | |
66 for (int y = 0; y < 224; y++, buf_32 += (screen->pitch/4 - 320)) { | |
67 for (int x = 0; x < 320; x++, buf_32++) { | |
68 uint16_t gen_color = context->framebuf[y * 320 + x]; | |
69 b = ((gen_color >> 8) & 0xE) * 18; | |
70 g = ((gen_color >> 4) & 0xE) * 18; | |
71 r = (gen_color& 0xE) * 18; | |
72 *buf_32 = SDL_MapRGB(screen->format, r, g, b); | |
73 } | |
74 } | |
75 for (int y = 224; y < 240; y++, buf_32 += (screen->pitch/4 - 320)) { | |
76 for (int x = 0; x < 320; x++, buf_32++) { | |
77 uint16_t gen_color = context->cram[x/10 + ((y-224)/8)*32]; | |
78 b = ((gen_color >> 8) & 0xE) * 18; | |
79 g = ((gen_color >> 4) & 0xE) * 18; | |
80 r = (gen_color& 0xE) * 18; | |
81 *buf_32 = SDL_MapRGB(screen->format, r, g, b); | |
82 } | |
83 } | |
84 break; | |
85 } | |
86 if ( SDL_MUSTLOCK(screen) ) { | |
87 SDL_UnlockSurface(screen); | |
88 } | |
89 SDL_UpdateRect(screen, 0, 0, 320, 240); | |
90 } | |
91 | |
92 void render_wait_quit() | |
93 { | |
94 SDL_Event event; | |
95 while(SDL_WaitEvent(&event)) { | |
96 switch (event.type) { | |
97 case SDL_QUIT: | |
98 return; | |
99 } | |
100 } | |
101 } | |
102 |