Skip to content

Instantly share code, notes, and snippets.

@oiotoxt
Created April 21, 2021 10:01
Show Gist options
  • Select an option

  • Save oiotoxt/b5ab2f2320ee6360f350bd4963c3f003 to your computer and use it in GitHub Desktop.

Select an option

Save oiotoxt/b5ab2f2320ee6360f350bd4963c3f003 to your computer and use it in GitHub Desktop.
C++ Lecture
// DpInputState.h
////////////////////////////////////////////////////////////////////////////////
#ifndef DPINPUTSTATE_H
#define DPINPUTSTATE_H
#include "DpBase.h"
_DP_BGN
//******************************************************************************
//
//------------------------------------------------------------------------------
struct eMoveKey
{
enum Enum : Mx::U8
{
kL = 1 << 0,
kR = 1 << 1,
kT = 1 << 2,
kB = 1 << 3,
};
};
//******************************************************************************
//
//------------------------------------------------------------------------------
class InputState
{
public:
static const InputState NULL_STATE;
Mx::U8 moveKey;
InputState() { Reset(); }
InputState(const InputState& rhs)
{
*this = rhs;
}
InputState(bool L, bool R, bool T, bool B)
{
Set(L, R, T, B);
}
void Reset()
{
moveKey = 0;
}
bool IsNull()
{
return (*this == NULL_STATE);
}
InputState& operator = (const InputState& rhs)
{
moveKey = rhs.moveKey;
return *this;
}
bool operator == (const InputState& rhs) const
{
return (moveKey == rhs.moveKey);
}
bool operator != (const InputState& rhs) const
{
return (moveKey != rhs.moveKey);
}
void Flag(Mx::U8 bits)
{
moveKey |= bits;
}
void Unflag(Mx::U8 bits)
{
moveKey &= ~bits;
}
bool IsFlagged(Mx::U8 bits)
{
return ((moveKey & bits) == bits);
}
bool GetL() const { return moveKey & eMoveKey::Enum::kL; }
bool GetR() const { return moveKey & eMoveKey::Enum::kR; }
bool GetT() const { return moveKey & eMoveKey::Enum::kT; }
bool GetB() const { return moveKey & eMoveKey::Enum::kB; }
void SetL(bool yes) { yes ? Flag(eMoveKey::Enum::kL) : Unflag(eMoveKey::Enum::kL); }
void SetR(bool yes) { yes ? Flag(eMoveKey::Enum::kR) : Unflag(eMoveKey::Enum::kR); }
void SetT(bool yes) { yes ? Flag(eMoveKey::Enum::kT) : Unflag(eMoveKey::Enum::kT); }
void SetB(bool yes) { yes ? Flag(eMoveKey::Enum::kB) : Unflag(eMoveKey::Enum::kB); }
void Set(bool L, bool R, bool T, bool B)
{
moveKey = 0;
if (L) moveKey |= eMoveKey::Enum::kL;
if (R) moveKey |= eMoveKey::Enum::kR;
if (T) moveKey |= eMoveKey::Enum::kT;
if (B) moveKey |= eMoveKey::Enum::kB;
}
};
_DP_END
#endif // DPINPUTSTATE_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment