Moving sprites in a vector - c++

I move sprites stored in a formationvector by holding the mousebutton. Problem is: Whenever i move the sprite onto another one, i move both sprites at the same time.
What i want: Moving the sprite1 over the other sprites2 without changing the position of sprite 2 or with other words:
-Testing in a loop if a sprite of a vector is clicked on
-If sprite is clicked on:
Move this sprite around while button is pressed but somehow stop this move while this movement in order to avoid moving more than this one sprite.
Here is my try so far:
while (App.pollEvent(Event))
{
// Window closed
if (Event.type == sf::Event::Closed)
{
return (-1);
}
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
for (size_t k = 0; k < formation.size(); k++)
{
if (isMouseOver(formation[k], App) == true)
{
Mouseposition = sf::Vector2f(sf::Mouse::getPosition(App));
Mouseposition.x = Mouseposition.x - formation[k].getLocalBounds().width / 2;
Mouseposition.y = Mouseposition.y - formation[k].getLocalBounds().height / 2;
formation[k].setPosition(sf::Vector2f(Mouseposition));
Formation_playernames.clear();
Formation_playerinformation.clear();
Formation_Playernames(Font, Formation_playernames, formation, playerlist);
Formation_Playerinformation(Font, Formation_playerinformation, formation, playerlist);
}
}
}
}
The Formation-functions store the correct new positions of the sprites and their colors based on the positions into the formation vector
The problem is the for-loop, i could do it without but that would lead to a much longer code. How could i do it ?

Instead of simply checking whether or not the mouse button is pressed, you should structure your code so you can check exactly when the button is pressed, and when it is released. You can then structure your code such that different presses of the button are distinguished from each other. semi-pseudocode follows
bool button_is_pressed = false;
Sprite* currently_selected_sprite = nullptr;
// main application loop
while (...)
{
...
// other application logic
...
if (!button_is_pressed)
{
if (CheckIfButtonIsPressed())
{
button_is_pressed = true;
// Button was just pressed.
// Select the appropriate sprite by checking
// the mouse coordinates against the positions
// of the sprites.
}
else
{
// Button not being pressed.
// Likely no logic needed here.
}
}
else // button_is_pressed == true
{
if (CheckIfButtonIsPressed())
{
// Button is being held down.
// Implement dragging logic using the
// pointer to the selected sprite.
}
else
{
button_is_pressed = false;
// Button was just released.
// Deselect the sprite.
currently_selected_sprite = nullptr;
}
}
}
Alternatively, you could handle mouse events, which will take care of much of this logic for you. http://sfml-dev.org/documentation/2.0/classsf_1_1Event.php
In a more English like pseudo code, this is what your function is doing:
if the mouse button is currently pressed
move all the sprites which are under the mouse to be centered on the mouse cursor
else
do nothing
This is what I'm suggesting
at the moment the mouse button is pressed down
select the sprite which is under the mouse cursor
at the moment the mouse button is released
deselect the selected sprite
if the mouse button is currently pressed, and a sprite is selected
move the selected sprite to the position of the mouse cursor

Related

box2d falling while moving

Hey my player isn't falling while I'm pressing any of movement inputs while I'm falling. Just stands still and moves right or left.
Just watch the video; Video
My movement code;
if (right == true) {
p_pBody.body->SetLinearVelocity(b2Vec2(5, 0));
}
else
{
p_pBody.rect.setPosition(p_xPos * s_METPX, p_yPos * s_METPX); // Set The SFML Graphics
}
if (left == true) {
p_pBody.body->SetLinearVelocity(b2Vec2(-5, 0));
}
else
{
p_pBody.rect.setPosition(p_xPos * s_METPX, p_yPos * s_METPX); // Set The SFML Graphics
}
You're setting the vertical velocity to 0 when you're pressing right or left. That's the second coordinate of b2Vec2. If you want to have gravity, replace that zero with the vertical velocity the block has when the buttons are not being pressed.

Opengl draw a triangle by clicking a mouse

