Jump to content
  • Advertisement
Sign in to follow this  
logank9

Drawing a .bmp in C++ (SDL)

This topic is 3324 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 just started out in C++ and I am trying to apply a background to a C++ window. (bg.bmp) (I followed the tutorial from Lazyfoo) Here's my code:
#include "SDL/SDL.h" // sdl library
#include <string> // string

//screen stuff
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BPP = 32;

SDL_Surface *background = NULL;
SDL_Surface *screen = NULL;

SDL_Surface *load_image( std::string filename )
{
	SDL_Surface* loadedimage = NULL; // image that is loaded
	SDL_Surface* optimizedimage = NULL; // image that will be used
	
	load_image = SDL_LoadBMP( filename.c_str() ); // load the image
	
	if(loadedimage != NULL) // if the image loaded correct
	{
		optimizedimage = SDL_DisplayFormat( loadedimage ); // create the image to use
		
		SDL_FreeSurface( loadedimage ); //free old image
	}
	
	return optimizedimage; // return image to use
}

void apply_surface( int x, int y, SDL_Surface* source, SDL_Surface* destination )
{
	SDL_Rect offset;
	
	offset.x = x;
	offset.y = y;
	
	//blit the surface
	SDL_BlitSurface( source, NULL, destination, &offset );
}

int main( int argc, char* args[] )
{
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
	{
		return 1;
	}
	
	screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
	
	if(screen==NULL)
	{
		return 1;
	}
	
	SDL_WM_SetCaption( "SDL IMAGE TEST", NULL );
	
	background = load_image( "bg.bmp" );
	apply_surface( 0, 0, background, screen );
	
	if(SDL_Flip(screen) == -1)
	{
		return 1;
	}
	
	SDL_Delay(5000);
	
	SDL_FreeSurface(background);
	
	SDL_Quit();
	
	return 0;
}




When I compile I get the error:
main.cpp: In function ‘SDL_Surface* load_image(std::string)’:
main.cpp:17: error: assignment of function ‘SDL_Surface* load_image(std::string)’
main.cpp:17: error: cannot convert ‘SDL_Surface*’ to ‘SDL_Surface* ()(std::string)’ in assignment




Judging by this it means it doesn't recognize the string library? I'm not sure. If anyone could help, I'd be very grateful!

Share this post


Link to post
Share on other sites
Advertisement
In this line:

load_image = SDL_LoadBMP( filename.c_str() ); // load the image

you probably actually wanted loadedimage not load_image.

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!