hud: keys are on top now
[dd2d.git] / d2dgfx.d
blob1e26f4e6b58d17f4c9030d765a63b70cb2582c64
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 d2dgfx is aliced;
19 private:
21 import arsd.color;
22 import iv.stream;
24 import glutils;
25 import console;
26 import wadarc;
29 // ////////////////////////////////////////////////////////////////////////// //
30 public __gshared Color[256] d2dpal;
33 public void loadPalette () {
34 auto fl = openFile("playpal.pal");
35 foreach (immutable idx; 0..256) {
36 ubyte r = cast(ubyte)(fl.readNum!ubyte()*4);
37 ubyte g = cast(ubyte)(fl.readNum!ubyte()*4);
38 ubyte b = cast(ubyte)(fl.readNum!ubyte()*4);
39 d2dpal[idx].r = r;
40 d2dpal[idx].g = g;
41 d2dpal[idx].b = b;
42 d2dpal[idx].a = 255;
44 // color 0 is transparent
45 d2dpal[0].asUint = 0;
49 // ////////////////////////////////////////////////////////////////////////// //
50 public final class D2DImage {
51 public:
52 int sx, sy;
53 int width, height;
54 ubyte[] data;
55 TrueColorImage img;
56 Texture tex;
58 this (string name) {
59 try {
60 auto fl = openFile(name);
61 load(fl, false);
62 return;
63 } catch (Exception) {}
64 import std.algorithm : endsWith;
65 if (name.endsWith("_mirrored.vga")) {
66 auto fl = openFile(name[0..$-13]~".vga");
67 load(fl, true);
68 return;
70 auto fl = openFile(name); // throw error message
73 this (int awdt, int ahgt) {
74 assert(awdt > 0 && ahgt > 0);
75 sx = sy = 0;
76 width = awdt;
77 height = ahgt;
78 data.length = width*height;
79 data[] = 0;
82 @property bool valid () const pure nothrow @safe @nogc { pragma(inline, true); return (data !is null && width > 0 && height > 0); }
84 Color opIndex (usize y, usize x) { pragma(inline, true); return (x < width && y < height ? d2dpal.ptr[data.ptr[y*width+x]] : Color(0, 0, 0, 0)); }
86 void clear () {
87 if (tex !is null) tex.clear;
88 //if (img !is null) img.clear;
89 tex = null;
90 img = null;
91 data = null;
92 width = height = 0;
93 sx = sy = 0;
96 @property TrueColorImage asTCImage () {
97 if (img is null && valid) {
98 img = new TrueColorImage(width, height);
99 auto cols = img.imageData.colors.ptr;
100 foreach (int y; 0..height) {
101 foreach (int x; 0..width) {
102 ubyte c = data.ptr[y*width+x];
103 if (c == 0) {
104 *cols = Color(0, 0, 0, 0); // transparent
105 } else {
106 *cols = d2dpal[c];
108 ++cols;
112 return img;
115 void releaseImage () { img = null; }
117 void createGLTex () {
118 if (tex is null && valid) tex = new Texture(asTCImage, Texture.Option.Nearest);
121 @property Texture asGLTex () {
122 if (tex is null && valid) tex = new Texture(asTCImage, Texture.Option.Nearest);
123 return tex;
126 // for bottom-up view
127 void drawAtXY (int x, int y) {
128 asGLTex();
129 if (tex !is null) {
130 y += sy-height;
131 glutils.drawAtXY(tex, x-sx, y);
135 private:
136 void load(ST) (auto ref ST fi, bool mirrored) if (isReadableStream!ST) {
137 width = fi.readNum!ushort();
138 height = fi.readNum!ushort();
139 if (width < 1) assert(0);
140 if (height < 1) assert(0);
141 sx = fi.readNum!short();
142 sy = fi.readNum!short();
143 data = new ubyte[width*height];
144 fi.rawReadExact(data[]);
145 if (mirrored) mirrorVga();
148 void mirrorVga () {
149 auto nd = new ubyte[](data.length);
150 foreach (int y; 0..height) {
151 int npos = y*width+width-1;
152 foreach (int x; 0..width) {
153 nd[npos--] = data[y*width+x];
156 data = nd;