-
Notifications
You must be signed in to change notification settings - Fork 4
/
picturefile.cpp
215 lines (174 loc) · 6.01 KB
/
picturefile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <QTemporaryFile>
#include "picturefile.h"
#include "resourcehandler.h"
#include "userthread.h"
extern ResourceHandler g_resourceHandler;
PictureFile::PictureFile( QObject *parent ) : TibiaFile( parent )
{
m_count = 0;
m_loaded = false;
m_thread = new PictureThread( this );
}
PictureFile::~PictureFile( void )
{
delete m_thread;
}
void PictureFile::unload( void )
{
TibiaFile::unload();
m_count = 0;
m_loaded = false;
}
bool PictureFile::createNew( void )
{
if( !m_loaded ) {
m_count = 1;
TibiaSprite picture;
picture.id = 1;
picture.setDummy( false );
SharedResource newResource = g_resourceHandler.createLocalResource( RESOURCE_TYPE_PICTURE, 0, picture );
ResourceList resources;
resources.push_back( newResource );
g_resourceHandler.addLocalResources( RESOURCE_TYPE_PICTURE, 0, resources );
m_loaded = true;
return true;
}
return false;
}
bool PictureFile::idle( const QString& name, bool read )
{
setFileName( name );
if( !TibiaFile::exists() ) {
emit documentError( name, QObject::tr( "File does not exist." ), -1 );
return false;
}
if( !TibiaFile::open( QIODevice::ReadOnly ) ) {
emit parseError( QObject::tr( "Open Error" ), TibiaFile::error() );
return false;
}
TibiaFile::seek( 0 );
if( read ) {
QDataStream in( this );
in.setByteOrder( QDataStream::LittleEndian );
in >> m_signature;
in >> m_count;
}
m_idle = true;
m_loaded = true;
return m_idle;
}
/*bool PictureFile::save( const QString& name )
{
if( isIdle() ) // File is on idle, we need to stream our file to our new file
{
// Create temporary file, write signature and count
QTemporaryFile file( name );
if( !file.open() ){
emit parseError( QObject::tr( "Open Error" ), file.error() );
return false;
}
quint32 offset = 0, now = 0;
QDataStream dest( &file );
dest.setByteOrder( QDataStream::LittleEndian );
dest << m_signature;
dest << m_count;
now = file.pos();
offset = sizeof( m_signature ) + sizeof( m_count ) + m_count * 5;
for( quint16 i = 0; i < m_count; i++ )
{
TibiaSprite picture = getPicture( i );
if( !picture.isDummy() ){
offset += picture.width * picture.height * sizeof( offset );
}
}
for( quint16 i = 0; i < m_count; i++ )
{
TibiaSprite picture = getPicture( i );
if( !picture.isDummy() ){
dest << picture.width;
dest << picture.height;
dest << picture.r; // Forget what these are for
dest << picture.g;
dest << picture.b;
quint32 now = TibiaFile::pos();
for( quint8 offset_y = 0; offset_y < picture.height; offset_y++ )
{
for( quint8 offset_x = 0; offset_x < picture.width; offset_x++ )
{
TibiaFile::seek( now );
dest << offset;
TibiaFile::writeSprite ( dest, picture, offset_x * 32, offset_y * 32, offset, false );
now += sizeof( quint32 );
}
}
}
}
if( QFile::exists( name ) )
{
if( name.compare( TibiaFile::fileName(), Qt::CaseInsensitive ) == 0 ) // Destination == Source, Close source
TibiaFile::close();
if( !QFile::remove( name ) ) // Overwrite Error
return false;
}
file.copy( name );
idle( name, true ); // Upon saving we must re-idle our new file to continue
return true;
}
return false;
//m_thread->setup();
//m_thread->setName( name );
//m_thread->execute();
//return true;
}*/
TibiaSprite PictureFile::loadPicture( quint16 pictureId )
{
QMutexLocker locker( &mutex );
if( isIdle() ) { // We are idling our file so we can read constantly
if( pictureId > m_count )
return dummy;
QDataStream in( this );
in.setByteOrder( QDataStream::LittleEndian );
TibiaFile::seek( 0 );
TibiaFile::seek( sizeof( m_signature ) + sizeof( m_count ) );
quint32 offset = 0;
for( quint16 i = 0; i < m_count; i++ ) {
quint8 width;
quint8 height;
in >> width;
in >> height;
if( i == pictureId ) {
TibiaSprite picture( pictureId, width, height );
in >> picture.r;
in >> picture.g;
in >> picture.b;
picture.setDummy( false );
for( quint8 offset_y = 0; offset_y < picture.height; offset_y++) {
for( quint8 offset_x = 0; offset_x < picture.width; offset_x++) {
in >> offset;
quint32 lastOffset = TibiaFile::pos();
if( offset == 0 ) {
lastOffset += sizeof( quint32 );
TibiaFile::seek( lastOffset );
continue;
}
TibiaFile::seek( offset );
TibiaFile::readSprite ( in, picture, offset_x * 32, offset_y * 32, false );
TibiaFile::seek( lastOffset );
}
}
return picture;
} else // width, height, r, g, b, offsets -> data
TibiaFile::seek( TibiaFile::pos() + 3 + ( width * height * sizeof( offset ) ) );
}
}
return dummy;
}
TibiaSprite PictureFile::getPicture( quint16 id )
{
SharedResource resource = g_resourceHandler.loadLocalResource( RESOURCE_TYPE_PICTURE, id, false );
if( resource )
return g_resourceHandler.getSpriteByResource( resource );
if( isIdle() )
return loadPicture( id );
return dummy;
}