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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Mosseman

Pages: [1]
1
Graphics / Re: Events not responding
« on: October 12, 2013, 11:10:53 am »
void game::userInput(sf::Keyboard::Key key)
{
    if(key == sf::Keyboard::W)
        mIsMovingUp=true;
    else if(key == sf::Keyboard::S)
        mIsMovingDown=true;
}

2
General / Re: Lethn's Programming Questions Thread
« on: October 11, 2013, 03:33:11 pm »
Yes! :D That said I will admit it looks like the only way I'm going to be able to become independent as a programmer is rely on the book like others have said so I'm still going to keep at it.
Good!  You're learning very slowly at the moment but keep reading your book (as others have said: use ONE resource at a time while you're learning).  It looks like making GUI apps is the only way you're keeping yourself motivate to do this, so if toying with SFML is the you've gotta do it, then it's the way you gotta do it.  I just wish, for your sake, that you'd stick to a good tutorial/book.

Quote
Every time though I read the damn book I end up wanting to do this, particularly when it comes to variables:
Variables are easy.  Wait until you start using pointers (essential to know).  Wrapping my head around that concept was difficult for me.

3
General / Re: Lethn's Programming Questions Thread
« on: October 10, 2013, 01:06:13 pm »
I actually understood that and I don't know why people didn't explain libraries that way to me before when I asked, so either this is a good website or people are all going to jump in now claiming it's bullshit. Thanks again for posting that Hatchet, I'm sure other people will find the website really useful too.
I'm hurt.

But serisously, I'm glad it helps.  It's taught me a lot, it's helped me design little programs and hopefully hasn't taught me any bad habits.

4
General / Re: Lethn's Programming Questions Thread
« on: October 10, 2013, 11:58:32 am »
I know what you guys have been saying with the console thing, I've done small bits with the console but god I hate it
You seem to be a lot like me: A novice programmer with dreams of creating his own computer games.  I know writing programs for a command line interface seems dull compared to making games but you need to learn to walk before you run.

By insisting on writing GUI applications with the SFML library you're wrestling with several beasts at once.  Each time you try to write something and it goes wrong, you've got no idea if the problem is caused by your use of the language, if the program is structured improperly or if you're not using SFML properly.

Go through a tutorial or book, piece by piece and attempt to write programs that incorporate the things you've just learned, many tutorials will actually give you basic programs for you to write such as basic sorting algorithms or finding the nth prime.

Every time you make a modification to your program, I've noticed you're coming to these forums for help and you're leaning heavily on others to write code for you.  If you learn the C++ language, you'll not only have a much easier time writing your own software, but you'll be able to use SFML's documentation.  Asking for help isn't a problem, it's why these forums exist, but insisting on writing software so far outside your skill range means you're not learning anything.  The things people have shown you so far, you don't seem to understand, which means it's stuff you'll inevitably forget.

In the four months this thread's been alive, you could've sat down and studied C++ to the point where you could start writing GUI applications much easier.  You may not have the best coding practices, but at least you'd understand the libraries you're using much better.

5
General / Re: Lethn's Programming Questions Thread
« on: October 10, 2013, 03:17:13 am »
F*CK YES! IT'S MESSY BUT IT WORKS! :P Apologies for the unindented code again but I was mainly going for function, I didn't realise that's what the window commands were for, I thought you kept the events separate.


#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::Texture SelectedButton;
if (!SelectedButton.loadFromFile("Button Selected.png"))
{

}

sf::Sprite SpriteSelectedButton ( SelectedButton );
SpriteSelectedButton.setPosition ( 350, 120 );


sf::Texture UnSelectedButton;
if (!UnSelectedButton.loadFromFile("Button.png"))
{

}

sf::Sprite SpriteUnselectedButton ( UnSelectedButton );
SpriteUnselectedButton.setPosition ( 350, 120 );

    sf::FloatRect FirstButton ( 350, 120, 5, 5 );

    FirstButton = SpriteSelectedButton.getGlobalBounds();


bool DrawTheSprites;

while (window.isOpen())
{

{
    sf::Event event;


while (window.pollEvent(event))

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


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


        window.clear();

          if ( DrawTheSprites )
        window.draw ( SpriteSelectedButton );


    else

    {
        window.draw ( SpriteUnselectedButton );
    }


        window.display();

    }
    return 0;
}


 


When in doubt, rage code.
Here's your program, rewritten so it doesn't break anything.  I'm learning how to write GUI applications with SFML myself and I thought this would be a fun little excercise for myself.

I'm no master programmer, but I'm inclined to agree with Laurent, Hatchet and Nexus.  STOP trying to learn how to write a GUI application when your understanding of C++ is so shaky.  If you're having trouble with physical books, I'd recommend http://www.learncpp.com/ as it's easy to follow, does a great job explaining not only the basic language, but why the things you do learn are so important.  I know more experienced programmers don't like online tutorials, but that site is fantastic for helping people learn the basics of C++, not to mention a few good tips for software design in general.
//#include <iostream>           You don't need this module
#include <SFML/Graphics.hpp>
//#include <sstream>            or this one

// code has been properly indented
// ALWAYS make your code easy to read!
int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "For Lethn");

    /* You're not writing any text to screen right now, so this bit is pointless
    When modifying code, be sure to expunge all the obsolete stuff.  this is why comments
    such as these and readable code are so important

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


    sf::Texture SelectedButton;
    // OLD CODE: if (!SelectedButton.loadFromFile("Button Selected.png")){} ...why?
    // I noticed you originally introduced this error back in June, despite months of on/off revisions, it's still here

    if (!SelectedButton.loadFromFile("ButtonSelected.png"))
        return EXIT_FAILURE;

    sf::Texture UnSelectedButton;
    if (!UnSelectedButton.loadFromFile("ButtonUnselected.png"))
        return EXIT_FAILURE;            // added return statement

    sf::Sprite buttonSprite;
    buttonSprite.setPosition(350, 120);

    sf::Rect<int> buttonRect(350,120,80,80); // I've cheated here and simply set the rect position & size
                                             // manually the same as our sprite.  it's late, brain no function sleep without

    // your while loop had two opening braces, though i'm not sure what caused that to happen.
    // Again, the more readable your code, the less likely you're to make this mistakes
    while (window.isOpen())
    {
        sf::Event event;

        while (window.pollEvent(event))
        {       // found the missing brace!
            switch(event.type)
            {
            case(sf::Event::MouseMoved):
                if(buttonRect.contains(event.mouseMove.x, event.mouseMove.y))
                    buttonSprite.setTexture(SelectedButton);            // use this texture when mouse is over the button
                else
                    buttonSprite.setTexture(UnSelectedButton);          // use this one when it's not
                break;

            case(sf::Event::Closed):
                window.close();
                break;

            default:
                break;
            }
        }

        window.clear();

        window.draw(buttonSprite);  // buttonSprite automatically uses the correct texture

        window.display();
    }
    return EXIT_SUCCESS;
}
 
Sorry about the dirty shortcut when defining buttonRect.

Pages: [1]