dungeon_crawler

annotate prototype/drawtext/drawtext.h @ 24:e122ba214ee1

implementing the command console
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 23 Aug 2012 18:03:11 +0300
parents
children 2fc004802739
rev   line source
nuclear@24 1 /*
nuclear@24 2 libdrawtext - a simple library for fast text rendering in OpenGL
nuclear@24 3 Copyright (C) 2011 John Tsiombikas <nuclear@member.fsf.org>
nuclear@24 4
nuclear@24 5 This program is free software: you can redistribute it and/or modify
nuclear@24 6 it under the terms of the GNU Lesser General Public License as published by
nuclear@24 7 the Free Software Foundation, either version 3 of the License, or
nuclear@24 8 (at your option) any later version.
nuclear@24 9
nuclear@24 10 This program is distributed in the hope that it will be useful,
nuclear@24 11 but WITHOUT ANY WARRANTY; without even the implied warranty of
nuclear@24 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
nuclear@24 13 GNU Lesser General Public License for more details.
nuclear@24 14
nuclear@24 15 You should have received a copy of the GNU Lesser General Public License
nuclear@24 16 along with this program. If not, see <http://www.gnu.org/licenses/>.
nuclear@24 17 */
nuclear@24 18 #ifndef TEXT_H_
nuclear@24 19 #define TEXT_H_
nuclear@24 20
nuclear@24 21 #include <stdio.h>
nuclear@24 22 #include <stdlib.h>
nuclear@24 23
nuclear@24 24 struct dtx_font;
nuclear@24 25 struct dtx_glyphmap;
nuclear@24 26
nuclear@24 27 /* draw buffering modes */
nuclear@24 28 enum {
nuclear@24 29 DTX_NBF, /* unbuffered */
nuclear@24 30 DTX_LBF, /* line buffered */
nuclear@24 31 DTX_FBF /* fully buffered */
nuclear@24 32 };
nuclear@24 33
nuclear@24 34 struct dtx_box {
nuclear@24 35 float x, y;
nuclear@24 36 float width, height;
nuclear@24 37 };
nuclear@24 38
nuclear@24 39 #ifdef __cplusplus
nuclear@24 40 extern "C" {
nuclear@24 41 #endif
nuclear@24 42
nuclear@24 43 /* Open a truetype/opentype/whatever font.
nuclear@24 44 *
nuclear@24 45 * If sz is non-zero, the default ASCII glyphmap at the requested point size is
nuclear@24 46 * automatically created as well, and ready to use.
nuclear@24 47 *
nuclear@24 48 * To use other unicode ranges and different font sizes you must first call
nuclear@24 49 * dtx_prepare or dtx_prepare_range before issuing any drawing calls, otherwise
nuclear@24 50 * nothing will be rendered.
nuclear@24 51 */
nuclear@24 52 struct dtx_font *dtx_open_font(const char *fname, int sz);
nuclear@24 53 void dtx_close_font(struct dtx_font *fnt);
nuclear@24 54
nuclear@24 55 /* prepare an ASCII glyphmap for the specified font size */
nuclear@24 56 void dtx_prepare(struct dtx_font *fnt, int sz);
nuclear@24 57 /* prepare an arbitrary unicode range glyphmap for the specified font size */
nuclear@24 58 void dtx_prepare_range(struct dtx_font *fnt, int sz, int cstart, int cend);
nuclear@24 59
nuclear@24 60 /* Finds the glyphmap that contains the specified character code and matches the requested size
nuclear@24 61 * Returns null if it hasn't been created (you should call dtx_prepare/dtx_prepare_range).
nuclear@24 62 */
nuclear@24 63 struct dtx_glyphmap *dtx_get_font_glyphmap(struct dtx_font *fnt, int sz, int code);
nuclear@24 64
nuclear@24 65 /* Finds the glyphmap that contains the specified unicode range and matches the requested font size
nuclear@24 66 * Will automatically generate one if it can't find it.
nuclear@24 67 */
nuclear@24 68 struct dtx_glyphmap *dtx_get_font_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend);
nuclear@24 69
nuclear@24 70 /* Creates and returns a glyphmap for a particular unicode range and font size.
nuclear@24 71 * The generated glyphmap is added to the font's list of glyphmaps.
nuclear@24 72 */
nuclear@24 73 struct dtx_glyphmap *dtx_create_glyphmap_range(struct dtx_font *fnt, int sz, int cstart, int cend);
nuclear@24 74 /* free a glyphmap */
nuclear@24 75 void dtx_free_glyphmap(struct dtx_glyphmap *gmap);
nuclear@24 76
nuclear@24 77 /* returns a pointer to the raster image of a glyphmap (1 byte per pixel grayscale) */
nuclear@24 78 unsigned char *dtx_get_glyphmap_image(struct dtx_glyphmap *gmap);
nuclear@24 79 /* returns the width of the glyphmap image in pixels */
nuclear@24 80 int dtx_get_glyphmap_width(struct dtx_glyphmap *gmap);
nuclear@24 81 /* returns the height of the glyphmap image in pixels */
nuclear@24 82 int dtx_get_glyphmap_height(struct dtx_glyphmap *gmap);
nuclear@24 83
nuclear@24 84 /* The following functions can be used even when the library is compiled without
nuclear@24 85 * freetype support. (TODO)
nuclear@24 86 */
nuclear@24 87 struct dtx_glyphmap *dtx_load_glyphmap(const char *fname);
nuclear@24 88 struct dtx_glyphmap *dtx_load_glyphmap_stream(FILE *fp);
nuclear@24 89 int dtx_save_glyphmap(const char *fname, const struct dtx_glyphmap *gmap);
nuclear@24 90 int dtx_save_glyphmap_stream(FILE *fp, const struct dtx_glyphmap *gmap);
nuclear@24 91
nuclear@24 92
nuclear@24 93 /* ---- rendering ---- */
nuclear@24 94
nuclear@24 95 /* before drawing anything this function must set the font to use */
nuclear@24 96 void dtx_use_font(struct dtx_font *fnt, int sz);
nuclear@24 97
nuclear@24 98 /* sets the buffering mode
nuclear@24 99 * - DTX_NBUF: every call to dtx_string gets rendered immediately.
nuclear@24 100 * - DTX_LBUF: renders when the buffer is full or the string contains a newline.
nuclear@24 101 * - DTX_FBUF: renders only when the buffer is full (you must call dtx_flush explicitly).
nuclear@24 102 */
nuclear@24 103 void dtx_draw_buffering(int mode);
nuclear@24 104
nuclear@24 105 /* Sets the vertex attribute indices to use for passing vertex and texture coordinate
nuclear@24 106 * data. By default both are -1 which means you don't have to use a shader, and if you
nuclear@24 107 * do they are accessible through gl_Vertex and gl_MultiTexCoord0, as usual.
nuclear@24 108 *
nuclear@24 109 * NOTE: If you are using OpenGL ES 2.x or OpenGL >= 3.1 pure (non-compatibility) context
nuclear@24 110 * you must specify valid attribute indices.
nuclear@24 111 */
nuclear@24 112 void dtx_vertex_attribs(int vert_attr, int tex_attr);
nuclear@24 113
nuclear@24 114 /* draws a single glyph at the origin */
nuclear@24 115 void dtx_glyph(int code);
nuclear@24 116 /* draws a utf-8 string starting at the origin. \n \r and \t are handled appropriately. */
nuclear@24 117 void dtx_string(const char *str);
nuclear@24 118
nuclear@24 119 /* render any pending glyphs (see dtx_draw_buffering) */
nuclear@24 120 void dtx_flush(void);
nuclear@24 121
nuclear@24 122
nuclear@24 123 /* ---- encodings ---- */
nuclear@24 124
nuclear@24 125 /* returns a pointer to the next character in a utf-8 stream */
nuclear@24 126 char *dtx_utf8_next_char(char *str);
nuclear@24 127
nuclear@24 128 /* returns the unicode character codepoint of the utf-8 character starting at str */
nuclear@24 129 int dtx_utf8_char_code(const char *str);
nuclear@24 130
nuclear@24 131 /* returns the number of bytes of the utf-8 character starting at str */
nuclear@24 132 int dtx_utf8_nbytes(const char *str);
nuclear@24 133
nuclear@24 134 /* returns the number of utf-8 character in a zero-terminated utf-8 string */
nuclear@24 135 int dtx_utf8_char_count(const char *str);
nuclear@24 136
nuclear@24 137 /* Converts a unicode code-point to a utf-8 character by filling in the buffer
nuclear@24 138 * passed at the second argument, and returns the number of bytes taken by that
nuclear@24 139 * utf-8 character.
nuclear@24 140 * It's valid to pass a null buffer pointer, in which case only the byte count is
nuclear@24 141 * returned (useful to figure out how much memory to allocate for a buffer).
nuclear@24 142 */
nuclear@24 143 size_t dtx_utf8_from_char_code(int code, char *buf);
nuclear@24 144
nuclear@24 145 /* Converts a unicode utf-16 wchar_t string to utf-8, filling in the buffer passed
nuclear@24 146 * at the second argument. Returns the size of the resulting string in bytes.
nuclear@24 147 *
nuclear@24 148 * It's valid to pass a null buffer pointer, in which case only the size gets
nuclear@24 149 * calculated and returned, which is useful for figuring out how much memory to
nuclear@24 150 * allocate for the utf-8 buffer.
nuclear@24 151 */
nuclear@24 152 size_t dtx_utf8_from_string(const wchar_t *str, char *buf);
nuclear@24 153
nuclear@24 154
nuclear@24 155 /* ---- metrics ---- */
nuclear@24 156
nuclear@24 157 /* rendered dimensions of a single glyph */
nuclear@24 158 void dtx_glyph_box(int code, struct dtx_box *box);
nuclear@24 159 float dtx_glyph_width(int code);
nuclear@24 160 float dtx_glyph_height(int code);
nuclear@24 161
nuclear@24 162 /* rendered dimensions of a string */
nuclear@24 163 void dtx_string_box(const char *str, struct dtx_box *box);
nuclear@24 164 float dtx_string_width(const char *str);
nuclear@24 165 float dtx_string_height(const char *str);
nuclear@24 166
nuclear@24 167 /* returns the horizontal position of the n-th character of the rendered string
nuclear@24 168 * (useful for placing cursors)
nuclear@24 169 */
nuclear@24 170 float dtx_char_pos(const char *str, int n);
nuclear@24 171
nuclear@24 172 int dtx_char_at_pt(const char *str, float pt);
nuclear@24 173
nuclear@24 174 #ifdef __cplusplus
nuclear@24 175 }
nuclear@24 176 #endif
nuclear@24 177
nuclear@24 178 #endif /* TEXT_H_ */