Fast question about HUD and strings

SGDK only sub forum

Moderator: Stef

Post Reply
danibus
Very interested
Posts: 135
Joined: Sat Feb 03, 2018 12:41 pm

Fast question about HUD and strings

Post by danibus » Wed Apr 15, 2020 10:50 pm

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 6443 times

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: Fast question about HUD and strings

Post by Stef » Thu Apr 16, 2020 10:01 am

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.

danibus
Very interested
Posts: 135
Joined: Sat Feb 03, 2018 12:41 pm

Re: Fast question about HUD and strings

Post by danibus » Thu Apr 16, 2020 12:38 pm

Thanks Stef. Is there any other way to show score than use VDP_drawText(your string here)?

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: Fast question about HUD and strings

Post by Stef » Thu Apr 16, 2020 12:49 pm

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.

danibus
Very interested
Posts: 135
Joined: Sat Feb 03, 2018 12:41 pm

Re: Fast question about HUD and strings

Post by danibus » Sun Apr 19, 2020 5:23 pm

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

Stef
Very interested
Posts: 3131
Joined: Thu Nov 30, 2006 9:46 pm
Location: France - Sevres
Contact:

Re: Fast question about HUD and strings

Post by Stef » Sun Apr 19, 2020 6:49 pm

Yeah i modified code so you can just use intToStr(..) now and it should be fast :)

vnsbr
Interested
Posts: 48
Joined: Sun Dec 17, 2017 4:13 pm

Re: Fast question about HUD and strings

Post by vnsbr » Wed Apr 22, 2020 10:53 am

Do you need to update string every frame? Maybe only when score and values change?

danibus
Very interested
Posts: 135
Joined: Sat Feb 03, 2018 12:41 pm

Re: Fast question about HUD and strings

Post by danibus » Wed Apr 22, 2020 1:29 pm

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

Post Reply