1 /* GemRB - Infinity Engine Emulator
2  * Copyright (C) 2003 The GemRB Project
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  *
19  */
20 
21 #include "TISImporter.h"
22 
23 #include "RGBAColor.h"
24 
25 #include "Interface.h"
26 #include "Sprite2D.h"
27 #include "Video.h"
28 
29 using namespace GemRB;
30 
TISImporter(void)31 TISImporter::TISImporter(void)
32 {
33 }
34 
~TISImporter(void)35 TISImporter::~TISImporter(void)
36 {
37 	delete str;
38 }
39 
Open(DataStream * stream)40 bool TISImporter::Open(DataStream* stream)
41 {
42 	if (stream == NULL) {
43 		return false;
44 	}
45 	delete str;
46 	str = stream;
47 	char Signature[8];
48 	str->Read( Signature, 8 );
49 	headerShift = 0;
50 	if (Signature[0] == 'T' && Signature[1] == 'I' && Signature[2] == 'S') {
51 		if (strncmp( Signature, "TIS V1  ", 8 ) != 0) {
52 			Log(ERROR, "TISImporter", "Not a Valid TIS file!");
53 			return false;
54 		}
55 		str->ReadDword( &TilesCount );
56 		str->ReadDword( &TilesSectionLen );
57 		str->ReadDword( &headerShift );
58 		str->ReadDword( &TileSize );
59 	} else {
60 		str->Seek( -8, GEM_CURRENT_POS );
61 	}
62 	return true;
63 }
64 
GetTile(unsigned short * indexes,int count,unsigned short * secondary)65 Tile* TISImporter::GetTile(unsigned short* indexes, int count,
66 	unsigned short* secondary)
67 {
68 	Animation* ani = new Animation( count );
69 	//pause key stops animation
70 	ani->gameAnimation = true;
71 	//the turning crystal in ar3202 (bg1) requires animations to be synced
72 	ani->pos = 0;
73 	for (int i = 0; i < count; i++) {
74 		ani->AddFrame( GetTile( indexes[i] ), i );
75 	}
76 	if (secondary) {
77 		Animation* sec = new Animation( count );
78 		for (int i = 0; i < count; i++) {
79 			sec->AddFrame( GetTile( secondary[i] ), i );
80 		}
81 		return new Tile( ani, sec );
82 	}
83 	return new Tile( ani );
84 }
85 
GetTile(int index)86 Holder<Sprite2D> TISImporter::GetTile(int index)
87 {
88 	Color Col[256];
89 	Color Palette[256]{};
90 	void* pixels = calloc(4096, 1);
91 	unsigned long pos = index *(1024+4096) + headerShift;
92 	if(str->Size()<pos+1024+4096) {
93 		// try to only report error once per file
94 		static TISImporter *last_corrupt = NULL;
95 		if (last_corrupt != this) {
96 			Log(ERROR, "TISImporter", "Corrupt WED file encountered; couldn't find any more tiles at tile %d", index);
97 			last_corrupt = this;
98 		}
99 
100 		// original PS:T AR0609 and AR0612 report far more tiles than are actually present :(
101 		Palette[0].g = 200;
102 		return core->GetVideoDriver()->CreatePalettedSprite( Region(0,0,64,64), 8, pixels, Palette );
103 	}
104 	str->Seek( pos, GEM_STREAM_START );
105 	str->Read( &Col, 1024 );
106 	int transindex = 0;
107 	bool transparent = false;
108 	for (int i = 0; i < 256; i++) {
109 		// bgra format
110 		Palette[i].r = Col[i].b;
111 		Palette[i].g = Col[i].g;
112 		Palette[i].b = Col[i].r;
113 		Palette[i].a = (Col[i].a) ? Col[i].a : 255; // alpha is unused by the originals but SDL will happily use it
114 		if (Palette[i].g==255 && !Palette[i].r && !Palette[i].b) {
115 			if (transparent) {
116 				Log(ERROR, "TISImporter", "Tile has two green (transparent) palette entries");
117 			} else {
118 				transparent = true;
119 				transindex = i;
120 			}
121 		}
122 	}
123 	str->Read( pixels, 4096 );
124 	return core->GetVideoDriver()->CreatePalettedSprite( Region(0,0,64,64), 8, pixels, Palette, transparent, transindex );
125 }
126 
127 #include "plugindef.h"
128 
129 GEMRB_PLUGIN(0x19F91578, "TIS File Importer")
130 PLUGIN_CLASS(IE_TIS_CLASS_ID, TISImporter)
131 END_PLUGIN()
132