if(keyUp[KEY_3] == 1) ??

Talk about making games.

Re: if(keyUp[KEY_3] == 1) ??

Postby skydereign » Thu May 31, 2012 5:36 pm

Well here is one I wrote a while ago. I edited it a bit to make it more convenient (just a function call). It uses a kind of cheap way of locking the function so it can be only called once per frame (it requires the GetKeys function to be called every frame). It was considerably better now, but it still has room for improvement. Keys that have been released have a value of -1.
Code: Select all
char*
GetKeys ()
{
  const int key_reg[] = {KEY_RIGHT, KEY_LEFT, -1}; // must end with -1
  // list of keys that can have keyup states
  // that way it doesn't need to loop through every key
  // because rarely do people want to catch gp2x keys

  // static variables are only initialized once
  static int protect = 0; // used to lock the function (only once per frame)
  static char key_prev[500] = {0}; // or however long GetKeyState is
  static char* key; // function remembers last key state
 
  if(frame%2==protect) // only update once per frame
    {
      int i;
      key=GetKeyState();
     
      for(i=0; key_reg[i]!=-1; i++) // loop through registered keys
        {
          if(key_prev[key_reg[i]]==1 && key[key_reg[i]]==0)
            { // if the key was pressed, but isn't anymore
              key_prev[key_reg[i]]=key[key_reg[i]]; // set key previous
              key[key_reg[i]]=-1; // set to keyup
            }
          else
            {
              key_prev[key_reg[i]]=key[key_reg[i]]; // set key previous
            }
        }

      protect=protect==0; // lock until next frame
    }
 
  return key;
}
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: if(keyUp[KEY_3] == 1) ??

Postby Game A Gogo » Thu May 31, 2012 7:01 pm

That function seems to be a bit strange to work with, but still a nice way of doing things :D My way would involve casting the GetKeyUp(); and GetKeyDown(); like the GetKeyState(); :)
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: if(keyUp[KEY_3] == 1) ??

Postby Game A Gogo » Thu May 31, 2012 7:57 pm

Urg, for some reason I can't get malloc and writting memory to it quite right, so I give up!
I think i'm starting to preffer Visual Studio C++ over GE C, in VSC++, you get a lot more flexibilty. To anyone interested, here's the code I came up so far. But don't use it, it's broken and probably really badly written:
Code: Select all
char* KeyBuffer=malloc(sizeof(char)*256);
char* LastKeyState=malloc(sizeof(char)*256);

//Call this function every frame in the view actor
//    by using View->Draw Event
void GetKeyUpdate()
{
    int i;
    if(KeyBuffer!=NULL)for(i=0;i<167;i++)LastKeyState[i]=KeyBuffer[i];
    KeyBuffer=GetKeyState();
}

//Call this function EVERYTIME you quit!
//That means DO NOT use GE's press esc to quit!
void ClearKeyMemory()
{
    free(KeyBuffer);
    KeyBuffer=NULL;
    free(LastKeyState);
    LastKeyState=NULL;
}

//In case you ever needed to retreive the last key state with a function
char* GetLastKeyState()
{
    return LastKeyState;
}

//Use like the GetKeyState() function, but will return true for
//     keys for 1 frame if the key just got unpressed
char* GetKeyUp()
{
    int i;
    char* ReturnKey=GetKeyState();
    char* KeyStates=GetKeyState();
    if(LastKeyState!=NULL)
    {
        for(i=0;i<167;i++)ReturnKey[i]=(KeyStates[i]==0 && LastKeyState[i]==1);
        return ReturnKey;
    }
    else return NULL;
}

//Use like the GetKeyState() function, but will return true for
//    keys for 1 frame that just got pressed
char* GetKeyDown()
{
    int i;
    char* ReturnKey=GetKeyState();
    char* KeyStates=GetKeyState();
    if(LastKeyState!=NULL)
    {
        for(i=0;i<167;i++)ReturnKey[i]=(KeyStates[i]==1 && LastKeyState[i]==0);
        return ReturnKey;
    }
    else return NULL;
}
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: if(keyUp[KEY_3] == 1) ??

Postby skydereign » Thu May 31, 2012 9:26 pm

Game A Gogo wrote:That function seems to be a bit strange to work with, but still a nice way of doing things :D My way would involve casting the GetKeyUp(); and GetKeyDown(); like the GetKeyState(); :)

I built mine to be like GetKeyState. To me state would be pressed, released, or neither, hence -1, 0, and 1. And with this method it works exactly like GetKeyState, except it has a state for key up (all you have to do is call it when you need it). But I guess that is a matter of opinion.

