Skip to content

Commit aff6581

Browse files
committed
Basic mouse stuffs
1 parent 59edad4 commit aff6581

2 files changed

Lines changed: 71 additions & 5 deletions

File tree

‎Chapter08/InputSystem.cpp‎

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,55 @@ bool KeyboardState::GetKeyValue(int keyCode) const
1717

1818
ButtonState KeyboardState::GetKeyState(int keyCode) const
1919
{
20-
if (mCurrState[keyCode] == 0)
20+
if (mPrevState[keyCode] == 0)
2121
{
22-
if (mPrevState[keyCode] == 0)
22+
if (mCurrState[keyCode] == 0)
2323
{
2424
return ENone;
2525
}
2626
else
27+
{
28+
return EPressed;
29+
}
30+
}
31+
else // Prev state must be 1
32+
{
33+
if (mCurrState[keyCode] == 0)
2734
{
2835
return EReleased;
2936
}
37+
else
38+
{
39+
return EHeld;
40+
}
3041
}
31-
else // must be 1
42+
}
43+
44+
bool MouseState::GetButtonValue(int button) const
45+
{
46+
return (SDL_BUTTON(button) & mCurrButtons) == 1;
47+
}
48+
49+
ButtonState MouseState::GetButtonState(int button) const
50+
{
51+
int mask = SDL_BUTTON(button);
52+
if ((mask & mPrevButtons) == 0)
3253
{
33-
if (mPrevState[keyCode] == 0)
54+
if ((mask & mCurrButtons) == 0)
55+
{
56+
return ENone;
57+
}
58+
else
3459
{
3560
return EPressed;
3661
}
62+
}
63+
else
64+
{
65+
if ((mask & mCurrButtons) == 0)
66+
{
67+
return EReleased;
68+
}
3769
else
3870
{
3971
return EHeld;
@@ -43,12 +75,16 @@ ButtonState KeyboardState::GetKeyState(int keyCode) const
4375

4476
bool InputSystem::Initialize()
4577
{
78+
// Keyboard
4679
// Assign current state pointer
4780
mState.Keyboard.mCurrState = SDL_GetKeyboardState(NULL);
4881
// Clear previous state memory
4982
memset(mState.Keyboard.mPrevState, 0,
5083
SDL_NUM_SCANCODES);
5184

85+
// Mouse (just set everything to 0)
86+
mState.Mouse.mCurrButtons = 0;
87+
mState.Mouse.mPrevButtons = 0;
5288
return true;
5389
}
5490

@@ -63,8 +99,16 @@ void InputSystem::PrepareForUpdate()
6399
memcpy(mState.Keyboard.mPrevState,
64100
mState.Keyboard.mCurrState,
65101
SDL_NUM_SCANCODES);
102+
103+
// Mouse
104+
mState.Mouse.mPrevButtons = mState.Mouse.mCurrButtons;
66105
}
67106

68107
void InputSystem::Update()
69108
{
109+
// Mouse
110+
int x = 0, y = 0;
111+
mState.Mouse.mCurrButtons = SDL_GetMouseState(&x, &y);
112+
mState.Mouse.mMousePos.x = static_cast<float>(x);
113+
mState.Mouse.mMousePos.y = static_cast<float>(y);
70114
}

‎Chapter08/InputSystem.h‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#pragma once
1010
#include <SDL/SDL_scancode.h>
11+
#include "Math.h"
1112

1213
// The different button states
1314
enum ButtonState
@@ -18,7 +19,7 @@ enum ButtonState
1819
EHeld
1920
};
2021

21-
// Helper for Keyboard input
22+
// Helper for keyboard input
2223
class KeyboardState
2324
{
2425
public:
@@ -33,10 +34,31 @@ class KeyboardState
3334
Uint8 mPrevState[SDL_NUM_SCANCODES];
3435
};
3536

37+
// Helper for mouse input
38+
class MouseState
39+
{
40+
public:
41+
friend class InputSystem;
42+
43+
// For mouse position
44+
const Vector2& GetPosition() const { return mMousePos; }
45+
46+
// For buttons
47+
bool GetButtonValue(int button) const;
48+
ButtonState GetButtonState(int button) const;
49+
private:
50+
// Store current mouse position
51+
Vector2 mMousePos;
52+
// Store button data
53+
Uint32 mCurrButtons;
54+
Uint32 mPrevButtons;
55+
};
56+
3657
// Wrapper that contains current state of input
3758
struct InputState
3859
{
3960
KeyboardState Keyboard;
61+
MouseState Mouse;
4062
};
4163

4264
class InputSystem

0 commit comments

Comments
 (0)