Skip to content

National Computer Games Conference

Sharif University is holding a two-day conference on games and game development. We (the guys at Fanafzar) are going to attend and some of us are going to have presentations and panels. There are also other interesting talks, mostly on the lighter side of technical issues. Here’s the web page for the conference, and here’s the pre-registration page (but I think you need to actually show up there on the first morning to register.)

The conference will be held on Esfand 19th and 20th (March 10th and 11th) somewhere inside SUT campus (yes, that’s tomorrow!)

My talk’s title is “Planning for Debugging Day” and in it I will present some thoughts and topics you should ponder and consider at the beginning of a game project to have less problems later on in the cycle when debugging, adding features, releasing, etc.

VN:F [1.8.4_1055]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.4_1055]
Rating: +1 (from 1 vote)

Smith, the Ascendant

“Oh God!”
“Smith will suffice.”

VN:F [1.8.4_1055]
Rating: 9.0/10 (1 vote cast)
VN:F [1.8.4_1055]
Rating: +1 (from 1 vote)

Tread Softly, Johnny Walker!

Had I the heavens’ embroidered cloths,
Enwrought with golden and silver light,
The blue and the dim and the dark cloths
Of night and light and the half-light,
I would spread the cloths under your feet:
But I, being poor, have only my dreams;
I have spread my dreams under your feet,
Tread softly because you tread on my dreams.

This, of course, applies specially to the “Black Label”. However, only those who know what I’m talking about can possibly know what I’m talking about! :D

VN:F [1.8.4_1055]
Rating: 9.8/10 (5 votes cast)
VN:F [1.8.4_1055]
Rating: +4 (from 6 votes)

The Problem

Religion is harmful. Throughout history so much conflict, war and misery has been caused by organized religion that religious people hardly even see it. They probably attribute it to human nature, or “other religions.”
I believe, and I think people would agree if they think about it, that religion has been the most effective and most used tool for mass conditioning and mass brain-washing. It has always brought an illogical imbalance to societies and has caused endless strife and animosity within them.
I’m not talking here about the wrongness of the concept of religion. All religion is bullshit, and all religion that divides people based on any criteria (believers and non-believers, sinners and saints, etc.) is harmful and must be treated as a disease of mind, but I’m not here to try to prove what seems to be obvious if you forget your childhood upbringing and conditioning. What I’m talking about is the danger and destructive effect of systematic, organized and political religion.

You look at human history, and almost every time there has been a war or oppression or genocide, it has had its roots in religion, in one way or the other. But, you might object, religion has brought happiness and peace of mind to billions. Well, while the extent of the happiness and the number of people affected by it is debatable, it can’t be denied that religious beliefs have been beneficial to many. But so has been the cartoons made by Disney! Have they not brought joy and happiness to hundreds of millions or perhaps billions, too? And there hasn’t been any wars or mass murders or tortures caused by a Walt Disney film. We must start analyzing the benefit/harm ratio of religions rationally.

So for some reason, among all the works of fiction it has been religion and religious materials that have gained special attention and have been given special status. Why? I don’t know. Maybe they have had more time with our collective brains to turn them into mush and turn us into brainless zombies. Maybe if you think about religion in terms of the theory of memes, it will become apparent that everything about a religion, specially the more widespread ones, is evolved with one most important purpose: to let it survive. That usually means spreading to as many people as possible. That means bringing people in and keeping them hooked up and destroying its rivals, be they other religions or science or whatnot. And we humans, have been the tools to be controlled by these vicious, dangerous and single-minded memes to ensure their survival.

If you think the purpose of any long lasting and popular religion is human happiness, you should think again. The purpose of any religion is its own survival and dominance. A few million people here and there doesn’t make much difference as long as the long-term success of that meme is guaranteed.

To those of you who still think, in this day and age, that any idea like religion should be enforced or even be allowed to be advertised or be a part of any government or ruling body, I can only say to look at history. Look at your own lives! Look at our own lives and the lives of those like us!

For those libertarians and agnostics among you, I have to say that I don’t think religion is something to be ignored and always categorized as a personal choice. Religion is harmful and dangerous. It’s not only publicly recognized dangerous religions and ideologies like Nazism that are dangerous, but the idea of any mass of humans controlled by any religion. We cannot afford to remain agnostic any more. People are entitled to their own beliefs of course, but those beliefs should never ever be the basis for special treatment or ever be let to come to power anywhere at any time.

VN:F [1.8.4_1055]
Rating: 7.0/10 (3 votes cast)
VN:F [1.8.4_1055]
Rating: 0 (from 2 votes)

The Joker

- This is boring.
- Yes, it is. It’s all boring.
- Except the bat. He is not boring.
- Not all the time though. He gets boring sometimes, with his ethics and stuff.
- But he can be very entertaining too.
- Agreed!
- I’m bored!
- Yeah, we are. I wonder what happens if I shoot that guy.
- Hehehe! We got him in the gut!
- Yeah, we did. His stomach acid must be gushing out onto the rest of his organs. Imagine the fun in there!
- But why did I shot the man who was supposed to open the safe for me?
- Who cares?! Let’s kill someone else too!
- Let’s throw the gun in the middle of the hostages and see what happens!
- Oooh! Look at them going for it.
- Look at that idiot henchman of mine mowing them down with his Uzi!
- This is entertaining!
- Yes it is. Yes it is.

VN:F [1.8.4_1055]
Rating: 7.0/10 (1 vote cast)
VN:F [1.8.4_1055]
Rating: 0 (from 0 votes)

The Human and the Machine

I was watching George Dyson’s interesting presentation at TED about birth of the digital computer, and something rather frightening occurred to me.
When I started out with computers, maybe 18 years ago, I was in awe of them. Computers and what they did were a closed book to me, the way a great musician’s work is shrouded in mystery for me, yet I was enjoying their output and imagining what else they would do. Pretty much everything surprised and delighted me. As I grew more familiar with their working and started writing code, my awe remained but it also transformed, to a kind that maybe a musics student feels when seeing a true master play.
The awe and delight pretty much remained as I grew closer and closer to the machine, I knew more, but it still was like seeing a loved one achieve a great accomplishment or part of a great orchestra that plays a fantastic piece. Seeing beauty even from inside inspires awe. Of course, I’m not suggesting that I was any good at any time, merely describing what I felt.
In general, it was a like a love affair. Every little things, even the hiccups were strangely endearing. I wouldn’t think of computers as a machine. Of course, the hardware is a piece of dead equipment, but there is something else in there and I think I felt it. The Ghost in the Machine. I was in love with the ideas behind the hardware. I never thought of myself as a master of the machine, or it just another tool like a can opener or something. We were conversing with each other and we took nothing for granted. If something didn’t work, it was neither my fault, nor its; the problem was in the communication.
But then my view started to change. I started to rely on certain things to work and certain things to go wrong. I started to view the computer as a terminal for information, as a tool for bilateral delivery of entertainment, data, software or communication with other humans. The machine started to get transparent in the process, the way your door is a transparent method of getting in and out of your home. The door is there, and you know how to use it, but the door itself is not at all important. The stuff and places on either side is what is important.
This change kinda creeped onto me. Today I was forced to think about it, and I was shocked and terrified to realize that most of that awe has shifted from the machine to other people. The machine is no longer there, only those who designed and built the hardware and software. I don’t know how to exactly explain my feeling, and my depiction here is not exactly accurate. But I have lost the reverence for the love of my life, and I am a worse person for it. And I am sadder, and certainly a worse programmer, which is ultimately the most important aspect of my life. Oh, I do write better code and probably design better today than any time in the past, but I am nonetheless a worse coder and programmer, because I have lost the sparkle, and the love and ability to be pleasantly surprised no matter what.

VN:F [1.8.4_1055]
Rating: 5.5/10 (2 votes cast)
VN:F [1.8.4_1055]
Rating: 0 (from 2 votes)

United States of Zombieland

The other day, my brother Ehsan recommended two new movies for me to see. One was Tarantino’s “Inglorious Basterds“, which is of course a no-brainer, and the other was “Zombieland“. Man his taste is impeccable!
I am now exactly 3 minutes and 14 seconds into the film (I swear to all the Random Number Gods that it was a coincidence!) and I love this movie already!
Guess what the track on the film’s opening credits is! Frakking “For Whom the Bell Tolls“!

