macroquad

repository·master·Indexed 26 days ago

https://github.com/not-fl3/macroquad

A simple, easy-to-use game library for Rust inspired by raylib. It provides efficient 2D rendering, an immediate mode UI, and high cross-platform compatibility for WASM, Android, Windows, Linux, and iOS. Version 0.4.15 includes features such as the #[macroquad::main] and #[macroquad::test] macros, a particle system with Emitter and EmittersCache, and a physics-platformer module for managing worlds, actors, and static tiled layers.

Tokens
9.2K
Snippets
15
Records
82
Agent score
88%

What's inside macroquad

  1. Understand the role of mq_js_bundle.js

    master
    The mq_js_bundle.js file is a consolidated JavaScript bundle that contains the most common dependencies required by Macroquad when running in a web environment. It aggregates core functionality from several underlying libraries into a single file to simplify web deployment.
  2. Set up a Macroquad project

    master

    Macroquad is a standard Rust dependency. To start a new project, initialize a cargo binary project and add macroquad to your Cargo.toml dependencies.

    To improve performance (especially image loading) while keeping compile times low, add the following snippet to your Cargo.toml to ensure dependencies compile with high optimization even in debug mode:

    [profile.dev.package.'*']
    opt-level = 3
    # Create empty cargo project
    cargo init --bin
    [dependencies]
    macroquad = "0.4"
  3. Build for WASM (HTML5)

    master

    To target WebAssembly, add the wasm32-unknown-unknown target and build using cargo build. The resulting .wasm file will be located in target/wasm32-unknown-unknown/ (debug or release). To serve the files, you can use basic-http-server.

    rustup target add wasm32-unknown-unknown
    cargo build --target wasm32-unknown-unknown
    
    # To serve static files
    cargo install basic-http-server
    basic-http-server .
  4. Install Linux system dependencies for Macroquad

    master

    Depending on your Linux distribution, install the following system packages to support graphics and audio:

    Ubuntu/Debian: apt install pkg-config libx11-dev libxi-dev libgl1-mesa-dev libasound2-dev

    Fedora: dnf install libX11-devel libXi-devel mesa-libGL-devel alsa-lib-devel

    Arch Linux: pacman -S pkg-config libx11 libxi mesa-libgl alsa-lib

    # 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
  5. Run on iOS Simulator

    master

    To run your game on an iOS simulator, build for the x86_64-apple-ios target, package it into a .app directory with an Info.plist, and use xcrun simctl to install and launch it.

    mkdir MyGame.app
    cargo build --target x86_64-apple-ios --release
    cp target/x86_64-apple-ios/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
  6. Build and run for Windows

    master

    Macroquad supports both MSVC and GNU targets on Windows without additional dependencies. You can also cross-compile to Windows from Linux using the x86_64-pc-windows-gnu target.

    # Cross-compiling to windows from linux
    rustup target add x86_64-pc-windows-gnu
    
    cargo run --target x86_64-pc-windows-gnu
  7. Configure Macroquad with Conf

    master

    To customize window settings, batching capacities, or update triggers, create a function that returns a macroquad::conf::Conf object and pass it to the #[macroquad::main] macro.

    use macroquad::prelude::*;
    
    fn window_conf() -> Conf {
        Conf {
            miniquad_conf: miniquad::conf::Conf {
                window_title: "Window name".to_owned(),
                fullscreen: true,
                ..Default::default()
            },
            ..Default::default()
        }
    }
    
    #[macroquad::main(window_conf)]
    async fn main() {
        // ...
    }
  8. Create a Macroquad application

    master

    Use the #[macroquad::main] attribute to define the entry point of your application. The main function must be async. You can pass a string for the window title or a configuration function that returns a Conf object for more advanced settings.

    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("HELLO", 20.0, 20.0, 20.0, DARKGRAY);
    
            next_frame().await
        }
    }
  9. Create a basic Macroquad application

    master

    Use the #[macroquad::main] attribute on an async fn main() to define your entry point. The main loop should use next_frame().await to yield control and allow the engine to process the next frame. This pattern is required for cross-platform compatibility, especially for WASM and Android.

    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
        }
    }
  10. Configure an Emitter with EmitterConfig

    master

    The EmitterConfig struct defines the behavior, appearance, and physics of a particle system. Key configuration options include:

    • local_coords: If true, particles spawn at the position supplied to .draw() but live in the current camera coordinate system. If false, they use a coordinate system relative to the emitter's draw position.
    • emission_shape: Defines the region where particles spawn (Point, Rect, or Sphere).
    • one_shot: If true, only one emission cycle occurs.
    • lifetime & lifetime_randomness: Base lifespan and the range of randomness applied to it.
    • amount: Total particles to be emitted in one cycle.
    • explosiveness: Controls emission timing (0 = equal gaps, 1 = all at once).
    • initial_direction & initial_direction_spread: The base direction vector and the angle of random fluctuation.
    • initial_velocity & initial_velocity_randomness: Base speed and randomness.
    • linear_accel: Velocity acceleration applied in the direction of motion.
    • gravity: A Vec2 applied to each particle.
    • shape: The geometry of individual particles (Rectangle, Circle, or CustomMesh).
    • colors_curve: A ColorCurve defining how particle color changes from start to mid to end over its lifetime.
    • size & size_randomness: Base size and randomness.
    • size_curve: An optional Curve to scale particle size over its lifetime.
    • blend_mode: Rendering mode (Alpha or Additive).
    • texture: An optional Texture2D for particles.
    • atlas: An optional AtlasConfig for animated textures.
  11. Handle disabled audio feature

    master
    If the audio feature is disabled during compilation, the audio API falls back to a dummy implementation. In this mode, play calls will print a warning to stderr (warn: macroquad's "audio" feature disabled.), and functions like load_sound or play_sound will not produce actual sound.