2009/11/25

Sometimes I hate Windows.



Surely, every OS has strong and weak points (I'm still using windows/linux dual-boot), but when something breaks for absolutely no reason, it is never funny.


Today I was supposed to spent a lot of time programming windows software. Unfortunately, at one point I had to reboot the system, and after reboot system went to hell.


For some reason, my network adapters disappeared from "Network Connections" folder (although they were still working), I couldn't start VPN connection anymore, connection sharing broke up, and shortly after startup I was greeted by message telling me that RPC could not start, and system will shutdown in 60 seconds. "Last working configuration" (F8 at the boot time) didn't help either, and of course I disabled system restore on all drives few months ago. System CD was lost too, although I still had copy of installation files.


Investigation (My Computer->Manage->Event Viewer) uncovered dozens of errors in system logs:


  1. Event ID: 4609

    The COM+ Event System detected a bad return code during its internal processing. HRESULT was 800706BA from line 44 of f:\xpsp3\com\com1x\src\events\tier1\eventsystemobj.cpp.
  2. Event ID: 8193

    Volume Shadow Copy Service error: Unexpected error calling routine CoCreateInstance. hr = 0x80040206.
  3. Event ID: 1802

    The Windows Security Center Service was unable to establish event queries with WMI to monitor third party AntiVirus and Firewall.
  4. Event ID: 7023

    The Windows Firewall/Internet Connection Sharing (ICS) service terminated with the following error:
    The handle is invalid.
  5. Event ID: 7023

    The Security Center service terminated with the following error:
    The endpoint mapper database entry could not be created.
  6. Event ID: 7034

    The Remote Procedure Call (RPC) service terminated unexpectedly. It has done this 1 time(s).


At first it looked as MsBlast virus comeback, but virus checking tool made by symantek didn't find the virus, and that vulnerability have been fixed long time ago.


in the end it looked like everything was tied to COM+ and access rights. Somehow access rights on %windir%\restore has been reset to "no access to anyone" and COM+ services couldn't start, but of course fixing access rights didn't fix the problem. Then I searched for instruction on reinstalling/resetting COM+ (google for "reinstall COM+" and you will find them yourself). There were couple of instructions but none fixed my problem. Error messages changed to


  1. Event ID: 4689

    The run-time environment has detected an inconsistency in its internal state. This indicates a potential instability in the process that could be caused by the custom components running in the COM+ application, the components they make use of, or other factors. Error in f:\xpsp3\com\com1x\src\comsvcs\events\lcedisp.cpp(131), hr = 80040206: Failed to CoCreate EventSystem object.
  2. Event ID: 7000

    The COM+ System Application service failed to start due to the following error:
    The service did not respond to the start or control request in a timely fashion.

Somehow I managed to make my NICs visible again, but this didn't make VPN work.
I also tried to monitor processes with processmonitor (to find broken privilegies or missing files), but it quickly become evident that processmonitor is no match for strace/ptrace available on Linux. I was unable to reinstall COM+ completely, because of "Sub-component COM+ raised an exception while processing the OC_COMPLETE_INSTALLATION setup message" error.


In the end, after several hours of googling, swearing, and numerous failed attempt to fix the system, when I was about to give up and initiate full system reinstall, I finally remembered that system was transferred from older harddrive only week ago, and old system partition wasn't overwritten yet. A quick boot into Slackware Linux, ntfsclone+ntfsresize, and voila, I had working system again.


Why all this stuff happened, still remains a mystery to me.


My advice: if you ever encounter similar problem, and if you were unable to fix it in 30 minutes, forget about it, and do full reinstall. You'll waste less time. It also makes sense to keep system restore enabled, and backup system at least sometimes (nothing beats backup, even search engine). NTFS backup can be easily done from linux system, see "ntfsclone manual" for more info.


Links I've been using while trying to fix the system:


  1. How to clean up a damaged COM+ catalog on microsoft.com
  2. Reinstall COM+ forum discussion.
  3. COM+-related events may be logged in Event Viewer when you install Windows XP Service Pack 2 and join the computer to a domain
  4. W32.Blaster.Worm Removal Tool
  5. Linux-NTFS wiki
  6. Linux-NTFS manpages.

2009/10/29

How to argue on the Internet


Rule #1:


Do not argue on the internet, unless you absolutely have to, or unless you know your opponent personally. (or unless you are a troll).



Why? Because:


  1. Unless you know your opponent personally, you will be trying to prove your point to a stranger you probably won't ever meet. In general, no matter whether you win or lose, nothing will change.
  2. Writing takes time. A lot of time. Good, cold-blooded, intelligent argument, supported by research (or at least by the Google search) might take from 30 minutes to a few hours to finish, even if you are typing 400 characters per second. In general, your time isn't worth the result - you in most cases you won't get satisfaction, even if you "win". Also, eventually someone else will join the discussion without reading your arguments first. So it will be a waste of time.
  3. Arguing sometimes takes emotions. If discussion becomes "heated", you might start to feel angry, etc. This also isn't worth the result.

Exceptions from rule:
It makes sense to argue, if one of following conditions applies:

  1. Your life is directly affected by result of argument.
  2. You have warranty that your opponent(s) is sane, is not a troll, and can provide good arguments.
  3. You truly enjoy the process.

Of course, it is easy to say "do not argue", but how to deal with annoying/insane arguments? Here goes


Rule #2


Add "I think" ("He/She thinks") in front of opponent's arguments before reading them.


Why?


Because every argument on the Internet is an opinion. However, people very frequently forget to mention (by adding "IMHO") that what they write is merely their opinion. And some unique individuals will truly believe that what they think is some kind of absolute truth. The problem is that when you see something outrageous that doesn't fit what you know/believe in, and this thing presented as a fact (or some kind of absolute truth), your brain (mine, at least) for some reason wants to prove that your point of view is right, and this argument is not. However, for some reason, opinions of other people are easier to dismiss/ignore than something that is presented as a fact.


How to use it (example):


You log into forum. The user XYZ posted a message saying (keep in mind, this is just an example) "The universe was created by a big purple pipe-smoking rabbit with a pitchfork!" Now, it doesn't fit your religious beliefs and makes you enraged (keep in mind, it is still an example). Before starting a flamewar, add "I think that" in front of original sentence, and read it again. Now the message turns into "I think that the universe was created by a big purple pipe-smoking rabbit with a pitchfork!". Now for some reason it doesn't feel enraging, because If this dude thinks so, it is his problem with his head, opinion of some random dude (you never met) doesn't matter for you, and he will not be able to provide interesting discussion anyway. So you ignore this guy, and move on instead of wasting next hours in flamewar.


Rule #3


Avoid discussions about religion at all costs


Why? Because:


Faith frequently blinds people, and makes them think they are right affects both atheists and believers) and their opponents are not. There are very few(less than 1% of population, I think) people that can easily analyze/and change their beliefs when they are trying to prove they are right. During my life I never saw any religious that didn't end in flamewar. I also don't remember anyone in such discussion ever convincing someone else. So, in general, it will be a waste of time (unless you are troll, of course). Of course, there are always nice people that calmly follow their religion (or atheism) and don't bother anyone. However they normally don't have a need to participate in discussions about religion.


Rule #4


Unless you are dealing with science, any opinion (including yours) can be wrong and is not a universal truth Best applied to discussions about religion, in case you skipped Rule #3.


Why? Because:


Imagine that you live in a city and know that population of your town is 5000000. The problem here is that you do not know, but believe that population is around 5000000 people. This is because information about size of population was given to you by some kind of authority you trust(which is supposed to provide correct information), and you cannot really verify number of people yourself. If this authority gave you invalid information, your knowledge about city population will be invalid. The problem here is that nearly everything you know comes from some kind of trusted authority. Perhaps you never been to South Pole, but you believe that it exists, because someone told you so. The bigger problem is that first trusted authorities you use are your senses. So if you think about it, entire world around you could be an illusion or result of your imagination, without any way to be absolutely sure that anything besides you is real (that's where "Cogito ergo sum" comes from). If we skip philosophy part, and return to simpler terms, this means that both you and your opponent most likely have incomplete or invalid information about the subject. For example, you or your opponent may refer to online resource that is not guaranteed to have valid information, and so on. Yes, it is possible to at least try to make sure that your information is valid, and trace/verify all sources you are using, but people rarely do it. So it makes sense to keep in mind that anyone can be wrong. And if you want to make things more fun, add Murphy's law to that ("Anything that can go wrong will go wrong."), which will mean that in any discussion is probably wrong at one place or another.


Rule #5


Remember that you don't have to agree with someone.


Why? Because:


There are many people in the world, and they all have different opinions, believe in different things, follow different religions, etc. And there is no absolute, universal truth, at least it isn't easily accessible. It is not possible make everyone agree, and it is not necessary. It makes sense to argue in situation when something serious depends on result - life of millions, perhaps. Clearly most Internet arguments and flamewars doesn't qualify as such situations, so participating in them is nearly always a waste of time (well, unless you are a troll and enjoy provoking other people). In most cases it is perfectly fine to disagree with anyone, as long as your position looks valid to yourself. And it makes sense to ignore "incorrect" opinions of other people, unless your life (or life or friends, relatives, etc) is directly affected by them. It makes sense, because people
don't like being told that they are wrong, proving something to someone takes time, and doesn't always provide useful result.


End of article.


Hope this helps someone.

2009/09/30

Computer piracy: common myths, and how copyright laws should be working


Intro



Recently I saw some old thread on ign or gamespot, where someone told people that he uses pirated game. Immediately a flamewar erupted, with standard pro/anti-piracy argument :"you should be paying game developers!", "why should I be paying for something I can get free?", "piracy is theft!", "no it is not a theft", etc.

I'm sick of such arguments, because they are based on mistakes and misconceptions.
So I've decided to write a small list of "standard" arguments, explain why (I think) they are wrong, how I think copyright should be working, and why it is not going to happen.

Everything written here is my personal opinion.

Standard pro/anti-piracy arguments and why they are wrong




  • "Piracy is a theft!"

    This one is most often discussed. If you do a little google search about piracy theft, you'll probably find a lot of blog posts explaining why it is not a theft. Many of them will make sense(this one, for example), and some are even written by people that were interesting in things other than legalizing piracy.


    I have to disappoint moralists, saying that "piracy is theft, which is why it is bad" - piracy is not a theft, it is copyright infringement or copyright violation (read wikipedia, or your local laws). It was already explained one billion times, but let's explain it once again (to keep all argument within this article).


    Theft is when you take someone's property, and after that owner doesn't have access to that property anywhere. This is a critical part of "theft". I.e. if you take item from the shop without paying, and shop can't sell that item anymore, then it is theft. However, when you copy material (be it music, movie or game), owner still has access to original media, and still can sell endless number of copies. So, because owner doesn't lose access to the property, it is not a theft. However, it is still copyright violation.


    In case of theft, stolen object doesn't have to be material. If you steal someone's email password, or if you log into online game, and steal someone's item, it is stell theft (although laws most likely will classify that as hacking/unathorized access). Same thing happens if you take someone's serial number/registration info and delete all trace of that information on victim's computer.


    The reason why you keep hearing that "piracy is theft" is because someone wants you to feel guilty for pirating stuff. Although this is a fine goal (since people in general will feel less guilt about pirating than they will feel about stealing something), it is a lie.

  • "If it isn't theft, then it isn't bad!"

    This one is never said in the open, but is probably by at least half of people that keep saying that "piracy is not a theft". Theft or not, it is still illegal. It is much simpler to keep words "good" and "bad"(and "right"/"wrong") away, and simply concentrate on "legal" and "illegal". This is because everyone has slightly different definition of "good" and "bad" while legality of something is defined by your laws which are at least supposed to be impossible to misinterpret. So if you still want to pirate something, keep in mind, that no matter whether you think it is "right" or "wrong" thing to do, it is still illegal, which means you might get your punishment eventually.


    The sad thing about this is that sometimes people that already bought computer game or movie (this problem doesn't affect music) have also to get pirated copy because of some problem with the game. To my experience, normally publisher is responsible for that. Some examples:


    • If the game or movie is localized and original game/movie was not in your language, then publisher might remove original soundtrack and/or text. Which means (if you know original language of the game), you will have to either to pirate original (because translation is never perfect), or tolerate bad acting and/or bad translation. The one nice example of this situation is "Witcher" computer game. The game had 3 versions: Polish, Russian, and English. Polish version had best voice acting. Russian had best translation, and worst voice acting. And Englih had mediocre voice acting and worst english translation (translators simply cut 50..75% of original material). Great, isn't it? Why is it that difficult to keep original soundtrack is beyond me. After all, we have DVD9 (not to mention bluray( at the moment.
    • The game has some kind of copy protection mechanism that doesn't work on user's computer. In this case user might be forced to buy game and then pirate it in order to play it. I know at least a few publishers that remove SECUROM and replace it with Starforce during localization. While (non disk-based)securom is pretty tame copy-protection mechanism(never had a single problem with my Spore), the destiny of starforce is to chew your CD/DVD to death, so you will have to buy game again. Also it is very picky and I heard more than one story about it causing BSODs.
    • The game has serious disk-based copy-protection, so user is unable to create a backup. In this case user will probably have to bypass copy protection mechanism in order to keep disk playable. Online authorization (steam, securom) is much better than disk-based protection, however you have no warranty that servers will exist forever. Will steam servers still exist 50 years later? I don't know, but it looks unrealistic.

  • "If you pay for your game/movie, you give money to developers!"

    This one is the funniest from all arguments.

    I do not know how cash flows in music, industry, but I worked as a hired programmer on few games, so I have at least brief idea how cash works in game industry.

    The cold truth that whether original game developer gets any money you gratuitously paid for the game depends on how they manage royalties. There are no warranties.


    At least some people won't get a dime from your money - because they were hired, paid by hour and won't get additional payment once development is over. There always will be one person that won't get your cash. Perhaps lead designer or lead programmer gets a cut from all the cash. Fine. But what about all the testers? Tool programmers? All artists? Motion capture actors? The janitor (yeah, he/she is also responsible for the game development)? Do they all get a cut from profits? I seriously doubt that. Most of those folks probably won't get any extra cash once development is over.


    Indie developer probably will get good portion of money, minus commission paid to reseller - because they might be selling stuff nearly directly. As for larger companies, this needs investigation (see this thread for more info, for example). There are couple of ways how publisher could acquire game right. Publisher could buy all rights (so game developers won't get your extra money), some of them might pay royalties, but this is not warrantied. Also keep in mind, that sometimes games are still sold when developer is long gone (example: Fallout 2, old games in general). Who is getting your money? The publisher.

    Same is probably true for the movie industry. Can you imagine camera man getting royalties for new Hollywood blockbuster? I cannot. Perhaps in music industry situation is different, but I do not know that for sure.

    There is always creator/developer who creates stuff and publisher who sells you the stuff. If they are same person/company, then you are lucky. And if they are not - then it is unclear whether creator gets anything from cash you gave or not. However thinking that all your cash goes straight to the developer is lying to yourself.

    The situation might be better for companies that develop software (not games), because they might be selling it themselves. However, in this case some developers probably still won't get a dime from your money. Certain large companies are famous for buying right for piece of software from other companies, and then selling it to end users. In this case original developer not getting anything from your cash.

  • "More cash means better games"

    It is a bit unrelated to to piracy, but I had to mention it anyway. For some strange reason, when (game) developer starts its' journey to the top, they create classics (example: Doom 1), but once they are well-known, well-paid and have tons of cash, situation sometimes changes and instead of quality product, they start aiming at larger audiences. This result in good graphics and bad gameplay. This is how (I think) we got boxes with unlimited ammo in first-person shooters, regenerating health, and enemies that scream at you to indicate where they are. This is how games from being a challenge turned into popular entertainment, like TV. There are some exceptions, but in general when product becomes popular, it somehow might get nice graphics and bad gameplay. So you get a game where you can see how much money was thrown at it judging by graphical quality, but which loses to its' predecessor when it comes to gameplay. There are exceptions, of course (and there are games that have good graphics and solid gameplay), but in general, more money does not mean better games.

  • "Why should I pay for something I can get for free". This one is obvious, but I had to mention it for the sake of completeness. Original creator(s) should be praised for his/her/their job, or there will be no more games/movies for you. If no one pays - eventually creators obviously won't get paid. However, on other hand, there will be always overpriced low-quality products, and you really need to be sure who gets your money. Which makes situation not so obvious.


How copyright should work


Here is how I see ideal copyright laws:

  • By purchasing digital product (music, video game, software), you acquire right to download, use, view, copy, recompress (or convert into another format), upload it onto any network and download it again in another resolution/language.
  • This right is can be sold/transferred to another person. After the the transfer you no longer have the rights mentioned in part 1.
  • You cannot keep the right after you gave it to someone else.
  • Only publisher or original creator can sell/give those rights in unlimited quantites.


Why it makes sense


In this situation you would be able to download movie from any p2p network you like, pay for it, and live happily ever after. You will be able to continue seeding the movie without fear of getting sued - because you are reducing transfer costs for the publisher, and make material available for other people. In this case guy that downloaded movie without paying will be responsible, not you. You will be able to convert movie or music to any format you want, to be able to listen/use it on other device. And if you had a hardware failure, you will be able to download your material again with no additional fees. And you will be able to get same thing dubbed into another language.

..and why it is never going to happen



  • This set of rules doesn't mention broadcast/public performance rights.
  • Such scheme requires central registry of your rights.
  • Such scheme requires a way to identify what you are using on your PC.
  • And I cannot imagine any way to manage those rights and keep user to be able to convert material into any possible format.
  • This will mean heavy losses for publishers - user won't have to buy same movie/game again if disk was destroyed by accident. Which would mean either higher prices, or lower-quality material
  • To be implemented perfectly, this system should not be dependant on any external servers. Which makes it impossible to implement.
  • We are not living in perfect world, unfortunately.


What we have now instead.



Right now two closest things we have are Valve's Steam and any DRM.


Steam is good because it allows you to get required language pack for your game, but it doesn't qualify for the ideal copyright management system, because it doesn't allow you to upload (to p2p)/sell your games, and because your account can be stolen. Also, if server is offline, you won't be able to play your game.


And DRM doesn't qualify because of dependency on external server (server is down - no music for you), and because it forbids copying. Pity that inventors of DRM seems to be more interested in restricting copying than in making user's life easier.


Conclusion


And, this is it...
End of article. There are still imperfect laws and there is still piracy. What to do? Honestly, I don't know. Perhaps, this writing will give someone better ideas, and perhaps someday there will be better copyright management system. Maybe even ideal one. Or maybe nothing will ever change, and people will always equalize piracy to theft, or they will still thing that if it is not a theft, than it is good thing to do, not recognizing that "there is no black and white, only shades of gray", and piracy situation is not so simple.
Only time will tell.

2009/09/29

Example-based texture generation with SDL/OpenGL using bruteforce


Intro:

During last weekend I've decided to try implementing texture generation algorithm, where texture is being generated from example (photo or something else). I've got several interesting results, and a couple of problems. The attempt was inspired by this paper(PDF link), but I decided to try simple bruteforce before trying to use "jumpmaps".

Links:

  • Steve Zelinka's publications(includes couple of interesting material, like "Interactive Texture Synthesis on Surfaces Using Jump Maps.", "Mesh Modelling With Curve Analogies". Definitely worth checking out, however, I was only interested in texture generation at the moment.
  • Jump maps on surfaces (pdf, Steve Zelinka and Michael Garland)


Results:
Here is the example pattern I used:

And here is the result I got:


Algorithm:

  • Fill destination image with black, and make every pixel transparent.
  • Copy any pixel from pattern image into top-left corner of destination image.
  • For every transparent pixel of destination image (i.e. alpha == 0) with non-transparent neighbors within search distance, replace it with pixel from pattern image where non-transparent pixels in neighborhood most closely resemble non-transparent pixels in destination image's neighborhood of current pixel. Best matching pixel found by bruteforce search (yes, it is slow). Repeat until there are no more transparent pixels left.


How neighborhoods are compared:
There are several ways to do this:

  • Compute distance(within search distance) for every non-transparent pixel in current neighborhood, add distances up, divide sum by number of non-transparent pixels. Find pixel in pattern image with the smallest distance.
  • Add up (into rgb vector) all non-transparent pixel within search distance in destination surface into one vector, Add up up all non-transparent pixels within search distance from candidate pixel in pattern surface, compute distance between them. Find surface with smallest distance.

Things to keep in mind:

  • When calculating differences, watch out for transparency. DO not use pixels at offsets (from candidate) where source or pattern pixel are transparent.
  • Destination surface should be tileable, so pixel operations wrap around. Pattern is no tileable, so pixels near the border should not used.


Good things:

  • Tileable (well, you can see some repeating patterns, bot no obvious seams)
  • Looks great/realistic.


Problems:

  • It is incredibly slow. Example image took at least one hour (or more) to be finished. Cannot be used in realtime.
  • I've run into problems when I tried to do same thing using OpenGL/GLSL and DirectX/HLSL (to see if it will work faster):

    • In DirectX HLSL shader could not be compiled. D3DXCompileShaderFromFile simply hung during compilation, and I wasn't in the mood for assembly.
    • In OpenGL, it looks like there is no way to select mip level from within GLSL fragment shader. So, I can say farewell to cheap search optimization.
    • In OpenGL, I hit weird problem - entire system becomes unusable (stops responding, etc) when current frame is being rendered. And because one frame takes several seconds, this means, you can't do anything else on the system while application is running.



Perhaps it makes sense to try another algorithm or reimplement this thing on CUDA. Maybe I'll do it some other time, maybe not.

Code:
Code is free to use/redistribute for non-commercial purposes.
Code didn't completely fit into window, and I'm not in the mood for template hacking right now. If you want to see everything, select, copy and paste code into notepad (or kedit/jedit or whatever).

sdl version
main.cpp:


#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <common/timer.h>
#include <common/keyboard.h>
#include <wchar.h>
#include <math.h>

using Keyboard::keyPressed;

static Timer timer;
static int texPowX = 9;
static int texPowY = 9;
static int texWidth = 1 << texPowX;
static int texHeight = 1 << texPowY;
static int scrWidth = texWidth;
static int scrHeight = texHeight;
static int texMaskX = texWidth - 1;
static int texMaskY = texHeight - 1;
static int searchWidth = 3;
static int searchHeight = 3;
struct Dist{
unsigned long r, g, b;
};

size_t numPatternPixels = 0;
Dist* precomputedDist = 0;

struct Pixel{
unsigned char b, g, r, a;
};

void fillPixel(int x, int y, SDL_Surface* dst, SDL_Surface* src, int dstMaskX, int dstMaskY){
Pixel& dstPix = *((Pixel*)((char*)(dst->pixels) + sizeof(Pixel)*x + dst->pitch*y));

int xMin = x + texWidth - searchWidth;
int xMax = xMin + searchWidth*2;
int yMin = y + texHeight - searchHeight;
int yMax = yMin + searchHeight*2;


int numFilled = 0;
for (int curY = yMin; curY < yMax; curY++)
for (int curX = xMin; curX < xMax; curX++){
Pixel& cur = *((Pixel*)((char*)(dst->pixels) + sizeof(Pixel)*(curX & texMaskX) + dst->pitch*(curY & texMaskY)));
if (cur.a != 0)
numFilled++;
}

if (numFilled == 0){
int srcX = rand() % src->w;
int srcY = rand() % src->h;
dstPix = *((Pixel*)((char*)(src->pixels) + sizeof(Pixel)*srcX + src->pitch*srcY));
dstPix.a = 0xFF;
return;
}

int storedSrcX = rand() % src->w;
int storedSrcY = rand() % src->h;
float lastDifference = 3.40282347e+37F;

//unsigned char mask =

for (int srcY = searchHeight; srcY < (src->h - searchHeight); srcY++)
for (int srcX = searchWidth; srcX < (src->w - searchWidth); srcX++){
float curDifference = 0;
int numPixels = 0;
for (int tmpY = -searchHeight; tmpY < searchHeight; tmpY++)
for(int tmpX = -searchWidth; tmpX < searchWidth; tmpX++){
Pixel& tmpSrc = *((Pixel*)((char*)(src->pixels) + sizeof(Pixel)*(srcX+tmpX) + src->pitch*(srcY+tmpY)));
Pixel& tmpDst = *((Pixel*)((char*)(dst->pixels) + sizeof(Pixel)*((x + dst->w + tmpX) & dstMaskX) + dst->pitch*((y + dst->h + tmpY) & dstMaskY)));
if (tmpDst.a){
numPixels++;
int dr = tmpSrc.r - tmpDst.r;
int dg = tmpSrc.g - tmpDst.g;
int db = tmpSrc.g - tmpDst.g;
curDifference += dr*dr + dg*dg + db*db;
}
}
if (numPixels)
curDifference /= (float)numPixels;
if (curDifference < lastDifference){
lastDifference = curDifference;
storedSrcX = srcX;
storedSrcY = srcY;
}
}

dstPix = *((Pixel*)((char*)(src->pixels) + sizeof(Pixel)*storedSrcX + src->pitch*storedSrcY));
dstPix.a = 0xFF;
}

int main(int argc, char** argv){
SDL_Init(SDL_INIT_VIDEO);

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

Keyboard::init();
timer.reset();
timer.setMaxFps(100);
timer.setMinFps(5);

SDL_Surface* screen = SDL_SetVideoMode(scrWidth, scrHeight, 32, SDL_SWSURFACE);

SDL_Surface *texture = SDL_CreateRGBSurface(SDL_SWSURFACE, scrWidth, scrHeight, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);

if (screen == 0){
fprintf(stderr, "couldn't set mode %dx%d!\n", 640, 480);
SDL_Quit();
return -1;
}

SDL_Surface *image = IMG_Load("example.png");
SDL_Surface *example = SDL_CreateRGBSurface(SDL_SWSURFACE, image->w, image->h, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
SDL_BlitSurface(image, 0, example, 0);

SDL_FreeSurface(image);
SDL_LockSurface(example);

numPatternPixels = example->w * example->h;
precomputedDist = new Dist[numPatternPixels * 256];
memset(precomputedDist, 0, sizeof(Dist)*numPatternPixels*256);
for (int mask = 0; mask < 256; mask++){
printf("processing mask %d", mask);
for (int y = 0; y < example->h; y++)
for (int x = 0; x < example->w; x++){
Dist& curDist = precomputedDist[x + y*example->h + mask*numPatternPixels];
}
}


size_t numPixels = scrWidth*scrHeight;
SDL_LockSurface(texture);
for (int y = 0; y <scrHeight; y++)
for (int x = 0; x < scrWidth; x++){
Pixel& cur = *((Pixel*)((char*)(texture->pixels) + sizeof(Pixel)*x + texture->pitch*y));
cur.r = cur.g = cur.b = 0xFF;
cur.a = 0;
}

SDL_UnlockSurface(texture);

int x = 0;
int y = 0;

bool running = true;
while (running){
SDL_Event event;
if (SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
running = false;
break;
};
}
timer.update();
Keyboard::update();

SDL_LockSurface(texture);

for (int i = 0; (i < 100) && (y < scrHeight); i++){
/*Pixel& cur = *((Pixel*)((char*)(texture->pixels) + sizeof(Pixel)*x + texture->pitch*y));
cur.r = rand() & 0xFF;
cur.g = rand() & 0xFF;
cur.b = rand() & 0xFF;
cur.a = 0xFF;*/
fillPixel(x, y, texture, example, texMaskX, texMaskY);
x++;
if (x >= scrWidth){
x = 0;
y++;
}
}
SDL_UnlockSurface(texture);

static bool saved = false;
if ((y >= scrHeight) && !saved){
SDL_SaveBMP(texture, "result.bmp");
saved = true;
}

if (keyPressed(SDLK_ESCAPE))
running = false;

SDL_BlitSurface(texture, 0, screen, 0);
SDL_UpdateRect(screen, 0, 0, scrWidth, scrHeight);
}

delete[] precomputedDist;
SDL_UnlockSurface(example);
SDL_FreeSurface(example);
SDL_FreeSurface(texture);
SDL_Quit();
return 0;
}





OpenGL version:


#include <SDL/SDL.h>
#include <glee/glee.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <common/glutils.h>
#include <common/Framebuffer.h>
#include <common/textures.h>
#include <common/timer.h>
#include <common/keyboard.h>
#include <FTGL/FTGL.h>
#include <FTGL/FTGLPixmapFont.h>
#include <wchar.h>
#include <math.h>

using Keyboard::keyPressed;

static Timer timer;
static const int texSize = 128;
static int scrWidth = texSize;
static int scrHeight = texSize;
static const int searchSize = 3;

static const char* vsText =
"varying vec2 texCoords;\n"
" const float gridSize = 10.0;\n"
"void main(void){\n"
" gl_Position = ftransform();\n"
" texCoords = gl_MultiTexCoord0;\n"
"}\n";

static const char* fsText =
"varying vec2 texCoords;\n"
"uniform sampler2D srcTex;\n"
"uniform sampler2D patternTex;\n"
"uniform vec2 srcTextureSize;\n"
"uniform vec2 srcPixelSize;\n"
"uniform vec2 patternTextureSize;\n"
"uniform vec2 patternPixelSize;\n"
"uniform vec2 searchSize;\n"
"void main(void){\n"
" vec2 uv = texCoords;\n"
" vec4 thisColor = texture2D(srcTex, uv);\n"
" gl_FragColor = thisColor;\n"
" if (thisColor.a > 0.0)\n"
" return;\n"
" vec4 leftColor = texture2D(srcTex, uv - vec2(srcPixelSize.x, 0.0));\n"
" vec4 topColor = texture2D(srcTex, uv - vec2(0.0, srcPixelSize.y));\n"
/*" vec4 rightColor = texture2D(srcTex, uv + vec2(srcPixelSize.x, 0.0));\n"
" vec4 bottomColor = texture2D(srcTex, uv + vec2(0.0, srcPixelSize.y));\n"
" if ((leftColor.a == 0.0)&&(topColor.a == 0.0)&&(rightColor.a == 0.0)&&(bottomColor.a == 0.0))\n"*/
" if ((leftColor.a == 0.0)&&(topColor.a == 0.0))\n"
" return;\n"
" float lastDistance = 0.0;\n"
" bool first = true;\n"
" vec2 lastCoords = vec2(0.0);\n"
" vec2 patEndCoords = (patternTextureSize - searchSize - vec2(0.5))*patternPixelSize;\n"
" vec2 patStartCoords = (searchSize + vec2(0.5))*patternPixelSize;\n"
" vec2 patCoords = patStartCoords;\n"
" for(patCoords.y = patStartCoords.y; patCoords.y < patEndCoords.y; patCoords.y += patternPixelSize.y){\n"
" for(patCoords.x = patStartCoords.x; patCoords.x < patEndCoords.x; patCoords.x += patternPixelSize.x){\n"
" vec2 tmpCoords = vec2(0.0);\n"
" float distance = 0.0;\n"
" for(tmpCoords.y = -searchSize.y; tmpCoords.y <= searchSize.y; tmpCoords.y += 1.0){\n"
" for(tmpCoords.x = -searchSize.x; tmpCoords.x <= searchSize.x; tmpCoords.x += 1.0){\n"
" vec4 srcColor = texture2D(srcTex, (tmpCoords*srcPixelSize) + uv);\n"
" vec4 patternColor = texture2D(patternTex, (tmpCoords*patternPixelSize) + patCoords);\n"
" if (srcColor.a > 0.0){\n"
" vec3 dist = srcColor.xyz - patternColor.xyz;\n"
" distance += dot(dist, dist);\n"
" }\n"
" }\n"
" }\n"
" if (first||(distance < lastDistance)){\n"
" lastDistance = distance;\n"
" lastCoords = patCoords;\n"
" first = false;\n"
" }\n"
" }\n"
" }\n"
" gl_FragColor = texture2D(patternTex, lastCoords);\n"
" gl_FragColor.a = 1.0;\n"
"}\n";


void textureRect(float x, float y, float width, float height){
float x1 = x, y1 = y, x2 = x1 + width, y2 = y1 + height;
glBegin(GL_TRIANGLE_FAN);
glTexCoord2f(0, 0);
glVertex2f(x1, y1);
glTexCoord2f(1, 0);
glVertex2f(x2, y1);
glTexCoord2f(1, 1);
glVertex2f(x2, y2);
glTexCoord2f(0, 1);
glVertex2f(x1, y2);
glEnd();
}

int main(int argc, char** argv){
srand(GetTickCount());
SDL_Init(SDL_INIT_VIDEO);

SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 0);

Keyboard::init();
timer.reset();
/*timer.setMaxFps(100);
timer.setMinFps(5);*/

if (scrWidth <= 128){
scrWidth = texSize * 4;
scrHeight = texSize * 4;
}

if (SDL_SetVideoMode(scrWidth, scrHeight, 32, SDL_OPENGL) == 0){
fprintf(stderr, "couldn't set mode %dx%d!\n", scrWidth, scrHeight);
SDL_Quit();
return -1;
}

FTGLPixmapFont font("DejaVuSans.ttf");
font.CharMap(ft_encoding_unicode);
font.FaceSize(10);
FTGLPixmapFont largeFont("DejaVuSans.ttf");
largeFont.CharMap(ft_encoding_unicode);
largeFont.FaceSize(40);
SDL_ShowCursor(SDL_DISABLE);

glClearColor(0, 0, 0, 0);
glClearDepth(1.0);

Framebuffer *framebuffers[2] = {0, 0};
framebuffers[0] = new Framebuffer();
framebuffers[1] = new Framebuffer();
for (int i = 0; i < 2; i++)
framebuffers[i]->setSize(texSize, texSize);

int currentFramebuffer = 0;

Texture* texture = new Texture("example3.png", GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR);

glEnable(GL_DEPTH_TEST);
glEnable(GL_TEXTURE_2D);

GLuint filterProgram = 0;

filterProgram = createProgram(vsText, fsText);
checkGlError();

GLuint srcSamplerUniform = glGetUniformLocation(filterProgram, "srcTex");
GLuint patternSamplerUniform = glGetUniformLocation(filterProgram, "patternTex");
GLuint srcTextureSizeUniform = glGetUniformLocation(filterProgram, "srcTextureSize");
GLuint srcPixelSizeUniform = glGetUniformLocation(filterProgram, "srcPixelSize");
GLuint patternTextureSizeUniform = glGetUniformLocation(filterProgram, "patternTextureSize");
GLuint patternPixelSizeUniform = glGetUniformLocation(filterProgram, "patternPixelSize");
GLuint searchSizeUniform = glGetUniformLocation(filterProgram, "searchSize");

bool running = true;
while (running){
SDL_Event event;
if (SDL_PollEvent(&event)){
switch(event.type){
case SDL_QUIT:
running = false;
break;
};
}
timer.update();
Keyboard::update();

currentFramebuffer = currentFramebuffer ^ 1;
Framebuffer* src = framebuffers[currentFramebuffer];
Framebuffer* dst = framebuffers[currentFramebuffer ^ 1];

if (keyPressed(SDLK_ESCAPE))
running = false;

glDisable(GL_DEPTH_TEST);

/*
framebuffer rendering
*/
dst->begin();
glClearColor(0, 0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, dst->width, 0, dst->height);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, src->renderTexture);

glEnable(GL_ALPHA_TEST);
glAlphaFunc(GL_GREATER, 0);
float f = 1.0f/ 9.1f;
/*glEnable(GL_BLEND);
glBlendFunc(GL_ONE, GL_ONE);*/
static bool first = true;
textureRect(0, 0, (float)(src->width), (float)(src->height));
bool justFinished = false;
if (first){
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texture->tex);

float s = (float)(rand() % texture->width)/(float)texture->width;
float t = (float)(rand() % texture->height)/(float)texture->height;
glTexCoord2f(s, t);
glBegin(GL_POINTS);
glVertex2f(0, 0);
glEnd();

glRasterPos2i(10, 20);

//textureRect((float)(rand() % dst->width), (float)(rand() % dst->height), (float)(texture->width), (float)(texture->height));
//textureRect(0, 0, (float)(texture->width), (float)(texture->height));
first = false;
}
else{
textureRect(0, 0, (float)src->width, (float)src->height);
static int x = 0;
static int y = 0;
static const int tileSize = texSize;
static int xSize = texSize/tileSize;
static int ySize = texSize/tileSize;
static int counter = -tileSize;
if (y < ySize){
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, texture->tex);
glUseProgram(filterProgram);
if(filterProgram){
glUniform1i(srcSamplerUniform, 0);
glUniform1i(patternSamplerUniform, 1);
glUniform2f(srcTextureSizeUniform, (float)src->width, (float)src->height);
glUniform2f(srcPixelSizeUniform, 1.0f/(float)src->width, 1.0f/(float)src->height);
glUniform2f(patternTextureSizeUniform, (float)texture->width, (float)texture->height);
glUniform2f(patternPixelSizeUniform, 1.0f/(float)texture->width, 1.0f/(float)texture->height);
glUniform2f(searchSizeUniform, (float)searchSize, (float)searchSize);
}

float x1 = (float)(x * tileSize);
float s1 = x1/(float)(src->width);
float x2 = (float)((x+1) * tileSize);
float s2 = x2/(float)(src->width);
float y1 = (float)(y * tileSize);
float t1 = y1/(float)(src->height);
float y2 = (float)((y+1) * tileSize);
float t2 = y2/(float)(src->height);
glBegin(GL_TRIANGLE_FAN);
glTexCoord2f(s1, t1);
glVertex2f(x1, y1);
glTexCoord2f(s2, t1);
glVertex2f(x2, y1);
glTexCoord2f(s2, t2);
glVertex2f(x2, y2);
glTexCoord2f(s1, t2);
glVertex2f(x1, y2);
glEnd();

counter++;
if (counter >= tileSize){
counter = 0;
x++;
if (x >= xSize){
x = 0;
y++;
if (y >= ySize){
//save texture
justFinished = true;
}
}
}
}
//textureRect(0, 0, (float)src->width, (float)src->height);
}
glUseProgram(0);
glActiveTexture(GL_TEXTURE0);
dst->end();

if (justFinished){
SDL_Surface* tmp = SDL_CreateRGBSurface(SDL_SWSURFACE, dst->width, dst->height, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0xFF000000);
SDL_LockSurface(tmp);
glBindTexture(GL_TEXTURE_2D, dst->renderTexture);
glGetTexImage(GL_TEXTURE_2D, 0, GL_BGRA, GL_UNSIGNED_BYTE, tmp->pixels);

SDL_UnlockSurface(tmp);
SDL_SaveBMP(tmp, "result.bmp");
SDL_FreeSurface(tmp);
}


glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0, scrWidth, scrHeight, 0);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();