P.S. I’m finally back in Tehran after a three-hour-plus flight delay.

VN:F [1.8.4_1055]
Rating: 5.0/10 (3 votes cast)
VN:F [1.8.4_1055]
Rating: -1 (from 1 vote)

Dragon Age, plus God of War Collection

I had heard of the new Bioware RPG Dragon Age: Origins but I guess I had forgotten about it. Today, I stumbled upon a couple of reviews and some videos and now I’m in love with the game! It appears to be very long (which probably means a few months of gameplay for me; yay!), difficult, deep and true to classic Bioware-RPGs. Almost everyone calls it a great successor to the Baldur’s Gate duo. And I enjoy Bioware games, from Baldur’s Gate and Neverwinter Nights to Mass Effect (yes, even KOTOR games!) My only worry is the departure from almost strict D&D ruleset of BG and NWN.
In any case, I’m downloading the whole 8GBs right now, unless somebody knows whether stores over here have got the game?

In other news, I finally got my hands on the God of War Collection trailer. For those rare GoW-challenged people among my audience, the Collection is a remastered version of the first two God of War games for the PS3 (they where released for the PS2 originally.) It seems that, as they had promised, SONY has not changed anything in the games, and just packed some higher resolution textures and (maybe) animations, which is a wise move. The first two GoWs deserve to be preserve at the masterpiece of gameplay they are. This will be release this month.
Obviously, the above means I have to get my own PS3. As soon as I figure out a robust solution to get both video and audio out of the PS3 and into my 1080p-capable but HDMI-incapable monitor and my speakers, I’m taking the trip to the Toopkhuneh(!) square and getting me a 120GiB PS3 Slim.

In yet other news, late last month (a day before my own birthday in fact) the twelfth book in the amazing and fantastic fantasy series The Wheel of Time has been released. It was supposed to be in November too IIRC, which makes the release a nice surprise. I’m already a fourth into the book and I’m liking it! May Robert Jordan rest in pieces… oops!… rest in peace.
I have one thing to say to anyone who likes high fantasy at all: GO READ THE BOOKS. The whole 12000-13000 pages of pure immersion and greatness. Doesn’t matter what other people say. I have read enough fantasy series to know which one is great! Go read it. Read the first book if you feel you don’t have the time. Do yourself a favor.

VN:F [1.8.4_1055]
Rating: 5.5/10 (4 votes cast)
VN:F [1.8.4_1055]
Rating: -1 (from 3 votes)

Tonight was the first time ever that I drank alone. I was, am, in a very bad mood. I sat on my couch and downed a third of bottle of whiskey, and then I cooked for myself too (that’s almost another first.) No matter how hard I try to forget what I did that led to this evening, I can’t.
I’m sitting here, alone, and I am sinking. I can’t stop crying. Remorse is not even an option. Even death is not an option.
I realized tonight that I am a real asshole. Not a lazy guy, not a very bad procrastinator, not a liar, not a completely useless person, not a drama queen; a real asshole and a very bad human being.
I think I need professional help. Shit. I do.

VN:F [1.8.4_1055]
Rating: 4.9/10 (7 votes cast)
VN:F [1.8.4_1055]
Rating: -7 (from 9 votes)

Lighning Through the Skull

I am walking on asphalt, on an endless field of black asphalt, when out of the black black sky, a single bolt of lightning hits me.
My eyes are the first things that explode out of their sockets. My hair catches on fire. My brain evaporates instantly, and the high pressure mixture pours out of my ears and nose. The skin on my eyeless face melts off. My skull shatters. My jaws spasm so hard that not only I bite off my tongue that is caught between them, but also most of my teeth are crushed to bits. My blood boils in a flash and my heart explodes inside my chest along with most of my major veins. The liquid in my stomach and my intestines flash boils too and burns through my abdominal wall. Almost all the muscle and flesh and sinew in my body are busy tearing themselves off from the rest or trying to burn. Some of my bones dry out in the space between two seconds and get ground into a thousand pieces. The skin, flesh and tissue of my extremities melt and my fingers and toes (not to mention the rest) are fused together in a shapeless lump. My feet have already turned to liquid wax and are spreading on the ground.
The charred wreckage that once spent his nights trying to optimize his assembly code remains there under the black black sky. Good thing I didn’t have my laptop with me, or the fall could have scratched it.

It’s that damn Gates’ birthday again.

VN:F [1.8.4_1055]
Rating: 7.0/10 (6 votes cast)
VN:F [1.8.4_1055]
Rating: +1 (from 5 votes)

Fun with C++: new, delete and Some of the Rest of the Story

Every C++ programmer knows new and delete and how they work. At least it must be so. I sure as hell didn’t know all the theory and detail behind C++ memory management facilities until 3-4 years ago, and I’m obviously still learning the practical details. So, please bear with me and see if there are things that you can learn about these old pals of ours, new and delete.

First, we all should know that new and delete are C++ operators, with all their facilities (and shortcomings, of course.) But not exactly like other operators, you can override them at a global level for every type that does not provide its own type-specific such operators. These global operators are provided as part of the C++ runtime library and are easily overridden. Their declarations are:

1
2
3
4
5
6
7
// The single-object versions
void * operator new (size_t mem_size);
void operator delete (void * mem_block);
 
// The array versions
void * operator new[] (size_t mem_size);
void operator delete[] (void * mem_block);

What new does is allocate a block of memory, and then call the constructor for the type with the address of the newly allocated block passed in as the this pointer. A delete call does the reverse; calling the destructor and then de-allocating the memory. The difference between the single-object versions and the array versions is only in the number of c’tor/d’tors they call. It amazes me how many C++ programmers don’t know and don’t care about details such as this (if you are programming in C++, these kind of details can and will bite you in the places you don’t want to be bitten!) If you fail to match them correctly, they allocate and de-allocate the correct amount of memory for the array or the single object, they just might not call constructors and destructors for all the objects being allocated or freed. That’s it.

Also, there is that small detail about exception-handling handling (yeah, two “handling”s!) The new operators may only throw an object of a sub-type of std::bad_alloc and only in the event that the requested amount of memory cannot be allocated. delete operators should never throw any exceptions (just like destructors. Remember that!) So, the correcter declaration for these operators would be:

1
2
3
4
5
6
7
// The single-object versions
void * operator new (size_t mem_size) throw (std::bad_alloc);
void operator delete (void * mem_block) throw ();
 
// The array versions
void * operator new[] (size_t mem_size) throw (std::bad_alloc);
void operator delete[] (void * mem_block) throw ();

Again, it is amazing how many programmers either don’t know about function exception specification and exception safety or just don’t use them (that includes me.) Of course, compiler providers are at least a little to blame here too. For example, Microsoft C++ compiler only distinguishes the empty exception list after a function declaration (meaning it doesn’t throw anything.) Anything else put there, just is taken to mean the default behavior is used (i.e. this function does throw something sometimes.)
In the meantime, the relationship between C++ programmers and exception handling remains in the love/hate/ignorance/hate/apathy/hate stage.

Later on, I’m going to talk abut overloading these global operators for fun and profit. Stay tuned! ;)

Obviously, a related problem to memory management is calling the c’tor and d’tor for an object directly. Uses for this may not be immediately apparent, but as a few examples I could name implementing good smart pointers, memory pools, memory managers, garbage collectors, generic object containers (e.g. std::vector) and such.
You probably already know how to call the destructor on an instance directly. If you have a pointer x to an object of type T, you can call its d’tor like this: x->~T() (note that you should not generally call the d’tor in this way, unless you yourself have called the c’tor directly on that instance as well.) Calling the constructor is a bit trickier though (not really; I’m just being foreboding!)

What you should realize is that you can overload new and delete with different signatures that the ones above. You can add arguments and of different types. There are a few other signatures for these two provided by the standard C++ library (yeah, there are others!) The less interesting of them are:

8
9
10
11
12
void * operator new (size_t mem_size, std::nothrow_t const & please_dont) throw ();
void operator delete (void * mem_block, std::nothrow_t const & please_dont) throw();
 
