koi moved to separate module
[dd2d.git] / xmain_d2d.d
blob3269566e2c2865c1fffdacdd12b5dc9919394636
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");
216 //postAddMessage("WRYYYYYYYYYY");
217 //postAddMessage("wryyyyyyyyyy");
220 //sdwindow.redrawOpenGlScene = delegate () { renderScene(); };
222 enum MSecsPerFrame = 1000/30; /* 30 is FPS */
224 enum { Left, Right, Up, Down }
225 bool[4] pressed = false;
226 bool testLightLocked = false;
228 sdwindow.eventLoop(MSecsPerFrame,
229 delegate () {
230 if (sdwindow.closed) return;
232 if (pressed[Left]) mapOfsX -= 8;
233 if (pressed[Right]) mapOfsX += 8;
234 if (pressed[Up]) mapOfsY -= 8;
235 if (pressed[Down]) mapOfsY += 8;
236 import std.math : cos, sin;
237 __gshared float itime = 0.0;
238 itime += 0.02;
239 if (movement) {
240 mapOfsX = cast(int)(800.0/2.0+cos(itime)*220.0);
241 mapOfsY = cast(int)(800.0/2.0+120.0+sin(itime)*160.0);
243 if (scale == 1) mapOfsX = mapOfsY = 0;
245 //sdwindow.redrawOpenGlSceneNow();
247 delegate (KeyEvent event) {
248 if (sdwindow.closed) return;
249 if (event.pressed && event.key == Key.Escape) { closeWindow(); return; }
251 switch (event.key) {
252 case Key.X: if (event.pressed) altMove = !altMove; break;
253 case Key.Left: if (altMove) pressed[Left] = event.pressed; break;
254 case Key.Right: if (altMove) pressed[Right] = event.pressed; break;
255 case Key.Up: if (altMove) pressed[Up] = event.pressed; break;
256 case Key.Down: if (altMove) pressed[Down] = event.pressed; break;
257 default:
260 if (true/*!altMove*/) {
261 switch (event.key) {
262 case Key.Left: case Key.Pad4: plrKeyUpDown(0, PLK_LEFT, event.pressed); break;
263 case Key.Right: case Key.Pad6: plrKeyUpDown(0, PLK_RIGHT, event.pressed); break;
264 case Key.Up: case Key.Pad8: plrKeyUpDown(0, PLK_UP, event.pressed); break;
265 case Key.Down: case Key.Pad2: plrKeyUpDown(0, PLK_DOWN, event.pressed); break;
266 case Key.Alt: plrKeyUpDown(0, PLK_JUMP, event.pressed); break;
267 case Key.Ctrl: plrKeyUpDown(0, PLK_FIRE, event.pressed); break;
268 case Key.Shift: plrKeyUpDown(0, PLK_USE, event.pressed); break;
269 default:
273 delegate (MouseEvent event) {
275 testLightX = event.x/*/scale*/;
276 testLightY = event.y/*/scale*/;
277 //testLightX += mapOfsX/scale;
278 //testLightY += mapOfsY/scale;
280 if (!testLightLocked) postTestLightMove(event.x, event.y);
282 delegate (dchar ch) {
283 if (ch == 'q') { closeWindow(); return; }
284 //if (ch == 's') scanlines = !scanlines;
285 if (ch == '1') postSetOption("scale", 1);
286 if (ch == '2') postSetOption("scale", 2);
287 if (ch == 'D') postSetOption("cheatNoDoors", true, showMessage:true, toggle:true);
288 if (ch == 'i') postSetOption("interpolation", true, showMessage:true, toggle:true);
289 if (ch == 'l') postSetOption("lighting", true, showMessage:true, toggle:true);
290 if (ch == 'L') testLightLocked = !testLightLocked;
291 //if (ch == ' ') movement = !movement;
294 } catch (Exception e) {
295 import std.stdio : stderr;
296 stderr.writeln("FUUUUUUUUUUUU\n", e.toString);
298 closeWindow();
299 flushGui();