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

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

0 Members and 1 Guest are viewing this topic.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Lethn's Programming Questions Thread
« on: June 20, 2013, 08:32:40 pm »
I thought instead of spamming the forum with new topics guys I'd just spam the one topic then that way everyone else can keep posting as normal. I didn't think I'd get this deep into programming but as I go through tutorials I've got more and more things to investigate, some stuff I just get on my own but other stuff like what I'm working on currently I need help with.

I'm looking at how to combine C++ and SFML so I can get a basic game running, so the obvious stuff like health or gold go up or down depending on what happens to the game. I understand the C++ code and I understand the SFML code for both of these functions but I'm of course running into problems combining the two because I'm not sure where to put it. I'm also getting these situations where the C++ code isn't liking my SFML objects very much and I suspect that's going to be because the two will be in conflict with each other somewhere so I need to experiment and find out what works.


#include <Iostream>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>

int main ()
{

    sf::RenderWindow window(sf::VideoMode( 800, 600 ), "C++ Programming Test");

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

    lineone.setFont( arial );
    lineone.setCharacterSize ( 12 );
    lineone.setPosition ( 340, 280 );





    while (window.isOpen())
    {
        {
            using namespace std;
            int numberinput;
            lineone.draw ( "Type in a number: " )
            cin>> numberinput;
            cin.ignore();
            if ( numberinput < 10 ){
            lineone.draw ( "Death, you entered too low a number" );


            }
        cin.get();
        }

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


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

        window.clear();
        window.draw ( lineone );
        window.display();

    }

    return 0;

}

I'm starting off with the basic C++ functions and combining them with the SFML text, I'm aware you could use SFML TextEntered but I want to be able to use all of my C++ functions rather than just a couple and getting stumped when I try to do something different.
« Last Edit: June 21, 2013, 05:23:59 am by Lethn »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Lethn Questions Thread
« Reply #1 on: June 20, 2013, 08:59:52 pm »
I understand the C++ code and I understand the SFML code for both of these functions but I'm of course running into problems combining the two because I'm not sure where to put it. I'm also getting these situations where the C++ code isn't liking my SFML objects very much and I suspect that's going to be because the two will be in conflict with each other somewhere so I need to experiment and find out what works.
You're speaking as if C++ and SFML were two separate things, and that's probably the source of your error. You can't really learn C++ and SFML in parallel, since SFML is pure C++. Do you have a good book to read about C++? I strongly recommend not to use any tutorials, especially not YouTube videos, to learn the programming language. They're mostly badly written and don't cover important details. Experimenting is a very bad idea too; C++ contains enough pitfalls to make this approach a nightmare. You should really invest some time into learning C++, you won't get around it anyway if you plan to develop seriously ;)

Also, you should definitely describe your problem more clearly. "Not sure where to put it" or "they don't like each other" doesn't tell us about the problem, so we have to look at the code (which should be in code tags and properly indented) and guess.

Why do you mix standard input and SFML rendering in the same loop? std::cin blocks the thread and you can't render and poll input regularly. Also, you should include lower-case <iostream>, and you certainly don't need <OpenGL.hpp>.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn Questions Thread
« Reply #2 on: June 21, 2013, 04:54:03 am »
You've partially answered my question already, I've fixed the code thanks to your critique and the if statement I've written works fine, my problem is that I just haven't gotten it to work in the GUI window as text, I can only get it to work in the console. What I need is to get things like basic if statements like this working in the actual GUI window as SFML text rather than just a console application and your point about C++ and SFML got me thinking again because yes there's C++ already in it so I guess I just need to think about it more.

The way I've been learning is using the documentation from SFML, cprogramming tutorials http://www.cprogramming.com/tutorial/c++-tutorial.html and of course searching through the internet to see if my questions have already been answered ages ago. If you can recommend me a good C++ book that's up to date that would be nice, the problem is a lot of the books I've seen are even more outdated sometimes than the tutorials so it's pretty unhelpful for a newbie like me I think so you may as well give that sort of thing to someone who already knows the code.

The reason as well I'm not sure about using the SFML text input is of course many games use statistics and numbers so I'm not sure that command could cope with having numbers update themselves constantly unless I'm missing something.

Here's the updated code:

#include <iostream>
#include <SFML/Graphics.hpp>
int main ()
{

    sf::RenderWindow window(sf::VideoMode( 800, 600 ), "C++ Programming Test");

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

    lineone.setFont( arial );
    lineone.setCharacterSize ( 12 );
    lineone.setPosition ( 340, 280 );





    while (window.isOpen())
    {
        {
            using namespace std;
            int numberinput;
            cout<< ( "Type in a number: " );
            cin>> numberinput;
            cin.ignore();
            if ( numberinput < 10 ){
            cout<< ( "Death, you entered too low a number" );


            }

        }

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


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

        window.clear();
        window.draw ( lineone );
        window.display();

    }

    return 0;

}
 

