Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS 134 Alternate Inputs.

Similar presentations


Presentation on theme: "CS 134 Alternate Inputs."— Presentation transcript:

1 CS 134 Alternate Inputs

2 Today in Video Games

3 Send me requests for the Flexi-Lectures
Updates Send me requests for the Flexi-Lectures

4 Alternate Inputs Other input methods are similar via polling Mouse
XInput Joystick Other inputs that must be done with events Touchscreen

5 Alternate Inputs Gesture recognition is much more important with other inputs Mouse Click vs Drag Joystick Light vs Heavy press

6 Alternate Inputs Each input has a different way of getting the actual input state May require setup / cleanup code But we usually don't care about cleanup, the OS will do that for us!

7 Keyboard

8 Keyboard No setup or cleanup code C API – Polling
SDL_GetKeyboardState() Returns the state of each key on the keyboard Java API – Events (converted to Polling) KeyListener.keyPressed, .keyReleased Store current state in kbState Gestures Keep the last state of the keyboard around to know if it was just pressed or just released

9 Mouse

10 Mouse No setup or cleanup code C API – Polling SDL_GetMouseState()
SDL_GetRelativeMouseState() SDL_SetRelativeMouseMode() Java API – Events (converted to polling) MouseListener.mouseMoved, .mouseClicked, .mouseDragged, etc. Gestures Click, double click, drag

11 Mouse SDL_GetMouseState(int* x, int* y)
SDL_GetRelativeMouseState(int* x, int* y) Fill out X, Y with the pixel position / change of the mouse. Returns the current state of the mouse buttons Bitand (&) with each button to see its state SDL_BUTTON_LMASK for left SDL_BUTTON_MMASK for middle SDL_BUTTON_RMASK for right

12 Mouse // globals int mouseX, mouseY, mouseDeltaX, mouseDeltaY;
Uint32 mouseButtons; // game loop while (!shouldExit) { lastFrameMs = currentFrameMs; memcpy(kbPrevState, kbState, sizeof(kbPrevState)); SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_QUIT: shouldExit = 1; } currentFrameMs = SDL_GetTicks(); float deltaTime = (currentFrameMs - lastFrameMs) / f; mouseButtons = SDL_GetMouseState(&mouseX, &mouseY); SDL_GetRelativeMouseState(&mouseDeltaX, &mouseDeltaY); /* Rest of the game loop goes here */ SDL_GL_SwapWindow(window);

13 Mouse MouseListener.mouseMoved(MouseEvent e)
Also mouseDragged, mousePressed, etc. e.getX(), e.getY() Get X, Y of mouse when event happened. e.getButton() Get button event happened for. No way to get only relative motion.

14 Mouse // fields public static int mouseX, mouseY;
public static boolean mouseButton[] = new boolean[3]; // initializiation window.addKeyListener(new KeyListener() {...}); window.addMouseListener(new MouseListener() { private void updateMousePos(MouseEvent ev) { mouseX = ev.getX(); mouseY = ev.getY(); } private void updateMouseButtons(MouseEvent ev) { int idx = mouseEvent.getButton() - 1; if (idx >= 0 && idx < mouseButtons.length) { mouseButtons[idx] = true; public void mouseEntered(MouseEvent ev) { updateMousePos(ev); } public void mouseMoved(MouseEvent ev) { updateMousePos(ev); } public void mouseDragged(MouseEvent ev) { updateMousePos(ev); } public void mouseExited(MouseEvent ev) { updateMousePos(ev); } public void mousePressed(MouseEvent ev) { updateMouseButtons(ev); } public void mouseReleased(MouseEvent ev) { updateMouseButtons(ev); } public void mouseClicked(MouseEvent ev) {} public void mouseWheelMoved(MouseEvent ev){} });

15 Mouse SDL_SetRelativeMouseMode(bool)
If set, the mouse will disappear and freeze. Relative motion will still work Useful for cameras! For RTS: Enable when dragging starts, disable when dragging ends For FPS: Enable when in gameplay, disable leaving gameplay No corresponding method for Java.

16 Mouse Using the mouse, you may want to distinguish between different gestures: Click / Double Click / Drag All you can detect is the low level operations Mouse Button Is Down / Mouse Button Is Up Unlike the keyboard, these gestures have a time component to them

17 Mouse What is a click? What is a double click? What is a drag?

18 Mouse What is a click? A down followed by an up within .1 sec
Minimal mouse movement What is a double click? Two clicks within .3 seconds Note that this will ALSO include the clicks! What is a drag? Mouse held down for more than .1 sec -OR- significant movement with mouse down

19 Mouse To detect these you will need to keep track of when and where the mouse went down last! int lastMouseDownX, lastMouseDownY int lastMouseDownTime Then a click is when all of the following are true Left button is just down currentFrameTime - lastMouseDownTime < .1 sec abs(mouseX - lastMouseDownX) < 5 abs(mouseY - lastMouseDownY) < 5

20 Mouse Similar checks for Drag, Double click:

21 Mouse Questions?

22 Joysticks

23 Joysticks APIs for Joysticks XInput JInput
XInput is significantly easier to use, but only works for Xbox 360-style controllers And only on Windows With Java bindings too!

24 Joysticks No setup or cleanup code C API – Polling XInputGetState()
struct XINPUT_STATE Java API – Polling XInputDevice.poll(), .getComponents().getAxes(), .getComponents().getButtons() Gestures Deadzone for sticks

25 Joysticks XInputGetState(int i, XINPUT_STATE* state)
Returns ERROR_SUCCESS if a gamepad is connected i is 0 – 3, for each of the gamepads Fills out state with all the different parts of a gamepad

26 Joysticks struct XINPUT_STATE .Gamepad.wButtons
Bitand with values to see the state of the DPAD, face buttons, bumpers, and click sticks XINPUT_GAMEPAD_DPAD_UP, etc. .Gamepad.bLeftTrigger, bRightTrigger 0 – 255 values for each trigger .Gamepad.sThumbLX, sThumbLY – values for the axis sThumbRX, sThumbRY for right stick

27 Joysticks XInputDevice.isAvailable()
Returns true if XInput is available and library is loaded XInputDevice.getAllDevices() Returns an array of four XInputDevice objects, one for each player. Player 1’s controller is in arr[0], Player 2’s in arr[1], etc.

28 Joysticks XInputDevice.poll()
Updates the state of all buttons and axes based on the current state of the controller. Returns true if that controller is plugged in. Call this every frame on every device! XInputDevice.getComponents().getButtons() Get the current state of all the buttons as an XinputButtons object. Has public fields .a, .b, .x, .y, .up, .down, etc.

29 Joysticks XInputDevice.getComponents().getAxes()
Get the current state of all the axes as an XinputAxes object. Has public fields .lx, .ly, .rx, .ry, .lt, .rt, and raw copies of each Axes range from -1 to +1 for left and right sticks and 0 to 1 for triggers Raw values have the underlying integer values, which range from to for sticks and 0 to 255 for triggers.

30 Joysticks XInputDevice.getLastComponents()
Gets the previous state for axes and buttons. Think “prevKb” but for the input device!

31 Joysticks Players are very bad at holding the sticks perfectly stationary. Need to create a “dead zone” where even if the stick is moving, it is treated as being at zero I suggest values from -500 to +500 (-0.02 to +0.02) Not an issue with triggers!

32 Joysticks Questions?


Download ppt "CS 134 Alternate Inputs."

Similar presentations


Ads by Google