I have the style of the window set to none, and this snippet allow the user to drag the window when the mouse is pressed.
void TitleBar::HandleDrag(sf::RenderWindow *renderWindow) {
dragging = true;
while (dragging) {
lastMousePosition = sf::Vector2i(sf::Mouse::getPosition(*renderWindow).x, sf::Mouse::getPosition(*renderWindow).y);
if (Utilities::AABBCollision(float(lastMousePosition.x), float(lastMousePosition.y), float(lastMousePosition.x), float(lastMousePosition.y),
float(area.left), float(area.top), float(area.left + area.width), float(area.top + area.height)) && draggable) {
//std::cout << "Within area" << std::endl;
int offsetX = lastMousePosition.x - sf::Mouse::getPosition(*renderWindow).x;
int offsetY = lastMousePosition.y - sf::Mouse::getPosition(*renderWindow).y;
renderWindow->setPosition(sf::Vector2i(renderWindow->getPosition().x - offsetX, renderWindow->getPosition().y - offsetY));
sf::Mouse::setPosition(lastMousePosition, *renderWindow);
if (!sf::Mouse::isButtonPressed(sf::Mouse::Button::Left)) {
dragging = false;
}
}
}
}
The code has CPU usage for the process spike up to the high 20's and the window stutters when dragging, but when I uncomment the console output statement the CPU usage for the process is about 6% and dragging works as intended. The length of the string that the console outputs also seems to impact performance. The shorter the length of the string the more lag there is.
I'll work on a minimal example tomorrow morning, but for now I hope someone can find a reason why.