How to select a specific sprite and move it in SDL - c++

I'm making a chess game, where I have a box shaped selector, now I want to select a sprite with that box and move it in a specific location with the selection box.
Any link or code?

You keep track of the position of everything.
Poll for events, see where the mouse is. If in the rectangle of a piece, put the selector on top of the piece (assuming you have colorkeying or alpha). You should be able to listen to keyboard input and move the selector based on that.
When mouse clicks, see where the selector is. Find the piece that's under it, and remember it.
When mouse clicks again, see where the selector is now. Check if moving there is valid. If yes, move the piece you remember to the new position.

Related

Is there an SFML delta mouse movement event?

I'm trying to implement a way to click and drag the screen in SFML. The idea being that when the user clicks down on their mouse, an sf::View gets changed by how much the user moves their mouse until they release it.
I tried to use the sf::Event::MouseMoveEvent struct under the assumption it would give the mouse movement and not the position. When I used it in code, however, it gets the position.
I could try and keep track of the previous mouse position and subtract it out to get the change, but I would expect that there would be a struct or function that would give me the delta position. It has this functionality for the scroll wheel, so I'd hope it does for the mouse.

Qt mouseMoveEvent - tracking mouse position

I'm programming my first 2D game in Qt.
I have QWidged where I draw my game (isometric view). When mouse enters border of widget it moves map view (like in every strategy game...).
And here is my trouble... I'm tracking mouse position with mouseMoveEvent but it fires only when mouse moves (only when position changes). So map moves only when I move mouse at borders. If mouse stand still, map does not move (mouseMoveEvent is not triggered). And I have no idea how to solve this. It's annoying when you try to play it.
This is my first post here.. and I hope that I explained my problem clearly :)
Edit (little more clarify):
Imagine this: you want to move map. So you move mouse to the edge of screen (QWidget) but at the moment when you stop mouse, map stops moving too. But mouse is still at edge of screen. What I want to do is that map will still move after mouse stops at edge.
You can create QPropertyAnimation for coordinates and start/stop it when mouse moves to/from widget's border.
Or you can remember current state ("changing x by -1 every 100ms, changing y by 0") and call some slot that does the real moving with QTimer.

Qt MouseMoveEvent only triggers with a mouse button press

I have an odd problem here.
I'm working on an application, and within one of my classes I'm monitoring my mouse events.
The weird thing is, my mouse move event will only get called if any mouse button is pressed.
I'm not even filtering for any button presses within the method; the method itself doesn't even get called unless I click on this object itself (the one that's monitoring it).
What generally causes this type of error to happen?
I'm not sure if it's relevant, but I have 2 different things monitoring my mouse inputs: 1) the main program monitoring the global mouse coordinates, and 2) an object within my program monitoring the mouse coordinates within itself.
Edit
So the problem has to be because mouse move event is generally used when people are dragging the cursor along the screen right?
My reason for not needing it like that is because I'm building a custom context menu of sorts, and I need to know when an item is hovered over.
It turns out that I didn't truly set everything within my class to enable mouse tracking.
I somehow thought if the class itself was set to have it enabled, I wouldn't need to set it to all the sub objects, but now I see how that wouldn't make any sense at all.
So just to clarify my solution:
The items that I needed to track my cursor's position needed to have
setMouseTracking(true);

Mouse programming

https://stackoverflow.com/questions/9466359/graphics-editor-in-c
I've developed a simple graphics editor in c++.It requires me to drag the mouse to draw a shape .After drawing the shape I want to fill it by picking a color but since dragging the mouse amounts to a large number of clicks because of which the entire screen gets filled with a default color even before i've drawn the shape. delay() doesn't work either.
the mouse click event in turbo c++ has two part. one where you press the button and two when you release the button. you need to drag so you should use the clrscr() function in the loop which goes on iterating until the mouse button is pressed down along with the code for the shape you want to draw. that way your screen keeps getting updated when you are dragging the mouse. and the loop ends when you release the button.
for filling the shape using the flood fill function should suffice
Perhaps one of the following links is what you're looking for:
http://www.codeproject.com/Articles/11313/Mouse-Programming-in-C-C
http://www.cprogrammingreference.com/Tutorials/Advance_Tutorials/mouseprogramming.php
http://www.brackeen.com/vga/mouse.html

Finding current mouse position in QT

This is my first attempt at writing a QT app, and I'm just trying to get a feel for how it works. My goal is to have a 400x400 widget which knows the exact position of the mouse when the mouse is hovering over it. For example, if the mouse was hovering in the top left corner, the position might be 10,10 (or something similar). If the mouse is in the bottom right corner, it might say 390,390.
Eventually, these coordinates will be displayed in a label on the main window, but that should be trivial. I'm stuck at the actual fetching of the coordinates. Any ideas?
For your widget, you must enable mouse tracking.
Then, you can either install an event filter, paying attention to mouse events and looking for the move event, or you can inherit from QWidget and override the mouse event, looking for mouse move events.
http://doc.qt.io/qt-4.8/qwidget.html#mouseTracking-prop
http://doc.qt.io/qt-4.8/eventsandfilters.html
http://doc.qt.io/qt-4.8/qmouseevent.html
If you are ever in a situation when you don't need actual tracking, just position at the moment, you can use QCursor::pos().