# Introduction to 2D Linux Game Programming/Printable version

The current, editable version of this book is available in Wikibooks, the open-content textbooks collection, at
https://en.wikibooks.org/wiki/Introduction_to_2D_Linux_Game_Programming

# Introduction
### What this Book Covers

This book covers writing a 2D game in Linux from start to finish. It starts with setting up the build environment for various distributions, and ends with packaging the game for distribution. It also includes creating tools to make creating the game easier. Sprite and level editors for instance. The game itself will be written in SDL 1.2 and use ALSA for audio. We'll primarily cover SDL 1.2, which is probably the most commonly used flavor of SDL out there right now, and briefly cover its successor SDL 1.3. (If SDL 1.3 becomes more prevalent, we may make the transition wholly to that library.) The level editors and other helper tools will largely be written in Qt4. Sample algorithms may be demonstrated first by simply printing to stdout where applicable for simplicity's sake.
Linux is available on a plethora of environments, so this book will also cover porting code from the original environment, in this case an AMD64 based PC, to other environments. The two primary target environments will be the F-200 and the Pandora. Both are ARM based hand-helds, but with very different capabilities, input schemes, and screen resolutions. We'll cover aspects of porting from one environment to another, including control scheme, screen resolution and aspect ratio, and programming considerations.
It will also briefly cover level design considerations, both when dealing with aspect ratio, and when transitioning from an arcade environment to a PC, and to a console.

### What this Book Doesn't Cover

This is a programming book, a how to guide for the creation of the code and levels behind a 2D Linux game. It doesn't cover managing your programming team, or suggest ideas for games other than the one that's created in the text of this book. It doesn't delve into marketing of the game.

### Languages and Libraries Used in this Book

The code in this book is written in C++. It uses the libraries SDL, Qt4, and ALSA.

### Who This Book is Intended For

This book is intended for those who who want to have fun programming games in Linux! It doesn't assume an extensive background in C++ programming, though a basic grasp of the C++ language is required. Language fundamentals are reviewed as those topics are reached while programming the game, but as a fundamental C++ primer this would present the language in an incomplete and haphazard fashion. There are many good C++ Primers that cover the language in a coherent and comprehensive fashion that would be better suited to teaching the student C++.
Some of the concepts covered in this text are complex. Where that's the case this book will cover the concept in a step by step fashion. It is intended to guide those who've been exposed to C++ but may not have a huge experience with the language, and may not have any experience with game programming at all. Experienced programmers may find utility in this book as a reference to topics that are scarcely covered in other texts, though they may find the detail we go into in this book exhaustive.

### The Target Game

The end goal of this book is to walk the student through programming and packaging a fully fledged 2D platforming and side scrolling game. Super Mario Brothers is often thought of as the epitome of this genre. Our game, "A Ragamuffin's Destiny" will follow the concepts laid out in those and other good platforming games, while introducing a few twists of our own. All the sprites in this initial game will be low resolution. Most the concepts should translate well to higher resolution 2D games.

# Introduction
### What this Book Covers

This book covers writing a 2D game in Linux from start to finish. It starts with setting up the build environment for various distributions, and ends with packaging the game for distribution. It also includes creating tools to make creating the game easier. Sprite and level editors for instance. The game itself will be written in SDL 1.2 and use ALSA for audio. We'll primarily cover SDL 1.2, which is probably the most commonly used flavor of SDL out there right now, and briefly cover its successor SDL 1.3. (If SDL 1.3 becomes more prevalent, we may make the transition wholly to that library.) The level editors and other helper tools will largely be written in Qt4. Sample algorithms may be demonstrated first by simply printing to stdout where applicable for simplicity's sake.
Linux is available on a plethora of environments, so this book will also cover porting code from the original environment, in this case an AMD64 based PC, to other environments. The two primary target environments will be the F-200 and the Pandora. Both are ARM based hand-helds, but with very different capabilities, input schemes, and screen resolutions. We'll cover aspects of porting from one environment to another, including control scheme, screen resolution and aspect ratio, and programming considerations.
It will also briefly cover level design considerations, both when dealing with aspect ratio, and when transitioning from an arcade environment to a PC, and to a console.

### What this Book Doesn't Cover

