Skip to content

Pixel Display

Hapaxia edited this page Apr 11, 2020 · 1 revision

Introduction

A pixel display class that allows accessing direct 'pixel' access of a resizeable 'display' object.

It is designed as a simple approach to emulating more 'retro' style pixel displays.

This is a simplified 'tilemap-style' class that allows you to draw a grid - or 'mosaic' - of squares/rectangles. The biggest difference between this and a tile map is that a normal tile map uses textures but this class uses colours instead. The entire object is the 'display' and the rectangles are the 'pixels'.

You may notice some similarities to Console Screen. A similar effect can be achieved using only the background cells of Console Screen. However, Console Screen is significantly larger and is also designed with the foreground cells to be the most important while the background cells can be completely avoided. This class was written from scratch and, while retaining the most important/useful Console Screen features, has more specific features tailored towards its usage.

Note: the 'colours' stored for each 'pixel' are unsigned integer that actually palette indices. Each index represents an actual colour stored in the palette.

Usage

Declaration

  • sw::PixelDisplay pixelDisplay;
    creates a Pixel Display

Drawing

This class inherits from sf::Drawable so it is drawn in the same way as all SFML drawables:
window.draw(pixelDisplay);
where window is an sf::RenderWindow.

Note: actually, window could be any sf::RenderTarget.

Transformations

This class inherits from sf::Transformable so it has all the usual SFML transformations available.

Manipulation

Visual Representation

Pixel Display updates itself automatically whenever something changes.

  • setSize()
    sets the size of the Pixel Display.

  • setResolution()
    sets the resolution of the Pixel Display. This is the number of pixels contained within the display (the 'grid size') and is independant from the display size.

Pixels

This is the actual content within the display - the 'pixels'.
The index of a pixel is an unsigned integer that starts from zero at top-left, increase across the row, then continues on each row downwards until the final index at bottom-right.

All pixels are stored as a single vector/array of palette indices.

  • 'setPixel(index, color)'
    sets the pixel at index to the colour in the palette. Both parameters are unsigned integers.

  • 'setPixels(colors, numberOfColors, startIndex)'
    sets a number of consecutive pixels at once from the colors array (with size of numberOfColors) starting from the pixel index at startIndex.

  • 'fill(color)'
    fills the entire display with the colour specified. This sets every pixel to the same colour.

  • 'clear()'
    clears the display, filling it with colour zero. Equivalent to 'fill(0u)'.

  • 'randomize()'
    sets all pixels to random colours from the palette.

Shifting/Scrolling

This 'moves' (groups of) pixels around the display. A rectangular area is specified using an sf::IntRect

  • 'shiftLeft(amount, startIndex, numberOfPixels)'
    shifts the stored pixel vector/array to the left (towards index zero). It is shifted by amount number of pixels; this is defaulted to 1 if omitted. To specify a range to shift instead of the entire display, startIndex is the index of the first index and numberOfPixels is the length/size of that range of pixels. If numberOfPixels is zero, the range extends to the end of the vector/array. Note that pixels are wrapped; this means amount number of pixels are removed from the beginning of the range and then added to the end of it.

  • 'shiftRight(amount, startIndex, numberOfPixels)'
    shifts the stored pixel vector/array to the right (away from index zero). It is shifted by amount number of pixels; this is defaulted to 1 if omitted. To specify a range to shift instead of the entire display, startIndex is the index of the first index and numberOfPixels is the length/size of that range of pixels. If numberOfPixels is zero, the range extends to the end of the vector/array. Note that pixels are wrapped; this means amount number of pixels are removed from the end of the range and then added in front of it.

  • 'scrollWrapUp(amount, rect)'
    scrolls the area of pixels contained in rect upwards by amount pixels. This wraps the area so the pixels in the amount rows at the top of rect are placed under the rest of the scrolled area.

  • 'scrollWrapDown(amount, rect)'
    scrolls the area of pixels contained in rect downwards by amount pixels. This wraps the area so the pixels in the amount rows at the bottom of rect are placed above the rest of the scrolled area.

  • 'scrollWrapLeft(amount, rect)'
    scrolls the area of pixels contained in rect to the left by amount pixels. This wraps the area so the pixels in the amount columns on the left of rect are placed on the right of the scrolled area.

  • 'scrollWrapRight(amount, rect)'
    scrolls the area of pixels contained in rect to the right by amount pixels. This wraps the area so the pixels in the amount columns on the right of rect are placed on the left of the scrolled area.

  • 'scrollUp(color, amount, rect)'
    scrolls the area of pixels contained in rect upwards by amount pixels. All pixels 'pushed' outside of this area are lost. The bottom rows in the rectangle of pixels are filled with color.

  • 'scrollDown(color, amount, rect)'
    scrolls the area of pixels contained in rect downwards by amount pixels. All pixels 'pushed' outside of this area are lost. The top rows in the rectangle of pixels are filled with color.

  • 'scrollLeft(color, amount, rect)'
    scrolls the area of pixels contained in rect to the left by amount pixels. All pixels 'pushed' outside of this area are lost. The rightmost columns in the rectangle of pixels are filled with color.

  • 'scrollRight(color, amount, rect)'
    scrolls the area of pixels contained in rect to the right by amount pixels. All pixels 'pushed' outside of this area are lost. The leftmost columns in the rectangle of pixels are filled with color.

