orxText and orxString

edited November 2014 in Help request
Was looking around for a good example of displaying dynamic text on the screen. All of the examples use text defined in the config file. Is there a simple example of changing text such as a digital clock or something? That'd sure be a nice example program. I'm still fumbling around here...

Comments

  • edited November 2014
    Basically you need to create an object with a text graphic, and then you set its text.
    [ClockObject_Text]
    String = "0"
    
    [ClockObject_Graphic]
    Text = ClockObject_Text
    
    [ClockObject]
    Graphic = ClockObject_Graphic
    Color = (0xFF, 0x0, 0x0)
    Position = (100.0, -100.0, 1.0)
    

    Then you create a clock and attach register a function that will be called everytime it ticks (notice that it is not 100% precise, so in this case I set the tick speed to 0.9, the error is very small, but since you want to update a timer, setting it for one second would eventually lead it skipping a second).
        orxCLOCK* clockTimer = orxClock_Create(0.9, orxCLOCK_TYPE_USER);
        orxOBJECT* clockObject = orxObject_CreateFromConfig("ClockObject");
        orxClock_Register(clockTimer, updateTimer, clockObject, orxMODULE_ID_MAIN, orxCLOCK_PRIORITY_NORMAL);
    

    Finally, you update the timer in the function:
    void orxFASTCALL updateTimer(const orxCLOCK_INFO *info, void *object){
        char timerStr[64];
        orxString_NPrint(timerStr, sizeof(timerStr), "%u", (orxU32)orxMath_Floor(info->fTime));
        orxObject_SetTextString((orxOBJECT*)object, timerStr);
    }
    
  • edited November 2014
    can even be done without code
    [Score]
    Position = (0, -120, 0)
    Color = (255, 255, 255)
    Scale = 2.0
    TrackList = ScoreLoop
    Graphic = @
    Text = @
    Pivot = center
    
    [ScoreLoop]
    Loop = true
    0 = > Config.GetValue Live Score #
        Object.SetText ^ <
    

    just update the Live.Score config value and the text object will be updated
  • edited November 2014
    Thanks guys! I basically did what knolan suggested here but it only works with an image but not text. I'll have to take your code and go back in and study what went wrong. As far as Lydesik's idea, I didn't know you could do a loop inside the config file!! That is very cool. I'm used to using scripts or c/c++. That config code is more like script coding. I wonder why not just use scripts instead of .ini files? I'll get back to you after I do some more testing. I've done something wrong somehow... Thanks again.
  • edited November 2014
    Timelines/tracks are definitely a very basic way of scripting but are a bit limited at the moment.
    Sausage just posted a couple of tutorials on the wiki and there's some related material on the orx-dev group as well, more details and links here: http://orx-project.org/component/content/article/1-orx/89-commands-and-timeline-tutorials

    Lastly, here are a couple of precisions on previous answers:

    - If you ever need a 1-second tick clock, orx has one internally that can be retrieved by users using orxCLOCK_TYPE_SECOND instead of orxCLOCK_TYPE_USER/orxCLOCK_TYPE_CORE. Orx uses it to compute framerate but it's there for anyone who needs it.

    - If you need a periodical call, you can also request a timer based on an already existing clock using orxClock_AddTimer()/orxClock_AddGlobalTimer(). This is usually a bit easier and more flexible than creating a brand new clock. Also a timer can be modified/cancelled/re-added from within the timer callback itself, which can come in handy (I'm using this to simulate keypress repetition in the console, for example, where the initial delay is different than subsequent ones).

    - Back to the original clock simulation example, here's a pure config-based version that uses a 1-second tick resolution:

    In config:
    [RunTime]
    Time = 0
    
    [Clock]
    Graphic = @
    Text = @
    TrackList = Tick
    
    [Tick]
    0 = > Config.GetValue RunTime Time   #
          Object.SetText ^ "Clock: <s"
    1 = > Config.GetValue RunTime Time   #
        > + < 1                          #
          Config.SetValue RunTime Time < #
          Object.AddTrack ^ Tick
    

    Open the console (by pressing backquote at runtime), and type:
    object.create Clock
    
  • edited November 2014
    Coosadog, have a quick look at the pinball game I'm working on.

    The score and ball counters are digital text that is defined in the config but changed in code.

    This sounds pretty much what you are trying to do.

    I created a bitmap LED font in Blender & Gimp:

    ledfont.png


    Then in the config, create a Font definition which sets the image to use, what the characters are to mapped to, and what the spacing and character size is:
    [LedFont]
    Texture = ledfont.png
    CharacterList     = 0123456789,
    CharacterSpacing  = (0, 0, 0)
    CharacterSize     = (46, 80,0)
    

    The CharacterSize and CharacterSpacing helps the Orx engine map a real character to the visual portion within the image.

    Then create a Text which uses LedFont, and sets the initial string to something:
    [LedText]
    Font	=	LedFont
    String	=	000,000,000
    

    Next, make a Graphic that uses the Text:
    [LedGraphic]
    Pivot = top left
    Text  = LedText
    

    And like all Objects, make one that uses the Graphic:
    [LedObject]
    Graphic		= LedGraphic
    Position	= (226, 62,0)
    Color		= (228, 228, 228)
    

    Then, as others have already said, you can change the text in code at any time on the Object to display text using the characters defined in your Font / CharacterList:
    orxOBJECT *scoreObject = orxObject_CreateFromConfig("LedObject");
    
    orxObject_SetTextString(scoreObject, "1,527,000");
    orxObject_SetTextString(scoreObject, "1,024");
    orxObject_SetTextString(scoreObject, "9,8,7,6,5");
    orxObject_SetTextString(scoreObject, "246813570378954534");
    

    Hopefully this is what you are after. Essentially, you need to make your font into a bitmap and map to characters. Then change the value in code to your heart's content.
    ledfont 35.1K
  • edited November 2014
    Thanks for all the scoop. I like the idea of using the internal clock instead of starting another. I'll get on some code here in a bit. 3 projects at the same time. I'm multitasking...
  • edited November 2014
    Thanks sausage. Your pinball game is very cool. I like the led font. nice. like i told iarwain, I'll plug in some of this stuff here in a bit and give it a go. I certainly appreciate all the feedback, as usual. I'll keep you posted on my progress.
  • edited November 2014
    Thanks all! I used Knolan's example to get out of the ditch. I wasn't setting String= and then setting Text= from that. I was just setting Text="---". I'll have to remember that. Next step is to use the internal clock and then I'm gonna try out sausage's font. That'll be pretty nice I think. So, really, I have my first tiny little project that works.
Sign In or Register to comment.