glClearColor(0, 0, 0.5f, 0);
glClear(GL_COLOR_BUFFER_BIT);

glBindTexture(GL_TEXTURE_2D, dst->renderTexture);

//textureRect(0, 0, (float)dst->width, (float)dst->height);
textureRect(0, 0, (float)scrWidth, (float)scrHeight);

glBindTexture(GL_TEXTURE_2D, 0);

wchar_t buf[256];
swprintf(buf, 256, L"fps: %.2f", timer.getFps());
glRasterPos2i(10, 20);

font.Render(buf);

glFlush();
SDL_GL_SwapBuffers();
}

if (glIsProgram(filterProgram))
glDeleteProgram(filterProgram);

delete texture;
for (int i = 0; i < 2; i++)
delete framebuffers[i];

SDL_Quit();
return 0;
}




2009/09/03

The meaning of life and everything


Well, this time it isn't about programming or computers, but this was bothering me for a while. Warning: this text might be depressing, because I have no answer for that question.

Have you ever thought about this: everything you see around you eventually will disappear. Think about it. Many human-made object will be broken within 100 years. People last less than 200 years, so in a 200 years there probably will be no you (well unless you are lucky enough to find or invent immortality cure). Buildings last longer, but most of them won't survive even a few hundreds of years. In a few billions of years sun will explode and destroy the planet. And in a few more billions of years universe will collapse back to the single point it appeared from. So there will be nothing left at all.

