What's new

C/C++ ButtonMonitor

X

XeClutch

Contingency Dev
Messages
3,687
Reaction score
2,452
This is something I made when I first started learning how to use classes in C++. This is obviously for Xbox.

ButtonMonitor.h
C++:
#pragma once

#include "stdafx.h"
#include <xinputdefs.h>

class ButtonMonitor
{
public:
    // Variables
    XINPUT_STATE ctrl;
    bool DPAD_UP;
    bool DPAD_DOWN;
    bool DPAD_LEFT;
    bool DPAD_RIGHT;
    bool START;
    bool BACK;
    bool THUMBSTICK_LEFT;
    bool THUMBSTICK_RIGHT;
    bool BUMPER_LEFT;
    bool BUMPER_RIGHT;
    bool MAIN_A;
    bool MAIN_B;
    bool MAIN_X;
    bool MAIN_Y;

    // Methods
    void ButtonMonitor::Refresh(DWORD Controller = 0)
    {
        if (!XInputGetState(Controller, &ctrl))
        {
            DPAD_UP = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP) > 0);
            DPAD_DOWN = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN) > 0);
            DPAD_LEFT = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) > 0);
            DPAD_RIGHT = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) > 0);
            START = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_START) > 0);
            BACK = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) > 0);
            THUMBSTICK_LEFT = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB) > 0);
            THUMBSTICK_RIGHT = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) > 0);
            BUMPER_LEFT = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) > 0);
            BUMPER_RIGHT = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) > 0);
            MAIN_A = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_A) > 0);
            MAIN_B = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_B) > 0);
            MAIN_X = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_X) > 0);
            MAIN_Y = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_Y) > 0);
        }
    }
};
 
Last edited:
A

AbeHD

6d 79 20 6e 61 6d 65 20 69 73 20 4a 61 63 6b 2e
Messages
679
Reaction score
156
This is something I made when I first started learning how to use classes in C++. This is obviously for Xbox.

ButtonMonitor.h
C++:
#pragma once

#ifndef _BUTTONMONITOR
#define _BUTTONMONITOR

#include "stdafx.h"
#include <xinputdefs.h>

class ButtonMonitor
{
public:
    // Variables
    XINPUT_STATE ctrl;
    bool DPAD_UP;
    bool DPAD_DOWN;
    bool DPAD_LEFT;
    bool DPAD_RIGHT;
    bool START;
    bool BACK;
    bool THUMBSTICK_LEFT;
    bool THUMBSTICK_RIGHT;
    bool BUMPER_LEFT;
    bool BUMPER_RIGHT;
    bool MAIN_A;
    bool MAIN_B;
    bool MAIN_X;
    bool MAIN_Y;

    // Methods
    VOID ButtonMonitor::Refresh(DWORD Controller = 0)
    {
        if (!XInputGetState(Controller, &ctrl))
        {
            DPAD_UP = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP) > 0);
            DPAD_DOWN = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN) > 0);
            DPAD_LEFT = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) > 0);
            DPAD_RIGHT = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) > 0);
            START = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_START) > 0);
            BACK = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) > 0);
            THUMBSTICK_LEFT = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB) > 0);
            THUMBSTICK_RIGHT = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) > 0);
            BUMPER_LEFT = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) > 0);
            BUMPER_RIGHT = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) > 0);
            MAIN_A = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_A) > 0);
            MAIN_B = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_B) > 0);
            MAIN_X = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_X) > 0);
            MAIN_Y = ((ctrl.Gamepad.wButtons & XINPUT_GAMEPAD_Y) > 0);
        }
    }
};

#endif
Could you make a combination button thing with this ?
 
X

XeClutch

Contingency Dev
Messages
3,687
Reaction score
2,452
Could you make a combination button thing with this ?
C++:
ButtonMonitor* Controller = new ButtonMoniter;
if (Controller->BUMPER_LEFT && Controller->BUMPER_RIGHT)
{ // do something
}
 
A

AceInfinity

Enthusiast
Messages
146
Reaction score
39
Why would you just use a collection of bool variables instead of an enum for bitflags? Also, why are you including "stdafx.h" in this header? :S
 
X

XeClutch

Contingency Dev
Messages
3,687
Reaction score
2,452
Why would you just use a collection of bool variables instead of an enum for bitflags? Also, why are you including "stdafx.h" in this header? :S
Just because. Don't see why it would be a problem. As far as the headers is concerned, I had a few "staple" headers included in stdafx.h that I included in a bunch of different code files.
 
A

AceInfinity

Enthusiast
Messages
146
Reaction score
39
Just because. Don't see why it would be a problem. As far as the headers is concerned, I had a few "staple" headers included in stdafx.h that I included in a bunch of different code files.

Bitflags would be much more efficient. For the headers, that's not the proper way to do it... None of your header files should have anything to do with stdafx.h directly.
 
Last edited:
S

Sto 2 da b

Enthusiast
Messages
273
Reaction score
62
Bitflags would be much more efficient. For the headers, that's not the proper way to do it... None of your header files should have anything to do with stdafx.h directly.

Also including any headers that aren't significant to your actual file is very sloppy and working in an actual environment dealing with code deals heavily with only using what you need considering the file sizes. I know when I was first hired I was actually asked to go through as much as I could of the old classes and see what all I could rip out. I mean I went in completely taking out a ton considering most was from .NET 2.0 and is now in the 4.5 framework so they even had a custom class for a combobox they had made.
 
Top Bottom