-
Notifications
You must be signed in to change notification settings - Fork 6
/
sdltest.nt
506 lines (468 loc) · 13.7 KB
/
sdltest.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
module sdltest;
import c.SDL.SDL, simplex, std.thread;
pragma(lib, "SDL");
SDL_Surface* surf;
int delegate(int b) add(int a) { return new delegate int(int b) { return a + b; }; }
string break-the-coarse-parser(int i) { return "lol } "; /* } */ }
int X1(int a) { if (true) return a; return X2(a); }
int X2(int a) { if (true) return a; return X1(a); }
class A { B b; void meep() { writeln "Hello World"; return; b.peep(); } }
class B { A a; void peep() { writeln "Fuck tha po-lice"; return; a.meep(); } }
void test(int foo) {
int bar = 17;
if (foo) writeln("meep");
else writeln("moop");
writeln("Hello World: $(foo * add a=>2 b=>bar), $bar");
int temp = 5;
while (temp) {
writeln("Countdown with $temp");
temp = temp - 1;
}
for (int x = 0; x < 10; ++x) {
writeln("Test: $x");
}
}
int acker(int m, int n) {
if (m) {
if (n) return acker(m - 1, acker(m, n - 1));
else return acker(m - 1, 1);
} else return n + 1;
}
struct X {
int a, b;
int c;
alias foo = a+b*c;
}
struct Y {
X x;
int i;
}
int s(int i, int k) {
writeln("meep $i, $k");
return k;
}
struct Z {
int i;
}
Z ztest() { Z z; z.i = 5; return z; }
struct W {
int i;
int test2(int k) { return i + k; }
void test() {
writeln("Hello W; i = $i, i + 3 = $$test2(3)");
}
}
import c.math, c.fenv, c.unistd, c.stdlib, c.time;
void sdlfun(vec3f delegate(float, float, float) dg) {
auto surf = SDL_SetVideoMode(1200, 900, 32, 0);
if (!surf) {
writeln "Cannot allocate SDL surface";
fail;
}
.surf = surf;
auto start = time(int*: null);
float t = 0;
int fps;
auto tp = new ThreadPool(4);
void run() {
t += 0.02;
void calc(int from, int to) {
int factor1 = 0xff0000, factor2 = 0xff00, factor3 = 0xff;
vec3f ff = vec3f(factor1, factor2, factor3);
auto w = int:surf.pitch / 4;
for (int y = from; y < to; y += 3) {
// auto p = &((int*:surf.pixels)[y * int:surf.pitch / 4]);
vec3f f = void;
vec3i i = void;
for (int x = 0; x < surf.w; x += 3) {
f = dg(float:x / (surf.w * 3), float:y / (surf.h * 3), t) * ff;
fastfloor3f (f, &i);
// *(p++) = i.x & factor1 + i.y & factor2 + i.z & factor3;
auto val = i.x & factor1 + i.y & factor2 + i.z & factor3;
auto p = &((int*:surf.pixels)[y * w + x]);
p[0] = val; p[1] = val; p[2] = val; p += w;
p[0] = val; p[1] = val; p[2] = val; p += w;
p[0] = val; p[1] = val; p[2] = val;
}
}
}
for (int i <- 0..8) {
auto step = surf.h / 8;
auto from = step * i, to = step * (i + 1);
void delegate() myApply(int from, int to, void delegate(int, int) dg) {
return new delegate void() { return dg(from, to); };
}
// myApply(from, to, &calc)();
tp.addTask myApply(from, to, &calc);
}
tp.waitComplete;
fps ++;
}
auto last = time(null), tstart = last;
while true {
if (time(null) - tstart > 12) return;
run();
SDL_Flip surf;
while SDL_PollEvent &SDL_Event ev using ev
if type == 12 return;
if (auto tvar = time(null)) > last {
last = tvar;
writeln("FPS: $fps");
fps = 0;
}
}
}
interface IA {
void iafun();
}
interface IB {
void ibfun();
}
interface IC : IA, IB {
void icfun();
}
interface ID {
void idfun();
}
class Class {
int i;
void foo(int k) { writeln("foo $(k+i); btw this is $this"); }
void bar() { writeln("bar here"); }
}
class Subclass : Class, IC, ID {
int k;
void iafun() { writeln("IA in Sub: this $this"); }
void ibfun() { writeln("IB in Sub: this $this"); }
void icfun() { writeln("IC in Sub: this $this"); }
void idfun() { writeln("ID in Sub: this $this"); }
void foo(int l) { writeln("subfoo $(i + k + l)"); }
}
void nesttest() {
int nest_test = 13;
void nestfun() { int a; a = 7; void nestfun2() { nest_test = a; } nestfun2(); }
void delegate() nf = &nestfun;
nf();
writeln("nest test: $nest_test. ");
int function(int, int x) fp = &s;
writeln("s test $$fp(4, 5)");
}
struct Blarg {
int ib;
void fun() { writeln("$ib!"); }
}
template Blorg(T) {
struct Blorg {
T t;
}
}
template FunTemp(T) {
void FunTemp(T t) {
writeln("T::$(string-of t)");
}
}
int globvar;
context ctest {
int var;
}
union U {
int I;
float F;
}
// c_include "gc.h";
int main(string[] args) {
// use Boehm GC
/* mem.malloc_dg = &GC_malloc;
void* myCalloc(int a, b) {
auto len = a * b;
auto res = sys.mem.malloc(len);
char ch;
(char*:res)[0 .. len] = [for 0..len: ch];
return res;
}
void* myRealloc(void* a, size_t b) { return GC_realloc(a, int:b); }
mem.calloc_dg = &myCalloc;
mem.realloc_dg = &myRealloc;
mem.free_dg = &GC_free;*/
/*if (int:_ebp & 0xf != 8) {
writeln "FEEEP! YOU BROKE THE FUCKING FRAME ALIGNMENT AGAIN. $(_ebp) ";
_interrupt 3;
}*/
auto cdg = mem.calloc_dg;
mem.calloc_dg = delegate void*(int a, b) {
// printf("Allocate %i, %i\n", a, b);
if (a*b > 65536 * 4) {
printf("Excessive allocation: %i, %i\n", a, b);
_interrupt 3;
}
return cdg(a, b);
};
test(2);
test(0);
int e = 5;
// writeln("a(3, 12) = $$acker(3, 12)");
int* ptr = &e;
*ptr = 7;
X x;
x.a = 5; x.b = 6; x.c = 3;
writeln("expression alias! $$x.foo");
Y y;
y.x = x;
y.x.c = 5;
writeln("It's a $(y.x.c)!");
writeln("yo .. $(x.a), $$(x.b), $(x.c)");
writeln("pointer to e: $ptr. e: $(*ptr), also $(*&*&e).");
int m = 5, n = 8;
writeln("post inc test: $(m++), $(m++)");
writeln("test: $$*(&m - 1)");
if (s(0, 1) && s(1, 0) && s(2, 1)) writeln("yes"); else writeln("no");
if (s(0, 1) && s(1, 0) && s(2, 1) || s(3, 1)) writeln("yes"); else writeln("no");
int x 5 ifield;
ifield[3] = 15;
writeln("field access $$ifield[3]");
int* ip = &ifield[3];
writeln("field access via ptr $$ip[0], oh btw $$ifield.length");
// ifield.length = 8; // will fail
// ztest().i = 5; // correctly doesn't work
char[] arr = "foob";
writeln("proper array test: $$arr.length, contents $arr");
writeln("slice: $$arr[1 .. 4], via ptr $$arr.ptr[1 .. 4]");
nesttest();
W w;
w.i = 5;
w.test();
writeln("And done. ");
Class cl = new Class;
writeln("class size is $$size-of type-of cl; method is $$string-of type-of &cl.foo");
writeln("forble $$string-of &cl.foo");
writeln("class is at $$void*:cl, i $$&(cl.i)");
cl.i = 3;
cl.foo(2);
void delegate(int) dgx = &cl.foo;
dgx(3);
(&cl.foo)(4);
writeln "Alloc subclass. ";
Class sup = new Subclass;
writeln "Do sub cast";
auto sub = Subclass:sup;
writeln("sup is $$void*:sup");
sup.foo(-5);
writeln("sub is $$void*:sub");
sub.iafun();
sub.ibfun();
sub.icfun();
sub.idfun();
IA ia = sub;
writeln "call ia on implicit IA cast from sub; ia is $(void*:ia), sub is $(void*:sub), ia delta is $(**int**:ia)";
writeln "iafun is $(string-of &ia.iafun) $(&ia.iafun), on sub would be $(&sub.iafun). ";
ia.iafun();
auto ic = IC:ia;
ic.icfun();
auto forb = char[]:"test";
Blarg blg;
{
Blarg lolz() { Blarg res; return res; }
using blg::
that.ib = 7;
using lolz()::
that.ib = 5;
}
blg.fun();
do int i = rand() % 10; while (i) writeln("::$i");
Blorg!int foo;
writeln("template test: $$string-of type-of foo.t");
FunTemp!int(5);
globvar = 17;
ctest.var = 17;
using scoped ctest {
writeln("var: $(that.var)");
that.var = 14;
writeln("now it's $(that.var)");
}
writeln("now back to $(ctest.var). ");
void memtest() using sys.mem {
writeln("memtest! ");
auto p = that.malloc(16);
that.free(p);
writeln("again.. ");
}
memtest();
auto old_malloc = sys.mem.malloc_dg;
using scoped sys.mem using that {
void* fun(int i) {
writeln("malloc()");
return old_malloc(i);
}
malloc_dg = &fun;
memtest();
}
memtest();
auto testp = sys.mem.malloc(15);
auto artest = new int[](3);
artest[2] = 15;
artest[0 .. 2] = artest[1 .. 3];
writeln("test is $$artest.length, $$artest[1], $$artest[2]");
writeln("Array test: $artest");
{
char[] s1 = "foo", s2 = "bar", s3 = s1 ~ s2;
writeln("s3 is $s3, or $$s1 ~ s2, length $$s3.length");
}
onExit writeln("global is $globvar, $$&globvar, $$*&globvar");
onExit writeln("Exit. ");
onExit writeln("Exit 2. ");
float fl = 2;
fl = fl + 10;
writeln("fl is $fl");
void testfl(float cmp) {
if (fl > cmp) writeln("fl > $cmp");
if (fl < cmp) writeln("fl < $cmp");
if (fl == cmp) writeln("fl == $cmp");
}
testfl(11);
testfl(12);
testfl(13);
double db = 12;
void testdb(double cmp) {
if (db > cmp) writeln "db > $cmp";
if (db < cmp) writeln "db < $cmp";
if (db == cmp) writeln "db == $cmp";
}
testdb(11);
testdb(12);
testdb(13);
{
onExit writeln("Exit 3. ");
float clamp(float from, float to, float f) {
if (f <= from) return from;
if (f >= to) return to;
return f;
}
vec3f clamp3f(float from, float to, vec3f f) {
return vec3f(clamp(from, to, f.x), clamp(from, to, f.y), clamp(from, to, f.z));
}
// http://en.wikipedia.org/wiki/Smoothstep
float smoothstep(float edge0, float edge1, float x) {
float old_x = x;
x = (x - edge0) / (edge1 - edge0);
if (x <= 0) return 0;
if (x >= 1) return 1;
return x * x * (3 - 2 * x);
}
vec3f transition(vec3f* a, vec3f* b, float f) {
return (*a) * (1 - f) + (*b) * f;
}
float PI = 3.1415926538;
float fun1(float x, float y) {
float rx = 2 * (x - 0.5) * 1.333;
float ry = 2 * (y - 0.5);
float h = 3 * sqrtf(x*x*x);
h = h * (1 - x);
float e = fabsf(ry) - h;
float f = smoothstep(0, 0.01, e);
return f;
}
vec3f fun2(float x, float y) {
x = x - 0.5;
y = y - 0.5;
float f = fun1(x + 0.5, y + 0.5);
float angle = 15 * PI / 180.0;
for (int i = 0; i < 24; ++i) {
float x2 = x * cosf(angle) - y * sinf(angle);
float y2 = y * cosf(angle) + x * sinf(angle);
x = x2; y = y2;
f = f * 0.8 + 0.2 * fun1(x + 0.5, y + 0.5);
}
return vec3f(f);
}
vec3f fun4(float x, float y, float t) {
float factor = 1;
float mew = noise3 vec3f(x * 2 + noise3 vec3f(-x*3, y*3, t/4), y * 2 + noise3 vec3f(x*3, -y*3, t/4), t);
auto noise = (mew + 1) * 20;
if (noise >= 30) factor = mew;
noise -= int:noise;
// Octave 2: Fine noise
noise += noise3 vec3f(x * 200, y * 200, t) * 0.5;
// Octave 3: Streak
noise += noise3 vec3f(x, y * 100, t) * 0.7;
// Adjust range to [0, 1]
noise = (noise + 1) / 2;
// Convert noise to colour
auto res = vec3f(noise * 0.7, noise * 0.507, noise * 0.313);
res *= factor;
for (int i <- 0..3) res[i] = clamp(0, 1, res[i]);
return res;
}
vec3f fun3(float x, float y, float t) {
// auto f2 = fun2(x, y);
x = x - 0.5f;
y = y - 0.5f;
// auto dist = sqrtf(x * x + y * y);
// auto n = 0.5 * noise2(x * 4 + t, y * 4) + 0.25 * noise2(x * 8, x * 8 + t) + 0.125 * noise2(y * 16 + t, y * 16 + t) + 0.0625 * noise2(x * 32 + t, y * 32 - t * 2);
// auto n = 0.5 * noise3 ((vec3f(x * 4, y * 4, sin(t) * 4)).zxy) + 0.25;
float noisex(vec3f v) {
float sqr(float f) { return f * f; }
return noise3 vec3f(v.x + sqr noise3(v), v.y + sqr noise3(-v), v.z);
// return noise3 vec3f(v.x + sqr noise3(v), v.y, v.z);
// return vec3f(noise3 vec3f(v.x + sqr noise3(v), v.y, v.z)); //, noise3 vec3f(v.x + sqr noise3(v), v.y, v.z + 1024), noise3 vec3f(v.x + sqr noise3(v), v.y, -v.z));
// return vec3f(noise3 v); //, noise3 -v, noise3 vec3f(v.x, v.y, v.z + 1024));
// return noise3 v;
// return sinf(v.x + sqr noise3 v + v.y + v.z) * 0.5 + 0.5;
}
// auto n = noisex vec3f(x * 8, y * 8, t);
/*auto res =
vec3f(0.5, 0.55, 0.7)
* noisex vec3f(x * 1.07, y * 1.01, t * 1.03)
+ vec3f(0.25, 0.25, 0.35)
* noisex vec3f(x * 4.06, y * 4.02, t * 2.09 + 4) // offset! important
+ vec3f(0.125, 0.125, 0.125)
* noisex vec3f(x *32.01, y *32.08, t * 4.05 + 8)
+ 0.0625 * noisex vec3f(x *64.03, y *64.05, t * 8.04 + 12)
// + 0.03125* noisex vec3f(x *256, y *256, t *16 + 16)
;*/
auto pos = vec3f(x, y, t) * 512;
auto res = 0
+ noisex vec3f(pos.x /256f, pos.y /256f, pos.z /256f) * 64
- noisex vec3f(pos.x /128f, pos.y /128f, pos.z /128f) * 32
- noisex vec3f(pos.x / 64f, pos.y / 64f, pos.z / 64f) * 16
- noisex vec3f(pos.x / 32f, pos.y / 32f, pos.z / 32f) * 8
- noisex vec3f(pos.x / 16f, pos.y / 16f, pos.z / 16f) * 4
;
res /= 64f;
res = clamp(0, 1, res);
// auto n = 0.5f * noise2(x * 4 + t, y * 4)+0.25;
// n = clamp(0, 1, n);
// auto n2 = vec3f(n, n * n, n * 2);
// auto n2 = vec3f (res);
// return n2;
return vec3f(res);
// return transition(&f2, &n2, smoothstep(0.3, 0.5, dist + noise2(x * 2 + 100, y * 2) * 0.1f));
}
{
set-handler (Error) invoke-exit "skip";
bool skip;
define-exit "skip" skip = true;
if !skip sdlfun(&fun4);
}
U u;
u.F = 15;
printf("comparison 0x%08x\n", float:15);
writeln("u.i is $$void*:u.I");
auto tuple = (2, 3);
writeln("Tuple is $(tuple[0]), $(tuple[1]). ");
int pow2(int i) {
int res = 1;
while (i--) res = res * 2;
return res;
}
(int, int) frob;
frob[0] = 15;
writeln "frob is $frob";
alias size = 4;
auto test = [for bin <-
[for tuple <- cross ([for i <- 0..2: i] x size): int x size:tuple]:
sum [for tup <- zip (bin, [for z <- 0..size:
pow2(size-z-1)]): tup[0] * tup[1]]].eval;
writeln("test is $$string-of type-of test: $test");
return 0;
onExit writeln("Exit 4. ");
}
}