timed string

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
EvIl_DeViL
Posts: 89
Joined: Tue Nov 06, 2007 4:49 pm

timed string

Post by EvIl_DeViL »

hi all! has anyone an idea of how can I create a function that gets in input a string pointer and a float that represent the string to be write and how long has it been to write?
i tryed

Code: Select all

void write(IGUIEnvironment* env, const wchar_t* stringa){
    int winwidth=1024,winheight=768,xsxmarg=0,xdxmarg=0,ymarg;
    core::dimension2d<s32>::dimension2d sxmarg;
    IGUISkin* skin = env->getSkin();
    IGUIFont* font = env->getFont("media/font/fonthaettenschweiler.bmp");
    if (font)
	skin->setFont(font);
	sxmarg=font->getDimension(stringa);
	xsxmarg=(winwidth/2)-(((sxmarg.Width)/2));
	xdxmarg=(winwidth/2)+(((sxmarg.Width)/2));
	ymarg=(winheight*80)/100;
	env->addStaticText(stringa,rect<int>(xsxmarg,ymarg,xdxmarg+3,(ymarg+(sxmarg.Height))), true);	
}  
but my FPS from ~250 gets to ~100 then to ~50 then to ~20 until they arrive to ~4... i use this function like this (this is quite similar to my main loop):

Code: Select all

while(device->run())
{
 write(env,stringa[0]);
 cameratoobject(camera,anms,distance);
 driver->beginScene(true, true, video::SColor(255,113,113,133));
 camcord=camera->getPosition();
 camera->setPosition(camcord);
 env->drawAll();
 smgr->drawAll();
 device->getGUIEnvironment()->drawAll();
 driver->endScene();
}
thank to anyone who will help me!
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

you're creating a new text box each time...
so you'll get 100s or even 1000s of text boxes... :lol:
either use setText(...) on a single text box or delete the old one before creating a new... ;)
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
EvIl_DeViL
Posts: 89
Joined: Tue Nov 06, 2007 4:49 pm

Post by EvIl_DeViL »

do you think that exist a function to display a string for a specified time or I have to create my own?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Yes, you have to do it on your own.
EvIl_DeViL
Posts: 89
Joined: Tue Nov 06, 2007 4:49 pm

Post by EvIl_DeViL »

any idea to suggest? something already made somewhere?
Kriolyth
Posts: 26
Joined: Wed Mar 26, 2008 6:59 pm
Location: Moscow, Russia

Post by Kriolyth »

Let me guess... sprintf? :)
The cake is a lie.
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Fortunately, sprintf does neither display a string, nor for a limited time :roll:
You'll have to check the timer and remove the text once some time went by. Or, in case you're doing scene node stuff, you can trigger some animator to wait for some time and remove the node later on.
EvIl_DeViL
Posts: 89
Joined: Tue Nov 06, 2007 4:49 pm

Post by EvIl_DeViL »

yes but i want to do this stuff in a function...
this function has to be placed in the main loop... now... how can this function understand that it has been called the first time?
I know i have to take the current time and add it the time that the string has to be displayed... then when the current time overcome the sum of the previous result the string has to be deleted... the problem is that this function has to display only one frame on any call, not for the entire during of the time that the string has to be displayed or the game will be paused until the string is displayed! (I hope you understood XD)
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Re: timed string

Post by Acki »

well, I'm not realy sure what you mean, but I think this is close to it:
I think you want to show a new text only if the old text was deleted...
the old text shall be deleted after a specific time...
am I right ???
for this you'll have to add a timer flag to the function and check it's state...
also you'll have to store the pointer to the text box, so you can access it later...

Code: Select all

