git repos / blockattack-game

blame: source/code/Libs/NFont.h

normal view · raw

925b7e58 sago007 2011-04-25 16:35 1
925b7e58 sago007 2011-04-25 16:35 2
// Define this here or in your project settings if you want to use the SDL_ttf features.
925b7e58 sago007 2011-04-25 16:35 3
#define NFONT_USE_TTF
925b7e58 sago007 2011-04-25 16:35 4
925b7e58 sago007 2011-04-25 16:35 5
/*
925b7e58 sago007 2011-04-25 16:35 6
NFont v2.0.0: A bitmap font class for SDL
925b7e58 sago007 2011-04-25 16:35 7
by Jonathan Dearborn 2-4-10
925b7e58 sago007 2011-04-25 16:35 8
(class originally adapted from Florian Hufsky)
925b7e58 sago007 2011-04-25 16:35 9
925b7e58 sago007 2011-04-25 16:35 10
Requires:
925b7e58 sago007 2011-04-25 16:35 11
    SDL ("SDL.h") [www.libsdl.org]
925b7e58 sago007 2011-04-25 16:35 12
925b7e58 sago007 2011-04-25 16:35 13
Optionally Requires:
925b7e58 sago007 2011-04-25 16:35 14
    SDL_ttf ("SDL_ttf.h") [www.libsdl.org]
925b7e58 sago007 2011-04-25 16:35 15
925b7e58 sago007 2011-04-25 16:35 16
Notes:
925b7e58 sago007 2011-04-25 16:35 17
    NFont is a bitmap font class with text-block alignment, full
925b7e58 sago007 2011-04-25 16:35 18
    support for the newline character ('\n'), animation, and extended ASCII 
925b7e58 sago007 2011-04-25 16:35 19
    support.  It accepts SDL_Surfaces so that any image format you can load 
925b7e58 sago007 2011-04-25 16:35 20
    can be used as an NFont.
925b7e58 sago007 2011-04-25 16:35 21
    
925b7e58 sago007 2011-04-25 16:35 22
    NFont has the ability to animate the font in two ways: It's position or 
925b7e58 sago007 2011-04-25 16:35 23
    it's everything.  By using drawPos(), you can use a function you create 
925b7e58 sago007 2011-04-25 16:35 24
    to handle the final positions of the drawn characters.  With drawAll(), 
925b7e58 sago007 2011-04-25 16:35 25
    you can handle everything that the font does (please use my 
925b7e58 sago007 2011-04-25 16:35 26
    drawToSurface() function as a reference).
925b7e58 sago007 2011-04-25 16:35 27
925b7e58 sago007 2011-04-25 16:35 28
    Internally, NFont uses a pointer (SDL_Surface*) to handle the destination
925b7e58 sago007 2011-04-25 16:35 29
    surface.  You have to set the destination before the font can be used.  Be 
925b7e58 sago007 2011-04-25 16:35 30
    aware that you will need to use setDest() if you replace the memory that 
925b7e58 sago007 2011-04-25 16:35 31
    it points to (like when using screen = SDL_SetVideoMode()).
925b7e58 sago007 2011-04-25 16:35 32
    
925b7e58 sago007 2011-04-25 16:35 33
    NFont can use standard SFont bitmaps or extended bitmaps.  The standard bitmaps
925b7e58 sago007 2011-04-25 16:35 34
    have the following characters (ASCII 33-126) separated by pink (255, 0, 255) pixels in the topmost
925b7e58 sago007 2011-04-25 16:35 35
    row:
925b7e58 sago007 2011-04-25 16:35 36
    ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~
925b7e58 sago007 2011-04-25 16:35 37
    
925b7e58 sago007 2011-04-25 16:35 38
    And the extended bitmaps have these (ASCII 161-255):
925b7e58 sago007 2011-04-25 16:35 39
    ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �
925b7e58 sago007 2011-04-25 16:35 40
    
925b7e58 sago007 2011-04-25 16:35 41
    NFont can also load SDL_ttf fonts.  Define NFONT_USE_TTF before including 
925b7e58 sago007 2011-04-25 16:35 42
    NFont.h to use the flexibility of NFont with TrueType fonts.
925b7e58 sago007 2011-04-25 16:35 43
925b7e58 sago007 2011-04-25 16:35 44
    If you come up with something cool using NFont, I'd love to hear about it.
925b7e58 sago007 2011-04-25 16:35 45
    Any comments can be sent to GrimFang4 [at] gmail [dot] com
925b7e58 sago007 2011-04-25 16:35 46
925b7e58 sago007 2011-04-25 16:35 47
License:
925b7e58 sago007 2011-04-25 16:35 48
    The short:
925b7e58 sago007 2011-04-25 16:35 49
    Use it however you'd like, but keep the copyright and license notice 
925b7e58 sago007 2011-04-25 16:35 50
    whenever these files or parts of them are distributed in uncompiled form.
925b7e58 sago007 2011-04-25 16:35 51
    
925b7e58 sago007 2011-04-25 16:35 52
    The long:
925b7e58 sago007 2011-04-25 16:35 53
Copyright (c) 2010 Jonathan Dearborn
925b7e58 sago007 2011-04-25 16:35 54
925b7e58 sago007 2011-04-25 16:35 55
Permission is hereby granted, free of charge, to any person obtaining a copy
925b7e58 sago007 2011-04-25 16:35 56
of this software and associated documentation files (the "Software"), to deal
925b7e58 sago007 2011-04-25 16:35 57
in the Software without restriction, including without limitation the rights
925b7e58 sago007 2011-04-25 16:35 58
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
925b7e58 sago007 2011-04-25 16:35 59
copies of the Software, and to permit persons to whom the Software is
925b7e58 sago007 2011-04-25 16:35 60
furnished to do so, subject to the following conditions:
925b7e58 sago007 2011-04-25 16:35 61
925b7e58 sago007 2011-04-25 16:35 62
The above copyright notice and this permission notice shall be included in
925b7e58 sago007 2011-04-25 16:35 63
all copies or substantial portions of the Software.
925b7e58 sago007 2011-04-25 16:35 64
925b7e58 sago007 2011-04-25 16:35 65
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
925b7e58 sago007 2011-04-25 16:35 66
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
925b7e58 sago007 2011-04-25 16:35 67
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
925b7e58 sago007 2011-04-25 16:35 68
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
925b7e58 sago007 2011-04-25 16:35 69
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
925b7e58 sago007 2011-04-25 16:35 70
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
925b7e58 sago007 2011-04-25 16:35 71
THE SOFTWARE.
925b7e58 sago007 2011-04-25 16:35 72
925b7e58 sago007 2011-04-25 16:35 73
*/
925b7e58 sago007 2011-04-25 16:35 74
925b7e58 sago007 2011-04-25 16:35 75
#ifndef _NFONT_H__
925b7e58 sago007 2011-04-25 16:35 76
#define _NFONT_H__
925b7e58 sago007 2011-04-25 16:35 77
925b7e58 sago007 2011-04-25 16:35 78
#include "SDL.h"
925b7e58 sago007 2011-04-25 16:35 79
#include "stdarg.h"
925b7e58 sago007 2011-04-25 16:35 80
925b7e58 sago007 2011-04-25 16:35 81
#ifdef NFONT_USE_TTF
925b7e58 sago007 2011-04-25 16:35 82
    #include "SDL_ttf.h"
