Changing Color Of Text With Input

Hello,

I have a menu where I would like the selected text to be changed to the color yellow from the color white. Basiclly it consists of “New Game”, “Game Options”, and “Quit Game”. I want it to start at “New Game”, when “New Game” is selected, the color of the text should yellow. However when the user pressed down, “Game Options” should be selected and that will turn from white to yellow, while “New Game” reverts back to the white color. How would I go about programming this? I’ll post my code in case that helps.

mainvars.h

Code:

const int MAX_WIDTH = 640;
const int MAX_HEIGHT = 480;
const int MAX_DEPTH = 32;
const char* szTitle = “Convict Paradise - DarkScar Games”;

SDL_Surface* gScreen = NULL;
SDL_Event event;
float deltaTime = 0.0f;
int ClockMins = 0;
int ClockSecs = 0;
TTF_Font* Font = NULL;
Uint8* keyStates = SDL_GetKeyState(NULL);

SDL_Surface* LogoImg;
SDL_Rect LogoDest;

SDL_Surface* TitleImg;
SDL_Rect TitleDest;

SDL_Surface* GameClockImg;
SDL_Rect GameClockRect;
SDL_Rect GameClockDest;

SDL_Surface* HealthBarImg[2];
SDL_Rect HealthBarRect[2];
SDL_Rect HealthBarDest[2];

SDL_Surface* GameMenuText[3];
SDL_Rect GameMenuDest[3];

SDL_Color TextColor = {255,255,255}; //white
SDL_Color BackColor = {0,0,255}; //blue
SDL_Color SelectedText = {255,255,0}; //yellow

int x_menu = 250;
int y_menu = 100;

int mouse_x = 0;
int mouse_y = 0;

bool Blood_Enabled = true;
bool Sound_Enabled = true;
float sound_level = 100.0f;
int sound_counter = 0;

bool LogoScreen = true;
bool TitleScreen = false;
bool PlayerSelectScreen = false;
bool GameScreen = false;
bool OptionScreen = false;
bool IntroScreen = false;
bool EndScene = false;

bool GameRunning = true;

void Cleanup()
{
SDL_FreeSurface(LogoImg);
SDL_FreeSurface(TitleImg);
SDL_FreeSurface(GameMenuText[0]);
SDL_FreeSurface(GameMenuText[1]);
SDL_FreeSurface(GameMenuText[2]);
}

Main.cpp

Code:

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <wchar.h>
#include <time.h>
#include <GL/GL.h>
#include
#include
#include

#include “SDL.h”
#include “SDL_image.h”
#include “SDL_mixer.h”
#include “SDL_ttf.h”

#include “mainvars.h”
#include “convicts.h”

using namespace std;

int main(int argc, char* argv[])
{
/////////////////////////////////
LogoImg = IMG_Load(“Logo.png”);
LogoDest.x = 0;
LogoDest.y = 0;
LogoDest.w = MAX_WIDTH;
LogoDest.h = MAX_HEIGHT;

TitleImg = IMG_Load("Title.png");
TitleDest.x = 0;
TitleDest.y = 0;
TitleDest.w = MAX_WIDTH;
TitleDest.h = MAX_HEIGHT;
////////////////////////////////

///////////////////////////////
Johnny sJohnny;
Johnny *pJohnny;
pJohnny = &sJohnny;

pJohnny->ihealth = 100;
pJohnny->imax_health = pJohnny->ihealth;
pJohnny->istrength = 15;
pJohnny->idefense = 10;
pJohnny->fspeed = 20.f;
pJohnny->szName = "Johnny";
pJohnny->ipoints = 0;
pJohnny->ikills = 0;
pJohnny->iwins = 0;
pJohnny->ilosts = 0;
///////////////////////////////
if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
{
    return 1;
}

gScreen = SDL_SetVideoMode(MAX_WIDTH,MAX_HEIGHT,MAX_DEPTH, SDL_HWSURFACE | SDL_DOUBLEBUF);

if(gScreen == NULL)
{
    return 1;
}

SDL_WM_SetCaption(szTitle, NULL);

if(TTF_Init() == -1)
{
    return 1;
}

Font = TTF_OpenFont("ARIAL.ttf",25);

///////////////////////////////////
GameMenuText[0] = TTF_RenderText_Solid(Font,"New Game",TextColor);
GameMenuText[1] = TTF_RenderText_Solid(Font,"Game Options",TextColor);
GameMenuText[2] = TTF_RenderText_Solid(Font,"Quit Game",TextColor);

GameMenuDest[0].x = 250;
GameMenuDest[0].y = 100;
GameMenuDest[0].w = 0;
GameMenuDest[0].h = 0;

GameMenuDest[1].x = 250;
GameMenuDest[1].y = 150;
GameMenuDest[1].w = 0;
GameMenuDest[1].h = 0;

GameMenuDest[2].x = 250;
GameMenuDest[2].y = 200;
GameMenuDest[2].w = 0;
GameMenuDest[2].h = 0;
//////////////////////////////////

if(Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096) == -1)
{
    return 1;
}

