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

Recent Posts

Pages: [1] 2 3 ... 10
1
General / Re: Efficient ways to create buttons
« Last post by Hapax on June 24, 2024, 09:02:43 pm »
Another way is to use events only as you can then only do something when you would expect to.

For example, check events to see if a mouse button press happened and then check if the mouse was inside the button. Similar work for mouse button release if you want it to activate on release.

If you also want the ability to hover, then - again with events - check to see if mouse was moved and then check if the mouse is now inside the button. If so, it's now hovering; if not, it's now not.
2
General / Re: Efficient ways to create buttons
« Last post by Me-Myself-And-I on June 24, 2024, 06:57:00 pm »
Howdy! :)

I personally prefer making a button class.

Like so.





bool released;

class Button
{
        Sprite sprite;
        bool clickedleft=false;
        bool clickedright=false;
       
        bool active=false;
       
       
       
       
        void update(RenderWindow &window)
        {
               
                if(active)
                {
               
                        if(Mouse::isButtonPressed(Mouse::Left))
                        {
                                if(released)
                                {
                                        clickedleft=true;
                                }
                                released=false;
                               
                        }
                        if(Mouse::isButtonPressed(Mouse::Right))
                        {
                                if(released)
                                {
                                        clickedright=true;
                                }
                                released=false;
                               
                        }
                       
                       
                       
                       
                       
                        window.draw(sprite);
                }
               
                       
        }
};






int main()
{
       
        Texture texture;
        texture.loadFromFile("button.png");
       
       
        Button btn[10];
        btn[0].sprite.setTexture(texture);
        btn[0].sprite.setPosition(300,300);
        btn[0].active=true;
       
        RenderWindow window(VideoMode(960,540),"");
       
        while(window.isOpen())
        {
                Event e;
                while(window.pollEvent(e))
                {
                        if(e.type==Event::Closed)
                                window.close();
                }
               
               
                if(!Mouse::isButtonPressed(Mouse::Left)&&!Mouse::isButtonPressed(Mouse::Right))
                        released=true;
                       
               
               
                if(btn[0].clickedleft)
                {
                        btn[0].active=false;
                        cout<<"clickedLeft";
                }
               
               
               
               
               
               
               
               
                window.clear();
               
                for(int a=0;a<sizeof(btn);a++)
                {
                        if(btn.active)
                                btn.update(window);
                }
               
                window.display();
        }
       
       
       
}
 



This limits the checking for a click to one time and makes the button invisible when clicked.
3
My comment wasn't directed at you or related to having a BDFL. I was trying to achieve calling out a self serving proposal by Vee, one which would give him much more influence over the direction of the library at the expense of other stakeholders.

Could you please point out where I said that my vote would weigh twice as much as the other stakeholders' vote?

Or could you please point out where my proposal mentioned that if one of Vittorio's proposals gets below 75% approval he gets a pass anyway?

Oh, I guess I never said neither of those things. Just complete insanity.
4
I guess its a hack solution that works for you in your specific usage. That's fine to do. As long as it works for you that's good. I just wanted to give the solution for anyone who doesn't happen to have their weapon barrel flip like that. Such as when you want the bullet to be offset but not necessarily have a barrel to cover it.This solution that you used doesn't work if the offset happens to be a larger number.

I successfully made the offset without noticing any rotation change.
Here is a snapshot.



The code I used was the following:

                       
Bullet newbullet;
                       
                       
newbullet.shape.setPosition(sprite.getPosition().x+(32*cos(angle+45)),sprite.getPosition().y+(32*sin(angle+45)));
newbullet.go=Vector2f((cos(angle)),sin(angle));
bullet.push_back(newbullet);

                       
newbullet.shape.setPosition(sprite.getPosition().x+(32*cos(angle-45)),sprite.getPosition().y+(32*sin(angle-45)));
newbullet.go=Vector2f((cos(angle)),sin(angle));
bullet.push_back(newbullet);
 

Since you already have a solution that is good enough i'll just offer this code to others and say to you Have a nice day! :D
5
General / Efficient ways to create buttons
« Last post by Alex.UA on June 24, 2024, 03:09:08 pm »
Hi, i`m new to sfml. Lately i was thinking about most efficient ways to create buttons, especially how to track clicks. I thought about two ways of making it. First: every button checks every tick is it hovered. If true then check for right mouse click event. Second is to check if my cursor is in zone of any button(or object).If true do something on click. So I`d like to know which of the ways better, or is there other efficient way before i move on
6
Quote
Now i'm confused. How does this work?

From what i understand, the formula does the following:
adds weapons x axis to 76 * x axis of the angle which is calculated by using cos. Does the same for the y axis.
So basically it adds an offset to the bullets spawning location which in my case is 76.
Since it is a ratio i cant put a different number thats multiplying by sin of the rotation angle which is y.
So what i did is just add 0,-4 to that ratio which doesn't change x, but changes y by -4, so now my bullets spawn on the barrel.

Regarding your problem with shifting of the barrel as the rotation occurs, i have this piece of code in my project that flips the weapon during rotation so it always shoots with its barrel.
Without this code the weapon shoots from its stock when aiming left, and shoots from its barrel when aiming right.
It's a simple fix for this visual bug, but if i understood you wrong let me know.

if (target.x < weaponSpr.getPosition().x && weaponSpr.getScale().y > 0)
{
    weaponSpr.setScale(weaponSpr.getScale().x, -weaponSpr.getScale().y);

}
if (target.x > weaponSpr.getPosition().x && weaponSpr.getScale().y < 0)
{
    weaponSpr.setScale(weaponSpr.getScale().x, -weaponSpr.getScale().y);

}

One thing that leaves a questionmark in my head, is that in that ratio y is also set to 76 which should offset my bullets spawning location way, way below the barrel, but it offsets only so little that when changing the y axis by -4 it spawns right on the barrel, perfectly.

I could've understood everything wrong and fixed it by accident which would be very weird, so let me know if what i said makes any sense.

PS. why 76?
Because its the  changed x origin of my weapon which i wanted to be 32 and since i changed the weapons scale to 2,2 then i had to multiply it by 2. now we have 64 and what is left is to add the bullet x axis of the origin to that number which gives us 76.

EDIT: I shared my weapons texture as an attachment, so you can copy the code, apply it to that specific texture and see for yourself.
7
General / Re: Check if SFML is installed
« Last post by Garwin on June 24, 2024, 11:13:20 am »
You can use macros not exactly what you ask but still can help you to detect certain header file.
#if __has_include
https://en.cppreference.com/w/cpp/preprocessor/include

eg.:
#if __has_include("SFML/System/String.hpp")
    #include <SFML/System/Vector2.hpp>
    #define has_sfVector 1
#else if
    #define has_sfVector 0
#endif // __has_include("SFML/System/String.hpp")


 

8
General / Re: Check if SFML is installed
« Last post by eXpl0it3r on June 24, 2024, 08:58:09 am »
Not really.

If you use CMake to build your application, you could search for it with find_package, which should find it, if it's installed (whatever you definition of "installed" is).
9
General / Check if SFML is installed
« Last post by Rhubarb on June 23, 2024, 09:06:21 pm »
Hello, is there any #ifdef to check if libsfml-dev is installed? Thanks.
10
Now i'm confused. How does this work? It doesn't work for me. Sure it stays close to the barrel but it gets shifted from one side of the barrel to the other as the rotation occurs. When you say x-axis and y-axis I assume your talking about the difference based on what you consider the vertical and horizontal position of the sprite. Rotations don't act this way. Your x and y axis positions change continually because as the rotation occurs the canvas coordinate system stays the same. You have to think in terms of angle and distance from the circle center.

The distance from the centerpoint is adjusted by multiplying one distance against the ratio.
This essentially changes the radius of the circle, making it farther away from the center.
newbullet.shape.setPosition(weaponSpr.getPosition().x+(32*cos(angle)),weaponSpr.getPosition().y+(32*sin(angle)));
 

Then the angle changes the rotation on that circles edge.

Like so.

                       
newbullet.shape.setPosition(weaponSpr.getPosition().x+(32*cos(angle+45)),weaponSpr.getPosition().y+(32*sin(angle+45)));
 

I'm not sure how to get the right values for it. I think you just have to adjust it by trial and error or figure out the math needed to convert the side height to the angle.
Perhaps i'm wrong. If i'm wrong then someone please say so. I'm just not sure how adding (0,-4) to the starting y position is any different than adding plain -4 to the y position. Which ,as we have seen in the last few posts, does not work because it offsets the angle.
So sorry to ruin your glory.
Pages: [1] 2 3 ... 10