now using PK3 instead of .WAD
[dd2d.git] / xmain_d2d.d
blobe8fc1206d285b13af16faea9ac6ad6535678d171
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 lastthink = MonoTime.currTime;
333 MonoTime time;
334 enum MaxFPSFrames = 16;
335 float frtimes = 0.0f;
336 int framenum = 0;
337 int prevFPS = -1;
338 bool first = true;
339 for (;;) {
340 if (sdwindow.closed) break;
341 if (atomicLoad(diedie) > 0) break;
344 time = MonoTime.currTime;
347 if ((time-lastthink).total!"msecs" >= 55) {
348 lastthink = time;
349 doActorsThink();
353 version(use_vsync) {
354 } else {
355 // max 60 FPS; capped by vsync
356 //{ import core.stdc.stdio; printf(" spent only %d msecs\n", cast(int)((time-ltt).total!"msecs")); }
357 if (!first && (time-ltt).total!"msecs" < 16) {
358 //{ import core.stdc.stdio; printf(" spent only %d msecs\n", cast(int)((time-ltt).total!"msecs")); }
359 import core.sys.posix.signal : timespec;
360 import core.sys.posix.time : nanosleep;
361 timespec ts = void;
362 ts.tv_sec = 0;
363 ts.tv_nsec = (16-cast(int)((time-ltt).total!"msecs"))*1000*1000; // milli to nano
364 nanosleep(&ts, null); // idc how much time was passed
365 time = MonoTime.currTime;
366 } else if (!first) {
367 { import core.stdc.stdio; printf(" spent whole %d msecs\n", cast(int)((time-ltt).total!"msecs")); }
369 ltt = time;
370 first = false;
374 auto frameTime = cast(float)(time-prevFrameStartTime).total!"msecs"/1000.0f;
375 prevFrameStartTime = time;
377 //{ import core.stdc.stdio; printf("frametime: %f\n", frameTime*1000.0f); }
378 //{ import core.stdc.stdio; printf("FPS: %d\n", cast(int)(1.0f/frameTime)); }
379 frtimes += frameTime;
380 if (++framenum >= MaxFPSFrames || frtimes >= 3.0f) {
381 import std.string : format;
382 int newFPS = cast(int)(cast(float)MaxFPSFrames/frtimes+0.5);
383 if (newFPS != prevFPS) {
384 sdwindow.title = "%s / FPS:%s".format("D2D", newFPS);
385 prevFPS = newFPS;
387 framenum = 0;
388 frtimes = 0.0f;
391 bool ctset;
393 sdwindow.mtLock();
394 scope(exit) sdwindow.mtUnlock();
395 ctset = sdwindow.setAsCurrentOpenGlContextNT;
397 if (!ctset) {
398 { import core.stdc.stdio; printf(" FUUUU\n"); }
399 //glXMakeCurrent(sdwindow.display, 0, null);
400 import core.sys.posix.signal : timespec;
401 import core.sys.posix.time : nanosleep;
402 timespec ts = void;
403 ts.tv_sec = 0;
404 ts.tv_nsec = 16*1000*1000; // milli to nano
405 nanosleep(&ts, null); // idc how much time was passed
406 time = MonoTime.currTime;
407 } else {
408 __gshared frp = 0;
409 if (--frp <= 0) {
410 frp = 3;
411 //map.doAllActorTicks();
413 renderScene();
415 sdwindow.mtLock();
416 scope(exit) sdwindow.mtUnlock();
417 sdwindow.swapOpenGlBuffers();
418 glFinish();
419 sdwindow.releaseCurrentOpenGlContext();
424 } catch (Exception e) {
425 import core.stdc.stdio;
426 fprintf(stderr, "FUUUUUUUUUUUUUUUUUUUUUUUUUU\n");
427 for (;;) {
428 if (sdwindow.closed) break;
429 if (atomicLoad(diedie) > 0) break;
430 //{ import core.stdc.stdio; printf(" spent only %d msecs\n", cast(int)((time-ltt).total!"msecs")); }
431 import core.sys.posix.signal : timespec;
432 import core.sys.posix.time : nanosleep;
433 timespec ts = void;
434 ts.tv_sec = 0;
435 ts.tv_nsec = 100*1000*1000; // milli to nano
436 nanosleep(&ts, null); // idc how much time was passed
439 atomicStore(diedie, 2);
443 // ////////////////////////////////////////////////////////////////////////// //
444 void closeWindow () {
445 if (atomicLoad(diedie) != 2) {
446 atomicStore(diedie, 1);
447 while (atomicLoad(diedie) != 2) {}
449 if (!sdwindow.closed) {
450 flushGui();
451 sdwindow.close();
456 // ////////////////////////////////////////////////////////////////////////// //
457 __gshared Thread renderTid;
460 void main (string[] args) {
461 FuncPool.dumpCode = false;
462 FuncPool.dumpCodeSize = false;
463 dacsDumpSemantic = false;
464 dacsOptimize = 9;
465 version(rdmd) { dacsOptimize = 0; }
466 bool compileOnly = false;
468 for (usize idx = 1; idx < args.length; ++idx) {
469 bool remove = true;
470 if (args[idx] == "--dump-code") FuncPool.dumpCode = true;
471 else if (args[idx] == "--dump-code-size") FuncPool.dumpCodeSize = true;
472 else if (args[idx] == "--dump-semantic") dacsDumpSemantic = true;
473 else if (args[idx] == "--compile") compileOnly = true;
474 else if (args[idx] == "--messages") ++dacsMessages;
475 else if (args[idx].length > 2 && args[idx][0..2] == "-O") {
476 import std.conv : to;
477 ubyte olevel = to!ubyte(args[idx][2..$]);
478 dacsOptimize = olevel;
480 else remove = false;
481 if (remove) {
482 foreach (immutable c; idx+1..args.length) args.ptr[c-1] = args.ptr[c];
483 args.length -= 1;
484 --idx; //hack
488 static void setDP () {
489 version(rdmd) {
490 setDataPath("data");
491 } else {
492 import std.file : thisExePath;
493 import std.path : dirName;
494 setDataPath(thisExePath.dirName);
496 addPK3(getDataPath~"base.pk3"); loadWadScripts();
497 //addWad("/home/ketmar/k8prj/doom2d-tl/data/doom2d.wad"); loadWadScripts();
498 //addWad("/home/ketmar/k8prj/doom2d-tl/data/meat.wad"); loadWadScripts();
499 //addWad("/home/ketmar/k8prj/doom2d-tl/data/megadm.wad"); loadWadScripts();
500 //addWad("/home/ketmar/k8prj/doom2d-tl/data/megadm1.wad"); loadWadScripts();
501 //addWad("/home/ketmar/k8prj/doom2d-tl/data/superdm.wad"); loadWadScripts();
502 //addWad("/home/ketmar/k8prj/doom2d-tl/data/zadoomka.wad"); loadWadScripts();
505 setDP();
507 if (compileOnly) return;
509 registerAPI();
510 loadPalette();
512 setOpenGLContextVersion(3, 2); // up to GLSL 150
513 //openGLContextCompatible = false;
515 map = new LevelMap("maps/map01.d2m");
517 //mapOfsX = 8*26;
518 //mapOfsY = 8*56;
519 map.getThingPos(1/*ThingId.Player1*/, &mapOfsX, &mapOfsY);
520 // fix viewport
521 mapOfsX = (mapOfsX*2)-vlWidth/2;
522 if (mapOfsX+vlWidth > map.width*16) mapOfsX = map.width*16-vlWidth;
523 if (mapOfsX < 0) mapOfsX = 0;
524 mapOfsY = (mapOfsY*2)-vlHeight/2;
525 if (mapOfsY+vlHeight > map.height*16) mapOfsY = map.height*16-vlHeight;
526 if (mapOfsY < 0) mapOfsY = 0;
527 scale = 2;
529 sdwindow = new SimpleWindow(vlWidth, vlHeight, "D2D", OpenGlOptions.yes, Resizablity.fixedSize);
531 sdwindow.visibleForTheFirstTime = delegate () {
532 sdwindow.setAsCurrentOpenGlContext(); // make this window active
533 glbindLoadFunctions();
536 import core.stdc.stdio;
537 printf("GL version: %s\n", glGetString(GL_VERSION));
538 GLint l, h;
539 glGetIntegerv(GL_MAJOR_VERSION, &h);
540 glGetIntegerv(GL_MINOR_VERSION, &l);
541 printf("version: %d.%d\n", h, l);
542 printf("shader version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
543 GLint svcount;
544 glGetIntegerv(GL_NUM_SHADING_LANGUAGE_VERSIONS, &svcount);
545 if (svcount > 0) {
546 printf("%d shader versions supported:\n", svcount);
547 foreach (GLuint n; 0..svcount) printf(" %d: %s\n", n, glGetStringi(GL_SHADING_LANGUAGE_VERSION, n));
550 GLint ecount;
551 glGetIntegerv(GL_NUM_EXTENSIONS, &ecount);
552 if (ecount > 0) {
553 printf("%d extensions supported:\n", ecount);
554 foreach (GLuint n; 0..ecount) printf(" %d: %s\n", n, glGetStringi(GL_EXTENSIONS, n));
559 // check if we have sufficient shader version here
561 bool found = false;
562 GLint svcount;
563 glGetIntegerv(GL_NUM_SHADING_LANGUAGE_VERSIONS, &svcount);
564 if (svcount > 0) {
565 foreach (GLuint n; 0..svcount) {
566 import core.stdc.string : strncmp;
567 auto v = glGetStringi(GL_SHADING_LANGUAGE_VERSION, n);
568 if (v is null) continue;
569 if (strncmp(v, "120", 3) != 0) continue;
570 if (v[3] > ' ') continue;
571 found = true;
572 break;
575 if (!found) assert(0, "can't find OpenGL GLSL 120");
577 auto adr = glGetProcAddress("glTexParameterf");
578 if (adr is null) assert(0);
581 sdwindow.vsync = false;
582 //sdwindow.useGLFinish = false;
583 initOpenGL();
584 //sdwindow.redrawOpenGlScene();
585 if (!sdwindow.releaseCurrentOpenGlContext()) { import core.stdc.stdio; printf("can't release OpenGL context(1)\n"); }
586 if (!renderTid) {
587 renderTid = new Thread(&renderThread);
588 renderTid.start();
592 //sdwindow.redrawOpenGlScene = delegate () { renderScene(); };
594 enum MSecsPerFrame = 1000/30; /* 30 is FPS */
596 uint[8] frameTimes = 1000;
597 enum { Left, Right, Up, Down }
598 bool[4] pressed = false;
600 sdwindow.eventLoop(MSecsPerFrame,
601 delegate () {
602 if (sdwindow.closed) return;
603 if (pressed[Left]) mapOfsX -= 8;
604 if (pressed[Right]) mapOfsX += 8;
605 if (pressed[Up]) mapOfsY -= 8;
606 if (pressed[Down]) mapOfsY += 8;
607 import std.math : cos, sin;
608 __gshared float itime = 0.0;
609 itime += 0.02;
610 if (movement) {
611 mapOfsX = cast(int)(800.0/2.0+cos(itime)*220.0);
612 mapOfsY = cast(int)(800.0/2.0+120.0+sin(itime)*160.0);
614 if (scale == 1) mapOfsX = mapOfsY = 0;
615 //sdwindow.redrawOpenGlSceneNow();
617 delegate (KeyEvent event) {
618 if (sdwindow.closed) return;
619 if (event.pressed && event.key == Key.Escape) { closeWindow(); return; }
620 switch (event.key) {
621 case Key.Left: pressed[Left] = event.pressed; break;
622 case Key.Right: pressed[Right] = event.pressed; break;
623 case Key.Up: pressed[Up] = event.pressed; break;
624 case Key.Down: pressed[Down] = event.pressed; break;
625 default:
628 delegate (MouseEvent event) {
629 lightX = event.x/scale;
630 lightY = event.y/scale;
631 lightX += mapOfsX/scale;
632 lightY += mapOfsY/scale;
634 delegate (dchar ch) {
635 if (ch == 'q') { closeWindow(); return; }
636 if (ch == 's') scanlines = !scanlines;
637 if (ch == '1') scale = 1;
638 if (ch == '2') scale = 2;
639 if (ch == ' ') movement = !movement;
642 closeWindow();
643 flushGui();