Note: I'm aware of what the OpenGL.hpp does, it enables OpenGL but I guess I'll keep it simple now and save that for later :D
« Last Edit: June 21, 2013, 04:57:46 am by Lethn »

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Lethn Questions Thread
« Reply #3 on: June 21, 2013, 04:58:58 am »
lineone.setString("Blah blah blah this is a string");

to actually set what you string says.

and
window.draw(lineone);

to draw it.

But you really need to find a good c++ book to get the general ideas of program flow, control statements and scope nailed down.  You have randome {  } brackets in your 'while(window.isOpen())' loop and half of your code that should be in said loop isn't.
« Last Edit: June 21, 2013, 05:01:57 am by The Hatchet »

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn Questions Thread
« Reply #4 on: June 21, 2013, 05:00:20 am »
So you'd put setString where cout would normally be? That would explain where I've been going wrong, I was doing the opposite lol :D

#include <iostream>
#include <SFML/Graphics.hpp>
int main ()
{

    sf::RenderWindow window(sf::VideoMode( 800, 600 ), "C++ Programming Test");

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

    line1.setFont( arial );
    line1.setCharacterSize ( 12 );
    line1.setPosition ( 340, 180 );

    sf::Text line2;

    line2.setFont ( arial );
    line2.setCharacterSize ( 12 );
    line2.setPosition ( 240, 180 );






    while (window.isOpen())

                {
            using namespace std;
            int numberinput;
            line1.setString ( "Enter a number:" );
            cin>> numberinput;
            cin.ignore();
            if ( numberinput < 10 ){
                    line2.setString  ( "Death! You entered in too low a number!/n" );


            }
        }
    {

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


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

        window.clear();
        window.draw ( line1 );
        window.display();

    }

    return 0;

}
 

The code is compiling now but for some reason it just crashes, will keep working away at it.
« Last Edit: June 21, 2013, 05:31:50 am by Lethn »

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Lethn's Programming Questions Thread
« Reply #5 on: June 21, 2013, 06:46:55 am »
Because I got lost in your ill formated code and was wondering if you had {} misplaced I cleaned your code up and only moved your code around and added 1 line i think to make it work fine.  Please take this as one of many examples of better formed code and try to get your hands on an actual book to learn core C++ practices and principles. 

Like others have said around this board and through my own explorations, online tutorials SUCK.  This site is one of the better formed that has actual working examples that are formed well and easily works with a little tweaking if there is something off about them *cough*'official' build and code::blocks default mingw compiler*cough*

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

int main (int argc, char** argv)
{
    sf::RenderWindow window(sf::VideoMode( 800, 600 ), "C++ Programming Test");

    sf::Font arial;
    if ( !arial.loadFromFile ( "arial.ttf" ) ){
                std::cout << "Font failed to load" << std::endl;
        }
    sf::Text line1;

    line1.setFont( arial );
    line1.setCharacterSize ( 12 );
    line1.setPosition ( 340, 180 );

    sf::Text line2;

    line2.setFont ( arial );
    line2.setCharacterSize ( 12 );
    line2.setPosition ( 240, 180 );

    while (window.isOpen()){
                //using namespace std;  
                /*a better habit to get into is NOT using namespaces,
                that way you access each library specifically and know
                who you are accessing.
                */

                int numberinput;
                line1.setString ( "Enter a number:" );
                /*
                because cin halts the thread on it we can't refocus our window when
                the console gets called forward.  Also we don't draw our screen
                for the first time until after this halt.  One should either do
                an initial draw, or figure a way to loop it so that the initial
                drawing does get done.
                */

                std::cin>> numberinput;  //added std:: library accessor to these calls
                std::cin.ignore();  //<-does this do what you think it does?

                if ( numberinput < 10 ){
                        line2.setString  ( "Death! You entered in too low a number!/n" );
                }

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

                window.clear();
                window.draw ( line1 );
                window.draw(line2);//add ones line2 to be drawn
                window.display();
        }
        return 0;
}
 

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #6 on: June 21, 2013, 07:42:13 am »
Thanks Hatchet, I'll be sure to have a look around for a good, up to date C++ book, to be honest, it's a wonder I've even gotten this far because even as a newbie I've seen some god awful tutorials out there, they're either completely out of date or the writer either forgets outright to explain basic things, I still don't know much about formatting and indentation, I tend to just focus on getting the code to function for now.

