Simple Pacman clone written in Zig.

Overview

pacman.zig

Like https://github.com/floooh/pacman.c, but in Zig

Zig bindings for the sokol headers are here: https://github.com/floooh/sokol-zig

Build and Run

Zig installation: https://github.com/ziglang/zig/wiki/Install-Zig-from-a-Package-Manager

On macOS only, a 'system linker hack' is currently needed which requires setting an environment variable in the shell session where 'zig build' will run:

# macOS only:
> export ZIG_SYSTEM_LINKER_HACK=1

From here on it's the same procedure on macOS, Windows and Linux:

> git clone https://github.com/floooh/pacman.zig
> cd pacman.zig
> zig build run

Tested with zig version 0.7.1+

On Windows, rendering is done through D3D11, on Linux through OpenGL and on macOS through Metal.

On Linux, you need to install the usual packages for GL-, X11- and ALSA-development.

Comments
  • Fix the codebase for latest zig and zld changes

    Fix the codebase for latest zig and zld changes

    which include:

    • ast fixes
    • extern enum(i32) is now enum(i32)
    • there is no need for ZIG_SYSTEM_LINKER_HACK on macOS

    The last change depends on ziglang/zig#9229


    Hope you don't mind the PR @floooh; I used your awesome project as a test ground for my linker work so thought I might as well contribute fixes for latest master. If you feel this should go in a different branch than main, lemme know and I'll redo a PR onto a different branch!

    opened by kubkon 5
  • eating powerup causes panic when starting with `const DbgStartRound >= 16`

    eating powerup causes panic when starting with `const DbgStartRound >= 16`

    on macos 10.15.7 with zig 0.8.0. setting DbgStartRound in [0,2,8,12,14,15] all work fine but values in [16,32,64] cause an integer overflow panic as soon as i eat a powerup. The panic appears to be somewhere in gameUpdateSprites() but i can't tell exactly where because of this.

    BTW I love this code! ❤️

    opened by colemanbroad 4
  • Sokol build on macOS

    Sokol build on macOS

    I noticed your are building an object file manually for macOS. You don’t have to do that. Zig can compile Sokol metal no problem. Here is what my build file looks like for one of my projects that used Sokol: https://github.com/prime31/zig-upaya/blob/master/src/deps/sokol/build.zig

    opened by prime31 4
  • Circumvent miscompilation of @Vector on arm64 macOS

    Circumvent miscompilation of @Vector on arm64 macOS

    We circumvent the miscompilation by moving all @ Vector fn args as final args.

    @floooh this is not a final solution, but rather a temp one which makes the project run correctly on arm64 macOS. Lemme know if you're OK with this fix, or whether you'd rather leave it as-is until a fix to upstream LLVM is provided.

    Fixes #8

    opened by kubkon 3
  • Errors building iOS on Mac M1 `'string.h' file not found`

    Errors building iOS on Mac M1 `'string.h' file not found`

    The Metal/MacOS build works great, but building for iOS or iOS simulator fails trying to find string.h--any advice on how to "correctly" hack in paths here would be great :)

    kev@mini pacman.zig % zig version
    0.9.0
    
    kev@mini pacman.zig % xcode-select -p
    /Applications/Xcode.app/Contents/Developer
    
    kev@mini pacman.zig % zig build -Dtarget=aarch64-ios
    error(compilation): clang failed with stderr: In file included from /Users/kev/src/pacman.zig/src/sokol/sokol.c:13:
    /Users/kev/src/pacman.zig/src/sokol/sokol_app.h:1539:10: fatal error: 'string.h' file not found
    
    /Users/kev/src/pacman.zig/src/sokol/sokol.c:1:1: error: unable to build C object: clang exited with code 1
    pacman...The following command exited with error code 1:
    /Users/kev/zig/zig build-exe /Users/kev/src/pacman.zig/src/pacman.zig -lc -cflags -ObjC -- /Users/kev/src/pacman.zig/src/sokol/sokol.c --cache-dir /Users/kev/src/pacman.zig/zig-cache --global-cache-dir /Users/kev/.cache/zig --name pacman -target aarch64-ios -mcpu generic --pkg-begin sokol /Users/kev/src/pacman.zig/src/sokol/sokol.zig --pkg-end -framework Cocoa -framework AudioToolbox -framework QuartzCore -framework MetalKit -framework Metal --enable-cache 
    error: the following build command failed with exit code 1:
    /Users/kev/src/pacman.zig/zig-cache/o/2a84dfe8266a0ba257d5a9640258a651/build /Users/kev/zig/zig /Users/kev/src/pacman.zig /Users/kev/src/pacman.zig/zig-cache /Users/kev/.cache/zig -Dtarget=aarch64-ios
    
    kev@mini pacman.zig % zig build -Dtarget=aarch64-ios-simulator
    error(compilation): clang failed with stderr: In file included from /Users/kev/src/pacman.zig/src/sokol/sokol.c:13:
    /Users/kev/src/pacman.zig/src/sokol/sokol_app.h:1539:10: fatal error: 'string.h' file not found
    
    /Users/kev/src/pacman.zig/src/sokol/sokol.c:1:1: error: unable to build C object: clang exited with code 1
    pacman...The following command exited with error code 1:
    /Users/kev/zig/zig build-exe /Users/kev/src/pacman.zig/src/pacman.zig -lc -cflags -ObjC -- /Users/kev/src/pacman.zig/src/sokol/sokol.c --cache-dir /Users/kev/src/pacman.zig/zig-cache --global-cache-dir /Users/kev/.cache/zig --name pacman -target aarch64-ios-simulator -mcpu generic --pkg-begin sokol /Users/kev/src/pacman.zig/src/sokol/sokol.zig --pkg-end -framework Cocoa -framework AudioToolbox -framework QuartzCore -framework MetalKit -framework Metal --enable-cache 
    error: the following build command failed with exit code 1:
    /Users/kev/src/pacman.zig/zig-cache/o/2a84dfe8266a0ba257d5a9640258a651/build /Users/kev/zig/zig /Users/kev/src/pacman.zig /Users/kev/src/pacman.zig/zig-cache /Users/kev/.cache/zig -Dtarget=aarch64-ios-simulator
    
    opened by kevinw 2
  • Missing stdint.h on macOS

    Missing stdint.h on macOS

    Using source-built zig 0.8.0, I get:

    frarees@MBP18 pacman.zig % zig build run                                                                              
    error(compilation): clang failed with stderr: In file included from /Users/frarees/Documents/pacman.zig/src/sokol/sokol.c:13:
    In file included from /Users/frarees/Documents/pacman.zig/src/sokol/sokol_app.h:996:
    /Users/frarees/Documents/zig/build/lib/zig/include/stdint.h:20:43: fatal error: cannot open file '/Users/frarees/Documents/zig/build/lib/zig/libc/include/x86_64-macos-gnu/stdint.h': No such file or directory
    
    /Users/frarees/Documents/pacman.zig/src/sokol/sokol.c:1:1: error: unable to build C object: clang exited with code 1
    pacman...The following command exited with error code 1:
    /Users/frarees/Documents/zig/build/bin/zig build-exe /Users/frarees/Documents/pacman.zig/src/pacman.zig -lc -cflags -ObjC -- /Users/frarees/Documents/pacman.zig/src/sokol/sokol.c --cache-dir /Users/frarees/Documents/pacman.zig/zig-cache --global-cache-dir /Users/frarees/.cache/zig --name pacman --pkg-begin sokol /Users/frarees/Documents/pacman.zig/src/sokol/sokol.zig --pkg-end -framework Cocoa -framework AudioToolbox -framework QuartzCore -framework MetalKit -framework Metal --enable-cache 
    error: the following build command failed with exit code 1:
    /Users/frarees/Documents/pacman.zig/zig-cache/o/15f2ba0993916df07b056287db8a15ea/build /Users/frarees/Documents/zig/build/bin/zig /Users/frarees/Documents/pacman.zig /Users/frarees/Documents/pacman.zig/zig-cache /Users/frarees/.cache/zig run
    

    Seems like stdint.h is in /zig/build/lib/zig/libc/include/any-macos-any, and not in /zig/build/lib/zig/libc/include/x86_64-macos-gnu

    Looks like this might be an issue with zig, but wanted to report it to you here since this project is where I could repro.

    opened by frarees 2
  • question: what does UndefinedData do?

    question: what does UndefinedData do?

    Hello! I'm new to Zig and have been pouring through this repo (which is really great btw! it's complicated enough to be interesting, but presented in a very reader-friendly way. thank you!)

    I don't understand this piece of the code, and I'm hoping someone can explain it to me:

    // all mutable state is in a single nested global
    const State = struct {
        ...
        audio: struct {
            ...
            num_samples: u32 = 0,
            // sample_buffer is separate in UndefinedData
        } = .{},
        ...
    };
    var state: State = .{};
    
    // keep the big undefined data out of the state struct, mixing initialized
    // and uninitialized data bloats the executable size
    const UndefinedData = struct {
        tile_ram:       [DisplayTilesY][DisplayTilesX]u8 = undefined,
        color_ram:      [DisplayTilesY][DisplayTilesX]u8 = undefined,
        vertices:       [MaxVertices]Vertex = undefined,
        tile_pixels:    [TileTextureHeight][TileTextureWidth]u8 = undefined,
        color_palette:  [256]u32 = undefined,
        sample_buffer:  [NumSamples]f32 = undefined,
    };
    var data: UndefinedData = .{};
    

    Specifically, why is data separate from state? I tried making data a field in state to see what the difference would be:

    const State = struct {
        ...
        data: struct {
            tile_ram: [DisplayTilesY][DisplayTilesX]u8 = undefined,
            color_ram: [DisplayTilesY][DisplayTilesX]u8 = undefined,
            vertices: [MaxVertices]Vertex = undefined,
            tile_pixels: [TileTextureHeight][TileTextureWidth]u8 = undefined,
            color_palette: [256]u32 = undefined,
            sample_buffer: [NumSamples]f32 = undefined,
        } = .{},
    };
    var state: State = .{};
    
    $ zig version
    0.10.0-dev.423+40c9ce2ca
    
    # without any changes:
    $ zig build -Drelease-small && du zig-out/bin/pacman.exe
    122K    zig-out/bin/pacman.exe
    
    # moving data to be state.data, and making the necessary changes to the rest of the code:
    $ zig build -Drelease-small && du zig-out/bin/pacman.exe
    296K    zig-out/bin/pacman.exe
    

    keep the big undefined data out of the state struct, mixing initialized and uninitialized data bloats the executable size

    This is clearly true, but why? Why does mixing initialized and uninitialized data bloat the exe from 120K to 300K?

    I noticed this pattern is not present in pacman.c, so it's something about Zig in particular?

    opened by pancelor 1
  • Add cross-compilation support from any to macOS

    Add cross-compilation support from any to macOS

    When cross-compiling to macOS from Linux say, it is required to point to a valid sysroot via the --sysroot flag:

    zig build --sysroot /path/to/macos-sdk-sysroot/ -Dtarget=x86_64-macos
    zig build --sysroot /path/to/macos-sdk-sysroot/ -Dtarget=aarch64-macos
    
    opened by kubkon 1
  • zig-0.10.0 fixes.

    zig-0.10.0 fixes.

    • fix build script deprecations
    • update sokol-bindings
    • fixed function pointer syntax
    • changed asRange() param to pointer
    • change Vertex struct packed to extern
    • fix a whitespace problem in the playfield map (unrelated to Zig 0.10.0)
    opened by floooh 0
  • Reminder: build currently fails in recent Emscripten SDK versions

    Reminder: build currently fails in recent Emscripten SDK versions

    building:ERROR: Closure compiler run failed:
    
    building:ERROR: /var/folders/dz/g9ydwg8973z9nn5bvffcwf3h0000gn/T/emscripten_temp_musl3rsg/pacman.js.jso.js.jso.js.jso.js.jso.js:572:14: ERROR - [JSC_UNDEFINED_VARIABLE] variable allocateUTF8OnStack is undeclared
      572|    var cstr = allocateUTF8OnStack(pasted_str);
                         ^^^^^^^^^^^^^^^^^^^
    

    This is because of this change in the Emscripten SDK:

    https://groups.google.com/g/emscripten-discuss/c/GqyrjgRmcEw

    opened by floooh 0
  • added touch input scheme for ios

    added touch input scheme for ios

    Probably not the best possible way of implementing touch input support, but it does make the game playable on mobile.

    The input scheme is adaptive with regard to the direction that pacman is going. For example, going left creates the following touch input grid:

     __________
    |      |   |
    |  U   |   |
    |______| R |
    |      |   |
    |  D   |   |
    |______|___|
    

    Similarly every other direction produces a corresponding "adaptive" grid.

    Tested it briefly with a mouse and seemed reasonable, testing on a real device might be warranted.

    opened by kristoff-it 2
  • Errors when compiled and tried to execute from Windows

    Errors when compiled and tried to execute from Windows

    I simply cloned the project and ran zig build from the directory, I received no errors but when I tried to run the resulting executable, I received this error:

    Assertion failed: ((HRESULT)(hr) >= 0) && _sapp.d3d11.swap_chain && _sapp.d3d11.device && _sapp.d3d11.device_context, file C:\prj\pacman.zig\src\sokol/sokol_app.h, line 5432

    Zig version 0.8.0, Windows version 10.0.18362.30 x86-64

    opened by HugoFlorentino 2
