Jump to content
  • Advertisement
Sign in to follow this  
BlueBan007

SDL text color changing help

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

ok Im getting a black screen when I do this... I have the main loop:
int main(int argc, char * args[]){

	//SDL init
		screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );
		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( "BreakOut!", NULL );
		if( TTF_Init() == -1 ) { return false; } 
	//
		//game init
		MODE_::MODE_();
		OPTIONS_::OPTIONS_();
		//

			while(event.type!=SDL_QUIT){

				font=TTF_OpenFont( "Mario and Luigi.ttf", 28 );
				if(font==NULL){return 1;}

				if( SDL_PollEvent( &event ) ){
				if(event.type==SDL_KEYDOWN)
					if(event.key.keysym.sym==SDLK_DOWN){
						OPTIONS_::TextColor("green");
					}
				}				

			message = TTF_RenderText_Solid( font, "Breakout!", textColor );
			if( message == NULL ) { return 1; } 
			
			apply_surface(50,50,message,screen);


					//quit checking
					if(checkQuit()==0)
						if( SDL_Flip( screen ) == -1 ) { return 1; }
			}

return 0;
}



and Options.h:


class OPTIONS_{

public:

	OPTIONS_();
	static void TextColor(std::string newcolor);

};



OPTIONS_::OPTIONS_(){

SDL_Color textColor = {255,255,255};

}

void OPTIONS_::TextColor(std::string newcolor){

	if(newcolor=="green")
		SDL_Color textColor={35,245,33};

}


and text should be up in white per the constructor, and keypress down should change it to green per the TextColor(newcolor) function..help pls! [Edited by - BlueBan007 on May 21, 2009 6:59:59 AM]

Share this post


Link to post
Share on other sites
Advertisement
Hello Blue Ban007,

actually I wonder where the variable textColor in your main loop is defined as
in Options.h the variable textColor is only local in both OPTIONS_::OPTIONS_() and OPTIONS_::TextColor(). As it is local, its value will be lost once the functions end, therefore it is not changed to green.

What you want to do is to add a variable of type SDL_Color to your OPTIONS_ class and set this in the constructor and the TextColor () function. You will have to pass also this variable to TTF_RenderText_Solid (...).

BTW: In your main loop you are loading the font each time the loop is executed. You probably want to put
font=TTF_OpenFont( "Mario and Luigi.ttf", 28 );
if(font==NULL){return 1;}

above
while(event.type!=SDL_QUIT) {


Cheers,
fng

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!