I once used LÖVE to implement a simple game where a player control a pixel, eats other pixels and grow. This is my second attempt: in order to get familiar with Rust, SDL2 and to develop my game architecture (described later) I decided to implement a simple game.
I've decided to use Rust, along with rust-sdl2
. I also use image
and ttf
features of rust-sdl2
.
First of all, ensure that you have dev versions of sdl2
, sdl2-image
and sdl2-ttf
. You should be able to obtain them either using your OS package manager or by downloading them from SDL2 site and then following instructions in INSTALL
file. If you're on Windows you should add directory with .dll files to your PATH
to run the built game.
Next, clone the repository and run cargo build --release
in it. You will need to move target/release/hungry-pixel-rs
executable along with resources
dir somewhere and then you'll be able to run it. As an alternative, you can use cargo run
command to just run the dev build.
Linux users can run sh scripts/release.sh
from repository root to create a .tar.gz
archive with everything needed to play (including a shell-script run.sh
).
If you want to build the game under Windows, you will have to do the following steps:
- Install Rust and Cargo (preferrable way to do so is to use rustup.rs)
- Install MinGW (you will likely want to install MSYS as well) and make sure its
bin
directory is added to PATH. - Use
rust-x86_64-pc-windows-stable
(or the corresponding 32 bit version) as your target in rustup - Download MinGW dev libs from SDL2 site. You will need SDL2, SDL2_image and SDL2_ttf.
- Unpack the archives downloaded in previous step, and put all
.la
and.a
files fromlib
directory in archive to your rustup toolchainlib
folder, e.g.C:\Users\<your username>\.rustup\toolchains\stable-x86_64-pc-windows-gnu\lib\rustlib\x86_64-pc-windows-gnu\lib
- Put
.dll
files from the archives somewhere in your PATH (MinGWbin
directory will do just fine) and also save them somewhere if your intention is to create a standalone executable archive. - Run MSYS prompt and put hungry-pixel-rs sources in your MSYS
home
directory (it is located within MinGW path) - Use
cargo build --release
to build the game, then copy resources and executable to target directory along with all thedll
files.
You will likely need the latest MSVC redistributable which can be obtained via Windows Update or from Microsoft site.
Game architecture is based on a notion of "States". A State
is an entity that knows how to respond to Messages
and how to render
itself on screen. States are gathered into a stack of states that resides in the Engine
entity. Engine
also has a queue of Messages
.
Inside the Engine
, a loop is running:
- First, the events from SDL are collected. Some of them are translated into
Messages
that are added to MessageQueue. - Then the rendering happens. All the states are analyzed, searching the first that returns
true
whenis_fullscreen
is called on it. Then they are rendered in the reverse order, from that state to the top of the stack. - Then, messages are propagated down the
States
stack. EveryState
in stack can either consume, transform or pass the message. Consuming happens whenprocess_message
fn returnsNone
, and makes theEngine
process next message right away.State
can transform message by returning aSome(Message)
variant ofOption<Message>
type fromprocess_message
fn, or pass it by returning the original message inSome
variant. The message is then propagated to the nextState
in queue and passed to theprocess_message
method of it. - When the message is either consumed or propagated all the way through the stack, it is processed by the
Engine
itself and next message is consumed.
The game is distributed under MIT
license. Font used (PressStart2P-Regular.ttf
) is distributed under OFL License.
Copyright (c) 2017 Crazy-Owl (https://github.com/Crazy-Owl)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.