925b7e58 sago007 2011-04-25 16:35 83
#endif
925b7e58 sago007 2011-04-25 16:35 84
925b7e58 sago007 2011-04-25 16:35 85
class NFont
925b7e58 sago007 2011-04-25 16:35 86
{
925b7e58 sago007 2011-04-25 16:35 87
public:
925b7e58 sago007 2011-04-25 16:35 88
925b7e58 sago007 2011-04-25 16:35 89
    // Nested struct
925b7e58 sago007 2011-04-25 16:35 90
    struct AnimData
925b7e58 sago007 2011-04-25 16:35 91
    {
925b7e58 sago007 2011-04-25 16:35 92
        const NFont* font;
925b7e58 sago007 2011-04-25 16:35 93
        
925b7e58 sago007 2011-04-25 16:35 94
        SDL_Surface* dest;
925b7e58 sago007 2011-04-25 16:35 95
        SDL_Surface* src;
925b7e58 sago007 2011-04-25 16:35 96
        char* text;  // Buffer for efficient drawing
925b7e58 sago007 2011-04-25 16:35 97
        int height;
925b7e58 sago007 2011-04-25 16:35 98
        const int* charPos;
925b7e58 sago007 2011-04-25 16:35 99
        const int* charWidth;
925b7e58 sago007 2011-04-25 16:35 100
        int maxX;
925b7e58 sago007 2011-04-25 16:35 101
        
925b7e58 sago007 2011-04-25 16:35 102
        int index;
925b7e58 sago007 2011-04-25 16:35 103
        int letterNum;
925b7e58 sago007 2011-04-25 16:35 104
        int wordNum;
925b7e58 sago007 2011-04-25 16:35 105
        int lineNum;
925b7e58 sago007 2011-04-25 16:35 106
        int startX;
925b7e58 sago007 2011-04-25 16:35 107
        int startY;
925b7e58 sago007 2011-04-25 16:35 108
        void* userVar;
925b7e58 sago007 2011-04-25 16:35 109
        
925b7e58 sago007 2011-04-25 16:35 110
        SDL_Rect dirtyRect;
925b7e58 sago007 2011-04-25 16:35 111
    };
925b7e58 sago007 2011-04-25 16:35 112
    
925b7e58 sago007 2011-04-25 16:35 113
    // Function pointer
925b7e58 sago007 2011-04-25 16:35 114
    typedef void (*AnimFn)(int&, int&, AnimData&);
925b7e58 sago007 2011-04-25 16:35 115
    
925b7e58 sago007 2011-04-25 16:35 116
protected:
925b7e58 sago007 2011-04-25 16:35 117
    
925b7e58 sago007 2011-04-25 16:35 118
925b7e58 sago007 2011-04-25 16:35 119
    SDL_Surface* src;  // bitmap source of characters
925b7e58 sago007 2011-04-25 16:35 120
    SDL_Surface* dest; // Destination to blit to
925b7e58 sago007 2011-04-25 16:35 121
925b7e58 sago007 2011-04-25 16:35 122
    int height;
925b7e58 sago007 2011-04-25 16:35 123
925b7e58 sago007 2011-04-25 16:35 124
    int maxWidth;
925b7e58 sago007 2011-04-25 16:35 125
    int baseline;
925b7e58 sago007 2011-04-25 16:35 126
    int ascent;
925b7e58 sago007 2011-04-25 16:35 127
    int descent;
925b7e58 sago007 2011-04-25 16:35 128
925b7e58 sago007 2011-04-25 16:35 129
    int lineSpacing;
925b7e58 sago007 2011-04-25 16:35 130
    int letterSpacing;
925b7e58 sago007 2011-04-25 16:35 131
925b7e58 sago007 2011-04-25 16:35 132
    int charPos[256];
925b7e58 sago007 2011-04-25 16:35 133
    int charWidth[256];
925b7e58 sago007 2011-04-25 16:35 134
    int maxPos;
925b7e58 sago007 2011-04-25 16:35 135
    
925b7e58 sago007 2011-04-25 16:35 136
    void init();
925b7e58 sago007 2011-04-25 16:35 137
925b7e58 sago007 2011-04-25 16:35 138
    SDL_Rect drawToSurface(int x, int y, const char* text) const;
925b7e58 sago007 2011-04-25 16:35 139
    SDL_Rect drawToSurfacePos(int x, int y, NFont::AnimFn posFn) const;
925b7e58 sago007 2011-04-25 16:35 140
    
925b7e58 sago007 2011-04-25 16:35 141
    // Static variables
925b7e58 sago007 2011-04-25 16:35 142
    static char* buffer;  // Buffer for efficient drawing
925b7e58 sago007 2011-04-25 16:35 143
    static AnimData data;  // Data is wrapped in a struct so it can all be passed to 
925b7e58 sago007 2011-04-25 16:35 144
                                 // the function pointers for animation
925b7e58 sago007 2011-04-25 16:35 145
925b7e58 sago007 2011-04-25 16:35 146
public:
925b7e58 sago007 2011-04-25 16:35 147
    
925b7e58 sago007 2011-04-25 16:35 148
    // Static functions
925b7e58 sago007 2011-04-25 16:35 149
    static char* copyString(const char* c);
925b7e58 sago007 2011-04-25 16:35 150
    static Uint32 getPixel(SDL_Surface *Surface, int x, int y);
925b7e58 sago007 2011-04-25 16:35 151
    static inline SDL_Rect makeRect(Sint16 x, Sint16 y, Uint16 w, Uint16 h)
925b7e58 sago007 2011-04-25 16:35 152
    {
925b7e58 sago007 2011-04-25 16:35 153
        SDL_Rect r = {x, y, w, h};
925b7e58 sago007 2011-04-25 16:35 154
        return r;
925b7e58 sago007 2011-04-25 16:35 155
    }
925b7e58 sago007 2011-04-25 16:35 156
    static SDL_Rect rectUnion(const SDL_Rect& A, const SDL_Rect& B);
925b7e58 sago007 2011-04-25 16:35 157
    static SDL_Surface* copySurface(SDL_Surface *Surface);
925b7e58 sago007 2011-04-25 16:35 158
    static SDL_Surface* verticalGradient(SDL_Surface* targetSurface, Uint32 topColor, Uint32 bottomColor, int heightAdjust = 0);
925b7e58 sago007 2011-04-25 16:35 159
    
925b7e58 sago007 2011-04-25 16:35 160
    // Static accessors
925b7e58 sago007 2011-04-25 16:35 161
    static void setAnimData(void* data);
925b7e58 sago007 2011-04-25 16:35 162
    static void setBuffer(unsigned int size);
925b7e58 sago007 2011-04-25 16:35 163
    
925b7e58 sago007 2011-04-25 16:35 164
    // Constructors
925b7e58 sago007 2011-04-25 16:35 165
    NFont();
925b7e58 sago007 2011-04-25 16:35 166
    NFont(SDL_Surface* src);
925b7e58 sago007 2011-04-25 16:35 167
    NFont(SDL_Surface* dest, SDL_Surface* src);
925b7e58 sago007 2011-04-25 16:35 168
    #ifdef NFONT_USE_TTF
925b7e58 sago007 2011-04-25 16:35 169
        NFont(TTF_Font* ttf, SDL_Color fg);  // Alpha bg
925b7e58 sago007 2011-04-25 16:35 170
        NFont(TTF_Font* ttf, SDL_Color fg, SDL_Color bg);
925b7e58 sago007 2011-04-25 16:35 171
        NFont(const char* filename_ttf, Uint32 pointSize, SDL_Color fg, int style = TTF_STYLE_NORMAL);  // Alpha bg
925b7e58 sago007 2011-04-25 16:35 172
        NFont(const char* filename_ttf, Uint32 pointSize, SDL_Color fg, SDL_Color bg, int style = TTF_STYLE_NORMAL);
925b7e58 sago007 2011-04-25 16:35 173
        NFont(SDL_Surface* dest, TTF_Font* ttf, SDL_Color fg);  // Alpha bg
925b7e58 sago007 2011-04-25 16:35 174
        NFont(SDL_Surface* dest, TTF_Font* ttf, SDL_Color fg, SDL_Color bg);
925b7e58 sago007 2011-04-25 16:35 175
        NFont(SDL_Surface* dest, const char* filename_ttf, Uint32 pointSize, SDL_Color fg, int style = TTF_STYLE_NORMAL);  // Alpha bg
925b7e58 sago007 2011-04-25 16:35 176
        NFont(SDL_Surface* dest, const char* filename_ttf, Uint32 pointSize, SDL_Color fg, SDL_Color bg, int style = TTF_STYLE_NORMAL);
925b7e58 sago007 2011-04-25 16:35 177
    #endif
925b7e58 sago007 2011-04-25 16:35 178
925b7e58 sago007 2011-04-25 16:35 179
    ~NFont();
925b7e58 sago007 2011-04-25 16:35 180
925b7e58 sago007 2011-04-25 16:35 181
    // Loading
925b7e58 sago007 2011-04-25 16:35 182
    bool load(SDL_Surface* FontSurface);
925b7e58 sago007 2011-04-25 16:35 183
    bool load(SDL_Surface* destSurface, SDL_Surface* FontSurface);
925b7e58 sago007 2011-04-25 16:35 184
    #ifdef NFONT_USE_TTF
925b7e58 sago007 2011-04-25 16:35 185
        bool load(TTF_Font* ttf, SDL_Color fg);  // Alpha bg
925b7e58 sago007 2011-04-25 16:35 186
        bool load(TTF_Font* ttf, SDL_Color fg, SDL_Color bg);
925b7e58 sago007 2011-04-25 16:35 187
        bool load(const char* filename_ttf, Uint32 pointSize, SDL_Color fg, int style = TTF_STYLE_NORMAL);  // Alpha bg
925b7e58 sago007 2011-04-25 16:35 188
        bool load(const char* filename_ttf, Uint32 pointSize, SDL_Color fg, SDL_Color bg, int style = TTF_STYLE_NORMAL);
925b7e58 sago007 2011-04-25 16:35 189
    #endif
925b7e58 sago007 2011-04-25 16:35 190
925b7e58 sago007 2011-04-25 16:35 191
    void freeSurface();
925b7e58 sago007 2011-04-25 16:35 192
925b7e58 sago007 2011-04-25 16:35 193
    // Drawing
925b7e58 sago007 2011-04-25 16:35 194
    SDL_Rect draw(int x, int y, const char* formatted_text, ...) const;
925b7e58 sago007 2011-04-25 16:35 195
    SDL_Rect drawCenter(int x, int y, const char* formatted_text, ...) const;
925b7e58 sago007 2011-04-25 16:35 196
    SDL_Rect drawRight(int x, int y, const char* formatted_text, ...) const;
925b7e58 sago007 2011-04-25 16:35 197
    SDL_Rect drawPos(int x, int y, NFont::AnimFn posFn, const char* text, ...) const;
925b7e58 sago007 2011-04-25 16:35 198
    SDL_Rect drawAll(int x, int y, NFont::AnimFn allFn, const char* text, ...) const;
925b7e58 sago007 2011-04-25 16:35 199
    
925b7e58 sago007 2011-04-25 16:35 200
    // Getters
925b7e58 sago007 2011-04-25 16:35 201
    SDL_Surface* getDest() const;
925b7e58 sago007 2011-04-25 16:35 202
    SDL_Surface* getSurface() const;
925b7e58 sago007 2011-04-25 16:35 203
    int getHeight(const char* formatted_text = NULL, ...) const;
925b7e58 sago007 2011-04-25 16:35 204
    int getWidth(const char* formatted_text, ...) const;
925b7e58 sago007 2011-04-25 16:35 205
    int getSpacing() const;
925b7e58 sago007 2011-04-25 16:35 206
    int getLineSpacing() const;
925b7e58 sago007 2011-04-25 16:35 207
    int getBaseline() const;
925b7e58 sago007 2011-04-25 16:35 208
    int getAscent(const char character) const;
925b7e58 sago007 2011-04-25 16:35 209
    int getAscent(const char* formatted_text = NULL, ...) const;
925b7e58 sago007 2011-04-25 16:35 210
    int getDescent(const char character) const;
925b7e58 sago007 2011-04-25 16:35 211
    int getDescent(const char* formatted_text = NULL, ...) const;
925b7e58 sago007 2011-04-25 16:35 212
    int getMaxWidth() const;
925b7e58 sago007 2011-04-25 16:35 213
    
925b7e58 sago007 2011-04-25 16:35 214
    // Setters
925b7e58 sago007 2011-04-25 16:35 215
    void setDest(SDL_Surface* Dest);
925b7e58 sago007 2011-04-25 16:35 216
    void setSpacing(int LetterSpacing);
925b7e58 sago007 2011-04-25 16:35 217
    void setLineSpacing(int LineSpacing);
925b7e58 sago007 2011-04-25 16:35 218
    int setBaseline(int Baseline = -1);
925b7e58 sago007 2011-04-25 16:35 219
925b7e58 sago007 2011-04-25 16:35 220
};
925b7e58 sago007 2011-04-25 16:35 221
925b7e58 sago007 2011-04-25 16:35 222
925b7e58 sago007 2011-04-25 16:35 223
925b7e58 sago007 2011-04-25 16:35 224
#endif // _NFONT_H__
1970-01-01 00:00 225