void * operator new[] (size_t mem_size, const std::nothrow_t & please_dont) throw ();
void operator delete[] (void * mem_block, std::nothrow_t const & please_dont) throw();

Forget about the deletes for a minute. The additional parameters to the new calls above are actually not used inside of the functions. Any object of type std::nothrow_t will suffice as the second parameter; it will be there just to signal the compiler to use this particular overload of the operator, which doesn’t throw any exceptions whatsoever. I just need to emphasize again that the regular new never returns a NULL pointer. It just throws an exception. But this one returns a 0 pointer upon failure and never throws anything, being it rocks, shoes or exceptions. The syntax for calling them, as you might suspect, is peculiar:

13
14
15
16
17
18
19
20
#include <new>  // for std::nothrow
//...
T * p = new (std::nothrow) T (/* usual constructor parameters */);
// The line above calls a particular "new" overload with two
// parameters: a size_t and a std::nothrow_t.
// Oh, and std::nothrow is just an object of type std::nothrow_t.
//... 
delete p;

Notice that I didn’t call the delete with any extra parameters or anything. In fact, there is no syntax in C++ for calling delete with any parameters! Besides, delete is already a non-throwing function. So what gives?! Why is there a paired delete for every frakking new when there is no frakking way of calling them?! You should keep your cool. I may explain them (if you don’t already know,) or we can leave the subject as an exercise. I would just say that the paired delete is called by the code generated by the compiler in a very specific situation.

Note that anything other than the straightforward, unary new and delete is called a “placement new/delete“. However, I’ve heard the term be used for a specific overload, which is more interesting and looks like this:

21
22
23
24
25
void * operator new (size_t mem_size, void * mem_ptr) throw ();
void operator delete (void * mem_block, void * mem_ptr) throw ();
 
void * operator new[] (size_t mem_size, void * mem_ptr) throw ();
void operator delete[] (void * mem_block, void * mem_ptr) throw ();

The implementations for the above operators are really simple. Here’s a complete listing:

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void * operator new (size_t mem_size, void * mem_ptr) throw ()
{
    return mem_ptr;
}
 
void operator delete (void * mem_block, void * mem_ptr) throw ()
{
}
 
void * operator new[] (size_t mem_size, void * mem_ptr) throw ()
{
    return mem_ptr;
}
 
void operator delete[] (void * mem_block, void * mem_ptr) throw ()
{
}

Note that although I haven’t written it, the constructor and destructor calls happen outside of my control. These versions of new and delete are used when we don’t want to allocate or free any memory, and just want the constructors and destructors to be called. For new, we just pass in a pointer to another sufficiently-sized memory location and ask the compiler to generate the code to call the c’tor upon that area of memory. That’s how we call a c’tor directly. We procure some memory area from somewhere and use that, like this:

43
44
45
T * x = (T *)malloc (10 * sizeof(T));
for (unsigned i = 0; i < 10; ++i)
    x[i] = new (x + i) T (/*usual c'tor params. */);

These standard placement operators cannot be hidden or overridden in user code, but there is still a ton of fun to be had.

You can very easily replace the old, simple and default operators with an implementation of yours, a la:

46
47
48
49
50
51
52
53
54
55
56
57
58
59
void * operator new (size_t mem_size)
{
    void * ret = malloc (mem_size);
    if (0 == ret)
        throw std::bad_alloc ();
    return ret;
}
 
void delete (void * mem_block)
{
    free (mem_block);
}
 
// the array versions are exactly the same

As I have stated earlier, the c’tor/d’tor calls are generated automatically for you by the compiler. So now you are free to write your own memory manager!

The way that memory manager/debugger/helper/whatevers usually work under the hood is that they allocate more memory than you have requested, and put their own junk right before and/or right after the area that gets passed back to the user (that’s a bad way to do memory management, but that one is a long story.) Some of the stuff that are usually kept there include a pointer to the next and/or previous allocated block of memory (so all the blocks form a linked list,) sentinel values right before and right after the user area to detect buffer overruns (e.g. 0xdeadbabe,) the size of the memory block, the code file/line/function/module that allocated this particular block and so on and so forth. Actually, your default memory manager in the CRT is doing this right now. Just new two large-enough blocks of memory and compare their address differences with the size of the first block. The runtime accompanying some compilers (like VC++) even exposes their internal data structures and means to work with the memory manager (although rather passively.)

