Moving some code around

This commit is contained in:
2021-05-23 22:27:19 -07:00
parent b0dce455f0
commit 76307d2746
19 changed files with 205 additions and 75 deletions

View File

@ -48,6 +48,16 @@ void frameBufferUse(framebuffer_t *frameBuffer, bool clear) {
}
}
void frameBufferResize(framebuffer_t *frameBuffer,int32_t width,int32_t height){
if((
frameBuffer->texture.width == width &&
frameBuffer->texture.height == height
)) return;
frameBufferDispose(frameBuffer);
frameBufferInit(frameBuffer, width, height);
}
void frameBufferUnbind(render_t *render, bool clear) {
glBindFramebuffer(GL_FRAMEBUFFER, 0);
glViewport(0, 0, render->width, render->height);

View File

@ -26,6 +26,16 @@ void frameBufferInit(framebuffer_t *buffer, int32_t width, int32_t height);
*/
void frameBufferUse(framebuffer_t *frameBuffer, bool clear);
/**
* Resize an existing frame buffer. This will do the check if resizing is even
* necessary for you.
*
* @param frameBuffer Frame buffer to resize.
* @param width New width of the frame buffer.
* @param height New height of the frame buffer.
*/
void frameBufferResize(framebuffer_t *frameBuffer,int32_t width,int32_t height);
/**
* Unbind the currently bound frame buffer.
*

View File

@ -13,18 +13,16 @@
#include <stb_truetype.h>
#endif
void fontInit(font_t *font, char *data, float size) {
void fontInit(font_t *font, char *data) {
int32_t i, s;
s = FONT_TEXTURE_WIDTH * FONT_TEXTURE_HEIGHT;
uint8_t *bitmapData = malloc(sizeof(uint8_t) * s);
pixel_t *pixels = malloc(sizeof(pixel_t) * s);
font->size = size;
// STBTT Loads fonts as single channel values only.
stbtt_BakeFontBitmap(
data, 0, size, bitmapData,
data, 0, FONT_TEXTURE_SIZE, bitmapData,
FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT,
FONT_FIRST_CHAR, FONT_NUM_CHARS,
font->characterData
@ -49,8 +47,8 @@ fonttextinfo_t fontGetTextInfo(font_t *font, char *text) {
fonttextinfo_t info = {
.length = 0,
.realChars = 0,
.lineHeight = FONT_LINE_HEIGHT * font->size,
.spaceSize = font->size * FONT_SPACE_SIZE
.lineHeight = FONT_LINE_HEIGHT,
.spaceSize = FONT_SPACE_SIZE
};
// Count how many "real characters" are in the string.
@ -65,18 +63,22 @@ fonttextinfo_t fontGetTextInfo(font_t *font, char *text) {
return info;
}
fontmeasure_t * fontTextMeasure(font_t *font,char *text,fonttextinfo_t *info) {
fontmeasure_t * fontTextMeasure(font_t *font, char *text, fonttextinfo_t *info,
float scale
) {
int32_t i, j;
char c;
float x, y;
stbtt_aligned_quad *quad;
fontmeasure_t *measure;
scale *= FONT_SCALE_ADJUST;
measure = malloc(sizeof(fontmeasure_t));
measure->scale = scale;
measure->quads = malloc(sizeof(stbtt_aligned_quad) * info->realChars);
x = 0;
y = FONT_INITIAL_LINE * font->size;
y = FONT_INITIAL_LINE;
i = 0, j = 0;
while(c = text[i++]) {
@ -94,9 +96,11 @@ fontmeasure_t * fontTextMeasure(font_t *font,char *text,fonttextinfo_t *info) {
// Calculate the quad of the character, store into the array.
quad = measure->quads + j;
stbtt_GetBakedQuad(font->characterData,
FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT, ((int32_t)c)-FONT_FIRST_CHAR,
&x, &y, quad, FONT_FILL_MODE
FONT_TEXTURE_WIDTH, FONT_TEXTURE_HEIGHT,
((int32_t)c)-FONT_FIRST_CHAR, &x, &y, quad, FONT_FILL_MODE
);
quad->x0 *= scale, quad->x1 *= scale;
quad->y0 *= scale, quad->y1 *= scale;
j++;
}
@ -108,6 +112,10 @@ void fontTextMeasureDispose(fontmeasure_t *measure) {
free(measure);
}
void fontDispose(font_t *font) {
textureDispose(&font->texture);
}
void fontTextBufferFromMeasure(font_t *font, primitive_t *primitive, char *text,
fontmeasure_t *measure
) {
@ -136,8 +144,4 @@ void fontTextInitFromMeasure(font_t *font, primitive_t *primitive, char *text,
QUAD_VERTICE_COUNT*info->realChars, QUAD_INDICE_COUNT*info->realChars
);
fontTextBufferFromMeasure(font, primitive, text, measure);
}
void fontDispose(font_t *font) {
textureDispose(&font->texture);
}

View File

@ -16,9 +16,8 @@
* Initializes Font from raw TTF data.
* @param font Font to initialize
* @param data Data to intialize for.
* @param size Font size to load the font in.
*/
void fontInit(font_t *font, char *data, float size);
void fontInit(font_t *font, char *data);
/**
* Generates information about how the given font will render the given text.
@ -34,9 +33,12 @@ fonttextinfo_t fontGetTextInfo(font_t *font, char *text);
* @param font Font to use for rendering and measuring
* @param text Text to measure/render.
* @param info Info about the text being rendered / measured.
* @param scale Scale of the text.
* @returns Font measurement calculation.
*/
fontmeasure_t * fontTextMeasure(font_t *font, char *text, fonttextinfo_t *info);
fontmeasure_t * fontTextMeasure(font_t *font, char *text, fonttextinfo_t *info,
float scale
);
/**
* Disposes a previously calculated font text measurement.