Page 1 of 1

OO, how do you guys handle things like controls?

Posted: Fri Jan 02, 2009 7:08 pm
by JS Lemming
Object orientation. Err, say you have a player class called hector and you need to control hector with the keyboard for your game to be... playable. How is this done nicely? My hector class is initiated deep within other classes. My controller class is initiated in the main. So far the only way I've thought of to give, say hector, access to the controller class is to send a pointer to the controller class through the chain of class initiations as arguments. This seems ridiculous, dirty, and unmanageable.

I was browsing my controller code when I noticed the SDL_PollEvent(SDL_Event *event) function. Now that's promising. It works liek soo:

Code: Select all

	SDL_Event event;
	if( SDL_PollEvent(&event) )
	{
		switch( event.type )
		{
...
And I could put that anywhere. Anyone know how this is done? How I could call something like getControllerClassPointer() and have it return the pointer to the initiated class?

Re: OO, how do you guys handle things like controls?

Posted: Fri Jan 02, 2009 8:38 pm
by M_D_K
you could use SDL_PollEvents for controls.

lets see...(haven't used it in awhile)

Code: Select all

SDL_PollEvents(&event);
switch(event.type)
{
    case SDL_KEYDOWN://I think
        if(event.key.keysym.sym == SDLK_LEFT)
            m_position.x += 5;//whatever assuming position is a vector
        else if(event.key.keysym.sym == SDLK_RIGHT)
            m_position.x -= 5;
    break;
};
Thats one reason I hate doing it like that...
I take a dump of the whole keyboard every frame and check through an input class thats also a singleton.
like this

Code: Select all

	SDL_PumpEvents();

	memcpy(keyboard.oldKeys, keyboard.keys,sizeof(unsigned char) * keycount);
	unsigned char *tempKeys = SDL_GetKeyState(&keycount);
	memcpy(keyboard.keys, tempKeys,sizeof(unsigned char) * keycount);
then when I want to check a key:

Code: Select all

if(Input::GetSingleton().keyDown(SDLK_RIGHT) == true)
or

Code: Select all

if(Input::GetSingleton().keyStillDown(SDLK_RIGHT) == true)

Re: OO, how do you guys handle things like controls?

Posted: Fri Jan 02, 2009 8:48 pm
by Arce
I'm a little busy atm and thus cannot give you a full reply...I'm familiar with sdl and recalling having handeled input in a number of different manners. I'll share some when I get some time, haha.

Anyway, what jumped out was this:
And I could put that anywhere. Anyone know how this is done? How I could call something like getControllerClassPointer() and have it return the pointer to the initiated class?
It seems alot like you're describing a *dun dun dun* singleton! xD

I'm not exactly sure what you're asking here--I can interpret it two different ways. One way would imply that you're trying to pass objects to a controller class, in which case you'd probably want some kind of overloaded function that takes objects pointers as a parameter, then create an accessor in all of said objects that looks so:

Code: Select all

class Hector {
     [ . . . ]
     public:
          Hector *get(void) { return this; }
     [ . . . ]
}
Then, when you're browsing through all of your hectors via list or whatever, you can just call movementClass.shoudMove(hector_object.get());

Honestly, that looks like it'd get messy as hell, as you'd then have to force the controller to act upon every object that input should effect...So chances are good you meant this:

Create an object to represent a keyboard class. Keep an array of booleans to represent the keyboard within that class or poll events, whichever method you prefer...And have it where you can retrieve a pointer to this one said class anywhere else in your application. This, I'd think, would be the easiest method, as you can do:

Code: Select all

InputClass *input =InputClass::getInstance();
if(input->key[A] ) {
      x++
}

Also, as marauder brought to my attention, you could rewrite the above code as

Code: Select all

if((InputClass::getInstance())->key[A] ) {
      x++
}
As you can see, you can globally access a singleton, though it's not actually global. Incase you haven't used them before, it's simply an object that you can only have one instance of at a time, and have a static accessor method "getInstance" that you can access anywhere by using the scope operator(::) like other static members.

There's a huge, bitchy debate over weather the use of these are evil or not, so if you don't want to use because of 'bad OO design'...Your loss. ;p

Alternatively, I guess you could make the input class friends with everything else, but that'd be pretty gay...o.o;

Hope I helped, haha. Spend more time here than I planned, I gotta head out. I'll check back later. xD

Re: OO, how do you guys handle things like controls?

Posted: Fri Jan 02, 2009 8:58 pm
by M_D_K
Arce wrote: Alternatively, I guess you could make the input class friends with everything else, but that'd be pretty gay...o.o;
Then input would be the equivalent of a MySpace whore :)

Re: OO, how do you guys handle things like controls?

Posted: Fri Jan 02, 2009 9:37 pm
by JS Lemming
haha. Sounds like a singleton is exactly what I need. It'll be my first time using one. So far my only complaint is the name. Reminds me of Carlton from Fresh Prince. I thank my peeps with a respectful bow. :bow:

Re: OO, how do you guys handle things like controls?

Posted: Fri Jan 02, 2009 10:26 pm
by Arce
M_D_K wrote:
Arce wrote: Alternatively, I guess you could make the input class friends with everything else, but that'd be pretty gay...o.o;
Then input would be the equivalent of a MySpace whore :)
:lol: :lol: :lol:

I busted an irl cackle at that one! XD

Re: OO, how do you guys handle things like controls?

Posted: Fri Jan 02, 2009 11:00 pm
by JS Lemming
Singletons officially rock. My code just got 50% cleaner.

Re: OO, how do you guys handle things like controls?

Posted: Fri Jan 02, 2009 11:03 pm
by Arce
Haha! Not it XxStillaVirgin69xX has anything to say about it...

"Omg, is that a singleton? My professor says those are bad..."

xD

I will confess, however, that marauder did make some valid points regarding the use of singletons in another thread somewhere. I personally couldn't imagine developing a game without them--however, I would try to use them sparingly if I was working on a highly object oriented, very large piece of software...

edit: I remember the confused look my programming teacher gave me when I turned this in:

Code: Select all

int main() {
    srand((unsigned)time(0));
 
    //main func init stuff
    Singleton *master;
    master = Singleton::getInstance();
    
    master->drawTitleScreen();
    master->showRules() ;
    //main loop
    while(master->running()) {
         //Start a new game if not already started
         if(!master->gameStarted()) 
              initGame();
         
        //Go, assbritches.
         master->contestant[master->current].playTurn();

         //self explanatory, rendering this comment useless...
         master->drawGame();
    }
    //thus end my platform independence...Oh, wait, it's been gone since well before this time. 
    system("pause");
    
    return 0;
}
Apparently she wasn't well acquainted with singletons, wasn't used to having project split into multiple files, and has never seen such a clean main loop to run a full wheel of fortune. xD

Re: OO, how do you guys handle things like controls?

Posted: Sat Jan 03, 2009 2:49 am
by trufun202
I usually just pass in a pointer to the entire inputState into each object. So each object can handle the input as they wish.

No idea if this is the best way to do things, but it works for me:

Code: Select all

Update()
{
    playerObj->handleInput(inputState);
}

Re: OO, how do you guys handle things like controls?

Posted: Sat Jan 03, 2009 12:04 pm
by Arce
Okay, Mr. Beautiful OO design, I see how you're too good for singletons. :lol:

Re: OO, how do you guys handle things like controls?

Posted: Sat Jan 03, 2009 12:09 pm
by Falco Girgis
Arce wrote:Okay, Mr. Beautiful OO design, I see how you're too good for singletons. :lol:
trufun actually isn't an OO Nazi.