Mercurial > repos > blastem
comparison render_sdl.c @ 33:2e15fa26fe58
Add support for simple resolution scaling
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Sat, 08 Dec 2012 22:07:25 -0800 |
parents | 037963b4c92d |
children | 3fc57e1a2c56 |
comparison
equal
deleted
inserted
replaced
32:8602ad493794 | 33:2e15fa26fe58 |
---|---|
3 #include <stdio.h> | 3 #include <stdio.h> |
4 #include "render.h" | 4 #include "render.h" |
5 | 5 |
6 SDL_Surface *screen; | 6 SDL_Surface *screen; |
7 | 7 |
8 void render_init() | 8 void render_init(int width, int height) |
9 { | 9 { |
10 if (SDL_Init(SDL_INIT_VIDEO) < 0) { | 10 if (SDL_Init(SDL_INIT_VIDEO) < 0) { |
11 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); | 11 fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); |
12 exit(1); | 12 exit(1); |
13 } | 13 } |
14 atexit(SDL_Quit); | 14 atexit(SDL_Quit); |
15 screen = SDL_SetVideoMode(320, 240, 32, SDL_SWSURFACE | SDL_ANYFORMAT); | 15 printf("width: %d, height: %d\n", width, height); |
16 screen = SDL_SetVideoMode(width, height, 32, SDL_SWSURFACE | SDL_ANYFORMAT); | |
16 if (!screen) { | 17 if (!screen) { |
17 fprintf(stderr, "Unable to get SDL surface: %s\n", SDL_GetError()); | 18 fprintf(stderr, "Unable to get SDL surface: %s\n", SDL_GetError()); |
18 exit(1); | 19 exit(1); |
19 } | 20 } |
20 if (screen->format->BytesPerPixel < 2) { | 21 if (screen->format->BytesPerPixel < 2) { |
32 if (SDL_MUSTLOCK(screen)) { | 33 if (SDL_MUSTLOCK(screen)) { |
33 if (SDL_LockSurface(screen) < 0) { | 34 if (SDL_LockSurface(screen) < 0) { |
34 return; | 35 return; |
35 } | 36 } |
36 } | 37 } |
38 uint16_t repeat_x = screen->clip_rect.w / 320; | |
39 uint16_t repeat_y = screen->clip_rect.h / 240; | |
40 if (repeat_x > repeat_y) { | |
41 repeat_x = repeat_y; | |
42 } else { | |
43 repeat_y = repeat_x; | |
44 } | |
45 printf("w: %d, h: %d, repeat_x: %d, repeat_y: %d\n", screen->clip_rect.w, screen->clip_rect.h, repeat_x, repeat_y); | |
37 switch (screen->format->BytesPerPixel) { | 46 switch (screen->format->BytesPerPixel) { |
38 case 2: | 47 case 2: |
39 buf_16 = (uint16_t *)screen->pixels; | 48 buf_16 = (uint16_t *)screen->pixels; |
40 for (int y = 0; y < 240; y++, buf_16 += (screen->pitch/2 - 320)) { | 49 for (int y = 0; y < 240; y++) { |
41 for (int x = 0; x < 320; x++, buf_16++) { | 50 for (int i = 0; i < repeat_y; i++,buf_16 += screen->pitch/2) { |
42 uint16_t gen_color = context->framebuf[y * 320 + x]; | 51 uint16_t *line = buf_16; |
43 b = ((gen_color >> 8) & 0xE) * 18; | 52 for (int x = 0; x < 320; x++) { |
44 g = ((gen_color >> 4) & 0xE) * 18; | 53 uint16_t gen_color = context->framebuf[y * 320 + x]; |
45 r = (gen_color& 0xE) * 18; | 54 b = ((gen_color >> 8) & 0xE) * 18; |
46 *buf_16 = SDL_MapRGB(screen->format, r, g, b); | 55 g = ((gen_color >> 4) & 0xE) * 18; |
47 } | 56 r = (gen_color& 0xE) * 18; |
57 for (int j = 0; j < repeat_x; j++) { | |
58 *(line++) = SDL_MapRGB(screen->format, r, g, b); | |
59 } | |
60 } | |
61 } | |
48 } | 62 } |
49 break; | 63 break; |
50 case 3: | 64 case 3: |
51 buf_8 = (uint8_t *)screen->pixels; | 65 buf_8 = (uint8_t *)screen->pixels; |
52 for (int y = 0; y < 240; y++, buf_8 += (screen->pitch - 320)) { | 66 for (int y = 0; y < 240; y++) { |
53 for (int x = 0; x < 320; x++, buf_8 += 3) { | 67 for (int i = 0; i < repeat_y; i++,buf_8 += screen->pitch) { |
54 uint16_t gen_color = context->framebuf[y * 320 + x]; | 68 uint8_t *line = buf_8; |
55 b = ((gen_color >> 8) & 0xE) * 18; | 69 for (int x = 0; x < 320; x++) { |
56 g = ((gen_color >> 4) & 0xE) * 18; | 70 uint16_t gen_color = context->framebuf[y * 320 + x]; |
57 r = (gen_color& 0xE) * 18; | 71 b = ((gen_color >> 8) & 0xE) * 18; |
58 *(buf_8+screen->format->Rshift/8) = r; | 72 g = ((gen_color >> 4) & 0xE) * 18; |
59 *(buf_8+screen->format->Gshift/8) = g; | 73 r = (gen_color& 0xE) * 18; |
60 *(buf_8+screen->format->Bshift/8) = b; | 74 for (int j = 0; j < repeat_x; j++) { |
61 } | 75 *(buf_8+screen->format->Rshift/8) = r; |
76 *(buf_8+screen->format->Gshift/8) = g; | |
77 *(buf_8+screen->format->Bshift/8) = b; | |
78 buf_8 += 3; | |
79 } | |
80 } | |
81 } | |
62 } | 82 } |
63 break; | 83 break; |
64 case 4: | 84 case 4: |
65 buf_32 = (uint32_t *)screen->pixels; | 85 buf_32 = (uint32_t *)screen->pixels; |
66 for (int y = 0; y < 224; y++, buf_32 += (screen->pitch/4 - 320)) { | 86 for (int y = 0; y < 240; y++) { |
67 for (int x = 0; x < 320; x++, buf_32++) { | 87 for (int i = 0; i < repeat_y; i++,buf_32 += screen->pitch/4) { |
68 uint16_t gen_color = context->framebuf[y * 320 + x]; | 88 uint32_t *line = buf_32; |
69 b = ((gen_color >> 8) & 0xE) * 18; | 89 for (int x = 0; x < 320; x++) { |
70 g = ((gen_color >> 4) & 0xE) * 18; | 90 uint16_t gen_color = context->framebuf[y * 320 + x]; |
71 r = (gen_color& 0xE) * 18; | 91 b = ((gen_color >> 8) & 0xE) * 18; |
72 *buf_32 = SDL_MapRGB(screen->format, r, g, b); | 92 g = ((gen_color >> 4) & 0xE) * 18; |
73 } | 93 r = (gen_color& 0xE) * 18; |
74 } | 94 for (int j = 0; j < repeat_x; j++) { |
75 for (int y = 224; y < 240; y++, buf_32 += (screen->pitch/4 - 320)) { | 95 *(line++) = SDL_MapRGB(screen->format, r, g, b); |
76 for (int x = 0; x < 256; x++, buf_32++) { | 96 } |
77 uint16_t gen_color = context->cram[x/8 + ((y-224)/8)*32]; | 97 } |
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 for (int x = 256; x < 320; x++, buf_32++) { | |
84 if ((x/8 & 1) ^ (y/8 & 1)) { | |
85 b = g = r = 255; | |
86 } else { | |
87 b = g = r = 0; | |
88 } | |
89 *buf_32 = SDL_MapRGB(screen->format, r, g, b); | |
90 } | 98 } |
91 } | 99 } |
92 break; | 100 break; |
93 } | 101 } |
94 if ( SDL_MUSTLOCK(screen) ) { | 102 if ( SDL_MUSTLOCK(screen) ) { |
95 SDL_UnlockSurface(screen); | 103 SDL_UnlockSurface(screen); |
96 } | 104 } |
97 SDL_UpdateRect(screen, 0, 0, 320, 240); | 105 SDL_UpdateRect(screen, 0, 0, screen->clip_rect.w, screen->clip_rect.h); |
98 } | 106 } |
99 | 107 |
100 void render_wait_quit() | 108 void render_wait_quit() |
101 { | 109 { |
102 SDL_Event event; | 110 SDL_Event event; |