Category: C/C++

We joined the 48-hour game dev competition and came up with a 2d platform game. And without further ado, here is our entry

Edit: Just for the information of others, we’re the team: “Crates, Kitchen Sinks and Duct Tapes” and the game is called “Cores and Cords".

Wil
21/06/07

Of Topcoder and C++

I’ve used C++ ever since I started serious programming in 2003 so when I knew that TopCoder was offering Development Competition for C++ the first time, I was excited to join.. too excited perhaps.

It was a big lesson for me on knowing how to follow directions - implement it EXACTLY as stated in the specifications. They were very particular about the access levels changes especially in the public interface. Furthermore, EVERY entity has to be documented. Whew, that was my first time taking part in a SERIOUS development “team'’.

Anyway, I landed second place (which is pretty ok, considering that the reward is $400 for the second place) but the thing is, I lost to the first placer by .87 points over (the perfect was 100). He got 79 while I got 78.13. I admit it was my stubborness of using tabs instead of the require 4 spaces (but.. but… you can always run it through a code beautifier!!) and some missed specs (didn’t read the instructions well enough). But at least, I have the pride of having a better makefile.

But the part that ticked me off the most (or else, I would’ve been content getting second place) was the parts on the winner’s entry that showed that he’s not too familiar with C++ :(.

Anyway, I’ll stop ranting for the meantime, what’s done is done. I’ll probably give more specifics in the following posts.

Manifests. Now windows programmers have more issues.

Brief Background

Those who lived through the Windows 9x era very well experience the situation known as DLL Hell. For those without background, it’s the situation where different programs overwrite shared libraries, known in Windows as DLLs. For example, Program A needs sound.dll to run, it then proceeds to copy that file to the Windows directory usually upon installation. Then here comes Program B with its own version of sound.dll, sees sound.dll in the Windows Directory and proceeds to overwrite sound.dll which Program A uses. Have a hundred programs overwriting each other’s shared libraries including Window’s own DLLs (DirectX, C runtime libraries) and the end result is different programs (including Windows!) are getting unexpected results from the shared libraries that they use, causing crashes and other odd behavior. Nevertheless, they didn’t prevent this (the use of shared libraries) because it was necessary for other things like servicing (more information about servicing could be found here.

Windows 2000 and XP “fixed” these problems by intoducing Protected DLLs and Side-by-side Assemblies (SxS for short). Protected DLLs are basically DLLs which are protected by the Operating System: modify it and the system will attempt to restore it. SxS assemblies, on the other hand, are more complicated. An article at CodeProject explains almost everything.

Abstractions, abstractions, abstractions…

Basically, in order to use the “manifested” DLLs, you’ll have to include an xml file either together with your application or compiled as a resource. This may not seem much but the problem is, it’s note entirely automatic. In order to be able to use Common Controls 6 and Windows XP visual styles to a Windows GUI Application, this XML has be included in the manifest (taken from http://msdn2.microsoft.com/en-us/library/ms649781.aspx):

XML:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
    version="1.0.0.0"
    processorArchitecture="X86"
    name="YourCompanyName.YourDivision.YourApplication"
    type="win32"
/>
<description>Your application description here.</description>
<dependency>
    <dependentAssembly>
        <assemblyIdentity
            type="win32"
            name="Microsoft.Windows.Common-Controls"
            version="6.0.0.0"
            processorArchitecture="X86"
            publicKeyToken="6595b64144ccf1df"
            language="*"
        />
    </dependentAssembly>
</dependency>
</assembly>

The first assemblyIdentity element was needed to enable Visual Styles in the application while the second one was to use common-controls. The first one is already automated by Visual Studio but the common controls element has to be manually placed either using that method (manually typing the XML) or using the #pragma directive:

C++:

#if defined(_M_IX86)
#define MANIFEST_PROCESSORARCHITECTURE "x86"
#elif defined(_M_AMD64)
#define MANIFEST_PROCESSORARCHITECTURE "amd64"
#elif defined(_M_IA64)
#define MANIFEST_PROCESSORARCHITECTURE "ia64"
#else
#error Unknown processor architecture.
#endif

#pragma comment(linker, \
    "\"/manifestdependency:type=’Win32′ "\
    "name=’Microsoft.Windows.Common-Controls’ "\
    "version=’6.0.0.0′ "\
    "processorArchitecture=’" MANIFEST_PROCESSORARCHITECTURE "’ "\
    "publicKeyToken=’6595b64144ccf1df’ "\
    "language=’*’\"")

Anyway, the main point is, why does it have to be this tedious? Can’t we just add references just like how .net applications do?

In the linux world, this doesn’t matter very much since the applications are compiles to the user’s machine so all, if not most, dependencies are resolved using package managers and the like. And unlike Microsoft, there’s no single entity that carries all responsibility of servicing so they don’t have to worry about it much.

For Mac, with what they call “Frameworks”, it gets even easier, just add the Framework to the project (like how you Add References in .Net) and place this in your code:

C++:

#include <Framework_name/Header_filename.h>

Sometimes, I just hate how Microsoft treat C/C++ programmers like 2nd class citizens.

Wil
10/05/07

Roman numbers

Before I get started, since it’s my first time posting a solution, it goes without saying that there are more than one way to solve a problem. So the last thing I want is a bigot smugly insisting that his solution is the correct one. Nevertheless, I’ll gladly accept criticisms, holes, and alternative solutions when they’re pointed out. Oh and when when ever I use superlatives, add “probably the” before the said superlative. So when I say “best” I mean “probably the best", when I say “easiest” I mean “probably the easiest” etc.

Converting and checking Roman numbers is a classic computing problem. For this problem, I’ll use an ACM problem in the 2006 Asia Regionals held in the Philippines: Nuevo-Romano it involves both converting from Decimal to Roman, the other way around, and checking for validity of roman numbers.

Read more »

One of the things I’ve always wondered in PostgreSQL is how to turn the timestamp data retrieved using libpq into something more managable aside from string values like time_t or struct tm (useful for programs that manipulates time data and not just display them). One way is to dig through the PostgresSQL sources to find the actual function (there’s actually a function to convert the binary data into struct pg_tm) but that’s not a good idea since it may change when there’s a new release and you’ll get one more thing to compile with your program. Another way is to use “extract(epoch from [identifier])” which converts the timestamp data into seconds elapsed after January 1, 1970 which is… you guessed it, equivalent to time_t. But before actually retrieving it, you have to cast it to bigint first since it’s actually using a floating-point representation. Here’s the SQL statement:

Read more »