Git Product home page Git Product logo

mmsprite's Introduction

Magic & Mayhem Sprite Reader

A simple JavaScript plugin to read image data from sprite files used in the video game Magic & Mayhem, aka Duel: The Mage Wars.

This is a JavaScript implementation of a C routine posted on the OpenXcom forums. These are included in the folder "Original". With thanks to user Nikita_Sadkov, the author, for sharing his work and findings.

Be sure to check out the map formats writeup if you're interested in how maps are structured.

How to Use

After including the JavaScript file in a page, pass an ArrayBuffer of the sprite file to the global MMSprite function. A minimal example is shown below:

<input type="file" id="file-input" />

<script src="./mm-reader.js"></script>
<script>
    document.getElementById("file-input").addEventListener("input", function() {
        for (const file of this.files) {
            const fileReader = new FileReader();

            fileReader.onload = function() {
                // Get a sprite reader instance
                const reader = new MMReader();

                // Read sprite data
                const sprite = reader.readSprite(this.result);

                // Unpack frame data
                for (let i = 0; i < sprite.frames.length; i++) {
                    const frame = sprite.frames[i];

                    // Render frame to canvas
                    const canvas = sprite.frameToCanvas(i);

                    // Do something with the canvas
                    document.getElementById("canvas-container").appendChild(canvas);
                }
            }

            fileReader.readAsArrayBuffer(file);
        }
    })
</script>

Properties

sprite.header : Object

The sprite file header.

// RedCap.spr
{
    "id"        : "SPR\u0000",
    "size"      : 528540,
    "unknown_1" : 4,
    "frames"    : 350,
    "palettes"  : 1,
    "unknown_2" : 1
}

sprite.palettes : Array

All colour palettes used in the file. palettes is an array containing arrays of RGB pixel value objects.

// RedCap.spr

// sprite.palettes[0][0]
{
    "r": 0,
    "g": 0,
    "b": 0
}

// sprite.palettes[0][1]
{
    "r": 191,
    "g": 0,
    "b": 0
}

sprite.frames : Array

Frame metadata. A frame object contains the following properties:

Name Type Description
begin_offset Number Offset in file.
size Number Frame size, in bytes.
width Number Frame width, in pixels.
height Number Frame height, in pixels.
centre_x Number Frame centre, X axis.
centre_y Number Frame centre, Y axis.
name String Frame name.
palette_index Number Frame colour palette index.
unknown_1 Number Unknown meaning. Only present if file header's unknown_1 value is 2.
unknown_2 Number Unknown meaning. Only present if file header's unknown_1 value is 2.
delta_offsets Array Delta value offsets. Use for reading pixel data.
pixel_offsets Array As above.

Methods

sprite.frameToCanvas(int frameIndex) returns Canvas

Renders a sprite's frame object to a Canvas element. Frames are specified by index. See example in "How to Use" section.

Notes

Certain sprite files ostensibly have no colour palette. timer.spr, for example, has a palette value of 0. Currently the plugin will generate a greyscale palette when encountering such values.

Inspecting the file in a hex editor, there does seem to be a palette; however, in the case of timer.spr the total number of pixels appears to be 384 instead of the usual 768 (3 RGB pixels ร— 16 ร— 16).

Extracting such palettes is yet to be implemented.

mmsprite's People

Contributors

sapphire-bt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.