![]() |
Rendering Text on the screen in real time. | ![]() |
shinn497
![]() |
![]() |
Hello
I'm following Lazy Foo's tutorial on rendering True Type Fonts. This would be in SDL 2 and it is his 16th tutorial. It seems he loads the Fonts into video memory before rendering it to the screen . This is nice but I would like to load things in real time. For example, I have a position of a character I would like to display that continuously updates. I can't do this if it loads once . I've tried putting the load media function in later but SDL crashes. In addition, I've tried following this tutorial ( http://stackoverflow.com/questions/3604889/sdl-sdl-ttf-how-do-you-render-text-in-sdlsdl-ttf-c) in order to render text as a surface. But that isn't working. I'm not sure how to handle both textures and surfaces at the same time. IS there an elegant way to get TTF on the screen in real time? I feel like this should be much easier. |
||||||||||
|
![]() |
![]() |
![]() |
![]() |
AlexRou
![]() |
![]() |
Or you could treat TTF fonts like image fonts, where you create a surface containing the static text then for things that change create each individual character as a surface. Then just pick the surfaces of the characters you want to draw and make the word/number
|
||||||||||
|
![]() |
![]() |
mr_tawan
![]() |
![]() |
I don' really understand what you're archiving. Mind me.
You could use SDL_TTF's TTF_RenderGlyph() to render a single glyph (an image of the character, from the font file) into a surface, and optionally call TTF_GlyphMetrics() to get the glyph metrics information, then place the glyph surface anywhere you want on the screen. You can also use SDL_CreateTextureFromSurface() to create a new texture from the surface you get from that function, if you're using SDL2 renderer. If you're archiving something like type writer effect, you could recall TTF_RenderText() to get the new surface every frame, or with a little logic that recall the function only when text changed. Anyway if you need finer-grain control over how the text is rendered, I'd suggest you to try using FreeType 2 directly instead of SDL_TTF. |
||||||||||
|
![]() |
Rendering Text on the screen in real time. | ![]() |
R Manard
Guest
![]() |
![]() |
How many times per second do you want to display the text? How is it not real time to have in video memory what you want rendered each sync cycle of the monitor? On Feb 2, 2014 4:07 PM, "shinn497" wrote:
|
||||||||||||
|
![]() |
Rendering Text on the screen in real time. | ![]() |
R Manard
Guest
![]() |
![]() |
Are you trying to use SDL 1 or 2?
|
||||||||||
|
![]() |
![]() |
mr_tawan
![]() |
![]() |
The font is indeed no inside the video memory. In fact I think it's not even read into main memory entirely, unless you specified so (using TTF_OpenFontRW()).
The SDL_ttf is designed so you have to render text into a surface, then you blit it to the screen. This surface has to be blit every frame otherwise once you clear the screen it would disappear. You could, also, re-render the text every frame (although rendering text is quite heavy). That could be called 'real-time text rendering' I believe. But if you want finer grain control over how the text is rendered, I think you have to go with using FreeType 2 (as I mentioned earlier). Again could you explain more about what you're trying to archive ? An example would be great. I still don't really understand what you're aiming to. Thanks! |
||||||||||
|
![]() |
![]() |
shinn497
![]() |
![]() |
By real time, I mean I want to update and continually change the text. Specifically I want to display the X,Y position of the sprite I'm using. In Lazy_Foo's tutorial, he uses the lTexture text wrapper class to convert the TTF surface into a texture and then renders the texture. This is unsuitable since he loads the Font once at the beginning and then Renders it in the loop. I have to continually update the font.
Currently I'm using SDL 2 (I said this in my original post by the way). I'm going to try alex rou's method since it seems the simplest but I'd prefer to still use textures. I'm sure the specifcs of both blitting a surface and rendering a texture. Sorry I'm kind of a noob. Games have text fonts in them all the time. You would think there would be a simple and elegant way of doing this. |
||||||||||
|
![]() |
Rendering Text on the screen in real time. | ![]() |
Alex Szpakowski
Guest
![]() |
![]() |
The typical way for games to have performant text is to have, for each font used, a big texture atlas with all the necessary glyphs in it (either created beforehand with a tool, or at startup time with some code to get the glyphs rasterized from e.g. FreeType and upload them to the texture.)
Then the game can simply reference a subsection of the glyph-atlas (via texture coordinates of the vertices to be drawn) when it wants to draw a character. No re-uploading of glyphs to the GPU necessary. This also lets the game batch up extremely long strings into a single draw call, since all the characters just reference different parts of the same texture and the game can just draw a bunch of vertices forming quads with a single call, so it’s quite efficient. I don’t think all of that is viable with SDL_Render though (the batching part in particular.) On Feb 3, 2014, at 5:12 AM, shinn497 wrote:
|
||||||||||||
|
![]() |
![]() |
shinn497
![]() |
![]() |
Update I tried implementbing alex's examplbe but it came out white. It compiled though. I still left pieces from Lazy Foo's code where he set up the render window and I kept his init and loadmedia functions.
Anyone want to tell me what I'm still doing wrong? I will need to render sprites as textures as well as blit fonts so I need to keep some of this stuff.
|
||||||||||||
|
![]() |
![]() |
AlexRou
![]() |
![]() |
[quote="shinn497"]Update I tried implementbing alex's examplbe but it came out white. It compiled though. I still left pieces from Lazy Foo's code where he set up the render window and I kept his init and loadmedia functions.
Anyone want to tell me what I'm still doing wrong? I will need to render sprites as textures as well as blit fonts so I need to keep some of this stuff. You cannot render surfaces like that using a renderer, you have to decide if you want to use the renderer or not. If you want to continue using a renderer then you have to first convert the surface to a texture. SDL_Texture* tex = SDL_CreateTextureFromSurface( Renderer, Surface ); Then to display it: SDL_RenderCopy( Renderer, tex, NULL, NULL ); SDL_RenderPresent( Renderer ); You can safely store the surface returned by TTF_RenderText and the texture generated by SDL_CreateTexture (generate and store these outside the loop if they are not changing). However for changing texts you have to regenerate the surface and texture each time the text gets updated. This will be extremely slow compared to making textures for 0 - 9 then drawing each number individually to form the coordinates, this way you only generate each texture once. |
||||||||||
|
![]() |
Rendering Text on the screen in real time. | ![]() |
Jeffrey Carpenter
Guest
![]() |
![]() |
Greetings,
If you so happened to be interested in the kind of implementation that Alex talks about with using a texture atlas (indeed an efficient means of going about TTF rendering), take a look at: https://github.com/i8degrees/nomlib include/nomlib/graphics/fonts/TrueTypeFont.hpp src/graphics/fonts/TrueTypeFont.cpp (Supporting files include the other files within that directory path, minus BitmapFont & IFont). I'd stick with regenerating the texture each time the text is updated, AKA the slow, but easy method, just like Alex describes. I did the same thing he describes in my code before re-writing it. (I refactored my code for fine-grain access into rendering, not because of performance). Cheers! On 2014/02/ 03, at 4:03, AlexRou wrote:
_______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
||||||||||||
|
![]() |
![]() |
shinn497
![]() |
![]() |
So ,if I want to have my own Fonts from a texture, i should load 0123456789abcdef..... and then write a function that associates each character to a position in the surface and then renders each individually?
It just seems odd that this isn't built in to SDL or someone hasn't made a library for it ... sigh. BTW I'm not entirely sure what the renderer is . Is there some literature on it and the specifics of what a surface is vs a texture? </N00b> |
||||||||||
|
![]() |
![]() |
AlexRou
![]() |
![]() |
The renderer is hardware accelerated rendering, using surfaces alone is software rendering. For a simple game and people starting out software rendering is more than enough, I would suggest you start off with SDL 1 and get familiar with things before using SDL 2 since there are less tutorials available and the renderer can confuse you. You can follow the lazy foo tutorials for SDL 1, do not skip to the parts you are interested in and do them one by one starting from the first one and make sure you understand what each tutorial is teaching you. |
||||||||||||
|
![]() |
Rendering Text on the screen in real time. | ![]() |
Jeffrey Carpenter
Guest
![]() |
![]() |
On 2014/02/ 03, at 5:17, shinn497 wrote:
Yes. (Very similar in concept to how bitmap fonts are rendered). Lazy foo tutorials has all the implementation details you need for this.
Sorry, no literature from me other than perhaps the migration guide for SDL2. Surfaces are stored in system RAM, and are best used where pixel-level manipulation is needed. Note that a surface is unaccelerated (work has to be done by CPU). Textures, on the other hand, are stored in GPU's memory and therefore accelerated. Textures are an expensive resource to create, and are also bound by video RAM (therefore a limited resource), but are more efficient when used correctly. The expensive calls on a texture involve anytime you copy pixels to and from. In short: surfaces for processing (rescaling, pixel collision, etc) and textures for final rendering.
_______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
||||||||||||||||||
|
![]() |
![]() |
shinn497
![]() |
![]() |
Can you point me to the Lazy Foo tutorial where he does this? Since I'm going based off his tutorials, it would be easier to implement.
|
||||||||||
|
![]() |
Rendering Text on the screen in real time. | ![]() |
R Manard
Guest
![]() |
![]() |
I can see how without researching ttf or openGL it would seem like you have mentioned. Experts have combed over the way it's done for years to produce what you see in lazy foo's examples. He didn't make all that up. An example of this is that it would seem like a good thing to make a texture with 1234567890abcdefgh and so on and then display a segment of this texture each time you want a particular glyph from it. This is like a bitmap font. For all of the reasons ttf are better than bmf it's not a good idea unless you just don't need professional results or you plan to write extra code to plan for the data ttf provides. Ttf has data components that change how one glyph is presented only if it follows another particular glyph or anywhere on that line there is a particular other glyph for example. If that sounds complicated then hang on to your hat because I simplified it quite a bit!
Don't think for minute that I'm disrespecting lazy. He takes the pieces of sdl and crafts them with c into a simple form artwork that starting coders can grasp. He's an artist! Anyone just staring out with sdl should trust that the makers of sdl and sdl libs are bringing their A game, and that if you follow how they lead, you are doing it right. Later when you understand what the codes are doing in detail, by all means, try to improve on their work, build use case specific examples, join the tide of code that lifts all boats of starting game makers ![]() I'm standing on a chair waving a flag right now that says C/C++ & SDL2. lol. I promise to build a web page that explains this stuff with use case code included. R A Manard whisper8.com p.s. Here is a full sdl2 example game code! Please don't hate on it, cuz it's alpha. It's not ready for release but it looks useful to you. On Mon, Feb 3, 2014 at 11:39 AM, shinn497 wrote:
|
||||||||||||
|
![]() |
![]() |
shinn497
![]() |
![]() |
Ok update. I noticed that in Lazy's 32nd tutorial he changed the text in real time. This is the one for handling text input. In it he has a string variable that he loads into his lTexture class everytime he wants to update. I'm trying that right now but, every time I try to call the loadfromrenderedtext function my program crashes. It is weird. It works from the loadmedia function that Lazy Foo wrote but not anywhere else.
Why is this? I'm including my code. I wrote it so that it will load a texture when you hit backspace, a which point it will crash. I'm so close to getting this is to work and any advice would be so appreciated.
|
||||||||||||
|
![]() |
![]() |
shinn497
![]() |
![]() |
Sorry that is a lot of code and it is really messy.
I've isolated the problem to a SegFault Occuriing in TTFsizeUTF8. according to the description, this occurs when there is a NULL font. I believe I've confirmed this by checking if the font is NULL. I added a line that will print to console if gFont = NULL and it did. anyone have an idea why this is? The gFont was already set in the load media function. Why would it turn back to NULL |
||||||||||
|
![]() |
Rendering Text on the screen in real time. | ![]() |
prushik
![]() |
![]() |
On Tue, Feb 4, 2014 at 10:14 AM, shinn497 wrote:
If you don't need scalable fonts, you could always use something simpler instead. Of course the basic method is the same: Load the font, create a texture (or surface) and then render that on the screen in such a way that it spells the words you want, but using a bitmap font instead of a TTF font makes loading easier. If you want an example, you can look at my SDL2 Ksec clock at http://betteros.org/sw/ksec.php which uses SDL2 and PSF fonts (which come with Linux) to display the time. The code is pretty minimal too. If you really NEED ttf fonts, then nevermind, but I think most people, especially game developers, do not need TTF at all. If you don't need it, it's not worth the overhead in my opinion. --Philip |
||||||||||||
|
![]() |
![]() |
AlexRou
![]() |
![]() |
This is what you would want in SDL 1 (you have to redownload and set things up again) http://lazyfoo.net/SDL_tutorials/lesson30/index.php You should seriously consider going to SDL 1 instead and things will be less complex and less confusing for you. |
||||||||||||
|
![]() |
![]() |
shinn497
![]() |
![]() |
I don't care about complex or confusing! I actually prefer it since I find the more difficult things are the more powerful they become. I'm also really passionate about learning and solving from first principles. This is why my day job is a theoretical physicist :b (although I took a day off to figure this shit out). I was just a little frustrated since this caught me off guard. I thought it would be really easy.
Hey everyone thanks for your help! I solved the problem. The issue was that I was passing the font as a NULL pointer which was causing a segfault in the TTF font DLL. Not out of the clear though since, while I can update things on the screen, I'm having an issue converting ints to strings. I'll figure it out though. Btw thanks everyone for helping me! I've learned a lot. I will use this solution for the time being but will go through all of your suggestions. I'm a little time constraned since I'm developing on the off hours. But I will definately go through this thread in the future! Sorry my noobishness. |
||||||||||
|
![]() |
![]() |
AlexRou
![]() |
![]() |
Well if you think that its worth the time/amount of work for what you are trying to do then nothing stopping you :) Also int a = 10; char *intStr = itoa(a); OR int a = 10; stringstream ss; ss << a; string str = ss.str(); |
||||||||||||
|
![]() |
Rendering Text on the screen in real time. | ![]() |
Jonny D
![]() |
![]() |
I like using snprintf(), personally. It's nice and flexible with a printf-style format. Visual Studio might have poor support for it until version 2014, but you can find snippets online to implement it.
int a = 10; char buffer[20]; snprintf(buffer, 20, "%d", a); Jonny D |
||||||||||
|
![]() |
Rendering Text on the screen in real time. | ![]() |
R Manard
Guest
![]() |
![]() |
The full SDL 2 game example I posted turns ints into strings and a lot of stuff. It is a full game that loads file data, does TTF, makes log files, puts thousands of enemy bugs crawling on the screen, and more. Plus its only 859 lines. I hope to finish in under one k without too much clutter. Wait till you see it finished. If you missed it scroll back and take a look at the zip
|
||||||||||
|
![]() |
Rendering Text on the screen in real time. | ![]() |
Eric Wing
Guest
![]() |
![]() |
On 2/4/14, Jonathan Dearborn wrote:
SDL provides SDL_snprintf in SDL_stdinc.h. -Eric -- Beginning iPhone Games Development http://playcontrol.net/iphonegamebook/ _______________________________________________ SDL mailing list http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org |
||||||||||||
|
![]() |
Rendering Text on the screen in real time. | ![]() |
Jonny D
![]() |
![]() |
Right. That makes it nice and easy.
Jonny D On Tue, Feb 4, 2014 at 5:13 PM, Eric Wing wrote:
|
||||||||||||||
|
![]() |
![]() |
shinn497
![]() |
![]() |
You guys are awesome! Thank you.
|
||||||||||
|
![]() |
Re: Rendering Text on the screen in real time. | ![]() |
shinn497
![]() |
![]() |
I don't think you posted the link. There are several games on your website, whisper 8. which one do you recommend? |
||||||||||||
|
![]() |
Rendering Text on the screen in real time. | ![]() |
Iván Vodopiviz
Guest
![]() |
![]() |
By the way, services like pastebin make this much easier and we even get syntax highlights :)
On Sat, Feb 8, 2014 at 9:00 PM, shinn497 wrote:
|
||||||||||||
|
![]() |
Rendering Text on the screen in real time. | ![]() |
R Manard
Guest
![]() |
![]() |
I put the zip in my webspace now. I think this mail list or peoples clients maybe cut the attachment off. Any, enjoy
![]() whisper8.com/data/TWINDRAGON_SDL2_GAME_EXAMPLE.zip sure hope it works now On Sat, Feb 8, 2014 at 6:00 PM, shinn497 wrote:
|
||||||||||||
|
![]() |
![]() |
Nathaniel J Fries
![]() |
![]() |
If you're only displaying ascii characters, and only in one size, the answer is easy: blit every ascii character into a single surface, turn that into a texture, and treat it as you would a bitmap font.
|
||||||||||
|