I have been developing with SDL for a while now, but thought i needed another challenge. So I decided to draw some text on the screen using SDL_TTF.
I try to render the text at 0,0 but it is a bit below the screenborder. So that means it is not rendering fully at 0,0.
This code is just for testing purposes.
Here is the code I use at the time for testing:
#include <SDL.h>
#include <SDL_ttf.h>
#include <iostream>
int main(int argc, char* args[])
{
SDL_Texture* tex;
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *win = SDL_CreateWindow("Font drawing test", 100, 100, 640, 480, SDL_WINDOW_SHOWN);
SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
TTF_Init();
while (true)
{
SDL_RenderClear(ren);
TTF_Font* font = TTF_OpenFont("./Resources/test.ttf", 72);
SDL_Surface *surf = TTF_RenderText_Blended(font, "Test", SDL_Color{ 255, 255, 255, 255 });
tex = SDL_CreateTextureFromSurface(ren, surf);
SDL_RenderCopy(ren, tex, NULL, new SDL_Rect{ 0, 0, 100, 100 });
SDL_RenderPresent(ren);
SDL_DestroyTexture(tex);
SDL_FreeSurface(surf);
TTF_CloseFont(font);
}
SDL_DestroyTexture(tex);
SDL_DestroyRenderer(ren);
SDL_DestroyWindow(win);
SDL_Quit();
return 0;
}
And this is how it looks on screen:
How can i fix this problem?
Marco
I would like to ask you more information by a comment, as for me it looks that rendering works OK. But I can't write comment because I don't have enough reputation, and I never get enough reputation to comment, if I don't answer, so I'm "answering" anyways and hope that site moderators at some point reconsider the limits/requirements for asking more info for questions to be able to answer (to gain reputation enough to ask for more info).
Like said, for me it looks that rendering works just OK. The "gap" comes from font metrics. Some sort of introduction TTF font metrics can be found e.g. here:
https://www.libsdl.org/projects/SDL_ttf/docs/SDL_ttf_38.html
http://chanae.walon.org/pub/ttf/ttf_glyphs.htm
I'm not sure if it is possible, but you might want to consider tweaking the metrics after loading, or if that does not work, use font more suitable for your purposes.