Homebrew libctru/citro3d - Is there an optimal way to make my code wait?

titoEarly

Active Member
OP
Newcomer
Joined
Jan 12, 2016
Messages
27
Trophies
0
XP
118
Country
Hey there! I'm making my own game and I'm using citro3d/citro2d. I'm using hidKeysHeld() in order to update some graphics depending on which button the user is holding. The thing is, it's too fast and I want to slow it down a bit. I would like to make sure the buttons are being held for like 500 ms and then proceed with the code. I haven't figured a nice way to do so, though. What I'm doing at the moment is using svcSleepThread(500000000) but I'm also monitoring the CPU usage with C3D_GetProcessingTime() and it increases dramatically from 4% to 300%. Do you know any workaround that would do better than what I'm currently doing? Thanks a lot!
 

StuntHacks

Well-Known Member
Newcomer
Joined
Jan 19, 2017
Messages
46
Trophies
0
Age
23
Location
Vienna
XP
217
Country
Austria
For what you're doing, a frame-counter (or, if you need to be more precise, a tick-counter), would be the best option. You can check for when the button is pressed with `hidKeysDown` and if that's the case, check whether they are still held with `hidKeysHeld`. If they are still held, increase the frame counter by 1 and execute the action once the frame-counter reaches 30 (i.e., 500ms).
 
  • Like
Reactions: titoEarly

titoEarly

Active Member
OP
Newcomer
Joined
Jan 12, 2016
Messages
27
Trophies
0
XP
118
Country
For what you're doing, a frame-counter (or, if you need to be more precise, a tick-counter), would be the best option. You can check for when the button is pressed with `hidKeysDown` and if that's the case, check whether they are still held with `hidKeysHeld`. If they are still held, increase the frame counter by 1 and execute the action once the frame-counter reaches 30 (i.e., 500ms).

Thanks! That really helped. So this is what I am doing:

Code:
int frame = 0;

while (aptMainLoop()) {
    hidScanInput();
    u32 kHeld = hidKeysHeld();

    if (kHeld & KEY_UP) {
        if (frame >= MAX_FRAME) {
            // do something ...
            frame = 0;
        }
        else frame++;
    }

    // ...
}

It works the way I want it to work and CPU seems to be doing fine, but I'm not too sure if it's safe to assume it'll always behave the same way. I mean, does it always take the same time to count up to MAX_FRAME? :unsure:
 

catlover007

Developer
Developer
Joined
Oct 23, 2015
Messages
711
Trophies
1
XP
3,550
Country
Germany
It works the way I want it to work and CPU seems to be doing fine, but I'm not too sure if it's safe to assume it'll always behave the same way. I mean, does it always take the same time to count up to MAX_FRAME? :unsure:
yes and no. As long you have vsync enabled or any other kind of frame cap, this approach works. But e.g. if you miss your 60 FPS target, everything is going to slow down. That's why you're better off by not counting frames but the durations of frames. You can use these functions to accomplish this: https://github.com/smealum/ctrulib/blob/master/libctru/include/3ds/os.h#L147-L168

This article is also a classic read on this subject:
https://gafferongames.com/post/fix_your_timestep/
 
Last edited by catlover007,
  • Like
Reactions: titoEarly
General chit-chat
Help Users
  • No one is chatting at the moment.
  • JuanMena @ JuanMena:
    Will you give me mouth to mouth oxygen if my throat closes?
  • K3N1 @ K3N1:
    Nah the air can do that
  • K3N1 @ K3N1:
    Ask @x65943 he's trained for that stuff
  • JuanMena @ JuanMena:
    Kissing random dudes choking in celery? Really? Need to study for that?
  • K3N1 @ K3N1:
    Yes it requires a degree
  • K3N1 @ K3N1:
    I could also yank out the rest of my teeth but theirs professionals for that
  • x65943 @ x65943:
    If your throat closes, putting oxygen in your mouth will not solve anything - as you will be introducing oxygen prior to the area of obstruction
  • JuanMena @ JuanMena:
    Just kiss me Kyle.
  • x65943 @ x65943:
    You either need to be intubated to bypass obstruction or create a stoma inferior to the the area of obstruction to survive
  • x65943 @ x65943:
    "Just kiss me Kyle." And I thought all the godreborn gay stuff was a smear campaign
  • JuanMena @ JuanMena:
    If I die, tell my momma I won't be carrying Baby Jesus this christmas :sad::cry:
  • K3N1 @ K3N1:
    Smear campaigns are in The political section now?
  • JuanMena @ JuanMena:
    Chary! Chary! Chary, Chary, Chary!
  • Sonic Angel Knight @ Sonic Angel Knight:
    Pork Provolone :P
  • Psionic Roshambo @ Psionic Roshambo:
    Sounds yummy
  • K3N1 @ K3N1:
    Sweet found my Wii u PSU right after I ordered a new one :tpi:
  • JuanMena @ JuanMena:
    It was waiting for you to order another one.
    Seems like, your PSU was waiting for a partner.
  • JuanMena @ JuanMena:
    Keep them both
    separated or you'll have more PSUs each year.
  • K3N1 @ K3N1:
    Well one you insert one PSU into the other one you get power
  • JuanMena @ JuanMena:
    It literally turns it on.
  • K3N1 @ K3N1:
    Yeah power supplies are filthy perverts
  • K3N1 @ K3N1:
    @Psionic Roshambo has a new friend
    +1
  • JuanMena @ JuanMena:
    It's Kyle, the guy that went to school to be a Certified man Kisser.
  • Psionic Roshambo @ Psionic Roshambo:
    Cartmans hand has taco flavored kisses
  • A @ abraarukuk:
    hi guys
    A @ abraarukuk: hi guys