• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
eektor

Drawing Text in SDL

11 posts in this topic

I'm pretty new and I know there must be an easier way to draw text with SDL. This is how I'm doing it. g_graphics->drawText(FONT_TYPE, "Press 'i' for instructions.", FONT_SIZE, 340,345,255,255,255,0,0,0); g_graphics is a class that deals with all the SDL graphics including drawing text. drawText is a function. The part that makes it complicated is the 340, 345, which are the x and y values for the screen. (The other numbers are the font color and background color.) Is there an easier way than trying to put the x and y for each line you type? I mean is there an easier way to center the text, write paragraphs, etc.?
0

Share this post


Link to post
Share on other sites
You mean something like this? (made by me)

int DisplayMsg(std::string Line1, std::string Line2, std::string Line3, std::string Line4)
{
SDL_Surface *FinalMessage = load_image("GFX\\mbox.bmp");
SDL_Event events;
bool MSG = true;

RenderTextToSurface(Line1, 5,5, FinalMessage);
RenderTextToSurface(Line2, 5,25+3, FinalMessage);
RenderTextToSurface(Line3, 5,45+6, FinalMessage);
RenderTextToSurface(Line4, 5,65+9, FinalMessage);

while (MSG)
{
SDL_PollEvent(&events);

if (events.key.keysym.sym == SDLK_RETURN) // If enter:
{
MSG = false;
}

put_surface(0, 380, FinalMessage, screen);

if(SDL_Flip(screen) == -1)
{
SDL_FreeSurface(FinalMessage);
return 1;
}
}

SDL_FreeSurface(FinalMessage);
return 0; // Normal termination
}



It loads a 640x100 bitmap and displays it at the bottom of the screen after blitting text to it.

[Edited by - orcfan32 on May 15, 2006 5:28:51 PM]
0

Share this post


Link to post
Share on other sites
Thanks, that gave me some ideas. Anybody else has some other ideas they use?
0

Share this post


Link to post
Share on other sites
The library ought to have some sort of function that can determine the size of text. You can parse the text by lines, measure the width/height of each, and use that accordingly.
[Sorry, somewhat a generic answer]
0

Share this post


Link to post
Share on other sites
if your using SDL_ttf then TTF_FontLineSkip( font ) gives you the recomended line skip for a font, so you can just add it to the previouse lines Y value
0

Share this post


Link to post
Share on other sites
Hi, if you want to center the text, write paragraphs, etc, then you will have a bit more work to do, I find it quite tedious. The point where I am is that a very large string is automatically multilined in a square, and that's enough for me (message boxes).
This worked well, I have a TextRenderer that I give a string and a rectangle, you could use SDL_Rect. Then let the text renderer figure out how to place the string into the rectangle. The benefit of working with rectangles is that it is easier to lay-out, and I usually think / code in terms of rectangles anyway so I have these ready to give to the renderer.

I also have the TextRenderer remember what font size and colors to use, as this won't be changing all over the place. This could be expanded if needed into setting some 'behaviors', so the renderer could have states (centered, left outlined, right outlined, bold, etc.)

Probably the most important thing was to keep breaking the steps involved up into small working bits, using multiple small helper functions. If you use SDL_ttf, besides the lineskip function there is also a function to check the length (in pixels iirc) of a to-be-rendered string without actually rendering it. There is also the issue of kerning, it will be easier to just use monospace fonts.

Here is an old thread about using SDL_ttf with opengl, koshmaar has a post there with his font manager including code and UML diagram. It may be useful to get some more ideas.

Good luck.


0

Share this post


Link to post
Share on other sites
@deadimp Yeah I was looking for functions like that in SDL but I couldn't find anything like that in the book "Focus on SDL"

@ender_341 Thanks I need that. Does that deal with the font size as well or is that just the font type? For example what do I put in the parenthisis, Arial or 12?

@DeadXorAlive I'm using only SDL. I haven't started on OpenGL and I would prefer to wait til I turn out a few more polished 2D games with SDL before I go into OpenGL. I like your idea of the TextRenderer and putting it into a SDL_Rect.

I'm trying to work with the Menu and Instruction screen and I wanted to work on having the title centered and the instructions or menu items to be in a paragraph format with margins.

0

Share this post


