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.

Topics - Nexus

Pages: 1 2 [3] 4 5 ... 7
General / CMake variable name conventions
« on: July 06, 2012, 09:24:51 pm »

I know that consistency is really a huge pain in CMake, one always has to know many special cases. SFML mainly uses the following variables:
I guess you didn't name it SFML_BUILD_SHARED_LIBS because BUILD_SHARED_LIBS seems to be common variable name. But is this really important enough to injure the SFML_ prefix convention?

And doesn't the CMake community rather use XY_BUILD_DOCS instead of XY_BUILD_DOC?

By the way, the GLEW variables are not marked as advanced, although the other ones are (OpenGL, OpenAL, FreeType, jpeg, sndfile).

SFML projects / Zloxx II - An action Jump'n'Run
« on: July 01, 2012, 01:02:37 pm »
Zloxx II

I would like to present you a bigger project of mine, namely a Jump'n'Run game called "Zloxx II". It has been inspired by various Super Mario titles, some Nifflas games, and probably much more.

Your goal is -- as usual -- to defeat enemies, to collect coins, weapons, power-ups or suits, and to reach the end of each level.

The game is freeware, but currently only works on Windows. More screenshots and a download link can be found on my homepage. Have fun! :)

Window / Rendering on closed window
« on: June 22, 2012, 07:44:54 pm »
The typical SFML example looks like this:
sf::RenderWindow window(...);
sf::Sprite sprite(...);

while (window.isOpen())
        sf::Event event;
        while (window.pollEvent(event))
                if (event.type == sf::Event::Closed)


After the event has fired and the close() function has been called, the three rendering-related functions are still invoked. Earlier this led to OpenGL error messages, now I think the behavior is protected by the sf::RenderTarget::activate() calls. Is this a potential problem, or only an esthetical one?

In the Thor examples, I have always used for (;;) and return 0; to leave the loop directly.

Window / Different framerate in Visual Studio and Explorer
« on: April 30, 2012, 12:07:23 pm »
I have an application which I limit to 40 frames by calling sf::Window::setFramerateLimit(). When I start the application from Visual Studio 2010, it works well, however the .exe started from Windows Explorer seems to run at ~33 FPS, or at ~37 FPS if VSync is enabled. The same result comes up when I manually call sf::Sleep(). With a busy-wait loop I achieve the 40 FPS even outside Visual Studio, but I hate to let the processor core work at capacity if only a few percents are required.

An idea why these different framerates occur? And what's the best approach to fix that?

Audio / loadFromStream() - read() exceeds file size
« on: April 28, 2012, 11:50:32 am »
When I load a sf::SoundBuffer or sf::Music through a stream, read() is passed a size which leads to reading past the end of the stream.

#include <SFML/Audio.hpp>
#include <fstream>
#include <iostream>

class MyStream : public sf::InputStream
                MyStream(const char* filename)
                : stream(filename, std::ios::in | std::ios::binary)

                virtual sf::Int64 read(void* data, sf::Int64 size)
                        std::cout << "Begin      " << tell() << "\n";
                        std::cout << "End        " << tell() + size << "\n";
                        std::cout << "Total size " << getSize() << "\n\n";

                        stream.read(static_cast<char*>(data), size);
                        if (!stream)
                                std::cout << "FAIL\n";

                        return stream.gcount();

                virtual sf::Int64 seek(sf::Int64 position)
                        return tell();

                virtual sf::Int64 tell()
                        return stream.tellg();

                virtual sf::Int64 getSize()
                        sf::Int64 backup = tell();
                        stream.seekg(0, std::ios::end);

                        sf::Int64 size = tell();

                        return size;

                std::ifstream stream;

int main()
        MyStream stream("Music.ogg");

        sf::SoundBuffer buffer;
        bool success = buffer.loadFromStream(stream);

        sf::Sound sound(buffer);
success is false in some cases (for wav, it seemed to work), but there is no SFML error output on the console.

Instead of size, one can write std::min(size, getSize() - tell()), but I suggest to handle this on SFML side if possible. I don't know however how well you can influence the low-level library callbacks, maybe this is given.

