Applying image to screen with SDL.

C++, C#, Java, PHP, ect...
Post Reply
Shihonoryu
Posts: 255
Joined: Sun Dec 12, 2010 7:30 am

Applying image to screen with SDL.

Post by Shihonoryu »

Im just getting a black screen...yes img is saved in same folder, and the type is BMP.

Code: Select all

#include <sdl.h>
#include <iostream>
#include <string>

SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
bool running = true;

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect rect;
rect.y;
rect.x;

SDL_BlitSurface(source,NULL,destination,&rect);

}

int main(int argc, char* argv[])
{
	SDL_Init(SDL_INIT_VIDEO);
	screen = SDL_SetVideoMode(800,800,32,SDL_SWSURFACE);
	SDL_WM_SetCaption("Blood Duel",NULL);
	background = SDL_LoadBMP("background");
	while (running == true)
	{

	SDL_Event event;

	if(SDL_PollEvent(&event))
	{
		if (event.type == SDL_QUIT)
			{
			running = false;

			}// end the quit if statement

	}// End poll event

	apply_surface(0,0,background,screen);
	SDL_Flip(screen);

}//end running Loop
	SDL_Quit();
	return 0;
}//end main
User avatar
Sakar
Posts: 520
Joined: Thu Apr 23, 2009 2:59 am

Re: Applying image to screen with SDL.

Post by Sakar »

I revised your code and put in some comments:

Code: Select all

#include <sdl.h>
#include <iostream>
#include <string>

SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
bool running = true;

void apply_surface(int x, int y, SDL_Surface* source, SDL_Surface* destination)
{
SDL_Rect rect;
rect.y = y;  // You need to set the x and y for the rect. You didn't do that
rect.x = x;  

SDL_BlitSurface(source,NULL,destination,&rect);

}

int main(int argc, char* argv[])
{
   SDL_Init(SDL_INIT_EVERYTHING); // Should be SDL_INIT_EVERYTHING, not SDL_INIT_VIDEO
   screen = SDL_SetVideoMode(800,800,32,SDL_SWSURFACE);
   SDL_WM_SetCaption("Blood Duel",NULL);
   background = SDL_LoadBMP("background.bmp"); // You need to include the file extension i.e. background.bmp, not background
   while (running == true)
   {

   SDL_Event event;

   if(SDL_PollEvent(&event))
   {
      if (event.type == SDL_QUIT)
         {
         running = false;

         }// end the quit if statement

   }// End poll event

   apply_surface(0,0,background,screen);
   SDL_Flip(screen);

}//end running Loop
   SDL_FreeSurface(background); // ALWAYS free the surface when you are done with it. C++ requires you to free all memory you allocate
   SDL_Quit();
   return 0;
}//end main
The main issue is that you didn't include the file extension. That is required to load the image.
Also you forgot to free the background surface (screen will automatically be freed by SDL)
And you didn't assign the x and y values to the rect in apply_surface()

Might want to take a closer look at the LazyFoo source code ;)
Shihonoryu
Posts: 255
Joined: Sun Dec 12, 2010 7:30 am

Re: Applying image to screen with SDL.

Post by Shihonoryu »

Heh i didn't use lazyfoo because i tried his and had the same problem. and im still getting just black with your code.

I have the pic in the same folder as the code...
Image
User avatar
Sakar
Posts: 520
Joined: Thu Apr 23, 2009 2:59 am

Re: Applying image to screen with SDL.

Post by Sakar »

The image needs to be in the same folder as the executable.

File paths are always relevant to the executable.
Post Reply

Return to “Coding”