Skip to content

Instantly share code, notes, and snippets.

@nddrylliog
Created November 13, 2012 21:31
Show Gist options
  • Save nddrylliog/4068528 to your computer and use it in GitHub Desktop.
Save nddrylliog/4068528 to your computer and use it in GitHub Desktop.
Game Programming in ooc

Game Programming in ooc

Getting started

The first thing you need to know about ooc is that it compiles down to C. Sure, in theory you could compile it to JavaScript or whatever, but it was really designed to spit out C under the hood.

Why bother then?

Couldn't we just write code like this?

#include <stdio.h>

int main(int argc, char *argv) {
  printf("%s", "Hi world!\n");
  return 0;
}

Sure, but I prefer writing code like this:

"Hi world!" println()

What else is there?

Apart from being shorter, allowing code outside the main function, and having a few nice things built-ins, ooc allows you to go object-oriented, like so:

Bullet: class extends Actor {

  init: func (.game) {
    super(game)
  }

  explode: func {
    game boombox play("bullet-explosion")
    destroy() // defined in Actor
  }

}

And then use it like so:

// Let's assume we have a game object from before
b := Bullet new(game)

game on("random", ||
  b explode()
)

Wow, wow, wow, back up.

Okay, that was probably pretty fast. But you did see some nice things, e.g.:

  • How to define a class - that inherits from another class
  • How to use dot-parameters to shorten parameter lists
  • How to call member methods and super-constructor
  • That you don't always need parameter lists
  • How to define a closure
  • How to define a variable with an inferred type
  • String literals

What's next?

I don't feel like going through each feature of the language individually so I'll just write example code and comment as I go.

Also, I have no idea if I'll follow through with this series, so don't expect much.

Game Programming in ooc

Go forth and compile

As we mentioned earlier, ooc compilers just generate C code. Which means once the C code is generated, it still has to be compiled to produce an executable.

Which ooc compiler to use?

Nowadays, everybody uses rock. I used to maintain it but now it's our dear friend Alexandros. Thanks dude!

Instructions for OSX

There's a homebrew formula for rock:

brew install rock

(Make sure you have XCode installed, it needs to compile C code - but homebrew should have told you that already)

Homebrew packages are stored somewhere in /usr/local/Cellar/, but usually formulas will put binaries in your path, which means rock should work out of the box:

rock -V

Should output its version number.

Instructions for Linux

There might be a package for your distribution, but compiling it is easy. Make sure you have the basics to build software (ie. a C compiler, make, etc. - the build-essential package takes care of that on Ubuntu), and go ahead and clone the git repository:

git clone git://github.com/nddrylliog/rock.git
cd rock
make rescue

And that's it! You now have a rock executable in bin/. You can check that it's working by running:

bin/rock -V

And it should output its version number. You probably want to add it to your $PATH, so edit your ~/.bashrc or ~/.zshrc to add a line like:

export PATH=$PATH:/path/to/rock/bin/

Where /path/to/rock/ is the path to where you cloned your rock.

Note: do not copy the rock executable elsewhere! It uses its location to find the SDK, which is needed to compile ooc programs.

(You can override the SDK path with the ROCK_SDK environment variable, but chances are, you probably won't need this)

Instructions for Windows

While rock does work on Windows, it's one of the least comfortable platforms to develop in. My personal favorite is Linux, then I can live with OSX, and I can survive on Windows, but I really wouldn't do any serious development there (except on the Microsoft toolchain, but that's another story.)

However, nothing stops you from developing most of your game on Linux/OSX and then come up with a Windows build for the release. Also, if you make music / graphics on Windows, nothing stops you from committing them to a git repository that you'll later pull from Linux/OSX and use them while developing your game.

If you should have to compile rock under Win32, however, here are a few tips:

First, you need to get mingw32. Long story short, it's a GCC port for Windows. Install the latest GCC (no need for C++/Java/Fortran/whatnot support) along with MSYS so you get a nice MinGW Shell shortcut with bash, vim, and all the utilities you're used to.

Then, you probably want to be able to deal with git repositories, if only to compile rock. Grab msysgit and install it. That'll provide you with another start menu entry: Git Bash.

Launch Git Bash, cd to somewhere comfy, and run:

git clone git://github.com/nddrylliog/rock.git

Then, exit Git Bash, launch MinGW Shell, cd to that same directory, and run:

make rescue

If everything goes fine, it should leave you a nice rock.exe file in bin/. In MinGW, you can use Unix-style paths (with /) and you don't have to specify the .exe extension, so this command should display the version number:

bin/rock -V

Be aware that some parts of the SDK have different underlying implementations for *nix and Windows. Even though they should behave the same, there might be some subtle differences.

Make sure to report any bug you encounter on rock's issue tracker.

Compiling your first program

Let's start with a classical hello world:

"Hi world!" println()

Save this as hello.ooc with your favorite text editor (but, honestly, you should learn vim).

Run

rock hello.ooc
./hello

And it should greet you. Congratulations! You've compiled and ran your first ooc program.

@FredericJacobs
Copy link

Nice. I know ooc now. :)

@alexnask
Copy link

@nddryliog Are you gonna use ldkit in this? :)

Btw, I'm running out of manageable bugs to fix here, so I will probably not push anything on rock for a few more days

@alexnask
Copy link

Also, you know, studies, architectural design inking etc...

@nddrylliog
Copy link
Author

@FredericJacobs Haha, almost :) There are lots of trick you still need to learn...

@Shamanas Welp, I have pretty much declared ldkit deprecated. I wanna show how to build your own kit, how to cover libraries, write modular code, some physics, audio code, event-based system.. pretty much everything you need in small bits and pieces.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment