Sunday, April 27, 2008

Fun with DirectX SDK

Whew! What a stressful week! I've been looking forward for this weekend to cave in my room and relax. Instead of talking about my SCTP work, I decided to spend my weekend to play around with DirectX SDK.

There was a point that I considered myself a hard-core gamer. I remember the days back in college where I save every little bit of my allowance just to get that state-of-the-art Video Card. You'll often find me browsing the net looking at the colorful video card performance comparison graphs on guru3d making sure I'd get the best card for my hard-earned cash. Then after 6 months I got a Radeon 9600 which was a midrange card at that time. It was money well spent and even after five years I still use it.

After wasting hours shooting pixelated enemies with my trusty video card, I got interested on DirectX. If you're a PC gamer there's is a point in time that you'll be asked to install DirectX first before the game could work. So what the hell is DirectX?

DirectX is a set of tools that provides software developers direct interface to video hardware. In addition to that it also provides API's for other multimedia devices like keyboard/mouse input and sound cards which makes it a great tool for developing games. Developing with DirectX gives you an assurance that your application will work with across different video cards and platforms (like XBox360) and the most important thing is that it's free! How cool is that? So I headed over Microsoft to download the DirectX 9.0 SDK to get my hands dirty.

There's a very good DirectX game programming Tutorial over the net. I recommend you to drop by and read the tutorials here . The website is a good jumpstart and I plan to read it throughout this study. One notable game programming resource is gamedev.net which I happen to read to get my interest going.

With the great help of the tutorials, I managed to write a code that speaks directly to my Radeon 9600! It is basically a morphing triangle that redraws in 25 frames per second. ^_^



I wrapped most of the Windows-related mumbo-jumbo stuff in functions (specially the Window creation). The code will compile as long as you added the DirectX SDK to the project. This is a very basic demonstration on how to use DirectX. The code is still in its early stages but in truth, it can be used as your default template for creating games! I added a comment in the code where you'll insert your game logic. Hurry up and download the source code!

It's been my dream to write my own game and so far I had my first step and I have a list of TODO's:

- Write a collection of classes to make my job easier
- Take a look at openGL
- Learn how to load fonts, bitmap, and 3d models in DirectX

I hope this could mutate into something interesting. When it gets to that point, I'll be sharing my code. :)

Thursday, April 17, 2008

Writing Python modules in C

Python just like other scripting languages is interpreted meaning that a program written in python is processed by an interpreter that encodes it to computer understandable machine code during runtime. The interpreter overhead does reduce its performance over machine compiled languages like C & C++. One advantage though is that programs written in interpreted languages are portable as long as the platform has the corresponding interpreter installed.

Thinking forward:
--- "Is there a way to write an application that should perform as fast as possible but at the same time take the advantage of python rapid development?"

--- "I have a project that uses a protocol that can only be written in C. Is there a surefire way to make my life easier?"

...and the answer to both of the questions is "yes" there's a way.

Most of the modules that comes with python is actually written in C or C++. They are compiled as a shared object library and it does makes applications using it run faster. It's just like writing small and fast C modules and gluing them together with a high-level language. One popular example of this technique is the game Civilization IV which uses python as its interface layer on its C++ game codes and today I'll teach you how to do that. :)

First you need to have these necessary stuff installed in your system:
gcc - GNU C compiler
python-dev - python include files (you can download this on their website)

For this tutorial, I wrote a very simple and useless python extension that takes two integer values and returns the sum:


#include <Python.h>
int a, b, sum;
static PyObject *
Add(PyObject *self, PyObject *args)
{
if (!PyArg_ParseTuple(args, "ii", &a, &b))
return NULL;
sum = a + b;

return Py_BuildValue("i", sum);
}

PyMethodDef adder_methods[] = {
{"add", Add, METH_VARARGS, "Add two integers"},
{NULL, NULL, 0, NULL}
};

PyMODINIT_FUNC
initadder()

{
(void) Py_InitModule("adder", adder_methods);
}


Here's a quick run through:
Line 1: Include Python header
Lines 3-4: Create a function named Add that accepts and returns an abstract Python Object
Lines 6-7: PyArg_ParseTuple says that the function will require two Integers denoted by "ii" if the requirement is not met return NULL (which causes an error)
Line 8: Our shameful adder logic
Line 9: Py_BuildValue converts the C integer variable sum to Python integer denoted by "i"
Lines 12-15: PyMethodDef defines the functions available in this modules. As you can see it defines a function name "add" that points to our C function "Add".
Lines 17-20: Initializes our modules and assigns it with a name "adder"

To be able to use this in python, we need to build it first. The easiest way to do this is to use python distutils. Create a file named setup.py:

#!/usr/bin/python
from distutils.core import setup, Extension
import sys

setup(name = "adder",

version = "0.1",
ext_modules = [Extension("adder", ["adder.c"])])

