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

📄 ascscrn.hpp

📁 使用BorlandC++4.5编译的一个MUD客户端程序
💻 HPP
字号:
head	2.2;access;symbols;locks; strict;comment	@// @;2.2date	96.02.04.22.31.59;	author tsurace;	state Release;branches;next	2.1;2.1date	95.10.24.15.52.51;	author tsurace;	state Beta;branches;next	1.4;1.4date	95.10.11.20.56.48;	author tsurace;	state Beta;branches;next	1.3;1.3date	95.10.08.23.27.24;	author tsurace;	state Exp;branches;next	1.2;1.2date	95.10.07.00.32.54;	author tsurace;	state Exp;branches;next	1.1;1.1date	95.10.05.18.32.35;	author tsurace;	state Exp;branches;next	;desc@Virtual text screen thingy with some screen manipulation.@2.2log@Added code to handle illegal cursor placement gracefully.@text@#ifndef ASCSCRN_HPP
#define ASCSCRN_HPP

// $Id: ascscrn.hpp 2.1 1995/10/24 15:52:51 tsurace Beta tsurace $
// $Log: ascscrn.hpp $
// Revision 2.1  1995/10/24  15:52:51  tsurace
// Roll.
//
// Revision 1.4  1995/10/11  20:56:48  tsurace
// Modified to use MY ASSERT macro.
//
// Revision 1.3  1995/10/08  23:27:24  tsurace
// Added ansi(fg) color support.
//
// (end of log)


#include "debug.hpp"
#include "units.hpp" // Pos
#include "esc_seq.hpp"

#define ASBUFFER_LEN 2048

// ASBuffer - class to buffer input for a while.  This is to speed
//   lots of stuff, and is used to detect escape sequences.
class ASBuffer
{
  public:
    ASBuffer();
    
    void   Append(char c);     // Push onto stack
    void   Clear();            // Clear buffer
    int    Length() const;
    operator char * ();        // Return pointer to buffer
    char   operator [](int offset) const; // Return element of buffer

  private:
    char _buffer[ASBUFFER_LEN];       // null-terminated character string
    int  _buf_len;

    ASBuffer (const ASBuffer &); // Disable copy 'cause it's slow
};

// AsciiScreen class header file
//
// Note that the position coordinates are zero-indexed.

class AsciiScreen
{
  public:
    AsciiScreen(int width,
                int height,
                COLORREF normal_foreground,
                COLORREF bold_foreground,
                void (*invalidate_func)(const Pos & pos,
                                        const Size & size,
                                        void * extra),
                void * invalidateFuncExtra,
                int (*scroll_func)(int top,
                                   int bottom,
                                   int amount,
                                   void * extra),
                void * scrollFuncExtra);

    ~AsciiScreen();

    const Pos & Cursor() const { return _cursor; };
    const char &     TextElement(const Pos & pos) const ;
    const char &     TextElement(int index) const ;
    const COLORREF & ForegroundElement(const Pos & pos) const;
    const COLORREF & ForegroundElement(int index) const;
    
    
    void        Foreground(COLORREF foreground);
    void        Goto(Pos & pos);
    int         Height() const { return _height; };
    int         IndexOf(int column, int row) const;
    int         IndexOf(const Pos & pos) const;
    int         IsOnScreen(const Pos & pos) const;
    void        Put(char c);
    void        Put(char c, Pos & pos);
    void        Put(char * c);
    void        Put(char * c, Pos & pos);
    void        Resize(int width, int height);
    void        Scroll(int top, int bottom, int amount);
    int         Width() const { return _width; };

  protected:
    Pos         _cursor;             // Cursor attributes
    COLORREF    _cursorForeground;   // Current color

    int         _useAnsiColor;       // 1 to use the color below
    int         _cursorAnsiColor;    // ANSI color attribute (0-7)
    int         _cursorIsBold;       // 0(normal) or 1(bold)
    
    COLORREF    _boldForeground;     // Default used if ANSI not used
    COLORREF    _normalForeground;   // Used for normal characters
    
    int         _width;       // Screen size
    int         _height;
    int         _scrollRegionTop;    // Top of virtual screen
    int         _scrollRegionBottom; // Bottom of virtual screen
    char      * _text;        // The actual screen buffer
    COLORREF  * _foreground;  // And its colors
    
    void        (*_invalidateFunc)(const Pos & pos,
                                   const Size & size,
                                   void * extra);
    
    void      * _invalidateFuncExtra;
    void        _Invalidate(const Pos & pos, const Size & size) const;
    
    int         (*_scrollFunc)(int target, int src, int size, void * extra);
    void      * _scrollFuncExtra;
    int         _CallScrollFunc(int target, int source, int size) const;

    ASBuffer    _inputBuffer;   // Buffer object

    void   _AnsiColor(const EscapeSequence & esc);
    void   _IncrementPos();     // Moves cursor one space to the right
    void   _BackSpace();
    void   _CopyRow(int target, int source);
    void   _ClearToEOL();
    void   _ClearScreen();
    void   _ClearRow(int row);
    void   _ClearRows(int top, int bottom);
    void   _EscapeSequence(EscapeSequence & esc);
    void   _ReverseNewLine();
    void   _CarriageReturn();
    void   _NewLine(int count = 1);
    void   _ScrollCurrentRegion(int amount);

    char &     _TextElement(const Pos & pos);
    COLORREF & _ForegroundElement(const Pos & pos);
    
    // Disable copy/assign for now
    AsciiScreen(const AsciiScreen &);
    const AsciiScreen & operator=(const AsciiScreen &);

};

// >>>>> Buffer inlines <<<<<