In case you cannot reproduce the problem, I'll upload an OGG file.

General / Best practice to find SFML with CMake
« on: April 27, 2012, 07:14:24 pm »

I have experienced that many users have problems to setup Thor correctly because the SFML path is not found. The recent modification to not copy FindSFML.cmake in the CMake module folder anymore makes matters even worse – not only because new users don't find SFML at all, but also because existing users are left with an incorrect FindSFML.cmake (defining SFMLDIR instead of SFML_ROOT for example).

What advice would you, Laurent, give for libraries depending on SFML? Should I copy FindSFML.cmake in my repository and expand CMAKE_MODULE_PATH correspondingly? Actually, that would be an easy solution, but I fear that I overlook updates in this file (but you mentioned they shouldn't happen too often).

Furthermore, to copy FindSFML.cmake to SFML_ROOT during the installation seems somehow questionable, because at this point one has already found SFML ;) But I see you can define the other variables like SFML_LIBRARIES etc.

By the way, you should probably mention those short steps to find SFML in another tutorial, like "Using SFML in other libraries with CMake"... One day, if you have time :)

Graphics / Incorrect pixel mapping at 0.5 (again)
« on: April 15, 2012, 03:52:20 pm »

When using coordinates with a float part of 0.5, it happens that pixels of the texture aren't mapped correctly to the screen. That is, texels outside the sub-rect are rendered, or texels inside are spread over multiple pixels. I have written a reproducing example, where you can move a sprite and see how its appearance changes, although it's moved only in one-pixel steps:


As far as I've observed, it is enough to round coordinates, i.e. making sure that setOrigin() or setPosition() calls don't lead to that coordinates -- for the case where no rotation or scale is involved.

I started some threads about this issue in the past, and I'm not sure if the situation has changed with the new rendering system. Anyway, I just wanted to remind you, in case you want to modify something before the SFML 2 release. I'd recommend to mention this behavior and the workaround at least in the documentation, since the circumstances to cause it are quite common.

My older threads:

SFML projects / Aurora
« on: March 19, 2012, 06:16:23 pm »

Although this project isn't directly related to SFML, I thought some people may find it interesting. Formerly a part of Thor, Aurora is now an autonomous library that provides various C++ functionality which can be useful in different situations.

I know this sounds extremely abstract, so here is an example of a quite intelligent smart pointer:
#include <Aurora/SmartPtr/CopiedPtr.hpp>

namespace sf { class Drawable; } // Forward declaration is enough

struct GameObject
        aur::CopiedPtr<sf::Drawable> drawable;

#include <SFML/Graphics.hpp>

int main()
        GameObject a, b, c;
        a.drawable.reset(new sf::RectangleShape);
        b.drawable.reset(new sf::Sprite); // different derivates
        c = a; // performs a deep copy
} // deletes everything correctly
sf::Drawable is an abstract base class, but we can wrap it into a smart pointer. Like this, GameObject has normal value semantics, i.e. we can copy it as usual, while the derived type (sf::RectangleShape) is copied correctly through the hierarchy. This without a single copy constructor, assignment operator or a virtual clone function.

Another example are the dispatchers (also known as a way to emulate multimethods in C++). Their task is to choose the correct overload of a set of functions, of which the arguments are only known at runtime. This allows you to work with abstract base classes (like Object in the example), while interaction between two objects can still be implemented with the full type information. No need for manual case differentiations.
#include <Aurora/Dispatch.hpp>
#include <iostream>

// Example class hierarchy
class Object { public: virtual ~Object() {} };
class Asteroid : public Object {};
class Ship     : public Object {};

// Functions for collision with different argument types
void collision(Asteroid*, Asteroid*)    { std::cout << "Asteroid-Asteroid\n"; }
void collision(Asteroid*, Ship*)        { std::cout << "Asteroid-Ship\n";     }
void collision(Ship*,     Ship*)        { std::cout << "Ship-Ship\n";         }

