cosmetix
[raymarch.git] / gfxcore.d
blob0474d1ac08949280c102c81606fb84aeed20d21b
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.drawText(Point(10, 10), aftstr);
47 public void mainLoop () {
48 texImage = new Image(vlEffectiveWidth, vlEffectiveHeight);
49 mtrenderInit();
51 sdwindow = new SimpleWindow(vlEffectiveWidth, vlEffectiveHeight, "RayMarching demo", OpenGlOptions.no, Resizablity.fixedSize);
52 blitScene();
54 enum MSecsPerFrame = 1000/60; /* 60 is FPS */
56 uint[8] frameTimes = 1000;
58 sdwindow.eventLoop(MSecsPerFrame,
59 delegate () {
60 if (sdwindow.closed) return;
61 if (mtrenderComplete) {
62 // do average frame time
63 uint aft = 0;
64 foreach (immutable idx; 1..frameTimes.length) {
65 aft += frameTimes.ptr[idx];
66 frameTimes.ptr[idx-1] = frameTimes.ptr[idx];
68 frameTimes[$-1] = mtrenderFrameTime;
69 aft += frameTimes[$-1];
70 aft /= cast(uint)frameTimes.length;
71 if (aft == 0) aft = 1000;
73 import std.string;
74 aftstr = "%s FPS".format(1000/aft);
76 //version(show_frame_time) { import core.stdc.stdio; printf("%u msecs per frame\n", frameDur); }
77 blitScene();
78 mtrenderStartSignal();
81 delegate (KeyEvent event) {
82 if (sdwindow.closed) return;
83 if (event.key == Key.Escape) {
84 flushGui();
85 sdwindow.close();
88 delegate (MouseEvent event) {
90 delegate (dchar ch) {
93 mtrenderShutdown();
94 if (!sdwindow.closed) {
95 flushGui();
96 sdwindow.close();
98 flushGui();