while(GameRunning)
{
    while(SDL_PollEvent(&event))
    {
        if(event.type == SDL_QUIT)
        {
            GameRunning = false;
        }
        
        if(event.type == SDL_KEYDOWN)
        {
            if(keyStates[SDLK_ESCAPE])
            {
                GameRunning = false;
            }
            
            if(keyStates[SDLK_RETURN] && LogoScreen == true)
            {
                LogoScreen = false;
                SDL_FillRect(gScreen,NULL,SDL_MapRGB(gScreen->format,0,0,0));
                TitleScreen = true;
            }
            
            if(keyStates[SDLK_RETURN] && x_menu == GameMenuDest[0].x && y_menu == GameMenuDest[0].y && TitleScreen == true)
            {
                TitleScreen = false;
                SDL_FillRect(gScreen,NULL,SDL_MapRGB(gScreen->format,0,0,0));
                PlayerSelectScreen = true;
            }
            else if(keyStates[SDLK_RETURN] && x_menu = GameMenuDest[1].x && y_menu == GameMenuDest[1].y && TitleScree == true)
            {
                TitleScreen = false;
                SDL_FillRect(gScreen,NULL,SDL_MapRGB(gScreen->format,0,0,0));
                OptionScreen = true;
            }
            else if(keyStates[SDLK_RETURN] && x_menu = GameMenuDest[2].x && y_menu == GameMenuDest[2].y && TitleScreen == true)
            {
                GameRunning = false;
            }
        }
        
        if(LogoScreen == true)
        {
            SDL_BlitSurface(LogoImg,NULL,gScreen,&LogoDest);
        }
        
        if(TitleScreen == true)
        {
            SDL_BlitSurface(TitleImg,NULL,gScreen,&TitleDest);
            SDL_BlitSurface(GameMenuText[0],NULL,gScreen,&GameMenuDest[0]);
            SDL_BlitSurface(GameMenuText[1],NULL,gScreen,&GameMenuDest[1]);
            SDL_BlitSurface(GameMenuText[2],NULL,gScreen,&GameMenuDest[2]);
        }
        
        if(PlayerSelectScreen == true)
        {
            
        }
        
        if(OptionScreen == true)
        {
            
        }
    }
    
    if(SDL_Flip(gScreen) == -1)
    {
        return 1;
    }
}

Cleanup();

Mix_CloseAudio();
TTF_CloseFont(Font);
TTF_Quit();
SDL_Quit();

return 0;

}

Thanks for any help.[/code]

Hello,

I have a menu where I would like the selected text to be changed to the color yellow from the color white. Basiclly it consists of “New Game”, “Game Options”, and “Quit Game”. I want it to start at “New Game”, when “New Game” is selected, the color of the text should yellow. However when the user pressed down, “Game Options” should be selected and that will turn from white to yellow, while “New Game” reverts back to the white color. How would I go about programming this? I’ll post my code in case that helps.

