Jump to content
  • Advertisement
Sign in to follow this  
TheShadow

SDL beginner

This topic is 4433 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

Hi maybe this answer is stupid enough to not be answered but i am having trouble with the following code to load a bitmap and draw it into screen this line img = SDL_DisplayFormat(tmp_img); gives me back NULL (tmp_img is not null) i also tried passing tmp_img variable so that it would be the one drawn... but that doesnt help me either. Any help?
#include "SDL/SDL.h"


const int SCREEN_WIDTH = 640; const int SCREEN_HEIGHT = 480; const int SCREEN_BPP = 32;
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;

SDL_Surface* Eng_LoadImage(char* file)
{
	SDL_Surface *img = NULL;
	SDL_Surface *tmp_img = NULL;
	
	tmp_img = SDL_LoadBMP(file);
	
	if(tmp_img != NULL)
	{
		img = SDL_DisplayFormat(tmp_img);
		SDL_FreeSurface(tmp_img);
	}
	return img;
}

void Eng_DrawSurface(SDL_Surface *source, SDL_Surface *destination, int x, int y)
{
	SDL_Rect offset;
	offset.x = x;
	offset.y = y;
	
	SDL_BlitSurface( source, NULL, destination, &offset );
}

int main( int argc, char* args[] )
{
	//Start SDL
	SDL_Init( SDL_INIT_EVERYTHING );
	background = Eng_LoadImage("bugz.bmp");
	screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
	Eng_DrawSurface(screen, background, 0,0);
	SDL_Flip(screen);
	SDL_WM_SetCaption( "Hello         World", NULL );
	SDL_Delay(2000);
	
	//QuitSDL 
	SDL_Quit();
	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
SDL_DisplayFormat must know the display format of the main video screen. Hence you need to call it after SDL_SetVideoMode(...);

Share this post


Link to post
Share on other sites
Could it be that you are blitting from the screen to the loaded image?

See here:

void Eng_DrawSurface(SDL_Surface *source, SDL_Surface *destination, int x, int y);

Eng_DrawSurface(screen, background, 0,0);

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!