Page 1 of 3

loading a .bmp file into SDL

Posted: Thu May 31, 2012 8:18 pm
by azaron08
Hello,
I need help. Ive been looking at tons of tutorials and searching on google for ways to get
pictures into my project in sld.....and need to learn it to continue with my tutorials!!! I wld like a really detailed answear but any help wanted!!! Also I'm looking for a more professional programmer that might be up to giving me some guidence if anyone would be intresed!!

Thank you

Re: loading a .bmp file into SDL

Posted: Thu May 31, 2012 8:39 pm
by lalacomun
so you want to load a BMP image into your game using SDL ?? if so yo should use the following function:

SDL_LoadBMP("location of file.bmp");

here, a code on how to load, and blit an image :

Code: Select all

SDL_Surface *image;
SDL_Surface *screen;
int main(int argc,char *argv[])
{
   ...//initialize and other crap
   screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE); 
   image = SDL_LoadBMP("LocationOfFile.bmp"); // load the image
   SDL_BlitSurface(player,NULL,screen,NULL); // use this function to blit the image onto you screen
   SDL_Flip(screen); //you need to flip your buffer so you can actually see something
  ..//rest of crap
}

Re: loading a .bmp file into SDL

Posted: Thu May 31, 2012 8:50 pm
by superLED
That's why we have www.lazyfoo.net
Check it out. I guess that page has helped most of us on the forum, that have walked the path of SDL.

Re: loading a .bmp file into SDL

Posted: Thu May 31, 2012 9:06 pm
by azaron08
Thank you so much and ive been to lazyfoo I just don't understand some of it!

Re: loading a .bmp file into SDL

Posted: Mon Jun 04, 2012 2:30 pm
by azaron08
also do i need to save my file or picture any where and if so where???? i already had the format shown above but it still only shows a black screen ....... idk wht im doing wrong but i never get an error
:(

Re: loading a .bmp file into SDL

Posted: Mon Jun 04, 2012 3:56 pm
by superLED
If you save it in the directory where you .exe file is, just load it like this: "image.bmp"
If you have a folder for you images (and the image folder is in the same directory as the .exe file), load it like this: "img_folder/image.bmp"

Remember to have the same set-up (folders and files) in the Debug/Release and the folder you have your project in.

Re: loading a .bmp file into SDL

Posted: Mon Jun 04, 2012 4:04 pm
by azaron08
so the file directory is that were the all the files are or inside of the the one project file........ive tried both but i wasnt for sure and th "image.bmp" does that mean i save the file as image? or image.bmp??

Re: loading a .bmp file into SDL

Posted: Mon Jun 04, 2012 4:13 pm
by superLED
azaron08 wrote:so the file directory is that were the all the files are or inside of the the one project file........ive tried both but i wasnt for sure and th "image.bmp" does that mean i save the file as image? or image.bmp??
No, you can name it as you like. But if you are trying to load up a BMP file, you'd better off saving it as a .bmp file. "whatever.bmp"

If you want to load up .png files or .jpg files, you should look into SDL_image (it's pretty simple to add, and you would only change the Load_BMP function to IMG_Load or something)

Re: loading a .bmp file into SDL

Posted: Mon Jun 04, 2012 4:26 pm
by azaron08
how do i save it as a .bmp file? just name it "image.bmp"?

Re: loading a .bmp file into SDL

Posted: Mon Jun 04, 2012 5:38 pm
by superLED
When you are done drawing your image, click on File -> Save As -> type in a name for your image, and chose ".bmp" as the desired filetype.

Re: loading a .bmp file into SDL

Posted: Mon Jun 04, 2012 5:42 pm
by azaron08
k ill c wht i can do.....and skype isnt work n for me any other options???

Re: loading a .bmp file into SDL

Posted: Mon Jun 04, 2012 5:48 pm
by azaron08
i cant save as just a bmp i can only save it as a "24-bit Bitmap (*.bmp;*.dib) is that it???? :oops:

Re: loading a .bmp file into SDL

Posted: Mon Jun 04, 2012 6:02 pm
by azaron08
this is what i have and all it does is load a black screen:

Code: Select all

#include <SDL.h>
#include <string>

int main( int argc,char* args[] )
{
	//initilization,setup screen,running screen
	SDL_Init(SDL_INIT_EVERYTHING);
	SDL_Surface*screen;
	SDL_Surface*image;
	screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
	bool running=true;
	const int FPS=30;
	Uint32 start;
	bool b[4]={0,0,0,0};
	SDL_Rect rect;
	rect.x=10;
	rect.y=10;
	rect.w=20;
	rect.h=20;
	Uint32 color=SDL_MapRGB(screen->format,0x00,0x00,0x00);
	Uint32 color2=SDL_MapRGB(screen->format,0xff,0x00,0x00);
	image=SDL_LoadBMP("bmp.bmp");
	while (running)
	{ 
		start=SDL_GetTicks();
		SDL_Event event;
		while(SDL_PollEvent(&event))
		{
			switch(event.type)
			{
			case SDL_QUIT:
				running=false;
				break;
			case SDL_KEYDOWN:
				switch(event.key.keysym.sym)
				{
				case SDLK_UP:
					b[0]=1;
					break;
				case SDLK_DOWN:
					b[1]=1;
					break;
				case SDLK_LEFT:
					b[2]=1;
					break;
				case SDLK_RIGHT:
					b[3]=1;
					break;

				}
				break;
				case SDL_KEYUP:
				switch(event.key.keysym.sym)
				{
				case SDLK_UP:
					b[0]=0;
					break;
				case SDLK_DOWN:
					b[1]=0;
					break;
				case SDLK_LEFT:
					b[2]=0;
					break;
				case SDLK_RIGHT:
					b[3]=0;
					break;


				}
				break;

			}
		}
		//logic&& render

		if(b[0])
			rect.y--;
		if(b[1])
			rect.y++;
		if(b[2])
			rect.x--;
		if(b[3])
			rect.x++;


		
		SDL_FillRect(screen,&screen->clip_rect,color);
		//SDL_FillRect(screen,&rect,color2);
		SDL_BlitSurface(image,NULL,screen,NULL);
		SDL_Flip(screen);
		if(1000/FPS>SDL_GetTicks()-start)
			SDL_Delay(1000/FPS-(SDL_GetTicks()-start));



	}
		SDL_Quit();
		return 0;
}

Re: loading a .bmp file into SDL

Posted: Mon Jun 04, 2012 6:46 pm
by dandymcgee
azaron08 wrote:k ill c wht i can do.....and skype isnt work n for me any other potions???
"Longbottom, at the end of this lesson we will feed a few drops of this potion to your toad and see that happens. Perhaps that will encourage you to do it properly."

Re: loading a .bmp file into SDL

Posted: Mon Jun 04, 2012 7:08 pm
by azaron08
Wht the h*ll does that mean