Skip to content

Commit

Permalink
First draft of a tutorial
Browse files Browse the repository at this point in the history
  • Loading branch information
17cupsofcoffee committed Dec 23, 2018
1 parent ac1b340 commit 6e218b8
Show file tree
Hide file tree
Showing 10 changed files with 384 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/target
/book
**/*.rs.bk
Cargo.lock
SDL2.dll
25 changes: 25 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
language: rust
sudo: false

cache:
- cargo

rust:
- stable

before_script:
- (test -x $HOME/.cargo/bin/cargo-install-update || cargo install cargo-update)
- (test -x $HOME/.cargo/bin/mdbook || cargo install --vers "^0.1" mdbook)
- cargo install-update -a

script:
- cd path/to/mybook && mdbook build

deploy:
provider: pages
skip-cleanup: true
github-token: $GITHUB_TOKEN
local-dir: book
keep-history: false
on:
branch: master
8 changes: 8 additions & 0 deletions book.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[book]
authors = ["Joe Clay"]
multilingual = false
src = "docs"
title = "Tetra"

[build]
preprocess = ["index"]
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{{#include ../README.md}}
8 changes: 8 additions & 0 deletions docs/SUMMARY.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Summary

- [Introduction](./README.md)
- [Tutorial](./tutorial/README.md)
- [Installation](./tutorial/installation.md)
- [Getting Started](./tutorial/getting-started.md)
- [Loading a Texture](./tutorial/loading-a-texture.md)
- [FAQ](./FAQ.md)
5 changes: 5 additions & 0 deletions docs/tutorial/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Tutorial

- [Installation](./installation.md)
- [Getting Started](./getting-started.md)
- [Loading a Texture](./loading-a-texture.md)
119 changes: 119 additions & 0 deletions docs/tutorial/getting-started.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# Getting Started

Once you have [installed SDL and set up your project](./installation.md), you're ready to start writing a game!

## Creating Some State

The first step is to create a struct to hold your game's state. To begin with, let's create some text, and store a position where we want to render it:

```rust
use tetra::graphics::{Text, Font, Vec2};

struct GameState {
text: Text,
position: Vec2,
}

impl GameState {
fn new() -> GameState {
GameState {
text: Text::new("Hello, world!", Font::default(), 16.0),
position: Vec2::new(0.0, 0.0),
}
}
}
```

## Adding Some Logic

Now that we have some data, we need a way to manipulate it. In Tetra, you do this by implementing the `State` trait.

`State` has two methods - `update`, which is where you write your game logic, and `draw`, which is where you draw things to the screen. By default, the former is called 60 times a second, and the latter is called in sync with your monitor's refresh rate.

Let's write some code that draws our text moving across the screen:

```rust
use tetra::graphics;
use tetra::{State, Context};

impl State for GameState {
fn update(&mut self, ctx: &mut Context) -> tetra::Result {
self.position.x += 1.0;

Ok(())
}

fn draw(&mut self, ctx: &mut Context, _dt: f64) -> tetra::Result {
graphics::clear(ctx, Color::rgb(0.392, 0.584, 0.929));
graphics::draw(ctx, &self.text, self.pos);

Ok(())
}
}
```

You might be wondering what the `Context` that we're passing around is for, or where it comes from - let's take a closer look!

## Building a Context

`Context` is the object that represents all the global state in the framework (the window settings, the rendering engine, etc.). Most functions provided by Tetra will require you to pass the current context, so that they can read from/write to it. As your game grows, you'll probably write your own functions that pass around `Context`, too.

Let's build a new context with a window size of 1280 by 720, and run an instance of our `GameState` struct on it:

```rust
use tetra::ContextBuilder;

fn main() -> tetra::Result {
ContextBuilder::new("My First Tetra Game", 1280, 720)
.build()?
.run(&mut GameState::new())
}
```

If you try `cargo run`, you should see your text scrolling across the screen!

## Next Steps

In [the next chapter](./loading-a-texture.md), we'll try loading a texture to display on the screen.

Here's the full example from this chapter:

```rust
use tetra::graphics::{self, Text, Font, Vec2};
use tetra::{State, Context, ContextBuilder};

struct GameState {
text: Text,
position: Vec2,
}

impl GameState {
fn new() -> GameState {
GameState {
text: Text::new("Hello, world!", Font::default(), 16.0),
position: Vec2::new(0.0, 0.0),
}
}
}

impl State for GameState {
fn update(&mut self, ctx: &mut Context) -> tetra::Result {
self.position.x += 1.0;

Ok(())
}

fn draw(&mut self, ctx: &mut Context, _dt: f64) -> tetra::Result {
graphics::clear(ctx, Color::rgb(0.392, 0.584, 0.929));
graphics::draw(ctx, &self.text, self.position);

Ok(())
}
}

fn main() -> tetra::Result {
ContextBuilder::new("My First Tetra Game", 1280, 720)
.build()?
.run(&mut GameState::new())
}
```
76 changes: 76 additions & 0 deletions docs/tutorial/installation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Installation

## Creating a New Project

Create a new Cargo project:

```bash
cargo new --bin my-first-tetra-game
```

Then, add Tetra as a dependency in `Cargo.toml`:

```toml
[dependencies]
tetra = "0.2"
```

## Installing SDL 2.0

Tetra is built on top of SDL 2.0, so you will need to have both the runtime and development libraries installed.

The instructions below are adapted from the README of the [sdl2](https://github.com/Rust-SDL2/rust-sdl2) crate - further information can be found there.

### Windows (with MSVC toolchain)

1. Go to [the SDL website](https://www.libsdl.org/download-2.0.php) and download the Visual C++ version of the development libraries.
1. Copy the `.lib` files from the `SDL2-2.0.x/lib/x64` folder of the zip to the `%USERPROFILE/.rustup/toolchains/stable-x86_64-pc-windows-msvc/lib/rustlib/x86_64-pc-windows-msvc/lib` folder on your machine.
* If you are building on a beta/nightly toolchain, adjust the location accordingly.
1. Copy `SDL2.dll` from the `SDL2-2.0.x/lib/x64` folder of the zip to the root of your Tetra project. You will also need to include this file alongside your `.exe` when distributing your game.

### Windows (with GNU toolchain)

1. Go to [the SDL website](https://www.libsdl.org/download-2.0.php) and download the MinGW version of the development libraries.
1. Copy the `.lib` files from the `SDL2-2.0.x/x86_64-w64-mingw32/lib` folder of the zip to the `%USERPROFILE/.rustup/toolchains/stable-x86_64-pc-windows-gnu/lib/rustlib/x86_64-pc-windows-gnu/lib` folder on your machine.
* If you are building on a beta/nightly toolchain, adjust the location accordingly.
1. Copy `SDL2.dll` from the `SDL2-2.0.x/x86_64-w64-mingw32/bin` folder of the zip to the root of your Tetra project. You will also need to include this file alongside your `.exe` when distributing your game.

### Mac

The easiest way to install SDL is via [Homebrew](http://brew.sh/):

```bash
brew install sdl2
```

You will also need to add the following to your `~/.bash_profile`, if it is not already present.

```bash
export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/lib"
```

### Linux

The SDL development libraries are distributed through most Linux package managers - here are a few examples:

#### Ubuntu/Debian

```bash
sudo apt-get install libsdl2-dev
```

#### Fedora/CentOS

```bash
sudo yum install SDL2-devel
```

#### Arch Linux

```bash
sudo pacman -S sdl2
```

## Next Steps

Once this is complete, you're ready to [start writing your first game with Tetra](/docs/getting-started)!
Loading

0 comments on commit 6e218b8

Please sign in to comment.