You need to keep in mind though, that what I have discussed so far barely scratches the surface of writing memory managers. These are just practicalities and implementation details; the state of the art on the theory of the matter and memory allocation algorithms, policies and mechanisms can fill several books. Even on the implementation side, there are really serious issues with performance, cache-friendliness, thread-safety, multiple thread support, etc. need taking care of.
Besides, much (if not most) of the memory (de)allocation going on in a C++ program these days go through C or operation system API, shared object files (DLLs,) through third part code or through STL, all of which bypass the basic technique discussed above. So, if you really are serious, you should investigate the existing memory debuggers or memory leak detectors or memory managers. There are several open source ones out there, with various degrees of sophistication and complexity. Have fun! (But for what it’s worth, I should mention that we have used a memory leak detector using nothing but this technique in Garshasp and a similar project before, and in both projects it has been a great help.)

VN:F [1.8.4_1055]
Rating: 8.0/10 (9 votes cast)
VN:F [1.8.4_1055]
Rating: +1 (from 5 votes)

Darth Vader in the “House”!

Holy Shit! It’s James Earl Jones appearing in the latest episode of House!
That voice, even after 30 years, is the most powerful and commanding voice in the whole frakking Galaxy; this one or the other one Far Far Away. :D There is even a bit of “Darth Vader breathing” in there!

Try reading the following with a picture of Darth Vader in front of you, his voice booming in your head, and preferably with a red-blade Light Saber replica on your mantelpiece (sorry folks, I really do have one of these!)

You are sure [...]?

…Leave him alone.

We want you to use her blood.

Who is that man?!

My own son…

Inject my IV with an air bubble. I will have another heart attack and no one will know. [...] You were trying to put a gun in his hand and point it at my head. The gun is now in your hand. That is a practical difference, not a moral one.

In my world there are dangers and bloodshed and death, and that makes you a man. And men make choices.

I just finished watching this 3rd episode of season 6 and it was a good one.

P.S. OK, that’s no way to come back and post to my blog after a few months, but give me a break; I’m trying to ease in (you know, 2x3-3x2 style!)

VN:F [1.8.4_1055]
Rating: 7.0/10 (6 votes cast)
VN:F [1.8.4_1055]
Rating: +2 (from 6 votes)

Self-justifying Choices

Running away is a solution to many things. Giving up, throwing in the towel, passing the buck, unshouldering the burden, admitting your incapability, playing dead…
It’s a perfect solution for mediocrity, isn’t it?

If you thought that was the rant, you’re sourly mistaken. Here it is.

Let’s try a thought experiment. Nothing real, just a wild and fantastical fantasy. Suppose you are standing on the edge of a rooftop, contemplating jumping down. As long as you haven’t jumped, the choice is always yours. You can jump, stay on the edge or move back. It’s not like you can’t jump two seconds or two hours later. If the situation changes, you can always weigh the odds again and make a more suitable choice.
But what if you jump? You make a choice and you jump. I think that’s the best choice you can make; the most logical. If you stay on the edge, or you move back, the edge and the jump will always be there, tempting and tantalizing. Why defer the choice?
Some might say because it’s a final choice and because you will always have the option, you can postpone it for later. It’s not like the edge is going anywhere, right? Wrong. I say you should always jump the first chance you get, because once you jump, there is no going back and there is no more choices. Why would you postpone a move that solves your dilemma so quickly? How come people (including myself) defer such obviously logical choices for more than a single second?
I like these kind of choices. I like changes and commitments that you cannot revert. They simplify life so much.

I think it’s kind of funny, I think it’s kind of sad,
The memories in which I’m dying are the best I’ve ever had.

That’s not about death of course. It’s about giving up in despair. It’s about indecision and inability to deal. It’s about petty problems that prove impossible for weak people.
It’s about me.

VN:F [1.8.4_1055]
Rating: 5.0/10 (9 votes cast)
VN:F [1.8.4_1055]
Rating: -2 (from 6 votes)