Hi,

I did something similar in an SDL game that I coded. Instead of
changing the color of the text I used two TTF font surfaces one in
reverse video of the other. The game is Classic Invaders and the
source code is available here
http://www.noquarterarcade.com/system/files/Source.zip.

The code I’m referring to is pretty much all contained in the UI class
ui.cpp and ui.h. There is an enum that tracks the current menu item
and then things are blitted based on keyboard input. It’s not perfect
but I found this to be a good way to build a User Interface while only
using SDL, no external UI libraries are needed.

Todd Steinackle
http://www.noquarterarcade.com/On Fri, Feb 19, 2010 at 10:21 PM, GameCoder <g_andy at live.com> wrote:

mainvars.h

Code:

const int MAX_WIDTH = 640;
const int MAX_HEIGHT = 480;
const int MAX_DEPTH = 32;
const char* szTitle = “Convict Paradise - DarkScar Games”;

SDL_Surface* gScreen = NULL;
SDL_Event event;
float deltaTime = 0.0f;
int ClockMins = 0;
int ClockSecs = 0;
TTF_Font* Font = NULL;
Uint8* keyStates = SDL_GetKeyState(NULL);

SDL_Surface* LogoImg;
SDL_Rect LogoDest;

SDL_Surface* TitleImg;
SDL_Rect TitleDest;

SDL_Surface* GameClockImg;
SDL_Rect GameClockRect;
SDL_Rect GameClockDest;

SDL_Surface* HealthBarImg[2];
SDL_Rect HealthBarRect[2];
SDL_Rect HealthBarDest[2];

SDL_Surface* GameMenuText[3];
SDL_Rect GameMenuDest[3];

SDL_Color TextColor = {255,255,255}; //white
SDL_Color BackColor = {0,0,255}; //blue
SDL_Color SelectedText = {255,255,0}; //yellow

int x_menu = 250;
int y_menu = 100;

int mouse_x = 0;
int mouse_y = 0;

bool Blood_Enabled = true;
bool Sound_Enabled = true;
float sound_level = 100.0f;
int sound_counter = 0;

bool LogoScreen = true;
bool TitleScreen = false;
bool PlayerSelectScreen = false;
bool GameScreen = false;
bool OptionScreen = false;
bool IntroScreen = false;
bool EndScene = false;

bool GameRunning = true;

void Cleanup()
{
? ? SDL_FreeSurface(LogoImg);
? ? SDL_FreeSurface(TitleImg);
? ? SDL_FreeSurface(GameMenuText[0]);
? ? SDL_FreeSurface(GameMenuText[1]);
? ? SDL_FreeSurface(GameMenuText[2]);
}

Main.cpp

Code:

#include
#include
#include
#include
#include
#include
#include
#include
#include

#include “SDL.h”
#include “SDL_image.h”
#include “SDL_mixer.h”
#include “SDL_ttf.h”

#include “mainvars.h”
#include “convicts.h”

using namespace std;

