Probably Dance

I can program and like games

Implementing coroutines with ucontext

Coroutines are a way of doing cooperative multithreading where each context has it’s own stack. And they can be incredibly useful for games. (I’ll use the word context as the coroutine equivalent of a thread) The basic idea of a coroutine is that you can yield from a context in the middle of a function, and then you will continue exactly at that point when the coroutine gets called the next time.
Imagine that you want something to happen in a sequence, for example you want to spawn an enemy, show a text for two seconds, then kill the enemy and show another text for two seconds. A trivial implementation would be this:

void sudden_exploding_unit(const Vector3 & pos)
{
    std::unique_ptr<Entity> guy = spawn_entity(pos, "grunt.entity");
    sleep(1.0f);
    show_text(pos, "oh no he is exploding!", 2.0f);
    sleep(0.5f);
    explode(*guy);
    sleep(1.0f);
    show_text(pos, "well that was underwhelming", 2.0f);
}

Even though this code is simple, you would probably have a large amount of pain to implement this in a game engine, because a game can never block, and this code is sleeping all over the place. You’d probably end up writing some kind of state machine (maybe using a kismet-like scripting system) or if you’re a terrible person you’d use asynchronous callbacks. State machines can be a fine solution, but they introduce way too much infrastructure for such a trivial piece of code.

Enter coroutines. If your engine has coroutines, you can write exactly the above code. With very little downsides. Because you could just sleep like this:

void sleep(float seconds)
{
    while (seconds >= 0.0f)
    {
        yield(); // resume here next frame
        seconds -= global_dt;
    }
}

Read the rest of this entry »

Learning D Part 3: Garbage Collection

Garbage collection is a red flag for any C++ programmer, simply because a garbage collector makes it more difficult to use the language for the kinds of things that C++ programmers like to program.

When I started programming in D, I was going to give the garbage collector the benefit of the doubt. The language has enough smart features that I figured that the designers must have had good reasons for including a GC.

But really, it’s a mess. In fact it’s a bigger mess than I have seen in other garbage collected languages. Fortunately that mess is solvable, and where it isn’t solvable the language designers could probably make it less of a problem.

Read the rest of this entry »

Learning D Part 2: Random Little Things I Like

D is better than C++ in many ways. Here are a couple examples that I have encountered so far.

Let’s start with the scope keyword:

void main()
{
    OSInterface.Load();
    scope(exit) OSInterface.Quit();
    //...
}

What this means is “at the end of the current scope, run OSInterface.Quit();. Meaning I can put the call to freeing resources right next to the line where I allocate it. There is also scope(failure) which only runs the when there is an exception, and scope(success) which only runs when the scope exits normally. This is a much better solution than try {} catch {} finally {}.

Another cool thing is lazy evaluation, for example in the function std.exception.enforce, which has this signature:

T enforce(T)(T value, lazy const(char)[] msg = null, string file = __FILE__, size_t line = __LINE__)

Read the rest of this entry »

Learning D Part 1: IDE and Libraries

I am learning the D programming language.

I’m a C++ programmer, and like most C++ programmers I am rather annoyed with C++. It can be a pretty bad language. The problem is only that all other languages are even worse for the stuff that you’d want to use C++ for.

But D seems promising. Reading through the features of the language, you notice that it has many smart solutions for things which are cumbersome or error-prone in C++. I’ll go into benefits and problems of features one by one as I am using them. But the D overview page says under “Who D is for:”

  • Programmers who routinely use lint or similar code analysis tools to eliminate bugs before the code is even compiled.
  • People who compile with maximum warning levels turned on and who instruct the compiler to treat warnings as errors.

Which sounds like me, or at least sounds like the programmer I wish I was.

But to start, you just want a simple “Hello, World” on the screen. And that already is kinda complicated in D.

Read the rest of this entry »

First Class Functions in C++

I’ve been working on a library that allows you to treat functions as first class objects in C++. Meaning you can assign to a function. Here’s a download link, and here’s a very simple example:

#include "fcf.h"
void foo()
{
    std::cout << "foo\n";
}
void bar()
{
    std::cout << "bar\n";
}
void main()
{
    foo(); // prints "foo"
    auto oldFoo = fcf::function(&foo);
    FCF_ASSIGN(&foo, &bar);
    foo(); // prints "bar"
    FCF_ASSIGN(&foo, oldFoo);
    foo(); // prints "foo"
}

If you have ever worked in a language with first class functions, you’ll know that you can do some quite cool stuff with this.

Read the rest of this entry »

The privacy implications of databases

Google Street View has had problems with being accepted in several countries. In Germany Google gave up for now after people requested that their houses be blurred. In Switzerland Google is now shooting images after some modifications and restrictions were agreed upon.

But one thing that you always find on the Internet is people expressing how they don’t get that someone might be upset about Google Street View. After all it only shows things that are already public. The problem is of course, that there are different scales of how publicly something is known about you. And if someone puts information about me in a searchable database on the internet, that’s very public.

Read the rest of this entry »

How to make successful student games

I’m almost done with DigiPen. Time to write down some learned lessons. I was only at DigiPen for two years, so others could probably write this better, but until they do there is this post. In both years I was in the most successful game team of the Master’s program, which is my claim to being qualified to write this post. So here are a couple rules, starting with

1. Gameplay first

This is the first point on Blizzard’s mission statement, and they mean “prioritize gameplay.” Do that. But for student games I consider it even more important to interpret that as “work on gameplay first.” The biggest mistake I have seen on student games is wasting time on features that don’t improve gameplay in the end. You do not have time for a student game.

Now how do you finish gameplay first?  Obviously you need tech to do your gameplay. If you want to make a physics-based puzzle platformer, someone has to implement your physics. But I would consider it a mistake to work on the physics until you know that it will result in good gameplay. Which is a problem.

Read the rest of this entry »

Job hunting in the Games Industry

I’m finishing my Master’s degree and applying to companies to work for. I had few pieces of data to know how difficult the job hunt would be: 1. Most of last year’s Master’s students didn’t get the job they wanted. 2. I know a lot of artists that graduated from DigiPen that ended up being unemployed, or who do game testing.

So I played it safe and applied to every company that I could see myself working at. Which wasn’t an incredibly long list, but it turned out that it was more than I should have applied to. Because every single company that I started talking to turned from “I could see myself working there” to “I would love to work there.”

And it surprised me that that happened so repeatedly. Maybe it shouldn’t have, because this is after all the industry that I wanted to work in. But it seems like a lot of people these days have figured out how to run a good studio and there is an impressive amount of likable personalities. And now my biggest problem is that I have to turn down companies that I would like to work at, because I have to make a decision…

But yeah, I just wanted to post something short expressing my delight at learning that this is not just a pretty cool industry in my imagination, but actually.

Old Wisdom

I recently came across this article about 10 papers every programmer should read. Now I don’t necessarily agree with all of that list, but here is the important part: I am about to finish a Master’s degree in computer science, and I have never heard of any of those papers before.

That is a list of papers which are sometimes more than 40 years old. Some of these gave us the assumptions that are normal for us to make about programming these days. I learned programming in the past ten years, and I had to rediscover some of this knowledge myself. I have made mistakes in programming that should never have been made, because people had solved those problems years ago. And I still see people make those same mistakes that I’m now smugly smarter about.

So what is there to do about it?

Read the rest of this entry »

GDC bragging

Since I’m handing out business cards at GDC this year, people should at least be able to see my current project on this site. So here is a simple video of my teammate running through a level.

Our game is very much inspired by Mirror’s Edge. We are receiving positive feedback from playtesting, and I think we’ll be able to finish a good game by the end of the semester.

We are still learning a lot from player feedback and we will not be using this level. We are working on new content, and are just interrupted by everyone going to GDC. Our tools allow us to make new content rather quickly, and I am really proud of them. So here is a video of them: