Cleaned Up AppPlatform & Input Classes

* Cleaned up AppPlatform classes
* Documented & improved Keyboard & Mouse classes
* Improved input-handling code in the Windows & SDL main.cpp files
This commit is contained in:
Brent Da Mage
2023-08-19 06:26:50 -05:00
committed by iProgramInCpp
parent e731fefbea
commit d5ee7bfc08
21 changed files with 422 additions and 375 deletions

View File

@@ -130,6 +130,7 @@ static const char* g_panoramaList[] =
};
static float g_panoramaAngle = 0.0f;
// TODO: This should be inside of an initialized "Minecraft" instance rather than the global namespace
bool g_bIsMenuBackgroundAvailable = false;
void Screen::renderMenuBackground(float f)
@@ -328,9 +329,9 @@ void Screen::mouseEvent()
if (pAction->isButton())
{
if (Mouse::getEventButtonState())
mouseClicked (m_width * pAction->field_8 / Minecraft::width, m_height * pAction->field_C / Minecraft::height - 1, Mouse::getEventButton());
mouseClicked (m_width * pAction->_posX / Minecraft::width, m_height * pAction->_posY / Minecraft::height - 1, Mouse::getEventButton());
else
mouseReleased(m_width * pAction->field_8 / Minecraft::width, m_height * pAction->field_C / Minecraft::height - 1, Mouse::getEventButton());
mouseReleased(m_width * pAction->_posX / Minecraft::width, m_height * pAction->_posY / Minecraft::height - 1, Mouse::getEventButton());
}
}

View File

@@ -43,7 +43,7 @@ void SelectWorldScreen::init()
m_buttons.push_back(&m_btnBack);
m_buttons.push_back(&m_btnDelete);
field_12C = Mouse::getButtonState(1) == 0;
field_12C = Mouse::getButtonState(Mouse::ButtonType::NONE) == Mouse::ButtonState::UP;
m_buttonTabList.push_back(&m_btnUnknown);
m_buttonTabList.push_back(&m_btnDelete);
@@ -171,7 +171,7 @@ void SelectWorldScreen::render(int mouseX, int mouseY, float f)
else
{
m_pWorldSelectionList->render(0, 0, f);
field_12C = Mouse::getButtonState(1) == 0;
field_12C = Mouse::getButtonState(Mouse::ButtonType::LEFT) == Mouse::ButtonState::UP;
}
Screen::render(mouseX, mouseY, f);

View File

@@ -12,9 +12,9 @@
std::vector<KeyboardAction> Keyboard::_inputs;
int Keyboard::_index = -1;
int Keyboard::_states[KEYBOARD_STATES_SIZE];
Keyboard::KeyState Keyboard::_states[KEYBOARD_STATES_SIZE];
void Keyboard::feed(int down, int key)
void Keyboard::feed(KeyState state, int key)
{
#ifndef ORIGINAL_CODE
// Prevent Crashes
@@ -23,9 +23,9 @@ void Keyboard::feed(int down, int key)
}
#endif
_inputs.push_back(KeyboardAction(key, down));
_inputs.push_back(KeyboardAction(key, state));
_states[key] = down;
_states[key] = state;
}
bool Keyboard::next()
@@ -39,12 +39,12 @@ bool Keyboard::next()
int Keyboard::getEventKey()
{
return _inputs[_index].field_4;
return _inputs[_index]._keyCode;
}
int Keyboard::getEventKeyState()
{
return _inputs[_index].field_0;
return _inputs[_index]._keyState;
}
bool Keyboard::isKeyDown(int keyCode)
@@ -52,7 +52,7 @@ bool Keyboard::isKeyDown(int keyCode)
if (keyCode < 0 || keyCode >= KEYBOARD_STATES_SIZE)
return false;
return _states[keyCode] == 1;
return _states[keyCode] == KeyState::DOWN;
}
void Keyboard::reset()

View File

@@ -16,22 +16,18 @@
#define KEYBOARD_STATES_SIZE 256
struct KeyboardAction
{
int field_0;
uint8_t field_4;
KeyboardAction(uint8_t key, int state)
{
field_0 = state;
field_4 = key;
}
};
struct KeyboardAction;
class Keyboard
{
public:
static void feed(int down, int key);
enum KeyState : bool
{
UP,
DOWN
};
static void feed(KeyState state, int key);
static bool next();
static int getEventKey();
static int getEventKeyState();
@@ -40,7 +36,18 @@ public:
private:
static std::vector<KeyboardAction> _inputs;
static int _states[KEYBOARD_STATES_SIZE];
static Keyboard::KeyState _states[KEYBOARD_STATES_SIZE];
static int _index;
};
struct KeyboardAction
{
int _keyState;
uint8_t _keyCode;
KeyboardAction(uint8_t keyCode, Keyboard::KeyState keyState)
{
_keyState = keyState;
_keyCode = keyCode;
}
};

View File

