creaturesImage work: add mutableCopy and tint methods to the base class (and change...
[openc2e.git] / sprImage.cpp
blob422baea81de0441fbd902617837262d0635fb094
1 /*
2 * sprImage.cpp
3 * openc2e
5 * Created by Alyssa Milburn on Sun Nov 19 2006.
6 * Copyright (c) 2006 Alyssa Milburn. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
20 #include "openc2e.h"
21 #include "sprImage.h"
22 #include <iostream>
24 sprImage::sprImage(mmapifstream *in, std::string n) : creaturesImage(n) {
25 stream = in;
26 imgformat = if_paletted;
28 uint16 spritecount;
29 in->read((char *)&spritecount, 2); m_numframes = swapEndianShort(spritecount);
31 widths = new uint16[m_numframes];
32 heights = new uint16[m_numframes];
33 offsets = new uint32[m_numframes];
34 buffers = new void *[m_numframes];
36 for (unsigned int i = 0; i < m_numframes; i++) {
37 in->read((char *)&offsets[i], 4); offsets[i] = swapEndianLong(offsets[i]);
38 in->read((char *)&widths[i], 2); widths[i] = swapEndianShort(widths[i]);
39 in->read((char *)&heights[i], 2); heights[i] = swapEndianShort(heights[i]);
40 buffers[i] = in->map + offsets[i];
45 * Hacky fix for corrupt offset tables in SPR files.
46 * Only works if the file has 'normal' offsets we can predict, but this will only be called
47 * on known files anyway.
49 void sprImage::fixBufferOffsets() {
50 // we can do this safely because we only have an mmapifstream constructor
51 mmapifstream *in = (mmapifstream *)stream;
53 unsigned int currpos = 2 + (8 * m_numframes);
54 for (unsigned int i = 0; i < m_numframes; i++) {
55 buffers[i] = in->map + currpos;
56 currpos += widths[i] * heights[i];
60 sprImage::~sprImage() {
61 delete[] widths;
62 delete[] heights;
63 delete[] buffers;
64 delete[] offsets;
67 bool sprImage::transparentAt(unsigned int frame, unsigned int x, unsigned int y) {
68 unsigned int offset = (y * widths[frame]) + x;
69 unsigned char *buffer = (unsigned char *)buffers[frame];
70 return (buffer[offset] == 0);
73 /* vim: set noet: */