Sign in to follow this  
MichaelBarth

SDL 2.0 and SDL_image problem

Recommended Posts

I tried out SDL 2.0 to see how much better it is than SDL 1.2. I came into a small little bump in road when I'm trying to load an image. These images that I'm using I've had no problem using with SDL 1.2. Basically what's happening is that the BytesPerPixel is getting set to 8 and the BitsPerPixel is getting set to 0. I don't know how this really affects the surface I'm working with, but I wanted to load an OpenGL texture by converting the surface into it. Here's my function:

[CODE]
int LoadTexture2D(const char* filename)
{
SDL_Surface* surface = IMG_Load(filename);
if (!surface)
{
errorlevel = 2;
return -1;
}
if ((surface->w & (surface->w - 1)) != 0)
++warnings;
if ((surface->h & (surface->h - 1)) != 0)
++warnings;
printf("%d\n", surface->format->BitsPerPixel);
numOfColors = surface->format->BytesPerPixel;
printf("%d\n", numOfColors);
switch (numOfColors)
{
case 3:
if (surface->format->Rmask == 0x000000ff)
textureFormat = GL_RGB;
else
textureFormat = GL_BGR;
break;
case 4:
if (surface->format->Rmask == 0x000000ff)
textureFormat = GL_RGBA;
else
textureFormat = GL_BGRA;
break;
default:;
++warnings;
errorlevel = 2;
return -1;
break;
}
Width = surface->w;
Height = surface->h;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, numOfColors, surface->w, surface->h, 0, textureFormat, GL_UNSIGNED_BYTE, surface->pixels);
SDL_FreeSurface(surface);
return 0;
}
[/CODE]

In case you're wondering, this function is inside a class and so are the variables that it's using. So what's the problem, is it just that this function is outdated for SDL 2.0? Should I load an OpenGL texture some other way? Edited by Spirrwell

Share this post


Link to post
Share on other sites
Never used SDL_image so no idea, but make sure you're using the SDL2 version of SDL_image and not the original one. SDL 2.0 looks similar to 1.2 but it isn't compatible, and as such all the satellite libraries (including SDL_image) had to be adapted.

I doubt that in itself will solve your problem, but make sure you update SDL_image and everything else or you're going to have some serious issues with SDL 2.0 (in some cases the libraries won't even [i]link[/i]).

Share this post


Link to post
Share on other sites
Great, I've got a new problem now. It won't even load the image. It tries to load and it's just null. I completely rebuilt the project and I linked SDL2_image and removed the link to SDL_image. Now I have the modern functions such as IMG_LoadTexture, but I can't even load an image.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this