I want to draw a triangle like this
I think I have to change these part of my codes
glBegin(GL_LINES);
glVertex2f(point[0][0], point[0][1]);
glVertex2f(point[1][0], point[1][1]);
glEnd();
and my mouse button down codes are like this
if (action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT)
{
inputMode = InputMode::DRAGGING; // Dragging starts
point[0][0] = xw; point[0][1] = yw; // Start point
point[1][0] = xw; point[1][1] = yw; // End point
}
How I have to do?
You need some global variables for 3 points and one index that is telling you which point you actually edit ...
float point[3][2];
int ix=0;
the render change to
glBegin(GL_LINE_LOOP); // or GL_TRIANGLE
glVertex2fv(point[0]);
glVertex2fv(point[1]);
glVertex2fv(point[2]);
glEnd();
now I do not code in GLFW but you need to change the onclick events to something like:
static bool q0 = false; // last state of left button
bool q1 = (action == GLFW_PRESS && button == GLFW_MOUSE_BUTTON_LEFT); //actual state of left button
if ((!q0)&&(q1)) // on mouse down
{
if (ix==0) // init all points at first click
{
point[0][0]=xw; point[0][1]=yw;
point[1][0]=xw; point[1][1]=yw;
point[2][0]=xw; point[2][1]=yw;
}
}
if (q1) // mouse drag
{
point[ix][0]=xw; point[ix][1]=yw;
}
if ((q0)&&(!q1)) // mouse up
{
point[ix][0]=xw; point[ix][1]=yw;
ix++;
if (ix==3)
{
ix=0;
// here finalize editation for example
// copy the new triangle to some mesh or whatever...
}
}
q0=q1; // remember state of mouse for next event
This is my standard editation code I use in my editors for more info see:
Does anyone know of a low level (no frameworks) example of a drag & drop, re-order-able list?
I am not sure about the q1 as I do not code in GLFW its possible you could extract the left mouse button state directly with different expression. the q0 does not need to be static but in such case it should be global... Also its possible the GLFW holds such state too in which case you could extract it similarly to q1 and no global or static is needed for it anymore...

How to get value with mouse event drag

I have in my UI one QScrollArea with an image, and I want to get some value when I click on the image.
Being more explict, I need to change the brightness of an image, and I will get the value with the mouse. I already see the MouseMoveEvent but I don't know how to use it.
If I get the position of the mouse when I click and drag, I can extract one value to change the bright of my image, that I know. I just don't know how I will get the position.
Does anyone know how I can do this?
Ps.: My QScrollArea was created on Design, so I don't have any code writed by me with the specifications of the QScrollArea.
there is a small correction . when i programmed like this i was getting the movements a liitle jerky . so i tried to modified the code and now working perfect .
heres how i changed
const int deltaX = event->scenePos().x() - m_lastClickPosition.x();
if ( deltaX > 10 )// to the right
{
moveBy(10,0);
m_lastClickPosition = event->scenePos();
}
else if ( deltaX <-10 )
{
moveBy(-10,0);
m_lastClickPosition = event->scenePos();
}
All the information you need is in the QMouseEvent object that is sent to the mouseMoveEvent handler of your widget.
QMouseEvent::buttons()
QMouseEvent::pos()
A simple way to do what you're after is to change the brightness of the image whenever you receive a "mouse movement event" and the QMouseEvent object reports a button down (this means the user is moving the mouse while holding down a button).
void MyWidget::mousePressEvent( QMouseEvent* event )
{
if ( event->button() == Qt::LeftButton )
{
// Keep the clicking position in some private member of type 'QPoint.'
m_lastClickPosition = event->pos();
}
}
void MyWidget::mouseMoveEvent( QMouseEvent* event )
{
// The user is moving the cursor.
// See if the user is pressing down the left mouse button.
if ( event->buttons() & Qt::LeftButton )
{
const int deltaX = event->pos().x() - m_lastClickPosition.x();
if ( deltaX > 0 )
{
// The user is moving the cursor to the RIGHT.
// ...
}
else if ( deltaX < 0 ) // This second IF is necessary in case the movement was all vertical.
{
// The user is moving the cursor to the LEFT.
// ...
}
}
}

MFC - How to draw pixel waves inside a rectangle

