"r_vsync" console var; slightly faster light tracer
[dd2d.git] / xmain_d2d.d
blob80d77cfd3a79bc4b091aada36bc9f82de1b0b08d
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 private:
21 import core.atomic;
22 import core.thread;
23 import core.time;
25 import std.concurrency;
27 import iv.glbinds;
28 import glutils;
29 import console;
30 import wadarc;
32 import iv.vfs.augs;
34 import d2dmap;
35 import d2dadefs;
36 import d2dimage;
37 import d2dfont;
38 import dacs;
40 import d2dunigrid;
42 // `map` is there
43 import dengapi;
45 import d2dparts;
47 import render;
50 // ////////////////////////////////////////////////////////////////////////// //
51 import arsd.color;
52 import arsd.png;
55 // ////////////////////////////////////////////////////////////////////////// //
56 void main (string[] args) {
57 FuncPool.dumpCode = false;
58 FuncPool.dumpCodeSize = false;
59 dacsDumpSemantic = false;
60 dacsOptimize = 9;
61 //version(rdmd) { dacsOptimize = 0; }
62 bool compileOnly = false;
63 bool doVBL = true;
65 for (usize idx = 1; idx < args.length; ++idx) {
66 bool remove = true;
67 if (args[idx].length < 1) continue;
68 if (args[idx] == "--dump-code") FuncPool.dumpCode = true;
69 else if (args[idx] == "--dump-code-size") FuncPool.dumpCodeSize = true;
70 else if (args[idx] == "--dump-semantic") dacsDumpSemantic = true;
71 else if (args[idx] == "--dump-all") { FuncPool.dumpCode = true; FuncPool.dumpCodeSize = true; dacsDumpSemantic = true; }
72 else if (args[idx] == "--compile") compileOnly = true;
73 else if (args[idx] == "--compile-only") compileOnly = true;
74 else if (args[idx] == "--messages") ++dacsMessages;
75 else if (args[idx] == "--no-copro") dacsOptimizeNoCoPro = true;
76 else if (args[idx] == "--no-deadass") dacsOptimizeNoDeadAss = true;
77 else if (args[idx] == "--no-purekill") dacsOptimizeNoPureKill = true;
78 else if (args[idx] == "--no-vsync") doVBL = false;
79 else if (args[idx] == "--vsync") doVBL = true;
80 else if (args[idx].length > 2 && args[idx][0..2] == "-O") {
81 import std.conv : to;
82 ubyte olevel = to!ubyte(args[idx][2..$]);
83 dacsOptimize = olevel;
84 } else if (args[idx][0] == '-') {
85 assert(0, "wut?!");
87 else remove = false;
88 if (remove) {
89 foreach (immutable c; idx+1..args.length) args.ptr[c-1] = args.ptr[c];
90 args.length -= 1;
91 --idx; //hack
94 if (processCL(args)) return;
96 addInternalActorFields();
98 static void setDP () {
99 version(rdmd) {
100 setDataPath("data");
101 } else {
102 import std.file : thisExePath;
103 import std.path : dirName;
104 setDataPath(thisExePath.dirName~"/data");
106 addPak(getDataPath~"base.pk3"); registerWadScripts();
107 //addWad("/home/ketmar/k8prj/doom2d-tl/data/doom2d.wad"); registerWadScripts();
108 //addWad("/home/ketmar/k8prj/doom2d-tl/data/meat.wad"); registerWadScripts();
109 //addWad("/home/ketmar/k8prj/doom2d-tl/data/megadm.wad"); registerWadScripts();
110 //addWad("/home/ketmar/k8prj/doom2d-tl/data/megadm1.wad"); registerWadScripts();
111 //addWad("/home/ketmar/k8prj/doom2d-tl/data/superdm.wad"); registerWadScripts();
112 //addWad("/home/ketmar/k8prj/doom2d-tl/data/zadoomka.wad"); registerWadScripts();
113 loadWadScripts();
116 setDP();
118 if (compileOnly) return;
120 try {
121 registerAPI();
122 loadD2DPalette();
124 setOpenGLContextVersion(3, 2); // up to GLSL 150
125 //openGLContextCompatible = false;
127 curmapname = "maps/map01.d2m";
128 //conwriteln(genNextMapName());
130 sdwindow = new SimpleWindow(vlWidth, vlHeight, "D2D", OpenGlOptions.yes, Resizablity.fixedSize);
131 //sdwindow.hideCursor();
133 sdwindow.closeQuery = delegate () { concmd("quit"); };
135 sdwindow.visibleForTheFirstTime = delegate () {
136 sdwindow.setAsCurrentOpenGlContext(); // make this window active
137 glbindLoadFunctions();
140 import core.stdc.stdio;
141 printf("GL version: %s\n", glGetString(GL_VERSION));
142 GLint l, h;
143 glGetIntegerv(GL_MAJOR_VERSION, &h);
144 glGetIntegerv(GL_MINOR_VERSION, &l);
145 printf("version: %d.%d\n", h, l);
146 printf("shader version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
147 GLint svcount;
148 glGetIntegerv(GL_NUM_SHADING_LANGUAGE_VERSIONS, &svcount);
149 if (svcount > 0) {
150 printf("%d shader versions supported:\n", svcount);
151 foreach (GLuint n; 0..svcount) printf(" %d: %s\n", n, glGetStringi(GL_SHADING_LANGUAGE_VERSION, n));
154 GLint ecount;
155 glGetIntegerv(GL_NUM_EXTENSIONS, &ecount);
156 if (ecount > 0) {
157 printf("%d extensions supported:\n", ecount);
158 foreach (GLuint n; 0..ecount) printf(" %d: %s\n", n, glGetStringi(GL_EXTENSIONS, n));
163 // check if we have sufficient shader version here
165 bool found = false;
166 GLint svcount;
167 glGetIntegerv(GL_NUM_SHADING_LANGUAGE_VERSIONS, &svcount);
168 if (svcount > 0) {
169 foreach (GLuint n; 0..svcount) {
170 import core.stdc.string : strncmp;
171 auto v = glGetStringi(GL_SHADING_LANGUAGE_VERSION, n);
172 if (v is null) continue;
173 if (strncmp(v, "120", 3) != 0) continue;
174 if (v[3] > ' ') continue;
175 found = true;
176 break;
179 if (!found) assert(0, "can't find OpenGL GLSL 120");
181 auto adr = glGetProcAddress("glTexParameterf");
182 if (adr is null) assert(0);
185 sdwindow.vsync = false;
186 //sdwindow.useGLFinish = false;
187 initOpenGL();
188 if (!sdwindow.releaseCurrentOpenGlContext()) { import core.stdc.stdio; printf("can't release OpenGL context(1)\n"); }
189 startRenderThread();
190 convar("r_vsync", doVBL);
191 concmd("exec config.rc");
192 concmd("exec autoexec.rc");
193 concmd("map '"~curmapname~"'");
194 //convar("r_console", true);
197 enum { Left, Right, Up, Down }
198 bool[4] pressed = false;
199 bool testLightLocked = false;
200 bool justConsoled = false;
202 sdwindow.eventLoop(5000,
203 delegate () {
204 if (sdwindow.closed) return;
206 delegate (KeyEvent event) {
207 if (sdwindow.closed) return;
208 if (event.pressed) justConsoled = false;
209 if (!conVisible && inEditMode) { postKeyEvent(event); return; }
210 if (event.pressed && event.key == Key.Escape) {
211 //if (convar!bool("r_console")) conwriteln("CONSOLE IS HERE"); else conwriteln("NO CONSOLE");
212 if (conVisible) concmd("r_console toggle"); else concmd("quit");
213 return;
215 if (!conVisible) {
216 switch (event.key) {
217 case Key.Left: case Key.Pad4: plrKeyUpDown(0, PLK_LEFT, event.pressed); break;
218 case Key.Right: case Key.Pad6: plrKeyUpDown(0, PLK_RIGHT, event.pressed); break;
219 case Key.Up: case Key.Pad8: plrKeyUpDown(0, PLK_UP, event.pressed); break;
220 case Key.Down: case Key.Pad2: plrKeyUpDown(0, PLK_DOWN, event.pressed); break;
221 case Key.Alt: plrKeyUpDown(0, PLK_JUMP, event.pressed); break;
222 case Key.Ctrl: plrKeyUpDown(0, PLK_FIRE, event.pressed); break;
223 case Key.Shift: plrKeyUpDown(0, PLK_USE, event.pressed); break;
224 case Key.Grave: if (event.pressed) { justConsoled = !conVisible; concmd("r_console toggle"); } break;
225 default:
227 } else {
228 plrKeyUpDown(0, PLK_LEFT, false);
229 plrKeyUpDown(0, PLK_RIGHT, false);
230 plrKeyUpDown(0, PLK_UP, false);
231 plrKeyUpDown(0, PLK_DOWN, false);
232 plrKeyUpDown(0, PLK_JUMP, false);
233 plrKeyUpDown(0, PLK_FIRE, false);
234 plrKeyUpDown(0, PLK_USE, false);
235 if (event.pressed && event.key == Key.Up) postChar('\x01');
236 else if (event.pressed && event.key == Key.Down) postChar('\x02');
237 else if (event.pressed && event.key == Key.PageUp) postChar('\x03');
238 else if (event.pressed && event.key == Key.PageDown) postChar('\x04');
239 else postKeyEvent(event);
242 delegate (MouseEvent event) {
243 if (inEditMode) { postMouseEvent(event); return; }
244 if (!testLightLocked) postTestLightMove(event.x, event.y);
246 delegate (dchar ch) {
247 if (conVisible) {
248 if (justConsoled && ch == '`') ch = 0;
249 justConsoled = false;
250 if (ch && ch < 128) postChar(cast(char)ch);
251 return;
253 if (inEditMode) return;
254 if (ch == 'q') concmd("quit");
255 if (ch == '1') concmd("r_scale 1");
256 if (ch == '2') concmd("r_scale 2");
257 if (ch == 'D') concmd("nodoorclip");
258 if (ch == 'i') concmd("r_interpolation toggle; hudmsg \"Interpolation: $r_interpolation\"");
259 if (ch == 'l') concmd("r_lighting toggle");
260 if (ch == 'W') concmd("nowallclip");
261 if (ch == 'L') testLightLocked = !testLightLocked;
262 if (ch == 'p') concmd("g_pause toggle");
263 if (ch == '!') concmd("skiplevel");
264 if (ch == 'e') concmd("ed_toggle");
267 } catch (Exception e) {
268 import std.stdio : stderr;
269 stderr.writeln("FUUUUUUUUUUUU\n", e.toString);
271 flushGui();