Just a quick question, with the int main (int argc, char** argv can that be used in SFML as well then? I've seen that on SDL tutorials so I assume that must be a C++ function if it's in both languages.
« Last Edit: June 21, 2013, 07:45:00 am by Lethn »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Lethn's Programming Questions Thread
« Reply #7 on: June 21, 2013, 08:40:10 am »
You only need this signature of main() if you parse the command line arguments. Otherwise, just write
int main()

About books, be careful; there is a lot of terrible literature about C++. See here for a good guide.
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 #8 on: June 21, 2013, 05:47:44 pm »
Ah, that's interesting, I copy and pasted your code hatchet just to see if it was me and I found the same window freeze happens, are you by any chance using a different compiler to me? I use codeblocks.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Lethn's Programming Questions Thread
« Reply #9 on: June 21, 2013, 06:50:03 pm »
Ah, that's interesting, I copy and pasted your code hatchet just to see if it was me and I found the same window freeze happens, are you by any chance using a different compiler to me?
Again, this is supposed to happen. It has nothing to do with the compiler.

Please read our posts carefully, I mentioned it already in the very beginning, and The Hatchet wrote it directly in his code:
Why do you mix standard input and SFML rendering in the same loop? std::cin blocks the thread and you can't render and poll input regularly.
                /*
                because cin halts the thread on it we can't refocus our window when
                the console gets called forward.  Also we don't draw our screen
                for the first time until after this halt.  One should either do
                an initial draw, or figure a way to loop it so that the initial
                drawing does get done.
                */

Furthermore, there is no problem if you open new threads for new problems. In fact, it is strongly encouraged to do so.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Lethn's Programming Questions Thread
« Reply #10 on: June 21, 2013, 08:56:02 pm »
That 'freeze' will always happen whenever you call 'std::cin', no matter what, no way around it, that's what happens.  If you don't want your windows freezing out of focus then learn to handle sf::Event and add code for Keyboard Events and get rid of std::cin.

Lethn

  • Full Member
  • ***
  • Posts: 133
    • View Profile
Re: Lethn's Programming Questions Thread
« Reply #11 on: June 21, 2013, 09:44:20 pm »
Well that's weird, I don't know why I didn't get this before, maybe it's because I had a break and did something else for a bit :D so like you say, the std::cin stuff is console and so replacing that with SFML code will let it run.

This is why I keep treating SFML and C++ differently, because it seems to me from a newbie perspective that some of the basic C++ is messing with the SFML code because SFML seems to act more like an addon library/mod for the basic C++ than C++ itself or am I thinking about this the wrong way?

By the way, this sort of thing is why I think it would be better to keep everything in one thread, I tend to get very detailed with my questions so the discussions can go on a lot :P you'd just end up with several Lethn threads being bumped on the front page.

Edit: Oh and I'm looking at the TextEntered Event on SFML and it looks a lot more flexible than I realised, I think I'll check out the syntax some more.
« Last Edit: June 21, 2013, 09:58:41 pm by Lethn »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Lethn's Programming Questions Thread
« Reply #12 on: June 21, 2013, 11:44:21 pm »
This is why I keep treating SFML and C++ differently, because it seems to me from a newbie perspective that some of the basic C++ is messing with the SFML code because SFML seems to act more like an addon library/mod for the basic C++ than C++ itself or am I thinking about this the wrong way?
Yes, you're thinking the wrong way. Don't put everything in philosophical context, the reason why this does not work is trivial. It has nothing to do with SFML in specific, or "libraries messing with C++".

If you read the tutorial carefully (which you should finally do!), you'll see that SFML (in fact, every graphical library) relies on constantly updating the main loop. This includes user input and rendering. std::cin interrupts this update, because it waits for a input in the console -- therefore, the window doesn't behave properly.

By the way, this sort of thing is why I think it would be better to keep everything in one thread, I tend to get very detailed with my questions so the discussions can go on a lot :P you'd just end up with several Lethn threads being bumped on the front page.
If there are different topics, make different threads. Very similar questions can of course be gathered in one thread.
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 #13 on: June 22, 2013, 03:00:34 pm »
Just thought I'd say, yes it does look like it's a fairly simple issue Nexus, I was overthinking it. When I removed the console stuff the code works fine my only issue is now that I need to make SFML work like the console stuff and get some input going.

This goes back to another one of my issues where I'm thinking about how to implement a gold statistics or an experience point statistic, SFML should be able to handle updating loops shouldn't it? I already know from me experimenting with input that it can handle if functions easily.

Here's the sort of fixed code, I'll look in how to implement the if statement I was after properly, I think I already have something in mind, you lot helped me understand what was possible and what wasn't so thanks for that :D

#include <iostream>
#include <SFML/Graphics.hpp>
int main ()
{

    sf::RenderWindow window(sf::VideoMode( 800, 600 ), "C++ Programming Test");

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

    line1.setFont( arial );
    line1.setCharacterSize ( 12 );
    line1.setPosition ( 340, 180 );
    line1.setString ( "Enter a number:" );

    sf::Text line2;

    line2.setFont ( arial );
    line2.setCharacterSize ( 12 );
    line2.setPosition ( 240, 80 );
    line2.setString  ( "Death! You entered in too low a number!" );






    while (window.isOpen())
    {

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


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

        window.clear();
        window.draw ( line1 );
        window.draw ( line2 );
        window.display();

    }

    return 0;

}
 
« Last Edit: June 22, 2013, 03:04:00 pm by Lethn »

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Lethn's Programming Questions Thread
« Reply #14 on: June 22, 2013, 03:06:42 pm »
If you want to read text from the user, have a look at the TextEntered event in the API documentation and the event tutorial. You should be able to gather the necessary informations yourself, in case of specific questions you can ask again.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything