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
c53e6443 sago007 2012-04-17 11:04 18
    support for the newline character ('\n'), animation, and extended ASCII
c53e6443 sago007 2012-04-17 11:04 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.
c53e6443 sago007 2012-04-17 11:04 21
c53e6443 sago007 2012-04-17 11:04 22
    NFont has the ability to animate the font in two ways: It's position or
c53e6443 sago007 2012-04-17 11:04 23
    it's everything.  By using drawPos(), you can use a function you create
c53e6443 sago007 2012-04-17 11:04 24
    to handle the final positions of the drawn characters.  With drawAll(),
c53e6443 sago007 2012-04-17 11:04 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
c53e6443 sago007 2012-04-17 11:04 29
    surface.  You have to set the destination before the font can be used.  Be
c53e6443 sago007 2012-04-17 11:04 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()).
c53e6443 sago007 2012-04-17 11:04 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 { | } ~
c53e6443 sago007 2012-04-17 11:04 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 { | } ~ � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � � �
c53e6443 sago007 2012-04-17 11:04 40
c53e6443 sago007 2012-04-17 11:04 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:
c53e6443 sago007 2012-04-17 11:04 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.
c53e6443 sago007 2012-04-17 11:04 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
c53e6443 sago007 2012-04-17 11:04 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
c53e6443 sago007 2012-04-17 11:04 89
	// Nested struct
c53e6443 sago007 2012-04-17 11:04 90
	struct AnimData
c53e6443 sago007 2012-04-17 11:04 91
	{
c53e6443 sago007 2012-04-17 11:04 92
		const NFont* font;
c53e6443 sago007 2012-04-17 11:04 93
c53e6443 sago007 2012-04-17 11:04 94
		SDL_Surface* dest;
c53e6443 sago007 2012-04-17 11:04 95
		SDL_Surface* src;
c53e6443 sago007 2012-04-17 11:04 96
		char* text;  // Buffer for efficient drawing
c53e6443 sago007 2012-04-17 11:04 97
		int height;
c53e6443 sago007 2012-04-17 11:04 98
		const int* charPos;
c53e6443 sago007 2012-04-17 11:04 99
		const int* charWidth;
c53e6443 sago007 2012-04-17 11:04 100
		int maxX;
c53e6443 sago007 2012-04-17 11:04 101
c53e6443 sago007 2012-04-17 11:04 102
		int index;
c53e6443 sago007 2012-04-17 11:04 103
		int letterNum;
c53e6443 sago007 2012-04-17 11:04 104
		int wordNum;
c53e6443 sago007 2012-04-17 11:04 105
		int lineNum;
c53e6443 sago007 2012-04-17 11:04 106
		int startX;
c53e6443 sago007 2012-04-17 11:04 107
		int startY;
c53e6443 sago007 2012-04-17 11:04 108
		void* userVar;
c53e6443 sago007 2012-04-17 11:04 109
c53e6443 sago007 2012-04-17 11:04 110
		SDL_Rect dirtyRect;
c53e6443 sago007 2012-04-17 11:04 111
	};
c53e6443 sago007 2012-04-17 11:04 112
c53e6443 sago007 2012-04-17 11:04 113
	// Function pointer
c53e6443 sago007 2012-04-17 11:04 114
	typedef void (*AnimFn)(int&, int&, AnimData&);
c53e6443 sago007 2012-04-17 11:04 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
c53e6443 sago007 2012-04-17 11:04 119
	SDL_Surface* src;  // bitmap source of characters
c53e6443 sago007 2012-04-17 11:04 120
	SDL_Surface* dest; // Destination to blit to
c53e6443 sago007 2012-04-17 11:04 121
c53e6443 sago007 2012-04-17 11:04 122
	int height;
c53e6443 sago007 2012-04-17 11:04 123
c53e6443 sago007 2012-04-17 11:04 124
	int maxWidth;
c53e6443 sago007 2012-04-17 11:04 125
	int baseline;
c53e6443 sago007 2012-04-17 11:04 126
	int ascent;
c53e6443 sago007 2012-04-17 11:04 127
	int descent;
c53e6443 sago007 2012-04-17 11:04 128
c53e6443 sago007 2012-04-17 11:04 129
	int lineSpacing;
c53e6443 sago007 2012-04-17 11:04 130
	int letterSpacing;
925b7e58 sago007 2011-04-25 16:35 131
c53e6443 sago007 2012-04-17 11:04 132
	int charPos[256];
c53e6443 sago007 2012-04-17 11:04 133
	int charWidth[256];
c53e6443 sago007 2012-04-17 11:04 134
	int maxPos;
