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

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

0 Members and 4 Guests are viewing this topic.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #135 on: October 09, 2013, 02:18:50 pm »

#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 ( DrawTheSprites )
        window.draw ( SpriteSelectedButton );


    else

    {
        window.draw ( SpriteUnselectedButton );
    }

}


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

        window.clear();
        window.display();

    }
    return 0;
}

 

Well this is interesting, I think I've got the code sorted now but nothing comes up when I move my mouse over to where the button should be. If I put the window.draw where you normally would put it the sprite comes up but it's not conditional and just sticks there. Going to mess around further with this and see if I can fix it by myself but if it's something pretty obvious comments would be appreciated as usual :D.
« Last Edit: October 09, 2013, 02:21:52 pm by Lethn »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Lethn's Programming Questions Thread
« Reply #136 on: October 09, 2013, 02:26:44 pm »
Could you please indent your code next time?

And please read the tutorial about event handling carefully. Seriously, if you never invest time into reading theory, you don't have to wonder nothing ever works ::)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #137 on: October 09, 2013, 02:34:14 pm »
lol! Sorry, it is a mess, I was editing a lot, I'll check it out thanks.

Edit: Just realised what was wrong, going to fix it now.
« Last Edit: October 09, 2013, 02:35:51 pm by Lethn »

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #138 on: October 09, 2013, 07:25:28 pm »
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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Lethn's Programming Questions Thread
« Reply #139 on: October 09, 2013, 08:39:26 pm »
I don't know why it works, but your way of handling events is still wrong. It's only a matter of time (or bad luck) until it breaks.

By the way, if meaningful indentation is too much of a challenge, there are tools like astyle :P
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #140 on: October 09, 2013, 09:21:22 pm »
I'm still not 100% sure what you mean by that though, could you explain it? I was looking at the documentation etc. and this is how I figured it out. When I did it Laurent's way I either found that it either threw up the blank screen or complained about event not being declared etc. it clearly must be right if it works but I still haven't found out how to do it his way.

There don't seem to be any weird glitches or anything like that going on right now so now I'm looking at it after it's working I'm a bit baffled too.
« Last Edit: October 09, 2013, 09:25:59 pm by Lethn »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Lethn's Programming Questions Thread
« Reply #141 on: October 09, 2013, 09:25:38 pm »
Just check how an event loop looks here and compare it with your code.

And read the text!
Quote
Before dealing with events, it is important to understand what the sf::Event type is, and how to correctly use it. sf::Event is a union, which means that only one of its member is valid at a time (remember your C++ lesson: all the members of a union share the same memory space). The valid member is the one that matches the event type, for example event.key for a KeyPressed event. Trying to read any other member will result in an undefined behaviour (most likely: random or invalid values). So never try to use an event member that doesn't match its type.

Now look at your code and recognize that you access event members before testing for the event type.

There is even a highlighted box which repeats and emphasizes the problem, Laurent wrote that exactly for people like you.
Quote
Read the above paragraph once again and make sure that it's printed in your head, the sf::Event union causes too many problems to inadvertent programmers.

