Page 2 of 2

Re: Need help with c++/sdl and drawing

Posted: Sun Feb 27, 2011 9:03 am
by sk1v3r
can you post the link when you've uploaded :)

Re: Need help with c++/sdl and drawing

Posted: Sun Feb 27, 2011 9:38 am
by civicdude95
Sure I'm putting in my Google docs account.

Here is the link for the zip
https://docs.google.com/leaf?id=0B-7lPg ... y=CMSKkpQF

Re: Need help with c++/sdl and drawing

Posted: Sun Feb 27, 2011 10:02 am
by sk1v3r
I am not quite sure why this is happening because the top that goes missing is blue not pink, but removing the colour key that you set in your image load function lets the program pritn out the top of the screen as it should

Re: Need help with c++/sdl and drawing

Posted: Sun Feb 27, 2011 1:03 pm
by civicdude95
Wow, that's weird. What in the world would cause that to happen? Is there another way to set the transparency color that would make it work properly? I have not ever seen this problem before

*EDIT*
Ok, so I modified my LoadImage() method to be like the following:

Code: Select all

SDL_Surface* Game::LoadImage(const char *filename, bool hasTrans)
{
	SDL_Surface *image = IMG_Load(filename);
	if (image == NULL)
	{
		std::cerr << "IMG_Load() Failed: " << SDL_GetError() << std::endl;
		exit(1);
	}
		
	if (hasTrans)
	{
		Uint32 colorkey = SDL_MapRGB(image->format, 0xFF, 0, 0xFF);
	
		SDL_SetColorKey(image, SDL_SRCCOLORKEY, colorkey);
	}

	return image;
}
This may be a hack-ish way to solve the problem but it works for this game at least.

Re: Need help with c++/sdl and drawing

Posted: Sun Feb 27, 2011 1:35 pm
by sk1v3r
Well, you are using PNG's so just go into gimp(free) or photoshop(not free) and add an alpha channel. Alpha channels are transparent, so you can draw over them and it will be seen, but if you don't wnat to draw over a bit it will be invisible.
http://www.sdltutorials.com/sdl-image/
This tutorial shows what code you need to do to get it to work, its easier than other ways of doing it :)

Re: Need help with c++/sdl and drawing

Posted: Sun Feb 27, 2011 1:44 pm
by like80ninjas
I don't use alpha in my engine, I just color mask (255,0,255), however, you have to make sure that it is exactly that color. Also, look up LazyFoo's tutorial on optimizing loaded images.

Code: Select all

void CResourceManager::LoadImage( std::string filename )
{
	//Surface for loading image
    SDL_Surface* load_image = NULL;
    
    //Second surface for optimizing
    SDL_Surface* optim_image = NULL;
    
    //Load the image ( requires SDL_image)
    load_image = IMG_Load( filename.c_str() );
    
    //If the image loaded
    if( load_image != NULL )
    {
        //Create an optimized image
        optim_image = SDL_DisplayFormat( load_image );
        
        //Free the old image
        SDL_FreeSurface( load_image );
    }
    Uint32 colorkey = SDL_MapRGB( optim_image->format, 255, 0, 255 );
	SDL_SetColorKey( optim_Image, SDL_SRCCOLORKEY, colorkey );

     //HERE YOU COULD RETURN THE IMAGE, BUT I ADD IT TO AN ARRAY

    image[image_count] = optim_image;

	cout << "Loaded Image: " << filename << " with image number " << image_count << ".\n";
	image_count++;
	cout << SDL_GetError();
}
That's how I do it in my engine.

It's roughly the same as how lazyfoo does it.

Re: Need help with c++/sdl and drawing

Posted: Sun Feb 27, 2011 2:42 pm
by sk1v3r
That looks nice :)
The reason why I was telling him to use alpha was that even though the bit that he wanted to show was blue and the colour key was pink, it still didn't show the blue... that kinda stumped me. Alpha would fix that short term, but do you know of this error/glitch happening before? because I haven't and it confused me ... :/

Re: Need help with c++/sdl and drawing

Posted: Sun Feb 27, 2011 7:30 pm
by civicdude95
Hey guys thanks for helping me out with this problem. I'm glad we were finally able to figure it out.

Re: Need help with c++/sdl and drawing

Posted: Sun Feb 27, 2011 11:10 pm
by like80ninjas
I really have no clue why it wouldn't output blue, i'm assuming it was an error loading the image or something, but he say's it's solved so I guess he's good to go. Change the topic to [Solved] man!

Re: [Solved] Need help with c++/sdl and drawing

Posted: Mon Feb 28, 2011 12:44 am
by sk1v3r
took a while, but hooray :)

Re: [Solved] Need help with c++/sdl and drawing

Posted: Mon Feb 28, 2011 1:30 pm
by civicdude95
Now to continue with the separation of the code. I believe I'm going to end up with a handful of classes: Game, Entity -> Player/Pill, InputHandler, Timer are the main ones that I have thought about implementing. I can't think of any more at the moment.