actor graphics!
[dd2d.git] / xmain_d2d.d
blob4d31541584c5740595828548ff5a691e4e335b78
1 module xmain_d2d is aliced;
3 private:
4 import core.atomic;
5 import core.thread;
6 import core.time;
8 import iv.glbinds;
9 import glutils;
10 import console;
11 import wadarc;
13 import iv.stream;
15 import d2dmap;
16 import d2dadefs;
17 //import d2dactors;
18 import d2dgfx;
19 import dacs;
21 // `map` is there
22 import dengapi;
25 // ////////////////////////////////////////////////////////////////////////// //
26 import arsd.color;
27 import arsd.png;
30 // ////////////////////////////////////////////////////////////////////////// //
31 __gshared SimpleWindow sdwindow;
34 public enum vlWidth = 800;
35 public enum vlHeight = 800;
36 __gshared int scale = 1;
37 __gshared bool scanlines = false;
40 // ////////////////////////////////////////////////////////////////////////// //
41 enum MaxLightRadius = 512;
44 // ////////////////////////////////////////////////////////////////////////// //
45 // for light
46 __gshared FBO[MaxLightRadius+1] fboOccluders, fboShadowMap;
47 __gshared Shader shadToPolar, shadBlur;
49 __gshared FBO fboLevel, fboOrigBack;
50 __gshared Shader shadScanlines;
51 __gshared Shader shadLiquidDistort, shadLiquidColor;
54 // ////////////////////////////////////////////////////////////////////////// //
55 void initOpenGL () {
56 glEnable(GL_TEXTURE_2D);
57 glDisable(GL_LIGHTING);
58 glDisable(GL_DITHER);
59 glDisable(GL_BLEND);
60 glDisable(GL_DEPTH_TEST);
62 // create shaders
63 shadScanlines = new Shader("scanlines", loadTextFile("data/shaders/srscanlines.frag"));
65 shadLiquidDistort = new Shader("liquid_distort", loadTextFile("data/shaders/srliquid_distort.frag"));
66 shadLiquidDistort.exec({ shadLiquidDistort["tex0"] = 0; });
68 shadLiquidColor = new Shader("liquid_color", loadTextFile("data/shaders/srliquid_color.frag"));
69 shadLiquidColor.exec({ shadLiquidColor["tex0"] = 0; });
71 // lights
72 shadToPolar = new Shader("topolar", loadTextFile("data/shaders/srlight_topolar.frag"));
73 shadBlur = new Shader("blur", loadTextFile("data/shaders/srlight_blur.frag"));
74 shadBlur.exec({
75 shadBlur["tex0"] = 0;
76 shadBlur["tex1"] = 1;
77 });
78 //TODO: this sux!
79 foreach (int sz; 2..MaxLightRadius+1) {
80 fboOccluders[sz] = new FBO(sz*2, sz*2, Texture.Option.Clamp); // create occluders FBO
81 fboShadowMap[sz] = new FBO(sz*2, 1, Texture.Option.Clamp); // create 1d shadowmap FBO
84 // setup matrices
85 glMatrixMode(GL_MODELVIEW);
86 glLoadIdentity();
88 map.oglBuildMega();
90 fboLevel = new FBO(map.width*8, map.height*8, Texture.Option.Nearest); // final level render will be here
91 fboOrigBack = new FBO(map.width*8, map.height*8, Texture.Option.Nearest); // original background
93 // build noise texture for liquid shaders
95 glActiveTexture(GL_TEXTURE0+1);
97 import std.random : uniform;
98 auto img = new TrueColorImage(64, 64);
99 foreach (int y; 0..img.width) {
100 foreach (int x; 0..img.height) {
101 ubyte b = cast(ubyte)uniform(0, 256);
102 img.imageData.colors[y*img.width+x] = Color(b, b, b, 255);
105 auto tex = new Texture(img);
106 tex.activate;
110 glActiveTexture(GL_TEXTURE0+0);
111 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
112 orthoCamera(vlWidth, vlHeight);
114 Actor.resetStorage();
115 loadMapMonsters();
116 //map.initActors(); // this will precache sprites too
117 //map.doAllActorTicks();
119 { import core.memory : GC; GC.collect(); }
123 // ////////////////////////////////////////////////////////////////////////// //
124 void renderLight() (int lightX, int lightY, in auto ref SVec4F lcol, int lightRadius) {
125 if (lightRadius < 2) return;
126 if (lightRadius > MaxLightRadius) lightRadius = MaxLightRadius;
127 int lightSize = lightRadius*2;
128 // is this light visible?
129 if (lightX <= -lightRadius || lightY <= -lightRadius || lightX-lightRadius >= map.width*8 || lightY-lightRadius >= map.height*8) return;
131 // draw shadow casters to fboOccludersId, light should be in the center
132 glUseProgram(0);
133 glDisable(GL_BLEND);
134 fboOccluders[lightRadius].exec({
135 glColor3f(0.0f, 0.0f, 0.0f);
136 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
137 glClear(GL_COLOR_BUFFER_BIT);
138 orthoCamera(lightSize, lightSize);
139 drawAtXY(map.texgl.ptr[map.LightMask], lightRadius-lightX, lightRadius-lightY);
142 // build 1d shadow map to fboShadowMapId
143 fboShadowMap[lightRadius].exec({
144 shadToPolar.exec({
145 glColor3f(0.0f, 0.0f, 0.0f);
146 shadToPolar["lightTexSize"] = SVec2F(lightSize, lightSize);
147 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
148 glClear(GL_COLOR_BUFFER_BIT);
149 orthoCamera(lightSize, 1);
150 drawAtXY(fboOccluders[lightRadius].tex.tid, 0, 0, lightSize, 1);
154 // blend light
155 fboLevel.exec({
156 shadBlur.exec({
157 shadBlur["lightTexSize"] = SVec2F(lightSize, lightSize);
158 glEnable(GL_BLEND);
159 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
160 // set light color
161 glColor4f(lcol.x, lcol.y, lcol.z, lcol.w);
162 orthoCamera(map.width*8, map.height*8);
163 drawAtXY(fboShadowMap[lightRadius].tex.tid, lightX-lightRadius, lightY-lightRadius, lightSize, lightSize);
169 // ////////////////////////////////////////////////////////////////////////// //
170 __gshared int lightX = vlWidth/2, lightY = vlHeight/2;
171 __gshared int mapOfsX, mapOfsY;
172 __gshared bool movement = false;
175 void renderScene () {
176 enum BackIntens = 0.05f;
178 glUseProgram(0);
180 fboLevel.exec({
181 glDisable(GL_BLEND);
182 glClearColor(BackIntens, BackIntens, BackIntens, 1.0f);
183 glClear(GL_COLOR_BUFFER_BIT);
186 // build background layer
187 glUseProgram(0);
188 fboOrigBack.exec({
189 glDisable(GL_BLEND);
190 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
191 glClear(GL_COLOR_BUFFER_BIT);
192 //glEnable(GL_BLEND);
193 //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
194 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
195 orthoCamera(map.width*8, map.height*8);
196 glEnable(GL_BLEND);
197 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
198 // draw sky
200 drawAtXY(map.skytexgl.tid, mapOfsX/2-map.MapSize*8, mapOfsY/2-map.MapSize*8, map.MapSize*8, map.MapSize*8);
201 drawAtXY(map.skytexgl.tid, mapOfsX/2-map.MapSize*8, mapOfsY/2, map.MapSize*8, map.MapSize*8);
202 drawAtXY(map.skytexgl.tid, mapOfsX/2, mapOfsY/2-map.MapSize*8, map.MapSize*8, map.MapSize*8);
203 drawAtXY(map.skytexgl.tid, mapOfsX/2, mapOfsY/2, map.MapSize*8, map.MapSize*8);
205 drawAtXY(map.skytexgl.tid, 0, 0, map.MapSize*8, map.MapSize*8);
206 // draw background
207 drawAtXY(map.texgl.ptr[map.Back], 0, 0);
208 // draw distorted liquid areas
209 shadLiquidDistort.exec({
210 __gshared float iGlobalTime = 0.0;
211 iGlobalTime += 0.04;
212 shadLiquidDistort["iGlobalTime"] = iGlobalTime;
213 shadLiquidDistort["liquidColorMul"] = SVec4F(1.0f, 1.0f, 1.0f, 1.0f);
214 drawAtXY(map.texgl.ptr[map.Water], 0, 0);
215 shadLiquidDistort["liquidColorMul"] = SVec4F(0.6f, 0.6f, 0.6f, 1.0f);
216 drawAtXY(map.texgl.ptr[map.Lava], 0, 0);
217 shadLiquidDistort["liquidColorMul"] = SVec4F(1.0f, 1.0f, 1.0f, 1.0f);
218 drawAtXY(map.texgl.ptr[map.Acid], 0, 0);
220 // monsters, items
221 glColor3f(1.0f, 1.0f, 1.0f);
223 foreach (auto actor; actors) {
224 if (actor.frame is null || actor.dead) continue;
225 //conwriteln("(", actor.x, ", ", actor.y, ") ", actor.frame.tex.tid, " ", actor.frame.tex.width, "x", actor.frame.tex.height);
226 //drawAtXY(actor.frame.tex, actor.x/*-actor.frame.vga.sx*/, actor.y/*-actor.frame.vga.sy*/);
227 //int y = LevelMap.MapSize*8-actor.y-1/*-(actor.height-actor.frame.vga.sy)*/;
228 //int y = LevelMap.MapSize*8-actor.y+(actor.frame.vga.sy-actor.frame.vga.height);
229 int y = actor.y-actor.frame.vga.sy;
230 drawAtXY(actor.frame.tex, actor.x-actor.frame.vga.sx, y);
233 Actor.forEach((ActorId me) {
234 // `act` is always valid here
235 if (auto adef = findActorDef(me.classtype!StrId, me.classname!StrId)) {
236 if (auto isp = adef.animSpr(me.zAnimstate!StrId, me.dir!uint, me.zAnimidx!int)) {
237 int y = me.y!int-isp.vga.sy;
238 drawAtXY(isp.tex, me.x!int-isp.vga.sx, y);
239 //conwriteln("found animation for actor ", me.id, " (", me.classtype!string, ":", me.classname!string, ")", " (", adef.fullname, ")");
240 } else {
241 conwriteln("no animation for actor ", me.id, " (", me.classtype!string, ":", me.classname!string, ")");
243 } else {
244 conwriteln("not found actor ", me.id, " (", me.classtype!string, ":", me.classname!string, ")");
249 // additive lights
250 glEnable(GL_BLEND);
251 glBlendFunc(GL_SRC_ALPHA, GL_ONE);
253 glActiveTexture(GL_TEXTURE0+1);
254 glBindTexture(GL_TEXTURE_2D, /*map.texgl.ptr[map.Back].tid*/fboOrigBack.tex.tid);
255 //glBindTexture(GL_TEXTURE_2D, map.texgl.ptr[map.Back].tid);
256 glActiveTexture(GL_TEXTURE0+0);
258 renderLight( 27, 391-0, SVec4F(0.0f, 0.0f, 0.0f, 0.0f), 100);
259 renderLight(542, 424-0, SVec4F(0.0f, 0.0f, 0.0f, 0.0f), 100);
260 renderLight(377, 368-0, SVec4F(0.0f, 0.0f, 0.0f, 0.0f), 32);
261 renderLight(147, 288-0, SVec4F(0.0f, 0.0f, 0.0f, 0.0f), 64);
262 renderLight( 71, 200-0, SVec4F(0.0f, 0.0f, 0.0f, 0.0f), 128);
263 renderLight(249, 200-0, SVec4F(0.0f, 0.0f, 0.0f, 0.0f), 128);
264 renderLight(426, 200-0, SVec4F(0.0f, 0.0f, 0.0f, 0.0f), 128);
265 renderLight(624, 200-0, SVec4F(0.0f, 0.0f, 0.0f, 0.0f), 128);
266 renderLight(549, 298-0, SVec4F(0.0f, 0.0f, 0.0f, 0.0f), 64);
267 renderLight( 74, 304-0, SVec4F(0.0f, 0.0f, 0.0f, 0.0f), 32);
269 renderLight(24*8+4, (24+18)*8-2, SVec4F(0.6f, 0.0f, 0.0f, 1.0f), 128);
270 foreach (immutable _; 0..1) {
271 renderLight(lightX, lightY, SVec4F(0.3f, 0.3f, 0.0f, 1.0f), 256);
274 //glActiveTexture(GL_TEXTURE0+1);
275 //glBindTexture(GL_TEXTURE_2D, 0);
276 //glActiveTexture(GL_TEXTURE0+0);
278 // draw final opaque and full-lit parts
279 fboLevel.exec({
280 glEnable(GL_BLEND);
281 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
282 glColor3f(1.0f, 1.0f, 1.0f);
283 orthoCamera(map.width*8, map.height*8);
284 // various occluders
285 drawAtXY(map.texgl.ptr[map.LightMask], 0, 0);
286 // foreground -- hide secrets, draw lifts and such
287 drawAtXY(map.texgl.ptr[map.Front], 0, 0);
288 // now do liquid coloring
289 shadLiquidColor.exec({
290 // additive blend
291 //glEnable(GL_BLEND);
292 // glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
293 //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
294 //glDisable(GL_BLEND);
295 //glColor3f(1.0f, 1.0f, 1.0f);
296 shadLiquidColor["liquidColor"] = SVec4F(0.0f, 0.0f, 0.4f, 0.5f);
297 drawAtXY(map.texgl.ptr[map.Water], 0, 0);
298 shadLiquidColor["liquidColor"] = SVec4F(0.6f, 0.0f, 0.0f, 0.4f);
299 drawAtXY(map.texgl.ptr[map.Lava], 0, 0);
300 shadLiquidColor["liquidColor"] = SVec4F(0.1f, 0.4f, 0.0f, 0.5f);
301 drawAtXY(map.texgl.ptr[map.Acid], 0, 0);
305 // draw scaled level
306 shadScanlines.exec({
307 shadScanlines["scanlines"] = scanlines;
308 glClearColor(BackIntens, BackIntens, BackIntens, 1.0f);
309 glClear(GL_COLOR_BUFFER_BIT);
310 orthoCamera(vlWidth, vlHeight);
311 //orthoCamera(map.width*8*scale, map.height*8*scale);
312 //glMatrixMode(GL_MODELVIEW);
313 //glLoadIdentity();
314 //glTranslatef(0.375, 0.375, 0); // to be pixel-perfect
315 // somehow, FBO objects are mirrored; wtf?!
316 drawAtXY(fboLevel.tex.tid, -mapOfsX, -mapOfsY, map.width*8*scale, map.height*8*scale, mirrorY:true);
317 //glLoadIdentity();
322 // ////////////////////////////////////////////////////////////////////////// //
323 // rendering thread
324 shared int diedie = 0;
327 void renderThread () {
328 try {
329 MonoTime prevFrameStartTime = MonoTime.currTime;
330 version(use_vsync) {} else MonoTime ltt = MonoTime.currTime;
331 MonoTime lasttime = MonoTime.currTime;
332 MonoTime time;
333 enum MaxFPSFrames = 16;
334 float frtimes = 0.0f;
335 int framenum = 0;
336 int prevFPS = -1;
337 bool first = true;
338 for (;;) {
339 if (sdwindow.closed) break;
340 if (atomicLoad(diedie) > 0) break;
342 time = MonoTime.currTime;
343 version(use_vsync) {
344 } else {
345 // max 60 FPS; capped by vsync
346 //{ import core.stdc.stdio; printf(" spent only %d msecs\n", cast(int)((time-ltt).total!"msecs")); }
347 if (!first && (time-ltt).total!"msecs" < 16) {
348 //{ import core.stdc.stdio; printf(" spent only %d msecs\n", cast(int)((time-ltt).total!"msecs")); }
349 import core.sys.posix.signal : timespec;
350 import core.sys.posix.time : nanosleep;
351 timespec ts = void;
352 ts.tv_sec = 0;
353 ts.tv_nsec = (16-cast(int)((time-ltt).total!"msecs"))*1000*1000; // milli to nano
354 nanosleep(&ts, null); // idc how much time was passed
355 time = MonoTime.currTime;
357 ltt = time;
358 first = false;
362 auto frameTime = cast(float)(time-prevFrameStartTime).total!"msecs"/1000.0f;
363 prevFrameStartTime = time;
365 //{ import core.stdc.stdio; printf("frametime: %f\n", frameTime*1000.0f); }
366 //{ import core.stdc.stdio; printf("FPS: %d\n", cast(int)(1.0f/frameTime)); }
367 frtimes += frameTime;
368 if (++framenum >= MaxFPSFrames || frtimes >= 3.0f) {
369 import std.string : format;
370 int newFPS = cast(int)(cast(float)MaxFPSFrames/frtimes+0.5);
371 if (newFPS != prevFPS) {
372 sdwindow.title = "%s / FPS:%s".format("D2D", newFPS);
373 prevFPS = newFPS;
375 framenum = 0;
376 frtimes = 0.0f;
379 bool ctset;
381 sdwindow.mtLock();
382 scope(exit) sdwindow.mtUnlock();
383 ctset = sdwindow.setAsCurrentOpenGlContextNT;
385 if (!ctset) {
386 { import core.stdc.stdio; printf(" FUUUU\n"); }
387 //glXMakeCurrent(sdwindow.display, 0, null);
388 import core.sys.posix.signal : timespec;
389 import core.sys.posix.time : nanosleep;
390 timespec ts = void;
391 ts.tv_sec = 0;
392 ts.tv_nsec = 16*1000*1000; // milli to nano
393 nanosleep(&ts, null); // idc how much time was passed
394 time = MonoTime.currTime;
395 } else {
396 __gshared frp = 0;
397 if (--frp <= 0) {
398 frp = 3;
399 //map.doAllActorTicks();
401 renderScene();
403 sdwindow.mtLock();
404 scope(exit) sdwindow.mtUnlock();
405 sdwindow.swapOpenGlBuffers();
406 glFinish();
407 sdwindow.releaseCurrentOpenGlContext();
412 } catch (Exception e) {
413 import core.stdc.stdio;
414 fprintf(stderr, "FUUUUUUUUUUUUUUUUUUUUUUUUUU\n");
415 for (;;) {
416 if (sdwindow.closed) break;
417 if (atomicLoad(diedie) > 0) break;
418 //{ import core.stdc.stdio; printf(" spent only %d msecs\n", cast(int)((time-ltt).total!"msecs")); }
419 import core.sys.posix.signal : timespec;
420 import core.sys.posix.time : nanosleep;
421 timespec ts = void;
422 ts.tv_sec = 0;
423 ts.tv_nsec = 100*1000*1000; // milli to nano
424 nanosleep(&ts, null); // idc how much time was passed
427 atomicStore(diedie, 2);
431 // ////////////////////////////////////////////////////////////////////////// //
432 void closeWindow () {
433 if (atomicLoad(diedie) != 2) {
434 atomicStore(diedie, 1);
435 while (atomicLoad(diedie) != 2) {}
437 if (!sdwindow.closed) {
438 flushGui();
439 sdwindow.close();
444 // ////////////////////////////////////////////////////////////////////////// //
445 __gshared Thread renderTid;
448 void main (string[] args) {
449 FuncPool.dumpCode = false;
450 FuncPool.dumpCodeSize = false;
451 dacsDumpSemantic = false;
452 dacsOptimize = 9;
453 bool compileOnly = false;
455 for (usize idx = 1; idx < args.length; ++idx) {
456 bool remove = true;
457 if (args[idx] == "--dump-code") FuncPool.dumpCode = true;
458 else if (args[idx] == "--dump-code-size") FuncPool.dumpCodeSize = true;
459 else if (args[idx] == "--dump-semantic") dacsDumpSemantic = true;
460 else if (args[idx] == "--compile") compileOnly = true;
461 else if (args[idx] == "--messages") ++dacsMessages;
462 else if (args[idx].length > 2 && args[idx][0..2] == "-O") {
463 import std.conv : to;
464 ubyte olevel = to!ubyte(args[idx][2..$]);
465 dacsOptimize = olevel;
467 else remove = false;
468 if (remove) {
469 foreach (immutable c; idx+1..args.length) args.ptr[c-1] = args.ptr[c];
470 args.length -= 1;
471 --idx; //hack
475 static void setDP () {
476 version(rdmd) {
477 setDataPath("data");
478 } else {
479 import std.file : thisExePath;
480 import std.path : dirName;
481 setDataPath(thisExePath.dirName);
483 addWad("/home/ketmar/k8prj/doom2d-tl/data/doom2d.wad"); loadWadScripts();
484 //addWad("/home/ketmar/k8prj/doom2d-tl/data/meat.wad"); loadWadScripts();
485 //addWad("/home/ketmar/k8prj/doom2d-tl/data/megadm.wad"); loadWadScripts();
486 //addWad("/home/ketmar/k8prj/doom2d-tl/data/megadm1.wad"); loadWadScripts();
487 //addWad("/home/ketmar/k8prj/doom2d-tl/data/superdm.wad"); loadWadScripts();
488 //addWad("/home/ketmar/k8prj/doom2d-tl/data/zadoomka.wad"); loadWadScripts();
491 setDP();
493 if (compileOnly) return;
495 registerAPI();
496 loadPalette();
498 setOpenGLContextVersion(3, 2); // up to GLSL 150
499 //openGLContextCompatible = false;
501 map = new LevelMap("maps/map01.d2m");
503 //mapOfsX = 8*26;
504 //mapOfsY = 8*56;
505 map.getThingPos(1/*ThingId.Player1*/, &mapOfsX, &mapOfsY);
506 // fix viewport
507 mapOfsX = (mapOfsX*2)-vlWidth/2;
508 if (mapOfsX+vlWidth > map.width*16) mapOfsX = map.width*16-vlWidth;
509 if (mapOfsX < 0) mapOfsX = 0;
510 mapOfsY = (mapOfsY*2)-vlHeight/2;
511 if (mapOfsY+vlHeight > map.height*16) mapOfsY = map.height*16-vlHeight;
512 if (mapOfsY < 0) mapOfsY = 0;
513 scale = 2;
515 sdwindow = new SimpleWindow(vlWidth, vlHeight, "D2D", OpenGlOptions.yes, Resizablity.fixedSize);
517 sdwindow.visibleForTheFirstTime = delegate () {
518 sdwindow.setAsCurrentOpenGlContext(); // make this window active
519 glbindLoadFunctions();
522 import core.stdc.stdio;
523 printf("GL version: %s\n", glGetString(GL_VERSION));
524 GLint l, h;
525 glGetIntegerv(GL_MAJOR_VERSION, &h);
526 glGetIntegerv(GL_MINOR_VERSION, &l);
527 printf("version: %d.%d\n", h, l);
528 printf("shader version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
529 GLint svcount;
530 glGetIntegerv(GL_NUM_SHADING_LANGUAGE_VERSIONS, &svcount);
531 if (svcount > 0) {
532 printf("%d shader versions supported:\n", svcount);
533 foreach (GLuint n; 0..svcount) printf(" %d: %s\n", n, glGetStringi(GL_SHADING_LANGUAGE_VERSION, n));
536 GLint ecount;
537 glGetIntegerv(GL_NUM_EXTENSIONS, &ecount);
538 if (ecount > 0) {
539 printf("%d extensions supported:\n", ecount);
540 foreach (GLuint n; 0..ecount) printf(" %d: %s\n", n, glGetStringi(GL_EXTENSIONS, n));
545 // check if we have sufficient shader version here
547 bool found = false;
548 GLint svcount;
549 glGetIntegerv(GL_NUM_SHADING_LANGUAGE_VERSIONS, &svcount);
550 if (svcount > 0) {
551 foreach (GLuint n; 0..svcount) {
552 import core.stdc.string : strncmp;
553 auto v = glGetStringi(GL_SHADING_LANGUAGE_VERSION, n);
554 if (v is null) continue;
555 if (strncmp(v, "120", 3) != 0) continue;
556 if (v[3] > ' ') continue;
557 found = true;
558 break;
561 if (!found) assert(0, "can't find OpenGL GLSL 120");
563 auto adr = glGetProcAddress("glTexParameterf");
564 if (adr is null) assert(0);
567 sdwindow.vsync = false;
568 //sdwindow.useGLFinish = false;
569 initOpenGL();
570 //sdwindow.redrawOpenGlScene();
571 if (!sdwindow.releaseCurrentOpenGlContext()) { import core.stdc.stdio; printf("can't release OpenGL context(1)\n"); }
572 if (!renderTid) {
573 renderTid = new Thread(&renderThread);
574 renderTid.start();
578 //sdwindow.redrawOpenGlScene = delegate () { renderScene(); };
580 enum MSecsPerFrame = 1000/30; /* 30 is FPS */
582 uint[8] frameTimes = 1000;
583 enum { Left, Right, Up, Down }
584 bool[4] pressed = false;
586 sdwindow.eventLoop(MSecsPerFrame,
587 delegate () {
588 if (sdwindow.closed) return;
589 if (pressed[Left]) mapOfsX -= 8;
590 if (pressed[Right]) mapOfsX += 8;
591 if (pressed[Up]) mapOfsY -= 8;
592 if (pressed[Down]) mapOfsY += 8;
593 import std.math : cos, sin;
594 __gshared float itime = 0.0;
595 itime += 0.02;
596 if (movement) {
597 mapOfsX = cast(int)(800.0/2.0+cos(itime)*220.0);
598 mapOfsY = cast(int)(800.0/2.0+120.0+sin(itime)*160.0);
600 if (scale == 1) mapOfsX = mapOfsY = 0;
601 //sdwindow.redrawOpenGlSceneNow();
603 delegate (KeyEvent event) {
604 if (sdwindow.closed) return;
605 if (event.pressed && event.key == Key.Escape) { closeWindow(); return; }
606 switch (event.key) {
607 case Key.Left: pressed[Left] = event.pressed; break;
608 case Key.Right: pressed[Right] = event.pressed; break;
609 case Key.Up: pressed[Up] = event.pressed; break;
610 case Key.Down: pressed[Down] = event.pressed; break;
611 default:
614 delegate (MouseEvent event) {
615 lightX = event.x/scale;
616 lightY = event.y/scale;
617 lightX += mapOfsX/scale;
618 lightY += mapOfsY/scale;
620 delegate (dchar ch) {
621 if (ch == 'q') { closeWindow(); return; }
622 if (ch == 's') scanlines = !scanlines;
623 if (ch == '1') scale = 1;
624 if (ch == '2') scale = 2;
625 if (ch == ' ') movement = !movement;
628 closeWindow();
629 flushGui();