Skip to content

olc::Sprite

Javidx9 edited this page Feb 13, 2019 · 3 revisions

A class that represents a 2D array of olc::Pixel in the form of an image. As Pixels can be transparent, these images can be used as Sprites in games. Fundamentally the PixelGameEngine uses Portable Network Graphics images (.png files) as its primary source of artwork.

Sprites can also be created without any content, i.e. they are just blank images of a defined width and height. These can be used as targets for all of the PixelGameEngine draw calls. This means you can render things to off-screen Sprite buffers for clever rendering effects. The PixelGameEngine will create a default Sprite of the dimensions of the desired screen size, this sprite is used as the primary screen buffer.

Sprites are defined with top left being (0,0) and bottom right being (width, height). Once a sprite is created it cannot be resized. It is recommended to create sprites on the heap to ensure the required libraries are loaded to handle PNG image formats.

NOTE! Currently on Windows olc::Sprites cannot be constructed in the constructor of any olc::PixelGameEngine derived class. The GDI Subsystem for loading PNG files needs to start before sprites can be loaded, so its important to only load sprites during or after OnUserCreate(). This restriction does not apply to other OS, but keep it in mind if you want your program to remain cross platform.

Fields:

int32_t olc::Sprite::width

Width of Sprite in olc::Pixels

int32_t olc::Sprite::height

Height of Sprite in olc::Pixels

Methods:

olc::Sprite::Sprite()

Default constructor

olc::Sprite::Sprite(std::string sImageFile)

Constructor that loads a Portable Network Graphics file specified by sImageFile. If the image file is invalid the sprite is dimensionless.

olc::Sprite::Sprite(int32_t w, int32_t h)

Constructor that creates a blank Sprite with w by h olc::Pixels, defaulted to olc::BLACK

olc::Sprite::~Sprite()

Default destructor clears memory allocated by the Sprite's constructor

olc::rcode olc::Sprite::LoadFromFile(std::string sImageFile)

Loads a Portable Network Graphics file specified by sImageFile into the Sprite. If the image file is invalid the sprite is cleared and olc::FAIL is returned. If the Sprite already contained data it is erased. On a successful load, this function returns olc::OK

olc::Pixel olc::Sprite::GetPixel(int32_t x, int32_t y)

Get a copy of the olc::Pixel at location (x, y) within the Sprite. If the location lies outside of the boundary of the sprite, a olc::BLANK Pixel is returned

void olc::Sprite::SetPixel(int32_t x, int32_t y, Pixel p)

Replaces the olc::Pixel at location (x, y) with that specified by p. If the location lies outside of the boundary of the sprite, no pixels are set

olc::Pixel olc::Sprite::Sample(float x, float y)

Returns an equivalent pixel for the location specified by the normalised coordinate (x, y). The sample location must lie within the boundary of 0 >= x < 1.0f and 0 >= y < 1.0f. No sampling filter is applied, following a nearest neighbour protocol

olc::Pixel* olc::Sprite::GetData()

Returns a raw pointer to the memory buffer underlying the Sprite