I am new to MFC trying to learn from the scratch.
I am trying to create a small dialog based application in VS2012 - MFC.
On the dialog window i have drawn a rectangle (in OnPaint event) like this.
CPaintDC dc(this);
CRect background1(10,10,208,92);
dc.Rectangle(10,10,208,92);
Then i filled the bacground with some color. Like this.
CBrush brush1;
brush1.CreateSolidBrush(RGB(2,3,4));
dc.FillRect(background1,&brush1);
Now i wanted to draw waves in form of pixels continuously inside the rectangle.
As of now what i have done is,
bool top = false;
for(i=10,j=92;i<200 && j>10;)
{
pDC->SetPixel(i,j,NewColor);
Sleep(10);
if(!top)
j-=1;
else
j+=1;
if(j%4==0)
{
i+=1;
}
if(j==12)
top=true;
if(j==90)
top = false;
}
I am just drawing the pixels straightaway on the window, but within the dimensions where the rectangle lies. And the waves stop as it reaches the right dimension of the rect. But i feel thats not the right way to do it.
I want to draw the waves inside the rectangle and also continuosly, like when it reacahes the right end it should move left and it should be continuous.
Is there any proper way in MFC to draw something inside a rectangle (technically inside another object)?
Please help me out.
The ScrollWindow function can be used to scroll the existing graph to the left. Then you draw the new data to the right of the scrolled area. The drawing will be much faster if you use the Polyline function instead of DrawPixel: Polyline draws an array of pixels.
I tried your code and just added some condition, so that if it reaches right side it should start moving toward left.
void CMyDlg::OnBnClickedOk()
{
CWnd *cWnd = FromHandle(m_hWnd);
CDC *pDC = cWnd->GetDC();
bool top = false;
bool right = false;
int i,j;
for(i=10,j=92;i<=200 && j>10;)
{
pDC->SetPixel(i,j,RGB(255,255,255));
Sleep(10);
if(!top)
j-=1;
else
j+=1;
if(j%4==0)
{
if(!right)
i++; // if reaches right side i--;
else{
i--;
}
}
if(j==12)
top=true;
else if(j==90)
top = false;
if(i == 200)// make right = true if reaches right side
right = true;
else if(i == 10)// make false it reaches left side
right = false;
}
}
I am getting output something like this
NOTE: this will cause infinite loop, you need to check condition where you have to stop printing pixels.
You are NOT looping with Sleep(10) in your WM_PAINT handler, are you?
MFC or not, you should separate your data processing from presentation.
It appears you are trying to do an animation; the simplest way to do it is to have an off-screen (memory) DC and drawing your pixels on it. At certain times (frame rate?), you would call parent's InvalidateRect()/UpdateWindow() pair. Then in ON_PAINT, you would BitBlt() your prepared DC onto your preferred location in the dialog.

Create variable from memory

I'm using sfml. I need to write a function that will create a type sf::RectangleShape and return a new sf::RectangleShape with the position of the mouse at the current time and stay there after I use window.draw() Right now my code is:
sf::RectangleShape functions::Map(sf::RectangleShape wall0){
wall0.setPosition(0,0);
wall0.setSize(sf::Vector2f(200,200));
wall0.setFillColor(sf::Color::Green);
wall0 = this->wall0;
return wall0;
}
void functions::Draw(){
window.clear();
window.draw(logo);
if (menu){
window.draw(menuStart);
window.draw(menuOptions);
window.draw(menuExit);
}
if (playing){
void Map();
window.draw(wall0);
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)){
}
}
I want to return wall0 with the new mouse position when the left mouse button is pressed. I know how to get the mouse input but not how to draw the object and keep it on the window. I've gotten it to where it draws the object while the mouse button is clicked but it doesn't stay.
I'm not familiar with sfml - but briefly looking at your code this is what I think you want to do:
void functions::Map(sf::RectangleShape & wall0){
wall0.setPosition(0,0);
wall0.setSize(sf::Vector2f(200,200));
wall0.setFillColor(sf::Color::Green);
}
Then functions::Draw method:
...
if (playing){
window.draw( Map(wall00) );
if (sf::Mouse::isButtonPressed(sf::Mouse::Left)){
...