Compiling Lua with Visual Studio 2010

It’s not often I have to venture into the Microsoftland development environment, but when I do it always seems to take me a while to get used to their visual projects instead of good ‘ole Makefiles. I’ve been using Lua to extend my C and C++ apps a lot recently, so I suddenly found myself needing to include the Lua static library with a Visual Studio 2010 project.

There are already binary distributions out there, but I like to compile things myself, so I whipped up a Visual Studio 2010 project to compile the Lua static library (lua.lib) the command line interpreter (lua.exe) and the script compiler (luac.exe).

First things first. If you just want a zip file with those three binary files (the library is Visual Studio 2010 compatible), look no further:

If you want to build Lua 5.1.4 yourself (like me) just grab the most recent copy of the 5.1.4 source code from the Lua website. Next, grab these Visual Studio 2010 project files and solution file:

To use the project files, just unzip them into the root directory of your Lua source files (where the README, INSTALL, and COPYRIGHT files are). Then, load up lua-vs2010.sln in Visual Studio 2010. You should see three projects. The first, lualib will build the static library under lib/lua.lib. The second and third, lua and luac will build the Lua interpreter and compiler under bin/lua.exe and bin/luac.exe.

Enjoy!

Posted in Code, Microsoft, Scripting | 1 Comment

Installing TrueType Fonts in Fedora

I’ve haven’t written recently because I was completely bogged down finishing up my Bachelors degree and applying to grad schools, but here’s a quick tip for those of you looking for a painless way to install third party TrueType fonts in Fedora. This may work in other Linux distros, but Fedora is my distro of choice so that’s why it’s used here.

If you need to install fonts accessible to all users on the system, you have to do some more complicated voodoo. I hate voodoo, so I’m not going to cover that.

If all you need is to install a font for your own user, it’s just this simple:

Put your myfont.ttf file in your home directory, under directory called /.fonts/. So in other words, your font lives at:

/home/youruser/.fonts/myfont.ttf

Create the directory if it doesn’t already exist. Finally, restart any application you want to use that font in, and you should see it show up. You’re good to go.

Posted in Linux | Leave a comment

Fixing the Fedora 12 VirtualBox Guest Additions problem

I’m a frequent VirtualBox user, and as I’ve noted in my previous posts, I’m an avid fan of Fedora as well.

However, there is a nasty bug in the most recent version of VirtualBox (3.1.2) when combined with Fedora 12. After installing the Guest Additions kernel modules as per the user docs, the system boots to a black screen with a cryptic error message that looks like a SELinux labeling problem (it’s not).

type=1305 audit(12587840002.571:32444): audit_enabled=0 old=1 auid=4294967295 ses=4294967295 subj=system_u:system_r:readahead_t:s0 res=1

The problem is actually with the Guest Additions video driver, the one that gives you the nice resizable desktop window. Once the driver is built and installed, for some reason the X server can’t find any screens and refuses to start.

Until the bug gets fixed in the video driver, here’s how you can fix the system so that it will boot correctly, although you’ll lose the dynamic resizing ability. You’ll have to stick with fixed, predefined resolutions for now.

  1. Mount a Fedora 12 ISO, such as the full or network install discs, and boot to it. Boot into Rescue Mode from the GRUB bootloader screen.
  2. Breeze through the language and network options, but be sure to have it mount your hard disk image (it will mount under /mnt/sysimage).
  3. Drop into a shell and change into your hard disk’s X11 config directory, so that would be:
    cd /mnt/sysimage/etc/X11
  4. Edit your xorg.conf file… but wait! In Fedora 12, they switched to HAL for X configuration, so there is no xorg.conf file! Never fear, you just need to create one and it will override the HAL:
    vi xorg.conf
  5. Now, use the following settings for the new xorg.conf file:
    Section "Device"
        Identifier "Configured Video Device"
        Driver "vboxvideo"
    EndSection
    
    Section "Monitor"
        Identifier "Configured Monitor"
    EndSection
    
    Section "Screen"
        Identifier "Configured Screen"
        Monitor "Configured Monitor"
        Device "Configured Video Device"
        SubSection "Display"
            Depth 24
            Modes "1440x900" "1680x1050"
        EndSubSection
    EndSection
    
    Section "InputDevice"
        Identifier "vboxmouse"
        Driver "vboxmouse"
        Option "CorePointer"
        Option "Device" "/dev/input/mice"
    EndSection
    
    Section "ServerLayout"
       Identifier   "Default Layout"
       Screen      "Configured Screen"   0 0
       InputDevice   "vboxmouse"
    EndSection
    
  6. You’ll see I’ve defined two resolutions, 1440×900 and 1680×1050. What this allows me to do is work windowed at 1440×900 and if I want to go full screen (remember, dynamic resizing won’t work) I can hit the full screen shortcut in VirtualBox (Host+F) and change the resolution within Fedora to match my screen res.
  7. Save from vi (:wq) and reboot the system. Remember to unmount the install disc! The system should boot correctly now, albeit without dynamic resizing.

