Hello All,
I’m currently working with sdl 1.3 and sdl_image 1.2.12, and there seems to be a problem on loading an image under mac osx ( my code runs fine under IOS )
Code:
std::string full_path = ResourceManager::Instance()->GetPathOfFile(filename);
SDL_Surface *image = NULL;
SDL_Surface *texture;
int tex_width, tex_height;
if((image = IMG_Load(full_path.c_str())) == NULL)
{
DebugOut("could not open image: %s \n", full_path.c_str());
SDL_FreeSurface(image);
return;
}
DebugOut("Error: %s \n", IMG_GetError());
if(image->w <= 0 || image->h <= 0)
{
DebugOut("Bad size of image: %s \n", filename.c_str());
SDL_FreeSurface(image);
}
//Make sure we create a texture with a size that is a power of 2
if (!isPower2(image->w))
tex_width = nextPowerOfTwo(image->w);
else
tex_width = image->w;
if (!isPower2(image->h))
tex_height = nextPowerOfTwo(image->h);
else
tex_height = image->h;
Uint32 rmask;
Uint32 gmask;
Uint32 bmask;
Uint32 amask;
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
{
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
}
else
{
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
}
texture = SDL_CreateRGBSurface(SDL_SWSURFACE, tex_width, tex_height, 32, rmask, gmask, bmask, amask);
SDL_SetAlpha(image, 0, 0);
SDL_BlitSurface(image, 0 ,texture, 0);
//And then create a new texture in the usual way
CHECK_GL_ERROR;
glGenTextures(1, &mglId);
CHECK_GL_ERROR;
CHECK_GL_ERROR;
glBindTexture(GL_TEXTURE_2D, mglId);
CHECK_GL_ERROR;
CHECK_GL_ERROR;
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_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);
CHECK_GL_ERROR;
GLenum internal_format;
GLenum img_format, img_type;
switch (texture->format->BitsPerPixel)
{
case 32:
img_format = GL_RGBA;
img_type = GL_UNSIGNED_BYTE;
internal_format = GL_RGBA;
break;
case 24:
img_format = GL_RGB;
img_type = GL_UNSIGNED_BYTE;
internal_format = GL_RGBA;
break;
case 16:
img_format = GL_RGBA;
img_type = GL_UNSIGNED_SHORT_5_5_5_1;
internal_format = GL_RGBA;
break;
default:
img_format = GL_LUMINANCE;
img_type = GL_UNSIGNED_BYTE;
internal_format=GL_RGBA;
break;
}
CHECK_GL_ERROR;
glTexImage2D(GL_TEXTURE_2D, 0, internal_format, tex_width, tex_height, 0, img_format, img_type, texture->pixels);
CHECK_GL_ERROR;
SDL_FreeSurface(image);
SDL_FreeSurface(texture);
The problem is after the IMG_LOAD the image ptr is valid, but it’s surface->map (BLITMap) isn’t and it will crash in the SetAlpha function. Maybe it has something todo with multithreading? If I debug the IMG_Load function the return surface seems to be valid, but as soon as I try to do something with the image it isn’t