Function: get controller state()

Talk about making games.

Function: get controller state()

Postby lcl » Tue Oct 25, 2011 6:31 pm

Hello!

So, some of you may know that GE has GetKeyState(); function, which returns the state of the keyboard.
And well I thought that it's simpler to create same kind of for controller than call getJoystick1Button() million times.. xD

So, here's what I got:
Code: Select all
#define BUTTON_1 1
#define BUTTON_2 2
#define BUTTON_3 3
#define BUTTON_4 4
#define BUTTON_5 5
#define BUTTON_6 6
#define BUTTON_7 7
#define BUTTON_8 8
#define BUTTON_9 9
#define BUTTON_10 10
#define BUTTON_11 11
#define BUTTON_12 12
#define BUTTON_13 13
#define BUTTON_14 14

char joystickKey[15];

char * getControllerState()
{
    int i = 0;
 
    for (i = 0; i < 14; i ++)
    {
        joystickKey[i + 1] = GetJoystick1Button(i); //+ 1 is because for some reason GE doesn't return pointers to array cell 0 correctly.. :P
    }

    return joystickKey;
}


Usage (just the same as using GetKeyState()):
Code: Select all
char * key = getControllerState();

if (key[BUTTON_1])//do some stuff ... xD


And another version with sticks treated as buttons, simply on/off.
Code: Select all
#define BUTTON_1 1
#define BUTTON_2 2
#define BUTTON_3 3
#define BUTTON_4 4
#define BUTTON_5 5
#define BUTTON_6 6
#define BUTTON_7 7
#define BUTTON_8 8
#define BUTTON_9 9
#define BUTTON_10 10
#define BUTTON_11 11
#define BUTTON_12 12
#define BUTTON_13 13
#define BUTTON_14 14
#define STICK_L_R 16 //stick left, pushed right :D
#define STICK_L_L 17
#define STICK_L_U 18
#define STICK_L_D 19
#define STICK_R_R 20
#define STICK_R_L 21
#define STICK_R_U 22
#define STICK_R_D 23

char joystickKey[24];

char * getControllerState()
{
    int i = 0;
 
    for (i = 0; i < 14; i ++)
    {
        joystickKey[i + 1] = GetJoystick1Button(i);
    }
    joystickKey[16] = (abs(GetJoystick1Axis(0))>3000) * (sign(GetJoystick1Axis(0)) == -1);
    joystickKey[17] = (abs(GetJoystick1Axis(0))>3000) * (sign(GetJoystick1Axis(0)) == 1);
    joystickKey[18] = (abs(GetJoystick1Axis(1))>3000) * (sign(GetJoystick1Axis(1)) == -1);
    joystickKey[19] = (abs(GetJoystick1Axis(1))>3000) * (sign(GetJoystick1Axis(1)) == 1);
 
    joystickKey[20] = (abs(GetJoystick1Axis(2))>3000) * (sign(GetJoystick1Axis(2)) == -1);
    joystickKey[21] = (abs(GetJoystick1Axis(2))>3000) * (sign(GetJoystick1Axis(2)) == 1);
    joystickKey[22] = (abs(GetJoystick1Axis(3))>3000) * (sign(GetJoystick1Axis(3)) == -1);
    joystickKey[23] = (abs(GetJoystick1Axis(3))>3000) * (sign(GetJoystick1Axis(3)) == 1);
 
    return joystickKey;
}


Usage:
Code: Select all
char * key = getControllerState();

if (key[STICK_L_R])xvelocity = 5; //If left stick is moved to right, set xvelocity to 5 :D


Sorry for not putting that much comments to the code, I just can't think of anything to put there.. :P

And please tell me if there is something that I could/should optimize. :)

-lcl-
Last edited by lcl on Fri Oct 28, 2011 12:21 pm, edited 1 time in total.
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Function: get controller state()

Postby Hblade » Tue Oct 25, 2011 8:02 pm

neat! :)
Subscribe to my YouTube? - Yes| No
User avatar
Hblade
 
Posts: 4455
Joined: Fri Dec 08, 2006 11:14 pm
Score: 181 Give a positive score

Re: Function: get controller state()

Postby Jagmaster » Thu Oct 27, 2011 12:36 am

Very useful, thank you!
User avatar
Jagmaster
 
Posts: 875
Joined: Sun May 08, 2011 4:14 pm
Location: Not where you think.
Score: 82 Give a positive score

Re: Function: get controller state()

Postby Game A Gogo » Thu Oct 27, 2011 11:33 am

Code: Select all
char * key = malloc(sizeof(char)*15);

Do I see memory leakage?
I'm wondering because it's never getting freed and the function always gets run :P
someone could clarify this for me, someone like Evan xD
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Function: get controller state()

Postby lcl » Thu Oct 27, 2011 5:53 pm

Game A Gogo wrote:
Code: Select all
char * key = malloc(sizeof(char)*15);

Do I see memory leakage?
I'm wondering because it's never getting freed and the function always gets run :P
someone could clarify this for me, someone like Evan xD