int main(int argc, char* argv[])
{
? ? /////////////////////////////////
? ? LogoImg = IMG_Load(“Logo.png”);
? ? LogoDest.x = 0;
? ? LogoDest.y = 0;
? ? LogoDest.w = MAX_WIDTH;
? ? LogoDest.h = MAX_HEIGHT;

? ? TitleImg = IMG_Load(“Title.png”);
? ? TitleDest.x = 0;
? ? TitleDest.y = 0;
? ? TitleDest.w = MAX_WIDTH;
? ? TitleDest.h = MAX_HEIGHT;
? ? ////////////////////////////////

? ? ///////////////////////////////
? ? Johnny sJohnny;
? ? Johnny *pJohnny;
? ? pJohnny = &sJohnny;

? ? pJohnny->ihealth = 100;
? ? pJohnny->imax_health = pJohnny->ihealth;
? ? pJohnny->istrength = 15;
? ? pJohnny->idefense = 10;
? ? pJohnny->fspeed = 20.f;
? ? pJohnny->szName = “Johnny”;
? ? pJohnny->ipoints = 0;
? ? pJohnny->ikills = 0;
? ? pJohnny->iwins = 0;
? ? pJohnny->ilosts = 0;
? ? ///////////////////////////////
? ? if(SDL_Init(SDL_INIT_EVERYTHING) == -1)
? ? {
? ? ? ? return 1;
? ? }

? ? gScreen = SDL_SetVideoMode(MAX_WIDTH,MAX_HEIGHT,MAX_DEPTH, SDL_HWSURFACE | SDL_DOUBLEBUF);

? ? if(gScreen == NULL)
? ? {
? ? ? ? return 1;
? ? }

? ? SDL_WM_SetCaption(szTitle, NULL);

? ? if(TTF_Init() == -1)
? ? {
? ? ? ? return 1;
? ? }

? ? Font = TTF_OpenFont(“ARIAL.ttf”,25);

? ? ///////////////////////////////////
? ? GameMenuText[0] = TTF_RenderText_Solid(Font,“New Game”,TextColor);
? ? GameMenuText[1] = TTF_RenderText_Solid(Font,“Game Options”,TextColor);
? ? GameMenuText[2] = TTF_RenderText_Solid(Font,“Quit Game”,TextColor);

? ? GameMenuDest[0].x = 250;
? ? GameMenuDest[0].y = 100;
? ? GameMenuDest[0].w = 0;
? ? GameMenuDest[0].h = 0;

? ? GameMenuDest[1].x = 250;
? ? GameMenuDest[1].y = 150;
? ? GameMenuDest[1].w = 0;
? ? GameMenuDest[1].h = 0;

? ? GameMenuDest[2].x = 250;
? ? GameMenuDest[2].y = 200;
? ? GameMenuDest[2].w = 0;
? ? GameMenuDest[2].h = 0;
? ? //////////////////////////////////

? ? if(Mix_OpenAudio(22050,MIX_DEFAULT_FORMAT,2,4096) == -1)
? ? {
? ? ? ? return 1;
? ? }

? ? while(GameRunning)
? ? {
? ? ? ? while(SDL_PollEvent(&event))
? ? ? ? {
? ? ? ? ? ? if(event.type == SDL_QUIT)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? GameRunning = false;
? ? ? ? ? ? }

? ? ? ? ? ? if(event.type == SDL_KEYDOWN)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? if(keyStates[SDLK_ESCAPE])
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? GameRunning = false;
? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? if(keyStates[SDLK_RETURN] && LogoScreen == true)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? LogoScreen = false;
? ? ? ? ? ? ? ? ? ? SDL_FillRect(gScreen,NULL,SDL_MapRGB(gScreen->format,0,0,0));
? ? ? ? ? ? ? ? ? ? TitleScreen = true;
? ? ? ? ? ? ? ? }

? ? ? ? ? ? ? ? if(keyStates[SDLK_RETURN] && x_menu == GameMenuDest[0].x && y_menu == GameMenuDest[0].y && TitleScreen == true)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? TitleScreen = false;
? ? ? ? ? ? ? ? ? ? SDL_FillRect(gScreen,NULL,SDL_MapRGB(gScreen->format,0,0,0));
? ? ? ? ? ? ? ? ? ? PlayerSelectScreen = true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if(keyStates[SDLK_RETURN] && x_menu = GameMenuDest[1].x && y_menu == GameMenuDest[1].y && TitleScree == true)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? TitleScreen = false;
? ? ? ? ? ? ? ? ? ? SDL_FillRect(gScreen,NULL,SDL_MapRGB(gScreen->format,0,0,0));
? ? ? ? ? ? ? ? ? ? OptionScreen = true;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? else if(keyStates[SDLK_RETURN] && x_menu = GameMenuDest[2].x && y_menu == GameMenuDest[2].y && TitleScreen == true)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? GameRunning = false;
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }

