Drawing a text dinamically

edited November 2011 in Help request
Hi, it's me again :P mmm... I was looking at the examples, but I did not find an answer...

How can i draw a string / text without use a ini file...

Use example:
// x, y, message
DrawText(32, 32, "hello world");

First try:

orxFONT* pstFont = orxFont_CreateFromConfig("MyFont");
orxTEXT* pstText = orxText_Create();
orxText_SetFont(pstText, pstFont);
orxText_SetString(pstText, "Hello big world");

Second try:

orxFONT* pstFont = orxFont_CreateFromConfig("MyFont");
orxTEXT* pstText = orxText_Create();
orxText_SetFont(pstText, pstFont);
orxText_SetString(pstText, "Hello big world");
orxVECTOR vec;
vec.fX = 200;
vec.fY = 200;
orxObject_SetPosition((orxOBJECT*)pstText, &vec); // In this line, the game spit on my face.

A lot of tries....

And nothing :(

Any idea?

Comments

  • edited November 2011
    Well, you're part way through. However, if you instantiate the font yourself, you'll be responsible of deleting it.

    In your second example, casting an orxTEXT into and orxOBJECT and writing something into it will result in memory corruption.
    As a matter of fact, never do any casting directly in orx but use the helpers.
    Here if you do:
    orxOBJECT *pstObject = orxOBJECT(pstText);
    You'll get an assert in debug as the casting helper will recognize that the types aren't compatible.

    Now, for your problem, why don't you want to use the config system to define your text, at least partly. You can still modify its properties afterward and you won't be responsible of deallocating any resource you haven't allocated directly.

    Something like:
    In config:
    
    [MyText]
    Graphic = MyText
    Text = MyText
    Font = MyFont
    String = Whatever you want to display, can be a placeholder if you want to change it at runtime
    
    Note that I'm using the same section name for the object, graphic and text, which doesn't matter as orx is only going to use the property Graphic for the object, Text for the graphic and String for the text.
    
    In code:
    
    orxOBJECT *pstMyText = orxObject_CreateFromConfig("MyText");
    

    Now you can modify that object as much as you want, including moving it and setting its content with orxObject_SetTextString(pstMyText, "My new text.").

    Btw, don't hesitate to use the BBCodes for quoting (
    ) and displaying code ([code]) in the forum.
  • edited November 2011
    Some quick details though:

    Orx is an engine and not a library, which means you usually don't issue direct game loop commands such as doing the rendering yourself. As you've noticed, objects exist in a 3D world, are "seen" by a camera that will then render to the screen or a texture view a viewport. Objects are updated in the game simulation and you can modify them at will.

    If you want to bypass that scheme and issue direct orders, like direct string rendering, there are ways to do it such as by listening to the render events and call directly the orxDisplay API, but those are advanced techniques to be used only in very specific cases and can lead to breaking the whole game loop if not carefully handled. :)
Sign In or Register to comment.