"Daisy Owl" nicely illustrates the problem(here).

This makes me wonder - what is the point of everything? Every single thing that exists will eventually disappear. And in the end there will be no one left to tell the tale or remember anything. As if nothing ever existed at all.

I guess happiest people never run into that question.

The problem here is not that all matter will disappear. Some of the theories about the end of universe suggests that matter will remain (but temperature will drop to absolute zero). The problem is that every object or person (including yourself) around you already doesn't exist anymore - at some point in the future (or there is no longer any observer for this object), which is hard to accept simply because it doesn't feel "right".

Another problem is that human mind wants a meaning for everything (well, at least mine does). The most logical answer is that life has no meaning and is an accident. However, this idea is incredibly hard to accept. I guess that it is easier for religious people to face this question - after all, at least in some religions you are immortal (your soul, which is basically you), or you are reborn, and at the "end of times" you will meet all other people and you will live happily ever after. On other hand that eternal life in heaven/hell might not be that different from "end of time" in one of scientific theories - everything kind of "stops" and remains same forever.

If you think more about all of this, you can notice one interesting thing: the more advanced technology becomes, the less chances it has to exist for long time and less chances it has to be comprehensible for next civilization should humanity disappear. Think about it. As far as I know, writings on the stone can exist for a lot of time. Paper is more convenient, but requires extra care in order to persist for many years. And in order to make all information in your computer inaccessible, all you need is to cut the power. Also, the way information is stored is incredibly complicated, which means that should humanity disappear, the next civilization won't be able to get much from remains of our computers. Digital info is incredibly fragile itself, this article illustrates some problems. If you think about this, our civilization suddenly looks incredibly fragile, like a card house - just lose a few key technologies, and you are back in the medieval times.
This returns me to original problem:

  • Our world is fragile, and should something serious happen, many things we know will be lost forever.
  • In fact, nearly everything (on earth, at least) is going to disappear eventually hardly leaving the trace. Organic creatures die. Machinery rusts. Buildings are being destroyed by the elements. Eventually, Sun itself will "blow up" and burn the earth down, or it will eventually stop producing light.
  • Most of those events cannot be avoided.


