Package net.mtrop.doom.graphics
Class Flat
java.lang.Object
net.mtrop.doom.graphics.Flat
- All Implemented Interfaces:
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
-
Nested Class Summary
Nested classes/interfaces inherited from interface net.mtrop.doom.object.BinaryObject
BinaryObject.InlineScanner<BO extends BinaryObject>, BinaryObject.Reflect, BinaryObject.Scanner<BO extends BinaryObject>, BinaryObject.Shared, BinaryObject.Transformer<BO extends BinaryObject>
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
Clears the pixel data to zeroes.static Flat
create
(int width, int height, byte[] bytes) Reads and creates a new Flat object from an array of bytes.int
int
int
int
getPixel
(int x, int y) Gets the pixel data at a location in the flat.int
getWidth()
static Flat
read
(int width, int height, InputStream in) Reads and creates a new Flat from anInputStream
implementation.void
readBytes
(InputStream in) Reads from anInputStream
and sets this object's fields.void
setDimensions
(int width, int height) Sets the dimensions of this flat.void
setPixel
(int x, int y, int value) Sets the pixel data at a location in the flat.void
writeBytes
(OutputStream out) Writes this object to anOutputStream
.
-
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
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
Reads and creates a new Flat from anInputStream
implementation. This reads from the stream until enough bytes for the fullFlat
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 openInputStream
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 interfaceGraphicObject
- Returns:
- the offset from the center, horizontally, in pixels.
-
getOffsetY
public int getOffsetY()- Specified by:
getOffsetY
in interfaceGraphicObject
- Returns:
- the offset from the center, vertically, in pixels.
-
getWidth
public int getWidth()- Specified by:
getWidth
in interfaceGraphicObject
- Returns:
- the width of this graphic in pixels.
-
getHeight
public int getHeight()- Specified by:
getHeight
in interfaceGraphicObject
- 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 interfaceGraphicObject
- 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 interfaceGraphicObject
- 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
Description copied from interface:BinaryObject
Reads from anInputStream
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 interfaceBinaryObject
- Parameters:
in
- theInputStream
to read from.- Throws:
IOException
- if a read error occurs.
-
writeBytes
Description copied from interface:BinaryObject
Writes this object to anOutputStream
.- Specified by:
writeBytes
in interfaceBinaryObject
- Parameters:
out
- theOutputStream
to write to.- Throws:
IOException
- if a write error occurs.
-