Posts tagged: boost

Using BOOST_FOREACH for Simpler Iteration

I’m working on a little C++ project at the moment. As part of it I’m trying to learn more about the Boost libraries and do clever things with the STL. I have found myself writing a lot of simple loops that iterate over containers of objects and do simple things like call a method on each object. I get tired of writing iterator code like this, it’s something we end up doing a lot as C++ programmers. The syntax for defining and using iterators can be a little long and it can be a right pain to debug if you make a simple typo.

Enter the BOOST_FOREACH macro. I am aware of the STL for_each() function that uses some fancy binding objects to do all sorts of clever stuff. Boost Bindings extend this and create a really powerful set of tools. The problem with this stuff is that it looks a bit weird and isn’t that easy to understand. While I was reading tutorials I came across BOOST_FOREACH. This simple macro gives us something very similar to the foreach keyword in C#.

Imagine you have a container of objects of class Foo, and you want to call the Bar method on each instance of Foo. Usually I’d write something like this:-

for( vector<Foo>::iterator f = container.begin();
                          f != container.end(); f++ )
{
    f->Bar();
}

The BOOST_FOREACH macro can cut out some of the donkey work by doing all the iterator stuff for us. So here’s the above example using BOOST_FOREACH.

BOOST_FOREACH( Foo &f, container )
{
    f.Bar();
}

I like this alternative because it’s a lot cleaner than the classic version and it will work the same with both STL containers and standard C arrays. You also end up typing less and so inevitably will make less mistakes. It’s not going to work in all circumstances, any code where you want to manipulate the container itself is probably a bit risky, but it makes doing simple stuff a bit easier and leads to better code without having to get bogged down with templates and std::for_each.

Game Programming with C++ and Gosu

I’ve always had a desire to make computer games. It obviously stems from my misspent youth playing Nintendo and getting a serious case of SAD. Sadly, I’ve grown up now and spend my days writing code instead of playing games. I’ve not realised my dreams of becoming a professional games designer but it’s something I’d like to do as a hobby.

I’ve been doing a fair bit of hunting round to find a decent set of tools to get me started. Coding everything from scratch doesn’t really appeal to me so I want something that does all the donkey work of setting up windows, graphics devices, sound and controller input, without being a full blown Game Engine. I also don’t fancy learning another programming language, so ideally it will be a C++ library. It must also be cross-platform as I use both Mac and PC. During my search I’ve looked at SDL, Allegro and Gosu and have decided that Gosu is definitely the way to go.

Gosu is pure object-oriented C++ goodness (Ruby bindings are also available), the API is neat as it is made up of only a small collection of classes. It is primarily for 2D games but it has support for mixing in OpenGL if you are brave enough to do some 3D. It is built on top of the Boost libraries (See my previous post on Installing Boost), so if you need advanced stuff like threads you can get those without too much hassle. It’s also cross-platform as there are binaries for Windows, Mac OS X and Linux. One of the real bonuses is that it is actively being developed and has some fairly comprehensive online documentation.

The getting started guides on the Wiki are great, and walk you through all the basics from installing and setting up a project, to writing a simple game. After I’d got everything installed properly it took me about an hour to code up a really simple Snake game clone from scratch. I’m going to play with it some more and see if I can knock up something a bit more advanced. Next stop Space Invaders!

Image | WordPress Themes