? ? ? ? ? ? if(LogoScreen == true)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? SDL_BlitSurface(LogoImg,NULL,gScreen,&LogoDest);
? ? ? ? ? ? }

? ? ? ? ? ? if(TitleScreen == true)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? SDL_BlitSurface(TitleImg,NULL,gScreen,&TitleDest);
? ? ? ? ? ? ? ? SDL_BlitSurface(GameMenuText[0],NULL,gScreen,&GameMenuDest[0]);
? ? ? ? ? ? ? ? SDL_BlitSurface(GameMenuText[1],NULL,gScreen,&GameMenuDest[1]);
? ? ? ? ? ? ? ? SDL_BlitSurface(GameMenuText[2],NULL,gScreen,&GameMenuDest[2]);
? ? ? ? ? ? }

? ? ? ? ? ? if(PlayerSelectScreen == true)
? ? ? ? ? ? {

? ? ? ? ? ? }

? ? ? ? ? ? if(OptionScreen == true)
? ? ? ? ? ? {

? ? ? ? ? ? }
? ? ? ? }

? ? ? ? if(SDL_Flip(gScreen) == -1)
? ? ? ? {
? ? ? ? ? ? return 1;
? ? ? ? }
? ? }

? ? Cleanup();

? ? Mix_CloseAudio();
? ? TTF_CloseFont(Font);
? ? TTF_Quit();
? ? SDL_Quit();

? ? return 0;
}

Thanks for any help.[/code]


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Excellent implementation! I’ve been looking for something close to the
original arcade game for some time. Thanks!

JeffOn Saturday 20 February 2010 08:58, Todd Steinackle wrote:

I did something similar in an SDL game that I coded. Instead of
changing the color of the text I used two TTF font surfaces one in
reverse video of the other. The game is Classic Invaders and the
source code is available here
http://www.noquarterarcade.com/system/files/Source.zip.

Yeah, with SDL, the easiest way is to have two fonts loaded that are
identical except for the color. If a given choice is selected, then use the
yellow font, else use the white one.

For your particular code and style, I would use something like:
SDL_Surface* GameMenuText[6];

int selectedMenuItem = 0;
GameMenuText[0] = TTF_RenderText_Solid(Font,“New Game”,TextColor);
GameMenuText[1] = TTF_RenderText_Solid(Font,“Game Options”,TextColor);
GameMenuText[2] = TTF_RenderText_Solid(Font,“Quit Game”,TextColor);
GameMenuText[3] = TTF_RenderText_Solid(Font,“New Game”,SelectedText);
GameMenuText[4] = TTF_RenderText_Solid(Font,“Game
Options”, SelectedText);
GameMenuText[5] = TTF_RenderText_Solid(Font,“Quit Game”, SelectedText);

for(int i = 0; i < 3; i++)
if(i == selectedMenuItem)

SDL_BlitSurface(GameMenuText[3+i],NULL,gScreen,&GameMenuDest[i]);
else

SDL_BlitSurface(GameMenuText[i],NULL,gScreen,&GameMenuDest[i]);

Jonny DOn Sat, Feb 20, 2010 at 12:40 PM, Jeff Post <j_post at pacbell.net> wrote:

On Saturday 20 February 2010 08:58, Todd Steinackle wrote:

I did something similar in an SDL game that I coded. Instead of
changing the color of the text I used two TTF font surfaces one in
reverse video of the other. The game is Classic Invaders and the
source code is available here
http://www.noquarterarcade.com/system/files/Source.zip.

Excellent implementation! I’ve been looking for something close to the
original arcade game for some time. Thanks!

Jeff


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanks Johnny, your post was very helpful.