SDL2 GL and RGBA PNG's

I’m porting my SDL 1.2 application to SDL 2.0 and have run into a problem with 32bit textures, 24bit textures load fine, but the 32bit ones don’t display on the screen… code bellow, thanks for any help in advance…

int SDL_GL_LoadTexture(struct textures *list, GLuint *texture, GLuint *texcount, GLfloat *texcoord, int *w, int *h, int *w2, int *h2)
{
SDL_Surface *tempSurf[100];
SDL_Surface *surface[100];
int d=0;
int x,y;
int tempW[100];
int tempH[100];
SDL_Rect area;
Uint32 saved_flags;
Uint8 saved_alpha;

// Load all surfaces in list
for(d=0;d<*(texcount);d++)
{
	tempSurf[d]=loadSurface(list[d].name);
	*(w+d)=0;
	*(h+d)=0;
	*(w+d) = tempSurf[d]->w;
	*(h+d) = tempSurf[d]->h;
	tempW[d]=0;
	tempH[d]=0;
	tempW[d]=pow2(*(w+d));
	tempH[d]=pow2(*(h+d));
	
	*(w2+d)=0;
	*(h2+d)=0;
	*(w2+d)=tempW[d];
	*(h2+d)=tempH[d];
}

for(d=0;d<*(texcount);d++)
{
	if ( tempSurf[d]->format->BytesPerPixel == 3 )
	{

	surface[d] = SDL_CreateRGBSurface(
			SDL_SWSURFACE,
			tempW[d], tempH[d],
			24,
	#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
				0x000000FF, 
				0x0000FF00, 
				0x00FF0000, 
				0xFF000000
	#else
				0xFF000000,
				0x00FF0000, 
				0x0000FF00, 
				0x000000FF
	#endif
		       );
	}else{
		surface[d] = SDL_CreateRGBSurface(
				SDL_SWSURFACE,
				tempW[d], tempH[d],
				32,
	#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks */
				0x000000FF, 
				0x0000FF00, 
				0x00FF0000, 
				0xFF000000
	#else
				0xFF000000,
				0x00FF0000, 
				0x0000FF00, 
				0x000000FF
	#endif
			       );

	}

/*
// Save the alpha blending attributes
saved_flags = tempSurf[d]->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
saved_alpha = tempSurf[d]->format->alpha;
if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
{
SDL_SetAlpha(tempSurf[d], 0, 0);
}*/

	SDL_GetSurfaceAlphaMod(tempSurf[d],&saved_alpha);
	SDL_SetSurfaceAlphaMod(tempSurf[d],0);
	SDL_BlitSurface(tempSurf[d], NULL, surface[d], NULL);
	SDL_SetSurfaceAlphaMod(tempSurf[d],saved_alpha);

/*
// Restore the alpha blending attributes
if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
{
SDL_SetAlpha(tempSurf[d], saved_flags, saved_alpha);
}*/

}

// Genorate and bind textures in list
glGenTextures(*(texcount), texture);
for(d=0;d<*(texcount);d++)
{
	glBindTexture(GL_TEXTURE_2D, *(texture+d));
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
	if ( surface[d]->format->BytesPerPixel == 3 )
	{
		glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,tempW[d],tempH[d],0,GL_RGB,GL_UNSIGNED_BYTE,surface[d]->pixels);
	}else{
		glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,tempW[d],tempH[d],0,GL_RGBA,GL_UNSIGNED_BYTE,surface[d]->pixels);
	}
	
}

for(d=0;d<*(texcount);d++)
{
	SDL_FreeSurface(tempSurf[d]);
	SDL_FreeSurface(surface[d]); 
}
return(0);

}

Maybe use 255 instead of 0 in SDL_SetSurfaceAlphaMod()?

Jonny DOn Fri, Mar 8, 2013 at 6:35 PM, image28 wrote:

**
I’m porting my SDL 1.2 application to SDL 2.0 and have run into a problem
with 32bit textures, 24bit textures load fine, but the 32bit ones don’t
display on the screen… code bellow, thanks for any help in advance…

