CC65 pause pattern

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems. See the NESdev wiki for more information.

Moderator: Moderators

User avatar
-Basti-
Posts: 40
Joined: Sun Sep 26, 2010 10:29 pm

Re: CC65 pause pattern

Post by -Basti- »

thefox wrote:
-Basti- wrote:

Code: Select all

static unsigned char input = pad_poll(0);
static unsigned char lf_input = pad_state(0);
This calls pad_poll/pad_state once during the initialization of the program, not what you want. (I can't even remember if this is valid in C, or only C++.)

Sorry my bad, I made a mistake when summarizing my problem. input and lf_input are written each frame through my input-routine.

I fooled around a bit more with my code and found out, that using function pad_trigger instead of pad_poll solves my problem without comparing any input of the last frame. :shock:

Could somebody explain the difference between function pad_trigger and pad_poll?
User avatar
-Basti-
Posts: 40
Joined: Sun Sep 26, 2010 10:29 pm

Re: CC65 pause pattern

Post by -Basti- »

While i am optimizing my input routine at the moment, I wonder if C`s switch-case statement is recommendable instead of a large if-statement cascade, when using CC65?
lidnariq
Posts: 11432
Joined: Sun Apr 13, 2008 11:12 am

Re: CC65 pause pattern

Post by lidnariq »

Yeah, switch is much better, because cc65 doesn't really do much optimization. It ends up producing a cmp #imm / jeq @label cascade instead of "calculate lvalue and rvalue and compare and then jump".

Switch also groups everything with the same MSB together, so it'll be better if it collides with C's type promotion.
niconii
Posts: 219
Joined: Sun Mar 27, 2016 7:56 pm

Re: CC65 pause pattern

Post by niconii »

-Basti- wrote:Could somebody explain the difference between function pad_trigger and pad_poll?
Ah! It seems that neslib actually already does the method I described (though in a slightly more roundabout way). So, you don't need to calculate that yourself after all, just use pad_trigger's value, which is equivalent to pressed.

EDIT: It's worth noting that pad_trigger seems to call pad_poll for some reason, though. You should update the variables each frame like this to avoid polling multiple times in a frame, which could make you miss button presses:

Code: Select all

pressed = pad_trigger(0);
pad = pad_state(0);
na_th_an
Posts: 558
Joined: Mon May 27, 2013 9:40 am

Re: CC65 pause pattern

Post by na_th_an »

I use *only* pad_poll and Nicole's method and I can get both button presses which have just been performed during the current frame and button presses which were performed before. I find it more simple.

Code: Select all

while (1) {
    pad_this_frame = pad0;
    pad0 = pad_poll (0);
    pad_this_frame = (pad_this_frame ^ pad0) & pad0;

    if (pad0 & PAD_B) { 
        // PAD B was pressed sometime in the past and it's still down.
    }

    if (pad_this_frame & PAD_A) { 
        // PAD A has been pressed *this* frame, and *not* before 
    }
}
Seems to work fine.
Post Reply