Archive
sf::Window::IsOpen()
Previously, we always checked if our SFML window was open using sf::Window::IsOpened(), which returned bool. Well, the function has recently been renamed to IsOpen(), although it appears otherwise unchanged (externally at least). So bear in mind that if you’re using a very up to date SFML snapshot, you may need to use sf::Window::IsOpen() rather than the old sf::Window::IsOpened().
Related articles
- SFML 2.0 Builds (sfmlcoder.wordpress.com)
Singletons
Here’s another little something to keep you entertained while you’re waiting for things to get underway here again (it’ll happen eventually, I promise!).
Migrating to SFML 2.0
Many people have difficulty migrating from SFML 1.6 to 2.0. The fact is there are quite a few public interface changes and there are as yet no SFML 2.0 tutorials. I suggest you still use the 1.6 tutorials if you want to be guided through the learning process, and then make up the gap using either or both of the following
- the SFML 2.0 documentation (note that the documentation on the site is also slightly out of date, but you can create your own using the source and Doxygen)
- this thread delineating the changes in SFML 2.0
Overloading Template Functions
Another little tip inspired by a question on cplusplus.com. How do we overload template functions? Well the answer is that we overload the template function in the same way as we would overload any other function.
template <class A> void foo()
{
}
template <class A> void foo(float f)
{
}
We do, however, have to be slightly more careful with specialised functions. If we overload a specialised function, the overload must match some overloaded version of the template.
template <class A> void foo()
{
}
template <class A> void foo(float f)
{
}
template<> void foo<int>() // allowed: matches the first template
{
}
template<> void foo<int>(float f) // allowed: matches the seconds template
{
}
template<> void foo<int>(const char*) // not allowed: does not match any template
{
}
C++ Tip – this
You probably all know about the this pointer already, but I just answered a question about it on cplusplus.com, so I thought that I may as well post here while it’s at the forefront of my mind!
So this is a keyword which we can use inside a member function. It is a pointer to the instance of the class from which the member function was called. Still not sure? Check out the following example program.
#include <iostream>
class foo {
public:
void bar() { std::cout << this << std::endl; }
};
int main()
{
using namespace std;
foo foo1, foo2;
cout << "foo1\n" << &foo1 << endl;
foo1.bar();
cout << "foo2\n" << &foo2 << endl;
foo2.bar();
return 0;
}
This program yielded the following output for me.
foo1 0031F916 0031F916 foo2 0031F917 0031F917
For you, the actual numerical values of the addresses may be different, but the point is that the address of foo1 is the same as the value printed by foo1.bar() (which prints the this pointer) and the address of foo2 is the same as the value printed by foo2.bar().
So, if you couldn’t before, you can now hopefully understand what I mean when I say that the this pointer points to the instance of the class from which the member function was called.
Interesting Point: Notice that the address of foo1 is only 1 behind the address of foo2. We created the two variables on the same line as stack variables, so they are in adjacent memory locations. However, as the classes are effectively empty, the memory locations are in fact even consecutive (each instance takes only one space on the stack).
Notes
- Remember that this may only be used from inside member functions (think about it – it wouldn’t make much sense anywhere else)
- this cannot be used in a constructor’s initializer list, but it can be used in its body
- this can be used in a destructor’s body, but be careful as if the class is being destroyed you probably don’t want to go around handing its address out to other functions and variables
More on Suffix Return Types
Previously I discusses suffix return types, and I provided an example of how it could simplify notation. This:
Icanos::System::String Icanos::System::FormattedLog::ApplyIndent(const String&);
became this, which I’m sure you’ll agree is tidier:
auto Icanos::System::FormattedLog::ApplyIndent(const String&) -> String;
However, there is another application which is perhaps more relevant. The above example is a matter of scope: String is not in scope before the function’s name, but it is after, so we don’t have to qualify it fully. But the following is also to do with scope.
Consider a C++ template function to find the quotient of two values (yes, I know it’s contrived…). It’s return type depends on the template types passed to it. Indeed, it’s return type is decltype(s / t) where s and t are the values to be divided. But we can’t put this as s and t aren’t in scope before the parameter list. They are, however, in scope after it.
template <class S, class T>
auto quotient(S s, T t) -> decltype(s / t)
{
return s/t;
}
And there we are. It works beautifully! Just as a contrast, here is how Dr. Stroustrup says you would have to do it with C++03/98 syntax. As he points out, it is much more messy.
template <class S, class T>
declytype(*(S*)(0) / *(T*)(0)) quotient(S s, T t)
{
return s/t;
}
While s and t aren’t in scope before the parameter list, S and T (the types themselves) are in scope. Thus we can cast 0 to a null pointer to S and a null pointer to T, and then dereference it. This wouldn’t be acceptable at runtime, but it works as decltype is evaluated at compile time. Though it works, the former solution is clearly nicer.
Read more about it on his FAQ.
C++0x Declared Types
Sometimes you might want to know the type of an expression. At runtime you can use RTTI (runtime type information) and typeid. But what about compile time? You can use typeof in some cases, but that is a nonstandard extension. C++0x provides a new keyword decltype which adds this functionality to the standard.
decltype stands for declared type and you can read more about it on Dr. Stroustrup’s FAQ. In summary though, if e is an expression, declytype(e) represents the type of the expression. So decltype(4) would mean int and decltype(4.0f) would mean float. Naturally this can be extended to user types.
Note that decltype is supported by Microsoft Visual C++, Intel Composer XE and GCC/MinGW.
class Blog {
// etc
};
Blog SFMLCoder;
decltype(SFMLCoder) // <--- this means 'Blog'
Read on for examples! Read more…
Windows Setup Files
When it comes to distribution, it is obviously advantageous to be able to use setup files to distribute on Windows. You can automate the install process, and also package any necessary redistributable files and dlls (license permitting) in a single file. But how do we make such setup files?
Well here are the links to two options: InnoSetup and NSIS. Both are free (check their licenses for details). In the past I have used InnoSetup.
It has a wizard which makes the creation of a generic installer very quick and easy – that includes readme and license files, installation of multiple files, password protection and control of shortcut creation. However, you can also write scripts directly, providing a greater degree of control over the generated installer.
A Choice of Editors
I know that some of you may prefer to use an editor and a toolset (compiler, linker, etc) from the command line, rather than a full featured IDE. I have already posted about some different compilers, but here – at last – is a post about editors.
So the obvious question is which IDE should I use? Here I provide a few suggestions of IDEs which I feel are good choices.
- Notepad++: This is a nice editor which I have used in the past. It is, however, Windows only.
- ConText: This, in fact, is my favourite editor. I use it extensively, in particular for editing game XML files for modding. It, too, is only for Windows; there is also a portable version available.
- Vim: This editor is available on most major platforms, and is highly configurable.
- Geany: This is in fact an IDE and not an editor, but as it is considered ‘light weight’, I have included it here and not with the other IDEs.
- Emacs: Another cross platform and customisable editor. There are extensions available, to add further functionality, such as a calendar
If you want to add your favourite editor to the list, just comment to let me know. And vote on your personal choice here.
A Choice of Compilers
As C++ programmers, the one of our most important tools is the compiler. So which compiler should you use. Well there are several good choices, which I shall list here.
- Microsoft Visual C++: This is a great compiler from Microsoft – which comes with Visual Studio and Visual C++ Express. While Visual Studio is rather expensive, you can get Visual C++ Express absolutely free.
- g++: This is the C++ compiler that comes with GCC (the GNU Compiler collection)
- Intel Composer: I mentioned this one in yesterday’s tip of the day. It’s not cheap, but it does have good optimisation features.
Now, there are others, so feel free to comment with your suggestions and opinions. You can also visit my programming software polls page, where you can vote on your favourite IDE and compiler.