A huge thanks goes out to Jits in the VirtualBox forums for this fix!

Posted in Linux | 7 Comments

Include dependencies

Generally when I write software, I try to keep things relatively well organized. Inevitably, however, things are going to get a bit messy, especially if you’re working on a large, disorganized codebase that you didn’t write to begin with… say, oh… something like the Source SDK.

Frequently you have some class which is composed inside another class, but occasionally needs to access the class it’s composed inside of. Basically, the classes are composed inside each other, though the abstraction really only makes sense in one direction. Confused yet?

In this example, we’ll use a Refrigerator class which stores inside it an instance of a Cheese class. Why cheese, you ask? Because cheese is delicious. Also, our refrigerator is from the future and can slice and serve cheese just like the built in ice maker and water dispenser. It’s a pretty sweet fridge.

Now, we were all taught to keep our #includes in our header files, not the implementation files, so like good little programmers we construct our classes like so:

refrigerator.h

#include "cheese.h"

class Refrigerator {
private:
    Cheese *pCheese;
    int temp = 35;

public:
    void ServeCheese();
    int GetTemp();
};

refrigerator.cpp

#include "refrigerator.h"

void Refrigerator::ServeCheese() {
    printf("Now dispensing %s cheese!\n", pCheese->GetFlavor());
}

int Refrigerator::GetTemp() {
    return temp;
}

cheese.h

class Cheese {
private:
    Refrigerator *pFridge;
public:
    char *GetFlavor();
    void CheckTemp();
    void BeginMolding();
};

cheese.cpp

#include "cheese.h"

void Cheese::CheckTemp() {
    if (pFridge->GetTemp() > 45) {
        BeginMolding();
    }
}

char *Cheese::GetFlavor() {
    return "cheddar";
}

I’ve left out the constructors in this example for brevity, but let’s assume that they get the pointers set up correctly so that our instance of the Refrigerator class has a correct pointer to an instance of the Cheese class and vice versa.

Now, at this point you may be screaming that this needs to be refactored and reorganized. Yes, it probably does. But there are many instances where you simply can’t, and in fact the abstraction really only makes sense one way. The fridge has cheese in it, but the cheese certainly doesn’t have a fridge in it. We just need that pesky reference around so we can check the temperature of the fridge every so often.

(Yes, I am aware that the fridge could push it’s temperature down to all the items in it, ala the Observer Pattern. Yes I am aware that would be a better solution. But this is a contrived example anyway, so stick with me here.)

Now, the code given above doesn’t compile, because the Cheese class has no idea what the heck this Refrigerator class is, so we either need to include it or forward declare it. If we try to do this:

cheese.h

#include "refrigerator.h"

our compiler (more specifically, the preprocessor) is going to get very angry at us, depending on which order it decides to compile refrigerator and cheese. The solution, is a forward delcaration:

cheese.h

class Refrigerator;

class Cheese {
    // ...etc...
};

Basically what this does is tell the compiler, “Hey! There’s this class called Refrigerator that I might talk about, so here’s an empty declaration of it!”

The problem, though, is that this is rather limiting. Within the Cheese class, we can declare pointers to Refrigerator class, no problem. Pointers are of fixed size, so the compiler doesn’t much what care what it’s a pointer to, since it knows how much memory it needs to hold a pointer to it. When we try to access members of the class, though, like properties or methods, it falls apart because as far as the compiler knows, the class is empty. After all, we told it the Refrigerator class didn’t have anything in it.

So if we can’t #include it and forward declaring it doesn’t give us what we want, what can we do?

Well, we can do both. Kind of.

The solution is to forward declare in your header file, and #include in your implementation file. This will avoid the preprocessor headaches of of the chicken and egg #include, while allowing us to access the members of forward declared class in the implementation. In other words, here’s the fix:

cheese.h

class Refrigerator;

class Cheese {
    ...
};

cheese.cpp

#include "cheese.h"
#include "refrigerator.h"

Again, it goes without saying, the better solution is to refactor or rearchitect your code if you can. These kind of hacks can get really out of hand and are usually a good code smell that something needs to be fixed at a deeper level. However, if you’re working on a large codebase that you can’t change, this can help out a lot.

Posted in C/C++, Code | Leave a comment

Removing entities in the Source SDK

I haven’t written for a while, mainly because I’ve been busy with classes and studying for the GRE for my grad school applications, but here’s a quick tip for those of you meddling around with the Source SDK.

It’s well documented on how you go about spawning entities, but I couldn’t find a good place explaining how to remove spawned entities through code.

Don’t try meddling with the global entity list (gEntList) or calling its RemoveEntity() method. It doesn’t do what you want.

Instead, used one of the super-handy UTIL_* functions. Given a pointer to the entity you want to remove, simply use:

UTIL_Remove(pEntity);

Poof. Entity gone. Remember, entities are created and destroyed on the server side only. The server will automatically broadcast any changes to the entity list to its connected clients for you.

Posted in C/C++, Code, Gaming | Leave a comment