void write(IGUIEnvironment* env, const wchar_t* stringa){
    // pointer to text box
    static IGUIStaticText* txt = 0;
    // flag for life time in ms
    static s32 lifeTime = 0;
    // holder for the time the function was called the last time
    // I'll do it with Windows API, but you can use the Irrlicht timer, too
    static s32 oldTime = GetTickCount();
    // check if the time has passed
    if(lifeTime > 0){
      // the time is running, so decrease the life time
      lifeTime -= GetTickCount() - oldTime;
      oldTime = GetTickCount();
      return; 
    }
    // if there is already a box then delete it first
    if(txt) txt->remove();
    // now set the life time (let's say 10 seconds)
    lifeTime = 10000;

    int winwidth=1024,winheight=768,xsxmarg=0,xdxmarg=0,ymarg;
    core::dimension2d<s32>::dimension2d sxmarg;
    IGUISkin* skin = env->getSkin();
    IGUIFont* font = env->getFont("media/font/fonthaettenschweiler.bmp");
    if (font)
	skin->setFont(font);
	sxmarg=font->getDimension(stringa);
	xsxmarg=(winwidth/2)-(((sxmarg.Width)/2));
	xdxmarg=(winwidth/2)+(((sxmarg.Width)/2));
	ymarg=(winheight*80)/100;

    // and don't forget to store the new text box
	txt = env->addStaticText(stringa,rect<int>(xsxmarg,ymarg,xdxmarg+3,(ymarg+(sxmarg.Height))), true);	
}  
I didn't test this, it's just from the top of my head, but it should work... ;)
well, it could be done better, but the principle should be cleare now... :lol:
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
EvIl_DeViL
Posts: 89
Joined: Tue Nov 06, 2007 4:49 pm

Post by EvIl_DeViL »

it works! O.O you're great!! thank you so much! =]
EvIl_DeViL
Posts: 89
Joined: Tue Nov 06, 2007 4:49 pm

Post by EvIl_DeViL »

but IGUIStaticText* txt, s32 lifeTime, s32 oldTime are static! O.o
they remain in memory... isn't there a way to declare them as normal variable without the static specificator?
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

the only other way would be to declare them global...
because you need their values after the function has returned and called again...

but I have another idea !!! 8)
you could add the lifetime stuff direct to the text box (Irrlicht source), so you don't have to take care about this in your program...
you add a function or a variable to the constructor to set the lifetime and in the onDraw() function you check if the lifetime has passed and the element deletes itself then and gives the recources free again...
I do this with my decal scene node...
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
EvIl_DeViL
Posts: 89
Joined: Tue Nov 06, 2007 4:49 pm

Post by EvIl_DeViL »

i tried to do the algorhytm with the pointer but i saw that even your algorythm is able to show only 2 string!!! i don't understand the problem T.T
Acki
Posts: 3496
Joined: Tue Jun 29, 2004 12:04 am
Location: Nobody's Place (Venlo NL)
Contact:

Post by Acki »

sorry, but I don't understand your question... :oops:

it should show only one string for 10 seconds, then another one for 10 secs, and so on...
as I said I didn't test it and it should be only a guide for you, as far as I don't know what you're trying to do... ;)
maybe this approach is completely wrong for what you want to do...

I already didn't understand your first post:
create a function that gets in input a string pointer and a float that represent the string to be write and how long has it been to write?
a float that represents the string ???
How should this work and where is this float ???
while(!asleep) sheep++;
IrrExtensions:Image
http://abusoft.g0dsoft.com
try Stendhal a MORPG written in Java
EvIl_DeViL
Posts: 89
Joined: Tue Nov 06, 2007 4:49 pm

Post by EvIl_DeViL »

the float should be the time XD
(string pointer and a float) that represent... XD
probably i understand the problem... every call to the function is able to decrease the lifetime... only the function that create the string should be able to decrease the time... so if the time reach 0 the following string is surely the next!!
in your version if the lifetime reach 0 in another function (not the one that creates the string) the next call to the function create his string! not the following one!!
i'm triyn'g to work with 2 id... the current id and the local id...
the current id is simply a counter... the local id is the id created by the function that create the string...

if (id==current id)
code of decreasing time
else
return;

i hope you understand XD
Post Reply