Advertisement

BMPs in SDL and byte order

Started by October 12, 2004 06:31 PM
11 comments, last by graveyard filla 19 years, 11 months ago
Are you sure your code isn't diffent? (or old versions of SDL/SDL_image maybe?)
I'm getting the same format out of loading BMPs and PNGs, using that conversion code.
Ok, I think I've fixed it. I never actually found the problem, because the behaviour seemed to reverse when I was creating a minimal test case to post up here. I think I was seeing a double error in my control case which led to it producing the right output for the wrong reason, which in turn confused my attempts to fix the actual problem code. Yeah, I didn't understand it either ;). I'll post back if I can't finish it off myself. Thanks all.
Advertisement
EDIT: doh, thats what i get for waiting 20 minutes before clicking reply... this might still be useful.

it was a real PITA to get every image to load in just the way it should... anyway, heres my image loading code, maybe it will help, it seems to work with all images.. note that it retains the alpha channel left over from the image. there was extra (tricky) work involved to do this. just call the Load_Texture() function, which uses Load_Image().

SDL_Surface* System::Load_Image(string image){	SDL_Surface *temp;  //the image we load in	SDL_Surface *temp2; //we copy temp into this surface using SDL_DisplayFormatAlpha 						//so its the same bpp as the screen and keeps its alpha channel	//first load in the image	temp = IMG_Load(image.c_str());		//if the image is NULL, we send a IMAGE_FAIL code along with the name of the image	// to the log file.. this will also terminate the program	if(!temp)	{		Log(image, IMAGE_FAIL);		return NULL;	}	else Log(image,IMAGE_OK);	//now lets copy the first image into temp2, so we convert the bpp and keep the alpha	temp2 = SDL_DisplayFormatAlpha(temp);		//free the original surface	SDL_FreeSurface(temp);	if(!temp2)	{		Log(image,IMAGE_FAIL);		return NULL;	}	//return the alpha one	return temp2;}GLuint System::Load_Texture(string image){	//temp texture to store the texture we will load in    GLuint texture;		//Create The Texture 	glGenTextures(1,&texture);	//load in the image	SDL_Surface *img = Load_Image(image);		//allow alpha blending on this new image. without this, we lose the alpha channel 	SDL_SetAlpha(img,0,0);						//create a new surface in RGB format. this is how all the images work in RGB format when calling glTexImage2D()	SDL_Surface *img2 = SDL_CreateRGBSurface(SDL_SWSURFACE,img->w,img->h,32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);	//blit the image we loaded in into our RGB surface    SDL_BlitSurface ( img, NULL , img2 , NULL );	//Typical Texture Generation Using Data From The Bitmap	glBindTexture(GL_TEXTURE_2D,texture);	 //Linear Filtering 	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	// Generate The Texture								//was GL_RGBA	glTexImage2D(GL_TEXTURE_2D, 0 , 4 , img2->w , img2->h, 0 ,GL_RGBA, GL_UNSIGNED_BYTE, img2->pixels);	//free our images and return our texture	SDL_FreeSurface(img);	SDL_FreeSurface(img2);	return texture;}
FTA, my 2D futuristic action MMORPG

This topic is closed to new replies.

Advertisement