switched to GPLv3 ONLY, because i don't trust FSF anymore
[knightmare.git] / zmythspr.d
blobd7a96099938edd24bdf75234e60a51bf43c4ba44
1 /* coded by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
2 * Understanding is not required. Only obedience.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, version 3 of the License ONLY.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 module zmythspr;
18 import arsd.simpledisplay;
19 import iv.vfs.io;
22 // ////////////////////////////////////////////////////////////////////////// //
23 immutable Color[16] egapal = [
24 Color(0x00, 0x00, 0x00),
25 Color(0x00, 0x00, 0xAA),
26 Color(0x00, 0xAA, 0x00),
27 Color(0x00, 0xAA, 0xAA),
28 Color(0xAA, 0x00, 0x00),
29 Color(0xAA, 0x00, 0xAA),
30 Color(0xAA, 0x55, 0x00),
31 Color(0xAA, 0xAA, 0xAA),
32 Color(0x55, 0x55, 0x55),
33 Color(0x55, 0x55, 0xFF),
34 Color(0x55, 0xFF, 0x55),
35 Color(0x55, 0xFF, 0xFF),
36 Color(0xFF, 0x55, 0x55),
37 Color(0xFF, 0x55, 0xFF),
38 Color(0xFF, 0xFF, 0x55),
39 Color(0xFF, 0xFF, 0xFF),
43 // ////////////////////////////////////////////////////////////////////////// //
44 final class MythSprite {
45 public:
46 int wdt, hgt;
47 Color[] pix;
49 public:
50 this (const(char)[] fname, bool masked) { load(VFile(fname), masked); }
51 this (VFile fl, bool masked) { load(fl, masked); }
53 final @property bool valid () const pure nothrow @safe @nogc { pragma(inline, true); return (wdt > 0 && hgt > 0); }
55 final @property int width () const pure nothrow @safe @nogc { pragma(inline, true); return wdt; }
56 final @property int height () const pure nothrow @safe @nogc { pragma(inline, true); return hgt; }
58 final void clear () {
59 wdt = hgt = 0;
60 delete pix;
63 void load (VFile fl, bool masked) {
64 clear();
65 scope(failure) clear();
67 wdt = fl.readNum!ubyte;
68 hgt = fl.readNum!ubyte;
70 ubyte[][5] planes;
71 scope(exit) foreach (ref p; planes[]) delete p;
73 foreach (immutable pidx, ref p; planes[]) {
74 p.length = wdt*hgt;
75 if (pidx == 4 && !masked) {
76 p[] = 0xff;
77 } else {
78 fl.rawReadExact(p[]);
82 ubyte getbit (int x, int y, uint pnum) {
83 auto p = planes[pnum];
84 return (planes[pnum][y*wdt+x/8]>>(7-(x&7)))&1;
87 pix.length = (wdt*8)*hgt;
89 foreach (immutable dy; 0..hgt) {
90 foreach (immutable dx; 0..wdt*8) {
91 Color c = Color.transparent;
92 if (getbit(dx, dy, 4)) {
93 c = egapal[getbit(dx, dy, 3)|(getbit(dx, dy, 2)<<1)|(getbit(dx, dy, 1)<<2)|(getbit(dx, dy, 0)<<3)];
95 pix[dy*(wdt*8)+dx] = c;
99 wdt *= 8;
102 final @property Color getpix (int x, int y) const nothrow @trusted @nogc {
103 if (x < 0 || y < 0 || x >= wdt*8 || y >= hgt) return Color.transparent;
104 return pix[y*wdt+x];
109 // ////////////////////////////////////////////////////////////////////////// //
110 MythSprite[] loadSprites (const(char)[] fname, bool masked) {
111 MythSprite[] res;
112 auto fl = VFile(fname);
113 while (fl.tell < fl.size) res ~= new MythSprite(fl, masked);
114 return res;