OO, how do you guys handle things like controls?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

OO, how do you guys handle things like controls?

Post 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?
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

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

Post 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)
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

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

Post 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
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

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

Post 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 :)
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

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

Post 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:
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

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

Post 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
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

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

Post by JS Lemming »

Singletons officially rock. My code just got 50% cleaner.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

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

Post 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
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
User avatar
trufun202
Game Developer
Game Developer
Posts: 1105
Joined: Sun Sep 21, 2008 12:27 am
Location: Dallas, TX
Contact:

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

Post 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);
}
-Chris

YouTube | Twitter | Rad Raygun

“REAL ARTISTS SHIP” - Steve Jobs
User avatar
Arce
Jealous Self-Righteous Prick
Jealous Self-Righteous Prick
Posts: 2153
Joined: Mon Jul 10, 2006 9:29 pm

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

Post by Arce »

Okay, Mr. Beautiful OO design, I see how you're too good for singletons. :lol:
<qpHalcy0n> decided to paint the office, now i'm high and my hands hurt
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

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

Post 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.
Post Reply