Updating text objects

edited March 2011 in Help request
I want to display time left and score info as text.

I figured out, how to update it, but I think it's not the proper way.
void orxFASTCALL Game::UpdateScore(){
	orxOBJECT *object = Tools::GetObjectByName("Skore"); // get score object
	orxConfig_Load("Skore.ini");
	orxConfig_GetKey(0);
	orxConfig_PushSection("SkoreString");
	orxConfig_SetS32("String", pocetKrtku); // update String in config file with actual score
	
	if (object != orxNULL) {
		orxObject_Delete(object); //delete text object
		orxObject_CreateFromConfig("Skore"); //and create it again so it will update
	}	
}

[strike]It works fine[/strike] Edit: It works fine on Mac/iPhone problem is with some render glitches while I switch ShowFPS to off. When new object appears, every text object disappears and render again with little delay.

I'm using generated font.

Comments

  • edited March 2011
    Hey mate,

    Give this a try:
    orxString *SkoreString = (orxSTRING*)Tools:GetObjectByName("SkoreString");
    
    SkoreString = pocetKrtku;
    

    Be aware, I am making assumptions about what your code is here. Firstly, SkoreString in your Skore.ini file is a text object.

    Like this:
    [SkoreString]
    Text = TextToBeReplaced
    

    and secondly, I am assuming that your variable 'pocetKrtku' is a pre-prepaired string of text to replace everything else with.
  • edited March 2011
    My config for score is
    [Skore]
    Graphic = SkoreText
    Position = (30.0, 200.0, 0.0)
    
    [SkoreText]
    Text = SkoreString
    Position = (0.0, -32.0, 0.0)
    Color = (200, 200, 0)
    
    [SkoreString]
    String = 0
    Font = Font
    

    and variable pocetKrtku is simple int with actual score
  • edited March 2011
    hmm, okay one second, I'm a little rusty, will do a quick check and get back to you. :) expect an edit momentarily!

    *first edit* okay my original suggestion was bad, sorry. I'll have a fix in a second when I correct my horrid assumptions :D

    *edit two goes here! :D*
  • edited March 2011
    I came up with this
    orxSTRING *SkoreString = (orxSTRING*)Tools::GetObjectByName("SkoreString");
    std::string skore = Tools::ConvertInt(pocetKrtku);
    	
    orxCHAR *cskore[3];
    strcpy(*cskore, skore.c_str());
    	
    SkoreString = cskore;
    
    But it works exact same way as previous code. It updates text string, but not update text object so number stays with default value.
  • edited March 2011
    Hmm, interesting.

    (and sorry for taking so long!)

    try also setting your
    [SkoreText] object to [SkoreString] afterwards. (This should 'update' the text object with the now-formatted string object.)

    *edit:* " orxObject_SetTextString( [SkoreText], [SkoreString] ) " might be of use to you. I believe this will allow you to do both steps. Update the [SkoreString] (for formatting etc) and then update the [SkoreText]with the [SkoreString].
  • edited March 2011
    Kenn wrote:
    I want to display time left and score info as text.
    As Grey pointed out, you simply need to call orxObject_SetTextString() on your score object with the new string.

    I wanted to give more details about some of the functions you were calling in your first version though:
    I figured out, how to update it, but I think it's not the proper way.
    void orxFASTCALL Game::UpdateScore(){
    	orxOBJECT *object = Tools::GetObjectByName("Skore"); // get score object
    	orxConfig_Load("Skore.ini");
    

    You might not want to call orxConfig_Load() in an update function as it will read the original data from disk and it is likely to kill your performances.
    	orxConfig_GetKey(0);
    

    This function has no effect if you don't store it's result. It's only use is to access config entries when you don't know their actual key (if you want to parse all the key/value pairs in a section for example).
    	orxConfig_PushSection("SkoreString");
    	orxConfig_SetS32("String", pocetKrtku); // update String in config file with actual score
    	
    	if (object != orxNULL) {
    		orxObject_Delete(object); //delete text object
    		orxObject_CreateFromConfig("Skore"); //and create it again so it will update
    	}	
    }
    
    Shouldn't you store your object again here? Otherwise your reference will still point to the deleted one, which could produce flickering artefacts.

    Also you don't call orxConfig_PopSection() which will lead to undefind behavior.
    [strike]It works fine[/strike] Edit: It works fine on Mac/iPhone problem is with some render glitches while I switch ShowFPS to off. When new object appears, every text object disappears and render again with little delay.

    I'm using generated font.

    Who call this update function? Is it called from the core clock or from another one, possibly one you created? If so, when turning VSync to off, they can't be synchronized and that will also result in flickering artefacts.

    Hope this helps! =)
  • edited March 2011
    Trying your examples and figured out I have problem with conversion from string (or char) to orxCHAR so I can call orxObject_SetTextString.
    orxSTRING *SkoreString = (orxSTRING*)Tools::GetObjectByName("SkoreString");
    	orxOBJECT *text = Tools::GetObjectByName("SkoreText");
    	std::string skore = Tools::ConvertInt(pocetKrtku);
    	const char *cskore;
    	cskore = skore.c_str();
    	orxCHAR *orxchar;
    	strcpy(orxchar, cskore);
    	SkoreString = *cskore;
    	
    	orxObject_SetTextString(text, *SkoreString);
    

    This crashes on strcpy. I also tried many other ways and always get compile error or crash on strcpy or SetTextString. Not mention that this code looks horrible for me :blush:

    And also sorry for taking time with my response, have some things to do.

    And for last, thank you for explaining me how orxConfig functions work, I've learned something new again :)
  • edited March 2011
    Sorry for the quick answer but I can't stay long on the computer for now.
    Your strcpy crashes because you didn't allocated any memory for the destination string. If you want to duplicate a string you need to call strdup (or orxString_Duplicate).

    In your case you should be able to use the C string directly with something like:
    orxObject_SetTextString(MyObject, MyString.c_str());
    

    If it doesn't compile you might simply have to add a cast on the second parameter. :)
  • edited March 2011
    It's finally working! Problem was in wrong object used for text update.

    Now closer to my display glitch problem. I'm using one spawner for enemies in game, standard clock for time updating (every second) and display problem appears when all spawned objects are deleted and new appears.

    You talked about clock syncing. I found function for resync clock or all clocks but nothing changed.
    orxCLOCK *clockScena = orxClock_Create(1, orxCLOCK_TYPE_USER);
    	orxOBJECT *time = orxObject_CreateFromConfig("Time");
    	orxClock_Register(clockScena, Game::UpdateTime, time, orxMODULE_ID_TEXT, orxCLOCK_PRIORITY_NORMAL);
    
    void orxFASTCALL Game::UpdateTime(const orxCLOCK_INFO* clockInfo, void* context){
    	timeLeft--;
    	
    	orxOBJECT *text = orxOBJECT(context);
    	
    	std::string time = Tools::ConvertInt(timeLeft);
    	
    	if (text != orxNULL) {
    		orxObject_SetTextString(text, time.c_str());
    	}	
    }
    

    And for more info. I'm using 4 text objects with own font, all of them configured in config files and if I comment font definition (text is rendered with basic font) in one of them, problem dissapears.
  • edited March 2011
    Mmh, interesting. Can you post a video somewhere or a running version of your project with sources so I can test myself and see exactly what's going on?
    If you want to keep that private, you can send me a link via the private messaging feature of the forum.

    In your case to keep everything synchronized on the same clock (the core one that also does the rendering) I would instead use orxClock_AddGlobalTimer() to update your time left counter instead of creating a new clock.
  • edited March 2011
    Do you prefer XCode or Visual Studio project?
  • edited March 2011
    Doesn't matter. =)
    I got your message, I'll try this tonight and let you know what I find.
  • edited March 2011
    Mmh, compiled your project on windows with vs2008 in both release and debug. Couldn't see any rendering glitch. :(

    I'll try again on a MacBook but I don't have access to it right now. Maybe could you upload a video of the glitch as I'm not exactly sure what to look for?

    Also, I found an optimization bug in vs2008's compiler with your project in release, leading to a stack corruption!
    If I slightly change a line while not changing its logic, the bug disappear. Very scary! ^^
  • edited March 2011
    Weird, Tried to cmpile my code in Visual Studio and it crashes while working with vectors :P

    Anyway, here is video witch text render glitch.

    Edit: wasn't successful uploading it here, so there's link

    http://www.ipappdb.com/Glitch.mov
  • edited March 2011
    Yep, the crash is the one I mentioned earlier. It's due to a wrong aggressive optimization made by the compiler (not sure why).
    You can fix it by changing your input function to directly return the result from orxRender_GetWorldPosition() (using return *orxRender_GetWorldPosition(...);).

    EDIT: I'll look at the video at home tonight. :)
  • edited March 2011
    Actually I watched it here and I saw the glitch. I'll try to reproduce it on a mac then, I definitely didn't see that on windows.

    What version of orx are you using? Which version of MacOSX/drivers?
  • edited March 2011
    Using ORX 1.2 from packages (not SVN) but probably should try to compile from SVN. Have lates OS X and compiling for 10.5 as you can see in my project template.

    Have this glitch even on iPhone (teser and device) and Windows too - Windows XP prof. Visual Studio 2008 pro
  • edited March 2011
    It looks a lot like an issue I fixed a couple of months ago (a texture not being updated in the pipeline under certain circumstances) so using the svn version should fix that. :)
    You'll also get a bunch of new features, improvements and better performances. You can read the full list in the CHANGELOG file.
  • edited March 2011
    Weird. I got crash in different function (SetNewPosition) while spawning object.

    I'll try SVN sources tonight, hope I won't have any problems :P
  • edited March 2011
    Yeah, that's definitely weird and using the SVN will hopefully solve these issues. :) Let me know if it's not the case.
Sign In or Register to comment.