OpenClonk
C4FoWLight Class Reference

#include <C4FoWLight.h>

Public Member Functions

 C4FoWLight (C4Object *pObj)
 
 ~C4FoWLight ()
 
int32_t getX () const
 
int32_t getY () const
 
int32_t getReach () const
 
int32_t getFadeout () const
 
int32_t getTotalReach () const
 
int32_t getSize () const
 
int32_t getNormalSize () const
 
float getBrightness () const
 
float getR () const
 
float getG () const
 
float getB () const
 
float getValue () const
 
float getLightness () const
 
C4FoWLightgetNext () const
 
C4ObjectgetObj () const
 
void SetReach (int32_t iReach, int32_t iFadeout)
 
void SetColor (uint32_t iValue)
 
void Invalidate (C4Rect r)
 
void Update (C4Rect r)
 
void Render (class C4FoWRegion *pRegion, const C4TargetFacet *pOnScreen, C4ShaderCall &call)
 
bool IsVisibleForPlayer (C4Player *player) const
 

Friends

class C4FoW
 

Detailed Description

This class represents one light source. A light source has an associated object with which the light source moves and one light section that handles the light beams for each direction (up, down, left, right).

Furthermore, each light source has a size. This is usually the object's lightRange. See SetReach.

Definition at line 34 of file C4FoWLight.h.

Constructor & Destructor Documentation

◆ C4FoWLight()

C4FoWLight::C4FoWLight ( C4Object pObj)

Definition at line 29 of file C4FoWLight.cpp.

30  : iX(fixtoi(pObj->fix_x)),
31  iY(fixtoi(pObj->fix_y)),
32  iReach(pObj->lightRange),
33  iFadeout(pObj->lightFadeoutRange),
34  iSize(20), gBright(0.5), colorR(1.0), colorG(1.0), colorB(1.0),
35  colorV(1.0), colorL(1.0),
36  pNext(nullptr),
37  pObj(pObj),
38  sections(4)
39 {
40  sections[0] = new C4FoWLightSection(this,0);
41  sections[1] = new C4FoWLightSection(this,90);
42  sections[2] = new C4FoWLightSection(this,180);
43  sections[3] = new C4FoWLightSection(this,270);
44 }
int fixtoi(const C4Fixed &x)
Definition: C4Real.h:259
int32_t lightRange
Definition: C4Object.h:120
C4Real fix_y
Definition: C4Object.h:123
C4Real fix_x
Definition: C4Object.h:123
int32_t lightFadeoutRange
Definition: C4Object.h:121

◆ ~C4FoWLight()

C4FoWLight::~C4FoWLight ( )

Definition at line 46 of file C4FoWLight.cpp.

47 {
48  for(auto & section : sections)
49  delete section;
50 }

Member Function Documentation

◆ getB()

float C4FoWLight::getB ( ) const
inline

Definition at line 68 of file C4FoWLight.h.

68 { return colorB; }

◆ getBrightness()

float C4FoWLight::getBrightness ( ) const
inline

Definition at line 65 of file C4FoWLight.h.

65 { return colorV * gBright; }

◆ getFadeout()

int32_t C4FoWLight::getFadeout ( ) const
inline

Definition at line 61 of file C4FoWLight.h.

61 { return iFadeout; }

◆ getG()

float C4FoWLight::getG ( ) const
inline

Definition at line 67 of file C4FoWLight.h.

67 { return colorG; }

◆ getLightness()

float C4FoWLight::getLightness ( ) const
inline

Definition at line 70 of file C4FoWLight.h.

70 { return colorL; }

◆ getNext()

C4FoWLight* C4FoWLight::getNext ( ) const
inline

Definition at line 71 of file C4FoWLight.h.

71 { return pNext; }

Referenced by C4FoW::Add(), C4FoW::ClearDeletedLights(), C4FoW::Invalidate(), C4FoW::Remove(), C4FoW::Render(), and C4FoW::Update().

Here is the caller graph for this function:

◆ getNormalSize()

int32_t C4FoWLight::getNormalSize ( ) const
inline