Game A Gogo wrote:Urg, for some reason I can't get malloc and writting memory to it quite right, so I give up!
I think i'm starting to preffer Visual Studio C++ over GE C, in VSC++, you get a lot more flexibilty. To anyone interested, here's the code I came up so far. But don't use it, it's broken and probably really badly written:

One thing that might be going wrong is that the memory returned by GetKeyState is over 400 characters long. For instance the KEY_RIGHT definition resolves to 275. Anyway, I'm surprised you are just starting to prefer other editors. gE's editor and its version of C in general is pretty restrictive. You have to worry about if things are implemented or not, and you don't get printf (among other things).
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: if(keyUp[KEY_3] == 1) ??

Postby Game A Gogo » Thu May 31, 2012 10:04 pm

well you have to realized this is the first time I use GE in like a year :P maybe I'll try making it work with 512 chars

Edit: Nope, ge doesn't like allocating 512 bytes it seems S:
For some reason GE handles arrays differently than it should... It can't convert a char* (Which can point to an array) into an array of char, there's no difference in memory between char* and char[] S: I can't use what I learned in college in ge lol
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: if(keyUp[KEY_3] == 1) ??

Postby bat78 » Sat Jun 02, 2012 8:05 pm

Game A Gogo wrote:Urg, for some reason I can't get malloc and writting memory to it quite right, so I give up!
I think i'm starting to preffer Visual Studio C++ over GE C, in VSC++, you get a lot more flexibilty. To anyone interested, here's the code I came up so far. But don't use it, it's broken and probably really badly written:
Code: Select all
char* KeyBuffer=malloc(sizeof(char)*256);
char* LastKeyState=malloc(sizeof(char)*256);

//Call this function every frame in the view actor
//    by using View->Draw Event
void GetKeyUpdate()
{
    int i;
    if(KeyBuffer!=NULL)for(i=0;i<167;i++)LastKeyState[i]=KeyBuffer[i];
    KeyBuffer=GetKeyState();
}

//Call this function EVERYTIME you quit!
//That means DO NOT use GE's press esc to quit!
void ClearKeyMemory()
{
    free(KeyBuffer);
    KeyBuffer=NULL;
    free(LastKeyState);
    LastKeyState=NULL;
}

//In case you ever needed to retreive the last key state with a function
char* GetLastKeyState()
{
    return LastKeyState;
}

//Use like the GetKeyState() function, but will return true for
//     keys for 1 frame if the key just got unpressed
char* GetKeyUp()
{
    int i;
    char* ReturnKey=GetKeyState();
    char* KeyStates=GetKeyState();
    if(LastKeyState!=NULL)
    {
        for(i=0;i<167;i++)ReturnKey[i]=(KeyStates[i]==0 && LastKeyState[i]==1);
        return ReturnKey;
    }
    else return NULL;
}

//Use like the GetKeyState() function, but will return true for
//    keys for 1 frame that just got pressed
char* GetKeyDown()
{
    int i;
    char* ReturnKey=GetKeyState();
    char* KeyStates=GetKeyState();
    if(LastKeyState!=NULL)
    {
        for(i=0;i<167;i++)ReturnKey[i]=(KeyStates[i]==1 && LastKeyState[i]==0);
        return ReturnKey;
    }
    else return NULL;
}


lolz the code is too long and i see a few bugs in continue pressing so i edit them little..
It is good at own code but is useless when u can use other method with simple codes..
The future of "Game-Editor" here
User avatar
bat78
 
Posts: 816
Joined: Sun Dec 14, 2008 9:13 pm
Location: Bulgaria, Sofia
Score: 88 Give a positive score

Re: if(keyUp[KEY_3] == 1) ??

Postby skydereign » Sat Jun 02, 2012 11:28 pm

Game A Gogo wrote:well you have to realized this is the first time I use GE in like a year :P maybe I'll try making it work with 512 chars

Edit: Nope, ge doesn't like allocating 512 bytes it seems S:
For some reason GE handles arrays differently than it should... It can't convert a char* (Which can point to an array) into an array of char, there's no difference in memory between char* and char[] S: I can't use what I learned in college in ge lol

Um... can you post a specific bit of code that you can't do this? While gE C lacks quite a bit of real C, that is something that still should work (I've never had any problems with handling pointers as arrays). It's not like makslane deliberately took away that functionality (that'd be a serious amount of work to just mess it up).
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Previous

Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest