Sort include order.
[gemrb.git] / gemrb / plugins / TISImporter / TISImporter.cpp
blob3a5cb1934734b0bdd42d93ed9ac9d83ced091779
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 The GemRB Project
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.
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.
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.
21 #include "TISImporter.h"
23 #include "RGBAColor.h"
24 #include "win32def.h"
26 #include "Interface.h"
27 #include "Video.h"
29 TISImporter::TISImporter(void)
31 str = NULL;
32 autoFree = false;
35 TISImporter::~TISImporter(void)
37 if (str && autoFree) {
38 delete( str );
42 bool TISImporter::Open(DataStream* stream, bool autoFree)
44 if (stream == NULL) {
45 return false;
47 if (str && this->autoFree) {
48 delete( str );
50 str = stream;
51 this->autoFree = autoFree;
52 char Signature[8];
53 str->Read( Signature, 8 );
54 headerShift = 0;
55 if (Signature[0] == 'T' && Signature[1] == 'I' && Signature[2] == 'S') {
56 if (strncmp( Signature, "TIS V1 ", 8 ) != 0) {
57 printf( "[TISImporter]: Not a Valid TIS File.\n" );
58 return false;
60 str->ReadDword( &TilesCount );
61 str->ReadDword( &TilesSectionLen );
62 str->ReadDword( &headerShift );
63 str->ReadDword( &TileSize );
64 } else {
65 str->Seek( -8, GEM_CURRENT_POS );
67 return true;
70 Tile* TISImporter::GetTile(unsigned short* indexes, int count,
71 unsigned short* secondary)
73 Animation* ani = new Animation( count );
74 ani->x = ani->y = 0;
75 //pause key stops animation
76 ani->gameAnimation = true;
77 for (int i = 0; i < count; i++) {
78 ani->AddFrame( GetTile( indexes[i] ), i );
80 if (secondary) {
81 Animation* sec = new Animation( count );
82 sec->x = sec->y = 0;
83 for (int i = 0; i < count; i++) {
84 sec->AddFrame( GetTile( secondary[i] ), i );
86 return new Tile( ani, sec );
88 return new Tile( ani );
91 Sprite2D* TISImporter::GetTile(int index)
93 RevColor RevCol[256];
94 Color Palette[256];
95 void* pixels = malloc( 4096 );
96 unsigned long pos = index *(1024+4096) + headerShift;
97 if(str->Size()<pos+1024+4096) {
98 // try to only report error once per file
99 static TISImporter *last_corrupt = NULL;
100 if (last_corrupt != this) {
101 /*printf("Invalid tile index: %d\n",index);
102 printf("FileSize: %ld\n", str->Size() );
103 printf("Position: %ld\n", pos);
104 printf("Shift: %d\n", headerShift);*/
105 printf("Corrupt WED file encountered; couldn't find any more tiles at tile %d\n", index);
106 last_corrupt = this;
109 // original PS:T AR0609 and AR0612 report far more tiles than are actually present :(
110 memset(pixels, 0, 4096);
111 memset(Palette, 0, 256 * sizeof(Color));
112 Palette[0].g = 200;
113 Sprite2D* spr = core->GetVideoDriver()->CreateSprite8( 64, 64, 8, pixels, Palette, false, 0 );
114 spr->XPos = spr->YPos = 0;
115 return spr;
117 str->Seek( ( index * ( 1024 + 4096 ) + headerShift ), GEM_STREAM_START );
118 str->Read( &RevCol, 1024 );
119 int transindex = 0;
120 bool transparent = false;
121 for (int i = 0; i < 256; i++) {
122 Palette[i].r = RevCol[i].r;
123 Palette[i].g = RevCol[i].g;
124 Palette[i].b = RevCol[i].b;
125 Palette[i].a = RevCol[i].a;
126 if (Palette[i].g==255 && !Palette[i].r && !Palette[i].b) {
127 if (transparent) {
128 printMessage( "TISImporter", "Tile has two green (transparent) palette entries\n", LIGHT_RED );
129 } else {
130 transparent = true;
131 transindex = i;
135 str->Read( pixels, 4096 );
136 Sprite2D* spr = core->GetVideoDriver()->CreateSprite8( 64, 64, 8, pixels, Palette, transparent, transindex );
137 spr->XPos = spr->YPos = 0;
138 return spr;
141 #include "plugindef.h"
143 GEMRB_PLUGIN(0x19F91578, "TIS File Importer")
144 PLUGIN_CLASS(IE_TIS_CLASS_ID, TISImporter)
145 END_PLUGIN()