Class Flat

java.lang.Object
net.mtrop.doom.graphics.Flat
All Implemented Interfaces:
BinaryObject, GraphicObject

public class Flat extends Object implements BinaryObject, GraphicObject
Doom graphic data that has no header data for its dimensions/offsets.

Normally, flats are the floor/ceiling textures in the Doom engine that are a set size (64x64) and thus have no need for header information, but fullscreen pictures like Doom's TITLEPIC are also a straight mapping of pixels with assumed dimensions (in this case, 320x200). This class can read both, and its dimensions can be arbitrarily set by the programmer regardless of the amount of data inside.

NOTE: The readBytes(InputStream) method will only read as many bytes as possible to fill the current dimensions of the flat, as this information is not found in the byte data.

Author:
Matthew Tropiano
  • Constructor Details

    • Flat

      public Flat(int width, int height)
      Creates a new flat.
      Parameters:
      width - the width of the flat in pixels. Must be greater than 1.
      height - the height of the flat in pixels. Must be greater than 1.
  • Method Details

    • create

      public static Flat create(int width, int height, byte[] bytes) throws IOException
      Reads and creates a new Flat object from an array of bytes. This reads until it reaches the end of the entry list.
      Parameters:
      width - the width of the flat in pixels. Must be greater than 1.
      height - the height of the flat in pixels. Must be greater than 1.
      bytes - the byte array to read.
      Returns:
      a new Switches object.
      Throws:
      IOException - if the stream cannot be read.
    • read

      public static Flat read(int width, int height, InputStream in) throws IOException
      Reads and creates a new Flat from an InputStream implementation. This reads from the stream until enough bytes for the full Flat are read. The stream is NOT closed at the end.
      Parameters:
      width - the width of the flat in pixels. Must be greater than 1.
      height - the height of the flat in pixels. Must be greater than 1.
      in - the open InputStream to read from.
      Returns:
      a new Flat with its fields set.
      Throws:
      IOException - if the stream cannot be read.
    • getOffsetX

      public int getOffsetX()
      Specified by:
      getOffsetX in interface GraphicObject
      Returns:
      the offset from the center, horizontally, in pixels.
    • getOffsetY

      public int getOffsetY()
      Specified by:
      getOffsetY in interface GraphicObject
      Returns:
      the offset from the center, vertically, in pixels.
    • getWidth

      public int getWidth()
      Specified by:
      getWidth in interface GraphicObject
      Returns:
      the width of this graphic in pixels.
    • getHeight

      public int getHeight()
      Specified by:
      getHeight in interface GraphicObject
      Returns:
      the height of this graphic in pixels.
    • clear

      public void clear()
      Clears the pixel data to zeroes.
    • setDimensions

      public void setDimensions(int width, int height)
      Sets the dimensions of this flat. WARNING: This will clear all of the data in the flat.
      Parameters:
      width - the width of the flat in pixels.
      height - the height of the flat in pixels.
    • setPixel

      public void setPixel(int x, int y, int value)
      Sets the pixel data at a location in the flat. Valid values are in the range of 0 to 255, with 0 to 255 being palette indexes.
      Specified by:
      setPixel in interface GraphicObject
      Parameters:
      x - patch x-coordinate.
      y - patch y-coordinate.
      value - the value to set.
      Throws:
      IllegalArgumentException - if the value of the pixel is outside the range 0 to 255.
      ArrayIndexOutOfBoundsException - if the provided coordinates is outside the graphic.
    • getPixel

      public int getPixel(int x, int y)
      Gets the pixel data at a location in the flat.
      Specified by:
      getPixel in interface GraphicObject
      Parameters:
      x - flat x-coordinate.
      y - flat y-coordinate.
      Returns:
      a palette index value from 0 to 255.
      Throws:
      ArrayIndexOutOfBoundsException - if the provided coordinates is outside the graphic.
    • readBytes

      public void readBytes(InputStream in) throws IOException
      Description copied from interface: BinaryObject
      Reads from an InputStream and sets this object's fields. Only reads the amount of bytes that it takes to read a single instance of the object. Note that not every object may have a consistent length!
      Specified by:
      readBytes in interface BinaryObject
      Parameters:
      in - the InputStream to read from.
      Throws:
      IOException - if a read error occurs.
    • writeBytes

      public void writeBytes(OutputStream out) throws IOException
      Description copied from interface: BinaryObject
      Writes this object to an OutputStream.
      Specified by:
      writeBytes in interface BinaryObject
      Parameters:
      out - the OutputStream to write to.
      Throws:
      IOException - if a write error occurs.