Quick fix for asynchronous subtitle (.srt) files

February 10th, 2008

So, yesterday I was getting down to watch a relaxing movie and I realized that (yet again), that the SubRip file was not properly synced with the video. I decided that I've had enough of this. It's happened far too many times. So I opened up the .srt file in TextPad and realized everything is right there in plain text. That's probably because M$ or the MPAA did not have a say in the matter. Anyway, so instead of spending 30 mins searching for a new subtitle file on DivxSubtitles or OpenSubtitles I decided to write my own app that will offset the timings in an srt file.

For those of you in a similar situation, I'll save you 30 mins of searching or writing a quick script.
Just click here and follow the instructions on the following page. You should be done in under a minute.


Compile time assertion

January 18th, 2008

Yesterday I finally found a use for compile time checking. I remembered reading this up in Andrei's book 'Modern C++ Design'. So I got down to implementing it in VS.NET 2005.

His code (code below) seemed to work well in theory, but knowing MS, that's as far it goes.

template<bool> struct CompileTimeChecker
{
     CompileTimeChecker(...);
};
template<> struct CompileTimeChecker<false> { };
#define STATIC_CHECK(expr, msg)
     {
          class ERROR_##msg {};
          (void)sizeof(CompileTimeChecker<(expr) != 0>((ERROR_##msg())));
     }

Turns out that MS VS.Net had some complaints about the code above…

error C2066: cast to function type is illegalerror C2070: 'CompileTimeChecker (main::ERROR_ONE_NOT_EQUAL_TO_ONE (__cdecl *)(void))': illegal sizeof operand

At first I thought I’d take the easy way out and just throw an error using a #pragma. But then that would beat the very purpose of the code being compiler independent. After some 30 mins of trial and error, managed to get the same code (code below) working with a few minor modifications.

template<bool> struct CompileTimeChecker
{
     CompileTimeChecker(...){};
};

template<> struct CompileTimeChecker<false> { };

#define STATIC_CHECK(expr, msg)
{
     class ERROR_##msg {};
     ERROR_##msg y ;
     (void)sizeof(CompileTimeChecker<(expr) != 0>((y)));
}

Now calling something like…

STATIC_CHECK(sizeof(char)>sizeof(int), SIZEOF_CHAR_NOT_GT_SIZEOF_INT) ;

will result in a compile time error that looks something like this…

error C2440: '<function-style-cast>' : cannot convert from 'main::ERROR_SIZEOF_CHAR_NOT_GT_SIZEOF_INT' to 'CompileTimeChecker'> No constructor could take the source type, or constructor overload resolution was ambiguous

Until next time, happy coding!


Random thought: Some advice from experts before you become that dark hacker

December 8th, 2007

I always find it hard as to why there are not so many genius hackers out there making a gazillion dollars a day hacking high profile government servers. And then I read this Q&A session with Bruce Schneier . Well basically he says…

"If I make a computer security mistake in a book, for a consulting client, at BT it’s a mistake. It might be expensive, but I learn from it and move on. As a criminal, a mistake likely means jail time time I can’t spend earning my criminal living. For this reason, it’s hard to improve as a criminal. And this is why there are more criminal masterminds in the movies than in real life."

I highly recommend that you read the Q&A session . It's about the past, present and future of computer security, selecting the right passwords and digital security for the ignorant common man like you and me.

PS: For those of you who don't know Bruce Schneier is one of the most respected computer security experts. I take his advice pretty seriously.


Trekking at the Sayhadri range

November 30th, 2007

STA_7767-STO_7781
Just came back from a trek in the Sayhadri range in Maharashtra, India. Took a 10 day break from the routine. This is after a two year break from trekking. I almost forgot what it was like to be out in the wilderness. It was a mind blowing experience, sleeping under the stars, breathing unpolluted air and visiting caves and forts built in 200BC. I thought that such stuff existed only in video games. All in all, it was an easy trek. It's one that I have wanted to do for some time now and one that I’d like to do again in the monsoons. The grass is greener (literally) and the landscape is ten times more beautiful. As you might guess there is a photo album where you can check out the pics.

For those of you interested, the trek was organized by YHAI Malad unit. The trek schedule was something like the one below, but we deviated from plan here and there, but below is what happened during the trek.

1st Day (17th Nov 2007) Base Camp Karjat
Arrival at Karjat.

2nd Day – Rajmachi Camp.
The journey started with a half an hour auto ride. The actual adventure began from Kondivade with with a walk with awe-inspiring views of Ulhas Valley. Along the way to Rajmachi we visited Kondhana Caves. In the evening there was a sight seeing program of lake and temple. We even took a dip in the lake.

3rd Day – Rajmachi Darshan.
In the morning there was a sightseeing program of twin forts of Rajmachi. In the evening we were kept busy with a treasure hunt.

4th Day – Ekvira Camp.
There was a rather flat walk through the Valvan village on the bank of Shirota Lake. Even though it was flat I would say it was the toughest route in the trek as we had very limited water and it was blazing hot. Then after a gradual climb we reached the top of Ekvira mountain. From there we were able to see the Valvan dam and Lonavala city. After that we climbed down to the famous Karle caves. After seeing the Ekvira caves and Ekvira temple we reached Ekvira camp.

5th Day – Lohagad
The trek started towards Village Bhaje via a tempo ride. After visiting beautiful Bhaje caves group we climbed Fort Visapur and then reached Lohagad wadi camp.

6th Day – Back To Civilization.
In the morning we visited Fort Lohgad. After this we had a certificate distribution program after which everyone split up and went their way

After the trek, I spent a few days in Bombay. Bombay reiterated it's coolness. That is where things happen and where you live your life to the fullest. I will be uploading a few pics from that trip as well.

In the mean while I uploaded an old album of an office party to the site. It’s one of those never ending jobs where I need to sync up all the albums from my HDD to my site.


Coding best practices: Minimizing the use of macros

November 30th, 2007

This coding ‘best practice’ is the first in the series of many posts that I will be making. Many of them have been introduced to me only recently, so it’s unlikely you will see this in my old code. I am making these posts as I think they are essential to every C++ programmer.

Lets get started on the first one…
Some may call this an opinion, but I call this a good programming practice.
Don’t declare a macro unless you cannot do without them. Below I have presented some problems plaguing macros and some workarounds.

1. Macros modify the code before the compiler sees it. This makes it harder for you to debug your program, step into code and find bugs.

2. Macros don't obey scoping rules. So the following example

// The macro below could very well be in another header file
// that you never seen or totally forgot about
#define width 20

// And then you name a variable the same name,
// forgetting there was a macro with the same name
// It’s than you think to forget, especially if the macro is
// hidden in one of those header files
void RenderRect(int height, int width) // …

Workarounds
Below are some workarounds for some common uses you might have for macros

// 1 //// Global strings
// macro:
#define SZ_HELLO "Hello world"
// workaround:
char const szHello[] = "Hello world";

// 2 //// Global setting
// macro:
#define NMAX 5
// workaround:
const int nMax = 5;

// 3 //// Generic functions
// macro:
#define SQUARE( x ) ((x)*(x))
// workaround:
template<typename T>
T inline square(T &x)
{
  return x * x ;
}

End note

Of course there are some places where only a macro can get the job done. In these cases go ahead and use a macro, but follow a strict naming convention for your macros so you will not confuse them with a standard variable later on.


My experience with optimizing a game engine

October 26th, 2007

"You're bound to be unhappy if you optimize everything"
Donald Knuth

As with every new programmer I would spend endless hours trying to squeeze optimization out of every small piece of code on my engine . I wanted it to be the fastest. I always thought that writing every line of code in assembly would make a super fast engine, not knowing that the compiler would probably optimize my code better. I would spend long hours studying SSE instructions. Well one advantage is that I am no longer afraid of ASM , but I was generally unhappy that my engine was not be super optimized and nearly as fast as a commercial game, even though it was probably doing only 25% of the tasks. Colleagues (just as inexperienced as me at the time) would come up to me and say 'Hey, you know this guy, he wrote a whole OS in assembly'. And we would all think that this guy wrote the best OS in the world. But hey, wait a minute. Then why are over 80% of the desktops still using Windows?


Restricted access to albums

August 26th, 2007

I had some free time this weekend so I spent the time learning Drupal API and rolled out a restricted access module. Most images in my albums with pictures of my family, friends and me will have restricted access. Writing a drupal module was not as hard as I thought it would be and took me only one day to write with other dependencies.

This was a much needed module. I was never happy with the fact that some unknown person out there could have such a close eye on my private life. I'm not so sure my friends would have liked it as well. My friends/family will get URLs that will log them in automatically. This way i'm keeping the hastle down to a minimum.


Code is a tricky friend…

June 7th, 2007

Well, it's been a long time since I have added anything more than pictures to my blog.

A lot has happened since I first started this blog.

The most important realization being that my old programming style is utterly junky.
The main problem is that I always sit down and start coding without any design and planning what so ever. And finally when I hit a road block I change the code. In fact the engine that I wrote went through so many changes, it's impossible to say it follows any solid programming style.
To a large extent I knew my programming style was quite crappy, but just didn't know any alternate way. I was always against C++ inheritance and was always for a static program structure, but really didn't know any proper implementation technique. Luckily for me, I discovered a whole new world of templates. Boy are they great!!! I think inheritance should be left at college level just so that we pass our exams.
Anyway this means that my precious project is a whole pile of binary junk. And now I have to start over. Oh well. Better late than never.
And then I learn that a whole bunch of other techniques are actually a big no-no. Some of which I am still quite hesitant to give up.

Luckily for me I met a really talented guy and he explained some good programming practices and I soon realized I was breaking most of them. There are one or two I don't agree with him on, but those don't make such a big difference in any case IMHO. On the other hand I was quite happy to realize that my basic priciples like having static code and getting the compiler to do as much work for me were the best principles to follow. It's just that I had no idea or techniue of how to do these things. (I'm actually making excuses for bad programming here)
I plan to write out the good programming practices that were told to me, so that everyone can start writing better code in future.

I also seem to have a decent amount of exposure to Unreal Engine 2.0 and learned a lot of new tricks in the trade. One thing I was surprised to learn is how well they followed most/all the good programming practices. It's amazing. I always knew that they were doing it right. This also means that my precious project is junk overnight and I have to start over. I have no idea if I am going to and if so when. I am thinking of keeping it small and simple and making just a rendering framework to test shaders and other things I write. Besides I still have another project that I'd like to release. I really need to open this one up and see how badly it has been contaminated by my bad planning and poor programming practices.

Other than that I put up an article that I wrote a long time back. I tried submitting it to DevMaster.net and gamedev.net. Both still haven't put it even after nearly 6 months after initial submission was made. DevMater.net showed interest in putting it up strangely. I got tired of waiting and put it up on my own site. At least it is out there, and boy I am happy I did not put any code in to the article. In any case part 2 will be coming soon. Just need to get some formatting done on it.


For my photographs – My very own flickr

November 14th, 2006

I've always had bad luck with publishing my photographs online. Every 3 months or so, I need to find a new place to store it.

I started off with Yahoo photos . After uploading about 3-4 albums there, I suddenly realized that the max you can zoom into a photo is about 400px and there was no way of getting larger images. It turned out that at this resolution I can barely make out who's who. Guys I wanted my thumbnails that size.

So then after a little searching I moved to myphotoalbum.com . Yup, they offered me unlimited space, bandwidth, full resolution photos, as long as I buy some stuff from their store. This did not seem to be all that bad. Even their paid accounts were quite cheap. So I upload another 4 albums there, only to realize that their interface absolutely sucks. It looks like it is straight from 1997 when all we had HTML 3.0. Time to move again.

So after a bit more searching and a carefully look at the list of restrictions, I moved to flickr.com. They had a very cool interface, nice tagging system, software to upload photos directly from the comp, and a social networking system, although I had only 2 friends on it at the time. Well just to be clear, the restrictions at the time were a 20Mb upload limit a month and a max of 3 albums, but this was all ok since they had an excellent tagging system and they had a nice way of showing photos, so visitors could quickly browse photos without having to look at the full rez ones of each photo. So I upload a 500 photographs there…
until one fine day I see this big black notice that reads – Only the first 200 photos of your album will be visible. !@#! A!@ H$#S. !@#!#@!#@!#. WHAT THE HELL WERE YOU GUYS SMOKING WHEN YOU CAME UP WITH THIS!!! There are times when I have more than 200 photographs in a month, and I'd definitely like my friends to see photographs from the last 10 years if they want to. So at this point I consider going in for a paid account here. This does not seem to be all that expensive. It works out to only $3 a month, but why the hell should I pay money to someone who has just screwed me over. I'd rather pay someone else.

So I go hunting again, and I find Zooomr . Seems to be cool, pretty much a clone of Flickr.com but what if… What if one day the guys at zooomr start smoking crack and reduce the max number of photographs to 10. Then what will I do? Move again? Another point is that yes, eventually I guess I'd have to move, but then I'd like a service that allows me to do a full export of all my photographs, tags, captions, everything, so I can pack up and move anytime, anywhere (The same thing goes for my email, which is why I bought a fastmail.fm account). My photographs belong to me!!! I should be able to do what I want with them. Yes, I did find jUploader and FlickBackup, both neat tools that lets you export and import photos, but these looked more like work arounds and hacks (yeah I know they work on the standard API, but still), and I've been screwed so many times over now, I think it's time to learn.

So I make a list of features that I want and see how I can get them together on my own. Come on, I was a pretty good PHP programmer once upon a time, and I'm sure I can put together something for myself.

So here is my wish list, in order of importance…

  1. Within my budget – of around $3 a month (I'm quite stingy actually)
  2. Full control over backups – Only smugmug and google.com seem to give me faith regarding this, but their accounts were too restrictive or over my budget
  3. A decent upload limit
  4. A desktop uploading tool
  5. A tagging system
  6. Nice UI, that doesn't make people feel that they are in the 90's
  7. Option to see full size pics if visitors want to
  8. Access control
  9. Integrated with my blog
  10. Hmmm… I think that's about it.

So it looks like I have only one option but to make my own…

I started off with ezPublish, since I had worked on it before, and I knew it was pretty much capable of anything. Well it turned out my old host did not support it. I thought I'd switch hosts and try again, but in the mean time I got restless and started trying other CMSs. WordPress , how can I miss this, most of my friends had used it and liked it, but then they all said the image system kinda sucked. So then I looked at Gallery. Very cool, feature rich, 110 % customizable , but it seemed to be bloated and their API was too complicated. Not that I couldn't learn it, but it was not the amount of time I was willing to put it (about a 100 hours or so). So then I find drupal though gallery, since gallery is well integrated with drupal. They seemed to have a small lightweight system, tons of modules and a simple and easy to understand API system.

So I got down to customizing drupal…

First things first, the UI. I pulled out a ready made theme, installed it and damn, it looked good. Doesn't it?? at least better than what most other coders had Cool
Next, the basics – taxonomy, tinymce, clean URLs and yeah the image system… I gave gallery another try, but it just did not seem to be worth it. I guess I'd just have to spend the 100 hours working on image.module and image_gallery.module. So then got down to installing it. The basics seemed right and that's all I wanted. Next a tag cloud system like the one at flickr.com. This would be simple to code, but wait, there is already one ready. tagadelic.module. Hmm what else, I needed a system that would save me disk space and the only way to put a 1000 photos without worrying abt disk space was to upload the images else where. Now which online service would be my bi+** and do this for me… Hmm, imageshack.us. A very nice service. They allowed hot-linking. Not too much of, but I don't get too many visitors anyways, so don't think they'd mind. They even had an API ready. Damn, they even gave me the PHP script for the API. So after a little hacking on the image.module code, I had all my images automatically relayed to imageshack.us. Damn!!! Things were looking good. REAL GOOD. Finally a system to bulk upload photos. Without this,I will spend my whole life just clicking photos and uploading them. So I decided to write this last part of code on my own, but then after a little searching guess what I find… image_pub.module It was still in beta, but it would do. After a bit of hacking, I made it work with my hacked imageshack <- image.module code. Now I could even upload photos with captions. It was better than what I had thought of initially (Which was uploading zip files). And when I finally put this all together last night, it worked like a charm. Take a look at my first uploaded album Looking good huh :-D and considering the amount of time and effort I put, it's awesome.

But still there is a little work left…

  1. A system to add tags in bulk, I think there is already a module for this
  2. A system to backup albums, their tags and pictures from imageshack.us into a archive with XML preferably something that will be compatible with picasa. I'll need this done, because I'm not too sure how long imageshack.us will remain alive, now that I'm there. He he he he.
  3. A customized access control system that will keep guests out of private photos yet not require friends to go though a painful sign up/login process

Well then, if anyone needs the hacked image.module, please feel free to ask me. I'm not going to release it though. It works fine alone, but not with other modules that depend on it.


The speed freak

November 9th, 2006

When I first started coding the Replica framework, I wanted it to be the fastest meanest framework out there. Due to some misconceptions I used to spend hours and hours optimizing things like the Win32 message loop, write code in ASM. AT one point I even thought I'd write the entire engine in ASM, to get the most out of the CPU. I spent almost 2 months in R&D on optimal vector maths and matrix multiplication.

Looking back I now realize that these are not the things that define the speed of the engine. Yes, they are important, but not as important as an improved graphics technique. Coming to realize it now, writing the whole framework in ASM may have given me just 1 FPS more (if at all), where as a simple improvement in a graphics technique will give me much more than that. Not to mention the inconveniences that I would have to go though managing ASM code. Don't get me wrong, an optimized vector class is important, but something like this is fairly straightforward and definitly did not need months of R&D. Just run it through VTune and you will get the most optimized code in the world.

Everytime the latest Quake source code came out, I'd go running to the vector class and see how they did it… And then be majorly dissapointed. For a long time, I even though that they used to replace the actual optimized code and put in the lame C code in it's place. I was most dissapointed when I started writing a mod for UT2K4. I realized that they were doing vector normalization in the .uc scripts. Mahn, what were they thinking!!!

Now after reading several presentations, articles and looking at some of the world's best engines, I'd say there is a looong list of optimizations that are needed, but none of them are near the vector class or the Win32 message loop as these are something that we will never be able to change.

IMO, a major amount of time spent is used to shovel data in memory and out of it, and just by using memory wisely you can improve the performace of any engine. And be warned this is not just in the CPU. Data shoveling takes place even in the GPU. Just try switching on mipmaps and see what a huge improvement in speed you get.