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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

sk1v3r
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Sun Jan 09, 2011 1:36 pm

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

Post by sk1v3r »

can you post the link when you've uploaded :)
User avatar
civicdude95
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Mon Feb 21, 2011 9:55 am
Current Project: Prospectus
Favorite Gaming Platforms: PC
Programming Language of Choice: C++, C#, Java
Contact:

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

Post 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
sk1v3r
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Sun Jan 09, 2011 1:36 pm

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

Post 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
User avatar
civicdude95
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Mon Feb 21, 2011 9:55 am
Current Project: Prospectus
Favorite Gaming Platforms: PC
Programming Language of Choice: C++, C#, Java
Contact:

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

Post 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.
sk1v3r
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Sun Jan 09, 2011 1:36 pm

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

Post 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 :)
like80ninjas
Chaos Rift Regular
Chaos Rift Regular
Posts: 101
Joined: Thu Dec 09, 2010 2:13 am

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

Post 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.
sk1v3r
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Sun Jan 09, 2011 1:36 pm

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

Post 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 ... :/
User avatar
civicdude95
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Mon Feb 21, 2011 9:55 am
Current Project: Prospectus
Favorite Gaming Platforms: PC
Programming Language of Choice: C++, C#, Java
Contact:

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

Post by civicdude95 »

Hey guys thanks for helping me out with this problem. I'm glad we were finally able to figure it out.
like80ninjas
Chaos Rift Regular
Chaos Rift Regular
Posts: 101
Joined: Thu Dec 09, 2010 2:13 am

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

Post 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!
sk1v3r
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 25
Joined: Sun Jan 09, 2011 1:36 pm

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

Post by sk1v3r »

took a while, but hooray :)
User avatar
civicdude95
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 26
Joined: Mon Feb 21, 2011 9:55 am
Current Project: Prospectus
Favorite Gaming Platforms: PC
Programming Language of Choice: C++, C#, Java
Contact:

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

Post 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.
Post Reply