rendering thread moved to it's own module; no more gshared hacks, all communications...
[dd2d.git] / xmain_d2d.d
blob02726a015e631cd7e0b48a3caa760c68e6d7c1ac
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.stream;
34 import d2dmap;
35 import d2dadefs;
36 //import d2dactors;
37 import d2dgfx;
38 import d2dfont;
39 import dacs;
41 import d2dunigrid;
43 // `map` is there
44 import dengapi;
46 import d2dparts;
48 import render;
51 // ////////////////////////////////////////////////////////////////////////// //
52 import arsd.color;
53 import arsd.png;
56 // ////////////////////////////////////////////////////////////////////////// //
57 void main (string[] args) {
58 FuncPool.dumpCode = false;
59 FuncPool.dumpCodeSize = false;
60 dacsDumpSemantic = false;
61 dacsOptimize = 9;
62 //version(rdmd) { dacsOptimize = 0; }
63 bool compileOnly = false;
64 bool doVBL = true;
66 for (usize idx = 1; idx < args.length; ++idx) {
67 bool remove = true;
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;
85 else remove = false;
86 if (remove) {
87 foreach (immutable c; idx+1..args.length) args.ptr[c-1] = args.ptr[c];
88 args.length -= 1;
89 --idx; //hack
93 addInternalActorFields();
95 static void setDP () {
96 version(rdmd) {
97 setDataPath("data");
98 } else {
99 import std.file : thisExePath;
100 import std.path : dirName;
101 setDataPath(thisExePath.dirName);
103 addPK3(getDataPath~"base.pk3"); loadWadScripts();
104 //addWad("/home/ketmar/k8prj/doom2d-tl/data/doom2d.wad"); loadWadScripts();
105 //addWad("/home/ketmar/k8prj/doom2d-tl/data/meat.wad"); loadWadScripts();
106 //addWad("/home/ketmar/k8prj/doom2d-tl/data/megadm.wad"); loadWadScripts();
107 //addWad("/home/ketmar/k8prj/doom2d-tl/data/megadm1.wad"); loadWadScripts();
108 //addWad("/home/ketmar/k8prj/doom2d-tl/data/superdm.wad"); loadWadScripts();
109 //addWad("/home/ketmar/k8prj/doom2d-tl/data/zadoomka.wad"); loadWadScripts();
110 scriptLoadingComplete();
113 setDP();
115 if (compileOnly) return;
117 try {
118 registerAPI();
119 loadPalette();
121 setOpenGLContextVersion(3, 2); // up to GLSL 150
122 //openGLContextCompatible = false;
124 //map = new LevelMap("maps/map01.d2m");
125 //ugInit(map.width*8, map.height*8);
127 //scale = 2;
130 //mapOfsX = 8*26;
131 //mapOfsY = 8*56;
132 map.getThingPos(1/*ThingId.Player1*/, &mapOfsX, &mapOfsY);
133 // fix viewport
135 mapOfsX = (mapOfsX*2)-vlWidth/2;
136 if (mapOfsX+vlWidth > map.width*16) mapOfsX = map.width*16-vlWidth;
137 if (mapOfsX < 0) mapOfsX = 0;
138 mapOfsY = (mapOfsY*2)-vlHeight/2;
139 if (mapOfsY+vlHeight > map.height*16) mapOfsY = map.height*16-vlHeight;
140 if (mapOfsY < 0) mapOfsY = 0;
142 setMapViewPos(mapOfsX, mapOfsY);
143 mapOfsX = mapViewPosX[1];
144 mapOfsY = mapViewPosY[1];
147 sdwindow = new SimpleWindow(vlWidth, vlHeight, "D2D", OpenGlOptions.yes, Resizablity.fixedSize);
149 sdwindow.visibleForTheFirstTime = delegate () {
150 sdwindow.setAsCurrentOpenGlContext(); // make this window active
151 glbindLoadFunctions();
154 import core.stdc.stdio;
155 printf("GL version: %s\n", glGetString(GL_VERSION));
156 GLint l, h;
157 glGetIntegerv(GL_MAJOR_VERSION, &h);
158 glGetIntegerv(GL_MINOR_VERSION, &l);
159 printf("version: %d.%d\n", h, l);
160 printf("shader version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
161 GLint svcount;
162 glGetIntegerv(GL_NUM_SHADING_LANGUAGE_VERSIONS, &svcount);
163 if (svcount > 0) {
164 printf("%d shader versions supported:\n", svcount);
165 foreach (GLuint n; 0..svcount) printf(" %d: %s\n", n, glGetStringi(GL_SHADING_LANGUAGE_VERSION, n));
168 GLint ecount;
169 glGetIntegerv(GL_NUM_EXTENSIONS, &ecount);
170 if (ecount > 0) {
171 printf("%d extensions supported:\n", ecount);
172 foreach (GLuint n; 0..ecount) printf(" %d: %s\n", n, glGetStringi(GL_EXTENSIONS, n));
177 // check if we have sufficient shader version here
179 bool found = false;
180 GLint svcount;
181 glGetIntegerv(GL_NUM_SHADING_LANGUAGE_VERSIONS, &svcount);
182 if (svcount > 0) {
183 foreach (GLuint n; 0..svcount) {
184 import core.stdc.string : strncmp;
185 auto v = glGetStringi(GL_SHADING_LANGUAGE_VERSION, n);
186 if (v is null) continue;
187 if (strncmp(v, "120", 3) != 0) continue;
188 if (v[3] > ' ') continue;
189 found = true;
190 break;
193 if (!found) assert(0, "can't find OpenGL GLSL 120");
195 auto adr = glGetProcAddress("glTexParameterf");
196 if (adr is null) assert(0);
199 version(dont_use_vsync) {
200 sdwindow.vsync = false;
201 } else {
202 sdwindow.vsync = true;
204 //sdwindow.useGLFinish = false;
205 initOpenGL();
206 //sdwindow.redrawOpenGlScene();
207 if (!sdwindow.releaseCurrentOpenGlContext()) { import core.stdc.stdio; printf("can't release OpenGL context(1)\n"); }
209 if (!renderTid) {
210 renderTid = new Thread(&renderThread);
211 renderTid.start();
214 startRenderThread();
215 postLoadLevel("maps/map01.d2m");
218 //sdwindow.redrawOpenGlScene = delegate () { renderScene(); };
220 enum MSecsPerFrame = 1000/30; /* 30 is FPS */
222 enum { Left, Right, Up, Down }
223 bool[4] pressed = false;
225 sdwindow.eventLoop(MSecsPerFrame,
226 delegate () {
227 if (sdwindow.closed) return;
229 if (pressed[Left]) mapOfsX -= 8;
230 if (pressed[Right]) mapOfsX += 8;
231 if (pressed[Up]) mapOfsY -= 8;
232 if (pressed[Down]) mapOfsY += 8;
233 import std.math : cos, sin;
234 __gshared float itime = 0.0;
235 itime += 0.02;
236 if (movement) {
237 mapOfsX = cast(int)(800.0/2.0+cos(itime)*220.0);
238 mapOfsY = cast(int)(800.0/2.0+120.0+sin(itime)*160.0);
240 if (scale == 1) mapOfsX = mapOfsY = 0;
242 //sdwindow.redrawOpenGlSceneNow();
244 delegate (KeyEvent event) {
245 if (sdwindow.closed) return;
246 if (event.pressed && event.key == Key.Escape) { closeWindow(); return; }
248 switch (event.key) {
249 case Key.X: if (event.pressed) altMove = !altMove; break;
250 case Key.Left: if (altMove) pressed[Left] = event.pressed; break;
251 case Key.Right: if (altMove) pressed[Right] = event.pressed; break;
252 case Key.Up: if (altMove) pressed[Up] = event.pressed; break;
253 case Key.Down: if (altMove) pressed[Down] = event.pressed; break;
254 default:
257 if (true/*!altMove*/) {
258 switch (event.key) {
259 case Key.Left: case Key.Pad4: plrKeyUpDown(0, PLK_LEFT, event.pressed); break;
260 case Key.Right: case Key.Pad6: plrKeyUpDown(0, PLK_RIGHT, event.pressed); break;
261 case Key.Up: case Key.Pad8: plrKeyUpDown(0, PLK_UP, event.pressed); break;
262 case Key.Down: case Key.Pad2: plrKeyUpDown(0, PLK_DOWN, event.pressed); break;
263 case Key.Alt: plrKeyUpDown(0, PLK_JUMP, event.pressed); break;
264 case Key.Ctrl: plrKeyUpDown(0, PLK_FIRE, event.pressed); break;
265 case Key.Shift: plrKeyUpDown(0, PLK_USE, event.pressed); break;
266 default:
270 delegate (MouseEvent event) {
272 testLightX = event.x/*/scale*/;
273 testLightY = event.y/*/scale*/;
274 //testLightX += mapOfsX/scale;
275 //testLightY += mapOfsY/scale;
277 postTestLightMove(event.x, event.y);
279 delegate (dchar ch) {
280 if (ch == 'q') { closeWindow(); return; }
281 //if (ch == 's') scanlines = !scanlines;
282 if (ch == '1') postSetOption("scale", 1);
283 if (ch == '2') postSetOption("scale", 2);
284 if (ch == 'D') postSetOption("cheatNoDoors", true, showMessage:true, toggle:true);
285 if (ch == 'i') postSetOption("interpolation", true, showMessage:true, toggle:true);
286 if (ch == 'l') postSetOption("lighting", true, showMessage:true, toggle:true);
287 //if (ch == ' ') movement = !movement;
290 } catch (Exception e) {
291 import std.stdio : stderr;
292 stderr.writeln("FUUUUUUUUUUUU\n", e.toString);
294 closeWindow();
295 flushGui();