📄 sdlgui.cpp
字号:
// ============================================================================
// Data Structures For Game Programmers
// Ron Penton
// SDLGUI.cpp
// This is a basic SDLGUI library that I've set up for use with the game demos
// I've used in the book.
// ============================================================================
#include "SDLGUI.h"
// ----------------------------------------------------------------
// Name: SDLGUI
// Description: constructor, creates a new GUI
// Arguments: p_width: width of the GUI
// p_height: height of the GUI
// p_items: the number of items the GUI can hold
// p_background: the background color of the GUI
// Return Value: None
// ----------------------------------------------------------------
SDLGUI::SDLGUI( int p_width,
int p_height,
int p_items,
SDL_Color p_background )
{
int index;
// enable key repeating.
int i = SDL_EnableKeyRepeat( SDL_DEFAULT_REPEAT_DELAY,
SDL_DEFAULT_REPEAT_INTERVAL );
// set the video mode to the specified height and width,
// in windowed mode.
m_screen = SDL_SetVideoMode( p_width, p_height, 0, SDL_ANYFORMAT );
// Set the number of items to 0, and the maximum number of items.
m_itemcount = 0;
m_itemmax = p_items;
// allocate the items array, and clear it out.
m_items = new SDLGUIItem*[m_itemmax];
for( index = 0; index < m_itemmax; index++ )
{
m_items[index] = 0;
}
// set the background color.
m_background = p_background;
// clear all the fonts.
for( index = 0; index < NFONTS; index++ )
{
m_fonts[index] = 0;
}
// set the current item to -1, invalid.
m_currentitem = -1;
}
// ----------------------------------------------------------------
// Name: ~SDLGUI
// Description: destructor, destroys the GUI
// Arguments: None
// Return Value: None
// ----------------------------------------------------------------
SDLGUI::~SDLGUI()
{
int index;
// loop through all the items and delete them.
for( index = 0; index < m_itemmax; index++ )
{
if( m_items[index] != 0 )
delete m_items[index];
}
// delete the items array.
delete[] m_items;
// delete all the fonts.
for( index = 0; index < NFONTS; index++ )
{
if( m_fonts[index] != 0 )
TTF_CloseFont( m_fonts[index] );
}
}
// ----------------------------------------------------------------
// Name: SetFont
// Description: loads a font from a TTF file into the GUI
// Arguments: p_name: filename of the font
// p_index: the index of the font
// p_size: the point size of the font
// p_style: the style of the font
// Return Value: None
// ----------------------------------------------------------------
void SDLGUI::SetFont( const char* p_name,
int p_index,
int p_size,
int p_style )
{
// check if the index is valid.
if( p_index >= NFONTS || p_index < 0 )
return;
// if the font already exists, close it.
if( m_fonts[p_index] != 0 )
TTF_CloseFont( m_fonts[p_index] );
// load the font and set its style.
m_fonts[p_index] = TTF_OpenFont( p_name, p_size );
TTF_SetFontStyle( m_fonts[p_index], p_style );
}
// ----------------------------------------------------------------
// Name: AddButton
// Description: Adds a button to the GUI
// Arguments: p_x, p_y: coordinates of the button
// p_up: filename of the BMP when the button is up
// p_down: name of the BMP when the button is down
// p_font: index of the font on the button.
// p_fore: the forecolor of the font
// p_back: the backcolor of the fond
// p_func: pointer to the function that is called
// when the button is pressed
// Return Value: None
// ----------------------------------------------------------------
void SDLGUI::AddButton( int p_x, int p_y, // x, y position
const char* p_up, // the filename of the up graphic
const char* p_down, // the filename of the down graphic
const char* p_text, // the text of the button.
int p_font, // the font to use.
SDL_Color p_fore, // the foreground color of the text
SDL_Color p_back, // the background color of the text
void (*p_func)(void) ) // the function that is called when it is pressed
{
SDLButton* temp;
// if there is room, create the button.
if( m_itemcount < m_itemmax )
{
temp = new SDLButton( p_up, p_down, p_x, p_y,
p_text, p_fore, p_back,
m_fonts[p_font], p_func );
m_items[m_itemcount] = temp;
m_itemcount++;
}
}
// ----------------------------------------------------------------
// Name: AddCheckbox
// Description: Adds a checkbox to the GUI
// Arguments: p_x, p_y: coordinates of the box
// p_up: filename of the BMP when the box is on
// p_down: name of the BMP when the box is off
// p_font: index of the font on the box.
// p_fore: the forecolor of the font
// p_back: the backcolor of the fond
// p_func: pointer to the function that is called
// when the box is pressed
// Return Value: None
// ----------------------------------------------------------------
void SDLGUI::AddCheckbox( int p_x, int p_y,
const char* p_up,
const char* p_down,
const char* p_text,
int p_font, // the font to use.
SDL_Color p_fore,
SDL_Color p_back,
void (*p_func)(void) )
{
SDLCheckBox* temp;
if( m_itemcount < m_itemmax )
{
temp = new SDLCheckBox( p_up, p_down, p_x, p_y,
p_text, p_fore, p_back,
m_fonts[p_font], p_func );
m_items[m_itemcount] = temp;
m_itemcount++;
}
}
// ----------------------------------------------------------------
// Name: AddLabel
// Description: Adds a label to the GUI
// Arguments: p_x, p_y: coordinates of the label
// p_text: the text of the label
// p_font: index of the font on the button.
// p_fore: the forecolor of the font
// p_back: the backcolor of the fond
// Return Value: None
// ----------------------------------------------------------------
void SDLGUI::AddLabel( int p_x, int p_y,
const char* p_text,
int p_font, // the font to use.
SDL_Color p_fore,
SDL_Color p_back )
{
SDLLabel* temp;
if( m_itemcount < m_itemmax )
{
temp = new SDLLabel( p_x, p_y,
p_text, p_fore, p_back,
m_fonts[p_font] );
m_items[m_itemcount] = temp;
m_itemcount++;
}
}
// ----------------------------------------------------------------
// Name: AddTextBox
// Description: Adds a textbox to the GUI
// Arguments: p_x, p_y: coordinates of the box
// p_w, p_y: width and height of the box
// p_string: string that the box contains
// p_length: max length of the string
// p_font: index of the font on the button.
// p_fore: the forecolor of the font
// p_back: the backcolor of the fond
// p_enabled: determines if box can be edited
// p_func: pointer to the function that is called
// when 'enter' is pressed in this box.
// Return Value: None
// ----------------------------------------------------------------
void SDLGUI::AddTextBox( int p_x, int p_y,
int p_w, int p_h,
char* p_string,
int p_length,
int p_font,
SDL_Color p_fore,
SDL_Color p_back,
bool p_enabled,
void (*p_func)(void) )
{
SDLTextBox* temp;
if( m_itemcount < m_itemmax )
{
temp = new SDLTextBox( p_x, p_y, p_w, p_h, p_string,
p_length, p_fore, p_back,
m_fonts[p_font], p_enabled, p_func );
m_items[m_itemcount] = temp;
m_itemcount++;
}
}
// ----------------------------------------------------------------
// Name: Draw
// Description: draws the GUI on the screen surface
// Arguments: None.
// Return Value: None
// ----------------------------------------------------------------
void SDLGUI::Draw()
{
int index;
// clear the screen to the background color
SDL_FillRect( m_screen,
NULL,
SDL_MapRGB( m_screen->format,
m_background.r,
m_background.g,
m_background.b ) );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -