Im wondering why i didn't have to use the q.y1 value returned from stbtt_GetBakedQuad()
I have this scale paramter that i wanted to scale the character while keeping the x/y pos.
if i scale anything other than 1.0f some chars gets displaced ( mostly lower case characters ).
______________________________________________________
I copied GetBakedQuad() definition in the printText() function for help only.
loading FontBitmap in main:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | stbtt_bakedchar BCD[96] = {}; unsigned char fontBuffer[30000000] = {}; const int fontMapWidth = 3500; const int fontMapHeight = 2080; float pixelHeight = 256.0f; unsigned char fontData[fontMapWidth*fontMapHeight] = {}; // FILE *hFontFile = fopen("C:\\windows\\fonts\\times.ttf", "rb"); FILE *hFontFile = fopen("C:\\windows\\fonts\\cour.ttf", "rb"); // FILE *hFontFile = fopen("C:\\CJ_FONTS\\FFF_Tusj.ttf", "rb"); if(!hFontFile) { printf("fail open file\n"); } fread(fontBuffer, 1, sizeof(fontBuffer), hFontFile); stbtt_BakeFontBitmap(fontBuffer, 0, pixelHeight, fontData, fontMapWidth, fontMapHeight, 32, 126, BCD); fclose(hFontFile) |
printText():
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | textIndexRange printText(stbtt_bakedchar *BCD, GameBuffer *gameBuffer, int *index, float x, float y, float in_scale, char *text, CJ_V4f vec4) { float advance = 0; float baseLineBreak = 0; float baseLine = 0; float result_X, result_Y, result_W, result_H; float scale = in_scale; int fontMapWidth = 3500; int fontMapHeight = 2080; textIndexRange tir = {}; tir.start = *index; while(*text) { float pX = 0; float pY = 0; int character = *text - 32; stbtt_aligned_quad q = {}; stbtt_GetBakedQuad(BCD, fontMapWidth, fontMapHeight, character, &pX, &pY, &q, 1); //typedef struct //{ // float x0,y0,s0,t0; // top-left // float x1,y1,s1,t1; // bottom-right //} stbtt_aligned_quad; // //STBTT_DEF void stbtt_GetBakedQuad(const stbtt_bakedchar *chardata, int pw, int ph, // same data as above // int char_index, // character to display // float *xpos, float *ypos, // pointers to current position in screen pixel space // stbtt_aligned_quad *q, // output: quad to draw // int opengl_fillrule); // true if opengl fill rule; false if DX9 or earlier // Call GetBakedQuad with char_index = 'character - first_char', and it // creates the quad you need to draw and advances the current position. // // The coordinate system used assumes y increases downwards. // // Characters will extend both above and below the current position; // see discussion of "BASELINE" above. // // It's inefficient; you might want to c&p it and optimize it. if(*text == (int)' ') { q.x1 = 50.5f; } if(*index > TEXT_LAST) { MessageBox(0, "Need to increase number of characters (TEXT_LAST)", "MAX TEXT INDEX REACHED", MB_OK); *text = 0; } // baseLineBreak sets the baseline for the textstring according to the first Character in the string if(baseLineBreak == 0) { baseLine = (y - q.y0); baseLineBreak++; } else { } // Dont scale the X-Value or Y-Value of the first character result_X = (x + advance); result_Y = (baseLine + q.y0); result_W = scale * (q.x1); result_H = scale * (-q.y0); gameBuffer[*index].setSolidColor(1.0f, 1.0f, 1.0f, 1.0f); gameBuffer[*index].createRect(result_X, result_Y, result_W, result_H); gameBuffer[*index].setTextureCoordinates(q.s0, q.t0, q.s1, q.t1); gameBuffer[*index].setSolidColor(vec4.r, vec4.g, vec4.b, vec4.a); advance += (scale * q.x1); // advance the x-value for the next character *index += 1; // increases the gameBuffer Index text++; } tir.end = (*index) - 1; return tir; } |