Skip to content

Commit

Permalink
ep5 mouse input
Browse files Browse the repository at this point in the history
  • Loading branch information
danielblagy committed Mar 27, 2022
1 parent 0356daf commit 248977c
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 0 deletions.
17 changes: 17 additions & 0 deletions dewcin_yt/src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ namespace dewcin
Input::processKeyboardInput(VKCode, wasDown, isDown);
}break;

case WM_LBUTTONDOWN:
case WM_LBUTTONUP:
case WM_RBUTTONDOWN:
case WM_RBUTTONUP:
case WM_MBUTTONDOWN:
case WM_MBUTTONUP:
case WM_XBUTTONDOWN:
case WM_XBUTTONUP:
{
Input::processMouseInput(wParam, lParam);
} break;

case WM_MOUSEMOVE:
{
Input::updateMousePosition(lParam);
} break;

case WM_PAINT:
{
OutputDebugString(L"window paint\n");
Expand Down
46 changes: 46 additions & 0 deletions dewcin_yt/src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace dewcin
{
Input::KeyboardInputMap Input::keyboard;
Input::MouseInputMap Input::mouse;

Input::KeyState Input::getKeyState(uint32_t keycode)
{
Expand Down Expand Up @@ -123,4 +124,49 @@ namespace dewcin
}
}
}


Input::Position Input::getMousePosition()
{
return mouse.position;
}

bool Input::isMouseButtonPressed(unsigned int buttonCode)
{
return mouse.buttons[buttonCode].isDown;
}

bool Input::isMouseButtonReleased(unsigned int buttonCode)
{
return !mouse.buttons[buttonCode].isDown;
}

// returns true if the mouse button has just been pressed
bool Input::wasMouseButtonHit(unsigned int buttonCode)
{
return (!mouse.buttons[buttonCode].wasDown) && mouse.buttons[buttonCode].isDown;
}

void Input::processMouseInput(WPARAM wParam, LPARAM lParam)
{
mouse.buttons[DC_MOUSE_LEFT].wasDown = mouse.buttons[DC_MOUSE_LEFT].isDown;
mouse.buttons[DC_MOUSE_RIGHT].wasDown = mouse.buttons[DC_MOUSE_RIGHT].isDown;
mouse.buttons[DC_MOUSE_MIDDLE].wasDown = mouse.buttons[DC_MOUSE_MIDDLE].isDown;
mouse.buttons[DC_MOUSE_X1].wasDown = mouse.buttons[DC_MOUSE_X1].isDown;
mouse.buttons[DC_MOUSE_X2].wasDown = mouse.buttons[DC_MOUSE_X2].isDown;

mouse.buttons[DC_MOUSE_LEFT].isDown = wParam & MK_LBUTTON;
mouse.buttons[DC_MOUSE_RIGHT].isDown = wParam & MK_RBUTTON;
mouse.buttons[DC_MOUSE_MIDDLE].isDown = wParam & MK_MBUTTON;
mouse.buttons[DC_MOUSE_X1].isDown = wParam & MK_XBUTTON1;
mouse.buttons[DC_MOUSE_X2].isDown = wParam & MK_XBUTTON2;

updateMousePosition(lParam);
}

void Input::updateMousePosition(LPARAM lParam)
{
mouse.position.x = GET_X_LPARAM(lParam);
mouse.position.y = GET_Y_LPARAM(lParam);
}
}
40 changes: 40 additions & 0 deletions dewcin_yt/src/input.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <windows.h>
#include <windowsx.h>

#include <stdint.h>

Expand Down Expand Up @@ -62,6 +63,14 @@
#define DC_BACKSPACE 50
#define DC_TILDE 51

#define DC_MAX_MOUSE_BUTTONS 5

#define DC_MOUSE_LEFT 0
#define DC_MOUSE_RIGHT 1
#define DC_MOUSE_MIDDLE 2
#define DC_MOUSE_X1 3
#define DC_MOUSE_X2 4

namespace dewcin
{
class Input
Expand All @@ -84,8 +93,25 @@ namespace dewcin
KeyState keys[DC_MAX_KEYS];
};

struct ButtonState
{
bool wasDown, isDown;
};

struct Position
{
int x, y;
};

struct MouseInputMap
{
ButtonState buttons[DC_MAX_MOUSE_BUTTONS];
Position position;
};

private:
static KeyboardInputMap keyboard;
static MouseInputMap mouse;

public:
static KeyState getKeyState(uint32_t keycode);
Expand All @@ -96,8 +122,22 @@ namespace dewcin

// returns true if the key has just been pressed
static bool wasKeyHit(uint32_t keycode);


static Position getMousePosition();

static bool isMouseButtonPressed(unsigned int buttonCode);

static bool isMouseButtonReleased(unsigned int buttonCode);

// returns true if the mouse button has just been pressed
static bool wasMouseButtonHit(unsigned int buttonCode);

private:
static void processKeyboardInput(uint32_t VKCode, bool wasDown, bool isDown);

static void processMouseInput(WPARAM wParam, LPARAM lParam);

static void updateMousePosition(LPARAM lParam);
};
}
7 changes: 7 additions & 0 deletions dewcin_yt/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ dewcin_app_entry_point
x -= 10;
else if (dewcin::Input::isKeyPressed(DC_RIGHT))
x += 10;

if (dewcin::Input::isMouseButtonPressed(DC_MOUSE_MIDDLE))
OutputDebugString(L"mouse middle button pressed!\n");

dewcin::Input::Position mousePosition = dewcin::Input::getMousePosition();
swprintf(charBuffer, 256, L"%d, %d\n", mousePosition.x, mousePosition.y);
OutputDebugString(charBuffer);
}
);

Expand Down

0 comments on commit 248977c

Please sign in to comment.