Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: shaking window [SOLVED]  (Read 7286 times)

0 Members and 2 Guests are viewing this topic.

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
shaking window [SOLVED]
« on: July 09, 2013, 05:39:15 am »
So i got this idea from most of the linux programs. When you type wrong username or password entire window moves lef, right, left right.. you get the point. so i tried [and obviusly failed] to implement that in my test program. Here is the code, i can post entire code if needed. I am using tgui also.

screen.setPosition(sf::Vector2i(10, 50)); // first
screen.setPosition(sf::Vector2i(30, 50));
screen.setPosition(sf::Vector2i(10, 50));
screen.setPosition(sf::Vector2i(30, 50));
 


The only thing that happends is that it loads first [as typed // first] value and stops there, so i tought maybe sfml is too fast so i made big for loop and blocked entire desktop :)

So is it possible to implement this into programs or i should drop it ?
« Last Edit: July 10, 2013, 06:47:12 pm by wmbuRn »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: shaking window
« Reply #1 on: July 09, 2013, 07:01:17 am »
Here's how I (used to) do it:
if (shake) screen.setPosition(sf::Vector2i(100 + rand() % 25, 100 + rand() % 25));
Very basic. It's called every frame, and shake is a bool.
Add timers, better positions, interpolation, etc. and you'll have something better looking.

When I try your 4 lines of code (with greater values, it's easier to see what's happening), I see the window moving very fast from the first value to the last.

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: shaking window
« Reply #2 on: July 09, 2013, 09:45:01 am »
I tried my code with larger values, but i never see the shaking.
so i did
for (int i= 0; i < 220; i++)
{
screen.setPosition(sf::Vector2i(10, 50));
screen.setPosition(sf::Vector2i(30, 50));
screen.setPosition(sf::Vector2i(10, 50));
screen.setPosition(sf::Vector2i(30, 50));
}
 
and i just see the last or the first code, not the shaking itself.

so i increased foor loop, making it i < 220000 [whick blocked my desktop entirely :)

Your command randomly sets screen somwhere else but there is no shaking. Will try more and will get it to work eventualy :)

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: shaking window
« Reply #3 on: July 09, 2013, 11:01:53 am »
Your command randomly sets screen somwhere else but there is no shaking.
You mean this small piece of code only sets the window position "somewhere else" once?
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(400, 300), "win");

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed) window.close();
        }

                window.setPosition(sf::Vector2i(100 + rand() % 25, 100 + rand() % 25));

        window.clear(sf::Color::Black);
        window.display();
    }

    return 0;
}
Because it shakes my window both on my windows (XP) and my linux (lubuntu).

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: shaking window
« Reply #4 on: July 09, 2013, 07:01:07 pm »
Your code is working. But when i add that to my program there is no shaking.  It is with tGui 0.6-dev, so when somebody click login, here is the code, it will explain better:

 while (screen.isOpen())
    {
        sf::Event event;
        while (screen.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                screen.close();

            // Pass the event to all the tgui stuff
            gui.handleEvent(event);
        }

        // The callback loop
        tgui::Callback callback;
        while (gui.pollCallback(callback))
        {
            // Make sure tha callback comes from the button
            if (callback.id == 1)
            {
                // Get the username and password
                tgui::EditBox::Ptr editBoxUsername = gui.get("Username");
                tgui::EditBox::Ptr editBoxPassword = gui.get("Password");

                sf::String username = editBoxUsername->getText();
                sf::String password = editBoxPassword->getText();

                if (username == "" && password == "")
                {
                  // clear the entire window and set new stuff to draw.
                }
            }
            if (callback.id == 2)
            {
             screen.setPosition(sf::Vector2i(100 + rand() % 25, 100 + rand() % 25));
            } // end if callback.id == 2  
        }

        screen.clear();

        // Draw all created tgui stuff
        gui.draw();

        screen.display();
    }
 

  if (callback.id == 2)  - Thats tGui button, its button [labeled: exit] but i will implement stuff there and when its working will move it to (callback.id == 1) which is actual login button. So my code  wont shake the window. It just move the screen one time. Also previous code i wrote here and for loop move the screen just once. Dont tell me tGui cant handle this :)

texus

  • Hero Member
  • *****
  • Posts: 505
    • View Profile
    • TGUI
    • Email
Re: shaking window
« Reply #5 on: July 09, 2013, 07:19:06 pm »
You only call setPosition once when the button is clicked, so the window only moves once.