Yeah, that's mostly what I meant with:
lcl wrote:And please tell me if there is something that I could/should optimize.

I just don't know where I can free it because I need the function to return it before freeing it.
So, help with this would be helpful :D
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Function: get controller state()

Postby skydereign » Thu Oct 27, 2011 9:07 pm

Simply put, don't use malloc. Just make it an array. Generally speaking you don't need to allocate memory for simple things. If all you are doing is returning the temporary array, then that means you are storing it elsewhere, which means you don't need to allocate the memory for it in the function.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Function: get controller state()

Postby lcl » Thu Oct 27, 2011 10:15 pm

skydereign wrote:Simply put, don't use malloc. Just make it an array. Generally speaking you don't need to allocate memory for simple things. If all you are doing is returning the temporary array, then that means you are storing it elsewhere, which means you don't need to allocate the memory for it in the function.

But I can't return a normal array.
If I try:
Code: Select all
char key[14];

The function returns nothing.
Do you know any way to do this?
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Function: get controller state()

Postby skydereign » Fri Oct 28, 2011 12:16 am

Oh, sorry about that. I didn't actually look at what you were doing , just the problem you were asking about (I was in class). Anyway, it is because you are returning a char*. All it is is an address to the actual array, and if you don't allocate memory for it, the array will disappear when the function ends. So, there are two ways to do this. One is to pass the array you want altered into the function.
Code: Select all
void
function (char string[])
{
    int i;
    for(i=0;i<20;i++)
    {
        //
    }
}


Or the other is to use your function with the memory allocation, and then free the char* you set the function to when you are done. I wouldn't suggest this method as it if unattended will cause a memory leak, and the above method is self contained.
Code: Select all
char* key = getControllerState();
// do stuff with key
free(key);
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Function: get controller state()

Postby Game A Gogo » Fri Oct 28, 2011 11:28 am

Code: Select all
char jkey[15];
char * getControllerState()
{
    int i;
    for (i=0;i<14;i++)
    {
        jkey[i+1]=GetJoystick1Button(i); //+ 1 is because for some reason GE doesn't return pointers to array cell 0 correctly.. :P
    }
    return jkey;
}

I expect this to work, since you're already allocating/un-allocating every time you run the function, which is most likely every frame, might as well allocate it for the time the game runs no?
Programming games is an art,
    Respect it.
User avatar
Game A Gogo
 
Posts: 3466
Joined: Wed Jun 29, 2005 10:49 pm
Location: French Canada *laughs*
Score: 181 Give a positive score

Re: Function: get controller state()

Postby lcl » Fri Oct 28, 2011 12:18 pm

Game A Gogo wrote:
Code: Select all
char jkey[15];
char * getControllerState()
{
    int i;
    for (i=0;i<14;i++)
    {
        jkey[i+1]=GetJoystick1Button(i); //+ 1 is because for some reason GE doesn't return pointers to array cell 0 correctly.. :P
    }
    return jkey;
}

I expect this to work, since you're already allocating/un-allocating every time you run the function, which is most likely every frame, might as well allocate it for the time the game runs no?

Thanks, got to try that, I don't know why I hadn't thought of it before.. :lol:
User avatar
lcl
 
Posts: 2339
Joined: Thu Mar 25, 2010 5:55 pm
Location: Finland
Score: 276 Give a positive score

Re: Function: get controller state()

Postby Zivouhr » Thu Jun 26, 2014 10:45 pm

Thanks for the info on programming for a controller handheld, LCL.
City of Rott Game created on Game Editor http://cityofrott.wordpress.com/
User avatar
Zivouhr
 
Posts: 549
Joined: Sat May 17, 2014 2:12 pm
Score: 59 Give a positive score

Re: Function: get controller state()

Postby Zivouhr » Sun Nov 02, 2014 3:32 pm

lcl wrote:Thanks, got to try that, I don't know why I hadn't thought of it before.. :lol:


Thanks for the tips. My PS3 controller DPad is registering on my computer now (after figuring out how to get it recognized on a computer). That other controller, the super nintendo one, might be broken or something for the DPad, as this one works on PS3.

EDIT: Here's the GED Game Editor file for all to test a PS3 Controller with.
Includes functioning for:
DPad
All buttons (excluding the PS Power on button)
Analog (have yet to figure out how to get both functioning at same time, but can get either one plus Dpad at same time working, just need to figure out how to have an Analog without the character slowly floating; any tips appreciated on how to do this). To have the character stop floating, I have to "//" the ANALOG code in this file, and just use the DPad to control your character.

PS3 GAME CONTROLLER ON GAME EDITOR
PS3gamepadGE2014.ged
(8.21 KiB) Downloaded 514 times


I hope that helps, thanks again for all the help here LCL and the Game Editor Forum! It's awesome to finally get a game working with a PS3 controller.
City of Rott Game created on Game Editor http://cityofrott.wordpress.com/
User avatar
Zivouhr
 
Posts: 549
Joined: Sat May 17, 2014 2:12 pm
Score: 59 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron