-
Notifications
You must be signed in to change notification settings - Fork 6
/
gl_spritecache.cpp
305 lines (272 loc) · 11.3 KB
/
gl_spritecache.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
/************************************************************************
* Copyright (c) 2005-2007 [email protected] *
* *
* This software is provided as-is, without any express or implied *
* warranty. In no event will the authors be held liable for any *
* damages arising from the use of this software. *
* *
* Permission is granted to anyone to use this software for any purpose, *
* including commercial applications, and to alter it and redistribute *
* it freely, subject to the following restrictions: *
* *
* 1. The origin of this software must not be misrepresented; you must *
* not claim that you wrote the original software. If you use this *
* software in a product, an acknowledgment in the product documentation *
* would be appreciated but is not required. *
* *
* 2. Altered source versions must be plainly marked as such, and must *
* not be misrepresented as being the original software. *
* *
* 3. This notice may not be removed or altered from any source *
* distribution. *
************************************************************************/
#include <map>
#include <cassert>
#include <SDL_image.h>
#include "gl_spritecache.h"
#include "image_loader.h"
#include "opengta.h"
#include "dataholder.h"
#include "buffercache.h"
#include "log.h"
//#include "physfsrwops.h"
//#include "image_loader.h"
namespace OpenGL {
SpriteIdentifier::SpriteIdentifier() : sprNum(0), remap(-1), delta(0) {}
SpriteIdentifier::SpriteIdentifier(PHYSFS_uint16 num, PHYSFS_sint16 map, PHYSFS_uint32 d) :
sprNum(num), remap(map), delta(d) {}
SpriteIdentifier::SpriteIdentifier(const SpriteIdentifier & other) :
sprNum(other.sprNum), remap(other.remap), delta(other.delta) {}
bool SpriteIdentifier::operator ==(const SpriteIdentifier & other) const {
if ((sprNum == other.sprNum) &&
(remap == other.remap) &&
(delta == other.delta))
return true;
return false;
}
bool SpriteIdentifier::operator <(const SpriteIdentifier & other) const {
if (sprNum < other.sprNum)
return true;
else if (sprNum > other.sprNum)
return false;
if (remap < other.remap)
return true;
else if (remap > other.remap)
return false;
if (delta < other.delta)
return true;
return false;
}
SpriteCache::SpriteCache() {
#ifdef DO_SCALE2X
doScale2x = true;
#else
doScale2x = false;
#endif
}
void SpriteCache::setScale2x(bool enabled) {
#ifndef DO_SCALE2X
if (enabled)
// FIXME: for some reason I can not catch this exception, thus it only prints
//throw E_NOTSUPPORTED("Scale2x feature disabled at compile time");
ERROR << "scale2x feature disabled at compile time - ignoring request" << std::endl;
#endif
if (loadedSprites.begin() == loadedSprites.end()) {
doScale2x = enabled;
}
else {
ERROR << "scale2x cannot be set during game - ignoring request" << std::endl;
}
}
bool SpriteCache::getScale2x() {
return doScale2x;
}
SpriteCache::~SpriteCache() {
clearAll();
}
void SpriteCache::clearAll() {
SpriteMapType::iterator i = loadedSprites.begin();
while (i != loadedSprites.end()) {
glDeleteTextures(1, &(*i).second.inPage);
++i;
}
loadedSprites.clear();
}
bool SpriteCache::has(PHYSFS_uint16 sprNum) {
SpriteMapType::iterator i = loadedSprites.find(SpriteIdentifier(sprNum, -1, 0));
if (i != loadedSprites.end())
return true;
INFO << "sprite not loaded sprnum: " << sprNum <<std::endl;
return false;
}
bool SpriteCache::has(PHYSFS_uint16 sprNum, PHYSFS_sint16 remap) {
SpriteMapType::iterator i = loadedSprites.find(SpriteIdentifier(sprNum, remap, 0));
if (i != loadedSprites.end())
return true;
INFO << "sprite not loaded sprnum: " << sprNum << " remap: " << remap <<std::endl;
return false;
}
bool SpriteCache::has(const SpriteIdentifier & si) {
SpriteMapType::iterator i = loadedSprites.find(si);
if (i != loadedSprites.end())
return true;
INFO << "sprite not loaded sprnum: " << si.sprNum << " remap: " << si.remap <<
" delta: " << si.delta << std::endl;
return false;
}
PagedTexture & SpriteCache::get(PHYSFS_uint16 sprNum) {
SpriteMapType::iterator i = loadedSprites.find(SpriteIdentifier(sprNum, -1, 0));
assert(i != loadedSprites.end());
return i->second;
}
PagedTexture & SpriteCache::get(PHYSFS_uint16 sprNum, PHYSFS_sint16 remap) {
SpriteMapType::iterator i = loadedSprites.find(SpriteIdentifier(sprNum, remap, 0));
assert(i != loadedSprites.end());
return i->second;
}
PagedTexture & SpriteCache::get(const SpriteIdentifier & si) {
SpriteMapType::iterator i = loadedSprites.find(si);
assert(i != loadedSprites.end());
return i->second;
}
void SpriteCache::add(PHYSFS_uint16 sprNum, PHYSFS_sint16 remap, PagedTexture & t) {
loadedSprites.insert(
std::make_pair<SpriteIdentifier, PagedTexture>(
SpriteIdentifier(sprNum, remap, 0), t));
}
void SpriteCache::add(const SpriteIdentifier & si, PagedTexture & t) {
loadedSprites.insert(std::make_pair<SpriteIdentifier, PagedTexture>(si, t));
}
PagedTexture SpriteCache::create(PHYSFS_uint16 sprNum,
OpenGTA::GraphicsBase::SpriteNumbers::SpriteTypes st, PHYSFS_sint16 remap = -1 ) {
/*
OpenGTA::GraphicsBase & style = OpenGTA::StyleHolder::Instance().get();
PHYSFS_uint16 real_num = style.spriteNumbers.reIndex(sprNum, st);
OpenGTA::GraphicsBase::SpriteInfo* info = style.getSprite(real_num);
assert(info);
OpenGL::PagedTexture t = createSprite(real_num, remap, info);
add(real_num, remap, t);
return t;
*/
return create(sprNum, st, remap, 0);
}
PagedTexture SpriteCache::create(PHYSFS_uint16 sprNum,
OpenGTA::GraphicsBase::SpriteNumbers::SpriteTypes st,
PHYSFS_sint16 remap, PHYSFS_uint32 delta) {
OpenGTA::GraphicsBase & style = OpenGTA::StyleHolder::Instance().get();
PHYSFS_uint16 real_num = style.spriteNumbers.reIndex(sprNum, st);
OpenGTA::GraphicsBase::SpriteInfo* info = style.getSprite(real_num);
assert(info);
OpenGL::PagedTexture t = createSprite(real_num, remap, delta, info);
SpriteIdentifier si(real_num, remap, delta);
add(si, t);
return t;
}
OpenGL::PagedTexture SpriteCache::createSprite(size_t sprite_num, PHYSFS_sint16 remap,
PHYSFS_uint32 delta, OpenGTA::GraphicsBase::SpriteInfo* info) {
INFO << "creating new sprite: " << sprite_num << " remap: " << remap << std::endl;
unsigned char* src = OpenGTA::StyleHolder::Instance().get().
getSpriteBitmap(sprite_num, remap , delta);
#if 0
if (sprite_num == 257) {
info->w = 72;
info->h = 72;
SDL_RWops * rwops = PHYSFSRWOPS_openRead("tree.png");//file.c_str());
SDL_Surface *surface = IMG_Load_RW(rwops, 1);
assert(surface);
uint16_t bpp = surface->format->BytesPerPixel;
ImageUtil::NextPowerOfTwo npot(surface->w, surface->h);
uint8_t * buffer = Util::BufferCacheHolder::Instance().requestBuffer(npot.w * npot.h * bpp);
SDL_LockSurface(surface);
ImageUtil::copyImage2Image(buffer, (uint8_t*)surface->pixels, surface->pitch, surface->h,
npot.w * bpp);
SDL_UnlockSurface(surface);
GLuint texture = ImageUtil::createGLTexture(npot.w, npot.h, (bpp == 4) ? true : false, buffer);
return OpenGL::PagedTexture(texture, 0, 0, GLfloat(surface->w)/npot.w, GLfloat(surface->h)/npot.h);
}
#endif
#if 0
int glwidth = 1;
int glheight = 1;
while(glwidth < info->w)
glwidth <<= 1;
while(glheight < info->h)
glheight <<= 1;
unsigned char* dst = Util::BufferCacheHolder::Instance().requestBuffer(glwidth * glheight * 4);
Util::BufferCacheHolder::Instance().unlockBuffer(src);
assert(dst != NULL);
unsigned char * t = dst;
unsigned char * r = src;
for (unsigned int i = 0; i < info->h; i++) {
memcpy(t, r, info->w * 4);
t += glwidth * 4;
r += info->w * 4;
}
#endif
ImageUtil::NextPowerOfTwo npot(info->w, info->h);
Util::BufferCache & bc = Util::BufferCacheHolder::Instance();
uint8_t* dst = bc.requestBuffer(npot.w * npot.h * 4);
ImageUtil::copyImage2Image(dst, src, info->w * 4, info->h, npot.w * 4);
bc.unlockBuffer(src);
#ifdef DO_SCALE2X
if (doScale2x) {
bc.lockBuffer(dst);
uint8_t* dst_scalex = ImageUtil::scale2x_32bit(dst, npot.w, npot.h);
bc.unlockBuffer(dst);
dst = dst_scalex;
#if 0
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
const int srcpitch = glwidth * 4;
const int dstpitch = glwidth * 8;
Uint8* srcpix = dst;
Util::BufferCacheHolder::Instance().lockBuffer(dst);
Uint8* dstpix = Util::BufferCacheHolder::Instance().requestBuffer(glwidth * glheight * 4 * 4);
Uint32 E0, E1, E2, E3, B, D, E, F, H;
for(int looph = 0; looph < glheight; ++looph)
{
for(int loopw = 0; loopw < glwidth; ++ loopw)
{
B = *(Uint32*)(srcpix + (MAX(0,looph-1)*srcpitch) + (4*loopw));
D = *(Uint32*)(srcpix + (looph*srcpitch) + (4*MAX(0,loopw-1)));
E = *(Uint32*)(srcpix + (looph*srcpitch) + (4*loopw));
F = *(Uint32*)(srcpix + (looph*srcpitch) + (4*MIN(glwidth-1,loopw+1)));
H = *(Uint32*)(srcpix + (MIN(glheight-1,looph+1)*srcpitch) + (4*loopw));
E0 = D == B && B != F && D != H ? D : E;
E1 = B == F && B != D && F != H ? F : E;
E2 = D == H && D != B && H != F ? D : E;
E3 = H == F && D != H && B != F ? F : E;
*(Uint32*)(dstpix + looph*2*dstpitch + loopw*2*4) = E0;
*(Uint32*)(dstpix + looph*2*dstpitch + (loopw*2+1)*4) = E1;
*(Uint32*)(dstpix + (looph*2+1)*dstpitch + loopw*2*4) = E2;
*(Uint32*)(dstpix + (looph*2+1)*dstpitch + (loopw*2+1)*4) = E3;
}
}
Util::BufferCacheHolder::Instance().unlockBuffer(dst);
dst = dstpix;
#endif
}
#endif
GLuint texid = (doScale2x) ?
ImageUtil::createGLTexture(npot.w * 2, npot.h * 2, true, dst)
: ImageUtil::createGLTexture(npot.w, npot.h, true, dst);
#if 0
glGenTextures(1, &texid);
glBindTexture(GL_TEXTURE_2D, texid);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
#ifdef DO_SCALE2X
if (doScale2x)
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, glwidth*2, glheight*2, 0, GL_RGBA, GL_UNSIGNED_BYTE, dst);
else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, glwidth, glheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, dst);
#else
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, glwidth, glheight, 0, GL_RGBA, GL_UNSIGNED_BYTE, dst);
#endif
#endif
return OpenGL::PagedTexture(texid, 0, 0,
float(info->w)/float(npot.w), float(info->h)/float(npot.h));
}
}