Skip to content

Instantly share code, notes, and snippets.

@skogler
Last active July 7, 2017 16:15
Show Gist options
  • Save skogler/9d9ab1b3e977d25901afe6fa1c2d608f to your computer and use it in GitHub Desktop.
Save skogler/9d9ab1b3e977d25901afe6fa1c2d608f to your computer and use it in GitHub Desktop.
SDL2_WarpMouseInWindow DeathAdder 2013 bug
/**
*
* This example demonstrates a bug with SDL2 and the Razer DeathAdder 2013
* mouse on Linux. There might be other configurations where this bug occurs.
*
* Press and hold the middle mouse button. This shows a 'Chat wheel' dummy.
* Move the mouse a bit while holding the middle mouse button. After releasing
* the middle mouse button, the cursor position should be restored to its
* original position.
*
* If it does not return to its position, you are affected by this bug.
*
* Compile with:
*
* g++ -lSDL2 sdl2warpmouse.cpp
*
*/
#include "SDL2/SDL.h"
#include <iostream>
int main(int, char**)
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Renderer* renderer;
SDL_Window* window;
if (SDL_CreateWindowAndRenderer(800, 600, SDL_WINDOW_OPENGL, &window, &renderer))
{
std::cout << "Could not create window: " << SDL_GetError() << std::endl;
return 1;
}
SDL_Event event;
bool exitRequested = false;
SDL_SetRelativeMouseMode(SDL_TRUE);
bool chatWheelActive = false;
const int chatWheelRadius = 60;
SDL_Rect chatWheelRect{.x = 0, .y = 0, .w = chatWheelRadius * 2, .h = chatWheelRadius * 2};
SDL_Point chatWheelSavedMousePosition;
SDL_Point currentMousePosition;
while (!exitRequested)
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_QUIT:
exitRequested = true;
break;
case SDL_KEYUP:
if (event.key.keysym.sym == SDLK_ESCAPE)
{
exitRequested = true;
}
break;
case SDL_MOUSEMOTION:
currentMousePosition.x = event.motion.x;
currentMousePosition.y = event.motion.y;
break;
case SDL_MOUSEBUTTONDOWN:
if (event.button.button == SDL_BUTTON_MIDDLE)
{
chatWheelActive = true;
chatWheelSavedMousePosition = currentMousePosition;
}
break;
case SDL_MOUSEBUTTONUP:
if (event.button.button == SDL_BUTTON_MIDDLE)
{
chatWheelActive = false;
currentMousePosition = chatWheelSavedMousePosition;
SDL_WarpMouseInWindow(window, chatWheelSavedMousePosition.x, chatWheelSavedMousePosition.y);
}
break;
default:
break;
}
}
SDL_Rect mouseCursorRect;
mouseCursorRect.w = 10;
mouseCursorRect.h = 10;
mouseCursorRect.x = currentMousePosition.x;
mouseCursorRect.y = currentMousePosition.y;
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
if (!chatWheelActive)
{
SDL_RenderFillRect(renderer, &mouseCursorRect);
}
else
{
SDL_GetWindowSize(window, &chatWheelRect.x, &chatWheelRect.y);
chatWheelRect.x = chatWheelRect.x / 2 - chatWheelRadius;
chatWheelRect.y = chatWheelRect.y / 2 - chatWheelRadius;
SDL_RenderDrawRect(renderer, &chatWheelRect);
}
SDL_RenderPresent(renderer);
}
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment