i'm an idiot: let threads consume working queue!
[raymarch.git] / gfxcore.d
blob37d346a7027d61da1948538965a7a33d6488b973
1 // main driver
2 module gfxcore;
4 private:
5 import mtrender;
7 //version = show_frame_time;
10 // ////////////////////////////////////////////////////////////////////////// //
11 static if (__traits(compiles, () { import arsd.simpledisplay; })) {
12 import arsd.simpledisplay;
13 } else {
14 import simpledisplay;
16 import arsd.color;
18 __gshared SimpleWindow sdwindow;
19 public __gshared Image texImage;
21 shared static ~this () {
22 destroy(texImage);
25 public enum vlWidth = 160;
26 public enum vlHeight = 120;
27 public enum scale = 3;
28 public enum scanlines = false;
30 public enum vlEffectiveWidth = vlWidth*scale;
31 public enum vlEffectiveHeight = vlHeight*scale;
34 // ////////////////////////////////////////////////////////////////////////// //
35 __gshared string aftstr; // average frame time
37 void blitScene () {
38 if (sdwindow.closed) return;
39 auto painter = sdwindow.draw();
40 painter.drawImage(Point(0, 0), texImage);
42 //painter.outlineColor = Color.white;
43 painter.outlineColor = Color.black;
44 painter.drawText(Point(10, 10), aftstr);
48 public void mainLoop () {
49 texImage = new Image(vlEffectiveWidth, vlEffectiveHeight);
50 mtrenderInit();
52 sdwindow = new SimpleWindow(vlEffectiveWidth, vlEffectiveHeight, "RayMarching demo", OpenGlOptions.no, Resizablity.fixedSize);
53 blitScene();
55 enum MSecsPerFrame = 1000/60; /* 60 is FPS */
57 uint[8] frameTimes = 1000;
59 sdwindow.eventLoop(MSecsPerFrame,
60 delegate () {
61 if (sdwindow.closed) return;
62 if (mtrenderComplete) {
63 // do average frame time
64 uint aft = 0;
65 foreach (immutable idx; 1..frameTimes.length) {
66 aft += frameTimes.ptr[idx];
67 frameTimes.ptr[idx-1] = frameTimes.ptr[idx];
69 frameTimes[$-1] = mtrenderFrameTime;
70 aft += frameTimes[$-1];
71 aft /= cast(uint)frameTimes.length;
72 if (aft == 0) aft = 1000;
74 import std.string;
75 aftstr = "%s FPS".format(1000/aft);
77 //version(show_frame_time) { import core.stdc.stdio; printf("%u msecs per frame\n", frameDur); }
78 blitScene();
79 mtrenderStartSignal();
82 delegate (KeyEvent event) {
83 if (sdwindow.closed) return;
84 if (event.pressed && event.key == Key.Escape) {
85 flushGui();
86 sdwindow.close();
89 delegate (MouseEvent event) {
91 delegate (dchar ch) {
92 if (ch == ' ') mtrenderTogglePause();
95 mtrenderShutdown();
96 if (!sdwindow.closed) {
97 flushGui();
98 sdwindow.close();
100 flushGui();