1 /* DooM2D: Midnight on the Firing Line
2 * coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
3 * Understanding is not required. Only obedience.
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 module render
is aliced
;
20 //version = dont_use_vsync;
27 import std
.concurrency
;
51 // ////////////////////////////////////////////////////////////////////////// //
56 // ////////////////////////////////////////////////////////////////////////// //
57 public __gshared
bool cheatNoDoors
= false;
58 public __gshared
bool cheatNoWallClip
= false;
61 // ////////////////////////////////////////////////////////////////////////// //
62 public __gshared SimpleWindow sdwindow
;
65 public enum vlWidth
= 800;
66 public enum vlHeight
= 800;
67 __gshared
int scale
= 2;
69 public int getScale () nothrow @trusted @nogc { pragma(inline
, true); return scale
; }
72 // ////////////////////////////////////////////////////////////////////////// //
73 __gshared
bool levelLoaded
= false;
76 // ////////////////////////////////////////////////////////////////////////// //
77 __gshared
bool scanlines
= false;
78 __gshared
bool doLighting
= true;
79 __gshared
bool gamePaused
= false;
80 shared bool editMode
= false;
82 public @property bool inEditMode () nothrow @trusted @nogc { import core
.atomic
; return atomicLoad(editMode
); }
83 @property void inEditMode (bool v
) nothrow @trusted @nogc { import core
.atomic
; atomicStore(editMode
, v
); }
86 // ////////////////////////////////////////////////////////////////////////// //
88 __gshared
ubyte[] prevFrameActorsData
;
89 __gshared
uint[65536] prevFrameActorOfs
; // uint.max-1: dead; uint.max: last
90 __gshared MonoTime lastthink
= MonoTime
.zero
; // for interpolator
91 __gshared MonoTime nextthink
= MonoTime
.zero
;
92 __gshared
bool frameInterpolation
= true;
95 // ////////////////////////////////////////////////////////////////////////// //
97 struct AttachedLightInfo
{
104 int w
, h
; // for ambient lights
110 __gshared AttachedLightInfo
[65536] attachedLights
;
111 __gshared
uint attachedLightCount
= 0;
114 // ////////////////////////////////////////////////////////////////////////// //
115 enum MaxLightRadius
= 255;
118 // ////////////////////////////////////////////////////////////////////////// //
120 __gshared FBO
[MaxLightRadius
+1] fboDistMap
;
121 __gshared FBO fboOccluders
;
122 __gshared Shader shadToPolar
, shadBlur
, shadBlurOcc
, shadAmbient
;
123 __gshared TrueColorImage editorImg
;
124 __gshared FBO fboEditor
;
126 __gshared FBO fboLevel
, fboLevelLight
, fboOrigBack
, fboLMaskSmall
;
127 __gshared Shader shadScanlines
;
128 __gshared Shader shadLiquidDistort
;
131 // ////////////////////////////////////////////////////////////////////////// //
133 public void initOpenGL () {
136 glEnable(GL_TEXTURE_2D
);
137 glDisable(GL_LIGHTING
);
138 glDisable(GL_DITHER
);
140 glDisable(GL_DEPTH_TEST
);
143 shadScanlines
= new Shader("scanlines", loadTextFile("shaders/srscanlines.frag"));
145 shadLiquidDistort
= new Shader("liquid_distort", loadTextFile("shaders/srliquid_distort.frag"));
146 shadLiquidDistort
.exec((Shader shad
) {
147 shad
["texLqMap"] = 0;
151 shadToPolar
= new Shader("light_trace", loadTextFile("shaders/srlight_trace.frag"));
152 shadToPolar
.exec((Shader shad
) {
154 shad
["texOccFull"] = 2;
155 shad
["texOccSmall"] = 3;
158 shadBlur
= new Shader("light_blur", loadTextFile("shaders/srlight_blur.frag"));
159 shadBlur
.exec((Shader shad
) {
163 shad
["texOccSmall"] = 3;
166 shadBlurOcc
= new Shader("light_blur_occ", loadTextFile("shaders/srlight_blur_occ.frag"));
167 shadBlurOcc
.exec((Shader shad
) {
171 shad
["texOccSmall"] = 3;
174 shadAmbient
= new Shader("light_ambient", loadTextFile("shaders/srlight_ambient.frag"));
175 shadAmbient
.exec((Shader shad
) {
178 shad
["texOccSmall"] = 3;
181 fboOccluders
= new FBO(MaxLightRadius
*2, MaxLightRadius
*2, Texture
.Option
.Clamp
, Texture
.Option
.Linear
);
183 foreach (int sz
; 2..MaxLightRadius
+1) {
184 fboDistMap
[sz
] = new FBO(sz
*2, 1, Texture
.Option
.Clamp
, Texture
.Option
.Linear
); // create 1d distance map FBO
187 editorImg
= new TrueColorImage(vlWidth
, vlHeight
);
188 editorImg
.imageData
.colors
[] = Color(0, 0, 0, 0);
189 fboEditor
= new FBO(vlWidth
, vlHeight
, Texture
.Option
.Nearest
);
192 glMatrixMode(GL_MODELVIEW
);
196 loadAllMonsterGraphics();
200 // ////////////////////////////////////////////////////////////////////////// //
201 // should be called when OpenGL is initialized
202 void loadMap (string mapname
) {
203 mapscripts
.runUnloading(); // "map unloading" script
206 if (map
!is null) map
.clear();
207 map
= new LevelMap(mapname
);
208 curmapname
= mapname
;
210 ugInit(map
.width
*TileSize
, map
.height
*TileSize
);
215 if (fboLevel
!is null) fboLevel
.clear();
216 if (fboLevelLight
!is null) fboLevelLight
.clear();
217 if (fboOrigBack
!is null) fboOrigBack
.clear();
218 if (fboLMaskSmall
!is null) fboLMaskSmall
.clear();
220 fboLevel
= new FBO(map
.width
*TileSize
, map
.height
*TileSize
, Texture
.Option
.Nearest
); // final level render will be here
221 fboLevelLight
= new FBO(map
.width
*TileSize
, map
.height
*TileSize
, Texture
.Option
.Nearest
); // level lights will be rendered here
222 fboOrigBack
= new FBO(map
.width
*TileSize
, map
.height
*TileSize
, Texture
.Option
.Nearest
/*, Texture.Option.Depth*/); // background+foreground
223 fboLMaskSmall
= new FBO(map
.width
, map
.height
, Texture
.Option
.Nearest
); // small lightmask
225 shadToPolar
.exec((Shader shad
) { shad
["mapPixSize"] = SVec2F(map
.width
*TileSize
-1, map
.height
*TileSize
-1); });
226 shadBlur
.exec((Shader shad
) { shad
["mapPixSize"] = SVec2F(map
.width
*TileSize
, map
.height
*TileSize
); });
227 shadBlurOcc
.exec((Shader shad
) { shad
["mapPixSize"] = SVec2F(map
.width
*TileSize
, map
.height
*TileSize
); });
228 shadAmbient
.exec((Shader shad
) { shad
["mapPixSize"] = SVec2F(map
.width
*TileSize
, map
.height
*TileSize
); });
230 glActiveTexture(GL_TEXTURE0
+0);
231 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
232 orthoCamera(vlWidth
, vlHeight
);
234 Actor
.resetStorage();
237 mapscripts
.runInit();
240 mapscripts
.runLoaded();
242 // save first snapshot
243 if (prevFrameActorsData
.length
== 0) prevFrameActorsData
= new ubyte[](Actor
.actorSize
*65536); // ~15-20 megabytes
244 prevFrameActorOfs
[] = uint.max
; // just for fun
245 Actor
.saveSnapshot(prevFrameActorsData
[], prevFrameActorOfs
.ptr
);
249 { import core
.memory
: GC
; GC
.collect(); }
253 // ////////////////////////////////////////////////////////////////////////// //
255 __gshared
uint mapTilesChanged
= 0;
258 //WARNING! this can be called only from DACS, so we don't have to sync it!
259 public void mapDirty (uint layermask
) { mapTilesChanged |
= layermask
; }
262 void rebuildMapMegaTextures () {
264 //mapTilesChanged = false;
265 //map.clearMegaTextures();
266 map
.oglBuildMega(mapTilesChanged
);
271 // ////////////////////////////////////////////////////////////////////////// //
274 enum Phase
{ FadeIn
, Stay
, FadeOut
}
283 private import core
.sync
.mutex
: Mutex
;
285 __gshared Message
[128] messages
;
286 __gshared
uint messagesUsed
= 0;
288 //__gshared Mutex messageLock;
289 //shared static this () { messageLock = new Mutex(); }
292 void addMessage (const(char)[] msgtext
, int pauseMsecs
=3000, bool noreplace
=false) {
293 //messageLock.lock();
294 //scope(exit) messageLock.unlock();
295 if (msgtext
.length
== 0) return;
297 if (pauseMsecs
<= 50) return;
298 if (messagesUsed
== messages
.length
) {
299 // remove top message
300 foreach (immutable cidx
; 1..messagesUsed
) messages
.ptr
[cidx
-1] = messages
.ptr
[cidx
];
301 messages
.ptr
[0].alpha
= 255;
305 if (!noreplace
&& messagesUsed
== 1) {
306 switch (messages
.ptr
[0].phase
) {
307 case Message
.Phase
.FadeIn
:
308 messages
.ptr
[0].phase
= Message
.Phase
.FadeOut
;
310 case Message
.Phase
.Stay
:
311 messages
.ptr
[0].phase
= Message
.Phase
.FadeOut
;
312 messages
.ptr
[0].alpha
= 255;
317 auto msg
= messages
.ptr
+messagesUsed
;
319 msg
.phase
= Message
.Phase
.FadeIn
;
321 msg
.pauseMsecs
= pauseMsecs
;
323 if (msgtext
.length
> msg
.text
.length
) {
324 msg
.text
= msgtext
[0..msg
.text
.length
];
325 msg
.textlen
= msg
.text
.length
;
327 msg
.text
[0..msgtext
.length
] = msgtext
[];
328 msg
.textlen
= msgtext
.length
;
333 void doMessages (MonoTime curtime
) {
334 //messageLock.lock();
335 //scope(exit) messageLock.unlock();
337 if (messagesUsed
== 0) return;
339 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
345 final switch (msg
.phase
) {
346 case Message
.Phase
.FadeIn
:
347 if ((msg
.alpha
+= 10) >= 255) {
348 msg
.phase
= Message
.Phase
.Stay
;
349 msg
.removeTime
= curtime
+dur
!"msecs"(msg
.pauseMsecs
);
350 goto case; // to stay
352 glColor4f(1.0f, 1.0f, 1.0f, msg
.alpha
/255.0f);
354 case Message
.Phase
.Stay
:
355 if (msg
.removeTime
<= curtime
) {
357 msg
.phase
= Message
.Phase
.FadeOut
;
358 goto case; // to fade
360 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
362 case Message
.Phase
.FadeOut
:
363 if ((msg
.alpha
-= 10) <= 0) {
364 if (--messagesUsed
== 0) return;
365 // remove this message
366 foreach (immutable cidx
; 1..messagesUsed
+1) messages
.ptr
[cidx
-1] = messages
.ptr
[cidx
];
369 glColor4f(1.0f, 1.0f, 1.0f, msg
.alpha
/255.0f);
373 smDrawText(10, 10, msg
.text
[0..msg
.textlen
]);
377 // ////////////////////////////////////////////////////////////////////////// //
378 //mixin(Actor.FieldPropMixin!("0drawlistpos", uint));
380 mixin(Actor
.FieldGetMixin
!("classtype", StrId
)); // fget_classtype
381 mixin(Actor
.FieldGetMixin
!("classname", StrId
)); // fget_classname
382 mixin(Actor
.FieldGetMixin
!("x", int));
383 mixin(Actor
.FieldGetMixin
!("y", int));
384 mixin(Actor
.FieldGetMixin
!("radius", int));
385 mixin(Actor
.FieldGetMixin
!("height", int));
386 mixin(Actor
.FieldGetMixin
!("flags", uint));
387 mixin(Actor
.FieldGetMixin
!("zAnimstate", StrId
));
388 mixin(Actor
.FieldGetMixin
!("zAnimidx", int));
389 mixin(Actor
.FieldGetMixin
!("dir", uint));
390 mixin(Actor
.FieldGetMixin
!("attLightXOfs", int));
391 mixin(Actor
.FieldGetMixin
!("attLightYOfs", int));
392 mixin(Actor
.FieldGetMixin
!("attLightRGBX", uint));
394 //mixin(Actor.FieldGetPtrMixin!("classtype", StrId)); // fget_classtype
395 //mixin(Actor.FieldGetPtrMixin!("classname", StrId)); // fget_classname
396 mixin(Actor
.FieldGetPtrMixin
!("x", int));
397 mixin(Actor
.FieldGetPtrMixin
!("y", int));
398 //mixin(Actor.FieldGetPtrMixin!("flags", uint));
399 //mixin(Actor.FieldGetPtrMixin!("zAnimstate", StrId));
400 //mixin(Actor.FieldGetPtrMixin!("zAnimidx", int));
401 //mixin(Actor.FieldGetPtrMixin!("dir", uint));
402 //mixin(Actor.FieldGetPtrMixin!("attLightXOfs", int));
403 //mixin(Actor.FieldGetPtrMixin!("attLightYOfs", int));
404 //mixin(Actor.FieldGetPtrMixin!("attLightRGBX", uint));
407 // ////////////////////////////////////////////////////////////////////////// //
408 __gshared
int vportX0
, vportY0
, vportX1
, vportY1
;
411 // ////////////////////////////////////////////////////////////////////////// //
412 void renderLightAmbient() (int lightX
, int lightY
, int lightW
, int lightH
, in auto ref SVec4F lcol
) {
413 //conwriteln("!!!000: (", lightX, ",", lightY, ")-(", lightW, ",", lightH, "): r=", cast(uint)(255.0f*lcol.r), "; g=", cast(uint)(255.0f*lcol.g), "; b=", cast(uint)(255.0f*lcol.b), "; a=", cast(uint)(255.0f*lcol.a));
414 if (lightW
< 1 || lightH
< 1) return;
415 int lightX1
= lightX
+lightW
-1;
416 int lightY1
= lightY
+lightH
-1;
417 // clip light to viewport
418 if (lightX
< vportX0
) lightX
= vportX0
;
419 if (lightY
< vportY0
) lightY
= vportY0
;
420 if (lightX1
> vportX1
) lightX1
= vportX1
;
421 if (lightY1
> vportY1
) lightY1
= vportY1
;
422 // is this light visible?
423 //conwriteln("!!!001: (", lightX, ",", lightY, ")-(", lightX1, ",", lightY1, "): r=", cast(uint)(255.0f*lcol.r), "; g=", cast(uint)(255.0f*lcol.g), "; b=", cast(uint)(255.0f*lcol.b), "; a=", cast(uint)(255.0f*lcol.a));
424 if (lightX1
< lightX || lightY1
< lightY || lightX
> vportX1 || lightY
> vportY1 || lightX1
< vportX0 || lightY1
< vportY0
) return;
425 //conwriteln("!!!002: (", lightX, ",", lightY, ")-(", lightX1, ",", lightY1, "): r=", cast(uint)(255.0f*lcol.r), "; g=", cast(uint)(255.0f*lcol.g), "; b=", cast(uint)(255.0f*lcol.b), "; a=", cast(uint)(255.0f*lcol.a));
429 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
430 //glDisable(GL_BLEND);
431 orthoCamera(map
.width
*TileSize
, map
.height
*TileSize
);
432 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
433 shadAmbient
.exec((Shader shad
) {
434 shad
["lightColor"] = SVec4F(lcol
.x
, lcol
.y
, lcol
.z
, lcol
.w
);
435 //shad["lightPos"] = SVec2F(lightX, lightY);
436 glRectf(lightX
, lightY
, lightX1
, lightY1
);
442 // ////////////////////////////////////////////////////////////////////////// //
443 void renderLight() (int lightX
, int lightY
, in auto ref SVec4F lcol
, int lightRadius
) {
444 if (lightRadius
< 2) return;
445 if (lightRadius
> MaxLightRadius
) lightRadius
= MaxLightRadius
;
446 int lightSize
= lightRadius
*2;
447 // is this light visible?
448 if (lightX
<= -lightRadius || lightY
<= -lightRadius || lightX
-lightRadius
>= map
.width
*TileSize || lightY
-lightRadius
>= map
.height
*TileSize
) return;
450 // out of viewport -- do nothing
451 if (lightX
+lightRadius
< vportX0 || lightY
+lightRadius
< vportY0
) return;
452 if (lightX
-lightRadius
> vportX1 || lightY
-lightRadius
> vportY1
) return;
454 if (lightX
>= 0 && lightY
>= 0 && lightX
< map
.width
*TileSize
&& lightY
< map
.height
*TileSize
&&
455 map
.teximgs
[map
.LightMask
].imageData
.colors
.ptr
[lightY
*(map
.width
*TileSize
)+lightX
].a
> 190) return;
457 // common color for all the following
459 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
461 // build 1d distance map to fboShadowMapId
462 fboDistMap
.ptr
[lightRadius
].exec({
463 // no need to clear it, shader will take care of that
464 shadToPolar
.exec((Shader shad
) {
465 shad
["lightTexSize"] = SVec2F(lightSize
, lightSize
);
466 shad
["lightPos"] = SVec2F(lightX
, lightY
);
467 orthoCamera(lightSize
, 1);
468 // it doesn't matter what we will draw here, so just draw filled rect
469 glRectf(0, 0, lightSize
, 1);
473 // build light texture for blending
475 // no need to clear it, shader will take care of that
477 //glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
478 //glClear(GL_COLOR_BUFFER_BIT);
479 shadBlur
.exec((Shader shad
) {
480 shad
["lightTexSize"] = SVec2F(lightSize
, MaxLightRadius
*2); // x: size of distmap; y: size of this texture
481 shad
["lightColor"] = SVec4F(lcol
.x
, lcol
.y
, lcol
.z
, lcol
.w
);
482 shad
["lightPos"] = SVec2F(lightX
-lightRadius
, lightY
-lightRadius
);
483 orthoCamera(fboOccluders
.tex
.width
, fboOccluders
.tex
.height
);
484 //drawAtXY(fboDistMap[lightRadius].tex.tid, 0, 0, lightSize, lightSize);
485 glBindTexture(GL_TEXTURE_2D
, fboDistMap
.ptr
[lightRadius
].tex
.tid
);
487 glTexCoord2f(0.0f, 0.0f); glVertex2i(0, 0); // top-left
488 glTexCoord2f(1.0f, 0.0f); glVertex2i(lightSize
, 0); // top-right
489 glTexCoord2f(1.0f, 1.0f); glVertex2i(lightSize
, lightSize
); // bottom-right
490 glTexCoord2f(0.0f, 1.0f); glVertex2i(0, lightSize
); // bottom-left
495 // blend light texture
498 //glDisable(GL_BLEND);
499 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
500 orthoCamera(fboLevelLight
.tex
.width
, fboLevelLight
.tex
.height
);
501 //drawAtXY(fboOccluders.tex, lightX-lightRadius, lightY-lightRadius, mirrorY:true);
502 float occe
= 1.0f*lightSize
/(MaxLightRadius
*2);
503 float occs
= 1.0f-occe
;
504 int x0
= lightX
-lightRadius
+0;
505 int y1
= lightY
-lightRadius
+0;
506 int x1
= lightX
+lightRadius
-1+1;
507 int y0
= lightY
+lightRadius
-1+1;
508 glBindTexture(GL_TEXTURE_2D
, fboOccluders
.tex
.tid
);
511 glTexCoord2f(0.0f, 0.0f); glVertex2i(x0, y0); // top-left
512 glTexCoord2f(occe, 0.0f); glVertex2i(x1, y0); // top-right
513 glTexCoord2f(occe, occe); glVertex2i(x1, y1); // bottom-right
514 glTexCoord2f(0.0f, occe); glVertex2i(x0, y1); // bottom-left
516 glTexCoord2f(0.0f, occs
); glVertex2i(x0
, y0
); // top-left
517 glTexCoord2f(occe
, occs
); glVertex2i(x1
, y0
); // top-right
518 glTexCoord2f(occe
, 1.0f); glVertex2i(x1
, y1
); // bottom-right
519 glTexCoord2f(0.0f, 1.0f); glVertex2i(x0
, y1
); // bottom-left
522 glBindTexture(GL_TEXTURE_2D, 0);
523 glRectf(x0, y0, x1, y1);
525 // and blend it again, with the shader that will touch only occluders
526 shadBlurOcc
.exec((Shader shad
) {
527 //shad["lightTexSize"] = SVec2F(lightSize, lightSize);
528 shad
["lightTexSize"] = SVec2F(lightSize
, fboOccluders
.tex
.height
);
529 shad
["lightColor"] = SVec4F(lcol
.x
, lcol
.y
, lcol
.z
, lcol
.w
);
530 shad
["lightPos"] = SVec2F(lightX
, lightY
);
531 //shad["lightPos"] = SVec2F(lightX-lightRadius, lightY-lightRadius);
532 glBindTexture(GL_TEXTURE_2D
, fboOccluders
.tex
.tid
);
534 glTexCoord2f(0.0f, occs
); glVertex2i(x0
, y0
); // top-left
535 glTexCoord2f(occe
, occs
); glVertex2i(x1
, y0
); // top-right
536 glTexCoord2f(occe
, 1.0f); glVertex2i(x1
, y1
); // bottom-right
537 glTexCoord2f(0.0f, 1.0f); glVertex2i(x0
, y1
); // bottom-left
539 //drawAtXY(fboOccluders.tex.tid, lightX-lightRadius, lightY-lightRadius, lightSize, lightSize, mirrorY:true);
545 // ////////////////////////////////////////////////////////////////////////// //
546 __gshared
int testLightX
= vlWidth
/2, testLightY
= vlHeight
/2;
547 __gshared
bool testLightMoved
= false;
548 //__gshared int mapOfsX, mapOfsY;
549 //__gshared bool movement = false;
550 __gshared
float iLiquidTime
= 0.0;
551 //__gshared bool altMove = false;
554 void renderScene (MonoTime curtime
) {
555 //enum BackIntens = 0.05f;
556 enum BackIntens
= 0.0f;
559 float atob
= (curtime
> lastthink ?
cast(float)((curtime
-lastthink
).total
!"msecs")/cast(float)((nextthink
-lastthink
).total
!"msecs") : 1.0f);
560 //{ import core.stdc.stdio; printf("atob=%f\n", cast(double)atob); }
563 int framelen = cast(int)((nextthink-lastthink).total!"msecs");
564 int curfp = cast(int)((curtime-lastthink).total!"msecs");
565 { import core.stdc.stdio; printf("framelen=%d; curfp=%d\n", framelen, curfp); }
569 int mofsx
, mofsy
; // camera offset, will be set in background layer builder
571 if (mapTilesChanged
!= 0) rebuildMapMegaTextures();
573 if (!dotsAtTexture
) dotDraw(atob
);
575 // build background layer
577 //glDisable(GL_BLEND);
578 //glClearDepth(1.0f);
579 //glDepthFunc(GL_LESS);
580 //glDepthFunc(GL_NEVER);
581 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
582 glClear(GL_COLOR_BUFFER_BIT
/*|GL_DEPTH_BUFFER_BIT*/);
583 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
584 orthoCamera(map
.width
*TileSize
, map
.height
*TileSize
);
587 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
590 drawAtXY(map.skytexgl.tid, mapOfsX/2-map.MapSize*TileSize, mapOfsY/2-map.MapSize*TileSize, map.MapSize*TileSize, map.MapSize*TileSize);
591 drawAtXY(map.skytexgl.tid, mapOfsX/2-map.MapSize*TileSize, mapOfsY/2, map.MapSize*TileSize, map.MapSize*TileSize);
592 drawAtXY(map.skytexgl.tid, mapOfsX/2, mapOfsY/2-map.MapSize*TileSize, map.MapSize*TileSize, map.MapSize*TileSize);
593 drawAtXY(map.skytexgl.tid, mapOfsX/2, mapOfsY/2, map.MapSize*TileSize, map.MapSize*TileSize);
595 drawAtXY(map
.skytexgl
.tid
, 0, 0, map
.MapSize
*TileSize
, map
.MapSize
*TileSize
);
597 drawAtXY(map
.texgl
.ptr
[map
.Back
], 0, 0);
598 // draw distorted liquid areas
599 shadLiquidDistort
.exec((Shader shad
) {
600 shad
["iDistortTime"] = iLiquidTime
;
601 drawAtXY(map
.texgl
.ptr
[map
.AllLiquids
], 0, 0);
603 // monsters, items; we'll do linear interpolation here
604 glColor3f(1.0f, 1.0f, 1.0f);
605 //glEnable(GL_DEPTH_TEST);
606 attachedLightCount
= 0;
608 // who cares about memory?!
609 // draw order: players, items, monsters, other
610 static struct DrawInfo
{
614 @disable this (this); // no copies
616 enum { Players
, Items
, Monsters
, Other
}
617 __gshared DrawInfo
[65536][4] drawlists
;
618 __gshared
uint[4] dlpos
;
623 Actor
.forEach((ActorId me
) {
624 //me.fprop_0drawlistpos = 0;
625 if (auto adef
= findActorDef(me
)) {
627 switch (adef
.classtype
.get
) {
628 case "monster": dlnum
= (adef
.classname
.get
!= "Player" ? Monsters
: Players
); break;
629 case "item": dlnum
= Items
; break;
630 default: dlnum
= Other
; break;
632 int actorX
, actorY
; // current actor position
634 auto ofs
= prevFrameActorOfs
.ptr
[me
.id
&0xffff];
635 if (frameInterpolation
&& ofs
< uint.max
-1 && Actor
.isSameSavedActor(me
.id
, prevFrameActorsData
.ptr
, ofs
)) {
636 import core
.stdc
.math
: roundf
;
637 auto xptr
= prevFrameActorsData
.ptr
+ofs
;
638 int ox
= xptr
.fgetp_x
;
640 int oy
= xptr
.fgetp_y
;
642 actorX
= cast(int)(ox
+roundf((nx
-ox
)*atob
));
643 actorY
= cast(int)(oy
+roundf((ny
-oy
)*atob
));
644 //conwriteln("actor ", me.id, "; o=(", ox, ",", oy, "); n=(", nx, ",", ny, "); p=(", x, ",", y, ")");
650 if (me
.id
== cameraChick
.id
) {
651 camchickdi
.adef
= adef
;
653 camchickdi
.actorX
= actorX
;
654 camchickdi
.actorY
= actorY
;
657 if ((me
.fget_flags
&AF_NODRAW
) == 0) {
658 //auto dl = &drawlists[dlnum][dlpos.ptr[dlnum]];
659 //me.fprop_0drawlistpos = (dlpos.ptr[dlnum]&0xffff)|((dlnum&0xff)<<16);
660 auto dl = drawlists
.ptr
[dlnum
].ptr
+dlpos
.ptr
[dlnum
];
667 // process attached lights
668 if ((me
.fget_flags
&AF_NOLIGHT
) == 0) {
669 uint alr
= me
.fget_attLightRGBX
;
670 bool isambient
= (me
.fget_classtype
.get
== "light" && me
.fget_classname
.get
== "Ambient");
671 if ((alr
&0xff) >= 4 ||
(isambient
&& me
.fget_radius
>= 1 && me
.fget_height
>= 1)) {
672 //if (isambient) conwriteln("isambient: ", isambient, "; x=", me.fget_x, "; y=", me.fget_y, "; w=", me.fget_radius, "; h=", me.fget_height);
674 auto li
= attachedLights
.ptr
+attachedLightCount
;
675 ++attachedLightCount
;
676 li
.type
= (!isambient ? AttachedLightInfo
.Type
.Point
: AttachedLightInfo
.Type
.Ambient
);
677 li
.x
= actorX
+me
.fget_attLightXOfs
;
678 li
.y
= actorY
+me
.fget_attLightYOfs
;
679 li
.r
= ((alr
>>24)&0xff)/255.0f; // red or intensity
680 if ((alr
&0x00_ff_ff
_00U) == 0x00_00_01_00U) {
683 li
.g
= ((alr
>>16)&0xff)/255.0f;
684 li
.b
= ((alr
>>8)&0xff)/255.0f;
685 li
.uncolored
= false;
687 li
.radius
= (alr
&0xff);
688 if (li
.radius
> MaxLightRadius
) li
.radius
= MaxLightRadius
;
690 li
.w
= me
.fget_radius
;
691 li
.h
= me
.fget_height
;
696 conwriteln("not found actor ", me
.id
, " (", me
.classtype
!string
, ":", me
.classname
!string
, ")");
701 foreach_reverse (uint dlnum
; 0..drawlists
.length
) {
702 auto dl = drawlists
.ptr
[dlnum
].ptr
;
703 if (dlnum
== Players
) dl += dlpos
.ptr
[dlnum
]-1;
704 foreach (uint idx
; 0..dlpos
.ptr
[dlnum
]) {
706 if (auto isp
= dl.adef
.animSpr(me
.fget_zAnimstate
, me
.fget_dir
, me
.fget_zAnimidx
)) {
707 //drawAtXY(isp.tex, dl.actorX-isp.vga.sx, dl.actorY-isp.vga.sy);
708 isp
.drawAtXY(dl.actorX
, dl.actorY
);
710 if (dlnum
!= Players
) ++dl; else --dl;
715 if (/*altMove || movement ||*/ scale
== 1 ||
!cameraChick
.valid
) {
720 vportX1
= map
.width
*TileSize
;
721 vportY1
= map
.height
*TileSize
;
723 int tiltHeight
= /*getMapViewHeight()*/(vlHeight
/scale
)/4;
724 int vy
= cameraChick
.looky
!int;
725 if (vy
< -tiltHeight
) vy
= -tiltHeight
; else if (vy
> tiltHeight
) vy
= tiltHeight
;
726 int swdt
= vlWidth
/scale
;
727 int shgt
= vlHeight
/scale
;
728 int x
= camchickdi
.actorX
-swdt
/2;
729 int y
= (camchickdi
.actorY
+vy
)-shgt
/2;
730 if (x
< 0) x
= 0; else if (x
>= map
.width
*TileSize
-swdt
) x
= map
.width
*TileSize
-swdt
-1;
731 if (y
< 0) y
= 0; else if (y
>= map
.height
*TileSize
-shgt
) y
= map
.height
*TileSize
-shgt
-1;
734 vportX0
= mofsx
/scale
;
735 vportY0
= mofsy
/scale
;
736 vportX1
= vportX0
+vlWidth
/scale
;
737 vportY1
= vportY0
+vlHeight
/scale
;
740 //glDisable(GL_DEPTH_TEST);
742 if (dotsAtTexture
) drawAtXY(texParts
, 0, 0); else dotDraw(atob
);
743 // do liquid coloring
744 drawAtXY(map
.texgl
.ptr
[map
.LiquidMask
], 0, 0);
745 // foreground -- hide secrets, draw lifts and such
746 drawAtXY(map
.texgl
.ptr
[map
.Front
], 0, 0);
753 glBindTexture(GL_TEXTURE_2D, 0);
754 glColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
755 glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
756 if (cameraChick.valid) {
758 glVertex2i(camchickdi.actorX, camchickdi.actorY-70);
760 //glRectf(camchickdi.actorX, camchickdi.actorY-70, camchickdi.actorX+4, camchickdi.actorY-70+4);
762 //glRectf(0, 0, 300, 300);
763 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
771 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
773 // make smaller occluder texture, so we can trace faster
774 //assert(fboLMaskSmall.tex.width == map.width);
775 //assert(fboLMaskSmall.tex.height == map.height);
777 orthoCamera(map
.width
, map
.height
);
778 drawAtXY(map
.texgl
.ptr
[map
.LightMask
].tid
, 0, 0, map
.width
, map
.height
, mirrorY
:true);
783 glClearColor(BackIntens
, BackIntens
, BackIntens
, 1.0f);
784 //glClearColor(0.15f, 0.15f, 0.15f, 1.0f);
785 ////glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
786 glClear(GL_COLOR_BUFFER_BIT
);
789 // texture 1 is background
790 glActiveTexture(GL_TEXTURE0
+1);
791 glBindTexture(GL_TEXTURE_2D
, fboOrigBack
.tex
.tid
);
792 // texture 2 is occluders
793 glActiveTexture(GL_TEXTURE0
+2);
794 glBindTexture(GL_TEXTURE_2D
, map
.texgl
.ptr
[map
.LightMask
].tid
);
795 // texture 3 is small occluder map
796 glActiveTexture(GL_TEXTURE0
+3);
797 glBindTexture(GL_TEXTURE_2D
, fboLMaskSmall
.tex
.tid
);
798 // done texture assign
799 glActiveTexture(GL_TEXTURE0
+0);
804 renderLight( 27, 391-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 100);
805 renderLight(542, 424-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 100);
806 renderLight(377, 368-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 32);
807 renderLight(147, 288-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 64);
808 renderLight( 71, 200-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 128);
809 renderLight(249, 200-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 128);
810 renderLight(426, 200-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 128);
811 renderLight(624, 200-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 128);
812 renderLight(549, 298-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 64);
813 renderLight( 74, 304-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 32);
815 renderLight(24*TileSize+4, (24+18)*TileSize-2+LYOfs, SVec4F(0.6f, 0.0f, 0.0f, 1.0f), 128);
817 renderLight(280, 330, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 64);
821 foreach (ref li
; attachedLights
[0..attachedLightCount
]) {
822 if (li
.type
== AttachedLightInfo
.Type
.Ambient
) {
823 //conwriteln("ambient: x=", li.x, "; y=", li.y, "; w=", li.w, "; h=", li.h);
826 renderLightAmbient(li
.x
, li
.y
, li
.w
, li
.h
, SVec4F(0.0f, 0.0f, 0.0f, li
.r
));
828 renderLightAmbient(li
.x
, li
.y
, li
.w
, li
.h
, SVec4F(li
.r
, li
.g
, li
.b
, 1.0f));
830 } else if (li
.type
== AttachedLightInfo
.Type
.Point
) {
833 renderLight(li
.x
, li
.y
, SVec4F(0.0f, 0.0f, 0.0f, li
.r
), li
.radius
);
835 renderLight(li
.x
, li
.y
, SVec4F(li
.r
, li
.g
, li
.b
, 1.0f), li
.radius
);
841 if (testLightMoved
) {
842 testLightX
= testLightX
/scale
+mofsx
/scale
;
843 testLightY
= testLightY
/scale
+mofsy
/scale
;
844 testLightMoved
= false;
846 foreach (immutable _
; 0..1) {
847 renderLight(testLightX
, testLightY
, SVec4F(0.3f, 0.3f, 0.0f, 1.0f), 96);
851 glActiveTexture(GL_TEXTURE0
+1);
852 glBindTexture(GL_TEXTURE_2D
, 0);
853 glActiveTexture(GL_TEXTURE0
+2);
854 glBindTexture(GL_TEXTURE_2D
, 0);
855 glActiveTexture(GL_TEXTURE0
+3);
856 glBindTexture(GL_TEXTURE_2D
, 0);
857 glActiveTexture(GL_TEXTURE0
+0);
862 shadScanlines.exec((Shader shad) {
863 shad["scanlines"] = scanlines;
864 glClearColor(BackIntens, BackIntens, BackIntens, 1.0f);
865 glClear(GL_COLOR_BUFFER_BIT);
866 orthoCamera(vlWidth, vlHeight);
867 //orthoCamera(map.width*TileSize*scale, map.height*TileSize*scale);
868 //glMatrixMode(GL_MODELVIEW);
870 //glTranslatef(0.375, 0.375, 0); // to be pixel-perfect
871 // somehow, FBO objects are mirrored; wtf?!
872 drawAtXY(fboLevel.tex.tid, -mapOfsX, -mapOfsY, map.width*TileSize*scale, map.height*TileSize*scale, mirrorY:true);
879 smDrawText(map.width*TileSize/2, map.height*TileSize/2, "Testing...");
884 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
889 //auto img = smfont.ptr[0x39];
892 //conwriteln("img.sx=", img.sx, "; img.sy=", img.sy, "; ", img.width, "x", img.height);
893 drawAtXY(img.tex, 10-img.sx, 10-img.sy);
900 { // http://stackoverflow.com/questions/7207422/setting-up-opengl-multiple-render-targets
901 GLenum[1] buffers = [ GL_BACK_LEFT, /*GL_COLOR_ATTACHMENT0_EXT*/ ];
902 //GLenum[1] buffers = [ GL_NONE, /*GL_COLOR_ATTACHMENT0_EXT*/ ];
903 glDrawBuffers(1, buffers.ptr);
908 orthoCamera(vlWidth
, vlHeight
);
909 auto tex
= (doLighting ? fboLevelLight
.tex
.tid
: fboOrigBack
.tex
.tid
);
910 drawAtXY(tex
, -mofsx
, -mofsy
, map
.width
*TileSize
*scale
, map
.height
*TileSize
*scale
, mirrorY
:true);
912 //drawAtXY(map.texgl.ptr[map.LightMask].tid, -mofsx, -mofsy, map.width*TileSize*scale, map.height*TileSize*scale, mirrorY:true);
913 //drawAtXY(fboLMaskSmall.tex.tid, 0, 0, map.width*TileSize, map.height*TileSize);
917 //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
918 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
919 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
921 fboEditor
.tex
.setFromImage(editorImg
);
922 drawAtXY(fboEditor
.tex
, 0, 0);
930 // ////////////////////////////////////////////////////////////////////////// //
931 // returns time slept
932 int sleepAtMaxMsecs (int msecs
) {
934 import core
.sys
.posix
.signal
: timespec
;
935 import core
.sys
.posix
.time
: nanosleep
;
936 timespec ts
= void, tpassed
= void;
938 ts
.tv_nsec
= msecs
*1000*1000+(500*1000); // milli to nano
939 nanosleep(&ts
, &tpassed
);
940 return (ts
.tv_nsec
-tpassed
.tv_nsec
)/(1000*1000);
947 // ////////////////////////////////////////////////////////////////////////// //
948 mixin(import("editor.d"));
951 // ////////////////////////////////////////////////////////////////////////// //
953 shared int diedie
= 0;
955 enum D2DFrameTime
= 55; // milliseconds
956 enum MinFrameTime
= 1000/60; // ~60 FPS
958 public void renderThread (Tid starterTid
) {
959 enum BoolOptVarMsgMixin(string varname
) =
960 "if (msg.toggle) msg.value = !"~varname
~";\n"~
961 "if ("~varname
~" != msg.value) "~varname
~" = msg.value; else msg.showMessage = false;\n";
963 send(starterTid
, 42);
965 MonoTime curtime
= MonoTime
.currTime
;
967 lastthink
= curtime
; // for interpolator
968 nextthink
= curtime
+dur
!"msecs"(D2DFrameTime
);
969 MonoTime nextvframe
= curtime
;
971 enum MaxFPSFrames
= 16;
972 float frtimes
= 0.0f;
975 int hushFrames
= 6; // ignore first `hushFrames` frames overtime
976 MonoTime prevFrameStartTime
= curtime
;
978 bool vframeWasLost
= false;
980 void resetFrameTimers () {
981 MonoTime curtime
= MonoTime
.currTime
;
982 lastthink
= curtime
; // for interpolator
983 nextthink
= curtime
+dur
!"msecs"(D2DFrameTime
);
984 nextvframe
= curtime
;
987 void loadNewLevel (string name
) {
990 conwriteln("ERROR: can't load new levels yet");
994 if (name
.length
== 0) {
995 conwriteln("ERROR: can't load empty level!");
997 conwriteln("loading map '", name
, "'");
1002 void receiveMessages () {
1004 import core
.time
: Duration
;
1005 //conwriteln("rendering thread: waiting for messages...");
1006 auto got
= receiveTimeout(
1007 Duration
.zero
, // don't wait
1009 addMessage(msg
.text
[0..msg
.textlen
], msg
.pauseMsecs
, msg
.noreplace
);
1011 (TMsgLoadLevel msg
) {
1012 nextmapname
= null; // clear "exit" flag
1013 loadNewLevel(msg
.mapfile
[0..msg
.textlen
].idup
);
1015 (TMsgSkipLevel msg
) {
1016 string mn
= genNextMapName(0);
1018 nextmapname
= null; // clear "exit" flag
1022 (TMsgIntOption msg
) {
1023 final switch (msg
.type
) with (TMsgIntOption
.Type
) {
1025 if (msg
.value
>= 1 && msg
.value
<= 2) scale
= msg
.value
;
1029 (TMsgBoolOption msg
) {
1030 final switch (msg
.type
) with (TMsgBoolOption
.Type
) {
1031 case Interpolation
: mixin(BoolOptVarMsgMixin
!"frameInterpolation"); break;
1032 case Lighting
: mixin(BoolOptVarMsgMixin
!"doLighting"); break;
1033 case Pause
: mixin(BoolOptVarMsgMixin
!"gamePaused"); break;
1035 if (msg
.toggle
) msg
.value
= !inEditMode
;
1036 if (inEditMode
!= msg
.value
) {
1037 inEditMode
= msg
.value
;
1038 if (msg
.value
) sdwindow
.hideCursor(); else sdwindow
.showCursor();
1040 msg
.showMessage
= false;
1043 mixin(BoolOptVarMsgMixin
!"inEditMode"); break;
1044 case CheatNoDoors
: mixin(BoolOptVarMsgMixin
!"cheatNoDoors"); break;
1045 case CheatNoWallClip
: mixin(BoolOptVarMsgMixin
!"cheatNoWallClip"); break;
1047 if (msg
.showMessage
) {
1050 void putStr(T
) (T s
) if (is(T
: const(char)[])) {
1051 foreach (char ch
; s
) {
1052 if (msgpos
>= mbuf
.length
) break;
1053 mbuf
.ptr
[msgpos
++] = ch
;
1056 putStr("Option \"");
1057 { import std
.conv
: to
; putStr(to
!string(msg
.type
)); }
1059 if (msg
.value
) putStr("N"); else putStr("FF");
1060 addMessage(mbuf
[0..msgpos
]);
1063 (TMsgTestLightMove msg
) {
1066 testLightMoved
= true;
1068 (TMsgMouseEvent msg
) { editorMouseEvent(msg
); },
1069 (TMsgKeyEvent msg
) { editorKeyEvent(msg
); },
1071 conwriteln("WARNING: unknown thread message received and ignored");
1076 //conwriteln("rendering thread: no more messages");
1080 if (nextmapname
.length
) {
1081 string mn
= nextmapname
;
1082 nextmapname
= null; // clear "exit" flag
1087 // "D2D frames"; curtime should be set; return `true` if frame was processed; will fix `curtime`
1088 bool doThinkFrame () {
1089 if (curtime
>= nextthink
) {
1090 lastthink
= curtime
;
1091 while (nextthink
<= curtime
) nextthink
+= dur
!"msecs"(D2DFrameTime
);
1094 // save snapshot and other data for interpolator
1095 Actor
.saveSnapshot(prevFrameActorsData
[], prevFrameActorOfs
.ptr
);
1097 if (!gamePaused
&& !inEditMode
) {
1104 auto tm
= MonoTime
.currTime
;
1105 int thinkTime
= cast(int)((tm
-curtime
).total
!"msecs");
1106 if (thinkTime
> 9) { import core
.stdc
.stdio
; printf("spent on thinking: %d msecs\n", thinkTime
); }
1114 // "video frames"; curtime should be set; return `true` if frame was processed; will fix `curtime`
1116 version(dont_use_vsync
) {
1118 enum doCheckTime
= true;
1121 __gshared
bool prevLost
= false;
1122 bool doCheckTime
= vframeWasLost
;
1123 if (vframeWasLost
) {
1125 { import core
.stdc
.stdio
; printf("frame was lost!\n"); }
1133 if (curtime
< nextvframe
) return false;
1134 version(dont_use_vsync
) {
1135 if (curtime
> nextvframe
) {
1136 auto overtime
= cast(int)((curtime
-nextvframe
).total
!"msecs");
1137 if (overtime
> 2500) {
1141 { import core
.stdc
.stdio
; printf(" spent whole %d msecs\n", overtime
); }
1147 while (nextvframe
<= curtime
) nextvframe
+= dur
!"msecs"(MinFrameTime
);
1151 scope(exit
) sdwindow
.mtUnlock();
1152 ctset
= sdwindow
.setAsCurrentOpenGlContextNT
;
1154 // if we can't set context, pretend that videoframe was processed; this should solve problem with vsync and invisible window
1157 iLiquidTime
= cast(float)((curtime
-MonoTime
.zero
).total
!"msecs"%10000000)/18.0f*0.04f;
1158 receiveMessages(); // here, 'cause we need active OpenGL context for some messages
1160 renderScene(curtime
);
1162 //renderLoading(curtime);
1165 scope(exit
) sdwindow
.mtUnlock();
1166 sdwindow
.swapOpenGlBuffers();
1168 sdwindow
.releaseCurrentOpenGlContext();
1169 vframeWasLost
= false;
1171 vframeWasLost
= true;
1172 { import core
.stdc
.stdio
; printf("xframe was lost!\n"); }
1174 curtime
= MonoTime
.currTime
;
1179 if (sdwindow
.closed
) break;
1180 if (atomicLoad(diedie
) > 0) break;
1182 curtime
= MonoTime
.currTime
;
1183 auto fstime
= curtime
;
1185 doThinkFrame(); // this will fix curtime if necessary
1187 if (!vframeWasLost
) {
1189 auto frameTime
= cast(float)(curtime
-prevFrameStartTime
).total
!"msecs"/1000.0f;
1190 prevFrameStartTime
= curtime
;
1191 frtimes
+= frameTime
;
1192 if (++framenum
>= MaxFPSFrames || frtimes
>= 3.0f) {
1193 import std
.string
: format
;
1194 int newFPS
= cast(int)(cast(float)MaxFPSFrames
/frtimes
+0.5);
1195 if (newFPS
!= prevFPS
) {
1196 sdwindow
.title
= "%s / FPS:%s".format("D2D", newFPS
);
1205 curtime
= MonoTime
.currTime
;
1207 // now sleep until next "video" or "think" frame
1208 if (nextthink
> curtime
&& nextvframe
> curtime
) {
1210 immutable nextVideoFrameSleep
= cast(int)((nextvframe
-curtime
).total
!"msecs");
1211 immutable nextThinkFrameSleep
= cast(int)((nextthink
-curtime
).total
!"msecs");
1212 immutable sleepTime
= (nextVideoFrameSleep
< nextThinkFrameSleep ? nextVideoFrameSleep
: nextThinkFrameSleep
);
1213 sleepAtMaxMsecs(sleepTime
);
1214 //curtime = MonoTime.currTime;
1217 } catch (Throwable e
) {
1218 // here, we are dead and fucked (the exact order doesn't matter)
1219 import core
.stdc
.stdlib
: abort
;
1220 import core
.stdc
.stdio
: fprintf
, stderr
;
1221 import core
.memory
: GC
;
1222 GC
.disable(); // yeah
1223 thread_suspendAll(); // stop right here, you criminal scum!
1224 auto s
= e
.toString();
1225 fprintf(stderr
, "\n=== FATAL ===\n%.*s\n", cast(uint)s
.length
, s
.ptr
);
1226 abort(); // die, you bitch!
1228 import core.stdc.stdio;
1229 fprintf(stderr, "FUUUUUUUUUUUUUUUUUUUUUUUUUU\n");
1230 import std.stdio : stderr;
1233 if (sdwindow.closed) break;
1234 if (atomicLoad(diedie) > 0) break;
1235 sleepAtMaxMsecs(100);
1239 atomicStore(diedie
, 2);
1243 // ////////////////////////////////////////////////////////////////////////// //
1244 __gshared Tid renderTid
;
1245 shared bool renderThreadStarted
= false;
1248 public void startRenderThread () {
1249 if (!cas(&renderThreadStarted
, false, true)) {
1250 assert(0, "render thread already started!");
1252 renderTid
= spawn(&renderThread
, thisTid
);
1253 setMaxMailboxSize(renderTid
, 1024, OnCrowding
.throwException
); //FIXME
1254 // wait for "i'm ready" signal
1257 if (ok
!= 42) assert(0, "wtf?!");
1260 conwriteln("rendering thread started");
1264 // ////////////////////////////////////////////////////////////////////////// //
1265 public void closeWindow () {
1266 if (atomicLoad(diedie
) != 2) {
1267 atomicStore(diedie
, 1);
1268 while (atomicLoad(diedie
) != 2) {}
1270 if (!sdwindow
.closed
) {
1277 // ////////////////////////////////////////////////////////////////////////// //
1281 // ////////////////////////////////////////////////////////////////////////// //
1282 struct TMsgMouseEvent
{
1283 MouseEventType type
;
1286 MouseButton button
; /// See $(LREF MouseButton)
1287 int modifierState
; /// See $(LREF ModifierState)
1290 public void postMouseEvent() (in auto ref MouseEvent evt
) {
1291 if (!atomicLoad(renderThreadStarted
)) return;
1293 msg
.type
= evt
.type
;
1298 msg
.button
= evt
.button
;
1299 msg
.modifierState
= evt
.modifierState
;
1300 send(renderTid
, msg
);
1304 // ////////////////////////////////////////////////////////////////////////// //
1305 struct TMsgKeyEvent
{
1313 public void postKeyEvent() (in auto ref KeyEvent evt
) {
1314 if (!atomicLoad(renderThreadStarted
)) return;
1317 msg
.pressed
= evt
.pressed
;
1318 msg
.character
= evt
.character
;
1319 msg
.modifierState
= evt
.modifierState
;
1320 send(renderTid
, msg
);
1324 // ////////////////////////////////////////////////////////////////////////// //
1325 struct TMsgTestLightMove
{
1329 public void postTestLightMove (int x
, int y
) {
1330 if (!atomicLoad(renderThreadStarted
)) return;
1331 auto msg
= TMsgTestLightMove(x
, y
);
1332 send(renderTid
, msg
);
1336 // ////////////////////////////////////////////////////////////////////////// //
1337 struct TMsgMessage
{
1344 public void postAddMessage (const(char)[] msgtext
, int pauseMsecs
=3000, bool noreplace
=false) {
1345 if (!atomicLoad(renderThreadStarted
)) return;
1346 if (msgtext
.length
> TMsgMessage
.text
.length
) msgtext
= msgtext
[0..TMsgMessage
.text
.length
];
1348 msg
.textlen
= cast(uint)msgtext
.length
;
1349 if (msg
.textlen
) msg
.text
[0..msg
.textlen
] = msgtext
[0..msg
.textlen
];
1350 msg
.pauseMsecs
= pauseMsecs
;
1351 msg
.noreplace
= noreplace
;
1352 send(renderTid
, msg
);
1356 // ////////////////////////////////////////////////////////////////////////// //
1357 struct TMsgLoadLevel
{
1362 public void postLoadLevel (const(char)[] mapfile
) {
1363 if (!atomicLoad(renderThreadStarted
)) return;
1364 if (mapfile
.length
> TMsgLoadLevel
.mapfile
.length
) mapfile
= mapfile
[0..TMsgLoadLevel
.mapfile
.length
];
1366 msg
.textlen
= cast(uint)mapfile
.length
;
1367 if (msg
.textlen
) msg
.mapfile
[0..msg
.textlen
] = mapfile
[0..msg
.textlen
];
1368 send(renderTid
, msg
);
1372 // ////////////////////////////////////////////////////////////////////////// //
1373 struct TMsgSkipLevel
{
1376 public void postSkipLevel () {
1377 if (!atomicLoad(renderThreadStarted
)) return;
1379 send(renderTid
, msg
);
1383 // ////////////////////////////////////////////////////////////////////////// //
1384 struct TMsgIntOption
{
1394 struct TMsgBoolOption
{
1410 bool strCaseEqu (const(char)[] s0
, const(char)[] s1
) {
1411 if (s0
.length
!= s1
.length
) return false;
1412 foreach (immutable idx
, char ch
; s0
) {
1413 if (ch
>= 'A' && ch
<= 'Z') ch
+= 32;
1414 char c1
= s1
.ptr
[idx
];
1415 if (c1
>= 'A' && c1
<= 'Z') c1
+= 32;
1416 if (ch
!= c1
) return false;
1422 public bool postToggleOption (const(char)[] name
, bool showMessage
=false) { /*pragma(inline, true);*/ return postSetOption(name
, true, showMessage
:showMessage
, toggle
:true); }
1424 public bool postSetOption(T
) (const(char)[] name
, T value
, bool showMessage
=false, bool toggle
=false) if ((is(T
== int) ||
is(T
== bool))) {
1425 if (!atomicLoad(renderThreadStarted
)) return false;
1426 if (name
.length
== 0 || name
.length
> 127) return false;
1427 static if (is(T
== int)) {
1429 foreach (string oname
; __traits(allMembers
, TMsgIntOption
.Type
)) {
1430 if (strCaseEqu(oname
, name
)) {
1431 msg
.type
= __traits(getMember
, TMsgIntOption
.Type
, oname
);
1433 msg
.showMessage
= showMessage
;
1434 send(renderTid
, msg
);
1438 } else static if (is(T
== bool)) {
1440 foreach (string oname
; __traits(allMembers
, TMsgBoolOption
.Type
)) {
1441 if (strCaseEqu(oname
, name
)) {
1442 msg
.type
= __traits(getMember
, TMsgBoolOption
.Type
, oname
);
1444 msg
.toggle
= toggle
;
1445 msg
.showMessage
= showMessage
;
1446 send(renderTid
, msg
);
1451 static assert(0, "invalid option type '"~T
.stringof
~"'");