last day (17 days later) » 

6:31 PM
2
Q: SDL2 Texture sometimes empty after loading multiple 8 bit surfaces

Martin VéronneauI'll try to make that question as concise as possible, but don't hesitate to ask for clarification. I'm dealing with legacy code, and I'm trying to load thousands of 8 bit images from the disk to create a texture for each. I've tried multiple things, and I'm at the point where I'm trying to loa...

 
It sounds like you're dealing with Undefined Behavior somewhere. I would start by getting rid of the Lock/Unlock Surface calls; since the surface is not RLE optimized, they aren't needed. Also, I think you can lose the memset call to zero out the pixels. They are already zero.
 
Thanks for your comment! You're right, I'll remove the SDL_memset. The help page for SDL_LockSurface seems to indicate that we should lock before accessing the pixels, not because the surface is RLE-optimized. I'll add a check to SDL_MUSTLOCK before locking and unlocking.
 
See this page One thing to note about the SDL documentation is that it is pretty spotty, so you pretty much have to read the entire thing, and then read the source code.
 
I tested with and without the surface locking, and I'm still getting the same problem. I'll edit my question to add a sample of what kind of problem I'm observing.
 
By "bad", you mean the pixels are mapped to the wrong color?
 
6:31 PM
Like I pointed out, the bad textures are completely blank (with all pixels transparent black). The size of the textures seems to match the original surface size though (as reported by NSight).
 
The thing about textures is that they are supposed to map the logical palette of the surface to the physical palette of the display hardware. You may have a palette issue. Are all of your 8-bit images using the same color space (The same palette colors)?
 
No, the palette can be different from image to image. That's why I'm passing the current palette as parameter and I'm converting the pixels to 32 bits color. That's what this line is doing ((u32*)tempSurface->pixels)[uiDestinationOffset + uiCurrentOffset] = (u32)((currentColor->a << 24) + (currentColor->r << 16) + (currentColor->g << 8) + (currentColor->b << 0));
I was under the impression that modern video card didn't support 8 bits textures.
 
That's another thing the abstract SDL textures are used for, but the point I'm getting at is that if you have a manageable number of 8-bit palettes, you should try again to use 8-bit surfaces, and set the palette colors to the appropriate palette. The transparency mask may be giving you bad mapping, because 8-bit color doesn't recognize transparency.
 
6:51 PM
Okay? So, you mean that the texture code in the SDL can map correctly 8-bit surface, right? I can certainly try to do that.

I'm not sure I understand why there would be bad color mapping. I left that part out of the code, but the palette I'm passing to this function was "hand baked". I'm reading each entry of the palette and creating an array of SDL_Color to map each entry. Wouldn't this work? Can you elaborate on "giving [me] bad mapping"? I just want to understand how it works.
 
Why don't we wait and see if that's even the problem first? I'd be glad to try and explain it, but I don't claim to be an expert on it myself. I'm going on what I have run into in the past.
 
Okay, I'll try that. I'll get back to you.

By the way, thank you very much for your help! I've been spiraling in that problem for almost two weeks, and it's starting to drive me crazy.
 
Yes, when I first started using SDL I discovered that the developers wrote it (and the little documentation there is) based on the assumption that I knew what I was doing and how to do it. I thought I would lose my mind.
 

  last day (17 days later) »