925b7e58 sago007 2011-04-25 16:35 135
c53e6443 sago007 2012-04-17 11:04 136
	void init();
925b7e58 sago007 2011-04-25 16:35 137
c53e6443 sago007 2012-04-17 11:04 138
	SDL_Rect drawToSurface(int x, int y, const char* text) const;
c53e6443 sago007 2012-04-17 11:04 139
	SDL_Rect drawToSurfacePos(int x, int y, NFont::AnimFn posFn) const;
925b7e58 sago007 2011-04-25 16:35 140
c53e6443 sago007 2012-04-17 11:04 141
	// Static variables
c53e6443 sago007 2012-04-17 11:04 142
	static char* buffer;  // Buffer for efficient drawing
c53e6443 sago007 2012-04-17 11:04 143
	static AnimData data;  // Data is wrapped in a struct so it can all be passed to
c53e6443 sago007 2012-04-17 11:04 144
	// the function pointers for animation
925b7e58 sago007 2011-04-25 16:35 145
925b7e58 sago007 2011-04-25 16:35 146
public:
c53e6443 sago007 2012-04-17 11:04 147
c53e6443 sago007 2012-04-17 11:04 148
	// Static functions
c53e6443 sago007 2012-04-17 11:04 149
	static char* copyString(const char* c);
c53e6443 sago007 2012-04-17 11:04 150
	static Uint32 getPixel(SDL_Surface *Surface, int x, int y);
c53e6443 sago007 2012-04-17 11:04 151
	static inline SDL_Rect makeRect(Sint16 x, Sint16 y, Uint16 w, Uint16 h)
c53e6443 sago007 2012-04-17 11:04 152
	{
c53e6443 sago007 2012-04-17 11:04 153
		SDL_Rect r = {x, y, w, h};
c53e6443 sago007 2012-04-17 11:04 154
		return r;
c53e6443 sago007 2012-04-17 11:04 155
	}
c53e6443 sago007 2012-04-17 11:04 156
	static SDL_Rect rectUnion(const SDL_Rect& A, const SDL_Rect& B);
c53e6443 sago007 2012-04-17 11:04 157
	static SDL_Surface* copySurface(SDL_Surface *Surface);
c53e6443 sago007 2012-04-17 11:04 158
	static SDL_Surface* verticalGradient(SDL_Surface* targetSurface, Uint32 topColor, Uint32 bottomColor, int heightAdjust = 0);
c53e6443 sago007 2012-04-17 11:04 159
c53e6443 sago007 2012-04-17 11:04 160
	// Static accessors
c53e6443 sago007 2012-04-17 11:04 161
	static void setAnimData(void* data);
c53e6443 sago007 2012-04-17 11:04 162
	static void setBuffer(unsigned int size);
c53e6443 sago007 2012-04-17 11:04 163
c53e6443 sago007 2012-04-17 11:04 164
	// Constructors
c53e6443 sago007 2012-04-17 11:04 165
	NFont();
c53e6443 sago007 2012-04-17 11:04 166
	NFont(SDL_Surface* src);
c53e6443 sago007 2012-04-17 11:04 167
	NFont(SDL_Surface* dest, SDL_Surface* src);
c53e6443 sago007 2012-04-17 11:04 168
#ifdef NFONT_USE_TTF
c53e6443 sago007 2012-04-17 11:04 169
	NFont(TTF_Font* ttf, SDL_Color fg);  // Alpha bg
c53e6443 sago007 2012-04-17 11:04 170
	NFont(TTF_Font* ttf, SDL_Color fg, SDL_Color bg);
c53e6443 sago007 2012-04-17 11:04 171
	NFont(const char* filename_ttf, Uint32 pointSize, SDL_Color fg, int style = TTF_STYLE_NORMAL);  // Alpha bg
c53e6443 sago007 2012-04-17 11:04 172
	NFont(const char* filename_ttf, Uint32 pointSize, SDL_Color fg, SDL_Color bg, int style = TTF_STYLE_NORMAL);
c53e6443 sago007 2012-04-17 11:04 173
	NFont(SDL_Surface* dest, TTF_Font* ttf, SDL_Color fg);  // Alpha bg
c53e6443 sago007 2012-04-17 11:04 174
	NFont(SDL_Surface* dest, TTF_Font* ttf, SDL_Color fg, SDL_Color bg);
c53e6443 sago007 2012-04-17 11:04 175
	NFont(SDL_Surface* dest, const char* filename_ttf, Uint32 pointSize, SDL_Color fg, int style = TTF_STYLE_NORMAL);  // Alpha bg