But please, please read those things. Laurent put a huge effort in making the tutorials, he also mentioned a lot of pitfalls in the hope that they would prevent people from making mistakes. If you read tutorials, documentations and C++ literature carefully, you will stop wasting the time of yourself and other people.
« Last Edit: October 09, 2013, 09:29:20 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #142 on: October 09, 2013, 09:30:45 pm »
Ahh, I think I understand that a bit now, so it's to do with this little bit here:


    // check the type of the event...
    switch (event.type)
    {

 

What I've done to make it work is ended up bypassing the whole event check entirely and dumped the code in with the drawing functions, so it works for the simple stuff but if I try to do anything more complicated it will break. Looks like another case of not checking the code carefully enough for missing bits and where it's all positioned >_< I tend to make those mistakes most even when I understand what I'm doing.

I'll see if I can't get the code working properly now I have an idea of what's going on.
« Last Edit: October 09, 2013, 09:34:14 pm by Lethn »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Lethn's Programming Questions Thread
« Reply #143 on: October 09, 2013, 09:33:51 pm »
I think I understand that a bit now
Don't think you understand it a bit, but be sure you understand it completely!

What I've done to make it work is ended up bypassing the whole event check entirely and dumped the code in with the drawing functions, so it works for the simple stuff but if I try to do anything more complicated it will break.
Your code is already now broken. Please read my last post again, I edited it to clarify the problem.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Lethn's Programming Questions Thread
« Reply #144 on: October 09, 2013, 09:41:15 pm »
Looks like another case of not checking the code carefully enough for missing bits and where it's all positioned >_< I tend to make those mistakes most even when I understand what I'm doing.
After loads of pages, I honestly doubt you understand what you're doing. And the problem is not tiny bits of code which you overlook, but rather the underlying knowledge which you are missing, or only know at the surface.

You want to achieve things with too fast pace, but you must learn that they inevitably take time. Trying to bypass theory or taking abbreviations makes everything worse, which can be seen very clearly in this thread.

Anyway, it's not as if not several people had said that months ago. It's really time that you actually consider our advice and not just speak of it.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Mosseman

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #145 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.

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Lethn's Programming Questions Thread
« Reply #146 on: October 10, 2013, 03:17:51 am »
Indeed, again you really should just sit down and develop some console versions of these programs:

1.)  Take input from the keyboard, only allow numbers, and then calculate whether that number is prime or not, return true or false, if true return the primes root, if false give a close estimate.
2.)  Simple text file I/O
3.)  Strings, learn to manipulate strings with user input and input from a text file.  Print strings back to the console and print back to another text file.
4.)  A program that takes a text file of random numbers and gives the user an option to pick one of X number of 'sorts' to sort the list.  Sorts could be quicksort, bubble sort, heap sort, any other kind of sort you want.
5.) a simple console based Tic-Tac-Toe game
6.) console based Checkers game
7.)  a program that makes use of pointers such as your own Linked List

By doing any of those first you'll start to gain a greater knowledge of what you're trying to play with now in SFML and all these tiny little graphical things you're trying to figure out will seem very simple.

If you were trying to build a bike you need to figure out how to build the gear/sprocket/chain and wheels first, instead you are focusing on what sound your horn is making.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #147 on: October 10, 2013, 11:13:33 am »
Thanks, I'll bookmark that site and have a look ahh, hang on this looks a lot easier to read than that book because it's laid out properly on a webpage rather than just scanned to a .pdf so I might have an easier time with that and I can use the book for reference to cross-check everything so I know the site hasn't made mistakes.

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 >_< it must be because I do art or something but I hate the idea of dealing with bland text which is why I'd rather struggle making multi-coloured text and interactive buttons that glow when you hover over them :P.

The console reminds me too bloody much of DOS >_< and DOS was something I always found irritating to use, I'll also be sure to use that code you posted Hatchet as reference and a starting point for everything else. What I don't understand is there seem to be conflicting bits in your code with the material I've been reading from, the only place where events are used in the way you used them are in the events tutorial but in the main documentation Laurent doesn't seem to use them. The same goes for that other piece of code I found for doing text based button work.
« Last Edit: October 10, 2013, 11:21:48 am by Lethn »

Mosseman

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #148 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.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #149 on: October 10, 2013, 12:22:07 pm »
Well this website is much easier to read through than the book so I'll post up some basic console code soon if only to stop Nexus from being condescending and repeatedly accusing me of not understanding anything throughout this entire thread, but I'm sure he'll find a way even when I get to OpenGL.

Quote

Libraries are groups of functions that have been “packaged up” for reuse in many different programs. The core C++ language is actually very small and minimalistic — however, C++ comes with a bunch of libraries, known as the C++ standard libraries, that provide programmers with lots of extra functionality. For example, the iostream library contains functions for doing input and output. During the link stage of the compilation process, the libraries from the C++ standard library are the runtime support libraries that are linked into the program (this will be discussed further in lesson 1.4).


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 Mosseman, I'm sure other people will find the website really useful too.
« Last Edit: October 10, 2013, 01:13:43 pm by Lethn »

 

anything