adapted to new saxy
[xreader.git] / edraw.d
blob472cc026ca748a9fc99deadd17a6554645367c05
1 module edraw is aliced;
3 import core.time;
4 import std.stdio;
5 import iv.strex;
7 import arsd.simpledisplay;
8 import arsd.color;
9 import arsd.png;
10 import arsd.jpeg;
12 import iv.nanovg;
13 import iv.nanovg.oui.blendish;
14 //import iv.nanovg.svg;
15 import iv.nanovg.perf;
17 import iv.vfs;
19 import elite;
22 // ////////////////////////////////////////////////////////////////////////// //
23 __gshared string RcDir = "~/.xreader";
24 __gshared string stateFileName;
26 __gshared int rotx, rotz;
29 // ////////////////////////////////////////////////////////////////////////// //
30 struct Interference {
31 int y, ey, speed;
35 // ////////////////////////////////////////////////////////////////////////// //
36 void run () {
37 NVGContext vg = null;
38 PerfGraph fps;
39 bool fpsVisible = false;
41 int GWidth = 900;
42 int GHeight = 1024;
43 int fSize = 24;
44 bool sbLeft = true;
46 if (GWidth < 32) GWidth = 32;
47 if (GHeight < 32) GHeight = 32;
49 int textFont, uiFont;
51 void loadFonts () {
52 import std.path : expandTilde;
53 textFont = vg.createFont("text:noaa", "~/ttf/ms/arial.ttf".expandTilde);
54 uiFont = vg.createFont("ui:noaa", "~/ttf/ms/verdana.ttf".expandTilde);
55 bndSetFont(uiFont);
58 bool doQuit = false;
60 //setOpenGLContextVersion(3, 2); // up to GLSL 150
61 setOpenGLContextVersion(2, 0); // it's enough
62 //openGLContextCompatible = false;
64 auto sdwindow = new SimpleWindow(GWidth, GHeight, "Elite Demo", OpenGlOptions.yes, Resizablity.allowResizing);
65 //sdwindow.hideCursor();
67 auto stt = MonoTime.currTime;
68 auto prevt = MonoTime.currTime;
69 auto curt = prevt;
70 float dt = 0, secs = 0;
71 bool needRedraw = true;
72 MonoTime nextIfTime = MonoTime.currTime;
73 Interference[] ifs;
75 void processIfs () {
76 foreach (ref l; ifs) {
77 if (l.speed == 0) continue;
78 needRedraw = true;
79 l.y += l.speed;
80 if (l.speed < 0) {
81 if (l.y < l.ey) l.speed = 0;
82 } else {
83 if (l.y > l.ey) l.speed = 0;
88 void drawIfs () {
89 foreach (ref l; ifs) {
90 if (l.speed == 0) continue;
91 vg.beginPath();
92 vg.fillColor(nvgRGB(0, 40, 0));
93 vg.rect(0, l.y-1, GWidth, 3);
94 vg.fill();
98 void addIf () {
99 import std.random : uniform;
100 version(none) {
101 int idx = 0;
102 while (idx < ifs.length && ifs[idx].speed != 0) ++idx;
103 if (idx >= ifs.length) {
104 if (ifs.length > 10) return;
105 ifs ~= Interference();
107 if (uniform!"[]"(0, 1) == 0) {
108 // up
109 ifs[idx].y = GHeight/2+uniform!"[]"(50, GHeight/2-100);
110 int ty = ifs[idx].y-40;
111 if (ty < 0) return;
112 ifs[idx].ey = uniform!"[]"(0, ty);
113 ifs[idx].speed = -uniform!"[]"(1, 25);
114 } else {
115 // down
116 ifs[idx].y = GHeight/2-uniform!"[]"(50, GHeight/2-100);
117 int ty = ifs[idx].y+40;
118 if (ty >= GHeight) return;
119 ifs[idx].ey = uniform!"[]"(ty, GHeight);
120 ifs[idx].speed = uniform!"[]"(1, 25);
122 needRedraw = true;
126 sdwindow.closeQuery = delegate () { doQuit = true; };
128 void closeWindow () {
129 if (!sdwindow.closed && vg !is null) {
130 //vg.deleteImage(vgimg);
131 //vgimg = -1;
132 vg.deleteGL2();
133 vg = null;
134 sdwindow.close();
138 sdwindow.visibleForTheFirstTime = delegate () {
139 sdwindow.setAsCurrentOpenGlContext(); // make this window active
140 sdwindow.vsync = false;
141 //sdwindow.useGLFinish = false;
142 //glbindLoadFunctions();
144 vg = createGL2NVG(NVG_ANTIALIAS|NVG_STENCIL_STROKES|NVG_DEBUG);
145 if (vg is null) {
146 import std.stdio;
147 writeln("Could not init nanovg.");
148 //sdwindow.close();
150 loadFonts();
151 fps = new PerfGraph("Frame Time", PerfGraph.Style.FPS, "ui");
153 sdwindow.redrawOpenGlScene();
156 sdwindow.windowResized = delegate (int w, int h) {
157 //writeln("w=", w, "; h=", h);
158 glViewport(0, 0, w, h);
159 GWidth = w;
160 GHeight = h;
163 sdwindow.redrawOpenGlScene = delegate () {
164 // timers
165 prevt = curt;
166 curt = MonoTime.currTime;
167 secs = cast(double)((curt-stt).total!"msecs")/1000.0;
168 dt = cast(double)((curt-prevt).total!"msecs")/1000.0;
170 //glClearColor(0, 0, 0, 0);
171 glClearColor(0.2, 0.2, 0.2, 0);
172 glClear(glNVGClearFlags);
174 if (vg !is null) {
175 if (fps !is null) fps.update(dt);
176 vg.beginFrame(GWidth, GHeight, 1);
177 drawIfs();
179 drawElite(model, skin, rotx, rotz, 0, 0, GWidth, GHeight);
180 vg.blitElite();
182 if (fps !is null && fpsVisible) fps.render(vg, GWidth-fps.width-4, GHeight-fps.height-4);
183 vg.endFrame();
185 needRedraw = (fps !is null && fpsVisible);
188 sdwindow.eventLoop(1000/60,
189 delegate () {
190 if (sdwindow.closed) return;
191 if (doQuit) { closeWindow(); return; }
193 auto ct = MonoTime.currTime;
194 if (ct >= nextIfTime) {
195 import std.random : uniform;
196 if (uniform!"[]"(0, 100) >= 50) addIf();
197 nextIfTime += (uniform!"[]"(50, 1500)).msecs;
200 processIfs();
201 version(all) {
202 __gshared bool skip;
203 skip = !skip;
204 if (skip) rotx = (rotx+1)%120;
205 rotz = (rotz+1)%120;
206 needRedraw = true;
208 if (needRedraw) sdwindow.redrawOpenGlSceneNow();
210 delegate (KeyEvent event) {
211 if (sdwindow.closed) return;
212 if (!event.pressed) return;
213 switch (event.key) {
214 case Key.Escape: sdwindow.close(); break;
215 case Key.P: fpsVisible = !fpsVisible; needRedraw = true; break;
216 case Key.C: addIf(); break;
217 default:
220 delegate (MouseEvent event) {
222 delegate (dchar ch) {
223 if (ch == 'q') { doQuit = true; return; }
226 closeWindow();
230 // ////////////////////////////////////////////////////////////////////////// //
231 void main (string[] args) {
232 string shipName = "escpod";
233 if (args.length > 1) shipName = args[1];
234 //model = loadModel(VFile("data/"~shipName~".emd"));
235 //skin = loadSkin(VFile("data/"~shipName~".esk"));
236 loadShip(shipName);
237 writeModelText(VFile("zmdl_00.txt", "w"), model);
238 writeSkinText(VFile("zskn_00.txt", "w"), skin);
239 run();