Link to post
Share on other sites
Quote:
Original post by eektor
@deadimp Yeah I was looking for functions like that in SDL but I couldn't find anything like that in the book "Focus on SDL"

@ender_341 Thanks I need that. Does that deal with the font size as well or is that just the font type? For example what do I put in the parenthisis, Arial or 12?

@DeadXorAlive I'm using only SDL. I haven't started on OpenGL and I would prefer to wait til I turn out a few more polished 2D games with SDL before I go into OpenGL. I like your idea of the TextRenderer and putting it into a SDL_Rect.

I'm trying to work with the Menu and Instruction screen and I wanted to work on having the title centered and the instructions or menu items to be in a paragraph format with margins.


In the linky I gave koshmaar has a diagram of how he designed his font manager class, it might still be useful to get a bigger picture of what is involved but I wouldn't recommend doing it so detailed, in the first place at least.
These are the functions from SDL_ttf that may be of use to you:
TTF_SizeText
TTF_FontLineSkip
TTF_GlyphMetrics
And this is from freetype (SDL_ttf is based upon that lib), has some useful info on what glyph metrics is about:
Glyph metrics
From the same site this explains kerning:
Kerning
This is just so you can know some background on the terms involved, and why you may want to decide between monospace and proportional fonts.
When you load a font in SDL_ttf, you determine which family it is from (arial for example) and what the size is (12). This determines what is the size of the text rendered, and all functions work with a TTF_Font from then on.

(About margins: working with rects this is easy, just subtract the total margin of the width and add margin to the x coordinate. You could make a function that takes a SDL_rect and margin, and returns the proper SDL_rect).
0

Share this post


Link to post
Share on other sites
If you want to put your text onto a target surface and centered, then you could use my RenderTextToSurface function and make the X the screen width / 2.

int RenderTextToSurface(std::string Text, int x, int y, SDL_Surface *Dest)
{
SDL_Color TXT_Color;
TXT_Color.r = 0xFF;
TXT_Color.g = 0xFF;
TXT_Color.b = 0xFF;

SDL_Surface *TTF_Message;
TTF_Font *font = TTF_OpenFont("font.ttf", 24);

if(!(TTF_Message = TTF_RenderText_Solid(font, Text.c_str(), TXT_Color)))
{
SDL_FreeSurface(TTF_Message);
Log("Error in function 'RenderTextToSurface': TTF_Message could not be blitted: returned 1");
return 1;
}

put_surface(x, y, TTF_Message, Dest);

SDL_FreeSurface(TTF_Message);

return 0;
}



It makes a color for the text which is white, then makes a surface to hold the message. Then in declares the font by opening a ttf in the same directory and makes it's size to 24. Then it checks to make sure that it rendered the text to the surface. After that, it puts the surface onto the destination and frees the surface.
0

Share this post


Link to post
Share on other sites
Thanks again. The post by koshmaar is pretty detailed and over my head. I'm just trying to clean up my Pong game which is my first 2D game.

Thanks for the link to the SDL_ttf site, those functions will come in very handy.

I like what you did in your project orcfan32.

I'm thinking on redoing what I have. This is the function I have:

void SDL_Graphics::drawText(const std::string font_type, const std::string text,
int size,
int x, int y,
int fR, int fG, int fB,
int bR, int bG, int bB)
{
TTF_Font* font = TTF_OpenFont(font_type.c_str(), size);

SDL_Color foregroundColor = { fR, fG, fB };
SDL_Color backgroundColor = { bR, bG, bB };

SDL_Surface* textSurface = TTF_RenderText_Shaded(font, text.c_str(),
foregroundColor, backgroundColor);

SDL_Rect textLocation = { x, y, 0, 0 };

SDL_BlitSurface(textSurface, NULL, m_screen, &textLocation);

SDL_FreeSurface(textSurface);

TTF_CloseFont(font);
}


Now I'm thinking I should split the text rendering (I guess that's the correct term instead of drawing text) into different functions. I would want a text renderer which is similar to what I have. Then create a function called ParagraphText and then CenterText. These functions I'll put the text into a SDL_Rect and render the text into a paragraph or center.

Does this sound good? Thanks for all the help you given me so far.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by eektor
@ender_341 Thanks I need that. Does that deal with the font size as well or is that just the font type? For example what do I put in the parenthisis, Arial or 12?


Neither, you send it the TTF_Font that has the font loaded to it.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0