1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2025-08-07 06:40:22 +00:00

Fixed more weirdness in functions using glGetTexImage(), add proper bounds checking

This commit is contained in:
fgenesis 2012-06-14 17:40:01 +02:00
parent a6fc88d885
commit 6d92cd24c8
2 changed files with 65 additions and 87 deletions

View file

@ -1977,8 +1977,8 @@ void Game::fillGridFromQuad(Quad *q, ObsType obsType, bool trim)
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w); glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h); glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h);
//glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS, &c);// assume 4 //glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS, &c);// assume 4
int size = w*h*4; unsigned int size = w*h*4;
if(size <= 0) if (!size || w <= 0 || h <= 0)
return; return;
unsigned char *data = (unsigned char*)malloc(size + 6); unsigned char *data = (unsigned char*)malloc(size + 6);
memcpy(data + size, "SAFE", 5); memcpy(data + size, "SAFE", 5);
@ -2014,12 +2014,12 @@ void Game::fillGridFromQuad(Quad *q, ObsType obsType, bool trim)
{ {
// starting position = // starting position =
// tx / scale.x // tx / scale.x
int px = int(tx/q->scale.x) + x; unsigned int px = int(tx/q->scale.x) + x;
int py = int(ty/q->scale.y) + y; unsigned int py = int(ty/q->scale.y) + y;
if (px < w && py < h) if (px < unsigned(w) && py < unsigned(h))
{ {
int p = (py*w*4) + px*4; unsigned int p = (py*unsigned(w)*4) + (px*4) + 3; // position of alpha component
if (data[p+3] >= 254) if (p < size && data[p] >= 254)
{ {
num ++; num ++;
} }
@ -2794,32 +2794,27 @@ void Game::generateCollisionMask(Quad *q, int overrideCollideRadius)
else else
q->collideRadius = TILE_SIZE/2; q->collideRadius = TILE_SIZE/2;
q->collisionMask.clear(); q->collisionMask.clear();
std::vector<TileVector> obs;
TileVector tpos(q->position); TileVector tpos(q->position);
int w2 = int(q->getWidth()*q->scale.x)>>1; int widthscale = q->getWidth()*q->scale.x;
int h2 = int(q->getHeight()*q->scale.y)>>1; int heightscale = q->getHeight()*q->scale.y;
int w2 = widthscale/2;
int h2 = heightscale/2;
w2/=TILE_SIZE; w2/=TILE_SIZE;
h2/=TILE_SIZE; h2/=TILE_SIZE;
tpos.x -= w2; tpos.x -= w2;
tpos.y -= h2; tpos.y -= h2;
GLuint id = q->texture->textures[0]; GLuint id = q->texture->textures[0];
float w, h, c=4; int w = 0, h = 0;
glBindTexture(GL_TEXTURE_2D, id); glBindTexture(GL_TEXTURE_2D, id);
glGetTexLevelParameterfv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w); glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
glGetTexLevelParameterfv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h); glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h);
//glGetTexLevelParameterfv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS, &c);// assume 4 //glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS, &c);// assume 4
int size = w*h*c;
unsigned char *data=0;
int sz = size*sizeof(unsigned char); unsigned int size = w*h*4;
if (!size || w <= 0 || h <= 0)
return;
/* unsigned char *data = (unsigned char*)malloc(size);
std::ostringstream os;
os << "size: " << sz;
debugLog(os.str());
*/
data = (unsigned char*)malloc(sz);
if (!data) if (!data)
{ {
@ -2832,15 +2827,16 @@ void Game::generateCollisionMask(Quad *q, int overrideCollideRadius)
Vector collisionMaskHalfVector = Vector(q->getWidth()/2, q->getHeight()/2); Vector collisionMaskHalfVector = Vector(q->getWidth()/2, q->getHeight()/2);
for (int tx = 0; tx < (q->getWidth()*q->scale.x); tx+=TILE_SIZE) int szx = TILE_SIZE/q->scale.x;
int szy = TILE_SIZE/q->scale.y;
if (szx < 1) szx = 1;
if (szy < 1) szy = 1;
for (int tx = 0; tx < widthscale; tx+=TILE_SIZE)
{ {
for (int ty = 0; ty < (q->getHeight()*q->scale.y); ty+=TILE_SIZE) for (int ty = 0; ty < heightscale; ty+=TILE_SIZE)
{ {
int num = 0; int num = 0;
int szx = TILE_SIZE/q->scale.x;
int szy = TILE_SIZE/q->scale.y;
if (szx < 1) szx = 1;
if (szy < 1) szy = 1;
for (int x = 0; x < szx; x++) for (int x = 0; x < szx; x++)
{ {
@ -2848,18 +2844,12 @@ void Game::generateCollisionMask(Quad *q, int overrideCollideRadius)
{ {
// starting position = // starting position =
// tx / scale.x // tx / scale.x
int px = int(tx/q->scale.x) + x; unsigned int px = int(tx/q->scale.x) + x;
int py = int(ty/q->scale.y) + y; unsigned int py = int(ty/q->scale.y) + y;
if (px < w && py < h) if (px < unsigned(w) && py < unsigned(h))
{ {
int p = (py*w*c) + px*c; unsigned int p = (py*unsigned(w)*4) + (px*4) + 3; // position of alpha component
/* if (p < size && data[p] >= 250)
std::ostringstream os;
os << "data(" << int(data[p]) << ", " << int(data[p+1]);
os << ", " << int(data[p+2]) << ", " << int(data[p+3]) << ")";
debugLog(os.str());
*/
if (int(data[p+3]) >= 250)
{ {
num ++; num ++;
} }
@ -2869,9 +2859,6 @@ void Game::generateCollisionMask(Quad *q, int overrideCollideRadius)
if (num >= int((szx*szy)*0.25f)) if (num >= int((szx*szy)*0.25f))
{ {
TileVector tile(int((tx+TILE_SIZE/2)/TILE_SIZE), int((ty+TILE_SIZE/2)/TILE_SIZE)); TileVector tile(int((tx+TILE_SIZE/2)/TILE_SIZE), int((ty+TILE_SIZE/2)/TILE_SIZE));
obs.push_back(tile);
// + Vector(0,TILE_SIZE) // + Vector(0,TILE_SIZE)
q->collisionMask.push_back(tile.worldVector() - collisionMaskHalfVector); q->collisionMask.push_back(tile.worldVector() - collisionMaskHalfVector);
} }
@ -2903,7 +2890,7 @@ void Game::generateCollisionMask(Quad *q, int overrideCollideRadius)
q->collisionMaskRadius = 512; q->collisionMaskRadius = 512;
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
if (data) free(data); free(data);
/* /*
int rot = rotation.z; int rot = rotation.z;
while (rot > 360) while (rot > 360)

View file

@ -194,34 +194,31 @@ void Texture::destroy()
int Texture::getPixelWidth() int Texture::getPixelWidth()
{ {
#ifdef BBGE_BUILD_OPENGL #ifdef BBGE_BUILD_OPENGL
float w, h, c; int w = 0, h = 0;
glBindTexture(GL_TEXTURE_2D, textures[0]); glBindTexture(GL_TEXTURE_2D, textures[0]);
glGetTexLevelParameterfv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w); glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
glGetTexLevelParameterfv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h); glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h);
glGetTexLevelParameterfv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS, &c);// assume 4 //glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS, &c);// assume 4
int size = w*h*c; unsigned int size = w*h*4;
unsigned char *data=0; if (!size || w <= 0 || h <= 0)
data = (unsigned char*)malloc(size*sizeof(char)); return 0;
if (c == 4)
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); unsigned char *data = (unsigned char*)malloc(size*sizeof(char));
/* if (!data)
else if (c == 3)
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
*/
else
{ {
if (data) debugLog("Texture::getPixelWidth() malloc failed");
free(data);
return 0; return 0;
} }
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
int smallestx = -1, largestx = -1; int smallestx = -1, largestx = -1;
for (int x = 0; x < w; x++) for (unsigned int x = 0; x < unsigned(w); x++)
{ {
for (int y = 0; y < h; y++) for (unsigned int y = 0; y < unsigned(h); y++)
{ {
int p = (y*w*c) + x*c; unsigned int p = (y*unsigned(w)*4) + (x*4) + 3;
if (data[p+3] >= 254) if (p < size && data[p] >= 254)
{ {
if (smallestx == -1 || x < smallestx) if (smallestx == -1 || x < smallestx)
smallestx = x; smallestx = x;
@ -241,33 +238,28 @@ int Texture::getPixelWidth()
int Texture::getPixelHeight() int Texture::getPixelHeight()
{ {
#ifdef BBGE_BUILD_OPENGL #ifdef BBGE_BUILD_OPENGL
float w, h, c; int w = 0, h = 0;
glBindTexture(GL_TEXTURE_2D, textures[0]); glBindTexture(GL_TEXTURE_2D, textures[0]);
glGetTexLevelParameterfv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w); glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w);
glGetTexLevelParameterfv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h); glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &h);
glGetTexLevelParameterfv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS, &c);// assume 4 //glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_COMPONENTS, &c);// assume 4
int size = w*h*c; unsigned int size = w*h*4;
unsigned char *data=0; if (!size || w <= 0 || h <= 0)
data = (unsigned char*)malloc(size*sizeof(char)); return 0;
if (c == 4) unsigned char *data = (unsigned char*)malloc(size*sizeof(char));
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); if (!data)
/*
else if (c == 3)
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
*/
else
{ {
if (data) debugLog("Texture::getPixelHeight() malloc failed");
free(data);
return 0; return 0;
} }
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, data);
int smallesty = -1, largesty = -1; int smallesty = -1, largesty = -1;
for (int x = 0; x < w; x++) for (unsigned int x = 0; x < unsigned(w); x++)
{ {
for (int y = 0; y < h; y++) for (unsigned int y = 0; y < unsigned(h); y++)
{ {
int p = (y*w*c) + x*c; int p = (y*unsigned(w)*4) + (x*4) + 3;
if (data[p+3] >= 254) if (p < size && data[p] >= 254)
{ {
if (smallesty == -1 || y < smallesty) if (smallesty == -1 || y < smallesty)
smallesty = y; smallesty = y;
@ -277,8 +269,7 @@ int Texture::getPixelHeight()
} }
} }
glBindTexture(GL_TEXTURE_2D, 0); glBindTexture(GL_TEXTURE_2D, 0);
if (data) free(data);
free(data);
return largesty - smallesty; return largesty - smallesty;
#elif defined(BBGE_BUILD_DIRECTX) #elif defined(BBGE_BUILD_DIRECTX)
return 0; return 0;