console font made smaller and converted to koi8
[dd2d.git] / xmain_d2d.d
blob8ffff06668cec457e95eb6ceddc0e660da234aee
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] == "--dump-code") FuncPool.dumpCode = true;
68 else if (args[idx] == "--dump-code-size") FuncPool.dumpCodeSize = true;
69 else if (args[idx] == "--dump-semantic") dacsDumpSemantic = true;
70 else if (args[idx] == "--dump-all") { FuncPool.dumpCode = true; FuncPool.dumpCodeSize = true; dacsDumpSemantic = true; }
71 else if (args[idx] == "--compile") compileOnly = true;
72 else if (args[idx] == "--compile-only") compileOnly = true;
73 else if (args[idx] == "--messages") ++dacsMessages;
74 else if (args[idx] == "--no-copro") dacsOptimizeNoCoPro = true;
75 else if (args[idx] == "--no-deadass") dacsOptimizeNoDeadAss = true;
76 else if (args[idx] == "--no-purekill") dacsOptimizeNoPureKill = true;
77 else if (args[idx] == "--no-vsync") doVBL = false;
78 else if (args[idx] == "--vsync") doVBL = true;
79 else if (args[idx].length > 2 && args[idx][0..2] == "-O") {
80 import std.conv : to;
81 ubyte olevel = to!ubyte(args[idx][2..$]);
82 dacsOptimize = olevel;
84 else remove = false;
85 if (remove) {
86 foreach (immutable c; idx+1..args.length) args.ptr[c-1] = args.ptr[c];
87 args.length -= 1;
88 --idx; //hack
91 if (processCL(args)) return;
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~"/data");
103 addPak(getDataPath~"base.pk3"); registerWadScripts();
104 //addWad("/home/ketmar/k8prj/doom2d-tl/data/doom2d.wad"); registerWadScripts();
105 //addWad("/home/ketmar/k8prj/doom2d-tl/data/meat.wad"); registerWadScripts();
106 //addWad("/home/ketmar/k8prj/doom2d-tl/data/megadm.wad"); registerWadScripts();
107 //addWad("/home/ketmar/k8prj/doom2d-tl/data/megadm1.wad"); registerWadScripts();
108 //addWad("/home/ketmar/k8prj/doom2d-tl/data/superdm.wad"); registerWadScripts();
109 //addWad("/home/ketmar/k8prj/doom2d-tl/data/zadoomka.wad"); registerWadScripts();
110 loadWadScripts();
113 setDP();
115 if (compileOnly) return;
117 try {
118 registerAPI();
119 loadD2DPalette();
121 setOpenGLContextVersion(3, 2); // up to GLSL 150
122 //openGLContextCompatible = false;
124 curmapname = "maps/map01.d2m";
125 //conwriteln(genNextMapName());
127 //map = new LevelMap("maps/map01.d2m");
128 //ugInit(map.width*TileSize, map.height*TileSize);
130 //scale = 2;
133 //mapOfsX = TileSize*26;
134 //mapOfsY = TileSize*56;
135 map.getThingPos(1/*ThingId.Player1*/, &mapOfsX, &mapOfsY);
136 // fix viewport
138 mapOfsX = (mapOfsX*2)-vlWidth/2;
139 if (mapOfsX+vlWidth > map.width*16) mapOfsX = map.width*16-vlWidth;
140 if (mapOfsX < 0) mapOfsX = 0;
141 mapOfsY = (mapOfsY*2)-vlHeight/2;
142 if (mapOfsY+vlHeight > map.height*16) mapOfsY = map.height*16-vlHeight;
143 if (mapOfsY < 0) mapOfsY = 0;
145 setMapViewPos(mapOfsX, mapOfsY);
146 mapOfsX = mapViewPosX[1];
147 mapOfsY = mapViewPosY[1];
150 sdwindow = new SimpleWindow(vlWidth, vlHeight, "D2D", OpenGlOptions.yes, Resizablity.fixedSize);
151 //sdwindow.hideCursor();
153 sdwindow.closeQuery = delegate () { concmd("quit"); };
155 sdwindow.visibleForTheFirstTime = delegate () {
156 sdwindow.setAsCurrentOpenGlContext(); // make this window active
157 glbindLoadFunctions();
160 import core.stdc.stdio;
161 printf("GL version: %s\n", glGetString(GL_VERSION));
162 GLint l, h;
163 glGetIntegerv(GL_MAJOR_VERSION, &h);
164 glGetIntegerv(GL_MINOR_VERSION, &l);
165 printf("version: %d.%d\n", h, l);
166 printf("shader version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION));
167 GLint svcount;
168 glGetIntegerv(GL_NUM_SHADING_LANGUAGE_VERSIONS, &svcount);
169 if (svcount > 0) {
170 printf("%d shader versions supported:\n", svcount);
171 foreach (GLuint n; 0..svcount) printf(" %d: %s\n", n, glGetStringi(GL_SHADING_LANGUAGE_VERSION, n));
174 GLint ecount;
175 glGetIntegerv(GL_NUM_EXTENSIONS, &ecount);
176 if (ecount > 0) {
177 printf("%d extensions supported:\n", ecount);
178 foreach (GLuint n; 0..ecount) printf(" %d: %s\n", n, glGetStringi(GL_EXTENSIONS, n));
183 // check if we have sufficient shader version here
185 bool found = false;
186 GLint svcount;
187 glGetIntegerv(GL_NUM_SHADING_LANGUAGE_VERSIONS, &svcount);
188 if (svcount > 0) {
189 foreach (GLuint n; 0..svcount) {
190 import core.stdc.string : strncmp;
191 auto v = glGetStringi(GL_SHADING_LANGUAGE_VERSION, n);
192 if (v is null) continue;
193 if (strncmp(v, "120", 3) != 0) continue;
194 if (v[3] > ' ') continue;
195 found = true;
196 break;
199 if (!found) assert(0, "can't find OpenGL GLSL 120");
201 auto adr = glGetProcAddress("glTexParameterf");
202 if (adr is null) assert(0);
205 version(dont_use_vsync) {
206 sdwindow.vsync = false;
207 } else {
208 sdwindow.vsync = true;
210 //sdwindow.useGLFinish = false;
211 initOpenGL();
212 //sdwindow.redrawOpenGlScene();
213 if (!sdwindow.releaseCurrentOpenGlContext()) { import core.stdc.stdio; printf("can't release OpenGL context(1)\n"); }
215 if (!renderTid) {
216 renderTid = new Thread(&renderThread);
217 renderTid.start();
220 startRenderThread();
221 //postLoadLevel(curmapname);
222 concmd("map '"~curmapname~"'");
223 //postAddMessage("WRYYYYYYYYYY");
224 //postAddMessage("wryyyyyyyyyy");
227 //sdwindow.redrawOpenGlScene = delegate () { renderScene(); };
229 enum MSecsPerFrame = 1000/30; /* 30 is FPS */
231 enum { Left, Right, Up, Down }
232 bool[4] pressed = false;
233 bool testLightLocked = false;
235 sdwindow.eventLoop(MSecsPerFrame,
236 delegate () {
237 if (sdwindow.closed) return;
238 if (quitRequested) { closeWindow(); return; }
240 if (pressed[Left]) mapOfsX -= TileSize;
241 if (pressed[Right]) mapOfsX += TileSize;
242 if (pressed[Up]) mapOfsY -= TileSize;
243 if (pressed[Down]) mapOfsY += TileSize;
244 import std.math : cos, sin;
245 __gshared float itime = 0.0;
246 itime += 0.02;
247 if (movement) {
248 mapOfsX = cast(int)(800.0/2.0+cos(itime)*220.0);
249 mapOfsY = cast(int)(800.0/2.0+120.0+sin(itime)*160.0);
251 if (scale == 1) mapOfsX = mapOfsY = 0;
253 //sdwindow.redrawOpenGlSceneNow();
255 delegate (KeyEvent event) {
256 if (sdwindow.closed) return;
257 if (!conVisible && inEditMode) { postKeyEvent(event); return; }
258 if (event.pressed && event.key == Key.Escape) {
259 if (conVisible) concmd("r_console toggle"); else concmd("quit");
260 return;
263 switch (event.key) {
264 case Key.X: if (event.pressed) altMove = !altMove; break;
265 case Key.Left: if (altMove) pressed[Left] = event.pressed; break;
266 case Key.Right: if (altMove) pressed[Right] = event.pressed; break;
267 case Key.Up: if (altMove) pressed[Up] = event.pressed; break;
268 case Key.Down: if (altMove) pressed[Down] = event.pressed; break;
269 default:
272 if (true/*!altMove*/) {
273 if (!conVisible) {
274 switch (event.key) {
275 case Key.Left: case Key.Pad4: plrKeyUpDown(0, PLK_LEFT, event.pressed); break;
276 case Key.Right: case Key.Pad6: plrKeyUpDown(0, PLK_RIGHT, event.pressed); break;
277 case Key.Up: case Key.Pad8: plrKeyUpDown(0, PLK_UP, event.pressed); break;
278 case Key.Down: case Key.Pad2: plrKeyUpDown(0, PLK_DOWN, event.pressed); break;
279 case Key.Alt: plrKeyUpDown(0, PLK_JUMP, event.pressed); break;
280 case Key.Ctrl: plrKeyUpDown(0, PLK_FIRE, event.pressed); break;
281 case Key.Shift: plrKeyUpDown(0, PLK_USE, event.pressed); break;
282 case Key.Grave: if (event.pressed) concmd("r_console toggle"); break;
283 default:
285 } else {
286 plrKeyUpDown(0, PLK_LEFT, false);
287 plrKeyUpDown(0, PLK_RIGHT, false);
288 plrKeyUpDown(0, PLK_UP, false);
289 plrKeyUpDown(0, PLK_DOWN, false);
290 plrKeyUpDown(0, PLK_JUMP, false);
291 plrKeyUpDown(0, PLK_FIRE, false);
292 plrKeyUpDown(0, PLK_USE, false);
293 if (event.pressed && event.key == Key.Up) postChar('\x01');
294 else if (event.pressed && event.key == Key.Down) postChar('\x02');
295 else if (event.pressed && event.key == Key.PageUp) postChar('\x03');
296 else if (event.pressed && event.key == Key.PageDown) postChar('\x04');
297 else postKeyEvent(event);
301 delegate (MouseEvent event) {
302 if (inEditMode) { postMouseEvent(event); return; }
304 testLightX = event.x/*/scale*/;
305 testLightY = event.y/*/scale*/;
306 //testLightX += mapOfsX/scale;
307 //testLightY += mapOfsY/scale;
309 if (!testLightLocked) postTestLightMove(event.x, event.y);
311 delegate (dchar ch) {
312 //if (ch == '`') { concmd("r_console toggle"); return; }
313 if (conVisible) {
314 //{ import core.stdc.stdio; printf("%u\n", cast(uint)ch); }
315 if (ch < 128) postChar(cast(char)ch);
316 return;
318 if (inEditMode) return;
319 if (ch == 'q') concmd("quit"); //{ closeWindow(); return; }
320 //if (ch == 's') scanlines = !scanlines;
321 if (ch == '1') concmd("r_scale 1");
322 if (ch == '2') concmd("r_scale 2");
323 if (ch == 'D') concmd("nodoorclip");
324 if (ch == 'i') concmd("r_interpolation toggle; hudmsg \"Interpolation: $r_interpolation\"");
325 if (ch == 'l') concmd("r_lighting toggle");
326 if (ch == 'W') concmd("nowallclip");
327 if (ch == 'L') testLightLocked = !testLightLocked;
328 if (ch == 'p') concmd("g_pause toggle");
329 if (ch == '!') concmd("skiplevel");
330 if (ch == 'e') concmd("ed_toggle");
331 //if (ch == ' ') movement = !movement;
334 } catch (Exception e) {
335 import std.stdio : stderr;
336 stderr.writeln("FUUUUUUUUUUUU\n", e.toString);
338 closeWindow();
339 flushGui();