Owner
Andre Weissflog
Andre Weissflog
A simple wrapper for 'pacman' with a syntax similar to 'apt' to help people transitioning to Arch and Arch based distributions like Manjaro.

aptpac aptpac is a program which helps with the transition to Arch Linux and Arch based distros like Manjaro. It simplifies using pacman as it works l

Itai Nelken 18 Dec 4, 2022
PACMAN for Raspberry Pi Pico (QVGA LCD)

PACMAN for Raspberry Pi Pico ラズベリーPi PicoにQVGAの液晶を接続して動作する本格的なパックマンです。 小型スピーカーを接続して音声も鳴らせます。 とてもシンプルな回路なので、簡単に製作することができます。 液晶は240x320ドット、コントローラにILI934

null 38 Dec 28, 2022
3D Pacman with C++ and OpenGL

Pacman 3D 3D version of my Homemade Pacman, made with C++ and OpenGL Dependencies ASSIMP (to load the 3D models) and SFML (to play sounds) are needed

Lilian Mallardeau 2 Sep 1, 2022
A KC85 emulator written in Zig

A simple KC85/2, /3 and /4 emulator for Windows, macOS and Linux, written in Zig. Uses the sokol headers for platform abstraction. Read more about the

Andre Weissflog 34 Dec 30, 2022
PAC-MAN emulator written in Zig

