Play With Lua!

Ternary

without comments

Another short post. It’s because I’m working on something.

A lot of languages have a ternary operator, that lets you do this:

int x = foo ? 1 : 2;

It’s an easy way to pack a conditional on to one line, especially useful when giving things default values. Another way is if the if statement returns a value, like in Ruby:

x = if foo
        1
    else
        2
    end

Read the rest of this entry »

Written by randrews

May 10th, 2012 at 3:15 pm

Posted in Uncategorized

Lua Unicode Snowman For You

without comments

Short one this time:

print( string.char( 0xe2, 0x98, 0x83 ) )

If you have a console that supports UTF-8, you’ll see a barely recognizable glyph of a snowman. If you don’t, here’s what you’re missing.

(happens to be my second-favorite website of all time, behind this one)

The handiness of this for roguelike development is plain. There’s a good character list (with hex codes) here.

Written by randrews

April 27th, 2012 at 7:07 pm

Posted in Uncategorized

Colored text in Lua

without comments

If you, like me, are writing a text-mode Roguelike game in Lua, you’ll probably be interested in the thing I just published: a small Lua library for coloring text output to the console.

It lets you do things like this:

print(color.bg.green .. color.fg.RED .. "This is bright red on green")

Couldn’t be simpler. Enjoy!

Written by randrews

April 25th, 2012 at 7:18 pm

Posted in Uncategorized

Raw keyboard input

without comments

I’ve wanted to do a small case study of making an extension for a long time now. I’m writing a Roguelike game in Lua, so I had a need for one, and knocked it together last night: a C extension to provide Lua with a function to read one key from the keyboard.

Read the rest of this entry »

Written by randrews

April 22nd, 2012 at 3:43 pm

Posted in Uncategorized

Why programmers get so upset about DRM

without comments

In a few hours, on the 18th of January, a lot of sites are going to “go dark” to protest SOPA, the latest in a series of draconian, badly-thought-out laws to regulate the internet and computing in general. This is probably old news to you, and I’m going to spare you another explanation of why this particular bill is so bad. Instead, I want to talk about in general, why programming knowledge is so strongly correlated with getting upset about things like this. It’s kind of a long story, and it starts in ancient Greece.

For what it’s worth, this site won’t go dark. I think it’s more important to leave it up so you can read this post.
Read the rest of this entry »

Written by randrews

January 17th, 2012 at 11:04 pm

Posted in Uncategorized

Lua distribution

without comments

So you have made a program in Lua. You want to give it to people to run. You run into two problems right away: one, you don’t want to require people to install Lua to run your program, and two, you may not want them to have your source code (if it’s a commercial program). So, what to do?

If we were in a language like C, we could precompile our program into a binary and distribute that. So, since Lua can very closely integrate with C, can we do that with Lua? The answer turns out to be “yes”.
Read the rest of this entry »

Written by randrews

August 8th, 2011 at 10:05 am

Posted in Uncategorized

No update this week

without comments

I am spending the time making a presentation on ZeroMQ for the Lone Star Ruby Conference.

Written by randrews

July 31st, 2011 at 1:24 pm

Posted in Uncategorized

A short trick: transparent containers

without comments

I thought of this today, and I thought it might make an interesting short post. One of the small annoyances of Lua is that it doesn’t have a lot of convenient support for arrays. You can implement arrays with tables, of course, but they’re still tables: they don’t have default metatables (so you can’t do things like join), they don’t pretty-print (so it’s a hassle to see what you have), and there’s still only one iterator (using ipairs with a for loop). You can do it but it’s not exactly convenient. So I started thinking about how exactly I’d add all that stuff.
Read the rest of this entry »

Written by randrews

July 24th, 2011 at 12:35 am

Posted in Uncategorized

Lua modules, explained simply

without comments

I’m not an expert on Lua. I’ve been using it for less than a year; most of the time before that I wrote Ruby, JavaScript, Scheme, etc. It’s not a terribly large language. I usually tell people that if they know JavaScript they can learn Lua in a weekend, and that’s true as far as the syntax goes. But, there are several Lua idioms that are either complicated or just not explained well, and one of those is modules. You can get a long way coding in Lua without ever using a module, but eventually you’ll want to know how they work.

I’m going to explain what modules are for, how to use them, and most importantly why they work that way, and I’m going to do that by implementing Lua’s module system myself. Let’s begin:
Read the rest of this entry »

Written by randrews

July 15th, 2011 at 1:53 pm

Posted in Uncategorized

Storing bitmaps in quadtrees

without comments

This is an adaptation of a chapter from The New Turing Omnibus, a great little tour of a few dozen areas of CS. This is about a way to compress a bitmap, to store it without storing each individual pixel.

The technique is called a quadtree. We’re going to split a bitmap up into four equally-sized chunks, then compress and store those. The savings comes from the fact that we aren’t going to store equivalent chunks twice.
Read the rest of this entry »

Written by randrews

July 10th, 2011 at 3:13 am

Posted in Uncategorized