texturefont.h (2228B)
1 /* Copyright 2013 Brian Swetland <swetland@frotz.net> 2 * 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef _TEXTUREFONT_H_ 17 #define _TEXTUREFONT_H_ 18 19 #include "app.h" 20 #include "matrix.h" 21 22 #include "Effect.h" 23 #include "Renderable.h" 24 25 struct CharInfo { 26 // location in texture 27 u16 x; 28 u16 y; 29 u16 w; 30 u16 h; 31 // adjustment relative to 0,0 baseline 32 s16 dx; 33 s16 dy; 34 // adjustment to the next character; 35 u16 advance; 36 u16 unused; 37 }; 38 39 struct FontInfo { 40 u32 magic; 41 u32 first; 42 u32 count; 43 u32 unused; 44 u32 ascent_max; 45 u32 descent_max; 46 u32 height_max; 47 u32 unused2; 48 CharInfo info[0]; 49 }; 50 51 #define TEXTUREFONT_MAGIC 0x746E6F46 52 53 struct CharData { 54 int x; 55 int y; 56 int id; 57 int rgba; 58 }; 59 60 class TextureFont { 61 friend class Text; 62 public: 63 void measure(const char *s, unsigned *width, unsigned *height); 64 int getMaxHeight(void) { return header->height_max; } 65 int getMaxAscent(void) { return header->ascent_max; } 66 static TextureFont *load(const char *fontname); 67 68 private: 69 TextureFont(void) {} 70 DISALLOW_COPY_AND_ASSIGN(TextureFont); 71 int init(const char *fontname); 72 73 FontInfo *header; 74 CharInfo *info; 75 unsigned first; 76 unsigned last; 77 78 Effect *effect; 79 VertexBuffer cbuf; 80 Texture2D glyphs; 81 unsigned tbid; 82 }; 83 84 class Text : public Renderable { 85 public: 86 void printf(int x, int y, const char *fmt, ...); 87 void puts(int x, int y, const char *s); 88 void setColor(unsigned rgba); 89 void render(void); 90 void clear(void); 91 static Text* create(TextureFont *_font); 92 protected: 93 Text(void) : Renderable() {}; 94 private: 95 DISALLOW_COPY_AND_ASSIGN(Text); 96 97 TextureFont *font; 98 99 VertexBuffer vtx; 100 VertexAttributes attr; 101 102 CharData *data; 103 CharData *next; 104 unsigned count; 105 unsigned max; 106 107 unsigned color; 108 int dirty; 109 }; 110 111 #endif