Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Actual programming thread

Name: Anonymous 2008-01-07 15:01

Okay negroes, listen up.

Started programming shit for PSP, and I want to implement a pseudo-polling thing for key checking (a la SDL). Currently I've got this big fuck-ass function that's ugly to look at, so I was wondering, /prog/, is there any way to make it smaller/more efficient?
I'd do a for loop but the PSP_CTRL_START etc are bitmasks, so that would be difficult.


enum _buttons {
    BTN_SELECT,
    BTN_START,
    BTN_UP,
    BTN_RIGHT,
    BTN_DOWN,
    BTN_LEFT,
    BTN_LTRIGGER,
    BTN_RTRIGGER,
    BTN_TRIANGLE,
    BTN_CIRCLE,
    BTN_CROSS,
    BTN_SQUARE,
    BTN_HOME,
    BTN_HOLD
};

void getKeysPoll (bool k[14], SceCtrlData pad) {
    sceCtrlReadBufferPositive(&pad, 1);
   
    //Is there an easier way to do this? It's a bit long-winded
    if(pad.Buttons & PSP_CTRL_SELECT && !k[BTN_SELECT]) {
        k [BTN_SELECT] = true;
        printf ("SELECT pressed\n");
    } else if (k[BTN_SELECT]) {
        k [BTN_SELECT] = false;
        printf ("SELECT released\n");
    }
   
    if(pad.Buttons & PSP_CTRL_START && !k[BTN_START]) {
        k [BTN_START] = true;
        printf ("START pressed\n");
    } else if (k[BTN_START]) {
        k [BTN_START] = false;
        printf ("START released\n");
    }
   
    if(pad.Buttons & PSP_CTRL_UP && !k[BTN_UP]) {
        k [BTN_UP] = true;
        printf ("UP pressed\n");
    } else if (k[BTN_UP]) {
        k [BTN_UP] = false;
        printf ("UP released\n");
    }
   
    if(pad.Buttons & PSP_CTRL_RIGHT && !k[BTN_RIGHT]) {
        k [BTN_RIGHT] = true;
        printf ("RIGHT pressed\n");
    } else if (k[BTN_RIGHT]) {
        k [BTN_RIGHT] = false;
        printf ("RIGHT released\n");
    }
   
    if(pad.Buttons & PSP_CTRL_DOWN && !k[BTN_DOWN]) {
        k [BTN_DOWN] = true;
        printf ("DOWN pressed\n");
    } else if (k[BTN_DOWN]) {
        k [BTN_DOWN] = false;
        printf ("DOWN released\n");
    }
   
    if(pad.Buttons & PSP_CTRL_LEFT && !k[BTN_LEFT]) {
        k [BTN_LEFT] = true;
        printf ("LEFT pressed\n");
    } else if (k[BTN_LEFT]) {
        k [BTN_LEFT] = false;
        printf ("LEFT released\n");
    }
   
    if(pad.Buttons & PSP_CTRL_LTRIGGER && !k[BTN_LTRIGGER]) {
        k [BTN_LTRIGGER] = true;
        printf ("LTRIGGER pressed\n");
    } else if (k[BTN_LTRIGGER]) {
        k [BTN_LTRIGGER] = false;
        printf ("LTRIGGER released\n");
    }
   
    if(pad.Buttons & PSP_CTRL_RTRIGGER && !k[BTN_RTRIGGER]) {
        k [BTN_RTRIGGER] = true;
        printf ("RTRIGGER pressed\n");
    } else if (k[BTN_RTRIGGER]) {
        k [BTN_RTRIGGER] = false;
        printf ("RTRIGGER released\n");
    }
   
    if(pad.Buttons & PSP_CTRL_TRIANGLE && !k[BTN_TRIANGLE]) {
        k [BTN_TRIANGLE] = true;
        printf ("TRIANGLE pressed\n");
    } else if (k[BTN_TRIANGLE]) {
        k [BTN_TRIANGLE] = false;
        printf ("TRIANGLE released\n");
    }
   
    if(pad.Buttons & PSP_CTRL_CIRCLE && !k[BTN_CIRCLE]) {
        k [BTN_CIRCLE] = true;
        printf ("CIRCLE pressed\n");
    } else if (k[BTN_CIRCLE]) {
        k [BTN_CIRCLE] = false;
        printf ("CIRCLE released\n");
    }
   
    if(pad.Buttons & PSP_CTRL_CROSS && !k[BTN_CROSS]) {
        k [BTN_CROSS] = true;
        printf ("CROSS pressed\n");
    } else if (k[BTN_CROSS]) {
        k [BTN_CROSS] = false;
        printf ("CROSS released\n");
    }
   
    if(pad.Buttons & PSP_CTRL_SQUARE && !k[BTN_SQUARE]) {
        k [BTN_SQUARE] = true;
        printf ("SQUARE pressed\n");
    } else if (k[BTN_SQUARE]) {
        k [BTN_SQUARE] = false;
        printf ("SQUARE released\n");
    }
   
    if(pad.Buttons & PSP_CTRL_HOME && !k[BTN_HOME]) {
        k [BTN_HOME] = true;
        printf ("HOME pressed\n");
    } else if (k[BTN_HOME]) {
        k [BTN_HOME] = false;
        printf ("HOME released\n");
    }
   
    if(pad.Buttons & PSP_CTRL_HOLD && !k[BTN_HOLD]) {
        k [BTN_HOLD] = true;
        printf ("HOLD pressed\n");
    } else if (k[BTN_HOLD]) {
        k [BTN_HOLD] = false;
        printf ("HOLD released\n");
    }
}

In b4 read K&R etc

Name: Anonymous 2008-01-07 15:07

Also, it doesn't work as it should yet. I just realised that.

Name: Anonymous 2008-01-07 15:36

read K&R etc

Name: Anonymous 2008-01-07 15:55

use a previous_state variable and bit masking ops to figure out down/pressed/released

Name: Anonymous 2008-01-07 15:57

Soppy, OP, you have to wait through the usual fifteen posts of NO EXCEPTIONS first.

Name: Anonymous 2008-01-07 15:58

Create an array for the bitmasks and description strings in the same order as the _buttons enum, then you can just loop through these arrays.

Name: Anonymous 2008-01-07 16:11

Create an array with pointers to callback functions and call them through the button id.

Name: Anonymous 2008-01-07 16:58

>>4,7
You'll still have to individually go through each button and check whether it's state has changed.

>>1
You'll still have to individually go through each button and check whether it's state has changed. There isn't a better way to do it. Just bite the bullet, and have this code parse the information into events (by using callback functions) or whatever, so you never have to look at it again.

Name: Anonymous 2008-01-07 16:59

>>6
I tried this. Looks much cleaner now, thanks anon!

Name: Anonymous 2008-01-09 19:39


                 
 █    █ ████ ████
 █    █ █    █  █
 █    █ ████ ████
 █    █    █ █   
 ████ █ ████ █   
                 

Name: Anonymous 2008-01-09 19:40


                 
 █    █ ████ ████
 █    █ █    █  █
 █    █ ████ ████
 █    █    █ █   
 ████ █ ████ █

Name: Anonymous 2008-01-09 19:40


                 
 █    █ ████ ████
 █    █ █    █  █
 █    █ ████ ████
 █    █    █ █   
 ████ █ ████ █    
                  

Name: Anonymous 2009-03-18 3:12

The word pirahna, is all I can think of that rhymes with marijuana

Marijuana MUST be legalized.

Name: Anonymous 2010-11-14 19:33


Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List