Tuesday, January 31, 2012

C++11x kinda neat but...

So I have been futzing around with porting some of the OpenGL SuperBible examples to use SDL instead of GLUT.  It turns out the new SDL 1.3 is using the zlib license which is much more permissive, and it also can create OpenGL 3.x+ GL contexts.

So I started writing some C++ wrappers to help instantiate SDL and create a GL context for me.  I forgot how atrocious C++ is.  I haven't seriously written any "real" C++ code in about 4 years (I wrote some C++ which basically looked like C, but I didn't use Templates, namespaces, or classes).  I even forgot how to pass in arguments to the parent class constructor.  And I am aleady dreading the whole Copy Constructor vs. = operator overloading, using smart pointers, etc etc.

But the thing that really confuses me?  Makefiles.  I hate them, so I've been looking for an alternative.  I found waf a long time ago, and I think I'll use it, but it looks like it will have a pretty steep learning curve too.  But what I like about it is that it is more generic.  It isn't JUST a C/C++ build/configuration tool.  It's kind of interesting how the waf author handles dependencies.  Although obviously he had to use a DAG to traverse dependencies, the python code he used to automatically determine these is pretty cool, and I wish I had thought of that for the code I did at work.

So I've already begun the tedious process of learning waf, in addition to re-learning C++, the C++11x additions, libSDL and of course OpenGL.  Oh, did I mention that the game logic is in Clojure and will do low-level network communication with Netty (on the clojure side) to a boost.asio asynchronous network?  To say that I have a lot to learn is a huge understatement.

Speaking of C++11x, some of the features seem interesting, but some of them I also just don't understand.  I'm still trying to wrap my head around the ability to bind to rvalues.  I am looking forward to auto and decltype, which may help me overcome my fear of Template programming.  But still, I had to wonder...was there a better way?

So I started looking into Google's Go, and the D programming language.  Go seems to be quite a departure from C/C++, eliminating some strongly typed aspects of the language.  The syntax is also a little funkier, but nothing too bad I think.  But I've already heard some people say that it's not exactly true that Go is a System Programming language, but rather a network programming language.  It's also still in a very early stage of development.  D on the other hand seems to be more of a real programming language.  It also seems to do everything C++11x does, including a few that C++11x doesn't.  In fact, the more I looked at D, the more I liked it.  For example, it does automatic garbage collection (Go does too btw), it has an easier to read Template system, it has lambdas and closures, it has built-in complex numbers, built in unit-testing, and a few other nice features.

But...it has one major drawback (Go suffers the same fate).  D does not have a preprocessor and thus does not understand .h header files.  Sure, it can natively call C API libraries..but you have to define the types yourself.  So basically, you have to convert header files into a D module.  uggghh.  I've been through this before with python ctypes, JNA, and bridj.  It's not fun.  And SWIG isn't much better.  With SWIG, you still have to make a .i file, and even if you use a "raw" header, the stuff it outputs is barely human legible.

I'd love to use D, but I'd have to convert libSDL to modules (already done with the Derelict module...but it's using the 1.2 version of SDL which doesn't do OpenGL 3+ contexts, and I don't know what version OpenGL it supports).  Sadly, if a system's programming language isn't compatible with C (and I don't just mean being able to call functions in a library, I mean to understand all the types in a header file too), then it's just not going to find a lot of uptake.

Tuesday, January 24, 2012

Quick update

Wow, long time no post.  Been busy over the holidays so I haven't had a chance to do much coding.  Doesn't help that I bought a ton of old Twilight 2000 pdf's from rpgnow.com to keep me busy reading and reminiscing. Hopefully this week, I'll put up the third post in the Emacs + Clojure + Leiningen series.

I have however lately been thinking about where to spend my free time in programming.  I still want to do some clojure programming, and I will, but the pragmatic side of me also wants to get back into some C/C++ programming.  I've slowly been reading the OpenGL SuperBible book (almost done with Chapter 4), and my original idea was that I would try to port the C++ code to clojure code (via the lwjgl library).  But I realized a couple things...

1)  The graphics side would be slow
2)  I don't want my C/C++ skills to rust away
3)  Might be an opportunity to pick up some C++11x
4)  I won't have to do any mental conversion of the code

The disadvantage of course is that I don't want all of my game to use C++.  I want the graphics side to be in C++ and possibly some OpenCL, but the actual game logic to be in Clojure.  So how am I going to accomplish that?  I could write a whole bunch of JNI so that the C++ code could call Java code.  Another option would be to do some IPC, and have the C++ code shuttle information and make method calls via some kind of RPC mechanism.

I already have a little bit of experience with Netty, and I even started writing my own messaging protocol to be communicated over the socket.  Although Netty was relatively painless, writing TCP socket programming in C/C++ is not fun...not to mention asynchronous or multi-threaded socket programming.  I could use boost's ASIO library, but that in itself looks like a pretty big learning curve.

So I'm still debating what to do.  JNI is tedious and error prone (usually requiring passing NIO byte buffers all over the place), and socket programming isn't fun.  I think I'll go the IPC socket route...but I am kind of dreading it.

But my decision to use C++ will be a time eater.  Not only will I have to refresh my skills (and pick up some of the new C++11x features), there's a ton of new libraries I will have to figure out like libSDL, boost.asio, boost.threads, and of course OpenGL itself.  I also want to learn a new build system called waf.  Makefiles are horrible, and I don't want to have specific IDE project files for each OS.  In a nutshell, waf is a replacement for autotools like functionality, but it is a bit complex.  That being said, I think it's approach seems more reasonable than other tools like cmake or rake.

But, a thousand mile journey begins with the first step.  I've already cloned the latest libSDL 1.3, built it, and make a simple OpenGL context window with it.  This will allow me to use SDL instead of GLUT that the OpenGL SuperBible book uses.  A small step, but a step none the less.