Palette

  • 'setPalette(palette)'
    sets the palette to match palette, which is an std::vector of sf::Colors.

  • 'setPaletteSize(numberOfColors)'
    sets the palette size. size is an unsigned integer.

  • 'setColor(color, rgb)'
    sets the colour in the palette (at color index) to rgb, which is an sf::Color.

  • 'removeColor(color)'
    removes the colour from the palette (at color index). All following palette colours' indices will decrease by one.

  • 'addRgb(rgb)'
    adds a new colour to the end of the palette, increasing the palette size by one. rgb is an sf::Color.

  • 'cyclePaletteDown(amount, firstColor, numberOfColors)'
    shifts the colours in the palette downwards (towards index zero) by amount steps. amount defaults to 1. To specify a range to shift instead of the entire palette, firstColor is the index of the first colour and numberOfColors is the length/size of that range of colours. firstColor and numberOfColors both default to zero. If numberOfColors is zero, the range extends to the end of the palette. Note that colours are wrapped; this means amount number of colours are removed from the beginning of the range and then added to the end of it.

  • 'cyclePaletteUp(amount, firstColor, numberOfColors)'
    shifts the colours in the palette upwards (away from index zero) by amount steps. amount defaults to 1. To specify a range to shift instead of the entire palette, firstColor is the index of the first colour and numberOfColors is the length/size of that range of colours. firstColor and numberOfColors both default to zero. If numberOfColors is zero, the range extends to the end of the palette. Note that colours are wrapped; this means amount number of colours are removed from the end of the range and then added to the beginning of it.

Buffers

Buffers are rectangular regions of pixel data that are not used to draw the display until pasted onto the display.

  • 'unsigned int copy()'
    copies the entire pixel display onto a new buffer of the same size. Returns an unsigned int representing the index of the newly created buffer.

  • 'unsigned int copy(selectionRectangle)'
    copies the pixels in the rectangular region specified by selectionRectangle from the display onto a new buffer of the size specified by selectionRectangle, which is an sf::IntRect. Returns an unsigned int representing the index of the newly created buffer.

  • 'paste(offset)'
    pastes the last added buffer (the final one stored) onto the display. It is pasted with its top-left corner offset from the display's top-left corner by offset, an sf::Vector2i. If offset is omitted, it is pasted at the top-left corner. Note that if the buffer is smaller than the display, some pixels will not be changed. Also, if the buffer is larger than the display - or otherwise would be displayed past the boundaries of the screen in any direction - those pixels will simply be lost (in the paste; the buffer remains unaffected).

  • 'remove()'
    removes the last added buffer, reducing the number of buffers by one.

  • 'copy(index)'
    copies the entire pixel display over an already existing buffer. This replaces the buffer at specified index.

  • 'copy(index, selectionRectangle)'
    copies the pixels in the rectangular region specified by selectionRectangle from the display over an already existing buffer of the size specified by selectionRectangle, which is an sf::IntRect. This replaces the buffer at specified index.

  • 'paste(index, offset)'
    pastes the buffer at specified index onto the display. It is pasted with its top-left corner offset from the display's top-left corner by offset, an sf::Vector2i. If offset is omitted, it is pasted at the top-left corner. Note that if the buffer is smaller than the display, some pixels will not be changed. Also, if the buffer is larger than the display - or otherwise would be displayed past the boundaries of the screen in any direction - those pixels will simply be lost (in the paste; the buffer remains unaffected).

  • 'removedBuffer(index)'
    removes the buffer at specified index, reducing the number of buffers by one. All following buffers' indices will decrease by one.

  • 'removeAllBuffers()'
    removes all buffers from storage.

  • 'unsigned int addBuffer(size)'
    adds a new buffer of the specified size. size is an sf::Vector2u that defaults to 1x1 if omitted. Returns an unsigned int representing the index of the newly created buffer. This allows pre-creation of buffers, ready to copy into instead of having to create new ones at time of copy, resulting in resizing of storage vector.

  • 'resizeBuffer(index, size)'
    resizes the buffer at the specified index. size is an sf::Vector2u that cannot be omitted.

Information

Value Conversion

  • 'getIndex(location)'
    returns an unsigned int representing the index of the pixel located at the specified 2D location of the display. location is an sf::Vector2u.

Visual Representation

  • getSize()
    returns an sf::Vector2f representing the size of the entire display.

  • getResolution()
    returns an sf::Vector2u representing the resolution of the display. This is the number of columns and rows of pixels.

Pixels

  • 'getPixels(colors, numberOfColors, startIndex)'
    copies the pixel colours (palette indices) from the display to the colors array. startIndex is the index of the first pixel and numberOfColors is the size of the colors array.

Palette

  • 'getPaletteSize()'
    returns an unsigned integer representing the size of the palette - the number of colours currently stored in the palette.

  • 'getRgb(color)'
    returns the RGB color (sf::Color) of the colour in the palette at index color.

Buffers

  • 'getNumberOfBuffers()'
    returns an unsigned int representing the number of buffers currently in storage.

  • 'getSizeOfBuffer(index)'
    returns an sf::Vector2u representing the 2D size of the buffer at the specified index.

(PixelDisplay v1.0)