c53e6443 sago007 2012-04-17 11:04 176
	NFont(SDL_Surface* dest, const char* filename_ttf, Uint32 pointSize, SDL_Color fg, SDL_Color bg, int style = TTF_STYLE_NORMAL);
c53e6443 sago007 2012-04-17 11:04 177
#endif
c53e6443 sago007 2012-04-17 11:04 178
c53e6443 sago007 2012-04-17 11:04 179
	~NFont();
c53e6443 sago007 2012-04-17 11:04 180
c53e6443 sago007 2012-04-17 11:04 181
	// Loading
c53e6443 sago007 2012-04-17 11:04 182
	bool load(SDL_Surface* FontSurface);
c53e6443 sago007 2012-04-17 11:04 183
	bool load(SDL_Surface* destSurface, SDL_Surface* FontSurface);
c53e6443 sago007 2012-04-17 11:04 184
#ifdef NFONT_USE_TTF
c53e6443 sago007 2012-04-17 11:04 185
	bool load(TTF_Font* ttf, SDL_Color fg);  // Alpha bg
c53e6443 sago007 2012-04-17 11:04 186
	bool load(TTF_Font* ttf, SDL_Color fg, SDL_Color bg);
c53e6443 sago007 2012-04-17 11:04 187
	bool load(const char* filename_ttf, Uint32 pointSize, SDL_Color fg, int style = TTF_STYLE_NORMAL);  // Alpha bg
c53e6443 sago007 2012-04-17 11:04 188
	bool load(const char* filename_ttf, Uint32 pointSize, SDL_Color fg, SDL_Color bg, int style = TTF_STYLE_NORMAL);
c53e6443 sago007 2012-04-17 11:04 189
#endif
c53e6443 sago007 2012-04-17 11:04 190
c53e6443 sago007 2012-04-17 11:04 191
	void freeSurface();
c53e6443 sago007 2012-04-17 11:04 192
c53e6443 sago007 2012-04-17 11:04 193
	// Drawing
c53e6443 sago007 2012-04-17 11:04 194
	SDL_Rect draw(int x, int y, const char* formatted_text, ...) const;
c53e6443 sago007 2012-04-17 11:04 195
	SDL_Rect drawCenter(int x, int y, const char* formatted_text, ...) const;
c53e6443 sago007 2012-04-17 11:04 196
	SDL_Rect drawRight(int x, int y, const char* formatted_text, ...) const;
c53e6443 sago007 2012-04-17 11:04 197
	SDL_Rect drawPos(int x, int y, NFont::AnimFn posFn, const char* text, ...) const;
c53e6443 sago007 2012-04-17 11:04 198
	SDL_Rect drawAll(int x, int y, NFont::AnimFn allFn, const char* text, ...) const;
c53e6443 sago007 2012-04-17 11:04 199
c53e6443 sago007 2012-04-17 11:04 200
	// Getters
c53e6443 sago007 2012-04-17 11:04 201
	SDL_Surface* getDest() const;
c53e6443 sago007 2012-04-17 11:04 202
	SDL_Surface* getSurface() const;
c53e6443 sago007 2012-04-17 11:04 203
	int getHeight(const char* formatted_text = NULL, ...) const;
c53e6443 sago007 2012-04-17 11:04 204
	int getWidth(const char* formatted_text, ...) const;
c53e6443 sago007 2012-04-17 11:04 205
	int getSpacing() const;
c53e6443 sago007 2012-04-17 11:04 206
	int getLineSpacing() const;
c53e6443 sago007 2012-04-17 11:04 207
	int getBaseline() const;
c53e6443 sago007 2012-04-17 11:04 208
	int getAscent(const char character) const;
c53e6443 sago007 2012-04-17 11:04 209
	int getAscent(const char* formatted_text = NULL, ...) const;
c53e6443 sago007 2012-04-17 11:04 210
	int getDescent(const char character) const;
c53e6443 sago007 2012-04-17 11:04 211
	int getDescent(const char* formatted_text = NULL, ...) const;
c53e6443 sago007 2012-04-17 11:04 212
	int getMaxWidth() const;
c53e6443 sago007 2012-04-17 11:04 213
c53e6443 sago007 2012-04-17 11:04 214
	// Setters
c53e6443 sago007 2012-04-17 11:04 215
	void setDest(SDL_Surface* Dest);
c53e6443 sago007 2012-04-17 11:04 216
	void setSpacing(int LetterSpacing);
c53e6443 sago007 2012-04-17 11:04 217
	void setLineSpacing(int LineSpacing);
c53e6443 sago007 2012-04-17 11:04 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