loading a .bmp file into SDL

Anything related in any way to game development as a whole is welcome here. Tell us about your game, grace us with your project, show us your new YouTube video, etc.

Moderator: PC Supremacists

azaron08
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 40
Joined: Sun May 27, 2012 12:50 pm

loading a .bmp file into SDL

Post 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
User avatar
lalacomun
VS Setup Wizard
VS Setup Wizard
Posts: 114
Joined: Wed Dec 28, 2011 10:18 pm
Favorite Gaming Platforms: psx, nintendo ds, gameboy advance, xbox 360, ps2
Programming Language of Choice: C++
Location: Argentina
Contact:

Re: loading a .bmp file into SDL

Post 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
}
Image
User avatar
superLED
Chaos Rift Junior
Chaos Rift Junior
Posts: 303
Joined: Sun Nov 21, 2010 10:56 am
Current Project: Engine
Favorite Gaming Platforms: N64
Programming Language of Choice: C++, PHP
Location: Norway

Re: loading a .bmp file into SDL

Post 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.
azaron08
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 40
Joined: Sun May 27, 2012 12:50 pm

Re: loading a .bmp file into SDL

Post by azaron08 »

Thank you so much and ive been to lazyfoo I just don't understand some of it!
azaron08
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 40
Joined: Sun May 27, 2012 12:50 pm

Re: loading a .bmp file into SDL

Post 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
:(
User avatar
superLED
Chaos Rift Junior
Chaos Rift Junior
Posts: 303
Joined: Sun Nov 21, 2010 10:56 am
Current Project: Engine
Favorite Gaming Platforms: N64
Programming Language of Choice: C++, PHP
Location: Norway

Re: loading a .bmp file into SDL

Post 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.
azaron08
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 40
Joined: Sun May 27, 2012 12:50 pm

Re: loading a .bmp file into SDL

Post 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??
User avatar
superLED
Chaos Rift Junior
Chaos Rift Junior
Posts: 303
Joined: Sun Nov 21, 2010 10:56 am
Current Project: Engine
Favorite Gaming Platforms: N64
Programming Language of Choice: C++, PHP
Location: Norway

Re: loading a .bmp file into SDL

Post 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)
azaron08
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 40
Joined: Sun May 27, 2012 12:50 pm

Re: loading a .bmp file into SDL

Post by azaron08 »

how do i save it as a .bmp file? just name it "image.bmp"?
User avatar
superLED
Chaos Rift Junior
Chaos Rift Junior
Posts: 303
Joined: Sun Nov 21, 2010 10:56 am
Current Project: Engine
Favorite Gaming Platforms: N64
Programming Language of Choice: C++, PHP
Location: Norway

Re: loading a .bmp file into SDL

Post 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.
azaron08
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 40
Joined: Sun May 27, 2012 12:50 pm

Re: loading a .bmp file into SDL

Post by azaron08 »

k ill c wht i can do.....and skype isnt work n for me any other options???
Last edited by azaron08 on Mon Jun 04, 2012 7:10 pm, edited 1 time in total.
azaron08
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 40
Joined: Sun May 27, 2012 12:50 pm

Re: loading a .bmp file into SDL

Post 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:
azaron08
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 40
Joined: Sun May 27, 2012 12:50 pm

Re: loading a .bmp file into SDL

Post 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;
}
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: loading a .bmp file into SDL

Post 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."
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
azaron08
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 40
Joined: Sun May 27, 2012 12:50 pm

Re: loading a .bmp file into SDL

Post by azaron08 »

Wht the h*ll does that mean
Post Reply