Jump to content
  • Advertisement
Sign in to follow this  
philipptr

problems with SDL_Blitsurface

This topic is 4843 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I started to code something for my 2d engine (c++ + sdl + ogl) which would convert images so that they are height==width==2^x and i can use them as textures. i try to blit the loaded surface on a surface that has the right height and width and then convert it to a texture. so my code looks like this:
	bige_texture &texture; //class which contains GLuint and size of the texture..

	SDL_Surface *temp, *temptext;
	SDL_Rect src; 

	if (temp = SDL_LoadBMP(filename))

	{	
		texture.width = temp->w;
		texture.height = temp->h;

		src.x=0;
		src.y=0;

		int pow=1;
		for (; !((pow>=temp->w)&&(pow>=temp->h)); pow=pow*2)
		{}

		src.w=temp->w;

		src.h=temp->h;

		cout << endl << pow << endl;
		temptext = SDL_CreateRGBSurface(SDL_SWSURFACE, pow, pow, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
		SDL_BlitSurface(temp, &src, temptext, NULL);
		cout << "blitting worked" << endl;
		glGenTextures(1, &texture.img);                                                                     

		glBindTexture(GL_TEXTURE_2D, texture.img);                                                          

		glTexImage2D(GL_TEXTURE_2D, 0, 3, temp->w, temp->h, 0, GL_BGR, GL_UNSIGNED_BYTE, temptext->pixels); 
		texture.size = pow;
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);  

		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,GL_NEAREST);  

		cout << "loading done;";

when i use this code it shows some flashy rgb lines but not what i actually want :( does someone know whats wrong, or should i post more code?

Share this post


Link to post
Share on other sites
Advertisement
seems like noone can help, so im gonna ask different:

how can i blit a surface onto another (bigger) one which has a specific background color (i only need black and white)?

Share this post


Link to post
Share on other sites
I think what you are missing is a simple SDL_FillRect on your temptext surface. After creating the surface the pixel data is uninitialized which explains the weird colored lines.

temptext = SDL_CreateRGBSurface(SDL_SWSURFACE, pow, pow, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
SDL_FillRect(temptext, 0, SDL_MapRGB(temptext->format, 0, 0, 0)); // black background
SDL_BlitSurface(temp, &src, temptext, NULL);

Share this post


Link to post
Share on other sites
thanks baumep, thats also i also found out that i had to use GL_RGBA instead of GL_BGR.
but still i have a problem. the texture is now looking strange because for example if the sprite had a width of 63 pixels the first pixel of the second row is taken to fill up the the last one for the 64 pixels wide texture.

in the tested case pow is 64 and the sprite is smaller:


temptext = SDL_CreateRGBSurface(SDL_SWSURFACE, pow, pow, 32, 0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000);
SDL_FillRect(temptext, 0, SDL_MapRGB(temptext->format, 0, 0, 0));
SDL_BlitSurface(temp, &src, temptext, 0);
glGenTextures(1, &texture.img);
glBindTexture(GL_TEXTURE_2D, texture.img);
glTexImage2D(GL_TEXTURE_2D, 0, 3, temp->w, temp->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, temptext->pixels);





EDIT: found the problem: should be "temptext->w, temptext->h" instead of "temp->w, temp->h" in last line :)

[Edited by - philipptr on August 16, 2005 6:44:48 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!