inline ASBuffer::ASBuffer()
: _buf_len(0)
{
    _buffer[0] = '\0'; 
};
    
inline void ASBuffer::Append(char c)
{
    _buffer[_buf_len] = c;
    ++ _buf_len;
    ASSERT(ASBUFFER_LEN > _buf_len, "Out of room in internal buffer");
    _buffer[_buf_len] = '\0';  // Null-terminate
}

inline void ASBuffer::Clear()
{
    _buf_len = 0;
    _buffer[0] = '\0';
}

inline ASBuffer::operator char * ()
{
    return _buffer;
}

inline char ASBuffer::operator [](int offset) const
{
    ASSERT(ASBUFFER_LEN > offset && 0 <= offset, "Index out of bounds");
    return _buffer[offset];
};

inline int ASBuffer::Length() const
{
    return _buf_len;
};

// >>>>> AsciiScreen inlines <<<<<

inline void AsciiScreen::Put(char c, Pos & pos)
{
    Goto(pos);
    Put(c);
}

// PutString with pos - puts string starting at a particular location
inline void AsciiScreen::Put(char * c, Pos & pos)
{
    Goto(pos);
    Put(c);
}

// ------------------------------------------------------------------------
// IndexOf - returns array offset of this screen pos
//
inline int AsciiScreen::IndexOf(int x, int y) const
{
    return (y * Width()) + x;
}

// IndexOf - returns array index of pos
//
inline int AsciiScreen::IndexOf(const Pos & pos) const
{
    return IndexOf(pos.X(), pos.Y());
}

// _TextElement - accesses a particular element of the text array
//
// Parameters:
//   pos - coordinate of element to address

inline char & AsciiScreen::_TextElement(const Pos & pos)
{
    ASSERT(IsOnScreen(pos), "Accessing element offscreen");
    
    return _text[IndexOf(pos)];
}

inline COLORREF &
AsciiScreen::_ForegroundElement(const Pos & pos)
{
    ASSERT(IsOnScreen(pos), "Accessing foreground color offscreen");
    
    return _foreground[IndexOf(pos)];
}

inline const char &
AsciiScreen::TextElement(int index) const
{
    ASSERT(0 <= index, "Negative array index not allowed");
    ASSERT((Width() * Height()) > index, "Index past end of buffer");
    
    return _text[index];
}

inline const char &
AsciiScreen::TextElement(const Pos & pos) const
{
    ASSERT(IsOnScreen(pos), "Reading element offscreen");
    
    return TextElement(IndexOf(pos));
}

inline const COLORREF &
AsciiScreen::ForegroundElement(int index) const
{
    ASSERT(0 <= index, "Negative array index not allowed");
    ASSERT((Width() * Height()) > index, "Index past end of array");
    
    return _foreground[index];
}

inline const COLORREF &
AsciiScreen::ForegroundElement(const Pos & pos) const
{
    ASSERT(IsOnScreen(pos), "Element is off screen?");
    
    return ForegroundElement(IndexOf(pos));
}

inline int AsciiScreen::_CallScrollFunc(int top,
                                        int bottom,
                                        int amount) const
{
    ASSERT(NULL != _scrollFunc, "Bad function pointer");
    
    return (*_scrollFunc)(top, bottom, amount, _scrollFuncExtra);
};


// _Invalidate - call the invalidate function, if non-null
inline void
AsciiScreen::_Invalidate(const Pos & pos, const Size & size) const
{
    ASSERT(NULL != _invalidateFunc, "Bad function pointer");
    
    (*_invalidateFunc)(pos, size, _invalidateFuncExtra);
}

// -------------------------------------------------------------------
// ScrollCurrentRegion - scrolls the current scroll region forward
//   by amount (amount may be  negative)
//
inline void AsciiScreen::_ScrollCurrentRegion(int amount)
{
    Scroll(_scrollRegionTop, _scrollRegionBottom, amount);
};


#endif // ASCSCRN_HPP
@2.1log@Roll.@text@d4 4a7 4// $Id: ascscrn.hpp 1.4 1995/10/11 20:56:48 tsurace Beta tsurace $// $Log: ascscrn.hpp $// Revision 1.4  1995/10/11  20:56:48  tsurace// Modified to use MY ASSERT macro.d9 3d129 1@1.4log@Modified to use MY ASSERT macro.@text@d4 5a8 2// $Id: ascscrn.hpp 1.3 1995/10/08 23:27:24 tsurace Exp tsurace $// $Log: ascscrn.hpp $@1.3log@Added ansi(fg) color support.@text@d4 5a8 2// $Id$// $Log$a10 1#include <assert.h>d12 1d147 1a147 1    assert(ASBUFFER_LEN > _buf_len);d164 1a164 1    assert(ASBUFFER_LEN > offset && 0 <= offset);d210 2a211 1    assert(IsOnScreen(pos)); // Element is off screen?d218 2a219 1    assert(IsOnScreen(pos)); // Element is off screen?d226 3a228 1    assert(0 <= index && (Width() * Height()) > index);  // on buffer?d235 2a236 1    assert(IsOnScreen(pos)); // Element is off screen?d243 3a245 1    assert(0 <= index && (Width() * Height()) > index);  // on buffer?d252 2a253 1    assert(IsOnScreen(pos)); // Element is off screen?d261 2a262 1    assert (NULL != _scrollFunc); // Bad function pointer!d271 2a272 1    assert(NULL != _invalidateFunc); // Oops!  Bad pointer!@1.2log@Added reverse newline, Resize()@text@d4 4d82 4d87 1a87 1    COLORREF    _boldForeground;     // Used for bold charactersd110 1@1.1log@Initial revision@text@d71 1d110 1@

⌨️ 快捷键说明

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