Catching Errors when your Lua Script doesn’t start
Getting an error code back when a Lua script dies is actually pretty straightforward. Use lua_pcall() or one of its relatives to call the script and the return value will tell if there's been a run time error.
The Hilbert Curve in Java, Lua and C++

I wrote a program a little while ago that draws a Hilbert Curve. Really all I did was adapt the Java source code that’s on the Wikipedia page talking about the curve. That Java source used a class called SimpleGraphics that I’m not familiar with. I wanted to use SDL in C++ for my program. It was easy enough to convert the Java code to C++ but to do the drawing I decided to just implement a SimpleGraphics class in C++ that uses SDL to perform the actions that the program needs.

Since then I revived my interest in integrating scripting with C++ and pulled a Lua interpreter in to that project. Now my program loads a Lua script and executes it. The Lua script has access to the SimpleGraphics class that I wrote. Now that the script interpreter works and can call the drawing methods I decided to take the algorithm for drawing the Hilbert curve and implement that as a Lua script. It was actually pretty easy. The resulting source code works as a sort of Rosetta stone for the three languages.

Ooops - A SWIG Interface Correction
I glossed over the fact that my program crashed at the end of the script in my post on hooking up Lua to a C++ application. I new at the time I posted that there was a memory problem in calling the SimpleGraphics destructor but what I didn't realize was that the problem was actually caused by the difference in the declaration of the SimpleGraphics class between the header file that the C++ program sees and the declaration that the SWIG-generated file sees. Here's the one that gets compiled in to the C++ application:
WoW: A few Lua Macros to save time and clicks

At level 69 you can use a Riding Crop to get a mounted speed bonus. Since my main is finally level 69 I've been eying the Auction House. I just lucked out and found one for only 41 gold. On my server they're usually over 80 so I snapped it up right away. Of course then I have the issue of switching trinkets so can have the Riding Crop while mounted and go back to my other trinkets when I dismount. This got me looking at macros again and since macros are written with Lua in WoW, I thought this ties in nicely with the Lua interpreter stuff I've been writing about lately. Today I'll share the macros I've been using for a while now to smooth the rough edges off the World of Warcraft interface. I didn't write most of them but I've tweaked them to meet my needs.

Using SWIG to connect C++ to Lua
I closed off my last post about adding Lua to an SDL application saying I’d look at a tool to do the bindings for me. I actually already had something in mind for the job. I looked at a couple but SWIG seems to be the most versatile. It’s not tied just to Lua so the work I put in to learning how to use SWIG will also apply partly to any other scripting languages I want to support. Any within the realm that SWIG supports, of course. Notably missing from the list so far is Javascript. Scheme is represented in the form of Guile (which is a specific Scheme interpreter as far as I understand). The documentation for SWIG is pretty sizable but of course with all the combinations of host & script language out there they aren’t all explained clearly enough for a beginner like me. So I started to experiment. SWIG focuses on wrappers for C, C++ support seems more complex. So to understand things I had to scale back even from my simple SDL C++ test program then scaled back up once I got some communication between code written in both languages.
Starting a Simple Example using Lua and SDL in C++

I’ve been poking around with a couple C++ programs recently and I started thinking again about how cool it is that you can hook in a scripting language. I’ve wanted to try setting up scripting for a few programs in the past but it always seems too far removed from any one project. This sounds kind of abstract, more concretely I’ve been thinking about scripting behaviours in a game or exposing a script interface from some simple graphics program.

Lua’s a popular scripting language for games. It’s used for add-ons in World of Warcraft and I’ve heard it brought up over and over again at the Game Developer’s Conference. I tried a short example of Lua a while back but I never went back to write any real code with Lua. Since the license is so liberal and writing a little Lua would help me out writing WoW macros and add-ons, I decided Lua would be a good place to start - have a look at yesterday's post about setting up an SDL project in Visual Studio.

Next for the test harness. I took some code to draw a Hilbert curve from Wikipedia about a week ago and converted it into something roughly equivalent in C++ using SDL. The code compiles and runs under Linux (I used KDevelop) or Visual Studio 2003. In the code I implemented a SimpleGraphics class which takes an SDL surface and gives some methods to draw lines on that surface. The HilbertCurve class uses a SimpleGraphics object to do the drawing. I decided this little program would be a great candidate to get scripted.

Moving Multiple Files in Bash
It seems like the shorter the code is the longer it takes to write. I'm trying to rename a group of similarly-name directories using Bash and for no good reason I decided I'd like to do it all on one line. There are a bunch of ways to do it, not all fit on a line and some are more efficient than others. There's apparently a package out there called mmv that I haven't looked for which does stuff like this. There's also sed. With sed I got something close:
Simple PHP for Dumping a Berkeley Database
I will one day come up with a better way to show PHP in my blog but in the meantime I'm not going to let that stop me from sharing a handy script I put together yesterday. I grabbed most of the code from examples on but the examples as they were didn't do what I wanted. What I came up with is a short PHP script that shows the DBA handlers available then dumps the contents of the database file specified in the $db variable up at the top. It uses the "db4" handler, you can change that (and whatever else) to suit your needs. I made this the other day just to have a look in some of the LDAP database files that I was messing with yesterday. So here's the BDB dump script as text. Download it, rename it as .php and enjoy.
Scheme Script for Processing Saturation and Lightness in the Gimp
I'm going to try to keep this short but I wanted to make a note of a quick script I just did to desaturate and lighten a bunch of images in the Gimp. The workflow I used was basically to open a bunch of images then do exactly the same thing to each one of them then close and save each one. My first thought was that clicking Tools->Colour Tools->Hue Saturation... then doing an adjustment & hitting Okay was taking too long. I knew I'd been here before so I dug out the script I did before to do Hue Rotation in the Gimp. I remember that painful learning curve to figure out Scheme. Luckily this time I want to use roughly the same command but I want to leave the hue alone and change the other two parameters. So I modified the code and pasted this in to the Script-Fu console.
Getting Last Month’s Date in Bash

This one just blew me away: I spent a while trying to figure out how to turn this month's date into last month's date in a Bash script. In the script (okay, it drives AWStats reporting) I want to make a summary file for last month. To do this I want to check if a file exists with last month in it's name and if it doesn't exist I want to create it.

Syndicate content