Perhaps I'm missing something, but for me all this combined together doesn't make any sense.

So, what is the point of all this? What is the meaning of everything? The purpose?

Ideas, anyone?

2009/05/07

How to become a programmer, How to choose programming language


Introduction


On many message boards and forums I've seen many wannabe programmers.
"Guys, I want to become a programmer! Can you help me?". Sounds
familiar, isn't it? Those wannabes were asking same basic questions, over and over:

  • "Which programming language should I learn?"
  • "Which programming language is the best?".
  • "Can you recommend a good book about programming?"

Each of those guys (well, maybe there also were few girls) appeared, asked question, got some answers, disappeared and was never heard of again. I assume that some of them maybe were able to learn something, and maybe even became a really skillful professionals, but I believe most of those people have failed to become programmers. Because to my opinion they were doing it wrong from the beginning.

What you should never do if want to become a programmer



  • Never ask "Which programming language is the best?" on any message board.

    This is because there is no "the best" programming language, but many people will think otherwise. Typically, asking this question anywhere will provoke horrible flamewar where each person will attemp to prove that their language is "the best one". Of course, few first responses might be helpful for you, people even might provide some polite, useful, and unbiased info, but (in most situations) the thread will quickly turn into flamewar, or people will start demonstrating various language features using complex constructs (which will be completely uncomprehensible for a wannabe). Either way, there will be a very good chance that all discussion won't be helpful for a newbie.

  • Don't waste your time searching for "the best book" about your programming language.
    You will waste more time searching than you could spend programming. You will need a book, but it isn't really important to get best available book, especially if you are just a beginner. The practice and writing code is more important than books, at least in the beginning. So if you are in doubt (about which book to read), pick any book about your language, and use it for study.


What is really important


Book and choice of language are secondary things. Here is a list of requirements to become a programmer:

  • You should have a really good motivation or a goal. "I want to become a professional and get paid a lot of $$$" won't work - because you don't need to be programmer in order to earn cash. However "I want to make my own game" will be perfect.
  • Programming should be always interesting and fun for you. If it isn't interesting, if you aren't having fun when you write programs, then you won't be able to learn it quickly and efficiently. If your first programs look like a miracle/wonder for you, and you feel joy and happiness once it is finished, then you will have no trouble learning more difficult aspects. However, if you are bored to death each time you want to write something, you are not going to learn much. You will probably decide not to learn programming.
  • Do not try to pick "the best" language. Try to pick language which is "the best" just for you. If you are more comfortable with Python than with C++, then you probably will achieve more with Python.
  • When you are practicing, you should stick with stuff which is interesting for you. Making test program that inverts matrices is boring - pure math, not much to do. However, making "tetris" is not boring. Try to avoid boring examples from book. Keep books nearby and frequently use it as reference, but in order to learn more you should solve interseting, real-life problems.


Specialization: one important question you should answer before you start


You can't (easily) become universal programmer that can write in every language on every operating system. You should decide what you want to program, and pick your language accordingly.
Below is a short list of languages with short descriptions and explanations about how they are commonly used.


  • Assembler

    Not really a langauge. Assembler is simply a mnemonic representation of CPU instruction codes. So, (although there were some advanced assembler packages, like MASM), it is tightly tied to CPU, and will be mostly useful when programming low-level otpimized routines for that CPU. It won't be easy to use it for large projects.

  • C

    One of the popular languages, right now mostly used with OpenSource software, mostly on Unix/Linux/BSD and other unix-like system. It is very close to assembler (you still manipulate memory directly), but isn't tied to CPU type, so, unlike Assembler, C code is portable.

  • C++

    High-level language, with some of C features, and with higher-level features: objects, templates, etc. This language is really complex, and although you can learn syntax in few days, it might year or two to master it. C++ is used in most applications on many platforms, in many different tasks. It is portable.

  • Python

    High-level interpreted language. Platform-independant, isn't supposed to be compiled into machine code.

  • Java

    High level language which is meant to be completely platform-independant. Java program compiles into pseudo-code of non-existent processor. The code is run using interpreter, which is available for several platforms. So it is a hybrid of compiled and interpreted language.

  • C# and .NET

    Microsoft's attempts to make another java. Also compiles into pseudocode, but resulting application will be windows-only, because interpreter is developed only for windows (yes, I know about "Mono project", and I know it isn't frequently used).

  • Perl

    Interpreted language suitable for text processing, scripts, making tools, also for web.

  • PHP

    Interpreted language, used on the web servers.

  • JavaScript

    Used in web-pages, hardly for anything else.

  • Shell

    Unix shell script language. Used for making tools and scripts.



There are also several languages I never used much:

  • Haskell
  • Prolog
  • Common Lisp
  • OCaml

I"ve heard a lot of good things about those but I didn't need to use any of them. All of those languages are supposed to be powerful, but they are not "mainstream", and they aren't used frequently. Anyway, many people recommend at least to take a look at them.