int main()
    // Create dispatcher and register functions
    aur::DoubleDispatcher<Object*> dispatcher;
    dispatcher.add<Asteroid, Asteroid>(&collision);
    dispatcher.add<Asteroid, Ship>    (&collision);
    dispatcher.add<Ship,     Ship>    (&collision);

    // Base class pointers (no static type information about derived classes)
    Object* a = new Asteroid;
    Object* s = new Ship;

    // Invoke functions, let dispatcher choose the correct overload
    dispatcher.call(a, s); // Output: "Asteroid-Ship"
    dispatcher.call(a, a); // Output: "Asteroid-Asteroid"
    dispatcher.call(s, s); // Output: "Ship-Ship"
    delete a;
    delete s;

Aurora uses the zlib/libpng license. Since it is a header-only library, it requires no build process and can thus be very easily be used in your projects.

Aurora project homepage
GitHub page

SFML projects / Thor 2.0 released!
« on: March 19, 2012, 05:40:54 pm »

Thor 2.0 released!

Thor is an open-source and cross-platform C++ library. It extends the multimedia library SFML with higher-level features such as:
  • Animations
  • Particle systems
  • Resource management
  • Time measurement utilities
  • Event handling and callbacks
  • Delaunay triangulation
  • Color gradients
  • Vector algebra
  • ...

(click to show/hide)

General discussions / Some last remarks
« on: March 16, 2012, 07:51:04 pm »
Since SFML 2 stands short before its release, I thought I could mention some smaller issues and name inconsistencies as long as they can still be fixed. I don't expect you to apply all suggestions, but maybe there are some in my list you find appropriate ;)

  • Thread::Thread((void(C::*function)(), C *object)
    The object parameter may not be NULL, thus it should be a reference, conforming to the general SFML style.

  • View::reset(const FloatRect& rectangle);
    The name suggests that this function has the same semantics as calling the constructor with this signature, however the viewport remains unchanged. setRect() might be an alternative.

  • VertexArray::VertexArray(PrimitiveType type, unsigned int vertexCount = 0)

Lock::Lock(Mutex& mutex)
Sound::Sound(const SoundBuffer& buffer)
Ftp::Response::Response(Status code = InvalidResponse, const std::string& message = "")[/b]
You might want to make the constructors explicit. On the other side, I assume the implicitness of sf::RenderStates constructors is on purpose to allow shortcuts.

  • Packet, InputStream, resource classes
    Some work with void* and some with char* for binary data.

  • Window, Image, Texture
    Vector2u getSize() vs. unsigned int getWidth()/getHeight()

  • Vector2f RenderTarget::convertCoords (unsigned int x, unsigned int y) const
    Considering the fact that most functions return vectors (especially Mouse::getPosition() which is often used in combination), it could be wiser to take a sf::Vector2u/i.

General discussions / Do you use C++11?
« on: March 10, 2012, 01:58:16 pm »

Please vote only if you program in C++ (not C or other languages).

I am interested if you make use of basic C++11 features that have already been implemented in compilers like VC 2010 or g++ 4.6.

This concerns mainly these language features:
  • RValue references
  • Lambda expressions
  • Type inference (auto, decltype)
  • Static assertions (static_assert)
  • Null pointer literal (nullptr)
And new parts of the standard library, for example:
  • Memory (std::shared_ptr, std::unique_ptr)
  • Functional (std::function, std::bind())
  • Containers (std::array, std::forward_list, std::unordered_set/map)
  • Algorithms (std::copy_if(), std::move(), std::all_of(), ...)
  • String Conversion (std::to_string(), std::stoi(), ...)
  • Tuples (std::tuple)
  • Type Traits
  • Random
  • Regex

Graphics / sf::Text::GetLocalBounds()
« on: March 02, 2012, 09:04:57 pm »
Not sure if this is better suited in feature requests. Actually, the topic doesn't concern a new feature, so I put it here. And since it isn't only connected to the whitespace problem, I think it deserves an own thread.

It would be nice if sf::Text::GetLocalBounds() behaved more intuitively. At the moment, the returned height is different depending on the characters in the text (e.g. 'y' is higher than 'a'). While this seems standing to reason, it makes simple use cases like aligning multiple texts to an edge impossible. Additionally, spaces at the end are not considered in the size calculation.

A proposal to compute the size in local coordinates:
sf::Vector2f GetLocalSize(const sf::Text& text)
        const sf::String str = text.GetString() + '\n';

        float maxLineWidth = 0.f;
        float lineWidth = 0.f;
        unsigned int lines = 0;

        for (sf::String::ConstIterator itr = str.Begin(); itr != str.End(); ++itr)
                if (*itr == '\n')
                        maxLineWidth = std::max(maxLineWidth, lineWidth);
                        lineWidth = 0.f;
                        lineWidth += text.GetFont().GetGlyph(*itr, text.GetCharacterSize(), text.GetStyle() & sf::Text::Bold).Advance;

        const float lineHeight = static_cast<float>(text.GetFont().GetLineSpacing(text.GetCharacterSize()));
        return sf::Vector2f(maxLineWidth, lines * lineHeight);
Some operations might be expensive, however a sf::Text could calculate them upon first call and batch them if necessary. And maybe it is appropriate to expand the bounding rect a little bit on the side and to move it towards the bottom, since it exceeds the text at the top. Or just decrease the height at the top ;)

