At the right time!

17.04.2011 02:44 in tools

I think that the source of many problems is doing something at the wrong moment. C++ is quite flexible, but a lot of its features can be accessed only in very awkward way. For example there is a limited support of compile-time evaluation, but accessible only with abusive use of templates*. There is no built-in reflection/serialization system — but again, people use templates and other language features to simulate it. Given enough macrodefinitions (and possibly Boost), everything is possible…

But this is so wrong. The key to success is using right tools at the right time. Just few ideas:

  1. If doing X in some language/tool is difficult, outsource it. Maybe other languages/tools are more suitable to do it.
  2. If you can isolate X in separate module/application, do it. Unix is all about small task-oriented application communicating over simple interfaces — and after 40 years it’s still doing great!
  3. Pre-processing and post-processing is not only about lighting and shading. They can be applied to any kind of data, including code.
  4. Auto-generated code is cool (as long as you control its generation).
  5. Have flexible build system. Be able to plug any kind of processing into it.
  6. Keep things simple!

And now, few stories and one longer example. When I started building my engine/pipeline in early 2009 I had many questions to be answered. One of them was game [engine] and editor communication. For some reason I decided to use C++ for all game code but C# for the editor**. I needed communication between those two — that includes sending some objects in both directions. C# features rich reflection/serialization support, but C++ is far more inferior about those. However, instead of using some template/#define trickery, I just made a simple application that generated both C++ and C# classes with all serialization needed from simple classes description in JSON-like language. Generated C++ code was extremely simple — no templates/macros/virtuals. Can I call it a win?

Once I was working on (non-gamedev) application that was using REST-like web services. How can you access web methods in your app? Some languages/frameworks (like C#/.NET) feature built-in support for those. For other toolsets the best solution could be similar to .NET — have a generator that creates code for all requests. And having custom generator is neat, because you’re not limited to one specific data/services descriptions (like WSDL/SOAP for .NET***).

Finally, live example. But first let me present a disclaimer: this is not a post about implementing properties in C++. Implementation is rough and may make your HDD and/or eyeballs explode. Thank you.

So it’s about properties. I mean C#-style properties. Imagine such code:

class Square
{
private:
  float _edge;
public:
  @property float edge
  {
    get
    {
      return _edge;
    }
    set
    {
      _edge = value;
    }
  }

  @property float area
  {
    get
    {
      return _edge * _edge;
    }
    set
    {
      _edge = sqrtf(value);
    }
  }
};
void print(float edge, float area)
{
  printf("edge = %f, area = %f\n", edge, area);
}
int main()
{
  Square s;
  s.area = 25;
  print(s.edge, s.area);
  s.edge = 3;
  print(s.edge, s.area);
  return 0;
}

to be valid C++. Well, using code pre-processing it’s possible.

Using this program: http://gist.github.com/923509 you can transform this “C++” into real C++: http://gist.github.com/923634

Results?

edge = 5.000000, area = 25.000000
edge = 3.000000, area = 9.000000

Final thoughts: using code generators/pre-processing you can avoid using many unnecessary C++ features like templates. Keep things simple. C++ is hell, you will never be able to fully parse it without complex tools. Just assume good faith of your users while creating code processing tools. And I hope that such code processing will be integrated in future language (Metalua seems to be quite cool!).

Other files: input C++, makefile.

* C++0x constexpr, metaprogramming being fun, whatever. This is not a post about C++ pros and cons. Keep the big picture.

** Now I know that it probably wasn’t the best decision. But writing 2009 code with 2011 mindset would be too great.

*** Disclaimer: I’m no .NET expert.

(from #altDevBlogADay)

Simple continous integration

18.03.2011 19:38 in tools

Continuous integration is fun. There is probably a million ready-to-use tools to do it, but they may be too expensive/complicated/crappy or you just spent last 10 years building engine in your garage and hate everyone not invented here (there). Either way, I’m with you.

CI toolkit we are about to make consists of 3 separate parts:

1. The Notifier

Somehow we need to know that there is a work to do. Most VCSes are pluggable, allowing custom hooks occur on certain events (for example commit/push). But personally I prefer scheduled checks instead (as post-commit hooks may be slow). So basically cron (or Windows Task Scheduler) is your friend here.

2. The Builder

You need an automated build system here. On Windows it’s quite easy, because Visual Studio’s solution files can be used directly (devenv.com YourProject.sln /rebuild Release). On Linux/OSX there are makefiles/autotools, but for me using bash+make is quite painful. After all, the thing that programmers do the best is programming — so I’ve just wrote simple scripts to find all sources and build them, using PHP as scripting language of choice. Also, using PHP for whole “build system” is more cross-platform — it’s usually easier to bundle PHP executables than force anyone to install Cygwin shell (however I can’t imagine using Windows without it).

After building process is complete just call some simple API (see #3). Informations you need to send (you can use CURL for this) include project ID, OS/build version, build status, build logs (especially when build failed) and any other information you find useful. If you have unit tests you can attach them to logs. It’s also uber-cool to have automated test process (i.e. run game and start playing replay/script), taking screenshots at certain moments. You can also upload zipped package of game bundle (nightly builds).

3. The Server

Server receives API calls from building scripts. Data can be stored in DB. You will also need few pages that user can see (list of projects, versions, last N builds, specific build log). No rocket science here — average housewife can do it with PHP & MySQL in no time. E-mail notifications about failed builds sum up the effort. If you use some project management tool you can send notifications there as well. For example Assembla.com lets you create/update a ticket with a plain old e-mail.

BTW: build process can be moved server-side if you are able and willing to do it. But it’s usually easier to find cheap PHP+MySQL hosting than fully-equipped server with SSH access and build tools (GCC + friends) installed (let alone running the game to take some screenshots).

I’ve been using such system for my small cross-platform experiments. I do most dev on Windows (where I have Visual Studio) but I want to keep Linux/OSX versions alive and kicking. Small open-source teams can (and should!) try it as well. 


(from #altDevBlogADay)

Free Windows sampling profiler

04.10.2010 21:29 in tools

I've found quite good sampling profiler for Windows -- and it's cool because it's free (GPL). It's called Very Sleepy. Doesn't need any special setup -- given that PDB files are provided it can attach to running application and sample callstack. There is even possibility to choose thread to sample. I've set up a git repository on github.com so I'll be able to improve it a little bit.

gitcharts: LOC charts generator for git

13.01.2010 04:17 in git, tools

I've made small C# utility to generate LOC charts for git repository. It's quite fancy and the other reason was to learn using github. The code is definitely not the art of computer programming and to be honest quite slow (anyway, git on Windows is slow) but anyway MUCH faster than any other svnstat-like tools: it took 6 minutes to generate stats for 80KLOC with about 2000 commits. I don't remember how long did it take with svnstat but it was hours. And the project was much smaller that days.

gitcharts on github * direct download (C# solution) * example:

gitcharts