int SDL_GL_LoadTexture(struct textures *list, GLuint *texture, GLuint
*texcount, GLfloat *texcoord, int *w, int *h, int *w2, int *h2)
{
SDL_Surface *tempSurf[100];
SDL_Surface *surface[100];
int d=0;
int x,y;
int tempW[100];
int tempH[100];
SDL_Rect area;
Uint32 saved_flags;
Uint8 saved_alpha;

// Load all surfaces in list
for(d=0;d<*(texcount);d++)
{
tempSurf[d]=loadSurface(list[d].name);
*(w+d)=0;
*(h+d)=0;
(w+d) = tempSurf[d]->w;
(h+d) = tempSurf[d]->h;
tempW[d]=0;
tempH[d]=0;
tempW[d]=pow2(
(w+d));
tempH[d]=pow2(
(h+d));

*(w2+d)=0;
*(h2+d)=0;
*(w2+d)=tempW[d];
*(h2+d)=tempH[d];
}

for(d=0;d<*(texcount);d++)
{
if ( tempSurf[d]->format->BytesPerPixel == 3 )
{

surface[d] = SDL_CreateRGBSurface(
SDL_SWSURFACE,
tempW[d], tempH[d],
24,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN /* OpenGL RGBA masks /
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000
#else
0xFF000000,
0x00FF0000,
0x0000FF00,
0x000000FF
#endif
);
}else{
surface[d] = SDL_CreateRGBSurface(
SDL_SWSURFACE,
tempW[d], tempH[d],
32,
#if SDL_BYTEORDER == SDL_LIL_ENDIAN /
OpenGL RGBA masks */
0x000000FF,
0x0000FF00,
0x00FF0000,
0xFF000000
#else
0xFF000000,
0x00FF0000,
0x0000FF00,
0x000000FF
#endif
);

}
/*
// Save the alpha blending attributes
saved_flags = tempSurf[d]->flags&(SDL_SRCALPHA|SDL_RLEACCELOK);
saved_alpha = tempSurf[d]->format->alpha;
if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
{
SDL_SetAlpha(tempSurf[d], 0, 0);
}*/

SDL_GetSurfaceAlphaMod(tempSurf[d],&saved_alpha);
SDL_SetSurfaceAlphaMod(tempSurf[d],0);
SDL_BlitSurface(tempSurf[d], NULL, surface[d], NULL);
SDL_SetSurfaceAlphaMod(tempSurf[d],saved_alpha);
/*
// Restore the alpha blending attributes
if ( (saved_flags & SDL_SRCALPHA) == SDL_SRCALPHA )
{
SDL_SetAlpha(tempSurf[d], saved_flags, saved_alpha);
}*/

}

// Genorate and bind textures in list
glGenTextures((texcount), texture);
for(d=0;d<
(texcount);d++)
{
glBindTexture(GL_TEXTURE_2D, *(texture+d));
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
if ( surface[d]->format->BytesPerPixel == 3 )
{

glTexImage2D(GL_TEXTURE_2D,0,GL_RGB8,tempW[d],tempH[d],0,GL_RGB,GL_UNSIGNED_BYTE,surface[d]->pixels);
}else{

glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8,tempW[d],tempH[d],0,GL_RGBA,GL_UNSIGNED_BYTE,surface[d]->pixels);
}

}

for(d=0;d<*(texcount);d++)
{
SDL_FreeSurface(tempSurf[d]);
SDL_FreeSurface(surface[d]);
}
return(0);
}


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Didn’t seem to change anything, still no images on the screen ( except for the RGB images )

How about using SDL_Get/SetSurfaceBlendMode() similar to how you’re using
SDL_SetSurfaceAlphaMod()?
e.g. before you blit: SDL_SetSurfaceBlendMode(tempSurf[d],
SDL_BLENDMODE_NONE);

Jonny DOn Fri, Mar 8, 2013 at 9:27 PM, image28 wrote:

**
Didn’t seem to change anything, still no images on the screen ( except for
the RGB images )


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

That worked perfectly… Thanks a lot

Would give you a free copy of my game for helping me, but It doesn’t allow private messages…

Oh, the forum? Yeah, I’m using the mailing list. I wouldn’t mind taking a
look at the game. :slight_smile:
@Jonathan_DearbornOn Fri, Mar 8, 2013 at 10:05 PM, image28 wrote:

**
Would give you a free copy of my game for helping me, but It doesn’t allow
private messages…


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

You can get the demo at http://www.ckmgaming.com runs on windows/linux/mac …

If you use twitter send me a message @CKMGaming and I message you with a unique download link to the full game…