---
deprecated: true
excerpt_separator: <br/>
category: technology
layout: post
title: 'Project TreeWars: The Beginning'
date: '2011-06-26T08:00:00.000-04:00'
author: Anna Wiggins
tags:
- Programming
- SDL
- C++
- TreeWars
modified_time: '2013-10-22T11:19:51.363-04:00'
blogger_id: tag:blogger.com,1999:blog-4209116010564764361.post-5020071627931204840
blogger_orig_url: http://www.stringofbits.net/2011/06/project-treewars-beginning.html
---

My goal is two posts a week - typically, one on Wednesday and one on the weekend. This is usually a pretty easy goal to obtain - it doesn't take too much of my free time to churn out two posts between 750 and 1500 words. And yet, there almost wasn't a post this weekend. Was I playing <a href="http://www.minecraft.net/">Minecraft</a>? Nope.<br/><br/>I was writing code.<br/><br/>Programming is something I've always wanted to do professionally, but somehow I managed to end up doing enterprise technical support instead. So, it has become a hobby. And when a project really starts flowing, it is easy to lose myself in it. So I spent three days this week spending every available moment adding code to my new project: a game I'm tentatively calling TreeWars. I've managed to tear myself away from the project for a while, but rather than put it out of my mind, I've decided to blog about interesting things that happen during the development process.<br/><br/>First, meet TreeWars:<br/><br/><a href="http://stringofbits.files.wordpress.com/2011/06/treewars.png"><img class="alignnone size-full wp-image-449" title="treewars" src="http://stringofbits.files.wordpress.com/2011/06/treewars.png" alt="" width="450" height="347" /></a><br/><br/>Not much to look at, I know. But the project is still very much a work in progress.<br/><br/>Some interesting things about TreeWars:<br/><ul><br/>	<li>It is my second attempt at games programming, and my first was just a software implementation of a classic abstract board game (hnefatafl). And it was written using the graphics tools in GTK (which are serviceable, but not ideal). As a result, I'm learning all sorts of interesting things about the fundamentals of programming games.</li><br/>	<li>It is my first (real) attempt at graphics programming as well. I'm using SDL, and may eventually rewrite the rendering engine (and parts of the game engine) to make the game 3d and use OpenGL. Either way, I hope to make it cross-platform, although I'm currently only focusing on Linux, since it's my native platform.</li><br/>	<li>It is based on <a href="http://en.wikipedia.org/wiki/Tree_%28graph_theory%29">trees from graph theory</a>, not <a href="http://en.wikipedia.org/wiki/Tree">those other kind of trees</a>.</li><br/>	<li>Currently, all of those beautiful graphics are <a href="http://en.wikipedia.org/wiki/Procedural_generation">procedurally generated</a>. It is actually harder than you would think to draw a circle, at least in SDL.</li><br/>	<li>It (and the idea to blog about the process) was inspired by <a href="http://www.shamusyoung.com/twentysidedtale/?p=11874">Project Frontier</a>, a fascinating project by an experienced graphics programmer who is making far more interesting things than I am with procedural content generation! But hey, give me time...</li><br/></ul><br/>So, the first step on the path to TreeWars was to learn enough SDL to generate a simple accelerated 2D image (not that it really needs to be accelerated right now, but I hope that will change in time). It turns out, this is both somewhat harder and somewhat easier than I expected.<br/><br/>I found a reasonably nice SDL tutorial <a href="http://www.sdltutorials.com/sdl-tutorial-basics/">here</a>. In fact, TreeWars was just 'sdltutorial' (with plans to start writing a game once I'd worked through some tutorial programs) until I got to lesson where you implement Tic Tac Toe, and my mind insisted 'Tic Tac Toe is boring, let's make a real game!' I justified that I often learn better when I'm puzzling out the best way to do something myself, and leapt into it.<br/><br/>The problem is, I often learn better that way, but I also often learn the <strong>hard</strong> way. I stumbled through a lot of 'wrong' ways of doing things in SDL, wasting a good deal of time hunting down the correct function (out of numerous functions available in the API) for getting a particular result. In the middle of all of that, it occurred to me that there is a knowledge level for which tutorials are never written. Tutorials are often written in a way that is so basic that I have to skip past large portions and carefully pluck out that bits that are relevant to me. On the other hand, APIs are often dense, and each function definition assumes you know the platform generally, so getting deep knowledge out of it presents a bootstrapping problem. Libraries almost never have a 'using our library for the first time' guide, or a best practices document, or a list of caveats that newcomers <em>to that particular library</em> may need to know about. So you get your choice between a too-low-level API for a huge, unfamiliar library, or a guide that tells you over and over that you need to call SDL_FreeSurface() when you're done with a surface.<br/><br/>Instead, I'd like a guide that goes something like this:<br/><br/>Before you start drawing anything with SDL, you need to include the headers and call SDL_Init. In Linux, the headers are typically at /usr/include/SDL/SDL.h, but for portability it is recommended that you include the SDL header directory directly with '-I /usr/include/SDL' and use "#include &lt;sdl.h&gt;". A common init call would look like this:<br/><br/>[sourcecode language="cpp" gutter="false" wraplines="false"]<br/>if (SDL_Init(SDL_INIT_EVERYTHING) &lt; 0) {<br/>    // error handling code goes here (return false<br/>    // if calling from an object or function,<br/>    // perhaps, or possibly exit())<br/>}<br/>[/sourcecode]<br/><br/>Typically, drawing graphics in SDL starts by creating a double-buffered, hardware-accelerated SDL_Surface object with SDL_SetVideoMode, like so:<br/><br/>[sourcecode language="cpp" gutter="false" wraplines="false"]<br/>SDL_Surface* window;<br/>window = SetVideoMode(1024, 768, 32,<br/>                      SDL_HWSURFACE | SDL_DOUBLEBUF);<br/>[/sourcecode]<br/><br/>You can think of this as your main window (or root window, if you are comfortable with that terminology). You can draw other SDL Surfaces onto it (a process known as 'blitting' for some reason), and you can render the current contents of the (double-buffered) window with:<br/><br/>[sourcecode language="cpp" gutter="false" wraplines="false"]<br/>SDL_Flip(window);<br/>[/sourcecode]<br/><br/>'Flip' may not make much sense until you understand the metaphor in use for a double-buffered surface. Think of a flat, thin surface with 2 sides - one side is visible (facing the user). When you draw onto the surface, all of your images are drawn on the invisible side - the side facing 'away' from the user. When you 'flip' this surface, the part that they were looking at is hidden, and the part you were drawing on is exposed. This lets you do your graphics updates 'all at once' instead of having the user see each object as you draw it.<br/><br/>So, to refresh the screen once per second, you might do something like this:<br/><br/>[sourcecode language="cpp" gutter="false" wraplines="false"]<br/>// assuming you have already initialized<br/>// a surface called 'window'<br/>while (true)<br/>{<br/>    // draw some stuff on window here<br/>    SDL_Flip(window);<br/>    sleep(1);<br/>}<br/>[/sourcecode]<br/><br/>There are several ways to draw things on surfaces - you can blit another surface on with SDL_BlitSurface(), or use SDL_FillRect() to draw a rectangle (this is SDL's primary primitive shape, so procedurally generating curves becomes an interesting problem).<br/><br/>Of course, in practice you would probably want to do other things than just continuously render images - you need to respond to user input, iterate any data that needs to periodically change, etc. You might do those things in separate threads, but it is often simpler to check for them in a loop like this one. In fact, most graphics-based programs that respond to user input do things like this.<br/><br/>Oh, one other thing. When you're done with a surface, you always want to clean it up with:<br/><br/>[sourcecode language="cpp" gutter="false" wraplines="false"]<br/>SDL_FreeSurface(window);<br/>[/sourcecode]<br/><br/>Next time, we'll look at some particular problems I encountered, and how I solved them. Then we'll look at how I solved them correctly, to demonstrate that there are usually many ways to solve a problem. This will also let us highlight the difference between good and bad code. And we might talk some more about the joys of using the SDL API.