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
);
197 loadAllMonsterGraphics();
201 // ////////////////////////////////////////////////////////////////////////// //
202 // should be called when OpenGL is initialized
203 void loadMap (string mapname
) {
204 mapscripts
.runUnloading(); // "map unloading" script
207 if (map
!is null) map
.clear();
208 map
= new LevelMap(mapname
);
209 curmapname
= mapname
;
211 ugInit(map
.width
*TileSize
, map
.height
*TileSize
);
216 if (fboLevel
!is null) fboLevel
.clear();
217 if (fboLevelLight
!is null) fboLevelLight
.clear();
218 if (fboOrigBack
!is null) fboOrigBack
.clear();
219 if (fboLMaskSmall
!is null) fboLMaskSmall
.clear();
221 fboLevel
= new FBO(map
.width
*TileSize
, map
.height
*TileSize
, Texture
.Option
.Nearest
); // final level render will be here
222 fboLevelLight
= new FBO(map
.width
*TileSize
, map
.height
*TileSize
, Texture
.Option
.Nearest
); // level lights will be rendered here
223 fboOrigBack
= new FBO(map
.width
*TileSize
, map
.height
*TileSize
, Texture
.Option
.Nearest
/*, Texture.Option.Depth*/); // background+foreground
224 fboLMaskSmall
= new FBO(map
.width
, map
.height
, Texture
.Option
.Nearest
); // small lightmask
226 shadToPolar
.exec((Shader shad
) { shad
["mapPixSize"] = SVec2F(map
.width
*TileSize
-1, map
.height
*TileSize
-1); });
227 shadBlur
.exec((Shader shad
) { shad
["mapPixSize"] = SVec2F(map
.width
*TileSize
, map
.height
*TileSize
); });
228 shadBlurOcc
.exec((Shader shad
) { shad
["mapPixSize"] = SVec2F(map
.width
*TileSize
, map
.height
*TileSize
); });
229 shadAmbient
.exec((Shader shad
) { shad
["mapPixSize"] = SVec2F(map
.width
*TileSize
, map
.height
*TileSize
); });
231 glActiveTexture(GL_TEXTURE0
+0);
232 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
233 orthoCamera(vlWidth
, vlHeight
);
235 Actor
.resetStorage();
238 mapscripts
.runInit();
241 mapscripts
.runLoaded();
243 // save first snapshot
244 if (prevFrameActorsData
.length
== 0) prevFrameActorsData
= new ubyte[](Actor
.actorSize
*65536); // ~15-20 megabytes
245 prevFrameActorOfs
[] = uint.max
; // just for fun
246 Actor
.saveSnapshot(prevFrameActorsData
[], prevFrameActorOfs
.ptr
);
250 { import core
.memory
: GC
; GC
.collect(); }
254 // ////////////////////////////////////////////////////////////////////////// //
256 __gshared
uint mapTilesChanged
= 0;
259 //WARNING! this can be called only from DACS, so we don't have to sync it!
260 public void mapDirty (uint layermask
) { mapTilesChanged |
= layermask
; }
263 void rebuildMapMegaTextures () {
265 //mapTilesChanged = false;
266 //map.clearMegaTextures();
267 map
.oglBuildMega(mapTilesChanged
);
269 dotsAwake(); // let dormant dots fall
273 // ////////////////////////////////////////////////////////////////////////// //
276 enum Phase
{ FadeIn
, Stay
, FadeOut
}
285 private import core
.sync
.mutex
: Mutex
;
287 __gshared Message
[128] messages
;
288 __gshared
uint messagesUsed
= 0;
290 //__gshared Mutex messageLock;
291 //shared static this () { messageLock = new Mutex(); }
294 void addMessage (const(char)[] msgtext
, int pauseMsecs
=3000, bool noreplace
=false) {
295 //messageLock.lock();
296 //scope(exit) messageLock.unlock();
297 if (msgtext
.length
== 0) return;
299 if (pauseMsecs
<= 50) return;
300 if (messagesUsed
== messages
.length
) {
301 // remove top message
302 foreach (immutable cidx
; 1..messagesUsed
) messages
.ptr
[cidx
-1] = messages
.ptr
[cidx
];
303 messages
.ptr
[0].alpha
= 255;
307 if (!noreplace
&& messagesUsed
== 1) {
308 switch (messages
.ptr
[0].phase
) {
309 case Message
.Phase
.FadeIn
:
310 messages
.ptr
[0].phase
= Message
.Phase
.FadeOut
;
312 case Message
.Phase
.Stay
:
313 messages
.ptr
[0].phase
= Message
.Phase
.FadeOut
;
314 messages
.ptr
[0].alpha
= 255;
319 auto msg
= messages
.ptr
+messagesUsed
;
321 msg
.phase
= Message
.Phase
.FadeIn
;
323 msg
.pauseMsecs
= pauseMsecs
;
325 if (msgtext
.length
> msg
.text
.length
) {
326 msg
.text
= msgtext
[0..msg
.text
.length
];
327 msg
.textlen
= msg
.text
.length
;
329 msg
.text
[0..msgtext
.length
] = msgtext
[];
330 msg
.textlen
= msgtext
.length
;
335 void doMessages (MonoTime curtime
) {
336 //messageLock.lock();
337 //scope(exit) messageLock.unlock();
339 if (messagesUsed
== 0) return;
341 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
347 final switch (msg
.phase
) {
348 case Message
.Phase
.FadeIn
:
349 if ((msg
.alpha
+= 10) >= 255) {
350 msg
.phase
= Message
.Phase
.Stay
;
351 msg
.removeTime
= curtime
+dur
!"msecs"(msg
.pauseMsecs
);
352 goto case; // to stay
354 glColor4f(1.0f, 1.0f, 1.0f, msg
.alpha
/255.0f);
356 case Message
.Phase
.Stay
:
357 if (msg
.removeTime
<= curtime
) {
359 msg
.phase
= Message
.Phase
.FadeOut
;
360 goto case; // to fade
362 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
364 case Message
.Phase
.FadeOut
:
365 if ((msg
.alpha
-= 10) <= 0) {
366 if (--messagesUsed
== 0) return;
367 // remove this message
368 foreach (immutable cidx
; 1..messagesUsed
+1) messages
.ptr
[cidx
-1] = messages
.ptr
[cidx
];
371 glColor4f(1.0f, 1.0f, 1.0f, msg
.alpha
/255.0f);
375 smDrawText(10, 10, msg
.text
[0..msg
.textlen
]);
379 // ////////////////////////////////////////////////////////////////////////// //
380 //mixin(Actor.FieldPropMixin!("0drawlistpos", uint));
382 mixin(Actor
.FieldGetMixin
!("classtype", StrId
)); // fget_classtype
383 mixin(Actor
.FieldGetMixin
!("classname", StrId
)); // fget_classname
384 mixin(Actor
.FieldGetMixin
!("x", int));
385 mixin(Actor
.FieldGetMixin
!("y", int));
386 mixin(Actor
.FieldGetMixin
!("s", int));
387 mixin(Actor
.FieldGetMixin
!("radius", int));
388 mixin(Actor
.FieldGetMixin
!("height", int));
389 mixin(Actor
.FieldGetMixin
!("flags", uint));
390 mixin(Actor
.FieldGetMixin
!("zAnimstate", StrId
));
391 mixin(Actor
.FieldGetMixin
!("zAnimidx", int));
392 mixin(Actor
.FieldGetMixin
!("dir", uint));
393 mixin(Actor
.FieldGetMixin
!("attLightXOfs", int));
394 mixin(Actor
.FieldGetMixin
!("attLightYOfs", int));
395 mixin(Actor
.FieldGetMixin
!("attLightRGBX", uint));
397 //mixin(Actor.FieldGetPtrMixin!("classtype", StrId)); // fget_classtype
398 //mixin(Actor.FieldGetPtrMixin!("classname", StrId)); // fget_classname
399 mixin(Actor
.FieldGetPtrMixin
!("x", int));
400 mixin(Actor
.FieldGetPtrMixin
!("y", int));
401 //mixin(Actor.FieldGetPtrMixin!("flags", uint));
402 //mixin(Actor.FieldGetPtrMixin!("zAnimstate", StrId));
403 //mixin(Actor.FieldGetPtrMixin!("zAnimidx", int));
404 //mixin(Actor.FieldGetPtrMixin!("dir", uint));
405 //mixin(Actor.FieldGetPtrMixin!("attLightXOfs", int));
406 //mixin(Actor.FieldGetPtrMixin!("attLightYOfs", int));
407 //mixin(Actor.FieldGetPtrMixin!("attLightRGBX", uint));
410 // ////////////////////////////////////////////////////////////////////////// //
411 __gshared
int vportX0
, vportY0
, vportX1
, vportY1
;
414 // ////////////////////////////////////////////////////////////////////////// //
415 void renderLightAmbient() (int lightX
, int lightY
, int lightW
, int lightH
, in auto ref SVec4F lcol
) {
416 //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));
417 if (lightW
< 1 || lightH
< 1) return;
418 int lightX1
= lightX
+lightW
-1;
419 int lightY1
= lightY
+lightH
-1;
420 // clip light to viewport
421 if (lightX
< vportX0
) lightX
= vportX0
;
422 if (lightY
< vportY0
) lightY
= vportY0
;
423 if (lightX1
> vportX1
) lightX1
= vportX1
;
424 if (lightY1
> vportY1
) lightY1
= vportY1
;
425 // is this light visible?
426 //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));
427 if (lightX1
< lightX || lightY1
< lightY || lightX
> vportX1 || lightY
> vportY1 || lightX1
< vportX0 || lightY1
< vportY0
) return;
428 //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));
432 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
433 //glDisable(GL_BLEND);
434 orthoCamera(map
.width
*TileSize
, map
.height
*TileSize
);
435 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
436 shadAmbient
.exec((Shader shad
) {
437 shad
["lightColor"] = SVec4F(lcol
.x
, lcol
.y
, lcol
.z
, lcol
.w
);
438 //shad["lightPos"] = SVec2F(lightX, lightY);
439 glRectf(lightX
, lightY
, lightX1
, lightY1
);
445 // ////////////////////////////////////////////////////////////////////////// //
446 void renderLight() (int lightX
, int lightY
, in auto ref SVec4F lcol
, int lightRadius
) {
447 if (lightRadius
< 2) return;
448 if (lightRadius
> MaxLightRadius
) lightRadius
= MaxLightRadius
;
449 int lightSize
= lightRadius
*2;
450 // is this light visible?
451 if (lightX
<= -lightRadius || lightY
<= -lightRadius || lightX
-lightRadius
>= map
.width
*TileSize || lightY
-lightRadius
>= map
.height
*TileSize
) return;
453 // out of viewport -- do nothing
454 if (lightX
+lightRadius
< vportX0 || lightY
+lightRadius
< vportY0
) return;
455 if (lightX
-lightRadius
> vportX1 || lightY
-lightRadius
> vportY1
) return;
457 if (lightX
>= 0 && lightY
>= 0 && lightX
< map
.width
*TileSize
&& lightY
< map
.height
*TileSize
&&
458 map
.teximgs
[map
.LightMask
].imageData
.colors
.ptr
[lightY
*(map
.width
*TileSize
)+lightX
].a
> 190) return;
460 // common color for all the following
462 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
464 // build 1d distance map to fboShadowMapId
465 fboDistMap
.ptr
[lightRadius
].exec({
466 // no need to clear it, shader will take care of that
467 shadToPolar
.exec((Shader shad
) {
468 shad
["lightTexSize"] = SVec2F(lightSize
, lightSize
);
469 shad
["lightPos"] = SVec2F(lightX
, lightY
);
470 orthoCamera(lightSize
, 1);
471 // it doesn't matter what we will draw here, so just draw filled rect
472 glRectf(0, 0, lightSize
, 1);
476 // build light texture for blending
478 // no need to clear it, shader will take care of that
480 //glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
481 //glClear(GL_COLOR_BUFFER_BIT);
482 shadBlur
.exec((Shader shad
) {
483 shad
["lightTexSize"] = SVec2F(lightSize
, MaxLightRadius
*2); // x: size of distmap; y: size of this texture
484 shad
["lightColor"] = SVec4F(lcol
.x
, lcol
.y
, lcol
.z
, lcol
.w
);
485 shad
["lightPos"] = SVec2F(lightX
-lightRadius
, lightY
-lightRadius
);
486 orthoCamera(fboOccluders
.tex
.width
, fboOccluders
.tex
.height
);
487 //drawAtXY(fboDistMap[lightRadius].tex.tid, 0, 0, lightSize, lightSize);
488 bindTexture(fboDistMap
.ptr
[lightRadius
].tex
.tid
);
490 glTexCoord2f(0.0f, 0.0f); glVertex2i(0, 0); // top-left
491 glTexCoord2f(1.0f, 0.0f); glVertex2i(lightSize
, 0); // top-right
492 glTexCoord2f(1.0f, 1.0f); glVertex2i(lightSize
, lightSize
); // bottom-right
493 glTexCoord2f(0.0f, 1.0f); glVertex2i(0, lightSize
); // bottom-left
498 // blend light texture
501 //glDisable(GL_BLEND);
502 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
503 orthoCamera(fboLevelLight
.tex
.width
, fboLevelLight
.tex
.height
);
504 //drawAtXY(fboOccluders.tex, lightX-lightRadius, lightY-lightRadius, mirrorY:true);
505 float occe
= 1.0f*lightSize
/(MaxLightRadius
*2);
506 float occs
= 1.0f-occe
;
507 int x0
= lightX
-lightRadius
+0;
508 int y1
= lightY
-lightRadius
+0;
509 int x1
= lightX
+lightRadius
-1+1;
510 int y0
= lightY
+lightRadius
-1+1;
511 bindTexture(fboOccluders
.tex
.tid
);
514 glTexCoord2f(0.0f, 0.0f); glVertex2i(x0, y0); // top-left
515 glTexCoord2f(occe, 0.0f); glVertex2i(x1, y0); // top-right
516 glTexCoord2f(occe, occe); glVertex2i(x1, y1); // bottom-right
517 glTexCoord2f(0.0f, occe); glVertex2i(x0, y1); // bottom-left
519 glTexCoord2f(0.0f, occs
); glVertex2i(x0
, y0
); // top-left
520 glTexCoord2f(occe
, occs
); glVertex2i(x1
, y0
); // top-right
521 glTexCoord2f(occe
, 1.0f); glVertex2i(x1
, y1
); // bottom-right
522 glTexCoord2f(0.0f, 1.0f); glVertex2i(x0
, y1
); // bottom-left
526 glRectf(x0, y0, x1, y1);
528 // and blend it again, with the shader that will touch only occluders
529 shadBlurOcc
.exec((Shader shad
) {
530 //shad["lightTexSize"] = SVec2F(lightSize, lightSize);
531 shad
["lightTexSize"] = SVec2F(lightSize
, fboOccluders
.tex
.height
);
532 shad
["lightColor"] = SVec4F(lcol
.x
, lcol
.y
, lcol
.z
, lcol
.w
);
533 shad
["lightPos"] = SVec2F(lightX
, lightY
);
534 //shad["lightPos"] = SVec2F(lightX-lightRadius, lightY-lightRadius);
535 bindTexture(fboOccluders
.tex
.tid
);
537 glTexCoord2f(0.0f, occs
); glVertex2i(x0
, y0
); // top-left
538 glTexCoord2f(occe
, occs
); glVertex2i(x1
, y0
); // top-right
539 glTexCoord2f(occe
, 1.0f); glVertex2i(x1
, y1
); // bottom-right
540 glTexCoord2f(0.0f, 1.0f); glVertex2i(x0
, y1
); // bottom-left
542 //drawAtXY(fboOccluders.tex.tid, lightX-lightRadius, lightY-lightRadius, lightSize, lightSize, mirrorY:true);
548 // ////////////////////////////////////////////////////////////////////////// //
549 __gshared
int testLightX
= vlWidth
/2, testLightY
= vlHeight
/2;
550 __gshared
bool testLightMoved
= false;
551 //__gshared int mapOfsX, mapOfsY;
552 //__gshared bool movement = false;
553 __gshared
float iLiquidTime
= 0.0;
554 //__gshared bool altMove = false;
557 void renderScene (MonoTime curtime
) {
558 //enum BackIntens = 0.05f;
559 enum BackIntens
= 0.0f;
562 float atob
= (curtime
> lastthink ?
cast(float)((curtime
-lastthink
).total
!"msecs")/cast(float)((nextthink
-lastthink
).total
!"msecs") : 1.0f);
563 if (gamePaused || inEditMode
) atob
= 1.0f;
564 //{ import core.stdc.stdio; printf("atob=%f\n", cast(double)atob); }
567 int framelen = cast(int)((nextthink-lastthink).total!"msecs");
568 int curfp = cast(int)((curtime-lastthink).total!"msecs");
569 { import core.stdc.stdio; printf("framelen=%d; curfp=%d\n", framelen, curfp); }
573 int mofsx
, mofsy
; // camera offset, will be set in background layer builder
575 if (mapTilesChanged
!= 0) rebuildMapMegaTextures();
577 // build background layer
579 //glDisable(GL_BLEND);
580 //glClearDepth(1.0f);
581 //glDepthFunc(GL_LESS);
582 //glDepthFunc(GL_NEVER);
583 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
584 glClear(GL_COLOR_BUFFER_BIT
/*|GL_DEPTH_BUFFER_BIT*/);
585 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
586 orthoCamera(map
.width
*TileSize
, map
.height
*TileSize
);
589 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
592 drawAtXY(map.skytexgl.tid, mapOfsX/2-map.MapSize*TileSize, mapOfsY/2-map.MapSize*TileSize, map.MapSize*TileSize, map.MapSize*TileSize);
593 drawAtXY(map.skytexgl.tid, mapOfsX/2-map.MapSize*TileSize, mapOfsY/2, map.MapSize*TileSize, map.MapSize*TileSize);
594 drawAtXY(map.skytexgl.tid, mapOfsX/2, mapOfsY/2-map.MapSize*TileSize, map.MapSize*TileSize, map.MapSize*TileSize);
595 drawAtXY(map.skytexgl.tid, mapOfsX/2, mapOfsY/2, map.MapSize*TileSize, map.MapSize*TileSize);
597 drawAtXY(map
.skytexgl
.tid
, 0, 0, map
.MapSize
*TileSize
, map
.MapSize
*TileSize
);
599 drawAtXY(map
.texgl
.ptr
[map
.Back
], 0, 0);
600 // draw distorted liquid areas
601 shadLiquidDistort
.exec((Shader shad
) {
602 shad
["iDistortTime"] = iLiquidTime
;
603 drawAtXY(map
.texgl
.ptr
[map
.AllLiquids
], 0, 0);
605 // monsters, items; we'll do linear interpolation here
606 glColor3f(1.0f, 1.0f, 1.0f);
607 //glEnable(GL_DEPTH_TEST);
608 attachedLightCount
= 0;
610 // who cares about memory?!
611 // draw order: players, items, monsters, other
612 static struct DrawInfo
{
616 @disable this (this); // no copies
618 enum { Pixels
, Players
, Items
, Monsters
, Other
}
619 __gshared DrawInfo
[65536][4] drawlists
;
620 __gshared
uint[4] dlpos
;
625 Actor
.forEach((ActorId me
) {
626 //me.fprop_0drawlistpos = 0;
627 if (auto adef
= findActorDef(me
)) {
629 switch (adef
.classtype
.get
) {
630 case "monster": dlnum
= (adef
.classname
.get
!= "Player" ? Monsters
: Players
); break;
631 case "item": dlnum
= Items
; break;
632 default: dlnum
= Other
; break;
634 if (me
.fget_flags
&AF_PIXEL
) dlnum
= Pixels
;
635 int actorX
, actorY
; // current actor position
637 auto ofs
= prevFrameActorOfs
.ptr
[me
.id
&0xffff];
638 if (frameInterpolation
&& ofs
< uint.max
-1 && Actor
.isSameSavedActor(me
.id
, prevFrameActorsData
.ptr
, ofs
)) {
639 import core
.stdc
.math
: roundf
;
640 auto xptr
= prevFrameActorsData
.ptr
+ofs
;
641 int ox
= xptr
.fgetp_x
;
643 int oy
= xptr
.fgetp_y
;
645 actorX
= cast(int)(ox
+roundf((nx
-ox
)*atob
));
646 actorY
= cast(int)(oy
+roundf((ny
-oy
)*atob
));
647 //conwriteln("actor ", me.id, "; o=(", ox, ",", oy, "); n=(", nx, ",", ny, "); p=(", x, ",", y, ")");
653 if (me
.id
== cameraChick
.id
) {
654 camchickdi
.adef
= adef
;
656 camchickdi
.actorX
= actorX
;
657 camchickdi
.actorY
= actorY
;
660 if ((me
.fget_flags
&AF_NODRAW
) == 0) {
661 //auto dl = &drawlists[dlnum][dlpos.ptr[dlnum]];
662 //me.fprop_0drawlistpos = (dlpos.ptr[dlnum]&0xffff)|((dlnum&0xff)<<16);
663 auto dl = drawlists
.ptr
[dlnum
].ptr
+dlpos
.ptr
[dlnum
];
670 // process attached lights
671 if ((me
.fget_flags
&AF_NOLIGHT
) == 0) {
672 uint alr
= me
.fget_attLightRGBX
;
673 bool isambient
= (me
.fget_classtype
.get
== "light" && me
.fget_classname
.get
== "Ambient");
674 if ((alr
&0xff) >= 4 ||
(isambient
&& me
.fget_radius
>= 1 && me
.fget_height
>= 1)) {
675 //if (isambient) conwriteln("isambient: ", isambient, "; x=", me.fget_x, "; y=", me.fget_y, "; w=", me.fget_radius, "; h=", me.fget_height);
677 auto li
= attachedLights
.ptr
+attachedLightCount
;
678 ++attachedLightCount
;
679 li
.type
= (!isambient ? AttachedLightInfo
.Type
.Point
: AttachedLightInfo
.Type
.Ambient
);
680 li
.x
= actorX
+me
.fget_attLightXOfs
;
681 li
.y
= actorY
+me
.fget_attLightYOfs
;
682 li
.r
= ((alr
>>24)&0xff)/255.0f; // red or intensity
683 if ((alr
&0x00_ff_ff
_00U) == 0x00_00_01_00U) {
686 li
.g
= ((alr
>>16)&0xff)/255.0f;
687 li
.b
= ((alr
>>8)&0xff)/255.0f;
688 li
.uncolored
= false;
690 li
.radius
= (alr
&0xff);
691 if (li
.radius
> MaxLightRadius
) li
.radius
= MaxLightRadius
;
693 li
.w
= me
.fget_radius
;
694 li
.h
= me
.fget_height
;
699 conwriteln("not found actor ", me
.id
, " (", me
.classtype
!string
, ":", me
.classname
!string
, ")");
704 foreach_reverse (uint dlnum
; 0..drawlists
.length
) {
705 if (dlnum
== Pixels
) continue;
706 auto dl = drawlists
.ptr
[dlnum
].ptr
;
707 if (dlnum
== Players
) dl += dlpos
.ptr
[dlnum
]-1;
708 foreach (uint idx
; 0..dlpos
.ptr
[dlnum
]) {
710 if (auto isp
= dl.adef
.animSpr(me
.fget_zAnimstate
, me
.fget_dir
, me
.fget_zAnimidx
)) {
711 //drawAtXY(isp.tex, dl.actorX-isp.vga.sx, dl.actorY-isp.vga.sy);
712 isp
.drawAtXY(dl.actorX
, dl.actorY
);
714 if (dlnum
!= Players
) ++dl; else --dl;
720 bool pointsStarted
= false;
721 Color lastColor
= Color(0, 0, 0, 0);
722 auto dl = drawlists
.ptr
[Pixels
].ptr
;
723 foreach (uint idx
; 0..dlpos
.ptr
[Pixels
]) {
726 if (s
< 0 || s
> 255) continue; //FIXME
727 Color clr
= d2dpal
.ptr
[s
&0xff];
728 if (clr
.a
== 0) continue;
729 if (clr
!= lastColor
) {
730 if (pointsStarted
) glEnd();
731 glColor4f(clr
.r
/255.0f, clr
.g
/255.0f, clr
.b
/255.0f, clr
.a
/255.0f);
733 pointsStarted
= false;
735 if (!pointsStarted
) {
737 pointsStarted
= true;
739 glVertex2i(dl.actorX
, dl.actorY
);
744 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
749 if (/*altMove || movement ||*/ scale
== 1 ||
!cameraChick
.valid
) {
754 vportX1
= map
.width
*TileSize
;
755 vportY1
= map
.height
*TileSize
;
757 int tiltHeight
= /*getMapViewHeight()*/(vlHeight
/scale
)/4;
758 int vy
= cameraChick
.looky
!int;
759 if (vy
< -tiltHeight
) vy
= -tiltHeight
; else if (vy
> tiltHeight
) vy
= tiltHeight
;
760 int swdt
= vlWidth
/scale
;
761 int shgt
= vlHeight
/scale
;
762 int x
= camchickdi
.actorX
-swdt
/2;
763 int y
= (camchickdi
.actorY
+vy
)-shgt
/2;
764 if (x
< 0) x
= 0; else if (x
>= map
.width
*TileSize
-swdt
) x
= map
.width
*TileSize
-swdt
-1;
765 if (y
< 0) y
= 0; else if (y
>= map
.height
*TileSize
-shgt
) y
= map
.height
*TileSize
-shgt
-1;
768 vportX0
= mofsx
/scale
;
769 vportY0
= mofsy
/scale
;
770 vportX1
= vportX0
+vlWidth
/scale
;
771 vportY1
= vportY0
+vlHeight
/scale
;
774 //glDisable(GL_DEPTH_TEST);
777 // do liquid coloring
778 drawAtXY(map
.texgl
.ptr
[map
.LiquidMask
], 0, 0);
779 // foreground -- hide secrets, draw lifts and such
780 drawAtXY(map
.texgl
.ptr
[map
.Front
], 0, 0);
788 glColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
789 glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
790 if (cameraChick.valid) {
792 glVertex2i(camchickdi.actorX, camchickdi.actorY-70);
794 //glRectf(camchickdi.actorX, camchickdi.actorY-70, camchickdi.actorX+4, camchickdi.actorY-70+4);
796 //glRectf(0, 0, 300, 300);
797 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
805 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
807 // make smaller occluder texture, so we can trace faster
808 //assert(fboLMaskSmall.tex.width == map.width);
809 //assert(fboLMaskSmall.tex.height == map.height);
811 orthoCamera(map
.width
, map
.height
);
812 drawAtXY(map
.texgl
.ptr
[map
.LightMask
].tid
, 0, 0, map
.width
, map
.height
, mirrorY
:true);
817 glClearColor(BackIntens
, BackIntens
, BackIntens
, 1.0f);
818 //glClearColor(0.15f, 0.15f, 0.15f, 1.0f);
819 ////glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
820 glClear(GL_COLOR_BUFFER_BIT
);
823 // texture 1 is background
824 glActiveTexture(GL_TEXTURE0
+1);
825 glBindTexture(GL_TEXTURE_2D
, fboOrigBack
.tex
.tid
);
826 // texture 2 is occluders
827 glActiveTexture(GL_TEXTURE0
+2);
828 glBindTexture(GL_TEXTURE_2D
, map
.texgl
.ptr
[map
.LightMask
].tid
);
829 // texture 3 is small occluder map
830 glActiveTexture(GL_TEXTURE0
+3);
831 glBindTexture(GL_TEXTURE_2D
, fboLMaskSmall
.tex
.tid
);
832 // done texture assign
833 glActiveTexture(GL_TEXTURE0
+0);
838 renderLight( 27, 391-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 100);
839 renderLight(542, 424-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 100);
840 renderLight(377, 368-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 32);
841 renderLight(147, 288-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 64);
842 renderLight( 71, 200-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 128);
843 renderLight(249, 200-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 128);
844 renderLight(426, 200-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 128);
845 renderLight(624, 200-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 128);
846 renderLight(549, 298-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 64);
847 renderLight( 74, 304-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 32);
849 renderLight(24*TileSize+4, (24+18)*TileSize-2+LYOfs, SVec4F(0.6f, 0.0f, 0.0f, 1.0f), 128);
851 renderLight(280, 330, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 64);
855 foreach (ref li
; attachedLights
[0..attachedLightCount
]) {
856 if (li
.type
== AttachedLightInfo
.Type
.Ambient
) {
857 //conwriteln("ambient: x=", li.x, "; y=", li.y, "; w=", li.w, "; h=", li.h);
860 renderLightAmbient(li
.x
, li
.y
, li
.w
, li
.h
, SVec4F(0.0f, 0.0f, 0.0f, li
.r
));
862 renderLightAmbient(li
.x
, li
.y
, li
.w
, li
.h
, SVec4F(li
.r
, li
.g
, li
.b
, 1.0f));
864 } else if (li
.type
== AttachedLightInfo
.Type
.Point
) {
867 renderLight(li
.x
, li
.y
, SVec4F(0.0f, 0.0f, 0.0f, li
.r
), li
.radius
);
869 renderLight(li
.x
, li
.y
, SVec4F(li
.r
, li
.g
, li
.b
, 1.0f), li
.radius
);
875 if (testLightMoved
) {
876 testLightX
= testLightX
/scale
+mofsx
/scale
;
877 testLightY
= testLightY
/scale
+mofsy
/scale
;
878 testLightMoved
= false;
880 foreach (immutable _
; 0..1) {
881 renderLight(testLightX
, testLightY
, SVec4F(0.3f, 0.3f, 0.0f, 1.0f), 96);
885 glActiveTexture(GL_TEXTURE0
+1);
886 glBindTexture(GL_TEXTURE_2D
, 0);
887 glActiveTexture(GL_TEXTURE0
+2);
888 glBindTexture(GL_TEXTURE_2D
, 0);
889 glActiveTexture(GL_TEXTURE0
+3);
890 glBindTexture(GL_TEXTURE_2D
, 0);
891 glActiveTexture(GL_TEXTURE0
+0);
896 shadScanlines.exec((Shader shad) {
897 shad["scanlines"] = scanlines;
898 glClearColor(BackIntens, BackIntens, BackIntens, 1.0f);
899 glClear(GL_COLOR_BUFFER_BIT);
900 orthoCamera(vlWidth, vlHeight);
901 //orthoCamera(map.width*TileSize*scale, map.height*TileSize*scale);
902 //glMatrixMode(GL_MODELVIEW);
904 //glTranslatef(0.375, 0.375, 0); // to be pixel-perfect
905 // somehow, FBO objects are mirrored; wtf?!
906 drawAtXY(fboLevel.tex.tid, -mapOfsX, -mapOfsY, map.width*TileSize*scale, map.height*TileSize*scale, mirrorY:true);
913 smDrawText(map.width*TileSize/2, map.height*TileSize/2, "Testing...");
918 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
924 //auto img = smfont.ptr[0x39];
927 //conwriteln("img.sx=", img.sx, "; img.sy=", img.sy, "; ", img.width, "x", img.height);
928 drawAtXY(img.tex, 10-img.sx, 10-img.sy);
935 { // http://stackoverflow.com/questions/7207422/setting-up-opengl-multiple-render-targets
936 GLenum[1] buffers = [ GL_BACK_LEFT, /*GL_COLOR_ATTACHMENT0_EXT*/ ];
937 //GLenum[1] buffers = [ GL_NONE, /*GL_COLOR_ATTACHMENT0_EXT*/ ];
938 glDrawBuffers(1, buffers.ptr);
943 orthoCamera(vlWidth
, vlHeight
);
944 auto tex
= (doLighting ? fboLevelLight
.tex
.tid
: fboOrigBack
.tex
.tid
);
945 drawAtXY(tex
, -mofsx
, -mofsy
, map
.width
*TileSize
*scale
, map
.height
*TileSize
*scale
, mirrorY
:true);
948 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
949 //orthoCamera(map.width*TileSize, map.height*TileSize);
951 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
952 hudScripts
.runDraw();
955 //drawAtXY(map.texgl.ptr[map.LightMask].tid, -mofsx, -mofsy, map.width*TileSize*scale, map.height*TileSize*scale, mirrorY:true);
956 //drawAtXY(fboLMaskSmall.tex.tid, 0, 0, map.width*TileSize, map.height*TileSize);
960 //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
961 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
962 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
964 fboEditor
.tex
.setFromImage(editorImg
);
965 drawAtXY(fboEditor
.tex
, 0, 0);
973 // ////////////////////////////////////////////////////////////////////////// //
974 // returns time slept
975 int sleepAtMaxMsecs (int msecs
) {
977 import core
.sys
.posix
.signal
: timespec
;
978 import core
.sys
.posix
.time
: nanosleep
;
979 timespec ts
= void, tpassed
= void;
981 ts
.tv_nsec
= msecs
*1000*1000+(500*1000); // milli to nano
982 nanosleep(&ts
, &tpassed
);
983 return (ts
.tv_nsec
-tpassed
.tv_nsec
)/(1000*1000);
990 // ////////////////////////////////////////////////////////////////////////// //
991 mixin(import("editor.d"));
994 // ////////////////////////////////////////////////////////////////////////// //
996 shared int diedie
= 0;
998 enum D2DFrameTime
= 55; // milliseconds
999 enum MinFrameTime
= 1000/60; // ~60 FPS
1001 public void renderThread (Tid starterTid
) {
1002 enum BoolOptVarMsgMixin(string varname
) =
1003 "if (msg.toggle) msg.value = !"~varname
~";\n"~
1004 "if ("~varname
~" != msg.value) "~varname
~" = msg.value; else msg.showMessage = false;\n";
1006 send(starterTid
, 42);
1008 MonoTime curtime
= MonoTime
.currTime
;
1010 lastthink
= curtime
; // for interpolator
1011 nextthink
= curtime
+dur
!"msecs"(D2DFrameTime
);
1012 MonoTime nextvframe
= curtime
;
1014 enum MaxFPSFrames
= 16;
1015 float frtimes
= 0.0f;
1018 int hushFrames
= 6; // ignore first `hushFrames` frames overtime
1019 MonoTime prevFrameStartTime
= curtime
;
1021 bool vframeWasLost
= false;
1023 void resetFrameTimers () {
1024 MonoTime curtime
= MonoTime
.currTime
;
1025 lastthink
= curtime
; // for interpolator
1026 nextthink
= curtime
+dur
!"msecs"(D2DFrameTime
);
1027 nextvframe
= curtime
;
1030 void loadNewLevel (string name
) {
1033 conwriteln("ERROR: can't load new levels yet");
1037 if (name
.length
== 0) {
1038 conwriteln("ERROR: can't load empty level!");
1040 conwriteln("loading map '", name
, "'");
1045 void receiveMessages () {
1047 import core
.time
: Duration
;
1048 //conwriteln("rendering thread: waiting for messages...");
1049 auto got
= receiveTimeout(
1050 Duration
.zero
, // don't wait
1052 addMessage(msg
.text
[0..msg
.textlen
], msg
.pauseMsecs
, msg
.noreplace
);
1054 (TMsgLoadLevel msg
) {
1055 nextmapname
= null; // clear "exit" flag
1056 loadNewLevel(msg
.mapfile
[0..msg
.textlen
].idup
);
1058 (TMsgSkipLevel msg
) {
1059 string mn
= genNextMapName(0);
1061 nextmapname
= null; // clear "exit" flag
1065 (TMsgIntOption msg
) {
1066 final switch (msg
.type
) with (TMsgIntOption
.Type
) {
1068 if (msg
.value
>= 1 && msg
.value
<= 2) scale
= msg
.value
;
1072 (TMsgBoolOption msg
) {
1073 final switch (msg
.type
) with (TMsgBoolOption
.Type
) {
1074 case Interpolation
: mixin(BoolOptVarMsgMixin
!"frameInterpolation"); break;
1075 case Lighting
: mixin(BoolOptVarMsgMixin
!"doLighting"); break;
1076 case Pause
: mixin(BoolOptVarMsgMixin
!"gamePaused"); break;
1078 if (msg
.toggle
) msg
.value
= !inEditMode
;
1079 if (inEditMode
!= msg
.value
) {
1080 inEditMode
= msg
.value
;
1081 if (msg
.value
) sdwindow
.hideCursor(); else sdwindow
.showCursor();
1083 msg
.showMessage
= false;
1086 mixin(BoolOptVarMsgMixin
!"inEditMode"); break;
1087 case CheatNoDoors
: mixin(BoolOptVarMsgMixin
!"cheatNoDoors"); break;
1088 case CheatNoWallClip
: mixin(BoolOptVarMsgMixin
!"cheatNoWallClip"); break;
1090 if (msg
.showMessage
) {
1093 void putStr(T
) (T s
) if (is(T
: const(char)[])) {
1094 foreach (char ch
; s
) {
1095 if (msgpos
>= mbuf
.length
) break;
1096 mbuf
.ptr
[msgpos
++] = ch
;
1099 putStr("Option \"");
1100 { import std
.conv
: to
; putStr(to
!string(msg
.type
)); }
1102 if (msg
.value
) putStr("N"); else putStr("FF");
1103 addMessage(mbuf
[0..msgpos
]);
1106 (TMsgTestLightMove msg
) {
1109 testLightMoved
= true;
1111 (TMsgMouseEvent msg
) { editorMouseEvent(msg
); },
1112 (TMsgKeyEvent msg
) { editorKeyEvent(msg
); },
1114 conwriteln("WARNING: unknown thread message received and ignored");
1119 //conwriteln("rendering thread: no more messages");
1123 if (nextmapname
.length
) {
1124 string mn
= nextmapname
;
1125 nextmapname
= null; // clear "exit" flag
1130 // "D2D frames"; curtime should be set; return `true` if frame was processed; will fix `curtime`
1131 bool doThinkFrame () {
1132 if (curtime
>= nextthink
) {
1133 lastthink
= curtime
;
1134 while (nextthink
<= curtime
) nextthink
+= dur
!"msecs"(D2DFrameTime
);
1136 // save snapshot and other data for interpolator
1137 Actor
.saveSnapshot(prevFrameActorsData
[], prevFrameActorOfs
.ptr
);
1138 if (!gamePaused
&& !inEditMode
) {
1145 auto tm
= MonoTime
.currTime
;
1146 int thinkTime
= cast(int)((tm
-curtime
).total
!"msecs");
1147 if (thinkTime
> 9) { import core
.stdc
.stdio
; printf("spent on thinking: %d msecs\n", thinkTime
); }
1155 // "video frames"; curtime should be set; return `true` if frame was processed; will fix `curtime`
1157 version(dont_use_vsync
) {
1159 enum doCheckTime
= true;
1162 __gshared
bool prevLost
= false;
1163 bool doCheckTime
= vframeWasLost
;
1164 if (vframeWasLost
) {
1166 { import core
.stdc
.stdio
; printf("frame was lost!\n"); }
1174 if (curtime
< nextvframe
) return false;
1175 version(dont_use_vsync
) {
1176 if (curtime
> nextvframe
) {
1177 auto overtime
= cast(int)((curtime
-nextvframe
).total
!"msecs");
1178 if (overtime
> 2500) {
1182 { import core
.stdc
.stdio
; printf(" spent whole %d msecs\n", overtime
); }
1188 while (nextvframe
<= curtime
) nextvframe
+= dur
!"msecs"(MinFrameTime
);
1192 scope(exit
) sdwindow
.mtUnlock();
1193 ctset
= sdwindow
.setAsCurrentOpenGlContextNT
;
1195 // if we can't set context, pretend that videoframe was processed; this should solve problem with vsync and invisible window
1198 iLiquidTime
= cast(float)((curtime
-MonoTime
.zero
).total
!"msecs"%10000000)/18.0f*0.04f;
1199 receiveMessages(); // here, 'cause we need active OpenGL context for some messages
1201 renderScene(curtime
);
1203 //renderLoading(curtime);
1206 scope(exit
) sdwindow
.mtUnlock();
1207 sdwindow
.swapOpenGlBuffers();
1209 sdwindow
.releaseCurrentOpenGlContext();
1210 vframeWasLost
= false;
1212 vframeWasLost
= true;
1213 { import core
.stdc
.stdio
; printf("xframe was lost!\n"); }
1215 curtime
= MonoTime
.currTime
;
1220 if (sdwindow
.closed
) break;
1221 if (atomicLoad(diedie
) > 0) break;
1223 curtime
= MonoTime
.currTime
;
1224 auto fstime
= curtime
;
1226 doThinkFrame(); // this will fix curtime if necessary
1228 if (!vframeWasLost
) {
1230 auto frameTime
= cast(float)(curtime
-prevFrameStartTime
).total
!"msecs"/1000.0f;
1231 prevFrameStartTime
= curtime
;
1232 frtimes
+= frameTime
;
1233 if (++framenum
>= MaxFPSFrames || frtimes
>= 3.0f) {
1234 import std
.string
: format
;
1235 int newFPS
= cast(int)(cast(float)MaxFPSFrames
/frtimes
+0.5);
1236 if (newFPS
!= prevFPS
) {
1237 sdwindow
.title
= "%s / FPS:%s".format("D2D", newFPS
);
1246 curtime
= MonoTime
.currTime
;
1248 // now sleep until next "video" or "think" frame
1249 if (nextthink
> curtime
&& nextvframe
> curtime
) {
1251 immutable nextVideoFrameSleep
= cast(int)((nextvframe
-curtime
).total
!"msecs");
1252 immutable nextThinkFrameSleep
= cast(int)((nextthink
-curtime
).total
!"msecs");
1253 immutable sleepTime
= (nextVideoFrameSleep
< nextThinkFrameSleep ? nextVideoFrameSleep
: nextThinkFrameSleep
);
1254 sleepAtMaxMsecs(sleepTime
);
1255 //curtime = MonoTime.currTime;
1258 } catch (Throwable e
) {
1259 // here, we are dead and fucked (the exact order doesn't matter)
1260 import core
.stdc
.stdlib
: abort
;
1261 import core
.stdc
.stdio
: fprintf
, stderr
;
1262 import core
.memory
: GC
;
1263 GC
.disable(); // yeah
1264 thread_suspendAll(); // stop right here, you criminal scum!
1265 auto s
= e
.toString();
1266 fprintf(stderr
, "\n=== FATAL ===\n%.*s\n", cast(uint)s
.length
, s
.ptr
);
1267 abort(); // die, you bitch!
1269 import core.stdc.stdio;
1270 fprintf(stderr, "FUUUUUUUUUUUUUUUUUUUUUUUUUU\n");
1271 import std.stdio : stderr;
1274 if (sdwindow.closed) break;
1275 if (atomicLoad(diedie) > 0) break;
1276 sleepAtMaxMsecs(100);
1280 atomicStore(diedie
, 2);
1284 // ////////////////////////////////////////////////////////////////////////// //
1285 __gshared Tid renderTid
;
1286 shared bool renderThreadStarted
= false;
1289 public void startRenderThread () {
1290 if (!cas(&renderThreadStarted
, false, true)) {
1291 assert(0, "render thread already started!");
1293 renderTid
= spawn(&renderThread
, thisTid
);
1294 setMaxMailboxSize(renderTid
, 1024, OnCrowding
.throwException
); //FIXME
1295 // wait for "i'm ready" signal
1298 if (ok
!= 42) assert(0, "wtf?!");
1301 conwriteln("rendering thread started");
1305 // ////////////////////////////////////////////////////////////////////////// //
1306 public void closeWindow () {
1307 if (atomicLoad(diedie
) != 2) {
1308 atomicStore(diedie
, 1);
1309 while (atomicLoad(diedie
) != 2) {}
1311 if (!sdwindow
.closed
) {
1318 // ////////////////////////////////////////////////////////////////////////// //
1322 // ////////////////////////////////////////////////////////////////////////// //
1323 struct TMsgMouseEvent
{
1324 MouseEventType type
;
1327 MouseButton button
; /// See $(LREF MouseButton)
1328 int modifierState
; /// See $(LREF ModifierState)
1331 public void postMouseEvent() (in auto ref MouseEvent evt
) {
1332 if (!atomicLoad(renderThreadStarted
)) return;
1334 msg
.type
= evt
.type
;
1339 msg
.button
= evt
.button
;
1340 msg
.modifierState
= evt
.modifierState
;
1341 send(renderTid
, msg
);
1345 // ////////////////////////////////////////////////////////////////////////// //
1346 struct TMsgKeyEvent
{
1354 public void postKeyEvent() (in auto ref KeyEvent evt
) {
1355 if (!atomicLoad(renderThreadStarted
)) return;
1358 msg
.pressed
= evt
.pressed
;
1359 msg
.character
= evt
.character
;
1360 msg
.modifierState
= evt
.modifierState
;
1361 send(renderTid
, msg
);
1365 // ////////////////////////////////////////////////////////////////////////// //
1366 struct TMsgTestLightMove
{
1370 public void postTestLightMove (int x
, int y
) {
1371 if (!atomicLoad(renderThreadStarted
)) return;
1372 auto msg
= TMsgTestLightMove(x
, y
);
1373 send(renderTid
, msg
);
1377 // ////////////////////////////////////////////////////////////////////////// //
1378 struct TMsgMessage
{
1385 public void postAddMessage (const(char)[] msgtext
, int pauseMsecs
=3000, bool noreplace
=false) {
1386 if (!atomicLoad(renderThreadStarted
)) return;
1387 if (msgtext
.length
> TMsgMessage
.text
.length
) msgtext
= msgtext
[0..TMsgMessage
.text
.length
];
1389 msg
.textlen
= cast(uint)msgtext
.length
;
1390 if (msg
.textlen
) msg
.text
[0..msg
.textlen
] = msgtext
[0..msg
.textlen
];
1391 msg
.pauseMsecs
= pauseMsecs
;
1392 msg
.noreplace
= noreplace
;
1393 send(renderTid
, msg
);
1397 // ////////////////////////////////////////////////////////////////////////// //
1398 struct TMsgLoadLevel
{
1403 public void postLoadLevel (const(char)[] mapfile
) {
1404 if (!atomicLoad(renderThreadStarted
)) return;
1405 if (mapfile
.length
> TMsgLoadLevel
.mapfile
.length
) mapfile
= mapfile
[0..TMsgLoadLevel
.mapfile
.length
];
1407 msg
.textlen
= cast(uint)mapfile
.length
;
1408 if (msg
.textlen
) msg
.mapfile
[0..msg
.textlen
] = mapfile
[0..msg
.textlen
];
1409 send(renderTid
, msg
);
1413 // ////////////////////////////////////////////////////////////////////////// //
1414 struct TMsgSkipLevel
{
1417 public void postSkipLevel () {
1418 if (!atomicLoad(renderThreadStarted
)) return;
1420 send(renderTid
, msg
);
1424 // ////////////////////////////////////////////////////////////////////////// //
1425 struct TMsgIntOption
{
1435 struct TMsgBoolOption
{
1451 bool strCaseEqu (const(char)[] s0
, const(char)[] s1
) {
1452 if (s0
.length
!= s1
.length
) return false;
1453 foreach (immutable idx
, char ch
; s0
) {
1454 if (ch
>= 'A' && ch
<= 'Z') ch
+= 32;
1455 char c1
= s1
.ptr
[idx
];
1456 if (c1
>= 'A' && c1
<= 'Z') c1
+= 32;
1457 if (ch
!= c1
) return false;
1463 public bool postToggleOption (const(char)[] name
, bool showMessage
=false) { /*pragma(inline, true);*/ return postSetOption(name
, true, showMessage
:showMessage
, toggle
:true); }
1465 public bool postSetOption(T
) (const(char)[] name
, T value
, bool showMessage
=false, bool toggle
=false) if ((is(T
== int) ||
is(T
== bool))) {
1466 if (!atomicLoad(renderThreadStarted
)) return false;
1467 if (name
.length
== 0 || name
.length
> 127) return false;
1468 static if (is(T
== int)) {
1470 foreach (string oname
; __traits(allMembers
, TMsgIntOption
.Type
)) {
1471 if (strCaseEqu(oname
, name
)) {
1472 msg
.type
= __traits(getMember
, TMsgIntOption
.Type
, oname
);
1474 msg
.showMessage
= showMessage
;
1475 send(renderTid
, msg
);
1479 } else static if (is(T
== bool)) {
1481 foreach (string oname
; __traits(allMembers
, TMsgBoolOption
.Type
)) {
1482 if (strCaseEqu(oname
, name
)) {
1483 msg
.type
= __traits(getMember
, TMsgBoolOption
.Type
, oname
);
1485 msg
.toggle
= toggle
;
1486 msg
.showMessage
= showMessage
;
1487 send(renderTid
, msg
);
1492 static assert(0, "invalid option type '"~T
.stringof
~"'");