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
);
268 dotsAwake(); // let dormant dots fall
272 // ////////////////////////////////////////////////////////////////////////// //
275 enum Phase
{ FadeIn
, Stay
, FadeOut
}
284 private import core
.sync
.mutex
: Mutex
;
286 __gshared Message
[128] messages
;
287 __gshared
uint messagesUsed
= 0;
289 //__gshared Mutex messageLock;
290 //shared static this () { messageLock = new Mutex(); }
293 void addMessage (const(char)[] msgtext
, int pauseMsecs
=3000, bool noreplace
=false) {
294 //messageLock.lock();
295 //scope(exit) messageLock.unlock();
296 if (msgtext
.length
== 0) return;
298 if (pauseMsecs
<= 50) return;
299 if (messagesUsed
== messages
.length
) {
300 // remove top message
301 foreach (immutable cidx
; 1..messagesUsed
) messages
.ptr
[cidx
-1] = messages
.ptr
[cidx
];
302 messages
.ptr
[0].alpha
= 255;
306 if (!noreplace
&& messagesUsed
== 1) {
307 switch (messages
.ptr
[0].phase
) {
308 case Message
.Phase
.FadeIn
:
309 messages
.ptr
[0].phase
= Message
.Phase
.FadeOut
;
311 case Message
.Phase
.Stay
:
312 messages
.ptr
[0].phase
= Message
.Phase
.FadeOut
;
313 messages
.ptr
[0].alpha
= 255;
318 auto msg
= messages
.ptr
+messagesUsed
;
320 msg
.phase
= Message
.Phase
.FadeIn
;
322 msg
.pauseMsecs
= pauseMsecs
;
324 if (msgtext
.length
> msg
.text
.length
) {
325 msg
.text
= msgtext
[0..msg
.text
.length
];
326 msg
.textlen
= msg
.text
.length
;
328 msg
.text
[0..msgtext
.length
] = msgtext
[];
329 msg
.textlen
= msgtext
.length
;
334 void doMessages (MonoTime curtime
) {
335 //messageLock.lock();
336 //scope(exit) messageLock.unlock();
338 if (messagesUsed
== 0) return;
340 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
346 final switch (msg
.phase
) {
347 case Message
.Phase
.FadeIn
:
348 if ((msg
.alpha
+= 10) >= 255) {
349 msg
.phase
= Message
.Phase
.Stay
;
350 msg
.removeTime
= curtime
+dur
!"msecs"(msg
.pauseMsecs
);
351 goto case; // to stay
353 glColor4f(1.0f, 1.0f, 1.0f, msg
.alpha
/255.0f);
355 case Message
.Phase
.Stay
:
356 if (msg
.removeTime
<= curtime
) {
358 msg
.phase
= Message
.Phase
.FadeOut
;
359 goto case; // to fade
361 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
363 case Message
.Phase
.FadeOut
:
364 if ((msg
.alpha
-= 10) <= 0) {
365 if (--messagesUsed
== 0) return;
366 // remove this message
367 foreach (immutable cidx
; 1..messagesUsed
+1) messages
.ptr
[cidx
-1] = messages
.ptr
[cidx
];
370 glColor4f(1.0f, 1.0f, 1.0f, msg
.alpha
/255.0f);
374 smDrawText(10, 10, msg
.text
[0..msg
.textlen
]);
378 // ////////////////////////////////////////////////////////////////////////// //
379 //mixin(Actor.FieldPropMixin!("0drawlistpos", uint));
381 mixin(Actor
.FieldGetMixin
!("classtype", StrId
)); // fget_classtype
382 mixin(Actor
.FieldGetMixin
!("classname", StrId
)); // fget_classname
383 mixin(Actor
.FieldGetMixin
!("x", int));
384 mixin(Actor
.FieldGetMixin
!("y", int));
385 mixin(Actor
.FieldGetMixin
!("s", int));
386 mixin(Actor
.FieldGetMixin
!("radius", int));
387 mixin(Actor
.FieldGetMixin
!("height", int));
388 mixin(Actor
.FieldGetMixin
!("flags", uint));
389 mixin(Actor
.FieldGetMixin
!("zAnimstate", StrId
));
390 mixin(Actor
.FieldGetMixin
!("zAnimidx", int));
391 mixin(Actor
.FieldGetMixin
!("dir", uint));
392 mixin(Actor
.FieldGetMixin
!("attLightXOfs", int));
393 mixin(Actor
.FieldGetMixin
!("attLightYOfs", int));
394 mixin(Actor
.FieldGetMixin
!("attLightRGBX", uint));
396 //mixin(Actor.FieldGetPtrMixin!("classtype", StrId)); // fget_classtype
397 //mixin(Actor.FieldGetPtrMixin!("classname", StrId)); // fget_classname
398 mixin(Actor
.FieldGetPtrMixin
!("x", int));
399 mixin(Actor
.FieldGetPtrMixin
!("y", int));
400 //mixin(Actor.FieldGetPtrMixin!("flags", uint));
401 //mixin(Actor.FieldGetPtrMixin!("zAnimstate", StrId));
402 //mixin(Actor.FieldGetPtrMixin!("zAnimidx", int));
403 //mixin(Actor.FieldGetPtrMixin!("dir", uint));
404 //mixin(Actor.FieldGetPtrMixin!("attLightXOfs", int));
405 //mixin(Actor.FieldGetPtrMixin!("attLightYOfs", int));
406 //mixin(Actor.FieldGetPtrMixin!("attLightRGBX", uint));
409 // ////////////////////////////////////////////////////////////////////////// //
410 __gshared
int vportX0
, vportY0
, vportX1
, vportY1
;
413 // ////////////////////////////////////////////////////////////////////////// //
414 void renderLightAmbient() (int lightX
, int lightY
, int lightW
, int lightH
, in auto ref SVec4F lcol
) {
415 //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));
416 if (lightW
< 1 || lightH
< 1) return;
417 int lightX1
= lightX
+lightW
-1;
418 int lightY1
= lightY
+lightH
-1;
419 // clip light to viewport
420 if (lightX
< vportX0
) lightX
= vportX0
;
421 if (lightY
< vportY0
) lightY
= vportY0
;
422 if (lightX1
> vportX1
) lightX1
= vportX1
;
423 if (lightY1
> vportY1
) lightY1
= vportY1
;
424 // is this light visible?
425 //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));
426 if (lightX1
< lightX || lightY1
< lightY || lightX
> vportX1 || lightY
> vportY1 || lightX1
< vportX0 || lightY1
< vportY0
) return;
427 //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));
431 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
432 //glDisable(GL_BLEND);
433 orthoCamera(map
.width
*TileSize
, map
.height
*TileSize
);
434 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
435 shadAmbient
.exec((Shader shad
) {
436 shad
["lightColor"] = SVec4F(lcol
.x
, lcol
.y
, lcol
.z
, lcol
.w
);
437 //shad["lightPos"] = SVec2F(lightX, lightY);
438 glRectf(lightX
, lightY
, lightX1
, lightY1
);
444 // ////////////////////////////////////////////////////////////////////////// //
445 void renderLight() (int lightX
, int lightY
, in auto ref SVec4F lcol
, int lightRadius
) {
446 if (lightRadius
< 2) return;
447 if (lightRadius
> MaxLightRadius
) lightRadius
= MaxLightRadius
;
448 int lightSize
= lightRadius
*2;
449 // is this light visible?
450 if (lightX
<= -lightRadius || lightY
<= -lightRadius || lightX
-lightRadius
>= map
.width
*TileSize || lightY
-lightRadius
>= map
.height
*TileSize
) return;
452 // out of viewport -- do nothing
453 if (lightX
+lightRadius
< vportX0 || lightY
+lightRadius
< vportY0
) return;
454 if (lightX
-lightRadius
> vportX1 || lightY
-lightRadius
> vportY1
) return;
456 if (lightX
>= 0 && lightY
>= 0 && lightX
< map
.width
*TileSize
&& lightY
< map
.height
*TileSize
&&
457 map
.teximgs
[map
.LightMask
].imageData
.colors
.ptr
[lightY
*(map
.width
*TileSize
)+lightX
].a
> 190) return;
459 // common color for all the following
461 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
463 // build 1d distance map to fboShadowMapId
464 fboDistMap
.ptr
[lightRadius
].exec({
465 // no need to clear it, shader will take care of that
466 shadToPolar
.exec((Shader shad
) {
467 shad
["lightTexSize"] = SVec2F(lightSize
, lightSize
);
468 shad
["lightPos"] = SVec2F(lightX
, lightY
);
469 orthoCamera(lightSize
, 1);
470 // it doesn't matter what we will draw here, so just draw filled rect
471 glRectf(0, 0, lightSize
, 1);
475 // build light texture for blending
477 // no need to clear it, shader will take care of that
479 //glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
480 //glClear(GL_COLOR_BUFFER_BIT);
481 shadBlur
.exec((Shader shad
) {
482 shad
["lightTexSize"] = SVec2F(lightSize
, MaxLightRadius
*2); // x: size of distmap; y: size of this texture
483 shad
["lightColor"] = SVec4F(lcol
.x
, lcol
.y
, lcol
.z
, lcol
.w
);
484 shad
["lightPos"] = SVec2F(lightX
-lightRadius
, lightY
-lightRadius
);
485 orthoCamera(fboOccluders
.tex
.width
, fboOccluders
.tex
.height
);
486 //drawAtXY(fboDistMap[lightRadius].tex.tid, 0, 0, lightSize, lightSize);
487 bindTexture(fboDistMap
.ptr
[lightRadius
].tex
.tid
);
489 glTexCoord2f(0.0f, 0.0f); glVertex2i(0, 0); // top-left
490 glTexCoord2f(1.0f, 0.0f); glVertex2i(lightSize
, 0); // top-right
491 glTexCoord2f(1.0f, 1.0f); glVertex2i(lightSize
, lightSize
); // bottom-right
492 glTexCoord2f(0.0f, 1.0f); glVertex2i(0, lightSize
); // bottom-left
497 // blend light texture
500 //glDisable(GL_BLEND);
501 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
502 orthoCamera(fboLevelLight
.tex
.width
, fboLevelLight
.tex
.height
);
503 //drawAtXY(fboOccluders.tex, lightX-lightRadius, lightY-lightRadius, mirrorY:true);
504 float occe
= 1.0f*lightSize
/(MaxLightRadius
*2);
505 float occs
= 1.0f-occe
;
506 int x0
= lightX
-lightRadius
+0;
507 int y1
= lightY
-lightRadius
+0;
508 int x1
= lightX
+lightRadius
-1+1;
509 int y0
= lightY
+lightRadius
-1+1;
510 bindTexture(fboOccluders
.tex
.tid
);
513 glTexCoord2f(0.0f, 0.0f); glVertex2i(x0, y0); // top-left
514 glTexCoord2f(occe, 0.0f); glVertex2i(x1, y0); // top-right
515 glTexCoord2f(occe, occe); glVertex2i(x1, y1); // bottom-right
516 glTexCoord2f(0.0f, occe); glVertex2i(x0, y1); // bottom-left
518 glTexCoord2f(0.0f, occs
); glVertex2i(x0
, y0
); // top-left
519 glTexCoord2f(occe
, occs
); glVertex2i(x1
, y0
); // top-right
520 glTexCoord2f(occe
, 1.0f); glVertex2i(x1
, y1
); // bottom-right
521 glTexCoord2f(0.0f, 1.0f); glVertex2i(x0
, y1
); // bottom-left
525 glRectf(x0, y0, x1, y1);
527 // and blend it again, with the shader that will touch only occluders
528 shadBlurOcc
.exec((Shader shad
) {
529 //shad["lightTexSize"] = SVec2F(lightSize, lightSize);
530 shad
["lightTexSize"] = SVec2F(lightSize
, fboOccluders
.tex
.height
);
531 shad
["lightColor"] = SVec4F(lcol
.x
, lcol
.y
, lcol
.z
, lcol
.w
);
532 shad
["lightPos"] = SVec2F(lightX
, lightY
);
533 //shad["lightPos"] = SVec2F(lightX-lightRadius, lightY-lightRadius);
534 bindTexture(fboOccluders
.tex
.tid
);
536 glTexCoord2f(0.0f, occs
); glVertex2i(x0
, y0
); // top-left
537 glTexCoord2f(occe
, occs
); glVertex2i(x1
, y0
); // top-right
538 glTexCoord2f(occe
, 1.0f); glVertex2i(x1
, y1
); // bottom-right
539 glTexCoord2f(0.0f, 1.0f); glVertex2i(x0
, y1
); // bottom-left
541 //drawAtXY(fboOccluders.tex.tid, lightX-lightRadius, lightY-lightRadius, lightSize, lightSize, mirrorY:true);
547 // ////////////////////////////////////////////////////////////////////////// //
548 __gshared
int testLightX
= vlWidth
/2, testLightY
= vlHeight
/2;
549 __gshared
bool testLightMoved
= false;
550 //__gshared int mapOfsX, mapOfsY;
551 //__gshared bool movement = false;
552 __gshared
float iLiquidTime
= 0.0;
553 //__gshared bool altMove = false;
556 void renderScene (MonoTime curtime
) {
557 //enum BackIntens = 0.05f;
558 enum BackIntens
= 0.0f;
561 float atob
= (curtime
> lastthink ?
cast(float)((curtime
-lastthink
).total
!"msecs")/cast(float)((nextthink
-lastthink
).total
!"msecs") : 1.0f);
562 if (gamePaused || inEditMode
) atob
= 1.0f;
563 //{ import core.stdc.stdio; printf("atob=%f\n", cast(double)atob); }
566 int framelen = cast(int)((nextthink-lastthink).total!"msecs");
567 int curfp = cast(int)((curtime-lastthink).total!"msecs");
568 { import core.stdc.stdio; printf("framelen=%d; curfp=%d\n", framelen, curfp); }
572 int mofsx
, mofsy
; // camera offset, will be set in background layer builder
574 if (mapTilesChanged
!= 0) rebuildMapMegaTextures();
576 // build background layer
578 //glDisable(GL_BLEND);
579 //glClearDepth(1.0f);
580 //glDepthFunc(GL_LESS);
581 //glDepthFunc(GL_NEVER);
582 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
583 glClear(GL_COLOR_BUFFER_BIT
/*|GL_DEPTH_BUFFER_BIT*/);
584 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
585 orthoCamera(map
.width
*TileSize
, map
.height
*TileSize
);
588 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
591 drawAtXY(map.skytexgl.tid, mapOfsX/2-map.MapSize*TileSize, mapOfsY/2-map.MapSize*TileSize, map.MapSize*TileSize, map.MapSize*TileSize);
592 drawAtXY(map.skytexgl.tid, mapOfsX/2-map.MapSize*TileSize, mapOfsY/2, map.MapSize*TileSize, map.MapSize*TileSize);
593 drawAtXY(map.skytexgl.tid, mapOfsX/2, mapOfsY/2-map.MapSize*TileSize, map.MapSize*TileSize, map.MapSize*TileSize);
594 drawAtXY(map.skytexgl.tid, mapOfsX/2, mapOfsY/2, map.MapSize*TileSize, map.MapSize*TileSize);
596 drawAtXY(map
.skytexgl
.tid
, 0, 0, map
.MapSize
*TileSize
, map
.MapSize
*TileSize
);
598 drawAtXY(map
.texgl
.ptr
[map
.Back
], 0, 0);
599 // draw distorted liquid areas
600 shadLiquidDistort
.exec((Shader shad
) {
601 shad
["iDistortTime"] = iLiquidTime
;
602 drawAtXY(map
.texgl
.ptr
[map
.AllLiquids
], 0, 0);
604 // monsters, items; we'll do linear interpolation here
605 glColor3f(1.0f, 1.0f, 1.0f);
606 //glEnable(GL_DEPTH_TEST);
607 attachedLightCount
= 0;
609 // who cares about memory?!
610 // draw order: players, items, monsters, other
611 static struct DrawInfo
{
615 @disable this (this); // no copies
617 enum { Pixels
, Players
, Items
, Monsters
, Other
}
618 __gshared DrawInfo
[65536][4] drawlists
;
619 __gshared
uint[4] dlpos
;
624 Actor
.forEach((ActorId me
) {
625 //me.fprop_0drawlistpos = 0;
626 if (auto adef
= findActorDef(me
)) {
628 switch (adef
.classtype
.get
) {
629 case "monster": dlnum
= (adef
.classname
.get
!= "Player" ? Monsters
: Players
); break;
630 case "item": dlnum
= Items
; break;
631 default: dlnum
= Other
; break;
633 if (me
.fget_flags
&AF_PIXEL
) dlnum
= Pixels
;
634 int actorX
, actorY
; // current actor position
636 auto ofs
= prevFrameActorOfs
.ptr
[me
.id
&0xffff];
637 if (frameInterpolation
&& ofs
< uint.max
-1 && Actor
.isSameSavedActor(me
.id
, prevFrameActorsData
.ptr
, ofs
)) {
638 import core
.stdc
.math
: roundf
;
639 auto xptr
= prevFrameActorsData
.ptr
+ofs
;
640 int ox
= xptr
.fgetp_x
;
642 int oy
= xptr
.fgetp_y
;
644 actorX
= cast(int)(ox
+roundf((nx
-ox
)*atob
));
645 actorY
= cast(int)(oy
+roundf((ny
-oy
)*atob
));
646 //conwriteln("actor ", me.id, "; o=(", ox, ",", oy, "); n=(", nx, ",", ny, "); p=(", x, ",", y, ")");
652 if (me
.id
== cameraChick
.id
) {
653 camchickdi
.adef
= adef
;
655 camchickdi
.actorX
= actorX
;
656 camchickdi
.actorY
= actorY
;
659 if ((me
.fget_flags
&AF_NODRAW
) == 0) {
660 //auto dl = &drawlists[dlnum][dlpos.ptr[dlnum]];
661 //me.fprop_0drawlistpos = (dlpos.ptr[dlnum]&0xffff)|((dlnum&0xff)<<16);
662 auto dl = drawlists
.ptr
[dlnum
].ptr
+dlpos
.ptr
[dlnum
];
669 // process attached lights
670 if ((me
.fget_flags
&AF_NOLIGHT
) == 0) {
671 uint alr
= me
.fget_attLightRGBX
;
672 bool isambient
= (me
.fget_classtype
.get
== "light" && me
.fget_classname
.get
== "Ambient");
673 if ((alr
&0xff) >= 4 ||
(isambient
&& me
.fget_radius
>= 1 && me
.fget_height
>= 1)) {
674 //if (isambient) conwriteln("isambient: ", isambient, "; x=", me.fget_x, "; y=", me.fget_y, "; w=", me.fget_radius, "; h=", me.fget_height);
676 auto li
= attachedLights
.ptr
+attachedLightCount
;
677 ++attachedLightCount
;
678 li
.type
= (!isambient ? AttachedLightInfo
.Type
.Point
: AttachedLightInfo
.Type
.Ambient
);
679 li
.x
= actorX
+me
.fget_attLightXOfs
;
680 li
.y
= actorY
+me
.fget_attLightYOfs
;
681 li
.r
= ((alr
>>24)&0xff)/255.0f; // red or intensity
682 if ((alr
&0x00_ff_ff
_00U) == 0x00_00_01_00U) {
685 li
.g
= ((alr
>>16)&0xff)/255.0f;
686 li
.b
= ((alr
>>8)&0xff)/255.0f;
687 li
.uncolored
= false;
689 li
.radius
= (alr
&0xff);
690 if (li
.radius
> MaxLightRadius
) li
.radius
= MaxLightRadius
;
692 li
.w
= me
.fget_radius
;
693 li
.h
= me
.fget_height
;
698 conwriteln("not found actor ", me
.id
, " (", me
.classtype
!string
, ":", me
.classname
!string
, ")");
703 foreach_reverse (uint dlnum
; 0..drawlists
.length
) {
704 if (dlnum
== Pixels
) continue;
705 auto dl = drawlists
.ptr
[dlnum
].ptr
;
706 if (dlnum
== Players
) dl += dlpos
.ptr
[dlnum
]-1;
707 foreach (uint idx
; 0..dlpos
.ptr
[dlnum
]) {
709 if (auto isp
= dl.adef
.animSpr(me
.fget_zAnimstate
, me
.fget_dir
, me
.fget_zAnimidx
)) {
710 //drawAtXY(isp.tex, dl.actorX-isp.vga.sx, dl.actorY-isp.vga.sy);
711 isp
.drawAtXY(dl.actorX
, dl.actorY
);
713 if (dlnum
!= Players
) ++dl; else --dl;
719 bool pointsStarted
= false;
720 Color lastColor
= Color(0, 0, 0, 0);
721 auto dl = drawlists
.ptr
[Pixels
].ptr
;
722 foreach (uint idx
; 0..dlpos
.ptr
[Pixels
]) {
725 if (s
< 0 || s
> 255) continue; //FIXME
726 Color clr
= d2dpal
.ptr
[s
&0xff];
727 if (clr
.a
== 0) continue;
728 if (clr
!= lastColor
) {
729 if (pointsStarted
) glEnd();
730 glColor4f(clr
.r
/255.0f, clr
.g
/255.0f, clr
.b
/255.0f, clr
.a
/255.0f);
732 pointsStarted
= false;
734 if (!pointsStarted
) {
736 pointsStarted
= true;
738 glVertex2i(dl.actorX
, dl.actorY
);
743 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
748 if (/*altMove || movement ||*/ scale
== 1 ||
!cameraChick
.valid
) {
753 vportX1
= map
.width
*TileSize
;
754 vportY1
= map
.height
*TileSize
;
756 int tiltHeight
= /*getMapViewHeight()*/(vlHeight
/scale
)/4;
757 int vy
= cameraChick
.looky
!int;
758 if (vy
< -tiltHeight
) vy
= -tiltHeight
; else if (vy
> tiltHeight
) vy
= tiltHeight
;
759 int swdt
= vlWidth
/scale
;
760 int shgt
= vlHeight
/scale
;
761 int x
= camchickdi
.actorX
-swdt
/2;
762 int y
= (camchickdi
.actorY
+vy
)-shgt
/2;
763 if (x
< 0) x
= 0; else if (x
>= map
.width
*TileSize
-swdt
) x
= map
.width
*TileSize
-swdt
-1;
764 if (y
< 0) y
= 0; else if (y
>= map
.height
*TileSize
-shgt
) y
= map
.height
*TileSize
-shgt
-1;
767 vportX0
= mofsx
/scale
;
768 vportY0
= mofsy
/scale
;
769 vportX1
= vportX0
+vlWidth
/scale
;
770 vportY1
= vportY0
+vlHeight
/scale
;
773 //glDisable(GL_DEPTH_TEST);
776 // do liquid coloring
777 drawAtXY(map
.texgl
.ptr
[map
.LiquidMask
], 0, 0);
778 // foreground -- hide secrets, draw lifts and such
779 drawAtXY(map
.texgl
.ptr
[map
.Front
], 0, 0);
787 glColor4f(r/255.0f, g/255.0f, b/255.0f, a/255.0f);
788 glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
789 if (cameraChick.valid) {
791 glVertex2i(camchickdi.actorX, camchickdi.actorY-70);
793 //glRectf(camchickdi.actorX, camchickdi.actorY-70, camchickdi.actorX+4, camchickdi.actorY-70+4);
795 //glRectf(0, 0, 300, 300);
796 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
804 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
806 // make smaller occluder texture, so we can trace faster
807 //assert(fboLMaskSmall.tex.width == map.width);
808 //assert(fboLMaskSmall.tex.height == map.height);
810 orthoCamera(map
.width
, map
.height
);
811 drawAtXY(map
.texgl
.ptr
[map
.LightMask
].tid
, 0, 0, map
.width
, map
.height
, mirrorY
:true);
816 glClearColor(BackIntens
, BackIntens
, BackIntens
, 1.0f);
817 //glClearColor(0.15f, 0.15f, 0.15f, 1.0f);
818 ////glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
819 glClear(GL_COLOR_BUFFER_BIT
);
822 // texture 1 is background
823 glActiveTexture(GL_TEXTURE0
+1);
824 glBindTexture(GL_TEXTURE_2D
, fboOrigBack
.tex
.tid
);
825 // texture 2 is occluders
826 glActiveTexture(GL_TEXTURE0
+2);
827 glBindTexture(GL_TEXTURE_2D
, map
.texgl
.ptr
[map
.LightMask
].tid
);
828 // texture 3 is small occluder map
829 glActiveTexture(GL_TEXTURE0
+3);
830 glBindTexture(GL_TEXTURE_2D
, fboLMaskSmall
.tex
.tid
);
831 // done texture assign
832 glActiveTexture(GL_TEXTURE0
+0);
837 renderLight( 27, 391-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 100);
838 renderLight(542, 424-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 100);
839 renderLight(377, 368-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 32);
840 renderLight(147, 288-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 64);
841 renderLight( 71, 200-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 128);
842 renderLight(249, 200-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 128);
843 renderLight(426, 200-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 128);
844 renderLight(624, 200-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 128);
845 renderLight(549, 298-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 64);
846 renderLight( 74, 304-0+LYOfs, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 32);
848 renderLight(24*TileSize+4, (24+18)*TileSize-2+LYOfs, SVec4F(0.6f, 0.0f, 0.0f, 1.0f), 128);
850 renderLight(280, 330, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 64);
854 foreach (ref li
; attachedLights
[0..attachedLightCount
]) {
855 if (li
.type
== AttachedLightInfo
.Type
.Ambient
) {
856 //conwriteln("ambient: x=", li.x, "; y=", li.y, "; w=", li.w, "; h=", li.h);
859 renderLightAmbient(li
.x
, li
.y
, li
.w
, li
.h
, SVec4F(0.0f, 0.0f, 0.0f, li
.r
));
861 renderLightAmbient(li
.x
, li
.y
, li
.w
, li
.h
, SVec4F(li
.r
, li
.g
, li
.b
, 1.0f));
863 } else if (li
.type
== AttachedLightInfo
.Type
.Point
) {
866 renderLight(li
.x
, li
.y
, SVec4F(0.0f, 0.0f, 0.0f, li
.r
), li
.radius
);
868 renderLight(li
.x
, li
.y
, SVec4F(li
.r
, li
.g
, li
.b
, 1.0f), li
.radius
);
874 if (testLightMoved
) {
875 testLightX
= testLightX
/scale
+mofsx
/scale
;
876 testLightY
= testLightY
/scale
+mofsy
/scale
;
877 testLightMoved
= false;
879 foreach (immutable _
; 0..1) {
880 renderLight(testLightX
, testLightY
, SVec4F(0.3f, 0.3f, 0.0f, 1.0f), 96);
884 glActiveTexture(GL_TEXTURE0
+1);
885 glBindTexture(GL_TEXTURE_2D
, 0);
886 glActiveTexture(GL_TEXTURE0
+2);
887 glBindTexture(GL_TEXTURE_2D
, 0);
888 glActiveTexture(GL_TEXTURE0
+3);
889 glBindTexture(GL_TEXTURE_2D
, 0);
890 glActiveTexture(GL_TEXTURE0
+0);
895 shadScanlines.exec((Shader shad) {
896 shad["scanlines"] = scanlines;
897 glClearColor(BackIntens, BackIntens, BackIntens, 1.0f);
898 glClear(GL_COLOR_BUFFER_BIT);
899 orthoCamera(vlWidth, vlHeight);
900 //orthoCamera(map.width*TileSize*scale, map.height*TileSize*scale);
901 //glMatrixMode(GL_MODELVIEW);
903 //glTranslatef(0.375, 0.375, 0); // to be pixel-perfect
904 // somehow, FBO objects are mirrored; wtf?!
905 drawAtXY(fboLevel.tex.tid, -mapOfsX, -mapOfsY, map.width*TileSize*scale, map.height*TileSize*scale, mirrorY:true);
912 smDrawText(map.width*TileSize/2, map.height*TileSize/2, "Testing...");
917 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
919 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
920 orthoCamera(map
.width
*TileSize
, map
.height
*TileSize
);
922 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
929 //auto img = smfont.ptr[0x39];
932 //conwriteln("img.sx=", img.sx, "; img.sy=", img.sy, "; ", img.width, "x", img.height);
933 drawAtXY(img.tex, 10-img.sx, 10-img.sy);
940 { // http://stackoverflow.com/questions/7207422/setting-up-opengl-multiple-render-targets
941 GLenum[1] buffers = [ GL_BACK_LEFT, /*GL_COLOR_ATTACHMENT0_EXT*/ ];
942 //GLenum[1] buffers = [ GL_NONE, /*GL_COLOR_ATTACHMENT0_EXT*/ ];
943 glDrawBuffers(1, buffers.ptr);
948 orthoCamera(vlWidth
, vlHeight
);
949 auto tex
= (doLighting ? fboLevelLight
.tex
.tid
: fboOrigBack
.tex
.tid
);
950 drawAtXY(tex
, -mofsx
, -mofsy
, map
.width
*TileSize
*scale
, map
.height
*TileSize
*scale
, mirrorY
:true);
952 hudScripts
.runDraw();
954 //drawAtXY(map.texgl.ptr[map.LightMask].tid, -mofsx, -mofsy, map.width*TileSize*scale, map.height*TileSize*scale, mirrorY:true);
955 //drawAtXY(fboLMaskSmall.tex.tid, 0, 0, map.width*TileSize, map.height*TileSize);
959 //glBlendFunc(GL_SRC_ALPHA, GL_ONE);
960 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
961 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
963 fboEditor
.tex
.setFromImage(editorImg
);
964 drawAtXY(fboEditor
.tex
, 0, 0);
972 // ////////////////////////////////////////////////////////////////////////// //
973 // returns time slept
974 int sleepAtMaxMsecs (int msecs
) {
976 import core
.sys
.posix
.signal
: timespec
;
977 import core
.sys
.posix
.time
: nanosleep
;
978 timespec ts
= void, tpassed
= void;
980 ts
.tv_nsec
= msecs
*1000*1000+(500*1000); // milli to nano
981 nanosleep(&ts
, &tpassed
);
982 return (ts
.tv_nsec
-tpassed
.tv_nsec
)/(1000*1000);
989 // ////////////////////////////////////////////////////////////////////////// //
990 mixin(import("editor.d"));
993 // ////////////////////////////////////////////////////////////////////////// //
995 shared int diedie
= 0;
997 enum D2DFrameTime
= 55; // milliseconds
998 enum MinFrameTime
= 1000/60; // ~60 FPS
1000 public void renderThread (Tid starterTid
) {
1001 enum BoolOptVarMsgMixin(string varname
) =
1002 "if (msg.toggle) msg.value = !"~varname
~";\n"~
1003 "if ("~varname
~" != msg.value) "~varname
~" = msg.value; else msg.showMessage = false;\n";
1005 send(starterTid
, 42);
1007 MonoTime curtime
= MonoTime
.currTime
;
1009 lastthink
= curtime
; // for interpolator
1010 nextthink
= curtime
+dur
!"msecs"(D2DFrameTime
);
1011 MonoTime nextvframe
= curtime
;
1013 enum MaxFPSFrames
= 16;
1014 float frtimes
= 0.0f;
1017 int hushFrames
= 6; // ignore first `hushFrames` frames overtime
1018 MonoTime prevFrameStartTime
= curtime
;
1020 bool vframeWasLost
= false;
1022 void resetFrameTimers () {
1023 MonoTime curtime
= MonoTime
.currTime
;
1024 lastthink
= curtime
; // for interpolator
1025 nextthink
= curtime
+dur
!"msecs"(D2DFrameTime
);
1026 nextvframe
= curtime
;
1029 void loadNewLevel (string name
) {
1032 conwriteln("ERROR: can't load new levels yet");
1036 if (name
.length
== 0) {
1037 conwriteln("ERROR: can't load empty level!");
1039 conwriteln("loading map '", name
, "'");
1044 void receiveMessages () {
1046 import core
.time
: Duration
;
1047 //conwriteln("rendering thread: waiting for messages...");
1048 auto got
= receiveTimeout(
1049 Duration
.zero
, // don't wait
1051 addMessage(msg
.text
[0..msg
.textlen
], msg
.pauseMsecs
, msg
.noreplace
);
1053 (TMsgLoadLevel msg
) {
1054 nextmapname
= null; // clear "exit" flag
1055 loadNewLevel(msg
.mapfile
[0..msg
.textlen
].idup
);
1057 (TMsgSkipLevel msg
) {
1058 string mn
= genNextMapName(0);
1060 nextmapname
= null; // clear "exit" flag
1064 (TMsgIntOption msg
) {
1065 final switch (msg
.type
) with (TMsgIntOption
.Type
) {
1067 if (msg
.value
>= 1 && msg
.value
<= 2) scale
= msg
.value
;
1071 (TMsgBoolOption msg
) {
1072 final switch (msg
.type
) with (TMsgBoolOption
.Type
) {
1073 case Interpolation
: mixin(BoolOptVarMsgMixin
!"frameInterpolation"); break;
1074 case Lighting
: mixin(BoolOptVarMsgMixin
!"doLighting"); break;
1075 case Pause
: mixin(BoolOptVarMsgMixin
!"gamePaused"); break;
1077 if (msg
.toggle
) msg
.value
= !inEditMode
;
1078 if (inEditMode
!= msg
.value
) {
1079 inEditMode
= msg
.value
;
1080 if (msg
.value
) sdwindow
.hideCursor(); else sdwindow
.showCursor();
1082 msg
.showMessage
= false;
1085 mixin(BoolOptVarMsgMixin
!"inEditMode"); break;
1086 case CheatNoDoors
: mixin(BoolOptVarMsgMixin
!"cheatNoDoors"); break;
1087 case CheatNoWallClip
: mixin(BoolOptVarMsgMixin
!"cheatNoWallClip"); break;
1089 if (msg
.showMessage
) {
1092 void putStr(T
) (T s
) if (is(T
: const(char)[])) {
1093 foreach (char ch
; s
) {
1094 if (msgpos
>= mbuf
.length
) break;
1095 mbuf
.ptr
[msgpos
++] = ch
;
1098 putStr("Option \"");
1099 { import std
.conv
: to
; putStr(to
!string(msg
.type
)); }
1101 if (msg
.value
) putStr("N"); else putStr("FF");
1102 addMessage(mbuf
[0..msgpos
]);
1105 (TMsgTestLightMove msg
) {
1108 testLightMoved
= true;
1110 (TMsgMouseEvent msg
) { editorMouseEvent(msg
); },
1111 (TMsgKeyEvent msg
) { editorKeyEvent(msg
); },
1113 conwriteln("WARNING: unknown thread message received and ignored");
1118 //conwriteln("rendering thread: no more messages");
1122 if (nextmapname
.length
) {
1123 string mn
= nextmapname
;
1124 nextmapname
= null; // clear "exit" flag
1129 // "D2D frames"; curtime should be set; return `true` if frame was processed; will fix `curtime`
1130 bool doThinkFrame () {
1131 if (curtime
>= nextthink
) {
1132 lastthink
= curtime
;
1133 while (nextthink
<= curtime
) nextthink
+= dur
!"msecs"(D2DFrameTime
);
1135 // save snapshot and other data for interpolator
1136 Actor
.saveSnapshot(prevFrameActorsData
[], prevFrameActorOfs
.ptr
);
1137 if (!gamePaused
&& !inEditMode
) {
1144 auto tm
= MonoTime
.currTime
;
1145 int thinkTime
= cast(int)((tm
-curtime
).total
!"msecs");
1146 if (thinkTime
> 9) { import core
.stdc
.stdio
; printf("spent on thinking: %d msecs\n", thinkTime
); }
1154 // "video frames"; curtime should be set; return `true` if frame was processed; will fix `curtime`
1156 version(dont_use_vsync
) {
1158 enum doCheckTime
= true;
1161 __gshared
bool prevLost
= false;
1162 bool doCheckTime
= vframeWasLost
;
1163 if (vframeWasLost
) {
1165 { import core
.stdc
.stdio
; printf("frame was lost!\n"); }
1173 if (curtime
< nextvframe
) return false;
1174 version(dont_use_vsync
) {
1175 if (curtime
> nextvframe
) {
1176 auto overtime
= cast(int)((curtime
-nextvframe
).total
!"msecs");
1177 if (overtime
> 2500) {
1181 { import core
.stdc
.stdio
; printf(" spent whole %d msecs\n", overtime
); }
1187 while (nextvframe
<= curtime
) nextvframe
+= dur
!"msecs"(MinFrameTime
);
1191 scope(exit
) sdwindow
.mtUnlock();
1192 ctset
= sdwindow
.setAsCurrentOpenGlContextNT
;
1194 // if we can't set context, pretend that videoframe was processed; this should solve problem with vsync and invisible window
1197 iLiquidTime
= cast(float)((curtime
-MonoTime
.zero
).total
!"msecs"%10000000)/18.0f*0.04f;
1198 receiveMessages(); // here, 'cause we need active OpenGL context for some messages
1200 renderScene(curtime
);
1202 //renderLoading(curtime);
1205 scope(exit
) sdwindow
.mtUnlock();
1206 sdwindow
.swapOpenGlBuffers();
1208 sdwindow
.releaseCurrentOpenGlContext();
1209 vframeWasLost
= false;
1211 vframeWasLost
= true;
1212 { import core
.stdc
.stdio
; printf("xframe was lost!\n"); }
1214 curtime
= MonoTime
.currTime
;
1219 if (sdwindow
.closed
) break;
1220 if (atomicLoad(diedie
) > 0) break;
1222 curtime
= MonoTime
.currTime
;
1223 auto fstime
= curtime
;
1225 doThinkFrame(); // this will fix curtime if necessary
1227 if (!vframeWasLost
) {
1229 auto frameTime
= cast(float)(curtime
-prevFrameStartTime
).total
!"msecs"/1000.0f;
1230 prevFrameStartTime
= curtime
;
1231 frtimes
+= frameTime
;
1232 if (++framenum
>= MaxFPSFrames || frtimes
>= 3.0f) {
1233 import std
.string
: format
;
1234 int newFPS
= cast(int)(cast(float)MaxFPSFrames
/frtimes
+0.5);
1235 if (newFPS
!= prevFPS
) {
1236 sdwindow
.title
= "%s / FPS:%s".format("D2D", newFPS
);
1245 curtime
= MonoTime
.currTime
;
1247 // now sleep until next "video" or "think" frame
1248 if (nextthink
> curtime
&& nextvframe
> curtime
) {
1250 immutable nextVideoFrameSleep
= cast(int)((nextvframe
-curtime
).total
!"msecs");
1251 immutable nextThinkFrameSleep
= cast(int)((nextthink
-curtime
).total
!"msecs");
1252 immutable sleepTime
= (nextVideoFrameSleep
< nextThinkFrameSleep ? nextVideoFrameSleep
: nextThinkFrameSleep
);
1253 sleepAtMaxMsecs(sleepTime
);
1254 //curtime = MonoTime.currTime;
1257 } catch (Throwable e
) {
1258 // here, we are dead and fucked (the exact order doesn't matter)
1259 import core
.stdc
.stdlib
: abort
;
1260 import core
.stdc
.stdio
: fprintf
, stderr
;
1261 import core
.memory
: GC
;
1262 GC
.disable(); // yeah
1263 thread_suspendAll(); // stop right here, you criminal scum!
1264 auto s
= e
.toString();
1265 fprintf(stderr
, "\n=== FATAL ===\n%.*s\n", cast(uint)s
.length
, s
.ptr
);
1266 abort(); // die, you bitch!
1268 import core.stdc.stdio;
1269 fprintf(stderr, "FUUUUUUUUUUUUUUUUUUUUUUUUUU\n");
1270 import std.stdio : stderr;
1273 if (sdwindow.closed) break;
1274 if (atomicLoad(diedie) > 0) break;
1275 sleepAtMaxMsecs(100);
1279 atomicStore(diedie
, 2);
1283 // ////////////////////////////////////////////////////////////////////////// //
1284 __gshared Tid renderTid
;
1285 shared bool renderThreadStarted
= false;
1288 public void startRenderThread () {
1289 if (!cas(&renderThreadStarted
, false, true)) {
1290 assert(0, "render thread already started!");
1292 renderTid
= spawn(&renderThread
, thisTid
);
1293 setMaxMailboxSize(renderTid
, 1024, OnCrowding
.throwException
); //FIXME
1294 // wait for "i'm ready" signal
1297 if (ok
!= 42) assert(0, "wtf?!");
1300 conwriteln("rendering thread started");
1304 // ////////////////////////////////////////////////////////////////////////// //
1305 public void closeWindow () {
1306 if (atomicLoad(diedie
) != 2) {
1307 atomicStore(diedie
, 1);
1308 while (atomicLoad(diedie
) != 2) {}
1310 if (!sdwindow
.closed
) {
1317 // ////////////////////////////////////////////////////////////////////////// //
1321 // ////////////////////////////////////////////////////////////////////////// //
1322 struct TMsgMouseEvent
{
1323 MouseEventType type
;
1326 MouseButton button
; /// See $(LREF MouseButton)
1327 int modifierState
; /// See $(LREF ModifierState)
1330 public void postMouseEvent() (in auto ref MouseEvent evt
) {
1331 if (!atomicLoad(renderThreadStarted
)) return;
1333 msg
.type
= evt
.type
;
1338 msg
.button
= evt
.button
;
1339 msg
.modifierState
= evt
.modifierState
;
1340 send(renderTid
, msg
);
1344 // ////////////////////////////////////////////////////////////////////////// //
1345 struct TMsgKeyEvent
{
1353 public void postKeyEvent() (in auto ref KeyEvent evt
) {
1354 if (!atomicLoad(renderThreadStarted
)) return;
1357 msg
.pressed
= evt
.pressed
;
1358 msg
.character
= evt
.character
;
1359 msg
.modifierState
= evt
.modifierState
;
1360 send(renderTid
, msg
);
1364 // ////////////////////////////////////////////////////////////////////////// //
1365 struct TMsgTestLightMove
{
1369 public void postTestLightMove (int x
, int y
) {
1370 if (!atomicLoad(renderThreadStarted
)) return;
1371 auto msg
= TMsgTestLightMove(x
, y
);
1372 send(renderTid
, msg
);
1376 // ////////////////////////////////////////////////////////////////////////// //
1377 struct TMsgMessage
{
1384 public void postAddMessage (const(char)[] msgtext
, int pauseMsecs
=3000, bool noreplace
=false) {
1385 if (!atomicLoad(renderThreadStarted
)) return;
1386 if (msgtext
.length
> TMsgMessage
.text
.length
) msgtext
= msgtext
[0..TMsgMessage
.text
.length
];
1388 msg
.textlen
= cast(uint)msgtext
.length
;
1389 if (msg
.textlen
) msg
.text
[0..msg
.textlen
] = msgtext
[0..msg
.textlen
];
1390 msg
.pauseMsecs
= pauseMsecs
;
1391 msg
.noreplace
= noreplace
;
1392 send(renderTid
, msg
);
1396 // ////////////////////////////////////////////////////////////////////////// //
1397 struct TMsgLoadLevel
{
1402 public void postLoadLevel (const(char)[] mapfile
) {
1403 if (!atomicLoad(renderThreadStarted
)) return;
1404 if (mapfile
.length
> TMsgLoadLevel
.mapfile
.length
) mapfile
= mapfile
[0..TMsgLoadLevel
.mapfile
.length
];
1406 msg
.textlen
= cast(uint)mapfile
.length
;
1407 if (msg
.textlen
) msg
.mapfile
[0..msg
.textlen
] = mapfile
[0..msg
.textlen
];
1408 send(renderTid
, msg
);
1412 // ////////////////////////////////////////////////////////////////////////// //
1413 struct TMsgSkipLevel
{
1416 public void postSkipLevel () {
1417 if (!atomicLoad(renderThreadStarted
)) return;
1419 send(renderTid
, msg
);
1423 // ////////////////////////////////////////////////////////////////////////// //
1424 struct TMsgIntOption
{
1434 struct TMsgBoolOption
{
1450 bool strCaseEqu (const(char)[] s0
, const(char)[] s1
) {
1451 if (s0
.length
!= s1
.length
) return false;
1452 foreach (immutable idx
, char ch
; s0
) {
1453 if (ch
>= 'A' && ch
<= 'Z') ch
+= 32;
1454 char c1
= s1
.ptr
[idx
];
1455 if (c1
>= 'A' && c1
<= 'Z') c1
+= 32;
1456 if (ch
!= c1
) return false;
1462 public bool postToggleOption (const(char)[] name
, bool showMessage
=false) { /*pragma(inline, true);*/ return postSetOption(name
, true, showMessage
:showMessage
, toggle
:true); }
1464 public bool postSetOption(T
) (const(char)[] name
, T value
, bool showMessage
=false, bool toggle
=false) if ((is(T
== int) ||
is(T
== bool))) {
1465 if (!atomicLoad(renderThreadStarted
)) return false;
1466 if (name
.length
== 0 || name
.length
> 127) return false;
1467 static if (is(T
== int)) {
1469 foreach (string oname
; __traits(allMembers
, TMsgIntOption
.Type
)) {
1470 if (strCaseEqu(oname
, name
)) {
1471 msg
.type
= __traits(getMember
, TMsgIntOption
.Type
, oname
);
1473 msg
.showMessage
= showMessage
;
1474 send(renderTid
, msg
);
1478 } else static if (is(T
== bool)) {
1480 foreach (string oname
; __traits(allMembers
, TMsgBoolOption
.Type
)) {
1481 if (strCaseEqu(oname
, name
)) {
1482 msg
.type
= __traits(getMember
, TMsgBoolOption
.Type
, oname
);
1484 msg
.toggle
= toggle
;
1485 msg
.showMessage
= showMessage
;
1486 send(renderTid
, msg
);
1491 static assert(0, "invalid option type '"~T
.stringof
~"'");