author | truelight |
Sun, 19 Aug 2007 13:16:06 +0000 | |
branch | noai |
changeset 9696 | 4384ed3de1f0 |
parent 9628 | b5c2449616b5 |
child 9703 | d2a6acdbd665 |
permissions | -rw-r--r-- |
9627 | 1 |
/* $Id$ */ |
2 |
||
3 |
/** @file grf.cpp */ |
|
4 |
||
9626 | 5 |
#include "../stdafx.h" |
6 |
#include "../gfx.h" |
|
7 |
#include "../fileio.h" |
|
8 |
#include "../debug.h" |
|
9 |
#include "grf.hpp" |
|
10 |
||
9628
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
11 |
bool SpriteLoaderGrf::LoadSprite(SpriteLoader::Sprite *sprite, const char *filename, uint32 file_pos) |
9626 | 12 |
{ |
13 |
/* Open the right file and go to the correct position */ |
|
14 |
FioSeekToFile(file_pos); |
|
15 |
||
16 |
/* Read the size and type */ |
|
17 |
int num = FioReadWord(); |
|
18 |
byte type = FioReadByte(); |
|
19 |
||
20 |
/* Type 0xFF indicates either a colormap or some other non-sprite info; we do not handle them here */ |
|
21 |
if (type == 0xFF) return false; |
|
22 |
||
23 |
sprite->height = FioReadByte(); |
|
24 |
sprite->width = FioReadWord(); |
|
25 |
sprite->x_offs = FioReadWord(); |
|
26 |
sprite->y_offs = FioReadWord(); |
|
27 |
||
28 |
/* 0x02 indicates it is a compressed sprite, so we can't rely on 'num' to be valid. |
|
29 |
* In case it is uncompressed, the size is 'num' - 8 (header-size). */ |
|
30 |
num = (type & 0x02) ? sprite->width * sprite->height : num - 8; |
|
31 |
||
32 |
/* XXX -- We should use a pre-located memory segment for this, malloc/free is pretty expensive */ |
|
33 |
byte *dest_orig = MallocT<byte>(num); |
|
34 |
byte *dest = dest_orig; |
|
35 |
||
36 |
/* Read the file, which has some kind of compression */ |
|
37 |
while (num > 0) { |
|
38 |
int8 code = FioReadByte(); |
|
39 |
||
40 |
if (code >= 0) { |
|
41 |
/* Plain bytes to read */ |
|
42 |
int size = (code == 0) ? 0x80 : code; |
|
43 |
num -= size; |
|
44 |
for (; size > 0; size--) { |
|
45 |
*dest = FioReadByte(); |
|
46 |
dest++; |
|
47 |
} |
|
48 |
} else { |
|
49 |
/* Copy bytes from earlier in the sprite */ |
|
50 |
const uint data_offset = ((code & 7) << 8) | FioReadByte(); |
|
51 |
int size = -(code >> 3); |
|
52 |
num -= size; |
|
53 |
for (; size > 0; size--) { |
|
54 |
*dest = *(dest - data_offset); |
|
55 |
dest++; |
|
56 |
} |
|
57 |
} |
|
58 |
} |
|
59 |
||
60 |
assert(num == 0); |
|
61 |
||
62 |
sprite->data = CallocT<SpriteLoader::CommonPixel>(sprite->width * sprite->height); |
|
63 |
||
64 |
/* When there are transparency pixels, this format has an other trick.. decode it */ |
|
65 |
if (type & 0x08) { |
|
66 |
for (int y = 0; y < sprite->height; y++) { |
|
67 |
bool last_item = false; |
|
68 |
/* Look up in the header-table where the real data is stored for this row */ |
|
69 |
int offset = (dest_orig[y * 2 + 1] << 8) | dest_orig[y * 2]; |
|
70 |
/* Go to that row */ |
|
71 |
dest = &dest_orig[offset]; |
|
72 |
||
73 |
do { |
|
74 |
SpriteLoader::CommonPixel *data; |
|
75 |
/* Read the header: |
|
76 |
* 0 .. 14 - length |
|
77 |
* 15 - last_item |
|
78 |
* 16 .. 31 - transparency bytes */ |
|
79 |
last_item = ((*dest) & 0x80) != 0; |
|
80 |
int length = (*dest++) & 0x7F; |
|
81 |
int skip = *dest++; |
|
82 |
||
83 |
data = &sprite->data[y * sprite->width + skip]; |
|
84 |
||
85 |
for (int x = 0; x < length; x++) { |
|
86 |
data->m = *dest; |
|
87 |
dest++; |
|
88 |
data++; |
|
89 |
} |
|
90 |
} while (!last_item); |
|
91 |
} |
|
92 |
} else { |
|
93 |
dest = dest_orig; |
|
94 |
for (int i = 0; i < sprite->width * sprite->height; i++) |
|
95 |
sprite->data[i].m = dest[i]; |
|
96 |
} |
|
97 |
||
9628
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
98 |
/* Make sure to mark all transparent pixels transparent on the alpha channel too */ |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
99 |
for (int i = 0; i < sprite->width * sprite->height; i++) |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
100 |
if (sprite->data[i].m != 0) sprite->data[i].a = 0xFF; |
b5c2449616b5
(svn r10195) [NoAI] -Sync: with trunk r10119:10194.
rubidium
parents:
9627
diff
changeset
|
101 |
|
9626 | 102 |
free(dest_orig); |
103 |
return true; |
|
104 |
} |