A simple test application:
void DrawText(sf::RenderWindow& window, const char* str, float x, float y)
        sf::Text text(str);
        text.SetPosition(x, y);

        sf::Vector2f size = GetLocalSize(text);

        sf::RectangleShape shape(size);


int main()
        sf::RenderWindow window(sf::VideoMode(640, 480), "SFML Application");

        while (window.IsOpen())
                sf::Event event;
                while (window.PollEvent(event))
                        if (event.Type == sf::Event::KeyPressed || event.Type == sf::Event::Closed)
                                return 0;


                DrawText(window, "hello", 10.f, 10.f);
                DrawText(window, "hello ", 10.f, 60.f);
                DrawText(window, "hello d", 10.f, 110.f);

                DrawText(window, "contains some spaces", 150.f, 10.f);
                DrawText(window, "and a tab\t", 150.f, 60.f);

                DrawText(window, "multiline\ntext", 10.f, 200.f);
                DrawText(window, "text\nmultiline", 150.f, 200.f);

                DrawText(window, "some\nreally long text\neven\nspread across\nmultiple lines.", 350.f, 200.f);


Graphics / sf::Transform::Combine() result
« on: February 18, 2012, 01:27:54 pm »
The function sf::Transform::Combine() is a little bit misleading in my opinion. It makes the impression as if the result is stored in *this, and not returned as an independent copy. Additionally, the documentation for Translate() says "Combine the current transform with a translation", where the term "combine" is used with semantics different from the Combine() method.

Other functions returning copies like GetInverse() are also named differently, maybe GetCombination()/GetCombined() would be more intuitive. Or you only provide Combine() which changes *this. Or a global function. Or you don't provide it at all, as the same functionality is already covered by operator* and operator*=.

I don't know what other users think about this, maybe I'm the only one. It was just something that took my attention.

SFML website / Documentation mistakes
« on: January 21, 2012, 11:41:38 am »
Hello, I suggest to collect errors spotted in the documentation, tutorials or other parts of the website in this thread, so that we don't have to create new threads or even GitHub issues.

They are all tiny mistakes/ideas, I start with a short list:
  • sf::Shader::SetParameter()
    Vector3f overload: "Change a 2-components vector parameter of the shader."

  • sf::InputStream::Seek()
    "seeked" -> "sought"

  • sf::ThreadLocalPtr
    Two times void Thread1(void*) in example code

  • sf::Image::GetPixelsPtr()
    "the returned pointer may become invalid if you modify the image, so you should never store it for too long."
    Single pixels are allowed to change, and only resize/reload is a problem, right?

Window / Removed sf::Window::GetFrameTime()?
« on: January 21, 2012, 10:58:24 am »
What has happened to GetFrameTime()? Do you prefer it time is measured on user side? It looks like sf::Window already uses a clock for SetFramerateLimit(), doesn't it?

Pages: 1 2 [3] 4 5 ... 7