Jump to content
  • Advertisement
Sign in to follow this  
dolphins87

SDL help

This topic is 4238 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 am doing the SDL tutorials on lazyfoo.net and I am doing tutorial 5 and of course he dosent write out the whole source file everytime he does a tutorial and I try to write what he has for the tutorials and I write the rest from what I have learned and I incoporate the rest from his tutroails. Now I am on tutorial 5 where you are using the blitting and I can get the program to compile good but the images arnt loading and I cant figure out why. I have checked the directory and where they go and everything and even compared my code to his source code but cant figure out the problem. Can anyone tell me why this wouldnt be working. Thank's
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include <string>

// screen surfaces
SDL_Surface *screen = NULL;
SDL_Surface *background = NULL;
SDL_Surface *image = NULL;

// SDL event
SDL_Event event;

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

bool init()
{
	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("Blit Demo",NULL);

	return true;
}

void CleanUp()
{
	// free the surface
	SDL_FreeSurface(image);
	SDL_FreeSurface(background);

	// quit SDL
	SDL_Quit();
}

SDL_Surface *load_image(std::string filename)
{
	// the image is loaded
	SDL_Surface *loadedImage = NULL;

	// the optimized image
	SDL_Surface *optimizedImage = NULL;

	// load the image
	loadedImage = IMG_Load(filename.c_str());

	if(loadedImage != NULL)
	{
		optimizedImage = SDL_DisplayFormat(loadedImage);

		// free the old image
		SDL_FreeSurface(loadedImage);

		// if image was optimized just fine
		if(optimizedImage != NULL)
		{
			// map the color key
			Uint32 colorkey = SDL_MapRGB(optimizedImage->format,0,0xFF,0xFF);

			// set all pixels of color r 0, G 0xFF, B 0xFF to be transparent
			SDL_SetColorKey(optimizedImage,SDL_RLEACCEL | SDL_SRCCOLORKEY ,colorkey);
		}
	}
	// return optimizedImage
	return optimizedImage;
}

void apply_surface(int x, int y, SDL_Surface *destination, SDL_Surface *source)
{
	// SDL_Rect
	SDL_Rect clip;

	clip.x = x;
	clip.y = y;

	SDL_BlitSurface(source,NULL,destination,&clip);
}

bool load_files()
{
	background = load_image("background.png");

	if(background == NULL)
	{
		return false;
	}

	image = load_image("foo.png");

	if(image == NULL)
	{
		return false;
	}

	return true;
}
int main(int argc, char *argv[])
{
	bool quit = false;

	if(init() == false)
	{
		return 1;
	}

	if(load_files() == false)
	{
		return 1;
	}

	apply_surface(0,0,background,screen);
	apply_surface(240,190,image,screen);

	// update the screen
	if(SDL_Flip(screen) == -1)
	{
		return 1;
	}

	while(quit == false)
	{
		while(SDL_PollEvent(&event))
		{
			if(event.type == SDL_QUIT)
			{
				quit = true;
			}
		}
	}

	// clean up
	CleanUp();

	// return 1;
	return 0;
}

Share this post


Link to post
Share on other sites
Advertisement
nevermind I figured it out in my apply surface function I was using the source as the destination and destination as the source wow that was a dumb mistake.

Share this post


Link to post
Share on other sites
At the bottom of every tutorial, there is a "Download the media and source code for this tutorial here." link ;-).

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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!