Definition at line 64 of file C4FoWLight.h.

64 { return iSize * 2; }

References iSize.

◆ getObj()

C4Object* C4FoWLight::getObj ( ) const
inline

Definition at line 72 of file C4FoWLight.h.

72 { return pObj; }

Referenced by C4FoW::Add().

Here is the caller graph for this function:

◆ getR()

float C4FoWLight::getR ( ) const
inline

Definition at line 66 of file C4FoWLight.h.

66 { return colorR; }

◆ getReach()

int32_t C4FoWLight::getReach ( ) const
inline

Definition at line 60 of file C4FoWLight.h.

60 { return iReach; }

Referenced by C4FoWLightSection::Update().

Here is the caller graph for this function:

◆ getSize()

int32_t C4FoWLight::getSize ( ) const
inline

Definition at line 63 of file C4FoWLight.h.

63 { return iSize; }

References iSize.

Referenced by C4FoWLightSection::Update().

Here is the caller graph for this function:

◆ getTotalReach()

int32_t C4FoWLight::getTotalReach ( ) const
inline

Definition at line 62 of file C4FoWLight.h.

62 { return iReach + iFadeout; }

Referenced by C4FoWLightSection::Update().

Here is the caller graph for this function:

◆ getValue()

float C4FoWLight::getValue ( ) const
inline

Definition at line 69 of file C4FoWLight.h.

69 { return colorV; }

◆ getX()

int32_t C4FoWLight::getX ( ) const
inline

Definition at line 58 of file C4FoWLight.h.

58 { return iX; }

◆ getY()

int32_t C4FoWLight::getY ( ) const
inline

Definition at line 59 of file C4FoWLight.h.

59 { return iY; }

◆ Invalidate()

void C4FoWLight::Invalidate ( C4Rect  r)

Triggers the recalculation of all light beams within the given rectangle for this light because the landscape changed.

Definition at line 52 of file C4FoWLight.cpp.

53 {
54  for(auto & section : sections)
55  section->Invalidate(r);
56 }

◆ IsVisibleForPlayer()

bool C4FoWLight::IsVisibleForPlayer ( C4Player player) const

Definition at line 364 of file C4FoWLight.cpp.

365 {
366  // check if attached to an object that is not hostile to the given player
367  if (!pObj || !player) return true;
368  return !::Hostile(pObj->Owner,player->Number);
369 }
int32_t Hostile(int32_t plr1, int32_t plr2)
int32_t Owner
Definition: C4Object.h:108

References Hostile(), C4Object::Owner, and C4ScriptGuiWindowPropertyName::player.

Here is the call graph for this function:

◆ Render()

void C4FoWLight::Render ( class C4FoWRegion pRegion,
const C4TargetFacet pOnScreen,
C4ShaderCall call 
)

Render this light

Definition at line 120 of file C4FoWLight.cpp.

121 {
122  TriangleList triangles;
123 
124  bool clip = false;
125 
126  for(auto & section : sections)
127  {
128  TriangleList sectionTriangles = section->CalculateTriangles(region);
129 
130  // if the triangles of one section are clipped completely, the neighbouring triangles
131  // must be marked as clipped
132  if(!triangles.empty()) triangles.rbegin()->clipRight |= clip;
133  if(!sectionTriangles.empty()) sectionTriangles.begin()->clipLeft |= clip;
134 
135  clip = sectionTriangles.empty();
136  triangles.splice(triangles.end(), sectionTriangles);
137  }
138 
139  CalculateFanMaxed(triangles);
140  CalculateIntermediateFadeTriangles(triangles);
141 
142  std::unique_ptr<C4FoWDrawStrategy>& strategy = onScreen ? OnScreenStrategy : OffScreenStrategy;
143  if (!strategy.get())
144  {
145  if (onScreen)
146  strategy = std::make_unique<C4FoWDrawWireframeStrategy>(this, onScreen);
147  else
148  strategy = std::make_unique<C4FoWDrawLightTextureStrategy>(this);
149  }
150 
151  strategy->Begin(region);
152 
153  DrawFan(strategy.get(), triangles);
154  DrawFanMaxed(strategy.get(), triangles);
155  DrawFade(strategy.get(), triangles);
156  DrawIntermediateFadeTriangles(strategy.get(), triangles);
157 
158  strategy->End(call);
159 }