This is a programming book, a how to guide for the creation of the code and levels behind a 2D Linux game. It doesn't cover managing your programming team, or suggest ideas for games other than the one that's created in the text of this book. It doesn't delve into marketing of the game.

### Languages and Libraries Used in this Book

The code in this book is written in C++. It uses the libraries SDL, Qt4, and ALSA.

### Who This Book is Intended For

This book is intended for those who who want to have fun programming games in Linux! It doesn't assume an extensive background in C++ programming, though a basic grasp of the C++ language is required. Language fundamentals are reviewed as those topics are reached while programming the game, but as a fundamental C++ primer this would present the language in an incomplete and haphazard fashion. There are many good C++ Primers that cover the language in a coherent and comprehensive fashion that would be better suited to teaching the student C++.
Some of the concepts covered in this text are complex. Where that's the case this book will cover the concept in a step by step fashion. It is intended to guide those who've been exposed to C++ but may not have a huge experience with the language, and may not have any experience with game programming at all. Experienced programmers may find utility in this book as a reference to topics that are scarcely covered in other texts, though they may find the detail we go into in this book exhaustive.

### The Target Game

The end goal of this book is to walk the student through programming and packaging a fully fledged 2D platforming and side scrolling game. Super Mario Brothers is often thought of as the epitome of this genre. Our game, "A Ragamuffin's Destiny" will follow the concepts laid out in those and other good platforming games, while introducing a few twists of our own. All the sprites in this initial game will be low resolution. Most the concepts should translate well to higher resolution 2D games.

# Algorithms/Simple Software Transformations/Non Optimized Integer Scaling
### Introduction

This algorithm demonstrates scaling an image by one positive integer value in both the x and y axis. A common use for this algorithm would be taking a game made for a GameBoy's low resolution screen and doubling or tripling it before displaying it on a PC's high resolution monitor.
This algorithm keeps the aspect ratio of the original image. Think of watching old TV shows on a widescreen monitor. You can either stretch the image to fit the screen, distorting the original image and making all the actors appear fatter, or you can watch in the original aspect ratio, but then you have black bars on either side of the screen. With this algorithm you'd have the black bars. Princess Peach won't have to worry about why she's gained so much weight!
To understand how this algorithm works, it's important to know how most libraries store image data in memory. Where an image is a two dimensional object, most libraries store the individual pixels of the image in a single dimensional array - a line. The game has to keep track of the height of the image and its width to manipulate the image. Some libraries, like SDL, hide this from you until you need to do something like rotate or scale the image.

### Setting up the Problem

To show how this works, we'll scale one 3x3 image. In the following images, each square will represent a single pixel.
Stored in an array, that image looks like:
If you're trying to come up with an algorithm, it's handy to step through the problem a piece at a time. The first thing we're going to do is copy the first pixel three times:
We need to continue copying 3 pixels for every pixel in the source image until we hit the end of the row to properly scale the width of the image:
Then we need to repeat this 3 times to properly scale the height of the image:
Which drawn on the screen looks like:

### The Source:
# Algorithms/Simple Software Transformations/Optimized Integer Scaling
### Optimized Image Scaling

This takes the previous, non-optimzed version of the integer scaling program and changes a few things to make it much quicker.

### The Source:
# Algorithms/Simple Software Transformations/90° Image Rotation
### 90° Image Rotation

In 2D games it's often necessary to rotate an image by 90 degrees. This algorithm does so quickly and easily.

### The Source:
# Algorithms/Simple Software Transformations/Flip Image
### Flip Image

Probably the most common reason to flip an image in gaming is to reverse the direction a character is running. For instance, if our Ragamuffin is running left, we could use this algorithm to flip his image and have him run to the right. You'd typically do this before you actually needed to have him run in the other direction. This algorithm may actually be more beneficial in a sprite creation program than in the game itself, but either way it neatly flips the image horizontally, or vertically.

### The Source:
# Algorithms/Simple Software Transformations/The Helper Functions
### The Helper Functions

Rather than writing a function to print the 2D array of integers for every one of these simple examples, or manually ask for and validate input, these helper functions do the work for us. It also keeps the code cleaner, keeping the printing away from the algorithm we're showing.

### The Source:
# Algorithms/Simple Software Transformations/The Zipped Code
### The Zipped Code

* The Zipped Source Code