| 1 |
/* |
| 2 |
CheeseCutter v2 (C) Abaddon. Licensed under GNU GPL. |
| 3 |
*/ |
| 4 |
|
| 5 |
module com.fb; |
| 6 |
import derelict.sdl2.sdl; |
| 7 |
import std.string : indexOf; |
| 8 |
import com.util; |
| 9 |
|
| 10 |
immutable SDL_Color[] PALETTE = [ |
| 11 |
{ 0,0,0 }, |
| 12 |
{ 63 << 2,63 << 2,63 << 2 }, |
| 13 |
{ 26 << 2,13 << 2,10 << 2 }, |
| 14 |
{ 28 << 2,41 << 2,44 << 2 }, |
| 15 |
{ 27 << 2,15 << 2,33 << 2 }, |
| 16 |
{ 22 << 2,35 << 2,16 << 2 }, |
| 17 |
{ 13 << 2,10 << 2,30 << 2 }, |
| 18 |
{ 46 << 2,49 << 2,27 << 2 }, |
| 19 |
{ 27 << 2,19 << 2,9 << 2 }, |
| 20 |
{ 16 << 2,14 << 2,0 << 2 }, |
| 21 |
{ 38 << 2,25 << 2,22 << 2 }, |
| 22 |
{ 17 << 2,17 << 2,17 << 2 }, |
| 23 |
{ 27 << 2,27 << 2,27 << 2 }, |
| 24 |
{ 38 << 2,52 << 2,33 << 2 }, |
| 25 |
{ 27 << 2,23 << 2,45 << 2 }, |
| 26 |
{ 37 << 2,37 << 2,37 << 2 } ]; |
| 27 |
|
| 28 |
immutable FONT_X = 8, FONT_Y = 14; |
| 29 |
__gshared ubyte[] font; |
| 30 |
immutable int mode, border = 1; |
| 31 |
private bool isDirty = false; |
| 32 |
|
| 33 |
immutable CHECKX = "assert(x >= 0 && x < width);"; |
| 34 |
immutable CHECKY = "assert(y >= 0 && y < height);"; |
| 35 |
immutable CHECKS = "assert(x + y >= 0 && x + y < width*height);"; |
| 36 |
|
| 37 |
static this() { |
| 38 |
void[] arr; |
| 39 |
font.length = 256*16; |
| 40 |
// realign font data |
| 41 |
immutable rawfont = import("font.psf"); |
| 42 |
for(int i=0;i<256;i++) { |
| 43 |
font[i*16..i*16+14] = cast(ubyte[])rawfont[i*FONT_Y+4..i*FONT_Y+4+FONT_Y]; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
class Video { |
| 49 |
protected { |
| 50 |
SDL_Window* window; |
| 51 |
SDL_Renderer* renderer; |
| 52 |
SDL_Texture* texture; |
| 53 |
uint[] framebuffer; |
| 54 |
bool useFullscreen; |
| 55 |
Screen screen; |
| 56 |
const int requestedWidth, requestedHeight; |
| 57 |
int correctedHeight, correctedWidth; |
| 58 |
int height, width; // resolution of window |
| 59 |
//int displayHeight, displayWidth; // resolution of the monitor |
| 60 |
SDL_Rect destRect; |
| 61 |
} |
| 62 |
|
| 63 |
this(int wx, int wy, Screen scr, int fs) { |
| 64 |
this.screen = scr; |
| 65 |
this.requestedHeight = wy; |
| 66 |
this.requestedWidth = wx; |
| 67 |
this.framebuffer = new uint[wx*wy]; |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
~this() { |
| 72 |
if(renderer !is null) |
| 73 |
SDL_DestroyRenderer(renderer); |
| 74 |
|
| 75 |
// if(texture !is null) |
| 76 |
// SDL_DestroyTexture |
| 77 |
|
| 78 |
if(window !is null) |
| 79 |
SDL_DestroyWindow(window); |
| 80 |
|
| 81 |
} |
| 82 |
|
| 83 |
bool init() { |
| 84 |
width = requestedWidth; |
| 85 |
height = requestedHeight; |
| 86 |
useFullscreen = false; |
| 87 |
|
| 88 |
import std.string, std.stdio; |
| 89 |
|
| 90 |
window = SDL_CreateWindow("CheeseCutter 2.10", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 800, 600, cast(SDL_WindowFlags) |
| 91 |
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE |
| 92 |
); |
| 93 |
|
| 94 |
if (window is null) { |
| 95 |
return false; |
| 96 |
} |
| 97 |
|
| 98 |
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); |
| 99 |
|
| 100 |
if (renderer is null) { |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
texture = SDL_CreateTexture(renderer, SDL_GetWindowPixelFormat(window), |
| 105 |
SDL_TEXTUREACCESS_TARGET, 800, 600); |
| 106 |
|
| 107 |
if (texture is null) { |
| 108 |
return false; |
| 109 |
} |
| 110 |
|
| 111 |
SDL_SetRenderTarget(renderer, null); |
| 112 |
|
| 113 |
calcAspect(); |
| 114 |
screen.refresh(); |
| 115 |
|
| 116 |
return true; |
| 117 |
} |
| 118 |
|
| 119 |
void drawVisualizer(int) {} |
| 120 |
|
| 121 |
void clearVisualizer() {} |
| 122 |
|
| 123 |
protected void enableFullscreen(bool fs) {} |
| 124 |
|
| 125 |
void toggleFullscreen() { |
| 126 |
useFullscreen ^= 1; |
| 127 |
enableFullscreen(useFullscreen); |
| 128 |
} |
| 129 |
|
| 130 |
// TBD |
| 131 |
void scalePosition(ref int x, ref int y) { |
| 132 |
x -= destRect.x; |
| 133 |
y -= destRect.y; |
| 134 |
/+ |
| 135 |
x *= cast(float)requestedWidth / width; |
| 136 |
y *= cast(float)requestedHeight / height; |
| 137 |
+/ |
| 138 |
} |
| 139 |
|
| 140 |
void updateFrame() { |
| 141 |
auto surface = SDL_GetWindowSurface(window); |
| 142 |
int x, y; |
| 143 |
int a,b,c; |
| 144 |
ushort* bptr = &screen.data[0]; |
| 145 |
ushort* cptr = &screen.olddata[0]; |
| 146 |
uint* sptr = framebuffer.ptr; |
| 147 |
uint* sp; |
| 148 |
ubyte* bp; |
| 149 |
ubyte ubg, ufg; |
| 150 |
|
| 151 |
if (!isDirty) return; |
| 152 |
isDirty = false; |
| 153 |
|
| 154 |
for(y = 0;y < screen.height; y++) { |
| 155 |
for(x = 0; x < screen.width; x++) { |
| 156 |
if(*bptr != *cptr) { |
| 157 |
*cptr = *bptr; |
| 158 |
sp = sptr; |
| 159 |
a = *bptr & 255; |
| 160 |
bp = &font[a * 16]; |
| 161 |
ufg = (*bptr >> 8) & 15; |
| 162 |
ubg = (*bptr >> 12); |
| 163 |
auto fgcolor = getColor(surface, ufg), |
| 164 |
bgcolor = getColor(surface, ubg); |
| 165 |
for(c = 4; c < 18; c++, bp++) { |
| 166 |
b = *bp; |
| 167 |
if(b & 0x80) *(sp++) = fgcolor; |
| 168 |
else *(sp++) = bgcolor; |
| 169 |
if(b & 0x40) *(sp++) = fgcolor; |
| 170 |
else *(sp++) = bgcolor; |
| 171 |
if(b & 0x20) *(sp++) = fgcolor; |
| 172 |
else *(sp++) = bgcolor; |
| 173 |
if(b & 0x10) *(sp++) = fgcolor; |
| 174 |
else *(sp++) = bgcolor; |
| 175 |
if(b & 0x08) *(sp++) = fgcolor; |
| 176 |
else *(sp++) = bgcolor; |
| 177 |
if(b & 0x04) *(sp++) = fgcolor; |
| 178 |
else *(sp++) = bgcolor; |
| 179 |
if(b & 0x02) *(sp++) = fgcolor; |
| 180 |
else *(sp++) = bgcolor; |
| 181 |
if(b & 0x01) *(sp++) = fgcolor; |
| 182 |
else *(sp++) = bgcolor; |
| 183 |
sp += width - 8; |
| 184 |
} |
| 185 |
} |
| 186 |
sptr += 8; |
| 187 |
bptr++; |
| 188 |
cptr++; |
| 189 |
} |
| 190 |
sptr += width*13; |
| 191 |
} |
| 192 |
|
| 193 |
// Apparently this is fairly slow: https://wiki.libsdl.org/SDL3/SDL_UpdateTexture |
| 194 |
SDL_UpdateTexture(texture, null, framebuffer.ptr, 800 * uint.sizeof); |
| 195 |
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); |
| 196 |
SDL_RenderClear(renderer); |
| 197 |
SDL_RenderCopy(renderer, texture, null, &destRect); |
| 198 |
SDL_RenderPresent(renderer); |
| 199 |
} |
| 200 |
|
| 201 |
void resizeEvent(int nw, int nh) { |
| 202 |
calcAspect(); |
| 203 |
screen.refresh(); |
| 204 |
} |
| 205 |
|
| 206 |
private void calcAspect() { |
| 207 |
int windowWidth, windowHeight; |
| 208 |
|
| 209 |
SDL_GetWindowSize(window, &windowWidth, &windowHeight); |
| 210 |
|
| 211 |
int correctedHeight = windowHeight; |
| 212 |
int correctedWidth = windowWidth; |
| 213 |
if(cast(float)windowHeight / windowWidth < 0.75) { |
| 214 |
correctedWidth = cast(int)(correctedHeight / 0.75); |
| 215 |
correctedHeight = windowHeight; |
| 216 |
} |
| 217 |
else { |
| 218 |
correctedWidth = windowWidth; |
| 219 |
correctedHeight = cast(int)(correctedWidth * 0.75); |
| 220 |
} |
| 221 |
destRect.w = correctedWidth; |
| 222 |
destRect.h = correctedHeight; |
| 223 |
if (windowWidth > correctedWidth) |
| 224 |
destRect.x = (windowWidth - correctedWidth) / 2; |
| 225 |
else destRect.x = 0; |
| 226 |
if (windowHeight > correctedHeight) |
| 227 |
destRect.y = (windowHeight - correctedHeight) / 2; |
| 228 |
else destRect.y = 0; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
class Screen { |
| 234 |
Uint16[] data; |
| 235 |
private Uint16[] olddata; |
| 236 |
immutable int width, height; |
| 237 |
alias width w; |
| 238 |
alias height h; |
| 239 |
|
| 240 |
this(int xchars, int ychars) { |
| 241 |
width = xchars; |
| 242 |
height = ychars; |
| 243 |
data.length = xchars * ychars; |
| 244 |
olddata.length = xchars * ychars; |
| 245 |
refresh(); |
| 246 |
} |
| 247 |
|
| 248 |
Uint16 getChar(int x, int y) { |
| 249 |
mixin(CHECKS); |
| 250 |
return data[x + y * width]; |
| 251 |
} |
| 252 |
|
| 253 |
void setChar(int x, int y, Uint16 c) { |
| 254 |
mixin(CHECKS); |
| 255 |
data[x + y * width] = c; |
| 256 |
isDirty = true; |
| 257 |
} |
| 258 |
|
| 259 |
void setColor(int x, int y, int fg, int bg) { |
| 260 |
mixin(CHECKS); |
| 261 |
Uint16* s = &data[x + y * width]; |
| 262 |
*s &= 0xff; |
| 263 |
*s |= (fg << 8) | (bg << 12); |
| 264 |
isDirty = true; |
| 265 |
} |
| 266 |
|
| 267 |
int getbg(int x, int y) { |
| 268 |
return getChar(x, y) >> 12; |
| 269 |
} |
| 270 |
|
| 271 |
void setbg(int x, int y, int bg) { |
| 272 |
Uint16* s = &data[x + y * width]; |
| 273 |
*s &= 0xfff; |
| 274 |
*s |= (bg << 12); |
| 275 |
isDirty = true; |
| 276 |
} |
| 277 |
|
| 278 |
void clrtoeol(int y, int bg) { |
| 279 |
clrtoeol(0, y, bg); |
| 280 |
} |
| 281 |
|
| 282 |
void clrtoeol(int x, int y, int bg) { |
| 283 |
mixin(CHECKY); |
| 284 |
Uint16* s = &data[x + y * width]; |
| 285 |
Uint16 v = cast(Uint16)(0x20 | (bg << 12)); |
| 286 |
while(x++ < width) *s++ = v; |
| 287 |
isDirty = true; |
| 288 |
} |
| 289 |
|
| 290 |
void clrscr() { |
| 291 |
data[] = 0x20; |
| 292 |
isDirty = true; |
| 293 |
} |
| 294 |
|
| 295 |
void refresh() { |
| 296 |
olddata[] = 255; |
| 297 |
isDirty = true; |
| 298 |
} |
| 299 |
|
| 300 |
void cprint(int x, int y, int fg, int bg, string txt) { |
| 301 |
mixin(CHECKS); |
| 302 |
bool skipbg, skipfg; |
| 303 |
if(bg < 0) { skipbg = true; bg = 0; } |
| 304 |
if(fg < 0) { skipfg = true; fg = 0; } |
| 305 |
Uint16[] s = data[x + y * width .. x + y * width + txt.length]; |
| 306 |
Uint16 col = cast(Uint16)((fg << 8) | (bg << 12)); |
| 307 |
foreach(i, char c; txt) { |
| 308 |
if(skipbg) |
| 309 |
col = cast(Uint16)((fg << 8) | (s[i] & 0xf000)); |
| 310 |
if(skipfg) |
| 311 |
col = cast(Uint16)((bg << 12) | (s[i] & 0x0f00)); |
| 312 |
|
| 313 |
s[i] = cast(Uint16)(c | col); |
| 314 |
} |
| 315 |
isDirty = true; |
| 316 |
} |
| 317 |
|
| 318 |
void fprint(int x, int y, string str) { |
| 319 |
mixin(CHECKS); |
| 320 |
assert(str.length < 256); |
| 321 |
Uint16[] outb = data[x + y * width .. $]; |
| 322 |
int bg = 0, fg = 0; |
| 323 |
int idx; |
| 324 |
while(idx < str.length) { |
| 325 |
int getcol(char c) { |
| 326 |
return cast(int)(c == '+' ? -1 : "0123456789abcdef".indexOf(c)); |
| 327 |
} |
| 328 |
if(str[idx] == '`') { |
| 329 |
bg = getcol(str[idx + 1]); |
| 330 |
fg = getcol(str[idx + 2]); |
| 331 |
idx += 3; |
| 332 |
continue; |
| 333 |
} |
| 334 |
if(bg >= 0) { |
| 335 |
outb[0] &= 0x0fff; |
| 336 |
outb[0] |= bg << 12; |
| 337 |
} |
| 338 |
if(fg >= 0) { |
| 339 |
outb[0] &= 0xf0ff; |
| 340 |
outb[0] |= fg << 8; |
| 341 |
} |
| 342 |
outb[0] &= 0xff00; |
| 343 |
outb[0] |= str[idx] & 255; |
| 344 |
outb = outb[1 .. $]; |
| 345 |
idx++; |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
interface Visualizer { |
| 351 |
void clear(); |
| 352 |
void draw(int); |
| 353 |
} |
| 354 |
|
| 355 |
private class Oscilloscope : Visualizer { |
| 356 |
private SDL_Surface* surface; |
| 357 |
private short* samples; |
| 358 |
private const short xconst, yconst; |
| 359 |
enum width = 960/4, height = 3*14; |
| 360 |
|
| 361 |
this(SDL_Surface* surface, short xpos, short ypos) { |
| 362 |
this.surface = surface; |
| 363 |
this.xconst = xpos; |
| 364 |
this.yconst = ypos; |
| 365 |
import audio.audio; |
| 366 |
samples = audio.audio.mixbuf; |
| 367 |
assert(samples !is null); |
| 368 |
} |
| 369 |
|
| 370 |
void clear() { |
| 371 |
SDL_FillRect(surface, new SDL_Rect(xconst, yconst, |
| 372 |
width, height), 0); |
| 373 |
} |
| 374 |
|
| 375 |
void draw(int frames) { |
| 376 |
float smpofs; |
| 377 |
float n = frames * 50.0f; |
| 378 |
int count = cast(int)(48000 / n); |
| 379 |
|
| 380 |
auto colh = getColor(surface, 13), |
| 381 |
coll = getColor(surface, 5); |
| 382 |
|
| 383 |
clear(); |
| 384 |
|
| 385 |
smpofs = 0.0f; |
| 386 |
import audio.audio; |
| 387 |
int oldposition = height / 2 + samples[cast(int)smpofs] / 768; |
| 388 |
|
| 389 |
for(int i = 0; i < width; i++) { |
| 390 |
int sample = samples[cast(int)smpofs] / 768; |
| 391 |
int position = height / 2 + sample; |
| 392 |
position = com.util.umod(position, 0, height-1); |
| 393 |
int a = oldposition, b = position; |
| 394 |
|
| 395 |
if(a > b) { |
| 396 |
int temp = b; |
| 397 |
b = a; |
| 398 |
a = temp; |
| 399 |
} |
| 400 |
assert(a <= b); |
| 401 |
Uint32* pos = cast(Uint32 *)surface.pixels + xconst + i + (a + yconst) * surface.w; |
| 402 |
*pos = (i > 12 && i < width - 12) ? colh : coll; |
| 403 |
for(int k = a; k < b; k++) { |
| 404 |
*pos = (i > 12 && i < width - 12) ? colh : coll; |
| 405 |
pos += surface.w; |
| 406 |
} |
| 407 |
smpofs++; |
| 408 |
if(smpofs >= audio.audio.getbufsize()) |
| 409 |
smpofs -= cast(int)audio.audio.getbufsize(); |
| 410 |
oldposition = position; |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
class DisplayError : Error { |
| 416 |
this(string msg) { |
| 417 |
super("SDL Error: " ~ msg); |
| 418 |
} |
| 419 |
} |
| 420 |
|
| 421 |
void enableKeyRepeat() { |
| 422 |
// SDL_EnableKeyRepeat(200, 10); |
| 423 |
} |
| 424 |
|
| 425 |
void disableKeyRepeat() { |
| 426 |
//SDL_EnableKeyRepeat(0, 0); |
| 427 |
} |
| 428 |
|
| 429 |
Uint16 readkey() { |
| 430 |
SDL_Event evt; |
| 431 |
bool loop = true; |
| 432 |
|
| 433 |
while(loop) { |
| 434 |
while(SDL_PollEvent(&evt)) { |
| 435 |
if(evt.type == SDL_QUIT) { |
| 436 |
SDL_Quit(); |
| 437 |
return 0; |
| 438 |
} |
| 439 |
if(evt.type == SDL_KEYDOWN) { |
| 440 |
loop = false; |
| 441 |
break; |
| 442 |
} |
| 443 |
} |
| 444 |
SDL_Delay(50); |
| 445 |
} |
| 446 |
return cast(Uint16)evt.key.keysym.unicode; |
| 447 |
} |
| 448 |
|
| 449 |
auto getColor(SDL_Surface* s, int c) { |
| 450 |
return SDL_MapRGBA(s.format, PALETTE[c].r, PALETTE[c].g, PALETTE[c].b, 255); |
| 451 |
} |