Things you should keep in mind when you select language



  • Interpreted languages have better development speed, but worse performance, when compared to compiled languages. I.e. You might create Python program using less time, but C++ program are likely to have better performance. Notice that this a not always true, but in most cases. It is easy to make slow C++ program, especially if you don't know what you are doing. Keep in mind that although you don't need always need maximum performance or maximum development speed, in some situation they are important. For example, you wouldn't want to throw all power of C++ to make a simple text processing program - it will be faster to make it using shell script, python, perl, etc. On other hand, it won't be wise to make a raytracer in python - it is possible thing to do, of course, but for this application you will need a good performance, low-level memory access, etc. Notice, that in this situation you might consider assembler, but development time will be too high, and good C++ compiler might be able to produce better code than you.
  • You might not want to be tied to just one operating system, architecture or platform. If you want to be able to switch platforms later, then you should avoid Assembler(it is linked to just one CPU type), C# and all .NET languages (right now they are basically Windows-only), and you should be extra careful with C++ and compiled languages. Several companies provide language extensions which will be a lot of trouble if you ever decide to port your application to another platform (or simply if you decide to use another compiler). FIne example of that is microsoft's "safe" version of C string manipulation routines - "strcpy_s" and similar. They are not in C++ standard, so if you decide to port your application, you will have to write those routines yourself, or replace them with something else. Also, all compiled languages need libraries to interact with operating system, or create GUI windows, etc. This is because that standard for those compiled languages typically doesn't define libraries for all tasks. Because of this you should be careful about what libraries you use - they shouldn't be available only on one platform.
  • Some languages (typically interpreted or high-level: java, python, etc) try to help you and do some things for you - manage memory, for example (Python, Java, etc). Other languages assume that you are the "smart one" and you know what you are doing. They don't babysit you, and you are supposed to free memory yourself (C, C++ in some cases). Some people like automatic memory management, some don't like it. Typically, if you want to have all things under your control, you won't like automatic memory management. Keep that in mind when selecting language.
  • If you want to program for web, then you will probably have to use interpreted languages: Perl, Python, PHP, JavaScript. Compiled languages can be used to the some extent, but they won't help you much. This is because PHP and Javascript are easy to integrate into page, and you can't do same thing with C++ program.
  • IF you are using Linux/Unix-like system, then the first thing you might want to learn are shell scripts. This is because they are used very frequently. Also you might want to learn Perl or Python - because they are also very popular and frequently are being used for making simple unix/linux tools.


Conclusion


This is it. Think about all that and pick language. If you still don't know what to choose, pick anything and start studying it, but remember that you can always select another language later.

2009/04/27

"Windows versus Linux": yet another opinion


Introduction


It is widely known that "Linux vs Windows" is one of the most popular discussion topics. There are numerous large threads (for example, this one), discussing this question. Typically such threads never end (unless they turn into flamewar, and moderators close them), and new messages are being added during many years. After reading yet another thread where several people tried to prove that

Why is this question so popular


People keep discussing this because of (on of the) following reasons:

  • Because they see only better side of their operating system, and refuse to accept that there are weaknesses in their operating system.
  • Because they finally discovered that "there is no ideal operating system", and are trying to explain it to someone else.
  • Because they don't know which OS is more suitable for them, and didn't think about using search engines
  • Because they like trolling. (Windows/Linux/MacOS users are very easy to enrage by making comments about their operating system)


Why Linux users discuss it


(Based on personal experience, not guaranteed to be true about every user) Most linux user go through "fanboy" (or "euphoria") phase. This typically happens within few months after user learned basic skills and medium-level system skills, used few applications, made first useful shell script, learned iptables, etc.

For example, there is a KDE application called kstars, which displays night sky for any place on earth for any date within few thousands of years from now. It also can control telescope, and has access to large database of stars. And it is free. And you can get its' source code, read it, modify, and derive your own application from it, and sell it. Now think about it - you have very complex, cool-looking application, which can control telescope, display sky for any point on earth and any date in human history, AND it is free, AND you can get source code, sell it. Even though I knew about GPL long before I discovered kstars, I couldn't understand how this is possible. And there are tons of very useful shell utilities, graphical desktops, compilers, browsers, there is web-server software, ipfilter software, and tons and tons of other utilities and all this is free. If you are know at least a little bit about programming, you will be able to estimate how much it would cost to develop all this stuff. If you have some medium-level programming skills, then you will be delighted to discover that you can tear any piece of software apart, read its' source code, understand how it works and modify it. When you will understand all this, (especially if you are a programmer) your mind first will refuse to accept this("how is this possible? We are paying for other software, why all this stuff is free?"), and then you will decide that you are in heaven and linux is the best thing ever created by mankind, so you should go forth and spread the word about opensource.

Congratulations, at this state you will become a "fanboy" which will attempt to convert all friends and relatives into using linux. This attitude will eventually wear off (it will take from several weeks to several months, depending on your personality. Some people are never cured, though).

Why windows users discuss it


Honestly, I have no idea. I've been using Windows for several years, and it never looked like something I would say is "the best operating system ever".

Personal experience with both platform


Windows: pros and cons


