1

So I just want to point out that I am pretty new to code outside engines so this is somewhat new to me.

I am using SDL as a base for my game and would like to know a easy way to draw text, in the form of score/time, on the screen.

So far when searching I have not found anything that I've really understood or how to use. The thing I find most when searching with the tag SDL is SDL_ttf and I've tried to look into it but with no success.

So again, I am looking for an easy way to display text, string and float/int, in the form of score/time.

tod
  • 1,539
  • 4
  • 17
  • 43
Elis Öhrman
  • 21
  • 1
  • 5
  • 1
    Ah, sorry.I'm using C++ – Elis Öhrman Mar 24 '16 at 11:19
  • If you're struggling with this it might be worth going back to engines. Not being snarky - just that SDL is going to repeatedly throw these kind of problems at you - it's more general purpose than a game engine and often not as well documented. – DanBennett Mar 24 '16 at 11:44
  • You can certainly not use SDL_ttf. You can use another font-renderer that can render into bitmaps (like the win32 function `DrawText`) and you can also just make [your own simple font-renderer with mono-spaced bitmap fonts](http://lazyfoo.net/SDL_tutorials/lesson30/). But none of the alternatives are really "easier" if you want to keep using SDL as your base. – PeterT Mar 24 '16 at 11:54
  • For an extensive and detailed implementation, see [my post here](https://stackoverflow.com/a/67553575/1147688). – not2qubit May 16 '21 at 06:14

2 Answers2

2

Library SDL alone do not have support for writing text to the screen. Your search leads to SDL_ttf, which is right library to use.

Example of usage (only extra code, supposing you already called SDL_Init, created SDL_Window and you have SDL_Renderer* renderer for that window.

const SDL_Rect* dstrect;
SDL_Color color;

TTF_Init();
TTF_Font* font = TTF_OpenFont("font.ttf" /*path*/, 15 /*size*/);
SDL_Surface* textSurface = TTF_RenderText_Blended(font, "Text to render", color);

SDL_Texture* textTexture = SDL_CreateTextureFromSurface(renderer, textSurface);
SDL_RenderCopy(renderer, textTexture, NULL, dstrect);

SDL_FreeSurface(textSurface);
SDL_DestroyTexture(textTexture);
TTF_CloseFont(font);
TTF_Quit();

Look into docs for other TTF_RenderText_* methods and how they differ.

And since you are using C++ (both SDL and SDL_ttf is in C), you probably want to write some wrappers around TTF rendering.

Zereges
  • 5,139
  • 1
  • 25
  • 49
0

you'll want the SDL ttf library - it handles truetype fonts relatively straightforwardly.

http://www.libsdl.org/projects/SDL_ttf/ and documentation http://jcatki.no-ip.org:8080/SDL_ttf/ http://jcatki.no-ip.org:8080/SDL_ttf/SDL_ttf.html

see especially documentation for TTF_OpenFont, TTF_CloseFont (to open & close a font file) and the rendering functions eg TTF_RenderText_Solid

In your position I'd probably write a couple of helper functions to handle drawing text that wraps these, passing on the string, location to draw etc. and call them whenever you need to print score/etc.

DanBennett
  • 448
  • 2
  • 5
  • 17
  • Ok, so I have trouble installing SDL ttf as in I don't get how it's done at all. The documentation says that it is suppose to be found in the source download however I can't find it there. – Elis Öhrman Mar 24 '16 at 12:06
  • The development libraries are at the first link I posted along with instructions on how to install. More instructions in the 2nd link under getting started. If that's not enough, you need to frame your question in more detail - your end goal is to write text to screen, and on the way to that you need to install the library, but what specifically are you having trouble with at this moment? What have you tried? – DanBennett Mar 24 '16 at 12:55