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
128
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
24
Location
Vienna
XP
227
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
128
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
722
Trophies
1
XP
3,953
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

Site & Scene News

Popular threads in this forum

General chit-chat
Help Users
  • Psionic Roshambo @ Psionic Roshambo:
    Also a food allergy study would be a good idea
  • K3Nv2 @ K3Nv2:
    Turns out you can't sprinkle methamphetamine on McDonald's French fries
    +1
  • ZeroT21 @ ZeroT21:
    they wouldn't be called french fries at that point
    +1
  • ZeroT21 @ ZeroT21:
    Probably just meth fries
    +1
  • K3Nv2 @ K3Nv2:
    White fries hold up
    +1
  • The Real Jdbye @ The Real Jdbye:
    @K3Nv2 sure you can
  • BakerMan @ BakerMan:
    why tf do people hate android users? is it the video quality? just because "AnDrOiD = pOoR" bc they don't cost an arm and a leg like iphones do?
    +1
  • BakerMan @ BakerMan:
    i won't be turned off by an iphone, but don't pick on me for having an android, that's just how this shit should work
  • ZeroT21 @ ZeroT21:
    Should say more what these kind of android users say bout nokia 3310 users
  • BigOnYa @ BigOnYa:
    I've owned both iPhone and Androids over the years. Both are just as good, other than Apples higher price. I'm currently on Android, Samsung S21 I think, and very happy with it.
  • K3Nv2 @ K3Nv2:
    Got my 60 minute steps in whew
    +2
  • BigOnYa @ BigOnYa:
    I get mine in everyday, going back n forth to the fridge for a beer.
    +1
  • K3Nv2 @ K3Nv2:
    6,000 steps in so far legs almost broke getting off
    +1
  • K3Nv2 @ K3Nv2:
    Your mind gets in a werid pattern of just finishing then when you're done you're like I need a soda
  • BigOnYa @ BigOnYa:
    You get a "walkers" high?
  • K3Nv2 @ K3Nv2:
    Not really I just use to love building up a sweat
  • BigOnYa @ BigOnYa:
    Funny, that's what uremum always says
  • K3Nv2 @ K3Nv2:
    Yeah and people that take viagra think they have a big dick
    +1
  • K3Nv2 @ K3Nv2:
    You cant fix one insult edit for another edit you pog
  • BigOnYa @ BigOnYa:
    Nuh I'm on my tablet n it always auto corrects me
  • K3Nv2 @ K3Nv2:
    Heorin and uremum do have close quarters
  • Sonic Angel Knight @ Sonic Angel Knight:
    BIG CHICKEN :P
    K3Nv2 @ K3Nv2: https://youtu.be/q855tNpvDoQ?si=Tl57KMjiVjyBherB +1