• Stars
    star
    3,266
  • Rank 13,752 (Top 0.3 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created almost 5 years ago
  • Updated about 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Cross-platform game engine in Rust.

macroquad

Github Actions Docs Crates.io version Discord chat

macroquad is a simple and easy to use game library for Rust programming language, heavily inspired by raylib.

macroquad attempts to avoid any Rust-specific programming concepts like lifetimes/borrowing, making it very friendly for Rust beginners. See the docs.

Features

  • Same code for all supported platforms, no platform dependent defines required.
  • Efficient 2D rendering with automatic geometry batching.
  • Minimal amount of dependencies: build after cargo clean takes only 16s on x230(~6 years old laptop).
  • Immediate mode UI library included.
  • Single command deploy for both WASM and Android build instructions.

Supported Platforms

  • PC: Windows/Linux/macOS;
  • HTML5;
  • Android;
  • IOS.

Build Instructions

Setting Up a Macroquad Project

Macroquad is a normal rust dependency, therefore an empty macroquad project may be created with:

# Create empty cargo project
cargo init --bin

Add macroquad as a dependency to Cargo.toml:

[dependencies]
macroquad = "0.3"

Put some macroquad code in src/main.rs:

use macroquad::prelude::*;

#[macroquad::main("BasicShapes")]
async fn main() {
    loop {
        clear_background(RED);

        draw_line(40.0, 40.0, 100.0, 200.0, 15.0, BLUE);
        draw_rectangle(screen_width() / 2.0 - 60.0, 100.0, 120.0, 60.0, GREEN);
        draw_circle(screen_width() - 30.0, screen_height() - 30.0, 15.0, YELLOW);

        draw_text("IT WORKS!", 20.0, 20.0, 30.0, DARKGRAY);

        next_frame().await
    }
}

And to run it natively:

cargo run

For more examples take a look at Macroquad examples folder

Linux

# ubuntu system dependencies
apt install pkg-config libx11-dev libxi-dev libgl1-mesa-dev libasound2-dev

# fedora system dependencies
dnf install libX11-devel libXi-devel mesa-libGL-devel alsa-lib-devel

# arch linux system dependencies
 pacman -S pkg-config libx11 libxi mesa-libgl alsa-lib

Windows

On windows both MSVC and GNU target are supported, no additional dependencies required.

Also cross-compilation to windows from linux is supported:

rustup target add x86_64-pc-windows-gnu

cargo run --target x86_64-pc-windows-gnu

WASM

rustup target add wasm32-unknown-unknown
cargo build --target wasm32-unknown-unknown

This will produce .wasm file in target/debug/wasm32-unknown-unknown/CRATENAME.wasm or in target/release/wasm32-unknown-unknown/CRATENAME.wasm if built with --release.

And then use the following .html to load it:

index.html
<html lang="en">

<head>
    <meta charset="utf-8">
    <title>TITLE</title>
    <style>
        html,
        body,
        canvas {
            margin: 0px;
            padding: 0px;
            width: 100%;
            height: 100%;
            overflow: hidden;
            position: absolute;
            background: black;
            z-index: 0;
        }
    </style>
</head>

<body>
    <canvas id="glcanvas" tabindex='1'></canvas>
    <!-- Minified and statically hosted version of https://github.com/not-fl3/macroquad/blob/master/js/mq_js_bundle.js -->
    <script src="https://not-fl3.github.io/miniquad-samples/mq_js_bundle.js"></script>
    <script>load("CRATENAME.wasm");</script> <!-- Your compiled wasm file -->
</body>

</html>

One of the ways to server static .wasm and .html:

cargo install basic-http-server
basic-http-server .

IOS

To run on the simulator:

mkdir MyGame.app
cargo build --target x86_64-apple-ios --release
cp target/release/mygame MyGame.app
# only if the game have any assets
cp -r assets MyGame.app
cat > MyGame.app/Info.plist << EOF
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>CFBundleExecutable</key>
<string>mygame</string>
<key>CFBundleIdentifier</key>
<string>com.mygame</string>
<key>CFBundleName</key>
<string>mygame</string>
<key>CFBundleVersion</key>
<string>1</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
</dict>
</plist>
EOF

xcrun simctl install booted MyGame.app/
xcrun simctl launch booted com.mygame

For details and instructions on provisioning for real iphone, check https://macroquad.rs/articles/ios/

Tips Adding the following snippet to your Cargo.toml ensures that all dependencies compile in release even in debug mode. In macroquad, this has the effect of making images load several times faster and your applications much more performant, while keeping compile times miraculously low.
[profile.dev.package.'*']
opt-level = 3

async/await

While macroquad attempts to use as few Rust-specific concepts as possible, .await in all examples looks a bit scary. Rust's async/await is used to solve just one problem - cross platform main loop organization.

Details

The problem: on WASM and android it's not really easy to organize the main loop like this:

fn main() {
    // do some initialization

    // start main loop
    loop {
        // handle input

        // update logic

        // draw frame
    }
}

It is fixable on Android with threads, but on web there is not way to "pause" and "resume" WASM execution, so no WASM code should block ever. While that loop is blocking for the entire game execution! The C++ solution for that problem: https://kripken.github.io/blog/wasm/2019/07/16/asyncify.html

But in Rust we have async/await. Rust's futures are basically continuations - future's stack may be stored into a variable to pause/resume execution of future's code at a later point.

async/await support in macroquad comes without any external dependencies - no runtime, no executors and futures-rs is not involved. It's just a way to preserve main's stack on WASM and keep the code cross platform without any WASM-specific main loop.

Community

  • Quads Discord server - a place to chat with the library's devs and other community members.
  • Awesome Quads - a curated list of links to miniquad/macroquad-related code & resources.

Platinum sponsors

Macroquad is supported by:

More Repositories

1

miniquad

Cross platform rendering in Rust
Rust
1,540
star
2

nanoserde

Serialisation library with zero dependencies
Rust
687
star
3

quad-snd

Cross-platform audio for Rust
Rust
110
star
4

egui-miniquad

Rust
78
star
5

tinyecs

[DEPRECATED] Rusty entity-component-system
Rust
62
star
6

bulletrs

Bullet Physics wrapper for the Rust language.
Rust
49
star
7

megaui

DEPRECATED in favor of macroquad::ui
Rust
38
star
8

rie

REPL-like interactive code editor
Rust
29
star
9

gameoff-2019

Rust
15
star
10

tinyprof

Simple frame profiler
Rust
14
star
11

quad-rand

Rust
13
star
12

quad-net

Rust
13
star
13

awesomium-rs

WIP rust bindings for Awesomium browser
Rust
11
star
14

quad-gl

Rust
11
star
15

nanoshredder

Rust
11
star
16

isalive

Project pulse monitor with Rust backend and Elm frontend
Elm
10
star
17

nanogltf

Rust
8
star
18

megaui-macroquad

DEPRECATED in favor of macroquad::ui
Rust
7
star
19

particles-editor

Rust
7
star
20

quad-android-playground

Rust
7
star
21

platformer-book

6
star
22

miniquad_text_rusttype

Rust
6
star
23

nanoserde-bench

Rust
5
star
24

ld44

Rust
5
star
25

miniquad-js-interop-demo

Rust
5
star
26

ld46

Rust
5
star
27

imgui-miniquad-render

Rust
5
star
28

sapp-jsutils

Rust
5
star
29

rust-swag

5
star
30

macroquad-website

Automatically generated site with all macroquad examples
HTML
5
star
31

miniquad-samples

JavaScript
4
star
32

cargo-webquad

Rust
4
star
33

sloop

Rust
4
star
34

hale-sample

Rust
2
star
35

rays

Ray tracing
Rust
2
star
36

macroquad-sample-project

Rust
2
star
37

sloop-example-projects

Rust
2
star
38

megaui_test

Rust
1
star
39

coldspace

Play with static-topology frp.
Haskell
1
star
40

example-android-fileopen

Java
1
star
41

tinypreprocessor

Rust
1
star
42

nanoimage

Rust
1
star
43

tinyprof-imgui

Rust
1
star
44

incremental_bug

Rust
1
star
45

AllocaFail

Rust
1
star
46

pong

The most correct pong ever
Idris
1
star
47

tinyprof-termion

Rust
1
star
48

not-fl3.github.io

1
star
49

squarebug

Rust
1
star
50

Notebot

Haskell
1
star
51

example-android-bluetooth

Rust
1
star
52

quad-androidx

Rust
1
star
53

quad-bt

1
star
54

derivebug

Rust
1
star
55

teapot

temporary repo for emscripten tests
JavaScript
1
star
56

test-sys

Rust
1
star
57

advent-of-code-2018

Haskell
1
star
58

acontrol

Web time tracking tool.
Haskell
1
star