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

Author Topic: Questions After Reading Tutorials  (Read 1877 times)

0 Members and 1 Guest are viewing this topic.

addel

  • Newbie
  • *
  • Posts: 17
    • View Profile
Questions After Reading Tutorials
« on: June 20, 2013, 07:26:25 pm »
Hi, i have been learning C++ and SFML and have been wondering a few things.

1) In the event tutorials the code
sf::Event event;
gets placed inside of the
while (window.isOpen()) {
loop but wouldn't this mean is is created many times? I would have expected it to get added after the RenderWindow was made at the top.

2) Also in the examples it uses sf:: but can this be removed with a "using namespace" so something like
sf::Text text("Hi", font);
would then be
Text text("Hi", font);
When i have used this with some simple examples for std:: i think doing that made the code seem simpler. Does it remain in the examples to help avoid name conflicts or something?

3) I notice that with C++ i can't just combine a string and int ("Hello" + 123 etc) which is simple with many other programming languages, i figured out the solution by using ostringstream however it then made what is normally a 1 line of code into about 4 instead.

For example i was originally expecting this type of code to work -

// Mouse wheel moved
        if (event.type == sf::Event::MouseWheelMoved) {
            text.setString("Mouse wheel moved" + event.mouseWheel.delta);
        }

but it seems to instead cut off letters like it's a substring.

So i fix it with -

if (event.type == sf::Event::MouseWheelMoved) {
            ostringstream d; d << event.mouseWheel.delta;
            text.setString("Mouse wheel moved " + d.str());
        }

So for more complex things you then need to use more code. I looked in the string class reference but didn't notice anything, does SMFL have any simple way/function to do this?

Thanks
« Last Edit: June 20, 2013, 07:38:29 pm by addel »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Questions After Reading Tutorials
« Reply #1 on: June 20, 2013, 07:47:34 pm »
1. There's no "creation". sf::Event is a trivial union with no constructor, declared on the stack. Creating such an instance is a no-op.

Generally speaking, it's better to create objects only where you need them, because:
- sometimes they have constructors with parameters, and these parameters may not be known in advance
- it improves the readability of the code
- it avoids to pollute the function, by keeping variables in small scopes

2. It's a matter of taste. But think about it: namespaces do not exist for people to cancel them everytime with "using namespace xxx" ;) They improve readability, by explicitly showing which library a function/class belongs to.

3. C++11 has the to_string function. Otherwise it's easy to create, just put your stringstream code into a template function.

template <typename T>
std::string to_string(T value)
{
    std::ostringstream oss;
    oss << value;
    return oss.str();
}

...

text.setString("Mouse wheel moved" + to_string(event.mouseWheel.delta));
Laurent Gomila - SFML developer

addel

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Questions After Reading Tutorials
« Reply #2 on: June 20, 2013, 08:03:17 pm »
Thanks for the help Laurent, this answers everything i had been wondering recently. SFML and the community here is great :D

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Questions After Reading Tutorials
« Reply #3 on: June 20, 2013, 08:50:14 pm »
Note that even if sf::Event had a constructor, constructing it inside the loop would be the better practice. On one side, the event needs to be filled every loop with the new events, possibly multiple times. Since this involves OS functions, you can assume that it takes long enough to make the initialization of a simple structure irrelevant. Not to mention all the other operations like rendering and game logic that occur in each iteration.

Anyway, please don't write bad code because of performance myths you've heard somewhere. These are micro-optimizations, nobody will notice if it takes 1-2 cycles longer. On the contrary, your declaration is not local anymore, which takes longer to find it and is more prone to errors (like using the event accidentally outside the loop). In such cases, you should always choose the easy way. If there should show up a performance problem, you could still investigate it -- but then do it correctly (using a profiler) and not based on assumptions.

Concerning namespaces, I personally write the prefixes almost always, so that I directly see of which library a function or class is. If you consider the identifier too long, you can still use namespace aliases.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything