Mouse

Query button states

Querying button states is done using cer::isMouseButtonDown() and cer::isMouseButtonUp().

// Gets a value indicating whether a specific mouse button is currently pressed.
auto isMouseButtonDown(MouseButton button) -> bool;

// Gets a value indicating whether a specific mouse button is currently released.
auto isMouseButtonUp(MouseButton button) -> bool;
Example
auto time     = game.time();
auto dt       = float(time.elapsed);
auto movement = dt * 200.0f;

if (isMouseButtonDown(MouseButton::Left)) {
  spritePosition.y -= movement;
}

if (isMouseButtonDown(MouseButton::Right)) {
  spritePosition.y += movement;
}

Query mouse position

Obtaining the mouse cursor’s current position is done via cer::currentMousePosition().

// Gets the current mouse position within the window.
// The coordinates are relative to the window's top-left corner.
auto currentMousePosition() -> Vector2;

Sometimes, you might be interested in how much the mouse has traveled between the previous and current frame. You could keep track of the current and previous mouse position, and calculate their difference.

This functionality is built into cerlib via cer::currentMousePositionDelta().

// Gets the amount of mouse movement within the window since
// the last game tick, in pixels.
auto currentMousePositionDelta() -> Vector2;
Example
auto mousePosition = currentMousePosition();

if (mousePosition.x > 100) {
  turnRight();
}
else if (mousePosition.x < 100) {
  turnLeft();
}

Lock the mouse cursor

To lock the mouse cursor into the game’s window, call the Window::setMouseGrab() method:

auto window = game.window();

window.setMouseGrab(true);

This will ensure that the mouse always stays within the window’s area. Such a behavior is desired in games that provide some form of aiming, such as shooters.

To release the mouse cursor again, pass false to the method:

auto window = game.window();

window.setMouseGrab(false);

DPI awareness

The mouse position must be multiplied by the window’s pixel ratio, in case the window’s display is a high DPI display. This is necessary because the mouse position is expressed in logical display units, not pixels. Multiplying it with the pixel ratio converts it to pixel space, which is what cerlib’s rendering uses. If you don’t intent to support high DPI displays in your game, you don’t have to take the pixel ratio into account and can leave the multiplication out.

Example
auto window                = game.window();
auto mousePosition         = currentMousePosition();
auto mousePositionInPixels = mousePosition * window.pixelRatio();

auto obj = pickObjectAt(mousePositionInPixels);

Mouse events

Note

This assumes that you’ve read Events.

The following events are related to the keyboard:

  • MouseButtonPressEvent: Raised when a key was pressed.
  • MouseButtonReleaseEvent: Raised when a key was released.
  • MouseDoubleClickEvent: Raised when a double click was performed.
  • MouseWheelEvent: Raised when the mouse wheel was scrolled.
Example
// ...

while (game.tick()) {
  game.handleEvents(
    [](MouseButtonPressEvent e) {
      logInfo("Mouse button {} was pressed.", e.button;
    },
    [](MouseButtonReleaseEvent e) {
      logInfo("Mouse button {} was released.", e.button;
    },
    cerIgnoreEvent,
  );

  // ...
}