-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsdl.nt
548 lines (505 loc) · 16.9 KB
/
sdl.nt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
module sdl;
public import std.lib.sdl_base;
import std.string;
extern(C) int abs(int d);
int floatToIntColor(vec3f col) {
vec3i ii = vec3i(0xff0000, 0xff00, 0xff);
vec3f ff = vec3f(0xff0000, 0xff00, 0xff);
vec3i i = void;
fastfloor3f (col * ff, &i);
// make sure we get opacity
return (i & ii).sum + int:0xff00_0000;
}
int floatToIntColor(vec4f col) {
vec3i ii = vec3i(0xff0000, 0xff00, 0xff);
vec3f ff = vec3f(0xff0000, 0xff00, 0xff);
vec3i i = void;
fastfloor3f (col.xyz * ff, &i);
return (i & ii).sum + (int:(col.w * 255) & 0xff) << 24;
}
vec4f intToFloatColor(int col) {
int blue = col & 0xff, green = (col >> 8) & 0xff,
red = (col >> 16) & 0xff, alpha = (col >> 24);
return vec4f(red, green, blue, alpha) / 255f;
}
reassign int abgr_to_argb(int i) {
auto bp = byte*:&i;
auto temp = bp[0]; bp[0] = bp[2]; bp[2] = temp;
return i;
}
struct RGBResult {
int value;
alias implicit-cast = value;
alias implicit-cast-2 = intToFloatColor(value);
alias implicit-cast-3 = intToFloatColor(value).xyz;
}
extern(C) void stamp_ptr(int* srcp, dstp, int w) {
for int x <- 0..w {
auto src = *srcp; alias dst = *dstp;
int srcalpha = (byte*:&src)[3], dstalpha = (byte*:&dst)[3], srcalpha2 = 255 - srcalpha;
dst =
((dstalpha + ((255 - dstalpha) * srcalpha) >> 8) << 24)
| (((byte*:&src)[0] * srcalpha + (byte*:&dst)[0] * srcalpha2) >> 8)
| (((byte*:&src)[1] * srcalpha + (byte*:&dst)[1] * srcalpha2) & 0xff00)
| (((byte*:&src)[2] * srcalpha + (byte*:&dst)[2] * srcalpha2) >> 8 << 16);
srcp ++;
dstp ++;
}
}
interface INullArea { }
class Area {
Surface surf;
(vec2i, vec2i) rect;
alias x0 = rect[0].x;
alias y0 = rect[0].y;
alias x1 = rect[1].x;
alias y1 = rect[1].y;
alias w = x1 - x0, h = y1 - y0;
void free() { surf.release; super.free; } // !! NOT surf.free! let the ref counting clean up!
void release() surf.release;
void claim() surf.claim;
void init(Surface s) {
surf = s;
s.claim;
rect[0] = vec2i(0, 0);
rect[1] = vec2i(surf.w, surf.h);
}
Area copy() {
auto res = new Area surf;
res.rect = rect;
return res;
}
Area add(int x, int y) {
auto res = copy();
if (x > w) { x = w; }
if (y > h) { y = h; }
res.rect[0] += vec2i(x, y);
return res;
}
Area at(int x, y) {
if (x < 0 || y < 0) raise new Error "Please don't call at() with negative coordinates; it breaks things";
return add(x, y);
}
Area sub(int x1, int y1, int x2, int y2) {
auto res = copy();
res.rect[0] += vec2i(x1, y1);
res.rect[1] = res.rect[0] + vec2i(x2 - x1, y2 - y1);
return res;
}
Area shrink(int sz) { return sub(sz, sz, w-sz, h-sz); }
void blit(Area dest) {
SDL_Rect sdlrect1, sdlrect2;
for (int i, SDL_Rect* rp) <- zip(0..2, [&sdlrect1, &sdlrect2])
using *rp {
auto r = [rect, dest.rect][i];
that.(x, y) = r[0].(short:x, short:y);
that.(w, h) = (short:(r[1].x - r[0].x), short: (r[1].y - r[0].y));
}
auto res = SDL_UpperBlit (surf.back, &sdlrect1, dest.surf.back, &sdlrect2);
if res raise new SDL-Error("SDL_UpperBlit", res);
}
// copy, overwriting target alpha values with [target..ours].
void stamp(Area dest, int xd = 0, yd = 0) {
if INullArea:dest raise new Error "Don't stamp onto a null area! ";
auto w = w, h = h;
if (xd + w <= 0 || yd + h <= 0) {
// writeln "stamping src of $((w, h)) onto {$(dest.x0), $(dest.y0)} of $(dest.(w, h)) - lies completely outside! ";
return;
}
auto src = this;
if (xd < 0) src = src.at(-xd, 0);
else dest = dest.at(xd, 0);
if (yd < 0) src = src.at(0, -yd);
else dest = dest.at(0, yd);
w = [src.w, dest.w][dest.w < src.w];
h = [src.h, dest.h][dest.h < src.h];
auto pitch = int:surf.back.pitch / 4, dpitch = int:dest.surf.back.pitch / 4;
auto srcp = &(
(int*:surf.back.pixels)
[src.y0 * pitch + src.x0]);
auto dstp = &(
(int*:dest.surf.back.pixels)
[dest.y0 * dpitch + dest.x0]);
for int y <- 0..h {
stamp_ptr(srcp, dstp, w);
srcp += pitch; dstp += dpitch;
}
}
void pset(int x, y, vec3f col) {
x += x0; y += y0;
if !( 0 <= x < [surf.w, w][w < surf.w] && 0 <= y < [surf.h, h][h < surf.h] ) return;
auto p = &((int*:surf.back.pixels)[y * int:surf.back.pitch / 4 + x]);
*p = floatToIntColor col;
}
void pset(int x, y, int icol) {
x += x0; y += y0;
if !( 0 <= x < surf.w && 0 <= y < surf.h ) return;
auto p = &((int*:surf.back.pixels)[y * int:surf.back.pitch / 4 + x]);
*p = icol;
}
RGBResult getp(int x, y) {
x += x0; y += y0;
if !( 0 <= x < surf.w && 0 <= y < surf.h ) raise new Error "Pixel access out of bounds: $x, $y";
auto p = &((int*:surf.back.pixels)[y * int:surf.back.pitch / 4 + x]);
return RGBResult: *p;
}
vec4f delegate(int, int) fillfun;
void hline_fillfun(int from-x, y, to-x) {
if !(0 <= y < h) return;
y += y0;
if !(0 <= y < surf.h) return;
from-x += x0; to-x += x0;
if (to-x < from-x) (from-x, to-x) = (to-x, from-x);
from-x = [from-x, 0] [from-x < 0];
to-x = [to-x, surf.w - 1][to-x >= surf.w];
if (from-x >= surf.w || to-x < 0) return;
auto p = &((int*:surf.back.pixels)[y * int:surf.back.pitch / 4 + from-x]);
auto delta = to-x - from-x + 1;
auto fillfun = fillfun;
int x = from-x;
while delta-- {
/*xmm[6] = fillfun(x++, y);
xmm[7] = vec4f(0xff, 0xff00, 0, 0);
xmm[6] = xmm[6].zyxw; // BGRA
xmm[4] = xmm[6] * xmm[7];
xmm[6] = xmm[6].zwxy;
xmm[5] = xmm[6] * xmm[7];
asm "cvttps2dq %xmm4, %xmm6";
asm `psrld $31, %xmm4`;
asm "psubd %xmm4, %xmm6";
asm "cvttps2dq %xmm5, %xmm7";
asm `psrld $31, %xmm5`;
asm "psubd %xmm5, %xmm7";
auto i1 = vec3i:xmm[6], i2 = vec3i:xmm[7];*/
vec4f xmm6 = fillfun(x++, y);
vec4f xmm7 = vec4f(0xff, 0xff00, 0, 0);
xmm6 = xmm6.zyxw; // BGRA
vec4f xmm4 = xmm6*xmm7;
xmm6 = xmm6.zwxy;
vec4f xmm5 = xmm6*xmm7;
fastfloor3f(xmm4.xyz, &vec3i i1);
fastfloor3f(xmm5.xyz, &vec3i i2);
int i = 0;
i += i2.x & 0xff;
i += i2.y & 0xff00;
i <<= 16;
i += i1.x & 0xff;
i += i1.y & 0xff00;
*(p++) = i;
}
}
void hline_plain(int from-x, y, to-x, vec4f col) {
y += y0;
if !(0 <= y < surf.h) return;
from-x += x0; to-x += x0;
if (to-x < from-x) (from-x, to-x) = (to-x, from-x);
from-x = [from-x, 0] [from-x < 0];
to-x = [to-x, surf.w - 1][to-x >= surf.w];
if (from-x >= surf.w || to-x < 0) return;
auto icol = floatToIntColor col;
auto p = &((int*:surf.back.pixels)[y * int:surf.back.pitch / 4 + from-x]);
auto delta = to-x - from-x + 1;
// thanks, http://stackoverflow.com/questions/3345042/how-to-memset-memory-to-a-certain-pattern-instead-of-a-single-byte answer with one upvote "Recursive memmove"
// you now have two upvotes
int stepsize = 1;
auto start = p;
if !delta return;
*(p++) = icol;
delta --;
while stepsize <= delta {
memcpy(p, start, stepsize * 4);
p += stepsize;
delta -= stepsize;
stepsize *= 2;
}
if delta memcpy(p, start, delta * 4);
}
void hline(int from-x, y, to-x, vec4f col) {
if fillfun { hline_fillfun(from-x, y, to-x); }
else hline_plain(from-x, y, to-x, col);
}
void hline(int from-x, y, to-x, vec3f col) {
hline(from-x, y, to-x, vec4f(col.(x, y, z, 1)));
}
void vline(int x, from-y, to-y, vec4f col) {
x += x0;
if !(0 <= x < surf.w) return;
from-y += y0; to-y += y0;
if (to-y < from-y) (from-y, to-y) = (to-y, from-y);
from-y = [from-y, 0] [from-y < 0];
to-y = [to-y, surf.h - 1][to-y >= surf.h];
if (from-y >= surf.h || to-y < 0) return;
auto icol = floatToIntColor col;
auto pitch = int:surf.back.pitch / 4;
auto p = &((int*:surf.back.pixels)[from-y * pitch + x]);
auto delta = to-y - from-y + 1;
while (delta --) {
*p = icol;
p += pitch;
}
}
void vline(int x, from-y, to-y, vec3f col) {
vline(x, from-y, to-y, vec4f(col.(x, y, z, 0)));
}
void cls(vec3f col) {
for int y <- 0..h
hline(0, y, w-1, col);
}
void cls(vec4f col) {
for int y <- 0..h {
hline(0, y, w-1, col);
}
}
// Blatantly ripped off from WP:Bresenham
void line(int from-x, from-y, to-x, to-y, vec4f col) {
// no need to do bounds checking here; pset is already safe
auto from = vec2i(from-x, from-y), to = vec2i(to-x, to-y);
auto icol = floatToIntColor col;
bool steep = abs(to.y - from.y) > abs(to.x - from.x);
if steep
(from.(x, y), to.(x, y)) = (from.(y, x), to.(y, x));
if from.x > to.x
(from, to) = (to, from);
auto
delta-x = to.x - from.x,
delta-y = abs(to.y - from.y),
error = delta-x / 2;
int ystep = [-1, 1][from.y < to.y], y = from.y;
for (int x = from.x; x <= to.x; ++x) {
if steep pset(y, x, icol); else pset(x, y, icol);
error -= delta-y;
if error < 0 {
y += ystep;
error += delta-x;
}
}
}
void line(int from-x, from-y, to-x, to-y, vec3f col = vec3f(1)) {
line(from-x, from-y, to-x, to-y, vec4f(col.(x, y, z, 1)));
}
// This one is WP:Midpoint circle algorithm. <3 you WP.
void circle(int x0, y0, radius,
xspread = 0, yspread = 0,
vec4f col = vec4f(1), vec4f fill = vec4f(-1)) {
int f = 1 - radius, ddF_x = 1, ddF_y = - 2 * radius, x, y = radius;
bool fillIt = fill.x >= 0;
if fillIt {
hline(x0 - radius + 1, y0, x0 + radius - 1 + xspread, fill);
}
auto icol = floatToIntColor col;
int lastY;
while x < y {
// ddF_x == 2 * x + 1;
// ddF_y == -2 * y;
// f == x*x + y*y - radius*radius + 2*x - y + 1;
if f >= 0 {
--y; ddF_y += 2; f += ddF_y;
}
++x; ddF_x += 2; f += ddF_x;
if (fillIt && lastY != y) {
lastY = y;
hline(x0 - x + 1, y0 - y , x0 + x - 1 + xspread, fill);
hline(x0 - x + 1, y0 + y + yspread, x0 + x - 1 + xspread, fill);
}
if (fillIt && x < y) {
hline(x0 - y + 1, y0 - x , x0 + y - 1 + xspread, fill);
hline(x0 - y + 1, y0 + x + yspread, x0 + y - 1 + xspread, fill);
}
for auto tup <- zip(cross([1, 0], [1, 0]), cross([1, -1], [1, -1])) {
pset(x0 + tup[1][0] * x + tup[0][0] * xspread,
y0 + tup[1][1] * y + tup[0][1] * yspread, icol);
pset(x0 + tup[1][0] * y + tup[0][0] * xspread,
y0 + tup[1][1] * x + tup[0][1] * yspread, icol);
}
}
// fill in the sides/corners
// those two are part of the frame!
{
auto backup = fillfun;
fillfun = null;
onSuccess fillfun = backup;
hline(x0, y0 + radius + yspread, x0 + xspread, col);
hline(x0, y0 - radius , x0 + xspread, col);
}
vline(x0 + radius + xspread, y0, y0 + yspread, col);
vline(x0 - radius , y0, y0 + yspread, col);
// fill in the middle
if fillIt {
for (int i = y0; i <= y0 + yspread; ++i) {
hline(x0 - radius + 1, i, x0 + radius - 1 + xspread, fill);
}
}
}
void circle(int x0, y0, radius,
xspread = 0, yspread = 0,
vec3f col = vec3f(1), vec3f fill = vec3f(-1))
{
circle(x0, y0, radius, xspread, yspread,
vec4f(col.(x, y, z, 0)), vec4f(fill.(x, y, z, 0)));
}
void rounded_box(int x0, y0, x1, y1,
radius = 5, vec4f col = vec4f(1), vec4f fill = vec4f(-1))
{
// translate into circle call
auto cx = x0 + radius, xspread = x1 - cx - radius;
xspread = [xspread, 0][xspread < 0];
auto cy = y0 + radius, yspread = y1 - cy - radius;
yspread = [yspread, 0][yspread < 0];
circle(cx, cy, radius, xspread => xspread, yspread => yspread,
col => col, fill => fill);
}
void rounded_box(int x0, y0, x1, y1,
radius = 5, vec3f col = vec3f(1), vec3f fill = vec3f(-1))
{
rounded_box(x0, y0, x1, y1, radius,
vec4f(col.(x, y, z, 0)), vec4f(fill.(x, y, z, 0)));
}
}
// drawing operations are no-ops
class NullArea : Area, INullArea {
void free() { }
void claim() { }
void init(Surface s) { super.init s; }
Area copy() {
auto res = new NullArea surf;
res.rect = rect;
res.surf = surf; // lol
return res;
}
void blit(Area dest) {
if NullArea:dest return;
raise new Error "Blitting null-area onto regular area - this makes no sense! ";
}
void stamp(Area dest, int xd = 0, int yd = 0) { }
void pset(int x, y, vec3f col) { }
void pset(int x, y, int icol) { }
int getp(int x, y) { }
void hline_fillfun(int from-x, y, to-x) { }
void hline_plain(int from-x, y, to-x, vec4f col) { }
void vline(int x, from-y, to-y, vec4f col) { }
void cls(vec3f col) { }
void cls(vec4f col) { }
void line(int from-x, from-y, to-x, to-y, vec3f col = vec3f(1)) { }
void circle(int x0, y0, radius,
xspread = 0, yspread = 0,
vec4f col = vec4f(1), vec4f fill = vec4f(-1)) { }
}
shared Area display;
/*
class WindowSurface13 : Surface {
SDL_Window* window;
SDL_GLContext gl;
void initGL() { gl = SDL_GL_CreateContext(window); }
void free() { super.free; if (gl) SDL_GL_DeleteContext gl; SDL_DestroyWindow window; }
void flip() { if gl SDL_GL_SwapWindow window; else SDL_UpdateWindowSurface window; }
void makeCurrent() { SDL_GL_MakeCurrent (window, gl); }
}
Area screen13(int w, h, bool fullscreen = false, bool surface = false,
int flags = 0) {
int cfg;
bool eatFlag(int flag) { if (flags & flag) { flags &= -flag - 1; return true; } return false; }
if (eatFlag SDL_OPENGL) cfg |= SDL_WINDOW_OPENGL;
if (eatFlag SDL_RESIZABLE) cfg |= SDL_WINDOW_RESIZABLE;
eatFlag SDL_ANYFORMAT;
if (flags) {
cfg = flags;
}
assert(eval !fullscreen || !surface, "Surfaces cannot be fullscreen! ");
if fullscreen cfg |= SDL_WINDOW_FULLSCREEN;
Surface surf;
if surface
surf = new Surface SDL_CreateRGBSurface(cfg, w, h, 32, 0xff0000, 0xff00, 0xff, int:0xff00_0000);
else {
auto window = SDL_CreateWindow("SDL window", SDL_WINDOWPOS_UNDEFINED x 2, w, h, cfg);
surf = new WindowSurface13 SDL_GetWindowSurface(window);
(WindowSurface13:surf).window = window;
if (cfg & SDL_WINDOW_OPENGL) {
(WindowSurface13:surf).initGL();
(WindowSurface13:surf).makeCurrent();
}
}
using new Area surf {
if surface return that;
else display = that;
}
return display;
}
*/
platform(default) {
import c.SDL.SDL_syswm;
struct SDL_SysWMinfo {
SDL_version version;
int subsystem;
Display *display; /**< The X11 display */
Window window; /**< The X11 display window */
/** These locking functions should be called around
* any X11 functions using the display variable,
* but not the gfxdisplay variable.
* They lock the event thread, so should not be
* called around event functions or from event filters.
*/
void function() lock_func, unlock_func;
/** @name Introduced in SDL 1.0.2 */
Window fswindow; /**< The X11 fullscreen window */
Window wmwindow; /**< The X11 managed input window */
/** @name Introduced in SDL 1.2.12 */
Display *gfxdisplay; /**< The X11 display to which rendering is done */
}
extern(C) int SDL_GetWMInfo(SDL_SysWMinfo*);
pragma(lib, "X11");
}
Area screen(int w, h, bool fullscreen = false, bool surface = false,
int flags = 0) {
int cfg = SDL_SWSURFACE;
if (flags) cfg = flags;
SDL_Surface* surf;
if surface
surf = SDL_CreateRGBSurface(cfg, w, h, 32, 0xff0000, 0xff00, 0xff, int:0xff00_0000);
else
surf = SDL_SetVideoMode(w, h, 32, cfg);
if (fullscreen) {
platform(!default) {
assert(false, "don't know how to reliably fullscreen on this platform");
}
platform(default) {
SDL_SysWMinfo info;
if (SDL_GetWMInfo(&info) == true) using XOpenDisplay(null) {
Atom
_NET_WM_STATE = XInternAtom("_NET_WM_STATE", true),
_NET_WM_STATE_FULLSCREEN = XInternAtom("_NET_WM_STATE_FULLSCREEN", true),
_NET_WM_STATE_ADD = Atom:1,
_NET_WM_STATE_TOGGLE = Atom:2;
assert eval int:_NET_WM_STATE && int:_NET_WM_STATE_FULLSCREEN;
XEvent e;
e.xany.type = ClientMessage;
info.window.XQueryTree(&Window root, &Window parent, &Window* children, &int nchildren);
e.xclient.(send_event, message_type, format, window) = (true, _NET_WM_STATE, 32, parent);
e.xclient.data.l[0..2] = [int:_NET_WM_STATE_ADD, int:_NET_WM_STATE_FULLSCREEN, 0];
XSendEvent(root, false,
SubstructureNotifyMask | SubstructureRedirectMask, &e);
XFlush;
}
}
}
if !surf raise new Error "Couldn't init screen with $w x $h - $(CToString SDL_GetError())! ";
using new Area new Surface surf {
if surface return that;
else display = that;
}
return display;
}
/*platform(!i686-mingw32) {
void saveBMP(string s) {
auto p = toStringz s;
onSuccess mem.free p;
auto res = SDL_SaveBMP_RW (display.surf.back, SDL_RWFromFile (p, "wb"), 1);
if res == -1 {
writeln "error - $(CToString SDL_GetError())";
_interrupt 3;
}
}
}*/
void flip() {
display.surf.flip();
update;
}