Posts Tagged ‘code snippets’

Supertux, cheating at the source code level

September 28, 2009

Who said that there are no games for Linux?

Other than running games for windows through Wine (as well as nearly any other .exe file), there are many games that run natively on Linux.

I don’t play much, but I’ve personally tried Sauerbraten and Supertux, both of which have been suggested to me by admiral0, a member of the POuL.

Supertux is a nice game, but I’ve found it a bit too difficult to play, especially some levels where there are these flying enemies, identified in this picture:

supertux_cheating

So, to finish this almost-impossible level, I decided to cheat :)

And since I’m a programmer, I thought that the easiest way to cheat was to alter the game’s source code :D It did not take much time to download the 0.3.1 beta release (which was the version I was playing) and find what to modify.

The code that makes these enemies attack from the sky is defined in

src/badguy/zeekling.cpp

and is the should_we_dive() member function of the Zeekling class.

Just replace the function’s body with a

return false;

and recompile. The game becomes a little bit easier, and I successfully completed the level :)

Measuring time durations

September 14, 2009

Today on the poul‘s mailing list someone asked: “How to measure the time spent in a function?”. I replied with the code that I use for that purpose, but later I thought that it was an interesting question, so I’m posting the solution here on the blog.

The solution is to use the boost date_time library. It is simple, portable (works on Linux, Win and Mac) and precise, since the time is printed in microseconds.

Here is an example code that shows how to profile a function:

#include <iostream>
#include <boost/date_time.hpp>

void f()
{
 for(volatile int i=0;i<100000;i++) ;
}

int main()
{
 using namespace std;
 using namespace boost::posix_time;

 ptime t1(microsec_clock::local_time());
 f();
 ptime t2(microsec_clock::local_time());

 cout<<t2-t1<<endl;
}

Just replace f() with the function you want to profile. Of course you can profile the time spent running your entire program by putting the whole program between the declaration of t1 and t2.

The time is printed in this format:

./test
00:00:00.000620

To be able to compile the program, you need to link with the boost_system library. With the g++ compiler you must add -lboost_system when linking.

Of course you need to have the boost libraries installed. On Linux type:

sudo apt-get install libboost-dev libboost-thread-dev

While on Windows, get this compiler that comes with boost precompiled.


Follow

Get every new post delivered to your Inbox.

Join 30 other followers