Got text rendering to work

This commit is contained in:
Joseph Ferano 2023-11-11 15:16:10 +07:00
parent 107ac6b2db
commit 177f73f78a

25
bt.c
View File

@ -108,7 +108,7 @@ int main(void) {
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
SDL_SetRenderDrawColor(renderer, 130, 163, 255, 1); SDL_SetRenderDrawColor(renderer, 130, 163, 255, 1);
TTF_Init();
TTF_Font* font = TTF_OpenFont( "./fira.ttf", 16); TTF_Font* font = TTF_OpenFont( "./fira.ttf", 16);
struct winsize sz; struct winsize sz;
@ -186,13 +186,6 @@ int main(void) {
// sb.ypos += GetMouseWheelMoveV().y * scroll_speed; // sb.ypos += GetMouseWheelMoveV().y * scroll_speed;
// sb.height = MeasureTextEx(*fontDefault, sb.buf, fontsize, 1).y; // sb.height = MeasureTextEx(*fontDefault, sb.buf, fontsize, 1).y;
SDL_Surface* surfaceMessage =
TTF_RenderText_Solid(font, "put your text here", WHITE);
// now you can convert it into a texture
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surfaceMessage);
// TODO: This is probably related to the font height and scrolling up, we just need // TODO: This is probably related to the font height and scrolling up, we just need
// the "fontsize" which I think is probably the height of the text // the "fontsize" which I think is probably the height of the text
if (new_read || new_char) { if (new_read || new_char) {
@ -217,6 +210,19 @@ int main(void) {
new_read = true; new_read = true;
} }
SDL_RenderClear(renderer); SDL_RenderClear(renderer);
SDL_Surface* surfaceMessage =
TTF_RenderText_Blended(font, "put your text here", WHITE);
// now you can convert it into a texture
SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surfaceMessage);
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = surfaceMessage->w;
rect.h = surfaceMessage->h;
SDL_RenderCopy(renderer, texture, NULL, &rect);
SDL_Color current_color = WHITE; SDL_Color current_color = WHITE;
int col_max = 200; int col_max = 200;
char row_buf[col_max]; char row_buf[col_max];
@ -224,7 +230,8 @@ int main(void) {
int ncol = 0; int ncol = 0;
int row_height = 18; int row_height = 18;
float row_posx = 0; float row_posx = 0;
for (int c = 0; c <= sb.length; c++) { // for (int c = 0; c <= sb.length; c++) {
for (int c = 0; c <= -1; c++) {
if (sb.buf[c] == '\r') { if (sb.buf[c] == '\r') {
continue; continue;
} else if (sb.buf[c] == '\n' || sb.buf[c] == '\0' || ncol >= col_max) { } else if (sb.buf[c] == '\n' || sb.buf[c] == '\0' || ncol >= col_max) {