◆ SetColor()

void C4FoWLight::SetColor ( uint32_t  iValue)

Sets the light's color in rgba format.

Definition at line 81 of file C4FoWLight.cpp.

82 {
83  colorR = GetRedValue(iValue) / 255.0f;
84  colorG = GetGreenValue(iValue) / 255.0f;
85  colorB = GetBlueValue(iValue) / 255.0f;
86 
87  float min = std::min(colorR, std::min(colorG, colorB));
88  colorV = std::max(std::max(colorR, std::max(colorG, colorB)), 1e-3f); // prevent division by 0
89  colorL = (min + colorV) / 2.0f;
90 
91  // maximize color, so that dark colors will not be desaturated after normalization
92  colorR = std::min(colorR / colorV, 1.0f);
93  colorG = std::min(colorG / colorV, 1.0f);
94  colorB = std::min(colorB / colorV, 1.0f);
95 }
#define GetRedValue(rgb)
Definition: StdColors.h:29
#define GetGreenValue(rgb)
Definition: StdColors.h:28
#define GetBlueValue(rgb)
Definition: StdColors.h:27

References GetBlueValue, GetGreenValue, and GetRedValue.

Referenced by C4FoW::Add().

Here is the caller graph for this function:

◆ SetReach()

void C4FoWLight::SetReach ( int32_t  iReach,
int32_t  iFadeout 
)

Sets the light's size in pixels. The reach is the total radius of the light while the fadeout is the number of pixels after which the light should dim down

Definition at line 58 of file C4FoWLight.cpp.

59 {
60  // Fadeout changes don't matter
61  iFadeout = iFadeout2;
62 
63  if (iReach == iReach2) return;
64 
65  if (iReach2 < iReach)
66  {
67  // Reach decreased? Prune beams
68  iReach = iReach2;
69  for(auto & section : sections)
70  section->Prune(iReach);
71 
72  } else {
73 
74  // Reach increased? Dirty beams that might get longer now
75  iReach = iReach2;
76  for(auto & section : sections)
77  section->Dirty(iReach);
78  }
79 }

Referenced by C4FoW::Add().

Here is the caller graph for this function:

◆ Update()

void C4FoWLight::Update ( C4Rect  r)

Update all light beams within the given rectangle for this light

Definition at line 97 of file C4FoWLight.cpp.

98 {
99  // Update position from object.
100  int32_t iNX = fixtoi(pObj->fix_x), iNY = fixtoi(pObj->fix_y);
101  // position may be affected by LightOffset property
102  C4ValueArray *light_offset = pObj->GetPropertyArray(P_LightOffset);
103  if (light_offset)
104  {
105  iNX += light_offset->GetItem(0).getInt();
106  iNY += light_offset->GetItem(1).getInt();
107  }
108  // Clear if we moved in any way
109  if (iNX != iX || iNY != iY)
110  {
111  for(auto & section : sections)
112  section->Prune(0);
113  iX = iNX; iY = iNY;
114  }
115 
116  for(auto & section : sections)
117  section->Update(Rec);
118 }
@ P_LightOffset
C4ValueArray * GetPropertyArray(C4PropertyName n) const
Definition: C4PropList.cpp:758
const C4Value & GetItem(int32_t iElem) const
Definition: C4ValueArray.h:38
int32_t getInt() const
Definition: C4Value.h:112

References C4Object::fix_x, C4Object::fix_y, fixtoi(), C4Value::getInt(), C4ValueArray::GetItem(), C4PropList::GetPropertyArray(), and P_LightOffset.

Here is the call graph for this function:

Friends And Related Function Documentation

◆ C4FoW

friend class C4FoW
friend

Definition at line 36 of file C4FoWLight.h.


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