zig-pacman A PAC-MAN emulator written in Zig to showcase the zig80 library. Requires Zig, zigmod, and SDL2. Usage usage: pacman [-htda] [-c <ratio>] [

Matthew 3 Jan 24, 2022
Simple Tetris clone written in C++ using SFML

stetris Simple Tetris clone written in C++ using SFML. The game has been tested only on GNU/Linux system so far. Building dependencies (on Linux) g++

sewe2000 3 Jun 14, 2022
Zig bindings for the excellent CRoaring library

Zig-Roaring This library implements Zig bindings for the CRoaring library. Naming Any C function that begins with roaring_bitmap_ is a method of the B

Justin Whear 15 Dec 13, 2022
C, C++, D, Fortran, Rust and Zig for your z80

z80 Babel: C, C++, D, Fortran, Rust and Zig for your z80 This is a proof of concept for a multi-language pipeline for the Z80 CPU, and its correspondi

Manuel Martinez Torres 15 Dec 23, 2022
A Sol-inspired minimalist Lua binding for Zig.

zoltan A Sol-inspired minimalist Lua binding for Zig. Features Supports Zig 0.9.0 Lua tables table creation from Zig get/set/create methods possible k

null 79 Dec 4, 2022
A pong clone written in C++ using Raylib

How To Play Objective: first player to reach 10 points is the winner! PLAYER 1: W: up S: down PLAYER 2: ARROW UP or I: up ARROW DOWN or S: down Requir

Victor Sarkisov 1 Dec 15, 2021
Asteroids Clone for Windows

Asteroids Clone for Windows This game is a simple Asteroids clone primarily intended to demonstrate the capabilities and flexibility of w64devkit. It

Christopher Wellons 24 Dec 25, 2022
A Rocket League clone designed for the GameBoy Pocket

gb-pocket-league A work-in-progress port of Rocket League to the GameBoy (Pocket)! What a save! What a save! What a save! (README disabled for 3 seco

Tyler Porter 16 Nov 22, 2022
Rat is a very simplified cat clone that includes only stdio library

rat - simplified cat clone. Rat is a very simplified cat clone that includes only stdio library. It's currently much slower than cat because it doesnt

r[0x30]tthin 1 Nov 8, 2021
A ShaderToy Clone for O3DE (Gem + Demo Project)

A ShaderToy Clone for O3DE (Gem + Demo Project)

null 7 Aug 8, 2022
A clone of the puzzle game Lights Out for the 68k Macintosh.

MacLO MacLO is a clone of the puzzle game Lights Out for the 68k Macintosh. It is a port of ArduLO for the Arduboy. MacLO is B&W and has been tested o

Jon Thysell 13 Oct 9, 2022
Space invaders clone in C++

memeinvaders Space invaders clone in C++ To build: Install SDL2 (and possibly OpenGL): https://wiki.libsdl.org/Installation Install CMake: https://cma

null 2 Jan 8, 2022
Neo - Simulates the digital rain from "The Matrix" (cmatrix clone with 32-bit color and Unicode support)

neo WARNING: neo may cause discomfort and seizures in people with photosensitive epilepsy. User discretion is advised. neo recreates the digital rain

Stew Reive 470 Dec 28, 2022
🇧🇬 Wordle clone in Bulgarian

wordle-bg A Wordle clone in Bulgarian Play online: https://wordle-bg.ggerganov.com Description This clone is written in C++ and ported to the Web with

Georgi Gerganov 23 Jan 3, 2023
A repository containing our learnings and implementations for the project "Anchor: The Docker Clone" under IEEE-NITK

Anchor: The Docker Clone A repository containing our learnings and implementations for the project "Anchor: The Docker Clone" under IEEE-NITK Currentl

Rakshita Varadarajan 1 Feb 7, 2022