ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
asw::lighting::TileLight Class Reference

Light that spreads across a grid of tiles and stops at solid tiles. More...

#include <lighting.h>

Classes

struct  Source
 

Public Member Functions

 TileLight (int width, int height, float tile_size)
 Create a grid with no solid tiles and no lights.
 
void set_solid (int x, int y, bool solid)
 Set whether a tile blocks light. A solid tile is lit, but does not pass light on.
 
bool is_solid (int x, int y) const
 Get whether a tile blocks light. Tiles off the grid are solid.
 
void set_falloff (float amount)
 Set how much light is lost per tile, from 0 to 1.
 
void clear_lights ()
 Remove all lights.
 
void add_light (int x, int y, const asw::Color &color)
 Add a light at a tile.
 
void compute ()
 Spread the lights and update the texture. Call after lights or solid tiles change.
 
asw::Color get (int x, int y) const
 Get the light at a tile after compute().
 
const asw::Texture & get_texture () const
 Get the texture, with one pixel per tile. Empty before compute().
 
asw::Vec2< float > get_size () const
 Get the size of the grid in pixels.
 

Private Member Functions

bool in_grid (int x, int y) const
 
void upload ()
 

Private Attributes

int width
 
int height
 
float tile_size
 
float falloff { 0.1F }
 
std::vector< uint8_t > solid
 
std::vector< Source > sources
 
std::vector< float > light
 
asw::Texture texture
 
std::vector< float > level
 
std::vector< std::size_t > open
 
std::vector< uint8_t > pixels
 

Detailed Description

Light that spreads across a grid of tiles and stops at solid tiles.

Lighting is worked out on the CPU in compute(), then kept in a texture with one pixel per tile. The texture is drawn with smooth scaling, so light blends between tiles. Add it to a LightMap with add_tiles().

Definition at line 121 of file lighting.h.

Constructor & Destructor Documentation

◆ TileLight()

asw::lighting::TileLight::TileLight ( int width,
int height,
float tile_size )

Create a grid with no solid tiles and no lights.

Parameters
widthWidth in tiles.
heightHeight in tiles.
tile_sizeSize of a tile in pixels.

Definition at line 169 of file lighting.cpp.

Member Function Documentation

◆ add_light()

void asw::lighting::TileLight::add_light ( int x,
int y,
const asw::Color & color )

Add a light at a tile.

Parameters
xTile x.
yTile y.
colorColour at the tile. It fades as it spreads.

Definition at line 206 of file lighting.cpp.

◆ clear_lights()

void asw::lighting::TileLight::clear_lights ( )

Remove all lights.

Definition at line 201 of file lighting.cpp.

◆ compute()

void asw::lighting::TileLight::compute ( )

Spread the lights and update the texture. Call after lights or solid tiles change.

Definition at line 213 of file lighting.cpp.

◆ get()

asw::Color asw::lighting::TileLight::get ( int x,
int y ) const

Get the light at a tile after compute().

Definition at line 318 of file lighting.cpp.

◆ get_size()

asw::Vec2< float > asw::lighting::TileLight::get_size ( ) const

Get the size of the grid in pixels.

Definition at line 333 of file lighting.cpp.

◆ get_texture()

const asw::Texture & asw::lighting::TileLight::get_texture ( ) const

Get the texture, with one pixel per tile. Empty before compute().

Definition at line 328 of file lighting.cpp.

◆ in_grid()

bool asw::lighting::TileLight::in_grid ( int x,
int y ) const
private

Definition at line 179 of file lighting.cpp.

◆ is_solid()

bool asw::lighting::TileLight::is_solid ( int x,
int y ) const

Get whether a tile blocks light. Tiles off the grid are solid.

Definition at line 191 of file lighting.cpp.

◆ set_falloff()

void asw::lighting::TileLight::set_falloff ( float amount)

Set how much light is lost per tile, from 0 to 1.

Definition at line 196 of file lighting.cpp.

◆ set_solid()

void asw::lighting::TileLight::set_solid ( int x,
int y,
bool solid )

Set whether a tile blocks light. A solid tile is lit, but does not pass light on.

Definition at line 184 of file lighting.cpp.

◆ upload()

void asw::lighting::TileLight::upload ( )
private

Definition at line 284 of file lighting.cpp.

Member Data Documentation

◆ falloff

float asw::lighting::TileLight::falloff { 0.1F }
private

Definition at line 186 of file lighting.h.

◆ height

int asw::lighting::TileLight::height
private

Definition at line 184 of file lighting.h.

◆ level

std::vector<float> asw::lighting::TileLight::level
private

Definition at line 193 of file lighting.h.

◆ light

std::vector<float> asw::lighting::TileLight::light
private

Definition at line 189 of file lighting.h.

◆ open

std::vector<std::size_t> asw::lighting::TileLight::open
private

Definition at line 194 of file lighting.h.

◆ pixels

std::vector<uint8_t> asw::lighting::TileLight::pixels
private

Definition at line 195 of file lighting.h.

◆ solid

std::vector<uint8_t> asw::lighting::TileLight::solid
private

Definition at line 187 of file lighting.h.

◆ sources

std::vector<Source> asw::lighting::TileLight::sources
private

Definition at line 188 of file lighting.h.

◆ texture

asw::Texture asw::lighting::TileLight::texture
private

Definition at line 190 of file lighting.h.

◆ tile_size

float asw::lighting::TileLight::tile_size
private

Definition at line 185 of file lighting.h.

◆ width

int asw::lighting::TileLight::width
private

Definition at line 183 of file lighting.h.


The documentation for this class was generated from the following files: