kpreid: (Default)
2012-04-02 08:16 pm

Practical optimization: where to start

Run your program on a platform slow enough that you

  1. care, and
  2. can feel where the problems are.

(Something — I assume a Chrome update — caused Cubes to run more slowly. Over fifteen seconds of startup time is just not fun for debugging, so I went looking for problems. Unfortunately, it wasn't anything straightforwardly bad, but the heaviest thing in the profile was the color-picking while constructing the default blockset, so I optimized that and got the startup down to about six seconds. Still slower than it really ought to be.)

kpreid: (Default)
2012-03-13 01:18 pm
Entry tags:

The rubber duck at the head of the army

You've probably heard (if you’re a programmer) about the cardboard programmer or rubber duck debugging.

These days, my rubber duck is the Ask a Question form at Stack Overflow (or another Stack Exchange Network site appropriate for the topic at hand). Writing a clear and considered question serves the same purpose as the hypothetical cardboard cutout or coworker — and if you don’t find your solution by the rubber duck method, then you have the question all ready to go, so no effort is wasted.

I haven’t gotten around to talking about it before, but I’m a big fan of Stack Exchange — it's a great place to get answers and give them, and focused on being a good resource in the long term and for all the web, not just another forum for discussions.

[profile for Kevin Reid on Stack Exchange]

kpreid: (Default)
2012-03-09 09:46 pm

Cubes update

Some new features in Cubes:

  • An automated test suite. It’s hardly complete coverage, but at least it exists and so can grow. (I ended up going with Jasmine for in-browser testing. I’m not a big fan of the Englishy syntax, but it does the job reasonably well and has niceties like rerunning individual tests and adequate support for asynchronous tests.)
  • Precise collision against rotated blocks: you can now walk up the slope of those funky pyramid blocks, and so on. Stairs, anyone?
  • Performance monitoring widget with fancy graphs. (If you click the “[-]” to hide it, then it won’t waste CPU time updating itself, either.)
kpreid: (Default)
2012-02-22 09:10 pm
Entry tags:

.innerText and .textContent

Today I learned that there is a standard-DOM alternative to the convenient IEism element.innerText (a close relative of element.innerHTML): element.textContent.

It is slightly different, according to MDN: .innerText returns the visible text (omitting scripts and CSS-hidden text), whereas .textContent returns everything, more like walking the document tree.

(This information crossed my awareness while working on Caja, but I didn't recognize it as something I could actually make use of until now.)

kpreid: (Default)
2012-02-17 08:44 pm
Entry tags:

My favorite git aliases

[alias]
	st = status
	hw = help -w
	ff = merge --ff-only
	unpushed = log @{u}..
kpreid: (Default)
2012-02-16 03:19 pm

Repositories moved to GitHub; new published project Timeline UI

I’ve uploaded almost all of my published Git repositories (previously hosted on a git-only server on on switchb.org, which is down at the moment) to my account on GitHub. Please update your remote URLs if you have any git clones.

The motivation for this change is simply that GitHub offers better visibility — an automatic web presence for each project, including viewing repository contents. I am not intending to depend on GitHub’s continued existence, of course; I still have local copies of each project, and additionally I plan to arrange so switchb.org automatically mirrors my GitHub repositories.

What I've just uploaded to GitHub also includes a project which I have not previously mentioned, timeline-ui:

A user interface experiment. Multiple types of time-series data, variously static/interactive, historical/future, etc. are displayed in a single view. (This was an idea I had floating around and which I used in 2010 for a class project; there is a lot more that could be done with it.) Written in Java.

I was going to write more about the concept, but I never got around to it; this will have to do.

List of projects just moved to GitHub:

kpreid: (Default)
2012-02-03 06:35 pm

Cubes vs. Minecraft

[I was asked about Cubes “Where do you think you might take the game play next?” and it turned into this.]

My original motivation for creating Cubes was a combination of the “blocks out of blocks” idea — which itself came from immersion in the graphics of Minecraft — and also dissatisfaction with certain bugs, limitations, and design choices in Minecraft. As a result, I’m not just building a voxel game; I’m building a game that shares what I like about Minecraft.

(What I like about Minecraft, broadly, is survival and engineering — I like building structures and machines to make my virtual life easier.)

Now, creating a Minecraft clone would be unoriginal — not just compared to Minecraft but because Minecraft clones are an entire genre. But I don’t have experience with what little exists of a genre of voxel building games to synthesize my own thing, and I myself am looking for something like Minecraft. What can I do? Here’s what I’ve been trying:

  • Be different.

    Whenever I see an opportunity to do something specifically unlike Minecraft, that doesn’t compromise what I’m trying to do, I take it and see what happens. However, most of these experiments have failed; for example, Cubes originally had a larger-scaled player character, but this turned out bad because it means tunneling and building is 8× more tedious, and it reduces the apparent size of the world. Also, it leads to thinking “OK, add this feature Minecraft has — but (superficially) differently!”

  • Be generic.

    This is my long-term goal, and it is one that ties neatly into the “blocks made of blocks” theme. The characteristics of blocks can be defined by building circuits (programs) inside them. What I’m aiming for is that by creating a blockset (collection of block designs which the player can build with), one is defining the game that can be played, by giving those blocks specific behaviors.

    In this way, I am working towards having a game which can be programmed to emulate Minecraft.

    (I have a working prototype of an importer for Minecraft worlds as well as for Minecraft blocks — that is, turning the terrain.png from a Minecraft texture pack into Cubes' 3D blocks — but I am not going to release that code until and unless I determine that Mojang doesn’t mind my doing so. I still love Minecraft and they deserve my not stepping on their toes that far.)

    However, this means both that Cubes itself needs to be very generic, and that the built-in example uses of such features should feel different from Minecraft.


So, returning to the original topic of “where am I going next”, I need to add the following functionality to the game world:

  • Extend the circuits feature so that there can be blocks that are active and interactive (e.g. opening and closing doors, “physics” like Minecraft falling sand and growing plants).

  • Add moving objects (for vehicles and mobs). I intend to generalize these so that they are worlds in themselves — this will allow large or unique vehicles, and mean that they can be designed using the same game tools.

  • Add some form of resource constraints/conservation laws (as in Minecraft survival mode) — that is, you have to gather stuff to make it into other stuff. I haven’t figured out specifically how I want to do this yet, and this seems particularly tricky to make programmable. One idea that keeps coming to mind is that when you break a block, specific subcubes are “resource cubes” (according to their type in the block world) which you collect, and in order to place a block you need to have the corresponding resources for its type. However, I’m not sure I like the “raw material counter” feel of this.

  • Add player attributes that can be modified (e.g. health) so that e.g. death, or other effects-by-the-world can be supported.

Less grandly, I plan to work on one of these specific technical features soon:

  • Allowing circuit blocks to be rotated to change their connectivity. (Right now, circuit blocks have specific faces — e.g. on a certain one the +X direction is always the output.)
  • Figure out what more circuit primitives I want to add. (Right now, the circuits are definitely not Turing-complete, and not capable of all the effects on the world they should be, but there are also already a lot of different primitives; I may have to invent new block-picking UI just to make them practical.)
  • Add moving objects (bodies) — things which can collide with the terrain as the player does. The current code is entangled with player behavior, and the player does not persist in a world.
  • Add subworld/multiple-world handling — the ability for more than one world (grid of blocks) to be present in the same space. Right now, there are hardwired assumptions that the player is in the single world’s coordinate system.

Another core feature which is currently missing is the ability to design a blockset and then reuse it for multiple worlds. The problem right now is that we're using a simple object-graph serializer, so each world has its own blockset which is modified independently. To fix this, it needs to be possible to save a blockset under a user-visible name, and have individual worlds which reference that blockset; also, the world generator needs to decouple blockset generation from terrain generation. The “persistence” framework which added support for multiple worlds is a step towards this; the main thing I am pondering is what the semantics of these separate-named-persistent objects are and what the user interface for editing them is.

kpreid: (Default)
2012-01-13 05:06 pm
Entry tags:

Followup on Cubes compatibility testing

Based on the feedback I’ve received, I conclude that the white-screen/crash problem with Cubes is hardware/driver-specific and not very widespread. Since

  • this is a hobby project and an experiment, not a Game to be Published,
  • the problem looks hard to debug,
  • I don't have a variety of machines to test on, and
  • I replaced the MacBook Pro in question with a newer model which does not exhibit the same problem,

I have decided not to debug it further at this time. I've added a note about the problem to the documentation, and I’ll instead work on the problems Cubes exhibits on the new machine :-) (such as a speckling of not-the-right-texture-tile at the edges of blocks, and a strange input lag) or adding new features, like more useful saving/persistence, sub-block collision detection, or subworlds.

kpreid: (Default)
2011-12-29 08:57 am
Entry tags:

Compatibility testing help wanted for Cubes

I upgraded to Mac OS X 10.7 Lion this month, and ever since then Cubes has crashed or otherwise misbehaved on my machine. However, I have only the one to test on, so I would like your help in figuring out what the extent of the incompatibility is — whether this is a GPU driver bug or something Cubes is doing wrong.

If you have a WebGL-capable browser (such as Chrome or Firefox) and GPU, and especially if you're running Lion or are otherwise similar to my configuration, please run Cubes (no installation is required) and tell me whether it works and what your system configuration is, including these properties:

PropertyI have
Hardware modelMacBookPro5,1
GPU modelNVIDIA GeForce 9600M GT
OS versionMac OS X 10.7.2
GPU driver version?
Browser versionChrome 17.0.963.12, Firefox 8.0

(For some MacBook Pro models such as mine, the GPU used (9600M or 9600M GT) depends on the “Graphics” setting in Energy Saver preferences.)

The problems I observe are:

  • All blocks (terrain) being flat white except for lighting; the sky and particle effects still have color. (For the interested, this appears to be a mis-execution of the over-unity-to-white code introduced in commit 48674f….)
  • After some amount of usage, a crash (in Firefox, the browser exits; in Chrome, the display goes gray and the Web Inspector console says “WARNING: WebGL content on the page might have caused the graphics card to reset” while the in-page text says “Previous GL errors: INVALID_OPERATION”).

I will appreciate any help as this is making it quite impractical to work on Cubes.

kpreid: (Default)
2011-12-05 06:16 pm
Entry tags:

On the creative placement of #define

I just invented a new (to me) abuse of the C preprocessor. From a Stack Overflow answer I just wrote:


My understanding is that you are defining a constant, using it later to size one or more arrays, and also want that constant to be a symbol, preferably without a messy namespace. (If it were a matter of exporting the size of a single array, I would instead suggest sizeof(array) / sizeof(*array) as missingno did.)

static const int ASIZE = 10;
#define ASIZE            10
int array[ASIZE];

Here, there is a variable with the desired value which will be in the object file, but the preprocessor macro shadows it with the value itself, so the array definition also succeeds.

However, you may find the need to duplicate the value expression ugly. Wouldn't it be nice if we could define the variable in terms of the macro?

static const int ASIZE =
#define ASIZE 10
                         ASIZE;
int array[ASIZE];

I'm not sure whether this is actually a better idea maintainability-wise than the above, but it works (and I couldn't get gcc to be offended by it) and it contains no duplication other than of the identifier. And it's amusing.


Previous C adventures: A dreadful thing

kpreid: (Default)
2011-11-12 07:52 am
Entry tags:

On asking the wrong question and getting the wrong right answer

These may be three different things:

  • The answer to the question you asked
  • The solution to the problem implied by the question you asked
  • The solution to the problem you actually have

I was reminded of this yesterday when, noticing that my code for Cubes was using an arbitrary constant for the near clip plane, I asked how to calculate it in a principled way.

kpreid: (Default)
2011-10-26 08:46 pm
Entry tags:

On compilation

Consider an interpreter as a function (Program, Input) → Output. Then:

A compiler is an optimized curried interpreter.

(Further reading: The Three Projections of Doctor Futamura)

kpreid: (Default)
2011-10-20 10:17 pm

Cubes Project: Block game user survey!

Cubes is a work-in-progress open-source block game (that is, like Minecraft) where the blocks are made out of blocks; in principle, you should be able to build a complete custom block game from scratch simply by building the appropriate block-set, out of blocks. As I mentioned earlier, I'm doing a project on the HCI for this game. I need data.

If you think you might be interested in playing my game, or if you're a current player of a block game (Minecraft, Terraria, FortressCraft, what-ever), please take this survey. I assure you there are no “On a scale from 1 to 5” questions in it.

kpreid: (Default)
2011-10-05 10:06 am

Reprioritization: Cubes wins. Users wanted.

In September, I wrote of my new project Cubes that “unfortunately, it wasn't an idea for either of my course projects this semester.” Since I have completely failed to find motivation for the course-related project I had planned to do, þreader (a thread-emphasizing feed reader; you can read the project introduction), I have decided to make the best of it and somehow make Cubes into the needed project.

I will therefore be using Cubes as my project for the course CS459 Human-Computer Interaction; the main consequence of this is that I need to actually study the potential users of my system.

So. To recap, Cubes is a block game (like Minecraft) where the blocks are made out of blocks; in principle, you should be able to build a complete custom block game from scratch simply by building the appropriate block-set, out of blocks. If you're a potential user of a game like this, and interested in giving me data (surveys, interviews, etc.), let me know. I will have something for you within the next few days.

Also, the live version of Cubes now supports load/save (either to Local Storage or by cut-and-paste of text (!)), so you can save your work! (I don't promise not to change the world format incompatibly, though.)

kpreid: (Default)
2011-09-12 01:05 pm

New Project: Cubes

On September 6, I had an idea which I couldn't not work on. (Unfortunately, it wasn't an idea for either of my course projects this semester.)

As you may know, in Minecraft, the individual cubical blocks are textured with unsmoothed 16×16 pixel textures. All blocks’ shapes (that aren't just cubes) have lengths in multiples of 1⁄16 block (example, so that the objects have (to some degree) the appearance of being made out of voxels, or smaller cubes. And, people have built giant versions of specific blocks out of colored blocks.

So, what if building blocks out of blocks was actually how the block types were defined?

And that's what I've implemented, in JavaScript/WebGL. The working project name is “Cubes”, because I don't know where it's going enough to give it a better name right now.

The block types you see above were all procedurally generated as placeholders, since I don't have any save/load facility to save manually edited blocks (and I'm not sure about the color scheme of the current set of blocks-you-make-blocks-out-of).

Play (requires WebGL-capable browser; I recommend Chrome).

Source code.

Controls: Standard WASD keys, or arrow keys, and mouselook. The left button adds or removes blocks at the location of the white rectangle cursor; the right button brings up a menu for choosing blocks (the empty square at the top left is the removal tool). The R key enters the world of the selected block; the F key exits.

Update 2011-09-13: I've improved startup; it now tells you more explicitly what went wrong if it fails to load, rather than a blank white screen. I've also added an in-game Help button with controls and browser compatibility info.

kpreid: (Default)
2011-08-14 02:30 pm
Entry tags:

(no subject)

I just wrote up a couple tips for debugging simple physics simulations (of the sort you use in 2D games), in a bit more detail than the context required.
kpreid: (Default)
2011-07-20 11:24 am

Apple goes least authority in “Lion”

Whoa. They even called it a powerbox as we do.

Apple has chosen to solve this problem by providing heightened permissions to a particular class of actions: those explicitly initiated by the user. Lion includes a trusted daemon process called Powerbox (pboxd) whose job is to present and control open/save dialog boxes on behalf of sandboxed applications. After the user selects a file or directory into which a file should be saved, Powerbox pokes a hole in the application sandbox that allows it to perform the specific action.

Mac OS X 10.7 Lion: the Ars Technica review

It probably ain't capabilities (no bundling of designation of the resource with authority to access it, unless they've replaced file pathnames, which I doubt), but it's a big step in a good direction. UPDATE: Ivan Krstić says “The implementation uses actual capabilities under the hood.”

kpreid: (Default)
2011-06-17 09:42 am

(no subject)

Something I still miss from the ORCA development shell for the Apple IIgs (at least, I think that's where it was) is the option to interpret a command name which is directory as a command to change to that directory; essentially letting you omit “cd ”.

kpreid: (Default)
2011-05-24 09:05 pm
Entry tags:

On the use of finalizers for managing external resources

I have several times heard that one should not rely on finalizers (that is, code invoked after some object becomes garbage) to reclaim external resources (file descriptors, temporary files, etc.), on the grounds that there is no guarantee they will be promptly reclaimed and therefore one might run out.

Certainly for resources whose use has semantic significance to an outside system (e.g. a network connection or a locked file) or if there is a potential shortage of resources affecting other processes (e.g. free disk space), one should free them promptly whenever possible. (Finalizers are still important for error recovery unless you’re programming completely without nonlocal exits and extremely carefully, in which case you’re probably writing C and don’t have finalizers.)

But if the concern is for limited internal resources (most prominently, the limit on number of open file descriptors), and the process is entirely managed by the GC, would it not suffice to force a garbage collection and retry in the event that opening a file fails due to lack of file descriptors, just as if running out of memory while allocating memory?

kpreid: (Default)
2011-05-11 06:58 pm

Not the Traveling Salesman Problem

The problem of arranging a set of unbreakable text blocks in columns in an arbitrary order such that the columns are of approximately equal height is equivalent to the NP-complete “multiprocessor scheduling” problem, and the “LPT algorithm” described in the linked article performed excellently for my instance of the problem (4 columns, 6 blocks), which is actually my packing list for traveling to California tomorrow.

(I have a program which generates my packing list for me, based on type, duration, season, and transportation. It encodes all the accumulated knowledge about what to pack and where to pack it; after a trip I review my changes written on the printed list and incorporate them into the program.)