Pros:

  • Well-known, widely spread, has most market share.
  • Supports newest hardware.
  • Has games.
  • Has visual studio (don't get me wrong - it has a bunch of really horrible features (like IntelliSense), but VS2005 had best GUI I saw since Delphi 5)

Cons:

  • Price.
  • Malware. You'll need AV software to deal with malware/viruses, and not all AV packages are free.
  • For most problems solution is reinstallation
  • Reinstallation takes forever. If you have a lot of custom software, which is configured for your needs, and managed to completely kill your system (so the only reinstallation from scratch will help), then it will take up to several days to repair everything.
  • Program settings are hidden in registry, which isn't really well documented. Which means if registry is damaged, you will lose all program settings. Also, it won't be that easy to recover the registry.
  • When you discover a bug, you are not allowed to fix it, even if you can. You'll have to wait another patch, which will introduce more bugs.
  • Poor quality of desktop environment. Only one desktop, can't configure shortcuts (not on WinXP, at least).
  • Fresh installation has poor selection of software and can't do much useful. Will need codecs, etc.
  • Many system-wide actions require reboot.
  • License conditions. Average Windows (XP) machine is allowed to connect 10 network devices at once (fixing this will probably break EULA). As far as I know, there is also limit on the number of hardware upgrades.
  • Commercial software isn't guaranteed to be free from bugs, to be usable or to do what you want. See EULAs.
  • Doesn't include development tools.
  • You can't legally become better programmer by analyzing this system - source code is available, reverse-engineering s forbiddeb by EULA. Basically, it is a car where engine compartment is welded shut.

Linux: pros and cons


Pros:

  • Price.
  • Several very configurable desktop environment (always liked KDE desktop, never liked Windows desktop)
  • Supported hardware "Just works" without additional steps.
  • Large amount of software available.
  • Very good environment for a programmer (unless he/she uses .NET) Provides good opportunity to learn code, use source from real projects (instead of book examples), many languages and dozens of free tools.
  • Most problems can be fixed without reinstalling system.
  • Most actions doesn't require reboot.
  • No limit on the number of connections, number of hardware upgrades.

Cons:

  • Smaller selection of commercial games. However, smaller companies tend to create Linux version of games more frequently.
  • No alternative to visual studio. Quality of free IDEs will vary. However, working without IDE seems to be the best option, and even simple text editor might be used as a sufficient alternative.
  • Newest hardware frequently isn't supported - because manufacturers do not want to create drivers or doesn't provide specification.
  • Using USB devices (web-cameras, scanners, cameras) can be a real trouble.
  • You should be willing to learn something to be comfortable on Linux.
  • Less popular than windows, although frequently used in large number of network devices (routers, modems, etc)
  • Available software might not be completely finished, and probably is still in development. On other hand, this also applies to commercial software.
  • You will have to be careful when upgrading hardware - you will have to research if your hardware is supported before purchasing something. Linux will work fine on old machines, but if you have top hardware (which isn't supported yet), you might not be able to even install Linux in some cases. This issue is known to cause problems for people that suddenly decide to move from Windows to Linux.

Commonly used myths and incorrect arguments


General



  • "My commercial applcation isn't available on your platform! Therefore, my platform is superior"

    Not true. When you have application you can't live without (maybe it is your main source of income), then using this application is more important for you that choosing certain operating system. I.e. you use OS where application works, instead of using applications that work on your OS (as people normally do). This is not an OS problem.

  • "Average user can't use Linux!"

    First, there is no "average user". Self-proclaimed "experts" often can get stuck with basic problems, while inexperienced novices can solve difficult things because it is more interesting for them. System administrator or programmer might have problems with word processors, while someone experienced with word processor surely will have problems compiling even basic application. Every user has his/her own area of expertise, and because using computer doesn't require license, everyone can be a computer user. Which means there will be no "average"

    Second, "complete newbie" can use Linux system in the same fashion as Windows machine. This is because configured Linux system doesn't look too different from windows machine. On Windows machine you will have graphical desktop with icons and buttons, where each program is window, and each window can be moved/resized with mouse. And, guess what? On Linux it will be exactly same things: graphical desktop with icons and buttons, where each program is a window, and each window can be moved/resized with mouse (although software is slightly different). To my experience true computer newbie will operate both systems with similar level of success, and might not even realize which operating system is installed on his computer.

  • "Linux is more difficult to learn"

    Not true. If you start from zero level on either system, you will have to spend similar amount of time to learn it (kids should have least amount of problems learning either system). No one learned windows instantly.

  • "Linux is only for geeks"

    Isn't anymore. Linux was only for geeks when it was created. Since that time, Linux has gone a long way, and right now there are several companies attempting to make Linux into a product suitable for everyone, "for novices" or for mythical "average user". While making a product useful for everyone isn't a good idea (what is easy to learn isn't always easy to use, and vice versa), there are couple of distributions which everyone can install from scratch.

  • "Linux is for everyone"

    No. Linux is free, and because of this, it demands something from you. To be able to successfully master Linux system, you should be willing to learn it. If you do not want to learn, you will not master the system (although you still might be able to use it for some extent). Also, if you have to use software which doesn't work on linux, then you won't be able to keep Linux as the only operating system on your PC.


Games



  • "Linux don't have games!"

    Linux has large amount of games. The problem is that there is a relatively small number of commercial games (examples: Doom 3, Penumbra, Unreal Tournament 2004, Quake 4, Darwinia, etc), for whatever reason. You can run some of the commercial windows games using WINE software (or similar commercial derivatives), but you will get minor performance drop if game extensively uses shaders, and you will get problems if games uses certain copy protection mechanisms. However, Linux has a large amount of free games written by hobbyists. One resource for them is happypenguin.org. Notable examples of free Linux games include Alien Arena, Urban Terror, War$ow, America's Army, Sauerbraten, etc.

  • "Linux isn't suitable for making games":

    It is suitable for making games. You have advanced graphic API (OpenGL, which is easier to use on Linux, because on Windows default supported OpenGL version is still 1.x), generic cross-platform framework (SDL), several free physics engines and a bunch of similar libraries. Also there are ready to use game engines, like Ogre3D, Sauerbraten's Engine, etc. And you have tons of free tools for every gaming task.

  • "Windows has best games" or "Windows has better gaming experience"

    This is not true. To my experience, best games are available on consoles, and PC gaming terribly suffers from one big PC problem: hardware. The problem with PC's hardware is that it is flexible, and every user will have different configuration. This will make hardware developers either spend too much time trying to make game work on every video card ever created (instead of spending this time on making better game), or place high system requirements (so you will be able to comfortably play the game only two years later after several hardware upgrades). Another problem is that there is a low number of high-quality games on PC. Latest titles I bought (Fallout 3, Spore, L4D), were clearly overhyped and didn't provide quality I expected. Sure, you will be able to spend several dozens hours playing this, but you will very quickly reach the point where you know everything and don't want to play game ever again - because there is nothing left to do and you already know how to beat it. In short: best games on PC currently are only mediocre, and I'd like to play a masterpiece. The games that are not "the best ones" focus on graphics too much, and because of this gameplay suffers. Another problem with PC games is that once game is released, instead of playing it, you will spend most of your time tuning performance, fighting bugs and crashes, and waiting for patches that will turn game into something stable and playable. Whether you like it or not, a PC game released without bugs is a very rare sight, which spoils PC gaming experience in general.

  • "Linux doesn't have DirectX 10"

    True. But I haven't seen a game that couldn't work without it. Every game created so far with DirectX 10 support seems to use it only for minor, small graphical improvements. I haven't seen a DirectX 10 game with a truly astonishing graphics. For example, compare Quake 1 and Quake 4. If game uses "superior" graphical API, it should provide similar increase of graphic quality. This means, that DirectX 10 game when compared to the best DirectX 9 game should look as Quake 4 when compared to Quake 1.


Hardware



  • "Linux supports small number of hardware"

    Only partially true. Linux has a good support for large number of devices, which is provided by the kernel. Which means that on Linux system if hardware is supported, it will work out of the box. While on Windows, you will have to search for drivers. Buy a new ethernet card, install it in WinXP machine and you will have to search for a driver. And if you've just installed windows on completely new computer, you will have to search for several drivers. Until you find all of them - your machine might stand here without sound and network access. On Linux this card will work right now.

    However, Linux does have trouble with new hardware, USB devices (cheap web-cameras, scanners, force feedback on joysticks). This is because most hardware manufacturers don't support Linux, and don't provide specifications for writing drivers. Which means, some time will have to pass before someone reverse-engineer device protocol and write a Linux driver for it. Which means when you decide to upgrade your computer, you will have to check if new hardware is supported or not.

    On other hand, situation with hardware on windows isn't that great. For some reason, with each year driver packages tend to become larger and larger. Sometimes, just one driver takes more megabytes than entire Linux kernel. If you have a very fast connection, this won't be a problem, but if your internet speed is below 500kbps, this will quickly become annoying. Also, many windows drivers are buggy. I was very surprised to discover than on WinXP my "Vista Certified" motherboard produces slightly noisy sound (can't be fixed), while on Linux same problem doesn't exist, I have better sound quality, although motherboard isn't officially supported.


Software



  • "Linux doesn't have any good software"

    On contrary, amount of Linux software is quite large. Currently average Linux distribution includes Word Processor, compiler, several music players, several different image editors, several IDEs, and several desktop environments on the same DVD. So when you install it, you have ready to use working environment. On windows, after first installation you'll have empty machine that can't do much useful - you'll have to spend several hours installing codecs, Adobe Acrobat, your Office Suite, IDE, normal music player, and so on. The downside of all this is that most of the linux software is provided "as is, without warranty of any kind", and many programs are still in development. Which means some of them might have unfinished features, etc.

  • "I don't want to compile software"

    On large number of distributions, software is installed via package manager - you won't have to compile anything (unless you are using Gentoo, LFS or Slackware). However, it is highly recommended to learn how to compile programs manually - sometimes software might not be available via package manager.

  • "Compiling software on Linux takes hours"

    Not on modern hardware. Only largest packages (Qt 4, KDE, etc) might take hours to build. Normally software compiles within 5..15 minutes.

  • "I don't want to compile kernel on Linux"

    You don't have to compile kernel. Most distribution already provide properly tune kernel for you. Unless you want to make your machine a bit faster, you don't want to attempt rebuilding kernel yourself.


Communities



  • "If something breaks on Linux, community will help you"

    Not quite true. Community will try to help you. However, people in communities are mostly volunteers, and they might refuse to help you if:


    • You didn't read and apply rules from "How to ask questions the smart way"
    • You haven't tried to fix problem yourself.
    • You didn't ask question properly ("Help me, please" is not a good idea);
    • You just wish someone fix your problem for you (and for free).
    • You complain about things, and say something like "This didn't happen on windows"

    Also, quality of community (skill, knowledge level) depends on distribution. As far as I know, most helpful/skillful people use most difficult distributions (Gentoo, Slackware), however they may be a bit strict about the "rules", and you will have to ask question properly (and read "How to ask questions smart way" - it isn't that hard) to get high quality help. On other hand, communities that use "simpler" and "friendlier" distributions are not guaranteed to have skilled users (personally, I had serious trouble with advanced questions on Ubuntu communities).

Licensing and philosophy



  • "Open Source is the way software all should be distributed! It is the future!":

    Unfortunately, this isn't true. Open Source software has one serious problem: you can't earn serious amount of money by making programs and selling them. Don't get me wrong: you still can earn some cash from donations, or by selling merchandise (T-shirts, toys, gadgets) which is somehow associated with you program, or you can sell CDs/DVDs with that program, but you can't sell the program. At least, not with GPL. Of course, GPL allows you to "sell" programs, but once you do this, anyone can resell your program for lower price, or give it away for free in unlimited quantities. I believe this is one of the reasons why large software companies aren't releasing their products under GPL. Of course, there are other licenses, like BSD license, but software under those licenses can be easily converted into proprietary. So, although Open Source is (in some cases) good for software development, it isn't really good for software developers. I don't think you can make the living by developing your own OpenSource software.

    Another problem is that OpenSource doesn't seem to be suitable for at least one kind of projects: games. Sure, there are many good games made by individual developers, but I haven't seen a sophisticated OpenSource game with a great story, music, gameplay and art. I'd love to see gaming masterpiece (for example, something comparable to "Max Payne", "Silent Hill 2", "The Movies", etc) developed under OpenSource license, but it looks like such game doesn't exist. Another interesting thing is that good OpenSource games (for example, War$ow) use different license for their art, music and models, and typically this license is proprietary. Which makes me believe that OpenSource isn't suitable for making games, only for making game engines (which is different story).

  • "Proprietary software always is the best!"

    Unfortunately it also isn't true. When some software is being developed by a single company, they control the course of software development. While it is exactly what you would want in game development, it isn't always good for other kinds of software. Software developer might decide to add flashy GUI with dozen of bugs instead of making software more useful for you. Or it might decide to change course of development, so the next version of software will not meet your requirements anymore, but will appeal to larger audience. And another problem with proprietary software is that once it is broken, you are not allowed to fix it, even if you can do it. And Let's not forget various licensing and DRM problems. However, this was already discussed many times.



The Truth


The truth is that there is no "the best" operating system. Every OS that is available has problems in one or another area. User's goal is to pick operating system that doesn't have problems in important (for that user) area. For example, if you don't use some windows application, lack of its' alternative on Linux won't be a problem for you. Or if you don't use some linux utilities (dd, mount, iptables, cron), their unavailability on Windows won't be a problem for you.

So, What should I do about all this?


Nothing. "Linux vs Windows" appears again and again and again. People keep using same arguments over. Some people provide a very lengthy, detailed responses which cover common problems and provide a really good comparison of operating system, but large amount of people don't read them, and simply jump in discussion to prove their point. So the best thing you can do about those discussions is never participate in them.

Links


Linux is not Windows
How to ask questions the smart way

2009/04/19

Time management: how to get things done (if lists doesn't help you)


Although this blog is supposed to be mostly about programming, I decided to post one "self-improvement" tip which was useful for me.

Standard approach



Everyone(or most people know) know that normal way to deal with tons of things you should do today is to make a "to do" list for every day. In ideal case you should have several lists - list of long-term goals (lifetime), mid-term goals (several years), short-term goals(several months) and list of things that should be done right now (today, during this week, etc). Every day your to do list should contain several items from each list (or items that will bring you closer to reaching goal from each list), that should be written down according to their priority. I.e. if your mid-term goal is to learn new language, and immediate goal is to buy food, your daily to-do list should have one item for each of those goals. This looks neat, easy enough, however as I found out, there will be several problems.

Problems


Typically with standard to-do list, following problems arise:

  • One item from the list takes too much time or even all your time.
  • You don't really want to do that right now.
  • One of the items on the list looks like it will take too much time today, so you decide to do it tomorrow. Tomorrow, same things happens again. As a result task is not being completed within weeks or months.
  • You have too many long-term goals, and can finish anything, because they all take too much time, and can't be finished during one day.

Part of those problems arise from natural laziness - your consciousness (or subconsciousness) will invent various excuses to prevent you from doing anything useful. It is hard to understand why your own mind is against you, but this thing happens. Another part of problems will happen when you will incorrectly estimate time required to finish the task. Also, life is full of surprises, and it is quite possible that something will get in your way and you won't be able to finish some important task today because of external events.

Solution


There is a very easy solution for those problems (I haven't seen it written anywhere, which is why I've decided to post this thing here): when you make your daily "to-do" list, for each task write down how much time you will spend on it today. For example, if you are learning new language, (to my opinion) you will learn a lot if you will exercise for 30 minutes every day.
When you start your day, for each task spend as much time as you wrote down in your to do list (Unless you finish the task faster than you expected). Of course, this time you should seriously work on the problem without fooling around, without being distracted, etc. If you won't finish task in time, proceed to the next task, and put unfinished task on tomorrow's "to do" list (you might want to adjust time according to today's experience). If you have a lot of tasks for today, you can spend 30 minutes for each. 30 minutes isn't much, but you still can do a lot during this time.
This approach is incredibly useful for a very long goals (write a large program, learn language, study something huge, etc). At least it works for me, so I think it will be useful for someone else.

2009/04/18

The most annoying problems in Mozilla Firefox 3.0.6


Introduction


Many people love Mozilla Firefox. For some reason it surpasses opera browser in popularity (or at least, it is said so) - possibly because it might be better than IE, or perhaps because of number of extensions, or because Firefox is yet another example of OpenSource philosophy.

List of problems


However, it has some problems that are clearly not going to disappear any time soon.
Below is the list of those problems. All problems happen on dualcore linux machine with 2Gb of RAM. Most of them exist on Windows XP as well. So you can't blame hardware or operating system.

1. Bug with resizing windows.



How to reproduce: open any Firefox dialog (preferences, or whatever). Now drag bottom right corner of dialog to the left and up. See what happens:


Typically this is a standard problem which occurs in GUI applications made by beginner programmer. However, in Firefox this bug has exists at least since 2.0.0.x version.

2. Slow startup


Firefox 3.0.6 takes at least 8 seconds to start up, more if it is started from "cold boot". On my system "cold boot" launch of firefox takes at least 15 seconds. This is quite annoying.

3. Firefox frequently stops responding


Firefox frequently stops responding for a very long time. 10, perhaps 30 seconds. Normally this happens when you type something into "location bar" - there is a very good probability that browser will hang in the middle of the word, won't accept any input for quite a while, and even won't repaint browser window. After "sleeping" for quite some time browser will suddenly wake up and finally show the rest of the phrase you were trying to enter. Firefox also stops responding for prolonged period when you open history. Or when you try to search history. Or when you attempt to close browser (which can be seen here). It also happens randomly when you try to load pages. It looks like problem is related to the way how browser history is stored. The problem exists since the first release of Firefox 3.

4. It is not possible to right-click link on the page when another page is loading


This is the only problem which exists on Linux only - It is not possible to right click a link on the page in the tab if another page is about to load. Doesn't sound like a big problem, but it is still annoying.

5. It is not possible to disable javascript when "alert" is being shown


Without adding more extensions it is not possible to disable javascript when you see alert shown by javascript. Have you ever run into a script which launches series of "alert" messages? When you run into such webpage, you will easily notice the problem.

6. No reasonable keyboard navigation


Caret mode is a joke - it is simply too slow when using from keyboard. And although it is possible to instantly focus to individual tab using keyboard shortcut (which is good), it isn't possible to navigate page with reasonable speed using keyboard only. For example, in Opera you can quickly navigate between links on the page if you use Shift+Arrow Keys, in firefox this feature is still missing.

7. Page loses focus for some reqson


On completely random occasions interesting thing happens: you can't jump to location bar using keyboard shortcut unless you first click on the page with mouse.

8. Stops responding to keyboard shortucts when flash movie is shown


When there is a flash content on the website, it is possible that you won't be able to close tab with this content using keyboard shortucts (Ctrl+F4 or Ctrl+W), unless you first click on the page somewhere outside of the flash content. Same things seems to happen with java applets as well.

9. It is not possible focus to "find" box without mouse if you view image


Assume you were viewing some page, tried to find some text on it (using Ctrl+F) and then opened some image in the same tab. You will notice that "Search" window is still present at the bottom of the screen. On normal page, to dismiss it without using mouse by pressing Ctrl+F (which will move text entyr cursor into search bar), and the "Escape" In case of image this won't work - you'll have to click on the text in search bar, then press "Escape". Happens frequently.

10. When saving picture to hard disk, it looks like it is being downloaded again


This is simply illogical. When you right click on any image and attempt to save it to disk, "Downloads" window will appear. On window platform picture will be even scanned by antivirus. I just don't understand why this works this way. Picture is already inside of browser cache, so there is no reason to downlaod it again - it will simply take longer.

Conclusion


So far I know the only two reasons to use firefox

  • Ad Block plus
  • some plugin which is supported only on Firefox.

Other than that, Firefox 3 gives the impression of unfinished and unpolished product with a lot of problems, several bugs, and very poor support for keyboard-only navigation.

2009/03/25

How to use sprintf/wsprintf with std::string/std::wstring


Explanation


One of the few problems with std::string (and std::wstring) class in C++ is the lack of sprintf function that would return std::string as result. Or sprintf-styled constructor would be nice to have. Of course, there are many alternatives to sprintf - boost format library, sstream class and QString class in Qt 4, but so far plain old sprintf is most compact and easy when compared to them. sstream requires several statements(lines) to make a simple formatted string. Unless you like it too much, it is an overkill for making small formatted string. Boost format library will require boost or a part of it. And Qt 4 QString class will require entire Qt installation, acceptance of one of few available licenses, and it still won't be as compact as sprintf.

Solution



The first thing that comes to mind is to create temporary buffer, sprintf into buffer, and then assign buffer to std::string class. When your programs grows, you'll eventually get sick of many temporary buffers, besides it would still require few lines of code. Here is better way to do that:
Str.h:


#ifndef STR_H
#define STR_H
#include

typedef std::string Str;
typedef std::wstring WStr;

WStr swprintf(const wchar_t* format, ...);
WStr vswprintf(const wchar_t* format, va_list args);
WStr swprintf(const WStr& format, ...);
WStr vswprintf(const WStr& format, va_list args);

Str sprintf(const char* format, ...);
Str vsprintf(const char* format, va_list args);
Str sprintf(const Str& format, ...);
Str vsprintf(const Str& format, va_list args);

#endif




Str.cpp:

#include "Str.h"

WStr swprintf(const wchar_t* format, ...){
va_list args;
va_start(args, format);
WStr result = vswprintf(format, args);
va_end(args);
return result;
}

WStr vswprintf(const wchar_t* format, va_list args){
const int bufferSize = 16384;
wchar_t buffer[bufferSize];
vswprintf(buffer, bufferSize, format, args);
return WStr(buffer);
}

WStr swprintf(const WStr& format, ...){
va_list args;
va_start(args, format);
WStr result = vswprintf(format, args);
va_end(args);
return result;
}

WStr vswprintf(const WStr& format, va_list args){
return vswprintf(format.c_str(), args);
}

Str sprintf(const char* format, ...){
va_list args;
va_start(args, format);
Str result = vsprintf(format, args);
va_end(args);
return result;
}

Str vsprintf(const char* format, va_list args){
const int bufferSize = 16384;
char buffer[bufferSize];
vsnprintf(buffer, bufferSize, format, args);
return Str(buffer);
}

Str sprintf(const Str& format, ...){
va_list args;
va_start(args, format);
Str result = vsprintf(format, args);
va_end(args);
return result;
}

Str vsprintf(const Str& format, va_list args){
return vsprintf(format.c_str(), args);
}





This will allow to quickly create formatted string in one function call.

Problems:



This code works with gcc on Linux system, but it might require some tweaking on different compilers. For example, mingw version of vswprintf has different number of arguments (it doesn't have "buffer size" argument), so it will need to be replaced by another function. In general, wchar_t-based printf functions might cause problems when making cross-platform application. For example, swnprintf exists on MSVC compiler but is missing in gcc. On other hand, default version of vswprintf used in MinGW compiler doesn't have "buffer size" argument, so it is vulnerable to buffer overruns (linux version of function doesn't have this problem).
And yet another problem is that linux/windows versions of wprintf-related functions might handle %s and %S differently. As I remember, in mingw compiler %S in swprintf does the same thing as %s in linux version of printf and vise versa. Those problems can be partially fixed by using few hacks, but people making portable applications with swprintf should be aware of those problems.

Another problem is that there is hard-coded size limit for created strings (it can be changed, but it still doesn't look "nice" when used with C++ string classes. This problem can be bypassed for std::string classes (by using vsnprintf which returns how much characters could not be written in buffer, so you could allocate buffer dynamically, then sprintf into it, and then assign it to std::string), but not for std::wstring (vswnprintf is not available on gcc, doesn't look like standard).

Other solutions


To my opinion, the best (not the fastest) way to make custom sprintf for std::string classes is probably to write it from scratch (in C++ it might be easier) or derive from existing C implementation of sprintf (for example, you can take one out of freebsd source repository). The reason for that is that several printf implementations might have differences. The problem here is that it won't be easy, and you probably will need some time to write standard-compliant (or "format-compliant") version of swprintf/sprintf which will operate on std::string/std::wstring. Of course, you can also implement limited version of those functions.

Another way is to make your own formatting routines or use already mentioned ones: boost format library, QString or sstream. This way you won't get sprintf function, but few formatting routines to use instead.