⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sdlbutton.cpp

📁 游戏开发数据结构Data Structures for Game Programmers
💻 CPP
字号:
// ============================================================================
// Data Structures For Game Programmers
// Ron Penton
// SDLButton.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 "SDLButton.h"



// -------------------------------------------------------
// Name:        SDLButton
// Description: This constructs the button
// Arguments:   - p_up: the up bitmap name
//              - p_down: the down bitmap name
//              - p_x: x coordinate
//              - p_y: y coordinate
//              - p_text: the text of the label
//              - p_tforecol: the foreground color of the 
//                            text
//              - p_tbackcol: the background color of the
//                            text
//              - p_font: the font of the label.
// -------------------------------------------------------
SDLButton::SDLButton( const char* p_up, const char* p_down,
                      int p_x, int p_y,
                      const char* p_text, 
                      SDL_Color p_tforecol,
                      SDL_Color p_tbackcol,
                      TTF_Font* p_font,
                      void (*p_func)(void) )
{
    // load the 2 bmps, and set the current bmp to the up
    // position.
    m_up = SDL_LoadBMP( p_up );
    m_down = SDL_LoadBMP( p_down );
    m_current = m_up;

    // load the text.
    m_text = TTF_RenderText_Shaded( p_font, p_text, p_tforecol, p_tbackcol );
    SDL_SetColorKey( m_text, 
                     SDL_SRCCOLORKEY, 
                     SDL_MapRGB( m_text->format, 
                                 p_tbackcol.r,
                                 p_tbackcol.g,
                                 p_tbackcol.b ) );

    m_x = p_x;
    m_y = p_y;
    m_func = p_func;
}



// -------------------------------------------------------
// Name:        ~SDLButton
// Description: This destructs the button
// Arguments:   None.
// -------------------------------------------------------
SDLButton::~SDLButton()
{
    if( m_up != 0 )
        SDL_FreeSurface( m_up );
    if( m_down != 0 )
        SDL_FreeSurface( m_down );
    if( m_text != 0 )
        SDL_FreeSurface( m_text );
}

    

// -------------------------------------------------------
// Name:        draw
// Description: draws the button.
// Arguments:   - p_dest: destination surface.
// -------------------------------------------------------
void SDLButton::Draw( SDL_Surface* p_dest )
{
    if( m_visible == true )
    {
        SDLBlit( m_current, p_dest, m_x, m_y );
        SDLBlit( m_text, p_dest, 
                 m_x + ((m_current->w - m_text->w) / 2),
                 m_y + ((m_current->h - m_text->h) / 2));
    }
}


// -------------------------------------------------------
// Name:        IsOver
// Description: determines if a position is over the button
// Arguments:   - p_x: x coordinate
//              - p_y: y coordinate
// Return Value: true if the position is over the button.
// -------------------------------------------------------
bool SDLButton::IsOver( int p_x, int p_y )
{
    if( p_x >= m_x && p_x < (m_x + m_current->w) )
    {
        if( p_y >= m_y && p_y < (m_y + m_current->h) )
        {
            return true;
        }
    }
    return false;
}


// -------------------------------------------------------
// Name:        State
// Description: gets the state of the button
// Arguments:   None.
// Return Value: BUTTONSTATE
// -------------------------------------------------------
BUTTONSTATE SDLButton::State()
{
    if( m_current == m_down )
        return BUTTONDOWN;
    else
        return BUTTONUP;
}


// -------------------------------------------------------
// Name:        ClickDown
// Description: sets the down state of the button
// Arguments:   None.
// Return Value: None.
// -------------------------------------------------------
void SDLButton::ClickDown()
{
    m_current = m_down;
}
 

// -------------------------------------------------------
// Name:        ClickUp
// Description: sets the up state of the button
// Arguments:   None.
// Return Value: None.
// -------------------------------------------------------
void SDLButton::ClickUp()
{
    m_current = m_up;
    if( m_func != 0 )
    {
        m_func();
    }
}


// -------------------------------------------------------
// Name:        ResetOnUp
// Description: resets the button state if needed.
// Arguments:   None.
// Return Value: None.
// -------------------------------------------------------
void SDLButton::ResetOnUp()
{
    m_current = m_up;
}


// -------------------------------------------------------
// Name:        Toggle
// Description: toggles the state of the button
// Arguments:   None.
// Return Value: None.
// -------------------------------------------------------
void SDLButton::Toggle()
{
    if( m_current == m_up )
        m_current = m_down;
    else
        m_current = m_up;
}


// -------------------------------------------------------
// Name:        CanGetFocus
// Description: determines if this object can get the 
//              focus of the GUI.
// Arguments:   None.
// Return Value: True or False
// -------------------------------------------------------
bool SDLButton::CanGetFocus()
{
    // in our GUI, buttons cannot get the focus.
    return false;
}


// ----------------------------------------------------------------
//  Name:           GetFocus
//  Description:    tells the item to get the focus or not.
//  Arguments:      p_focus: true if the item gets the focus
//  Return Value:   None
// ----------------------------------------------------------------
void SDLButton::GetFocus( bool p_focus )
{
    // do nothing
    return;
}


// ----------------------------------------------------------------
//  Name:           KeyPress
//  Description:    tells the GUI that a key was pressed when this
//                  item was in focus
//  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 SDLButton::KeyPress( SDLKey p_key, SDLMod p_mod, Uint16 p_char )
{
    return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -