... | ... |
@@ -17,36 +17,20 @@ clear(Ppu *p) |
17 | 17 |
{ |
18 | 18 |
int i, sz = p->height * p->width; |
19 | 19 |
for(i = 0; i < sz; ++i) { |
20 |
- p->fg.pixels[i] = p->fg.colors[0]; |
|
21 |
- p->bg.pixels[i] = p->bg.colors[0]; |
|
20 |
+ p->pixels[i] = 0; |
|
22 | 21 |
} |
23 | 22 |
} |
24 | 23 |
|
25 | 24 |
void |
26 |
-putcolors(Ppu *p, Uint8 *addr) |
|
27 |
-{ |
|
28 |
- int i; |
|
29 |
- for(i = 0; i < 4; ++i) { |
|
30 |
- Uint8 |
|
31 |
- r = (*(addr + i / 2) >> (!(i % 2) << 2)) & 0x0f, |
|
32 |
- g = (*(addr + 2 + i / 2) >> (!(i % 2) << 2)) & 0x0f, |
|
33 |
- b = (*(addr + 4 + i / 2) >> (!(i % 2) << 2)) & 0x0f; |
|
34 |
- p->bg.colors[i] = 0xff000000 | (r << 20) | (r << 16) | (g << 12) | (g << 8) | (b << 4) | b; |
|
35 |
- p->fg.colors[i] = 0xff000000 | (r << 20) | (r << 16) | (g << 12) | (g << 8) | (b << 4) | b; |
|
36 |
- } |
|
37 |
- p->fg.colors[0] = 0; |
|
38 |
- clear(p); |
|
39 |
-} |
|
40 |
- |
|
41 |
-void |
|
42 |
-putpixel(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color) |
|
25 |
+putpixel(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color) |
|
43 | 26 |
{ |
27 |
+ Uint8 mask = layer ? 0x3 : 0xc, shift = layer * 2; |
|
44 | 28 |
if(x < p->width && y < p->height) |
45 |
- layer->pixels[y * p->width + x] = layer->colors[color]; |
|
29 |
+ p->pixels[y * p->width + x] = (p->pixels[y * p->width + x] & mask) | (color << shift); |
|
46 | 30 |
} |
47 | 31 |
|
48 | 32 |
void |
49 |
-puticn(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy) |
|
33 |
+puticn(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy) |
|
50 | 34 |
{ |
51 | 35 |
Uint16 v, h; |
52 | 36 |
for(v = 0; v < 8; v++) |
... | ... |
@@ -62,7 +46,7 @@ puticn(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uin |
62 | 46 |
} |
63 | 47 |
|
64 | 48 |
void |
65 |
-putchr(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy) |
|
49 |
+putchr(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy) |
|
66 | 50 |
{ |
67 | 51 |
Uint16 v, h; |
68 | 52 |
for(v = 0; v < 8; v++) |
... | ... |
@@ -86,10 +70,5 @@ initppu(Ppu *p, Uint8 hor, Uint8 ver) |
86 | 70 |
p->ver = ver; |
87 | 71 |
p->width = 8 * p->hor; |
88 | 72 |
p->height = 8 * p->ver; |
89 |
- if(!(p->bg.pixels = malloc(p->width * p->height * sizeof(Uint32)))) |
|
90 |
- return 0; |
|
91 |
- if(!(p->fg.pixels = malloc(p->width * p->height * sizeof(Uint32)))) |
|
92 |
- return 0; |
|
93 |
- clear(p); |
|
94 | 73 |
return 1; |
95 | 74 |
} |
... | ... |
@@ -17,17 +17,12 @@ typedef unsigned char Uint8; |
17 | 17 |
typedef unsigned short Uint16; |
18 | 18 |
typedef unsigned int Uint32; |
19 | 19 |
|
20 |
-typedef struct Layer { |
|
21 |
- Uint32 *pixels, colors[4]; |
|
22 |
-} Layer; |
|
23 |
- |
|
24 | 20 |
typedef struct Ppu { |
25 | 21 |
Uint16 hor, ver, width, height; |
26 |
- Layer fg, bg; |
|
22 |
+ Uint8 *pixels; |
|
27 | 23 |
} Ppu; |
28 | 24 |
|
29 | 25 |
int initppu(Ppu *p, Uint8 hor, Uint8 ver); |
30 |
-void putcolors(Ppu *p, Uint8 *addr); |
|
31 |
-void putpixel(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 color); |
|
32 |
-void puticn(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); |
|
33 |
-void putchr(Ppu *p, Layer *layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); |
|
26 |
+void putpixel(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 color); |
|
27 |
+void puticn(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); |
|
28 |
+void putchr(Ppu *p, Uint8 layer, Uint16 x, Uint16 y, Uint8 *sprite, Uint8 color, Uint8 flipx, Uint8 flipy); |
... | ... |
@@ -23,8 +23,7 @@ WITH REGARD TO THIS SOFTWARE. |
23 | 23 |
|
24 | 24 |
static SDL_AudioDeviceID audio_id; |
25 | 25 |
static SDL_Window *gWindow; |
26 |
-static SDL_Renderer *gRenderer; |
|
27 |
-static SDL_Texture *fgTexture, *bgTexture; |
|
26 |
+static SDL_Surface *winSurface, *idxSurface, *rgbaSurface; |
|
28 | 27 |
static SDL_Rect gRect; |
29 | 28 |
static Ppu ppu; |
30 | 29 |
static Apu apu[POLYPHONY]; |
... | ... |
@@ -33,7 +32,7 @@ static Uint32 stdin_event; |
33 | 32 |
|
34 | 33 |
#define PAD 4 |
35 | 34 |
|
36 |
-static Uint8 zoom = 0, reqdraw = 0, bench = 0; |
|
35 |
+static Uint8 zoom = 1, reqdraw = 0, bench = 0; |
|
37 | 36 |
|
38 | 37 |
static Uint8 font[][8] = { |
39 | 38 |
{0x00, 0x7c, 0x82, 0x82, 0x82, 0x82, 0x82, 0x7c}, |
... | ... |
@@ -85,24 +84,24 @@ inspect(Ppu *p, Uint8 *stack, Uint8 wptr, Uint8 rptr, Uint8 *memory) |
85 | 84 |
Uint8 i, x, y, b; |
86 | 85 |
for(i = 0; i < 0x20; ++i) { /* stack */ |
87 | 86 |
x = ((i % 8) * 3 + 1) * 8, y = (i / 8 + 1) * 8, b = stack[i]; |
88 |
- puticn(p, &p->fg, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0); |
|
89 |
- puticn(p, &p->fg, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0); |
|
87 |
+ puticn(p, 1, x, y, font[(b >> 4) & 0xf], 1 + (wptr == i) * 0x7, 0, 0); |
|
88 |
+ puticn(p, 1, x + 8, y, font[b & 0xf], 1 + (wptr == i) * 0x7, 0, 0); |
|
90 | 89 |
} |
91 | 90 |
/* return pointer */ |
92 |
- puticn(p, &p->fg, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0); |
|
93 |
- puticn(p, &p->fg, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0); |
|
91 |
+ puticn(p, 1, 0x8, y + 0x10, font[(rptr >> 4) & 0xf], 0x2, 0, 0); |
|
92 |
+ puticn(p, 1, 0x10, y + 0x10, font[rptr & 0xf], 0x2, 0, 0); |
|
94 | 93 |
for(i = 0; i < 0x20; ++i) { /* memory */ |
95 | 94 |
x = ((i % 8) * 3 + 1) * 8, y = 0x38 + (i / 8 + 1) * 8, b = memory[i]; |
96 |
- puticn(p, &p->fg, x, y, font[(b >> 4) & 0xf], 3, 0, 0); |
|
97 |
- puticn(p, &p->fg, x + 8, y, font[b & 0xf], 3, 0, 0); |
|
95 |
+ puticn(p, 1, x, y, font[(b >> 4) & 0xf], 3, 0, 0); |
|
96 |
+ puticn(p, 1, x + 8, y, font[b & 0xf], 3, 0, 0); |
|
98 | 97 |
} |
99 | 98 |
for(x = 0; x < 0x10; ++x) { /* guides */ |
100 |
- putpixel(p, &p->fg, x, p->height / 2, 2); |
|
101 |
- putpixel(p, &p->fg, p->width - x, p->height / 2, 2); |
|
102 |
- putpixel(p, &p->fg, p->width / 2, p->height - x, 2); |
|
103 |
- putpixel(p, &p->fg, p->width / 2, x, 2); |
|
104 |
- putpixel(p, &p->fg, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2); |
|
105 |
- putpixel(p, &p->fg, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2); |
|
99 |
+ putpixel(p, 1, x, p->height / 2, 2); |
|
100 |
+ putpixel(p, 1, p->width - x, p->height / 2, 2); |
|
101 |
+ putpixel(p, 1, p->width / 2, p->height - x, 2); |
|
102 |
+ putpixel(p, 1, p->width / 2, x, 2); |
|
103 |
+ putpixel(p, 1, p->width / 2 - 0x10 / 2 + x, p->height / 2, 2); |
|
104 |
+ putpixel(p, 1, p->width / 2, p->height / 2 - 0x10 / 2 + x, 2); |
|
106 | 105 |
} |
107 | 106 |
} |
108 | 107 |
|
... | ... |
@@ -111,12 +110,15 @@ redraw(Uxn *u) |
111 | 110 |
{ |
112 | 111 |
if(devsystem->dat[0xe]) |
113 | 112 |
inspect(&ppu, u->wst.dat, u->wst.ptr, u->rst.ptr, u->ram.dat); |
114 |
- SDL_UpdateTexture(bgTexture, &gRect, ppu.bg.pixels, ppu.width * sizeof(Uint32)); |
|
115 |
- SDL_UpdateTexture(fgTexture, &gRect, ppu.fg.pixels, ppu.width * sizeof(Uint32)); |
|
116 |
- SDL_RenderClear(gRenderer); |
|
117 |
- SDL_RenderCopy(gRenderer, bgTexture, NULL, NULL); |
|
118 |
- SDL_RenderCopy(gRenderer, fgTexture, NULL, NULL); |
|
119 |
- SDL_RenderPresent(gRenderer); |
|
113 |
+ if(rgbaSurface == NULL) |
|
114 |
+ SDL_BlitScaled(idxSurface, NULL, winSurface, &gRect); |
|
115 |
+ else if(zoom == 1) |
|
116 |
+ SDL_BlitSurface(idxSurface, NULL, winSurface, &gRect); |
|
117 |
+ else { |
|
118 |
+ SDL_BlitSurface(idxSurface, NULL, rgbaSurface, NULL); |
|
119 |
+ SDL_BlitScaled(rgbaSurface, NULL, winSurface, &gRect); |
|
120 |
+ } |
|
121 |
+ SDL_UpdateWindowSurface(gWindow); |
|
120 | 122 |
reqdraw = 0; |
121 | 123 |
} |
122 | 124 |
|
... | ... |
@@ -132,40 +134,32 @@ togglezoom(Uxn *u) |
132 | 134 |
{ |
133 | 135 |
zoom = zoom == 3 ? 1 : zoom + 1; |
134 | 136 |
SDL_SetWindowSize(gWindow, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom); |
137 |
+ winSurface = SDL_GetWindowSurface(gWindow); |
|
138 |
+ gRect.x = zoom * PAD; |
|
139 |
+ gRect.y = zoom * PAD; |
|
140 |
+ gRect.w = zoom * ppu.width; |
|
141 |
+ gRect.h = zoom * ppu.height; |
|
135 | 142 |
redraw(u); |
136 | 143 |
} |
137 | 144 |
|
138 | 145 |
static void |
139 | 146 |
screencapture(void) |
140 | 147 |
{ |
141 |
- const Uint32 format = SDL_PIXELFORMAT_RGB24; |
|
142 | 148 |
time_t t = time(NULL); |
143 | 149 |
char fname[64]; |
144 |
- int w, h; |
|
145 |
- SDL_Surface *surface; |
|
146 |
- SDL_GetRendererOutputSize(gRenderer, &w, &h); |
|
147 |
- surface = SDL_CreateRGBSurfaceWithFormat(0, w, h, 24, format); |
|
148 |
- SDL_RenderReadPixels(gRenderer, NULL, format, surface->pixels, surface->pitch); |
|
149 | 150 |
strftime(fname, sizeof(fname), "screenshot-%Y%m%d-%H%M%S.bmp", localtime(&t)); |
150 |
- SDL_SaveBMP(surface, fname); |
|
151 |
- SDL_FreeSurface(surface); |
|
151 |
+ SDL_SaveBMP(winSurface, fname); |
|
152 | 152 |
fprintf(stderr, "Saved %s\n", fname); |
153 | 153 |
} |
154 | 154 |
|
155 | 155 |
static void |
156 | 156 |
quit(void) |
157 | 157 |
{ |
158 |
- free(ppu.fg.pixels); |
|
159 |
- free(ppu.bg.pixels); |
|
160 | 158 |
SDL_UnlockAudioDevice(audio_id); |
161 |
- SDL_DestroyTexture(bgTexture); |
|
162 |
- bgTexture = NULL; |
|
163 |
- SDL_DestroyTexture(fgTexture); |
|
164 |
- fgTexture = NULL; |
|
165 |
- SDL_DestroyRenderer(gRenderer); |
|
166 |
- gRenderer = NULL; |
|
159 |
+ SDL_FreeSurface(winSurface); |
|
160 |
+ SDL_FreeSurface(idxSurface); |
|
161 |
+ if(rgbaSurface) SDL_FreeSurface(rgbaSurface); |
|
167 | 162 |
SDL_DestroyWindow(gWindow); |
168 |
- gWindow = NULL; |
|
169 | 163 |
SDL_Quit(); |
170 | 164 |
exit(0); |
171 | 165 |
} |
... | ... |
@@ -176,27 +170,30 @@ init(void) |
176 | 170 |
SDL_AudioSpec as; |
177 | 171 |
if(!initppu(&ppu, 64, 40)) |
178 | 172 |
return error("ppu", "Init failure"); |
179 |
- gRect.x = PAD; |
|
180 |
- gRect.y = PAD; |
|
181 |
- gRect.w = ppu.width; |
|
182 |
- gRect.h = ppu.height; |
|
183 | 173 |
if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) < 0) |
184 | 174 |
return error("sdl", SDL_GetError()); |
185 | 175 |
gWindow = SDL_CreateWindow("Uxn", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, (ppu.width + PAD * 2) * zoom, (ppu.height + PAD * 2) * zoom, SDL_WINDOW_SHOWN); |
186 | 176 |
if(gWindow == NULL) |
187 | 177 |
return error("sdl_window", SDL_GetError()); |
188 |
- gRenderer = SDL_CreateRenderer(gWindow, -1, 0); |
|
189 |
- if(gRenderer == NULL) |
|
190 |
- return error("sdl_renderer", SDL_GetError()); |
|
191 |
- SDL_RenderSetLogicalSize(gRenderer, ppu.width + PAD * 2, ppu.height + PAD * 2); |
|
192 |
- bgTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2); |
|
193 |
- if(bgTexture == NULL || SDL_SetTextureBlendMode(bgTexture, SDL_BLENDMODE_NONE)) |
|
194 |
- return error("sdl_texture", SDL_GetError()); |
|
195 |
- fgTexture = SDL_CreateTexture(gRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STATIC, ppu.width + PAD * 2, ppu.height + PAD * 2); |
|
196 |
- if(fgTexture == NULL || SDL_SetTextureBlendMode(fgTexture, SDL_BLENDMODE_BLEND)) |
|
197 |
- return error("sdl_texture", SDL_GetError()); |
|
198 |
- SDL_UpdateTexture(bgTexture, NULL, ppu.bg.pixels, 4); |
|
199 |
- SDL_UpdateTexture(fgTexture, NULL, ppu.fg.pixels, 4); |
|
178 |
+ winSurface = SDL_GetWindowSurface(gWindow); |
|
179 |
+ if(winSurface == NULL) |
|
180 |
+ return error("sdl_surface win", SDL_GetError()); |
|
181 |
+ idxSurface = SDL_CreateRGBSurfaceWithFormat(0, ppu.width, ppu.height, 8, SDL_PIXELFORMAT_INDEX8); |
|
182 |
+ if(idxSurface == NULL || SDL_SetSurfaceBlendMode(idxSurface, SDL_BLENDMODE_NONE)) |
|
183 |
+ return error("sdl_surface idx", SDL_GetError()); |
|
184 |
+ if(SDL_MUSTLOCK(idxSurface) == SDL_TRUE) |
|
185 |
+ return error("sdl_surface idx", "demands locking"); |
|
186 |
+ gRect.x = zoom * PAD; |
|
187 |
+ gRect.y = zoom * PAD; |
|
188 |
+ gRect.w = zoom * ppu.width; |
|
189 |
+ gRect.h = 2 * ppu.height; /* force non-1:1 scaling for BlitScaled test */ |
|
190 |
+ if(SDL_BlitScaled(idxSurface, NULL, winSurface, &gRect) < 0) { |
|
191 |
+ rgbaSurface = SDL_CreateRGBSurfaceWithFormat(0, ppu.width, ppu.height, 32, SDL_PIXELFORMAT_RGB24); |
|
192 |
+ if(rgbaSurface == NULL || SDL_SetSurfaceBlendMode(rgbaSurface, SDL_BLENDMODE_NONE)) |
|
193 |
+ return error("sdl_surface rgba", SDL_GetError()); |
|
194 |
+ } |
|
195 |
+ gRect.h = zoom * ppu.height; |
|
196 |
+ ppu.pixels = idxSurface->pixels; |
|
200 | 197 |
SDL_StartTextInput(); |
201 | 198 |
SDL_ShowCursor(SDL_DISABLE); |
202 | 199 |
SDL_zero(as); |
... | ... |
@@ -275,7 +272,19 @@ system_talk(Device *d, Uint8 b0, Uint8 w) |
275 | 272 |
d->dat[0x2] = d->u->wst.ptr; |
276 | 273 |
d->dat[0x3] = d->u->rst.ptr; |
277 | 274 |
} else if(b0 > 0x7 && b0 < 0xe) { |
278 |
- putcolors(&ppu, &d->dat[0x8]); |
|
275 |
+ SDL_Color pal[16]; |
|
276 |
+ int i; |
|
277 |
+ for(i = 0; i < 4; ++i) { |
|
278 |
+ pal[i].r = ((d->dat[0x8 + i / 2] >> (!(i % 2) << 2)) & 0x0f) * 0x11; |
|
279 |
+ pal[i].g = ((d->dat[0xa + i / 2] >> (!(i % 2) << 2)) & 0x0f) * 0x11; |
|
280 |
+ pal[i].b = ((d->dat[0xc + i / 2] >> (!(i % 2) << 2)) & 0x0f) * 0x11; |
|
281 |
+ } |
|
282 |
+ for(i = 4; i < 16; ++i) { |
|
283 |
+ pal[i].r = pal[i / 4].r; |
|
284 |
+ pal[i].g = pal[i / 4].g; |
|
285 |
+ pal[i].b = pal[i / 4].b; |
|
286 |
+ } |
|
287 |
+ SDL_SetPaletteColors(idxSurface->format->palette, pal, 0, 16); |
|
279 | 288 |
reqdraw = 1; |
280 | 289 |
} else if(b0 == 0xf) |
281 | 290 |
d->u->ram.ptr = 0x0000; |
... | ... |
@@ -294,7 +303,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w) |
294 | 303 |
if(w && b0 == 0xe) { |
295 | 304 |
Uint16 x = mempeek16(d->dat, 0x8); |
296 | 305 |
Uint16 y = mempeek16(d->dat, 0xa); |
297 |
- Layer *layer = d->dat[0xe] >> 4 & 0x1 ? &ppu.fg : &ppu.bg; |
|
306 |
+ Uint8 layer = d->dat[0xe] >> 4 & 0x1; |
|
298 | 307 |
Uint8 mode = d->dat[0xe] >> 5; |
299 | 308 |
if(!mode) |
300 | 309 |
putpixel(&ppu, layer, x, y, d->dat[0xe] & 0x3); |
... | ... |
@@ -309,7 +318,7 @@ screen_talk(Device *d, Uint8 b0, Uint8 w) |
309 | 318 |
} else if(w && b0 == 0xf) { |
310 | 319 |
Uint16 x = mempeek16(d->dat, 0x8); |
311 | 320 |
Uint16 y = mempeek16(d->dat, 0xa); |
312 |
- Layer *layer = d->dat[0xf] >> 0x6 & 0x1 ? &ppu.fg : &ppu.bg; |
|
321 |
+ Uint8 layer = d->dat[0xf] >> 0x6 & 0x1; |
|
313 | 322 |
Uint8 *addr = &d->mem[mempeek16(d->dat, 0xc)]; |
314 | 323 |
if(d->dat[0xf] >> 0x7 & 0x1) |
315 | 324 |
putchr(&ppu, layer, x, y, addr, d->dat[0xf] & 0xf, d->dat[0xf] >> 0x4 & 0x1, d->dat[0xf] >> 0x5 & 0x1); |
... | ... |
@@ -460,7 +469,6 @@ int |
460 | 469 |
main(int argc, char **argv) |
461 | 470 |
{ |
462 | 471 |
Uxn u; |
463 |
- zoom = 1; |
|
464 | 472 |
|
465 | 473 |
stdin_event = SDL_RegisterEvents(1); |
466 | 474 |
SDL_CreateThread(stdin_handler, "stdin", NULL); |