📄 sdlgui.h
字号:
// ============================================================================
// Data Structures For Game Programmers
// Ron Penton
// SDLGUI.h
// This is a basic SDLGUI library that I've set up for use with the game demos
// I've used in the book.
// ============================================================================
#ifndef SDLGUI_H
#define SDLGUI_H
#include "SDL.h"
#include "SDL_ttf.h"
#include "SDLGUIItem.h"
#include "SDLButton.h"
#include "SDLCheckBox.h"
#include "SDLLabel.h"
#include "SDLTextBox.h"
#include "SDLHelpers.h"
// ----------------------------------------------------------------
// Name: NFONTS
// Description: the number of fonts each GUI can have.
// ----------------------------------------------------------------
#define NFONTS 16
// ----------------------------------------------------------------
// Name: SDLGUI
// Description: The GUI class
// ----------------------------------------------------------------
class SDLGUI
{
public:
// ----------------------------------------------------------------
// 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( int p_width,
int p_height,
int p_items,
SDL_Color p_background );
// ----------------------------------------------------------------
// Name: ~SDLGUI
// Description: destructor, destroys the GUI
// Arguments: None
// Return Value: None
// ----------------------------------------------------------------
~SDLGUI();
// ----------------------------------------------------------------
// 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 SetFont( const char* p_name,
int p_index,
int p_size,
int 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 AddButton( int p_x, int p_y,
const char* p_up,
const char* p_down,
const char* p_text,
int p_font,
SDL_Color p_fore,
SDL_Color p_back,
void (*p_func)(void) );
// ----------------------------------------------------------------
// 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 AddCheckbox( int p_x, int p_y,
const char* p_up,
const char* p_down,
const char* p_text,
int p_font,
SDL_Color p_fore,
SDL_Color p_back,
void (*p_func)(void) );
// ----------------------------------------------------------------
// 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 AddLabel( int p_x, int p_y,
const char* p_text,
int p_font,
SDL_Color p_fore,
SDL_Color p_back );
// ----------------------------------------------------------------
// 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 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) );
// ----------------------------------------------------------------
// Name: Draw
// Description: draws the GUI on the screen surface
// Arguments: None.
// Return Value: None
// ----------------------------------------------------------------
void Draw();
// ----------------------------------------------------------------
// Name: Update
// Description: updates the GUI surface onto the screen.
// Arguments: None.
// Return Value: None
// ----------------------------------------------------------------
void Update();
// ----------------------------------------------------------------
// Name: MouseDown
// Description: tells the GUI that a mouse was clicked.
// Arguments: p_x, p_y: coordinates of the click
// Return Value: None
// ----------------------------------------------------------------
void MouseDown( int p_x, int p_y );
// ----------------------------------------------------------------
// Name: MouseUp
// Description: tells the GUI that a mouse button was released
// Arguments: p_x, p_y: coordinates of the mouse
// Return Value: None
// ----------------------------------------------------------------
void MouseUp( int p_x, int p_y );
// ----------------------------------------------------------------
// Name: KeyDown
// Description: Tells the GUI that a key was pressed
// Arguments: p_key: the that was pressed
// p_mod: the keyboard modifier flag (shift, alt,
// control).
// p_char: the UNICODE representation of the key
// Return Value: None
// ----------------------------------------------------------------
void KeyDown( SDLKey p_key, SDLMod p_mod, Uint16 p_char );
// ----------------------------------------------------------------
// Name: Point
// Description: draws a point on the GUI
// Arguments: x, y: coordinates
// p_color: the color of the point
// Return Value: None
// ----------------------------------------------------------------
void Point( int x, int y, SDL_Color p_color );
// ----------------------------------------------------------------
// Name: Line
// Description: draws a line on the GUI
// Arguments: x1, y1: first point
// x2, y2: second point
// p_color: color of the line
// Return Value: None
// ----------------------------------------------------------------
void Line( int x1, int y1, int x2, int y2, SDL_Color p_color );
// ----------------------------------------------------------------
// Name: ArrowLine
// Description: draws a line, with optional arrowheads
// Arguments: x1, y1: first point
// x2, y2: second point
// r1, r2: the radius of the points (see Appendx
// C in the book)
// arrow1, arrow2: determines if an arrow is drawn
// p_color: color of the line
// Return Value: None
// ----------------------------------------------------------------
void ArrowLine( int x1, int y1, int x2, int y2,
int r1, int r2, bool arrow1, bool arrow2,
SDL_Color p_color );
// ----------------------------------------------------------------
// Name: Blit
// Description: draws a surface on the GUI
// Arguments: p_source: surface to draw from
// x, y: coordinates
// Return Value: None
// ----------------------------------------------------------------
void Blit( SDL_Surface* p_source, int x, int y );
// ----------------------------------------------------------------
// Name: Box
// Description: draws a solid box on the GUI
// Arguments: x, y: coordinates
// width, height: width and height of the box
// p_color: color of the box
// Return Value: None
// ----------------------------------------------------------------
void Box( int x, int y, int width, int height, SDL_Color p_color );
// ----------------------------------------------------------------
// Name: GetFont
// Description: gets a pointer to a given font in the GUI
// Arguments: p_index: the index of the font
// Return Value: a pointer to the font
// ----------------------------------------------------------------
TTF_Font* GetFont( int p_index );
// ----------------------------------------------------------------
// Name: GetItem
// Description: Gets a pointer to the given item
// Arguments: p_item: the index of the item
// Return Value: a pointer to the item
// ----------------------------------------------------------------
SDLGUIItem* GetItem( int p_item );
// ----------------------------------------------------------------
// Name: GetScreen
// Description: Gets a pointer to the screen surface
// Arguments: None
// Return Value: a pointer to the screen surface
// ----------------------------------------------------------------
SDL_Surface* GetScreen();
// ----------------------------------------------------------------
// Name: SetFocus
// Description: This sets the focus of the GUI to a particular
// item.
// Arguments: p_item: index of the item
// Return Value: None
// ----------------------------------------------------------------
void SetFocus( int p_item );
private:
// ----------------------------------------------------------------
// Name: m_screen
// Description: the screen surface pointer
// ----------------------------------------------------------------
SDL_Surface* m_screen;
// ----------------------------------------------------------------
// Name: m_background
// Description: the color of the background
// ----------------------------------------------------------------
SDL_Color m_background;
// ----------------------------------------------------------------
// Name: m_items
// Description: an array of SDLHUIItem pointers; the items in
// the GUI
// ----------------------------------------------------------------
SDLGUIItem** m_items;
// ----------------------------------------------------------------
// Name: m_fonts
// Description: The array of fonts
// ----------------------------------------------------------------
TTF_Font* m_fonts[NFONTS];
// ----------------------------------------------------------------
// Name: m_itemcount
// Description: Number of items currently in the GUI
// ----------------------------------------------------------------
int m_itemcount;
// ----------------------------------------------------------------
// Name: m_itemmax
// Description: the maximum number of items the GUI can hold
// ----------------------------------------------------------------
int m_itemmax;
// ----------------------------------------------------------------
// Name: m_currentitem
// Description: the index of the item that has the focus
// ----------------------------------------------------------------
int m_currentitem;
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -