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

Author Topic: Lethn's Programming Questions Thread  (Read 53743 times)

0 Members and 1 Guest are viewing this topic.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #120 on: September 23, 2013, 12:35:11 pm »
Oh yeah, I figured out why I was so bored, it was because I'd spent too much time learning and not enough time actually coding and making stuff, so I guess I just need to balance it out more, look! Text input! :D need to do m0ar experimenting and looking at example code.


#include <iostream>
#include <SFML/Graphics.hpp>
#include <sstream>

int main()
{

    sf::RenderWindow window(sf::VideoMode(800, 600), "");

    sf::Font arial;
    if ( !arial.loadFromFile ( "arial.ttf" ) )
    { }

    std::ostringstream txtinput;





    sf::Text writtentext;
    writtentext.setFont ( arial );
    writtentext.setCharacterSize ( 12 );
    writtentext.setColor ( sf::Color::White );
    writtentext.setPosition ( 20, 20 );


while (window.isOpen())
{

sf::Event event;
while (window.pollEvent(event))

    if ( event.type == sf::Event::TextEntered )

{
    if ( event.text.unicode < 128 )


        txtinput << ( char  ) event.text.unicode;
        gaingold.setString ( txtinput.str() );


}
{

if (event.type == sf::Event::Closed)
window.close();
}

        window.clear();
        window.draw ( writtentext );
        window.display();
        window.setFramerateLimit ( 30 );

    }
    return 0;
}

 

Edit: Oops cocked up my naming, need to be more careful, the code still works fine though.

Edit 2: Better now.
« Last Edit: September 23, 2013, 01:03:34 pm by Lethn »

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #121 on: September 28, 2013, 12:09:14 pm »
So I'm having a look at how buttons are made just out of curiosity while I keep doing experiments with them and I noticed that one thing is consistent with them is sf::Rect located here http://www.sfml-dev.org/documentation/2.1/classsf_1_1Rect.php, I was digging through old posts about this subject and I saw people like Nexus mentioning this documentation being what you need to look up :D.

I've also been looking at various games very closely which have a lot of buttons in them and I've noticed that even though you have buttons which clearly look circular or have an image that doesn't match the shape of a rectangle the way the programs read the mouse cursor going over it is that of a rectangle because if you go near the corners the hover colour just flips straight over to the other button rather than as a circle.

This is just a detail thing but I was wondering if there was an sf::circle somewhere I've missed? Or would it be the case you'd have to program your own thing somehow.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: Lethn's Programming Questions Thread
« Reply #122 on: September 28, 2013, 02:52:57 pm »
This is just a detail thing but I was wondering if there was an sf::circle somewhere I've missed? Or would it be the case you'd have to program your own thing somehow.
No there's not, since SFML doesn't use it and overall it would have only limited usage, where as sf::Rect has a lot of different usage.
Circular collision detection is quite easy though. All you need to do is measure the distance between button origin and mouse position and compare it against the button radius. And circle/circle collision is equally easy: Take the distance between two circle origins and check whether the distance is smaller than the sum of both radius.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #123 on: September 28, 2013, 03:07:27 pm »
Thanks eXpl0it3r, that's interesting, one of the things I like about buttons is they can be used to represent and do almost anything in the game whereas with keys you're a bit more limited so I figure is a good idea to learn about it as soon as possible.
« Last Edit: September 28, 2013, 03:29:13 pm by Lethn »

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #124 on: September 29, 2013, 12:58:41 pm »
I just successfully implemented some simple mouse movement detection thanks to an old post I found here http://www.gamedev.net/topic/619329-help-with-making-sfml-buttons/ I have some questions about the code provided though

The text changes to cyan when you put your mouse over it, the next step for me will be to make something happen when I click on it, like switching over to a different line of text or putting up a picture but that's for later.


#include <iostream>
#include <SFML/Graphics.hpp>
#include <sstream>

int main()
{

    sf::RenderWindow window(sf::VideoMode(800, 600), "");

    sf::Font arial;
    if ( !arial.loadFromFile ( "arial.ttf" ) )
    { }


    sf::Text FontOne;
    FontOne.setFont ( arial );
    FontOne.setCharacterSize ( 12 );
    FontOne.setColor ( sf::Color::Blue );
    FontOne.setPosition ( 350, 120 );

    FontOne.setString ( "This is line one!" );


    sf::Text FontTwo;
    FontTwo.setFont ( arial );
    FontTwo.setCharacterSize ( 12 );
    FontTwo.setColor ( sf::Color::Green );
    FontTwo.setPosition ( 350, 240 );

    FontTwo.setString ( "This is line two!" );


        sf::Text FontThree;
    FontThree.setFont ( arial );
    FontThree.setCharacterSize ( 12 );
    FontThree.setColor ( sf::Color::Red );
    FontThree.setPosition ( 350, 360 );

    FontThree.setString ( "This is line three!" );

    sf::FloatRect FirstButton ( 350, 120, 1, 1 );
    sf::FloatRect SecondButton ( 350, 240, 1, 1 );
    sf::FloatRect ThirdButton ( 350, 360, 1, 1 );

    FirstButton = FontOne.getGlobalBounds();
    SecondButton = FontTwo.getGlobalBounds();
    ThirdButton = FontThree.getGlobalBounds();

while (window.isOpen())
{

sf::Event event;
while (window.pollEvent(event))
{

    if ( FirstButton.contains ( event.mouseMove.x, event.mouseMove.y ) == true )

    {
        FontOne.setColor ( sf::Color::Cyan );
    }

    else

    {
        FontOne.setColor ( sf::Color::Blue );
    }

    if ( SecondButton.contains ( event.mouseMove.x, event.mouseMove.y ) == true )

    {
        FontTwo.setColor ( sf::Color::Cyan );
    }

    else

    {
        FontTwo.setColor ( sf::Color::Green );
    }

    if ( ThirdButton.contains ( event.mouseMove.x, event.mouseMove.y ) == true )

    {
        FontThree.setColor ( sf::Color::Cyan );
    }

    else

    {
        FontThree.setColor ( sf::Color::Red );
    }


if (event.type == sf::Event::Closed)
window.close();
}

        window.clear();
        window.draw ( FontOne );
        window.draw ( FontTwo );
        window.draw ( FontThree );
        window.display();

    }
    return 0;
}




 

What I wanted to know is that I found my code worked fine after I deleted:


else if(Event.Type == Event.MouseMoveEvent)

 

Is this because the code is old and it's something to do with an earlier version of SFML? I don't see any reason for it being there, I'd appreciate an explanation on that one because while I did find the problem I'm still a bit baffled as to what that line is supposed to do compared to the other stuff even though I understood the individual bits. Is it actually necessary to be there for a particular reason? Have I just made it more difficult for me to do something else later on if I delete it?

Secondly I wanted to know if it was possible to make the FloatRect visible so having some kind of block representing it so you knew that it went in correctly because at the moment when it comes to the way I'm doing it now it feels like I'm just guessing where a specific FloatRect is rather than actually knowing how wide it is etc. which will become a problem if I'm making a more complicated UI which I almost certainly will be.

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
« Last Edit: September 29, 2013, 03:44:00 pm by FRex »
Back to C++ gamedev with SFML in May 2023

Tobberoth

  • Guest
Re: Lethn's Programming Questions Thread
« Reply #126 on: September 30, 2013, 08:25:39 am »
FRex is being a bit short, but he has a good point: Some of your questions you can answer yourself very easily by simply reading the resources available to you. While it might feel comforting to ask the forum, you'll learn far more and progress faster if you try to rely on the reference etc as much as possible.

FloatRect is drawable and transformable. This pretty much tells you straight up that you can go rect.setTexture(someTexture), then window.draw(rect) and you'll see it just fine. Experimentation like this is great to learn more about SFML.

As for the MouseMoveEvent, I can't tell you why it messed up your code or anything like that... but I can say that if you don't understand why you would need it, I'd say it's fine to delete it. The code is there to help you, if you don't see its use, remove it and you'll eventually learn what it's needed for when you run into a problem where you need it. It's far too little code to put you in a position where you'll get stuck and not realize you need it. Events are only interesting if you specifically care about the event at hand anyway.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #127 on: September 30, 2013, 08:52:12 am »
lol Thanks guys, I had a look at the FloatRect documentation which helped me get through this problem so I'll read through the other stuff now as well, the book I was recommended is helping me understand the documentation a lot better :D. I just need to keep the reading and coding balanced now because I found that experimenting with the knowledge I had also helped me understand it better anyway, now I actually need to have a serious think about what games I want to get making O_O.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
AW: Re: Lethn's Programming Questions Thread
« Reply #128 on: September 30, 2013, 10:41:32 am »
FloatRect is drawable and transformable. This pretty much tells you straight up that you can go rect.setTexture(someTexture), then window.draw(rect) and you'll see it just fine. Experimentation like this is great to learn more about SFML.
Nope, sf::FloatRect is a typedef for sf::RECT<float> which is neither an sf::Drawable nor an sf::Transformable.
I guess what he meant to say was sf::RectangleShape has all those properties. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Tobberoth

  • Guest
Re: AW: Re: Lethn's Programming Questions Thread
« Reply #129 on: September 30, 2013, 11:08:08 am »
FloatRect is drawable and transformable. This pretty much tells you straight up that you can go rect.setTexture(someTexture), then window.draw(rect) and you'll see it just fine. Experimentation like this is great to learn more about SFML.
Nope, sf::FloatRect is a typedef for sf::RECT<float> which is neither an sf::Drawable nor an sf::Transformable.
I guess what he meant to say was sf::RectangleShape has all those properties. :)
Very true, I need to follow my own advice and spend more time reading the reference ^^

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #130 on: October 02, 2013, 03:00:05 pm »
Does anyone have some documentation they could point me towards that shows you how to hide and show sprites? I'm experimenting a lot more now but straight away I've run into a problem because I need to work with different sprites going on top of each other, I suppose I could hide the sprites off screen and use setPosition but that just seems like daft workaround really.

The closest I've been able to find is for cursors https://github.com/SFML/SFML/wiki/Tutorial:-Change-Cursor but I haven't found any sprite equivalent of setVisible. It will probably be one of those things that is really obvious if someone just points it out to me :P but in the mean time I'll keep searching.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Lethn's Programming Questions Thread
« Reply #131 on: October 02, 2013, 03:02:34 pm »
Umm, how about simply not drawing the sprite?
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #132 on: October 02, 2013, 04:40:32 pm »
It's got to be conditional, based on whether or not the mouse goes over it and stuff like that.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Lethn's Programming Questions Thread
« Reply #133 on: October 02, 2013, 04:47:57 pm »
bool sprite_must_be_drawn;

while (window.isOpen())
{
    ...

    sprite_must_be_drawn = mouse over it and stuff like that;

    if (sprite_must_be_drawn)
        window.draw(sprite);

    ...
}
Laurent Gomila - SFML developer

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #134 on: October 02, 2013, 05:29:27 pm »
Oh that's perfect! Thanks! Will need to experiment with it and see what happens.
« Last Edit: October 02, 2013, 05:31:00 pm by Lethn »