@@ -10,22 +10,25 @@
std::vector<MouseAction> Mouse::_inputs;
int Mouse::_index, Mouse::_x, Mouse::_y;
int Mouse::_xOld, Mouse::_yOld, Mouse::_buttonStates[3];
int Mouse::_xOld, Mouse::_yOld;
Mouse::ButtonState Mouse::_buttonStates[MOUSE_STATES_SIZE];
void Mouse::feed(int x1, int x2, int x3, int x4)
void Mouse::feed(ButtonType buttonType, ButtonState buttonState, int posX, int posY)
{
_inputs.push_back(MouseAction(x1, x2, x3, x4));
_inputs.push_back(MouseAction(buttonType, buttonState, posX, posY));
if (x1 >= 3)
return;
// Make sure button type is valid
if (buttonType <= ButtonType::_MAX)
{
// Check if we're processing a button-state update
if (buttonType != ButtonType::NONE)
_buttonStates[buttonType] = buttonState;
if (x1 != 0)
Mouse::_buttonStates[x1] = x2;
_xOld = _x;
_yOld = _y;
_x = x3;
_y = x4;
_xOld = _x;
_yOld = _y;
_x = posX;
_y = posY;
}
}
short Mouse::getX()
@@ -40,16 +43,16 @@ short Mouse::getY()
bool Mouse::next()
{
if (_index + 1 >= int(_inputs.size()))
if (_index + 1 >= _inputs.size())
return false;
_index++;
return true;
}
int Mouse::getEventButton()
Mouse::ButtonType Mouse::getEventButton()
{
return _inputs[_index].field_0;
return _inputs[_index]._buttonType;
}
bool Mouse::isButtonDown(int btn)
@@ -68,10 +71,10 @@ MouseAction* Mouse::getEvent()
return &_inputs[_index];
}
int Mouse::getButtonState(int btn)
Mouse::ButtonState Mouse::getButtonState(ButtonType btn)
{
if (btn <= 0 || btn >= 3)
return 0;
if (btn <= ButtonType::_MIN || btn > ButtonType::_MAX)
return ButtonState::UP;
return _buttonStates[btn];
}
@@ -92,7 +95,7 @@ void Mouse::reset2()
_yOld = _y;
}
int Mouse::getEventButtonState()
Mouse::ButtonState Mouse::getEventButtonState()
{
return _inputs[_index].field_4;
return _inputs[_index]._buttonState;
}

View File

@@ -10,46 +10,39 @@
#include <vector>
struct MouseAction
{
int field_0;
int field_4;
int field_8;
int field_C;
#define MOUSE_STATES_SIZE (Mouse::ButtonState::MAX + 1)
MouseAction()
{
field_0 = 0;
field_4 = 0;
field_8 = 0;
field_C = 0;
}
MouseAction(int x1, int x2, int x3, int x4)
{
field_0 = x1;
field_4 = x2;
field_8 = x3;
field_C = x4;
}
bool isButton()
{
return field_0 == 1 || field_0 == 2;
}
};
struct MouseAction;
class Mouse
{
public:
static void feed(int, int, int, int);
enum ButtonType
{
NONE,
LEFT,
RIGHT,
MIDDLE,
_MIN = LEFT,
_MAX = MIDDLE
};
enum ButtonState : bool
{
UP,
DOWN,
MAX = DOWN // God yes, I love C++. It doesn't let me name this _MAX despite it being in a DIFFERENT ENUM ENTIRELY...
};
static void feed(ButtonType buttonType, ButtonState buttonState, int posX, int posY);
static short getX();
static short getY();
static bool next();
static bool isButtonDown(int btn);
static int getButtonState(int btn);
static int getEventButton();
static int getEventButtonState();
static ButtonState getButtonState(ButtonType btn);
static ButtonType getEventButton();
static ButtonState getEventButtonState();
static MouseAction* getEvent();
static void setX(int x);
static void setY(int y);
@@ -63,6 +56,36 @@ private:
static int _index;
static int _x, _y;
static int _xOld, _yOld;
static int _buttonStates[3];
static ButtonState _buttonStates[MOUSE_STATES_SIZE];
};
struct MouseAction
{
Mouse::ButtonType _buttonType;
Mouse::ButtonState _buttonState;
int _posX;
int _posY;
MouseAction()
{
_buttonType = Mouse::ButtonType::NONE;
_buttonState = Mouse::ButtonState::DOWN;
_posX = 0;
_posY = 0;
}
MouseAction(Mouse::ButtonType buttonType, Mouse::ButtonState buttonState, int posX, int posY)
{
_buttonType = buttonType;
_buttonState = buttonState;
_posX = posX;
_posY = posY;
}
bool isButton()
{
return _buttonType == Mouse::ButtonType::LEFT ||
_buttonType == Mouse::ButtonType::RIGHT ||
_buttonType == Mouse::ButtonType::MIDDLE;
}
};

View File

@@ -1244,6 +1244,7 @@ void LevelRenderer::renderSky(float f)
glDepthMask(true);
}
// TODO: This should be inside of an initialized "Minecraft" instance rather than the global namespace
bool g_bAreCloudsAvailable = false; // false because 0.1 didn't have them
void LevelRenderer::renderClouds(float f)