Had a late start, and feeling a bit under the weather today, so not a whole lot of progress on Rubygame today. I did manage to take care of some thoughtless work like putting license/copyright headers on some of the new source files.
I was also thinking about keyboard events. It bugs me that keyboard events use integer constants (e.g.
Rubygame::K_A) to identify the key that was pressed. This is one of those ugly style issues that Rubygame inherited from Pygame and SDL. My main objections to this style are:
- Awkward to use. You either have to
include Rubygameor litter your code with
- Clutters up the Rubygame namespace with hundreds of constants.
- Similarly clutters the C code, which has a huge list of "rb_define_const(...)" calls.
- Too C-ish!
The obvious, Ruby-ish way is to use :symbols instead. Lovely, lovely :symbols. Instead of the cumbersome
Rubygame::K_LEFT, you get the much easier to read
And it actually requires much less code to implement the symbols than it does the integer constants. SDL has a SDL_GetKeyName function which returns a string like "left", "space", "a", "left shift". It's trivial to turn those strings into a Ruby symbol, and only slightly more difficult to sanitize them a bit, e.g. changing spaces to underscores to get
:left_shift instead of
There are a few keys that don't work so great as symbols, because of the names SDL returns. The colon key, for example, is returned as the printable ":", rather than "colon". So, to make that a symbol, it has to become
:":", which is not so pretty. Or backslash (
:"\\") or double-quote (
:"\""); also not very pretty. Prettier than
Rubygame::K_QUOTEDBL, but not as pretty as
:3. (By the way, I actually had to look up
K_QUOTEDBL in the docs just now — that gives you a clue about how memorable these constant names are.)
Since Rubygame 3.0.0 is my chance to break backwards compatibility in as many ways as I want, I'm considering trying to eliminate the keyboard integer constants and replace them with the symbols instead. Also perhaps for other things, like Surface/Screen flags (SWSURFACE, FULLSCREEN).