Then build adder by this chain of commands:
# python setup.py build (compiles it with gcc)
# sudo python setup.py install (installs it to python site-packages directory)

And there you have it. Your own python module written in C. Cool isn't it?

Sadly this is a blog, if I just have an actual web page I could explain this much further. For now, if you want to know more about the Python.h stuff that I used in this program, head over to python/C API reference manual

This tutorial actually leads to my next blog which is about how do SMS (Short Messaging Service) actually work and at the same time give you a nice python extension that I wrote (SCTP sockets in python!). I hope that I helped somebody out there, Til next blog! ^_^

Monday, April 14, 2008

A Simple Chat Room Server

Server programming poses a lot of challenges but nowadays with the advent of high-level languages such as python, the code that actually makes the basic server functions is put out of the equation giving us the time to think more about the logic and features that were going to implement.

Last March I conducted a network programming seminar in PUP's College of Eng'g. I expected a good question and answer session from them but sad to say, there were very few questions that was thrown at me. Probably the reason was that the seminar was a little bit too technical for the students and network programming is very rarely taught in College. (I wish I could've at least had more time to teach them with a hands on session)

On the seminar I provided a simple Chat Room server program written in python. This is a very good example how easy it is to write server programs nowadays. The code is roughly 100 lines and is self-explanatory (rewriting this to C++ could take more lines).

The Chat room server implements a very simple protocol:

LIST - returns the nickname of the people that are online
LOGIN: - login to the chat room with the provided nick name (Default is 'Anonymous Coward')
EXIT - leave the chat room

The server assumes that it's a message if the typed message does not match the existing commands. The chat_server.py is tested to work under linux and should be started just like:

# ./chat_server.py "port number to listen to"

You can use telnet to connect to the server:

# telnet localhost "port number where the server is listening"


We use localhost or 127.0.0.1 assuming that the server in running in the same machine. The interface is similar to the old unix talk program.



So there, a Chat Room server written in 100 lines of code! If you're bored, you can download the Source Code and see for yourself (some explanatory comments added).

Now that I've posted a sample python code, next blog is about how to make your own python modules in C. That's it for now. ^_^


Sunday, April 13, 2008

Making a living out of python


I was introduced with a number of scripting languages when I started developing unix applications. I've tried quite a few: PHP, perl, and even ruby but nothing can compare on how many projects I've done using python.

What I like about python is the very simple syntax to the point that it's self-documenting. It has its own console (just what is shown above) which is great for debugging and checking what methods and members is inside a package or class (using the dir() function). It has a huge community of developers writing cool modules on it like DirectX and openGL wrappers, game libraries (pygame), network programming api's (python-twisted), and more. In case if you need help, there's a very active mailing-list (comp.lang.python) where you could ask questions.

In my work which is mostly writing web services and implementing network protocols and stuff, python helped me to think more about how to make things work rather than how should I code it. Funny thing is that I know a lot of software developers who's never heard of it (I live in Philippines by the way, it could differ in other places). After googling on the web you'll find companies like google and youtube using python on their backend services. So if you have some spare time head to python.org and see for yourself. There also a free online book that you can download.

Okay after a brief introduction, we'll write some python code next. After that we'll write a python module in C. Till next blog! :)

Saturday, April 12, 2008

Tools of Trade

I've been professionally writing software for three years and worked with a number of IDEs, platforms, and languages depending on the problem at hand. So far here are my favourites:

Platform: Linux/Unix
Editor: emacs
IDE: none
Languages: C/C++ and python

These are the combination that puts food on my table. I don't use IDE in linux even though there's eclipse. So far I can stay productive hacking away at a dark console window that is running my favorite emacs editor. Python hands down is the best scripting language in my own opinion.


Platform: Windows
Editor: emacs
IDE: BloodShed Dev-C++
Languages: C/C++

If I want to awe myself out of boredom, I write some useless software in Windows. Writing C++ software in Windows is really a headache but thanks to MSDN library and intellisense, it helps to ease out some pain. Lately, I've been horsing around with DirectX 9 SDK.

There we go. I should have added Microsoft Visual Studio but I can't afford it. Microsoft does release free editions called "Express" editions but MS Visual C++ Express can't even compile a simple Win32 "Hello World!" program for it doesn't include the necessary libraries.

I have a lot of plans for this blog. Namely introducing the Python language. It irks me to find that there is not much python job offerings in Philippines even knowing how wonderful it is. A more advanced Python tutorial (Writing C extensions in Python) will follow given time.

Why Playground?

All right this is my first time writing an online blog. Since I don't have any resources to have my own dedicated web server, I resorted to blogging for the meantime.

I have a lot of hobbies specially dreaming of things to make while staring blankly on a car's window during commutes. Most of the time they eventually get written in code - Source Code under the directory "Playground".

From here I'll be posting my ideas, code, and literature in hope to make the world a better place. :)