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

Author Topic: Image doesn't stand still  (Read 1834 times)

0 Members and 1 Guest are viewing this topic.

klainse

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Image doesn't stand still
« on: January 09, 2017, 10:52:50 pm »
I m trying to make a menu for a simple snake game.
I ve got 2 photos,one for the normal menu,and one for when the mouse goes through a particular area of a text from the menu(simple makes the text look different ).
The problem is that the image "vibrates" ,simply the first image wont change into second to enlighten the text,it will change just for very short time (0,1 ms 0,2 ms)

Also ,on click ,i tried to make the game start.

So let's get straight.
If the mouse just goes through a specific area,i want the image to change.
If then the mouse clicks,i also want the game to start.
 
there is the code from the main function

int main()
{
sf::RenderWindow window(sf::VideoMode(size*length+size, size*width+size), "SFML works!");
 Texture t4,t5;
    t4.loadFromFile("images/men.jpg");
    t5.loadFromFile("images/men2.jpg");
        Sprite nebunie(t4);
        Sprite nebunie2(t5);
        while (window.isOpen())
    {window.draw(nebunie);
    window.display();
    Event Event;
    while (window.pollEvent(Event))
    {sf::Vector2i position = sf::Mouse::getPosition();
    cout<<position.x<<" "<<position.y<<"\n";
            if (position.x>583&&position.x<820&&position.y>293&&position.y<319)
            {
                window.draw(nebunie2);window.display();
                if (Event::MouseButtonPressed)
                if (Event.mouseButton.button==Mouse::Left) game();
                }
}
}

This is my first sfml project,i m sorry if i haven't explained quite well.
« Last Edit: January 10, 2017, 01:48:31 am by eXpl0it3r »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10777
    • View Profile
    • development blog
    • Email
Re: Image doesn't stand still
« Reply #1 on: January 10, 2017, 01:48:18 am »
Please make use of the [code=cpp][/code] tags when posting code.

You seem not have a misunderstanding of how SFML's clear, draw, display process works. As such you should revise the official tutorials again. Make sure you actually understand what your code is doing and don't just assume what it might do.

Here are some questions you need to be able to answer:

What does the display() function do? When should you be calling the display() function?
What's the purpose of the clear() function? When should you be calling the clear() function?
Why does there exist a game loop? What is its purpose?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

klainse

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Image doesn't stand still
« Reply #2 on: January 10, 2017, 02:57:09 pm »
The game simply works ,and the window also.
My problem is that "If the mouse just goes through a specific area,i want the image to change.
If then the mouse clicks,i also want the game to start.
 " doesnt work.
and i don t know why.
i ve read the tutorial,and i told u,the game works.
My problem is or with the mouse functions,or with the window display.
It s kinda flickering when mouse goes throug that specific pixels,the image doesnt permanently change(while cursor is there).

Rhimlock

  • Jr. Member
  • **
  • Posts: 73
    • View Profile
Re: Image doesn't stand still
« Reply #3 on: January 10, 2017, 04:18:28 pm »
You draw nebunie every frame and display it on your screen.

When your mouse is over your button you still draw nebunie every frame and display it, but you also draw nebunie2 and display it.

So its no wonder you get a flickering.

you should take your position-check out of the event-loop and make the if-statement decide wether do draw your first sprite of your last.

pseudo code:

clear()
if (mouse over button)
{
  draw(sprite2)
}
else
{
  draw(sprite1)
}
display()
while (eventloop)
{
  check mouse button
}
« Last Edit: January 10, 2017, 04:22:31 pm by Rhimlock »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10777
    • View Profile
    • development blog
    • Email
Image doesn't stand still
« Reply #4 on: January 10, 2017, 05:24:23 pm »
Just because something works it doesn't mean that it's correct.

Study what the code does and answer the provided questions for yourself.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

klainse

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: Image doesn't stand still
« Reply #5 on: January 10, 2017, 10:35:35 pm »
Thank you very much for your help.
Rhimlock i understood where i was wrong,your pseoudo helped me a lot.
Thank u eXpl0it3r,this is my first sfml code ,i ll learn from my mistakes.
U can T.C

Hapax

  • Hero Member
  • *****
  • Posts: 3339
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Image doesn't stand still
« Reply #6 on: January 15, 2017, 02:41:53 pm »
                if (Event::MouseButtonPressed)
This will always be true as it's a non-zero-valued enum.

I think that it's very possible that you meant this:
                if (Event.type == sf::Event::MouseButtonPressed)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything