17
17
#include "precompiled.hpp"
18
18
#include "hrCache.hpp"
20
const int HR_FILE_SIZE = 0;//200000;
20
const int HR_FILE_SIZE = 50000;
22
22
hrCache::hrCache() : inc(0), target(0)
24
cache.setMaxCost(200);
24
cache.setMaxCost(300);
27
27
dir.mkdir("cache/");
28
file.setFileName("cache/cache0");
29
file.open(QIODevice::ReadOnly | QIODevice::Append);
28
cacheFile.setFileName("cache/cache0");
29
cacheFile.open(QIODevice::ReadOnly | QIODevice::Truncate | QIODevice::Append);
31
QFile fileFat("cache/cache1");
31
/*QFile fileFat("cache/cache1");
32
32
if (fileFat.open(QIODevice::ReadOnly))
34
34
QDataStream in(&fileFat);
39
39
hrCache::~hrCache()
41
QFile fileFat("cache/cache1");
41
/*QFile fileFat("cache/cache1");
42
42
if (fileFat.open(QIODevice::WriteOnly | QIODevice::Truncate))
44
44
QDataStream out(&fileFat);
49
49
void hrCache::setTarget(GLuint textureTarget)
96
79
return hrGraphicsItem(key, item->countFrames(), item->getSize());
99
int hrCache::loadFromDisc(QString name, hrCacheItem* item)
83
hrCacheItem* hrCache::Load(const QString &name) const
85
hrCacheItem *item = new hrCacheItem();
103
87
QImageReader ir("vfs:/" + name);
106
for (int i = 0; ir.jumpToImage(i); i++)
109
QImage conv = QGLWidget::convertToGLFormat(target == GL_TEXTURE_2D
113
GLuint tx = bindImage(conv.bits(), conv.width(), conv.height());
115
bytes += conv.byteCount();
120
qFatal("hrCache: %s", qPrintable(ir.errorString() + name));
122
item->setSize(im.size());
126
void hrCache::loadFromDiscPrepared(qint64 pos, hrCacheItem *item)
131
QDataStream in(&file);
89
for (int i = 0; ir.jumpToImage(i); i++)
92
QImage conv = QGLWidget::convertToGLFormat(target == GL_TEXTURE_2D
96
GLuint tx = bindImage(conv.bits(), conv.width(), conv.height());
99
item->setSize(im.size());
103
qFatal("hrCache: %s", qPrintable(ir.errorString() + name));
109
hrCacheItem* hrCache::LoadAndPrepare(const QString &name)
111
hrCacheItem *item = new hrCacheItem();
113
QFile file("vfs:/" + name);
114
file.open(QIODevice::ReadOnly);
116
bool isHeaderWrite = true;
117
bool isFileLarge = false;
118
if (file.size() > HR_FILE_SIZE)
122
if (!cacheFile.atEnd())
124
cacheFile.seek(cacheFile.size());
126
fat[name] = cacheFile.pos();
128
QDataStream out(&cacheFile);
130
QImageReader ir(&file);
132
for (int i = 0; ir.jumpToImage(i); i++)
136
QImage conv = QGLWidget::convertToGLFormat(target == GL_TEXTURE_2D
140
GLuint tx = bindImage(conv.bits(), conv.width(), conv.height());
148
out << ir.imageCount();
149
isHeaderWrite = false;
152
out << QByteArray::fromRawData((char*)conv.bits(), conv.byteCount());
156
item->setSize(im.size());
160
qFatal("hrCache: %s", qPrintable(ir.errorString() + name));
162
if (cacheFile.error())
164
qFatal("hrCache: %s", qPrintable(cacheFile.errorString()));
170
hrCacheItem* hrCache::LoadPrepared(qint64 pos)
172
hrCacheItem *item = new hrCacheItem();
176
QDataStream in(&cacheFile);
134
180
item->setSize(size);
135
QVector<QByteArray> images;
137
for (int i = 0; i < images.size(); i++)
184
for (int i = 0; i < count; i++)
139
GLuint tx = bindImage(images.at(i).data()
187
GLuint tx = bindImage(image.data(), size.width(), size.height());
143
188
item->addFrame(tx);
191
if (cacheFile.error())
147
qFatal("hrCache: %s", qPrintable(file.errorString()));
193
qFatal("hrCache: %s", qPrintable(cacheFile.errorString()));
151
198
GLuint hrCache::getTexture(const hrGraphicsItem &item)
160
207
cacheItem = cache[item.key];
162
else if (offsets.contains(item.key))
164
cacheItem = new hrCacheItem();
165
loadFromDiscPrepared(offsets[item.key], cacheItem);
166
cache.insert(item.key, cacheItem);
168
209
else if (files.contains(item.key))
170
cacheItem = new hrCacheItem();
171
loadFromDisc(files[item.key], cacheItem);
211
const QString &name = files[item.key];
212
if (fat.contains(name))
214
cacheItem = LoadPrepared(fat[name]);
218
cacheItem = Load(name);
172
220
cache.insert(item.key, cacheItem);
176
224
qFatal("hrCache: key not found");
179
227
GLuint tx = cacheItem->getFrame(item.curFrame);
183
GLuint hrCache::bindImage(const GLvoid* image, int width, int height)
231
GLuint hrCache::bindImage(const GLvoid* image, int width, int height) const
185
233
Q_ASSERT(target);
186
234
GLuint format = GL_RGBA;
218
void hrCache::saveToDisc(QString name)
220
QImageReader ir("vfs:/" + name);
221
QVector<QByteArray> images;
223
for (int i = 0; ir.jumpToImage(i); i++)
227
QImage conv = QGLWidget::convertToGLFormat(target == GL_TEXTURE_2D
231
QByteArray bytes((char*)conv.bits(), conv.byteCount());
232
images.append(bytes);
237
qFatal("hrCache: %s", qPrintable(ir.errorString() + name));
240
QSize size = im.size();
242
QDataStream out(&file);
248
qFatal("hrCache: %s", qPrintable(file.errorString()));
252
266
QImage hrCache::ImageToPOT(const QImage &im) const
254
268
int s = NearestGLTextureSize(qMax(im.width(), im.height()));