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 xmain_d2d
is aliced
;
20 //version = dont_use_vsync;
49 // ////////////////////////////////////////////////////////////////////////// //
54 // ////////////////////////////////////////////////////////////////////////// //
55 public __gshared
bool cheatNoDoors
= false;
58 // ////////////////////////////////////////////////////////////////////////// //
59 __gshared SimpleWindow sdwindow
;
62 public enum vlWidth
= 800;
63 public enum vlHeight
= 800;
64 public __gshared
int scale
= 1;
67 // ////////////////////////////////////////////////////////////////////////// //
68 __gshared
bool scanlines
= false;
69 __gshared
bool doLighting
= true;
72 // ////////////////////////////////////////////////////////////////////////// //
74 __gshared
ubyte[] prevFrameActorsData
;
75 __gshared
uint[65536] prevFrameActorOfs
; // uint.max-1: dead; uint.max: last
76 __gshared MonoTime lastthink
= MonoTime
.zero
; // for interpolator
77 __gshared MonoTime nextthink
= MonoTime
.zero
;
78 __gshared
bool frameInterpolation
= true;
81 __gshared
int[2] mapViewPosX
, mapViewPosY
; // [0]: previous frame -- for interpolator
84 // this should be screen center
85 public void setMapViewPos (int x
, int y
) {
91 int swdt
= vlWidth
/scale
;
92 int shgt
= vlHeight
/scale
;
95 if (x
< 0) x
= 0; else if (x
>= map
.width
*8-swdt
) x
= map
.width
*8-swdt
-1;
96 if (y
< 0) y
= 0; else if (y
>= map
.height
*8-shgt
) y
= map
.height
*8-shgt
-1;
97 mapViewPosX
[1] = x
*scale
;
98 mapViewPosY
[1] = y
*scale
;
102 // ////////////////////////////////////////////////////////////////////////// //
104 struct AttachedLightInfo
{
111 __gshared AttachedLightInfo
[65536] attachedLights
;
112 __gshared
uint attachedLightCount
= 0;
115 // ////////////////////////////////////////////////////////////////////////// //
116 enum MaxLightRadius
= 512;
119 // ////////////////////////////////////////////////////////////////////////// //
121 __gshared FBO
[MaxLightRadius
+1] fboOccluders
, fboShadowMap
;
122 __gshared Shader shadToPolar
, shadBlur
, shadBlurOcc
;
124 __gshared FBO fboLevel
, fboLevelLight
, fboOrigBack
;
125 __gshared Shader shadScanlines
;
126 __gshared Shader shadLiquidDistort
;
129 // ////////////////////////////////////////////////////////////////////////// //
131 glEnable(GL_TEXTURE_2D
);
132 glDisable(GL_LIGHTING
);
133 glDisable(GL_DITHER
);
135 glDisable(GL_DEPTH_TEST
);
138 shadScanlines
= new Shader("scanlines", loadTextFile("data/shaders/srscanlines.frag"));
140 shadLiquidDistort
= new Shader("liquid_distort", loadTextFile("data/shaders/srliquid_distort.frag"));
141 shadLiquidDistort
.exec((Shader shad
) { shad
["tex0"] = 0; });
144 shadToPolar
= new Shader("light_topolar", loadTextFile("data/shaders/srlight_topolar.frag"));
146 shadBlur
= new Shader("light_blur", loadTextFile("data/shaders/srlight_blur.frag"));
147 shadBlur
.exec((Shader shad
) {
153 shadBlurOcc
= new Shader("light_blur_occ", loadTextFile("data/shaders/srlight_blur_occ.frag"));
154 shadBlurOcc
.exec((Shader shad
) {
161 foreach (int sz
; 2..MaxLightRadius
+1) {
162 fboOccluders
[sz
] = new FBO(sz
*2, sz
*2, Texture
.Option
.Clamp
, Texture
.Option
.Linear
); // create occluders FBO
163 fboShadowMap
[sz
] = new FBO(sz
*2, 1, Texture
.Option
.Clamp
); // create 1d shadowmap FBO
167 glMatrixMode(GL_MODELVIEW
);
173 fboLevel
= new FBO(map
.width
*8, map
.height
*8, Texture
.Option
.Nearest
); // final level render will be here
174 fboLevelLight
= new FBO(map
.width
*8, map
.height
*8, Texture
.Option
.Nearest
); // level lights will be rendered here
175 //fboForeground = new FBO(map.width*8, map.height*8, Texture.Option.Nearest); // level foreground
176 fboOrigBack
= new FBO(map
.width
*8, map
.height
*8, Texture
.Option
.Nearest
); // background+foreground
178 shadBlur
.exec((Shader shad
) { shad
["mapPixSize"] = SVec2F(map
.width
*8, map
.height
*8); });
179 shadBlurOcc
.exec((Shader shad
) { shad
["mapPixSize"] = SVec2F(map
.width
*8, map
.height
*8); });
181 glActiveTexture(GL_TEXTURE0
+0);
182 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
183 orthoCamera(vlWidth
, vlHeight
);
187 Actor
.resetStorage();
192 // save first snapshot
193 prevFrameActorsData
= new ubyte[](Actor
.actorSize
*65536); // ~15-20 megabytes
194 prevFrameActorOfs
[] = uint.max
; // just for fun
195 Actor
.saveSnapshot(prevFrameActorsData
[], prevFrameActorOfs
.ptr
);
196 mapViewPosX
[0] = mapViewPosX
[1];
197 mapViewPosY
[0] = mapViewPosY
[1];
199 { import core
.memory
: GC
; GC
.collect(); }
203 // ////////////////////////////////////////////////////////////////////////// //
205 __gshared
uint mapTilesChanged
= 0;
208 public void mapDirty (uint layermask
) { mapTilesChanged |
= layermask
; }
210 void rebuildMapMegaTextures () {
212 //mapTilesChanged = false;
213 //map.clearMegaTextures();
214 map
.oglBuildMega(mapTilesChanged
);
219 // ////////////////////////////////////////////////////////////////////////// //
220 __gshared
int vportX0
, vportY0
, vportX1
, vportY1
;
223 void renderLight() (int lightX
, int lightY
, in auto ref SVec4F lcol
, int lightRadius
) {
224 if (lightRadius
< 2) return;
225 if (lightRadius
> MaxLightRadius
) lightRadius
= MaxLightRadius
;
226 int lightSize
= lightRadius
*2;
227 // is this light visible?
228 if (lightX
<= -lightRadius || lightY
<= -lightRadius || lightX
-lightRadius
>= map
.width
*8 || lightY
-lightRadius
>= map
.height
*8) return;
230 // out of viewport -- do nothing
231 if (lightX
+lightRadius
< vportX0 || lightY
+lightRadius
< vportY0
) return;
232 if (lightX
-lightRadius
> vportX1 || lightY
-lightRadius
> vportY1
) return;
234 // draw shadow casters to fboOccludersId, light should be in the center
237 fboOccluders
[lightRadius
].exec({
238 //glDisable(GL_BLEND);
239 glColor3f(0.0f, 0.0f, 0.0f);
240 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
241 glClear(GL_COLOR_BUFFER_BIT
);
242 orthoCamera(lightSize
, lightSize
);
243 drawAtXY(map
.texgl
.ptr
[map
.LightMask
], lightRadius
-lightX
, lightRadius
-lightY
);
246 // build 1d shadow map to fboShadowMapId
247 fboShadowMap
[lightRadius
].exec({
248 shadToPolar
.exec((Shader shad
) {
249 //glDisable(GL_BLEND);
250 glColor3f(0.0f, 0.0f, 0.0f);
251 shad
["lightTexSize"] = SVec2F(lightSize
, lightSize
);
252 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
253 glClear(GL_COLOR_BUFFER_BIT
);
254 orthoCamera(lightSize
, 1);
255 drawAtXY(fboOccluders
[lightRadius
].tex
.tid
, 0, 0, lightSize
, 1);
259 // build light texture for blending
260 fboOccluders
[lightRadius
].exec({
261 // no need to clear it, shader will take care of that
262 //glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
263 //glClear(GL_COLOR_BUFFER_BIT);
264 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
265 //glDisable(GL_BLEND);
266 shadBlur
.exec((Shader shad
) {
267 shad
["lightTexSize"] = SVec2F(lightSize
, lightSize
);
268 shad
["lightColor"] = SVec4F(lcol
.x
, lcol
.y
, lcol
.z
, lcol
.w
);
269 shad
["lightPos"] = SVec2F(lightX
, lightY
);
270 orthoCamera(lightSize
, lightSize
);
271 drawAtXY(fboShadowMap
[lightRadius
].tex
.tid
, 0, 0, lightSize
, lightSize
);
275 // blend light texture
278 glBlendFunc(GL_SRC_ALPHA
, GL_ONE
);
279 orthoCamera(map
.width
*8, map
.height
*8);
280 drawAtXY(fboOccluders
[lightRadius
].tex
, lightX
-lightRadius
, lightY
-lightRadius
, mirrorY
:true);
281 // and blend it again, somewhat bigger, with the shader that will touch only occluders
283 shadBlurOcc
.exec((Shader shad
) {
284 shad
["lightTexSize"] = SVec2F(lightSize
, lightSize
);
285 shad
["lightColor"] = SVec4F(lcol
.x
, lcol
.y
, lcol
.z
, lcol
.w
);
286 shad
["lightPos"] = SVec2F(lightX
, lightY
);
287 drawAtXY(fboOccluders
[lightRadius
].tex
.tid
, lightX
-lightRadius
-szmore
, lightY
-lightRadius
-szmore
, lightSize
+szmore
*2, lightSize
+szmore
*2, mirrorY
:true);
293 // ////////////////////////////////////////////////////////////////////////// //
296 enum Phase
{ FadeIn
, Stay
, FadeOut
}
305 private import core
.sync
.mutex
: Mutex
;
307 __gshared Message
[128] messages
;
308 __gshared
uint messagesUsed
= 0;
309 __gshared Mutex messageLock
;
311 shared static this () { messageLock
= new Mutex(); }
314 public void addMessage (const(char)[] msgtext
, int pauseMsecs
=3000, bool noreplace
=false) {
316 scope(exit
) messageLock
.unlock();
317 if (msgtext
.length
== 0) return;
319 if (pauseMsecs
<= 50) return;
320 if (messagesUsed
== messages
.length
) {
321 // remove top message
322 foreach (immutable cidx
; 1..messagesUsed
) messages
.ptr
[cidx
-1] = messages
.ptr
[cidx
];
323 messages
.ptr
[0].alpha
= 255;
327 if (!noreplace
&& messagesUsed
== 1) {
328 switch (messages
.ptr
[0].phase
) {
329 case Message
.Phase
.FadeIn
:
330 messages
.ptr
[0].phase
= Message
.Phase
.FadeOut
;
332 case Message
.Phase
.Stay
:
333 messages
.ptr
[0].phase
= Message
.Phase
.FadeOut
;
334 messages
.ptr
[0].alpha
= 255;
339 auto msg
= messages
.ptr
+messagesUsed
;
341 msg
.phase
= Message
.Phase
.FadeIn
;
343 msg
.pauseMsecs
= pauseMsecs
;
345 if (msgtext
.length
> msg
.text
.length
) {
346 msg
.text
= msgtext
[0..msg
.text
.length
];
347 msg
.textlen
= msg
.text
.length
;
349 msg
.text
[0..msgtext
.length
] = msgtext
[];
350 msg
.textlen
= msgtext
.length
;
355 void doMessages (MonoTime curtime
) {
357 scope(exit
) messageLock
.unlock();
359 if (messagesUsed
== 0) return;
361 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
367 final switch (msg
.phase
) {
368 case Message
.Phase
.FadeIn
:
369 if ((msg
.alpha
+= 10) >= 255) {
370 msg
.phase
= Message
.Phase
.Stay
;
371 msg
.removeTime
= curtime
+dur
!"msecs"(msg
.pauseMsecs
);
372 goto case; // to stay
374 glColor4f(1.0f, 1.0f, 1.0f, msg
.alpha
/255.0f);
376 case Message
.Phase
.Stay
:
377 if (msg
.removeTime
<= curtime
) {
379 msg
.phase
= Message
.Phase
.FadeOut
;
380 goto case; // to fade
382 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
384 case Message
.Phase
.FadeOut
:
385 if ((msg
.alpha
-= 10) <= 0) {
386 if (--messagesUsed
== 0) return;
387 // remove this message
388 foreach (immutable cidx
; 1..messagesUsed
+1) messages
.ptr
[cidx
-1] = messages
.ptr
[cidx
];
391 glColor4f(1.0f, 1.0f, 1.0f, msg
.alpha
/255.0f);
395 smDrawText(10, 10, msg
.text
[0..msg
.textlen
]);
399 // ////////////////////////////////////////////////////////////////////////// //
400 mixin(Actor
.FieldGetMixin
!("classtype", StrId
)); // fget_classtype
401 mixin(Actor
.FieldGetMixin
!("classname", StrId
)); // fget_classname
402 mixin(Actor
.FieldGetMixin
!("x", int));
403 mixin(Actor
.FieldGetMixin
!("y", int));
404 mixin(Actor
.FieldGetMixin
!("flags", uint));
405 mixin(Actor
.FieldGetMixin
!("zAnimstate", StrId
));
406 mixin(Actor
.FieldGetMixin
!("zAnimidx", int));
407 mixin(Actor
.FieldGetMixin
!("dir", uint));
408 mixin(Actor
.FieldGetMixin
!("attLightXOfs", int));
409 mixin(Actor
.FieldGetMixin
!("attLightYOfs", int));
410 mixin(Actor
.FieldGetMixin
!("attLightRGBX", uint));
412 mixin(Actor
.FieldGetPtrMixin
!("classtype", StrId
)); // fget_classtype
413 mixin(Actor
.FieldGetPtrMixin
!("classname", StrId
)); // fget_classname
414 mixin(Actor
.FieldGetPtrMixin
!("x", int));
415 mixin(Actor
.FieldGetPtrMixin
!("y", int));
416 mixin(Actor
.FieldGetPtrMixin
!("flags", uint));
417 mixin(Actor
.FieldGetPtrMixin
!("zAnimstate", StrId
));
418 mixin(Actor
.FieldGetPtrMixin
!("zAnimidx", int));
419 mixin(Actor
.FieldGetPtrMixin
!("dir", uint));
420 mixin(Actor
.FieldGetPtrMixin
!("attLightXOfs", int));
421 mixin(Actor
.FieldGetPtrMixin
!("attLightYOfs", int));
422 mixin(Actor
.FieldGetPtrMixin
!("attLightRGBX", uint));
425 // ////////////////////////////////////////////////////////////////////////// //
426 __gshared
int lightX
= vlWidth
/2, lightY
= vlHeight
/2;
427 __gshared
int mapOfsX
, mapOfsY
;
428 __gshared
bool movement
= false;
429 __gshared
float iLiquidTime
= 0.0;
430 __gshared
bool altMove
= false;
433 void renderScene (MonoTime curtime
) {
434 //enum BackIntens = 0.05f;
435 enum BackIntens
= 0.0f;
437 float atob
= (curtime
> lastthink ?
cast(float)((curtime
-lastthink
).total
!"msecs")/cast(float)((nextthink
-lastthink
).total
!"msecs") : 1.0f);
438 //{ import core.stdc.stdio; printf("atob=%f\n", cast(double)atob); }
441 int framelen = cast(int)((nextthink-lastthink).total!"msecs");
442 int curfp = cast(int)((curtime-lastthink).total!"msecs");
443 { import core.stdc.stdio; printf("framelen=%d; curfp=%d\n", framelen, curfp); }
449 if (altMove || movement || scale
== 1) {
454 vportX1
= map
.width
*8;
455 vportY1
= map
.height
*8;
457 if (frameInterpolation
) {
458 import core
.stdc
.math
: roundf
;
459 mofsx
= cast(int)(mapViewPosX
[0]+roundf((mapViewPosX
[1]-mapViewPosX
[0])*atob
));
460 mofsy
= cast(int)(mapViewPosY
[0]+roundf((mapViewPosY
[1]-mapViewPosY
[0])*atob
));
462 mofsx
= mapViewPosX
[1];
463 mofsy
= mapViewPosY
[1];
465 vportX0
= mofsx
/scale
;
466 vportY0
= mofsy
/scale
;
467 vportX1
= vportX0
+vlWidth
/scale
;
468 vportY1
= vportY0
+vlHeight
/scale
;
474 if (mapTilesChanged
!= 0) rebuildMapMegaTextures();
478 // build background layer
480 //glDisable(GL_BLEND);
481 glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
482 glClear(GL_COLOR_BUFFER_BIT
);
483 glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
484 orthoCamera(map
.width
*8, map
.height
*8);
486 glBlendFunc(GL_SRC_ALPHA
, GL_ONE_MINUS_SRC_ALPHA
);
489 drawAtXY(map.skytexgl.tid, mapOfsX/2-map.MapSize*8, mapOfsY/2-map.MapSize*8, map.MapSize*8, map.MapSize*8);
490 drawAtXY(map.skytexgl.tid, mapOfsX/2-map.MapSize*8, mapOfsY/2, map.MapSize*8, map.MapSize*8);
491 drawAtXY(map.skytexgl.tid, mapOfsX/2, mapOfsY/2-map.MapSize*8, map.MapSize*8, map.MapSize*8);
492 drawAtXY(map.skytexgl.tid, mapOfsX/2, mapOfsY/2, map.MapSize*8, map.MapSize*8);
494 drawAtXY(map
.skytexgl
.tid
, 0, 0, map
.MapSize
*8, map
.MapSize
*8);
496 drawAtXY(map
.texgl
.ptr
[map
.Back
], 0, 0);
497 // draw distorted liquid areas
498 shadLiquidDistort
.exec((Shader shad
) {
499 shad
["iDistortTime"] = iLiquidTime
;
500 drawAtXY(map
.texgl
.ptr
[map
.AllLiquids
], 0, 0);
502 // monsters, items; we'll do linear interpolation here
503 // we need this for saved frame anyway, so let's play dirty and speed up the things
504 glColor3f(1.0f, 1.0f, 1.0f);
505 attachedLightCount
= 0;
506 Actor
.forEach((ActorId me
) {
507 if (auto adef
= findActorDef(me
)) {
508 int actorX
, actorY
; // current actor position
510 auto ofs
= prevFrameActorOfs
.ptr
[me
.id
&0xffff];
511 if (frameInterpolation
&& ofs
< uint.max
-1 && Actor
.isSameSavedActor(me
.id
, prevFrameActorsData
.ptr
, ofs
)) {
512 import core
.stdc
.math
: roundf
;
513 auto xptr
= prevFrameActorsData
.ptr
+ofs
;
514 int ox
= xptr
.fgetp_x
;
516 int oy
= xptr
.fgetp_y
;
518 actorX
= cast(int)(ox
+roundf((nx
-ox
)*atob
));
519 actorY
= cast(int)(oy
+roundf((ny
-oy
)*atob
));
520 //conwriteln("actor ", me.id, "; o=(", ox, ",", oy, "); n=(", nx, ",", ny, "); p=(", x, ",", y, ")");
527 if ((me
.fget_flags
&AF_NODRAW
) == 0) {
528 if (auto isp
= adef
.animSpr(me
.fget_zAnimstate
, me
.fget_dir
, me
.fget_zAnimidx
)) {
529 drawAtXY(isp
.tex
, actorX
-isp
.vga
.sx
, actorY
-isp
.vga
.sy
);
531 //conwriteln("no animation for actor ", me.id, " (", me.classtype!string, ":", me.classname!string, ")");
534 // process attached lights
535 if ((me
.fget_flags
&AF_NOLIGHT
) == 0) {
536 uint alr
= me
.fget_attLightRGBX
;
537 if ((alr
&0xff) >= 4) {
539 auto li
= attachedLights
.ptr
+attachedLightCount
;
540 ++attachedLightCount
;
541 li
.x
= actorX
+me
.fget_attLightXOfs
;
542 li
.y
= actorY
+me
.fget_attLightYOfs
;
543 li
.r
= ((alr
>>24)&0xff)/255.0f; // red or intensity
544 if ((alr
&0x00_ff_ff
_00U) == 0x00_00_01_00U) {
547 li
.g
= ((alr
>>16)&0xff)/255.0f;
548 li
.b
= ((alr
>>8)&0xff)/255.0f;
549 li
.uncolored
= false;
551 li
.radius
= (alr
&0xff);
552 if (li
.radius
> MaxLightRadius
) li
.radius
= MaxLightRadius
;
556 conwriteln("not found actor ", me
.id
, " (", me
.classtype
!string
, ":", me
.classname
!string
, ")");
560 drawAtXY(texParts
, 0, 0);
561 // do liquid coloring
562 drawAtXY(map
.texgl
.ptr
[map
.LiquidMask
], 0, 0);
563 // foreground -- hide secrets, draw lifts and such
564 drawAtXY(map
.texgl
.ptr
[map
.Front
], 0, 0);
572 glClearColor(BackIntens
, BackIntens
, BackIntens
, 1.0f);
573 //glClearColor(0.15f, 0.15f, 0.15f, 1.0f);
574 ////glColor4f(1.0f, 1.0f, 1.0f, 0.0f);
575 glClear(GL_COLOR_BUFFER_BIT
);
578 // texture 1 is background
579 glActiveTexture(GL_TEXTURE0
+1);
580 glBindTexture(GL_TEXTURE_2D
, fboOrigBack
.tex
.tid
);
581 // texture 2 is occluders
582 glActiveTexture(GL_TEXTURE0
+2);
583 glBindTexture(GL_TEXTURE_2D
, map
.texgl
.ptr
[map
.LightMask
].tid
);
584 // done texture assign
585 glActiveTexture(GL_TEXTURE0
+0);
589 renderLight( 27, 391-0+LYOfs
, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 100);
590 renderLight(542, 424-0+LYOfs
, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 100);
591 renderLight(377, 368-0+LYOfs
, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 32);
592 renderLight(147, 288-0+LYOfs
, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 64);
593 renderLight( 71, 200-0+LYOfs
, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 128);
594 renderLight(249, 200-0+LYOfs
, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 128);
595 renderLight(426, 200-0+LYOfs
, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 128);
596 renderLight(624, 200-0+LYOfs
, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 128);
597 renderLight(549, 298-0+LYOfs
, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 64);
598 renderLight( 74, 304-0+LYOfs
, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 32);
600 renderLight(24*8+4, (24+18)*8-2+LYOfs
, SVec4F(0.6f, 0.0f, 0.0f, 1.0f), 128);
602 renderLight(280, 330, SVec4F(0.0f, 0.0f, 0.0f, 1.0f), 64);
605 foreach (ref li
; attachedLights
[0..attachedLightCount
]) {
607 renderLight(li
.x
, li
.y
, SVec4F(0.0f, 0.0f, 0.0f, li
.r
), li
.radius
);
609 renderLight(li
.x
, li
.y
, SVec4F(li
.r
, li
.g
, li
.b
, 1.0f), li
.radius
);
613 foreach (immutable _
; 0..1) {
614 renderLight(lightX
, lightY
, SVec4F(0.3f, 0.3f, 0.0f, 1.0f), 96);
617 glActiveTexture(GL_TEXTURE0
+1);
618 glBindTexture(GL_TEXTURE_2D
, 0);
619 glActiveTexture(GL_TEXTURE0
+2);
620 glBindTexture(GL_TEXTURE_2D
, 0);
621 glActiveTexture(GL_TEXTURE0
+0);
626 shadScanlines.exec((Shader shad) {
627 shad["scanlines"] = scanlines;
628 glClearColor(BackIntens, BackIntens, BackIntens, 1.0f);
629 glClear(GL_COLOR_BUFFER_BIT);
630 orthoCamera(vlWidth, vlHeight);
631 //orthoCamera(map.width*8*scale, map.height*8*scale);
632 //glMatrixMode(GL_MODELVIEW);
634 //glTranslatef(0.375, 0.375, 0); // to be pixel-perfect
635 // somehow, FBO objects are mirrored; wtf?!
636 drawAtXY(fboLevel.tex.tid, -mapOfsX, -mapOfsY, map.width*8*scale, map.height*8*scale, mirrorY:true);
643 smDrawText(map.width*8/2, map.height*8/2, "Testing...");
648 glBindFramebufferEXT(GL_FRAMEBUFFER_EXT
, 0);
653 //auto img = smfont.ptr[0x39];
656 //conwriteln("img.sx=", img.sx, "; img.sy=", img.sy, "; ", img.width, "x", img.height);
657 drawAtXY(img.tex, 10-img.sx, 10-img.sy);
662 orthoCamera(vlWidth
, vlHeight
);
663 auto tex
= (doLighting ? fboLevelLight
.tex
.tid
: fboOrigBack
.tex
.tid
);
664 drawAtXY(tex
, -mofsx
, -mofsy
, map
.width
*8*scale
, map
.height
*8*scale
, mirrorY
:true);
670 // ////////////////////////////////////////////////////////////////////////// //
671 // returns time slept
672 int sleepAtMaxMsecs (int msecs
) {
674 import core
.sys
.posix
.signal
: timespec
;
675 import core
.sys
.posix
.time
: nanosleep
;
676 timespec ts
= void, tpassed
= void;
678 ts
.tv_nsec
= msecs
*1000*1000+(500*1000); // milli to nano
679 nanosleep(&ts
, &tpassed
);
680 return (ts
.tv_nsec
-tpassed
.tv_nsec
)/(1000*1000);
687 // ////////////////////////////////////////////////////////////////////////// //
689 shared int diedie
= 0;
691 enum D2DFrameTime
= 55; // milliseconds
692 enum MinFrameTime
= 1000/60; // ~60 FPS
694 void renderThread () {
696 MonoTime curtime
= MonoTime
.currTime
;
698 lastthink
= curtime
; // for interpolator
699 nextthink
= curtime
+dur
!"msecs"(D2DFrameTime
);
700 MonoTime nextvframe
= curtime
;
702 enum MaxFPSFrames
= 16;
703 float frtimes
= 0.0f;
706 int hushFrames
= 6; // ignore first `hushFrames` frames overtime
707 MonoTime prevFrameStartTime
= curtime
;
709 bool vframeWasLost
= false;
711 // "D2D frames"; curtime should be set; return `true` if frame was processed; will fix `curtime`
712 bool doThinkFrame () {
713 if (curtime
>= nextthink
) {
715 while (nextthink
<= curtime
) nextthink
+= dur
!"msecs"(D2DFrameTime
);
716 // save snapshot and other data for interpolator
717 Actor
.saveSnapshot(prevFrameActorsData
[], prevFrameActorOfs
.ptr
);
718 mapViewPosX
[0] = mapViewPosX
[1];
719 mapViewPosY
[0] = mapViewPosY
[1];
724 auto tm
= MonoTime
.currTime
;
725 int thinkTime
= cast(int)((tm
-curtime
).total
!"msecs");
726 if (thinkTime
> 9) { import core
.stdc
.stdio
; printf("spent on thinking: %d msecs\n", thinkTime
); }
734 // "video frames"; curtime should be set; return `true` if frame was processed; will fix `curtime`
736 version(dont_use_vsync
) {
738 enum doCheckTime
= true;
741 __gshared
bool prevLost
= false;
742 bool doCheckTime
= vframeWasLost
;
745 { import core
.stdc
.stdio
; printf("frame was lost!\n"); }
753 if (curtime
< nextvframe
) return false;
754 version(dont_use_vsync
) {
755 if (curtime
> nextvframe
) {
756 auto overtime
= cast(int)((curtime
-nextvframe
).total
!"msecs");
757 if (overtime
> 2500) {
761 { import core
.stdc
.stdio
; printf(" spent whole %d msecs\n", overtime
); }
767 while (nextvframe
<= curtime
) nextvframe
+= dur
!"msecs"(MinFrameTime
);
771 scope(exit
) sdwindow
.mtUnlock();
772 ctset
= sdwindow
.setAsCurrentOpenGlContextNT
;
774 // if we can't set context, pretend that videoframe was processed; this should solve problem with vsync and invisible window
777 iLiquidTime
= cast(float)((curtime
-MonoTime
.zero
).total
!"msecs"%10000000)/18.0f*0.04f;
778 renderScene(curtime
);
780 scope(exit
) sdwindow
.mtUnlock();
781 sdwindow
.swapOpenGlBuffers();
783 sdwindow
.releaseCurrentOpenGlContext();
784 vframeWasLost
= false;
786 vframeWasLost
= true;
787 { import core
.stdc
.stdio
; printf("xframe was lost!\n"); }
789 curtime
= MonoTime
.currTime
;
794 if (sdwindow
.closed
) break;
795 if (atomicLoad(diedie
) > 0) break;
797 curtime
= MonoTime
.currTime
;
798 auto fstime
= curtime
;
800 doThinkFrame(); // this will fix curtime if necessary
802 if (!vframeWasLost
) {
804 auto frameTime
= cast(float)(curtime
-prevFrameStartTime
).total
!"msecs"/1000.0f;
805 prevFrameStartTime
= curtime
;
806 frtimes
+= frameTime
;
807 if (++framenum
>= MaxFPSFrames || frtimes
>= 3.0f) {
808 import std
.string
: format
;
809 int newFPS
= cast(int)(cast(float)MaxFPSFrames
/frtimes
+0.5);
810 if (newFPS
!= prevFPS
) {
811 sdwindow
.title
= "%s / FPS:%s".format("D2D", newFPS
);
820 curtime
= MonoTime
.currTime
;
822 // now sleep until next "video" or "think" frame
823 if (nextthink
> curtime
&& nextvframe
> curtime
) {
825 immutable nextVideoFrameSleep
= cast(int)((nextvframe
-curtime
).total
!"msecs");
826 immutable nextThinkFrameSleep
= cast(int)((nextthink
-curtime
).total
!"msecs");
827 immutable sleepTime
= (nextVideoFrameSleep
< nextThinkFrameSleep ? nextVideoFrameSleep
: nextThinkFrameSleep
);
828 sleepAtMaxMsecs(sleepTime
);
829 //curtime = MonoTime.currTime;
832 } catch (Exception e
) {
833 import core
.stdc
.stdio
;
834 fprintf(stderr
, "FUUUUUUUUUUUUUUUUUUUUUUUUUU\n");
836 if (sdwindow
.closed
) break;
837 if (atomicLoad(diedie
) > 0) break;
838 sleepAtMaxMsecs(100);
841 atomicStore(diedie
, 2);
845 // ////////////////////////////////////////////////////////////////////////// //
846 void closeWindow () {
847 if (atomicLoad(diedie
) != 2) {
848 atomicStore(diedie
, 1);
849 while (atomicLoad(diedie
) != 2) {}
851 if (!sdwindow
.closed
) {
858 // ////////////////////////////////////////////////////////////////////////// //
859 __gshared Thread renderTid
;
862 void main (string
[] args
) {
863 FuncPool
.dumpCode
= false;
864 FuncPool
.dumpCodeSize
= false;
865 dacsDumpSemantic
= false;
867 //version(rdmd) { dacsOptimize = 0; }
868 bool compileOnly
= false;
870 for (usize idx
= 1; idx
< args
.length
; ++idx
) {
872 if (args
[idx
] == "--dump-code") FuncPool
.dumpCode
= true;
873 else if (args
[idx
] == "--dump-code-size") FuncPool
.dumpCodeSize
= true;
874 else if (args
[idx
] == "--dump-semantic") dacsDumpSemantic
= true;
875 else if (args
[idx
] == "--dump-all") { FuncPool
.dumpCode
= true; FuncPool
.dumpCodeSize
= true; dacsDumpSemantic
= true; }
876 else if (args
[idx
] == "--compile") compileOnly
= true;
877 else if (args
[idx
] == "--compile-only") compileOnly
= true;
878 else if (args
[idx
] == "--messages") ++dacsMessages
;
879 else if (args
[idx
] == "--no-copro") dacsOptimizeNoCoPro
= true;
880 else if (args
[idx
] == "--no-deadass") dacsOptimizeNoDeadAss
= true;
881 else if (args
[idx
] == "--no-purekill") dacsOptimizeNoPureKill
= true;
882 else if (args
[idx
].length
> 2 && args
[idx
][0..2] == "-O") {
883 import std
.conv
: to
;
884 ubyte olevel
= to
!ubyte(args
[idx
][2..$]);
885 dacsOptimize
= olevel
;
889 foreach (immutable c
; idx
+1..args
.length
) args
.ptr
[c
-1] = args
.ptr
[c
];
895 static void setDP () {
899 import std
.file
: thisExePath
;
900 import std
.path
: dirName
;
901 setDataPath(thisExePath
.dirName
);
903 addPK3(getDataPath
~"base.pk3"); loadWadScripts();
904 //addWad("/home/ketmar/k8prj/doom2d-tl/data/doom2d.wad"); loadWadScripts();
905 //addWad("/home/ketmar/k8prj/doom2d-tl/data/meat.wad"); loadWadScripts();
906 //addWad("/home/ketmar/k8prj/doom2d-tl/data/megadm.wad"); loadWadScripts();
907 //addWad("/home/ketmar/k8prj/doom2d-tl/data/megadm1.wad"); loadWadScripts();
908 //addWad("/home/ketmar/k8prj/doom2d-tl/data/superdm.wad"); loadWadScripts();
909 //addWad("/home/ketmar/k8prj/doom2d-tl/data/zadoomka.wad"); loadWadScripts();
910 scriptLoadingComplete();
915 if (compileOnly
) return;
921 setOpenGLContextVersion(3, 2); // up to GLSL 150
922 //openGLContextCompatible = false;
924 map
= new LevelMap("maps/map01.d2m");
925 ugInit(map
.width
*8, map
.height
*8);
931 map
.getThingPos(1/*ThingId.Player1*/, &mapOfsX
, &mapOfsY
);
934 mapOfsX = (mapOfsX*2)-vlWidth/2;
935 if (mapOfsX+vlWidth > map.width*16) mapOfsX = map.width*16-vlWidth;
936 if (mapOfsX < 0) mapOfsX = 0;
937 mapOfsY = (mapOfsY*2)-vlHeight/2;
938 if (mapOfsY+vlHeight > map.height*16) mapOfsY = map.height*16-vlHeight;
939 if (mapOfsY < 0) mapOfsY = 0;
941 setMapViewPos(mapOfsX
, mapOfsY
);
942 mapOfsX
= mapViewPosX
[1];
943 mapOfsY
= mapViewPosY
[1];
945 sdwindow
= new SimpleWindow(vlWidth
, vlHeight
, "D2D", OpenGlOptions
.yes
, Resizablity
.fixedSize
);
947 sdwindow
.visibleForTheFirstTime
= delegate () {
948 sdwindow
.setAsCurrentOpenGlContext(); // make this window active
949 glbindLoadFunctions();
952 import core.stdc.stdio;
953 printf("GL version: %s\n", glGetString(GL_VERSION));
955 glGetIntegerv(GL_MAJOR_VERSION, &h);
956 glGetIntegerv(GL_MINOR_VERSION, &l);
957 printf("version: %d.%d\n", h, l);
958 printf("shader version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
960 glGetIntegerv(GL_NUM_SHADING_LANGUAGE_VERSIONS, &svcount);
962 printf("%d shader versions supported:\n", svcount);
963 foreach (GLuint n; 0..svcount) printf(" %d: %s\n", n, glGetStringi(GL_SHADING_LANGUAGE_VERSION, n));
967 glGetIntegerv(GL_NUM_EXTENSIONS, &ecount);
969 printf("%d extensions supported:\n", ecount);
970 foreach (GLuint n; 0..ecount) printf(" %d: %s\n", n, glGetStringi(GL_EXTENSIONS, n));
975 // check if we have sufficient shader version here
979 glGetIntegerv(GL_NUM_SHADING_LANGUAGE_VERSIONS
, &svcount
);
981 foreach (GLuint n
; 0..svcount
) {
982 import core
.stdc
.string
: strncmp
;
983 auto v
= glGetStringi(GL_SHADING_LANGUAGE_VERSION
, n
);
984 if (v
is null) continue;
985 if (strncmp(v
, "120", 3) != 0) continue;
986 if (v
[3] > ' ') continue;
991 if (!found
) assert(0, "can't find OpenGL GLSL 120");
993 auto adr
= glGetProcAddress("glTexParameterf");
994 if (adr
is null) assert(0);
997 version(dont_use_vsync
) {
998 sdwindow
.vsync
= false;
1000 sdwindow
.vsync
= true;
1002 //sdwindow.useGLFinish = false;
1004 //sdwindow.redrawOpenGlScene();
1005 if (!sdwindow
.releaseCurrentOpenGlContext()) { import core
.stdc
.stdio
; printf("can't release OpenGL context(1)\n"); }
1007 renderTid
= new Thread(&renderThread
);
1012 //sdwindow.redrawOpenGlScene = delegate () { renderScene(); };
1014 enum MSecsPerFrame
= 1000/30; /* 30 is FPS */
1016 uint[8] frameTimes
= 1000;
1017 enum { Left
, Right
, Up
, Down
}
1018 bool[4] pressed
= false;
1020 sdwindow
.eventLoop(MSecsPerFrame
,
1022 if (sdwindow
.closed
) return;
1023 if (pressed
[Left
]) mapOfsX
-= 8;
1024 if (pressed
[Right
]) mapOfsX
+= 8;
1025 if (pressed
[Up
]) mapOfsY
-= 8;
1026 if (pressed
[Down
]) mapOfsY
+= 8;
1027 import std
.math
: cos
, sin
;
1028 __gshared
float itime
= 0.0;
1031 mapOfsX
= cast(int)(800.0/2.0+cos(itime
)*220.0);
1032 mapOfsY
= cast(int)(800.0/2.0+120.0+sin(itime
)*160.0);
1034 if (scale
== 1) mapOfsX
= mapOfsY
= 0;
1035 //sdwindow.redrawOpenGlSceneNow();
1037 delegate (KeyEvent event
) {
1038 if (sdwindow
.closed
) return;
1039 if (event
.pressed
&& event
.key
== Key
.Escape
) { closeWindow(); return; }
1040 switch (event
.key
) {
1041 case Key
.X
: if (event
.pressed
) altMove
= !altMove
; break;
1042 case Key
.Left
: if (altMove
) pressed
[Left
] = event
.pressed
; break;
1043 case Key
.Right
: if (altMove
) pressed
[Right
] = event
.pressed
; break;
1044 case Key
.Up
: if (altMove
) pressed
[Up
] = event
.pressed
; break;
1045 case Key
.Down
: if (altMove
) pressed
[Down
] = event
.pressed
; break;
1049 switch (event
.key
) {
1050 case Key
.Left
: case Key
.Pad4
: plrKeyUpDown(0, PLK_LEFT
, event
.pressed
); break;
1051 case Key
.Right
: case Key
.Pad6
: plrKeyUpDown(0, PLK_RIGHT
, event
.pressed
); break;
1052 case Key
.Up
: case Key
.Pad8
: plrKeyUpDown(0, PLK_UP
, event
.pressed
); break;
1053 case Key
.Down
: case Key
.Pad2
: plrKeyUpDown(0, PLK_DOWN
, event
.pressed
); break;
1054 case Key
.Alt
: plrKeyUpDown(0, PLK_JUMP
, event
.pressed
); break;
1055 case Key
.Ctrl
: plrKeyUpDown(0, PLK_FIRE
, event
.pressed
); break;
1056 case Key
.Shift
: plrKeyUpDown(0, PLK_USE
, event
.pressed
); break;
1061 delegate (MouseEvent event
) {
1062 lightX
= event
.x
/scale
;
1063 lightY
= event
.y
/scale
;
1064 lightX
+= mapOfsX
/scale
;
1065 lightY
+= mapOfsY
/scale
;
1067 delegate (dchar ch
) {
1068 if (ch
== 'q') { closeWindow(); return; }
1069 if (ch
== 's') scanlines
= !scanlines
;
1070 if (ch
== '1') scale
= 1;
1071 if (ch
== '2') scale
= 2;
1072 if (ch
== 'D') cheatNoDoors
= !cheatNoDoors
;
1074 frameInterpolation
= !frameInterpolation
;
1075 if (frameInterpolation
) addMessage("Interpolation: ON"); else addMessage("Interpolation: OFF");
1078 doLighting
= !doLighting
;
1079 if (doLighting
) addMessage("Lighting: ON"); else addMessage("Lighting: OFF");
1081 if (ch
== ' ') movement
= !movement
;
1084 } catch (Exception e
) {
1085 import std
.stdio
: stderr
;
1086 stderr
.writeln("FUUUUUUUUUUUU\n", e
.toString
);