DrawText.fxh (9359B)
1 #ifndef _DRAWTEXT_H_ 2 #define _DRAWTEXT_H_ 3 4 #define _DRAWTEXT_GRID_X 14.0 5 #define _DRAWTEXT_GRID_Y 7.0 6 7 8 /////////////////////////////////////////////////////////////////////////////////////////////////////// 9 // // 10 // DrawText.fxh by kingreic1992 ( update: Sep.28.2019 ) // 11 // // 12 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++// 13 // // 14 // Available functions: // 15 // DrawText_String( offset, text size, xy ratio, input coord, string array, array size, output) // 16 // float2 offset = top left corner of string, screen hight pixel unit. // 17 // float text size = text size, screen hight pixel unit. // 18 // float xy ratio = xy ratio of text. // 19 // float2 input coord = current texture coord. // 20 // int string array = string data in float2 array format, ex: "Demo Text" // 21 // int String0[9] = { __D, __e, __m, __o, __Space, __T, __e, __x, __t}; // 22 // int string size = size of the string array. // 23 // float output = output. // 24 // // 25 // DrawText_Digit( offset, text size, xy ratio, input coord, precision after dot, data, output) // 26 // float2 offset = same as DrawText_String. // 27 // float text size = same as DrawText_String. // 28 // float xy ratio = same as DrawText_String. // 29 // float2 input coord = same as DrawText_String. // 30 // int precision = digits after dot. // 31 // float data = input float. // 32 // float output = output. // 33 // // 34 // float2 DrawText_Shift(offset, shift, text size, xy ratio) // 35 // float2 offset = same as DrawText_String. // 36 // float2 shift = shift line(y) and column. // 37 // float text size = same as DrawText_String. // 38 // float xy ratio = same as DrawText_String. // 39 // // 40 /////////////////////////////////////////////////////////////////////////////////////////////////////// 41 42 43 //Sample Usage 44 45 /* 46 47 #include "DrawText.fxh" 48 49 float4 main_fragment( float4 position : POSITION, 50 float2 txcoord : TEXCOORD) : COLOR { 51 float res = 0.0; 52 53 int line0[9] = { __D, __e, __m, __o, __Space, __T, __e, __x, __t }; //Demo Text 54 int line1[15] = { __b, __y, __Space, __k, __i, __n, __g, __e, __r, __i, __c, __1, __9, __9, __2 }; //by kingeric1992 55 int line2[6] = { __S, __i, __z, __e, __Colon, __Space }; // Size: %d. 56 57 DrawText_String(float2(100.0 , 100.0), 32, 1, txcoord, line0, 9, res); 58 DrawText_String(float2(100.0 , 134.0), textSize, 1, txcoord, line1, 15, res); 59 DrawText_String(DrawText_Shift(float2(100.0 , 134.0), int2(0, 1), textSize, 1), 18, 1, txcoord, line2, 6, res); 60 DrawText_Digit(DrawText_Shift(DrawText_Shift(float2(100.0 , 134.0), int2(0, 1), textSize, 1), int2(8, 0), 18, 1), 61 18, 1, txcoord, 0, textSize, res); 62 return res; 63 } 64 */ 65 66 //Text display 67 //Character indexing 68 #define __Space 0 // (space) 69 #define __Exclam 1 // ! 70 #define __Quote 2 // " 71 #define __Pound 3 // # 72 #define __Dollar 4 // $ 73 #define __Percent 5 // % 74 #define __And 6 // & 75 #define __sQuote 7 // ' 76 #define __rBrac_O 8 // ( 77 #define __rBrac_C 9 // ) 78 #define __Asterisk 10 // * 79 #define __Plus 11 // + 80 #define __Comma 12 // , 81 #define __Minus 13 // - 82 83 #define __Dot 14 // . 84 #define __Slash 15 // / 85 #define __0 16 // 0 86 #define __1 17 // 1 87 #define __2 18 // 2 88 #define __3 19 // 3 89 #define __4 20 // 4 90 #define __5 21 // 5 91 #define __6 22 // 6 92 #define __7 23 // 7 93 #define __8 24 // 8 94 #define __9 25 // 9 95 #define __Colon 26 // : 96 #define __sColon 27 // ; 97 98 #define __Less 28 // < 99 #define __Equals 29 // = 100 #define __Greater 30 // > 101 #define __Question 31 // ? 102 #define __at 32 // @ 103 #define __A 33 // A 104 #define __B 34 // B 105 #define __C 35 // C 106 #define __D 36 // D 107 #define __E 37 // E 108 #define __F 38 // F 109 #define __G 39 // G 110 #define __H 40 // H 111 #define __I 41 // I 112 113 #define __J 42 // J 114 #define __K 43 // K 115 #define __L 44 // L 116 #define __M 45 // M 117 #define __N 46 // N 118 #define __O 47 // O 119 #define __P 48 // P 120 #define __Q 49 // Q 121 #define __R 50 // R 122 #define __S 51 // S 123 #define __T 52 // T 124 #define __U 53 // U 125 #define __V 54 // V 126 #define __W 55 // W 127 128 #define __X 56 // X 129 #define __Y 57 // Y 130 #define __Z 58 // Z 131 #define __sBrac_O 59 // [ 132 #define __Backslash 60 // \.. 133 #define __sBrac_C 61 // ] 134 #define __Caret 62 // ^ 135 #define __Underscore 63 // _ 136 #define __Punc 64 // ` 137 #define __a 65 // a 138 #define __b 66 // b 139 #define __c 67 // c 140 #define __d 68 // d 141 #define __e 69 // e 142 143 #define __f 70 // f 144 #define __g 71 // g 145 #define __h 72 // h 146 #define __i 73 // i 147 #define __j 74 // j 148 #define __k 75 // k 149 #define __l 76 // l 150 #define __m 77 // m 151 #define __n 78 // n 152 #define __o 79 // o 153 #define __p 80 // p 154 #define __q 81 // q 155 #define __r 82 // r 156 #define __s 83 // s 157 158 #define __t 84 // t 159 #define __u 85 // u 160 #define __v 86 // v 161 #define __w 87 // w 162 #define __x 88 // x 163 #define __y 89 // y 164 #define __z 90 // z 165 #define __cBrac_O 91 // { 166 #define __vBar 92 // | 167 #define __cBrac_C 93 // } 168 #define __Tilde 94 // ~ 169 #define __tridot 95 // (...) 170 #define __empty0 96 // (null) 171 #define __empty1 97 // (null) 172 //Character indexing ends 173 174 texture Texttex < source = "FontAtlas.png"; > { 175 Width = 512; 176 Height = 512; 177 }; 178 179 sampler samplerText { 180 Texture = Texttex; 181 }; 182 183 //accomodate for undef array size. 184 #define DrawText_String( pos, size, ratio, tex, array, arrSize, output ) \ 185 { float text = 0.0; \ 186 float2 uv = (tex * float2(BUFFER_WIDTH, BUFFER_HEIGHT) - pos) / size; \ 187 uv.y = saturate(uv.y); \ 188 uv.x *= ratio * 2.0; \ 189 float id = array[int(trunc(uv.x))]; \ 190 if(uv.x <= arrSize && uv.x >= 0.0) \ 191 text = tex2D(samplerText, (frac(uv) + float2( id % 14.0, trunc(id / 14.0))) \ 192 / float2( _DRAWTEXT_GRID_X, _DRAWTEXT_GRID_Y) ).x; \ 193 output += text; } 194 195 float2 DrawText_Shift( float2 pos, int2 shift, float size, float ratio ) { 196 return pos + size * shift * float2(0.5, 1.0) / ratio; 197 } 198 199 void DrawText_Digit( float2 pos, float size, float ratio, float2 tex, int digit, float data, inout float res) { 200 int digits[13] = { 201 __0, __1, __2, __3, __4, __5, __6, __7, __8, __9, __Minus, __Space, __Dot 202 }; 203 204 float2 uv = (tex * float2(BUFFER_WIDTH, BUFFER_HEIGHT) - pos) / size; 205 uv.y = saturate(uv.y); 206 uv.x *= ratio * 2.0; 207 208 float t = abs(data); 209 int radix = floor(t)? ceil(log2(t)/3.32192809):0; 210 211 //early exit: 212 if(uv.x > digit+1 || -uv.x > radix+1) return; 213 214 float index = t; 215 if(floor(uv.x) > 0) 216 for(int i = ceil(-uv.x); i<0; i++) index *= 10.; 217 else 218 for(int i = ceil(uv.x); i<0; i++) index /= 10.; 219 220 index = (uv.x >= -radix-!radix)? index%10 : (10+step(0, data)); //adding sign 221 index = (uv.x > 0 && uv.x < 1)? 12:index; //adding dot 222 index = digits[(uint)index]; 223 224 res += tex2D(samplerText, (frac(uv) + float2( index % 14.0, trunc(index / 14.0))) / 225 float2( _DRAWTEXT_GRID_X, _DRAWTEXT_GRID_Y)).x; 226 } 227 228 #endif