SDL text color changing help

Started by
0 comments, last by fng 14 years, 11 months ago
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]
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
-- blog: www.fysx.org

This topic is closed to new replies.

Advertisement