Try changing it into something like this:
int shaking = 0;

    // Main loop
    while (screen.isOpen())
    {
        sf::Event event;
        while (screen.pollEvent(event))
        {
             // ...
        }

        // The callback loop
        tgui::Callback callback;
        while (gui.pollCallback(callback))
        {
            // Make sure tha callback comes from the button
            if (callback.id == 1)
            {
                // ...
            }
            if (callback.id == 2)
            {
             shaking = 1;
            } // end if callback.id == 2
        }

        if (shaking > 0)
        {
            screen.setPosition(sf::Vector2i(100 + rand() % 25, 100 + rand() % 25));

            if (++shaking == 5)
                shaking = 0;
        }

        screen.clear();
        gui.draw();
        screen.display();
    }
 
TGUI: C++ SFML GUI

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: shaking window
« Reply #6 on: July 09, 2013, 07:29:33 pm »
Yup it is working. Thank you G. and thank you texus. :)

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: shaking window
« Reply #7 on: July 09, 2013, 09:52:30 pm »
Of course if you only call it once. :-\

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: shaking window
« Reply #8 on: July 10, 2013, 03:50:03 am »
little clarification on getPosition please.

this is what documentation says:
Vector2i sf::Window::getPosition ( ) const
Get the position of the window.
Returns
    Position of the window, in pixels
so the code should like like this?

int x,y;
window.getPosition(sf::Vector2i(x,y));
 
i get error: no matching function for call to ‘sf::RenderWindow::getPosition(sf::Vector2i)’
so i tried this:

window.getPosition(sf::Vector2i());
 
and i get error: no matching function for call to ‘sf::RenderWindow::getPosition(sf::Vector2i)’

so i tried this:

window.getPosition();
 
and it compiles without errors, but how will i know what are the X and Y coordinates of the program ?
I am asking this becouse if somebody move a window and type wrong username or password the program will go to Y = 0 coordinates provided with this code:
window.setPosition(sf::Vector2i(100 + rand() % 20, 0));
 
i want it to shake only on X Vector, and to keep Y vector intact. [so basicly moving left-right-left-right and so on :) ] Thats why i need getPosition() to get Y coordinate and implement it into code:

window.setPosition(sf::Vector2i(100 + rand() % 20, Y)); // Y will be integer of current position of the screen
 

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: shaking window
« Reply #9 on: July 10, 2013, 06:37:24 am »
I know you claimed to not be a c++ newb and jump at people telling you otherwise, but this kind of shit is very basic C++ and programming. There's no shame in being a newb.
The prototype is very clear: Window::getPosition() returns the position of the window as an sf::Vector2i.
Why would you even try to put a parameter to this function when the prototype and doc clearly show that there is none? This method returns something, so use it.
sf::Vector2i currentWindowPosition = window.getPosition();
int currentWindowY = currentWindowPosition.y;
or
int currentWindowY = window.getPosition().y;

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: shaking window
« Reply #10 on: July 10, 2013, 08:20:52 am »
I am newb in sfml but not newb in c++. So i you said that i am a newb i should jump on you? Nope, maybe you are right. Main problem is that english is not my mothertong language and most of the time i dont understand people. But i do understand c++ code. And if somebody give me code i will try every solution it comes to my mind and will earn it. Since there is no good c++ book in my language everything i learned was errors and tries. But that never stopped me to learn. I tought

if setPosition looks like this:
window.setPosition(sf::Vector2i(100 + rand() % 20, 0));
 
than getPosition should look like this:
window.getPosition(sf::Vector2i(x,y));
 

which is obviusly a mistake. Anyway maybe i am a newb. I dont mind :)
Thank you G for helping me in apsolutly every topic :)
if i ever meet you you get a beer [6 pack] on me :)

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: shaking window
« Reply #11 on: July 10, 2013, 08:31:18 am »
I don't drink alcohol. :D

wmbuRn

  • Jr. Member
  • **
  • Posts: 58
    • View Profile
    • Email
Re: shaking window [SOLVED] partitialy
« Reply #12 on: July 10, 2013, 09:19:58 am »
More for me :)

anyway i included <iostream>
put those code
couted positions with

int currentWindowY = window.getPosition().y;
std::cout << "position Y: " << currentWindowY << std::endl;
 

And i get position that is not related to my app at all :)
window.setPosition(sf::Vector2i(100, 400));  // called this after creating a window
 

Output is: "position Y: 225" and when i move window output doesnt change.
[ i put that cout everywhere after creating window, after while (window.isOpen() loop [ and yes my terminal went crazy since cout is in loop ] But it doesnt display info as it should, it displays same X and Y positions, which are X: 10 X: 30 no matter where my window is . So i have no idea what i am doing. And its not  big problem. Need to start from basics again, to leearn everything better. :)

Thank you for your answers guys you helped me alot

« Last Edit: July 11, 2013, 02:14:52 am by wmbuRn »