Page 1 of 1

Fast question about HUD and strings

Posted: Wed Apr 15, 2020 10:50 pm
by danibus
Actually made a HUD using tiles in PLAN A, and using sprintf+VDP_drawText() to draw numbers(lives, energy, etc).
I'm using 5 different sprintf+VDP_drawText(...),10 fps less than not use them.

I'm not using a "big" sprintf( %d1 %d2 %d3 etc), then sprintf will delete HUD tiles when printing "space" characters.

Alternatives?
Print HUD with sprites and numbers with sprintf+VDP_drawText() ?
Don't want to waste sprites in HUD, if possible. Maybe in bottom UI I must use sprites, to avoid being covered with main character...

Maybe use PLAN_B? Don't want to use as in another part of game this plan will get some horizontal scroll.


This is my code (just for bottom UI only, top UI is similar):

Code: Select all

    
    
    //HUD
    Game.IU2 = unpackMap(IU_bottom.map, NULL);
    VDP_loadTileSet(IU_bottom.tileset, Game.id_tile_final_IU_top, CPU);
    VDP_setMapEx(PLAN_A, Game.IU2, TILE_ATTR_FULL(PAL1, FALSE, FALSE, FALSE, Game.id_tile_final_IU_top), 23, 25, 0, 0, 19, 2);
    [...]  
    
    
    [...]
    //HUD:numbers
    char s1[7], s2[7], s3[2], s4[2], s5[2];

    sprintf(s1, "%4u", Game.score);
    sprintf(s2, "%4u", Game.high_score);
    sprintf(s3, "%1u", Player.lives);
    sprintf(s4, "%1u", Player.money);
    sprintf(s5, "%1u", Player.magic);
    VDP_drawText(s1, 06, 0);
    VDP_drawText(s2, 18, 0);
    VDP_drawText(s3, 26, 26);
    VDP_drawText(s4, 32, 26);
    VDP_drawText(s5, 37, 26);

Screenshot:
Sin título.png
Sin título.png (139.31 KiB) Viewed 6701 times

Re: Fast question about HUD and strings

Posted: Thu Apr 16, 2020 10:01 am
by Stef
Don't use sprintf for something you need to update often. sprintf is *terribly* slow.
If you really need to convert int to String then use intToStr(..) method. It's a bit less convenient than sprintf(..) but much faster (still consuming CPU time).
About overlapping, you can put plane A (containing the HUD) in high priority while keeping plane B and sprites in low priority so HUD is always on top.

Re: Fast question about HUD and strings

Posted: Thu Apr 16, 2020 12:38 pm
by danibus
Thanks Stef. Is there any other way to show score than use VDP_drawText(your string here)?

Re: Fast question about HUD and strings

Posted: Thu Apr 16, 2020 12:49 pm
by Stef
VDP_drawText(..) is the easiest way definitely (using the window plan to not sacrifice a whole background for that)..
You can use sprites to do it but SGDK doesn't provide any help for that unfortunately.

Re: Fast question about HUD and strings

Posted: Sun Apr 19, 2020 5:23 pm
by danibus
So I'm doing this right now:

Code: Select all

    uintToStr(Game.score,        s1, 0);
    uintToStr(Game.high_score,   s2, 0);
    uintToStr(Player.lives,      s3, 0);
    uintToStr(Player.money,    s4, 0);
    uintToStr(Player.magic,     s5, 0);

    SYS_disableInts();
    VDP_drawText(s1, 06, 0);
    VDP_drawText(s2, 18, 0);
    VDP_drawText(s3, 26, 26);
    VDP_drawText(s4, 32, 26);
    VDP_drawText(s5, 37, 26);
    SYS_enableInts();

Almost the same result. Just one question, reading changelog of 1.32 SGDK version, I read:
"added int16ToStr(..) and uint16ToStr(..) methods (faster than intToStr(..) or uintToStr(..) for 16 bit integer, thanks to clbr)"

But there is not int16ToStr(..) functions in last version, also not in 1.4x version.
It was removed but see nothing in forum.

I will try to use sprites instead VDP_drawText

Re: Fast question about HUD and strings

Posted: Sun Apr 19, 2020 6:49 pm
by Stef
Yeah i modified code so you can just use intToStr(..) now and it should be fast :)

Re: Fast question about HUD and strings

Posted: Wed Apr 22, 2020 10:53 am
by vnsbr
Do you need to update string every frame? Maybe only when score and values change?

Re: Fast question about HUD and strings

Posted: Wed Apr 22, 2020 1:29 pm
by danibus
vnsbr wrote:
Wed Apr 22, 2020 10:53 am
Do you need to update string every frame? Maybe only when score and values change?
That's a very good idea. I will try it