OpenClonk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
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 ( 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 }
int32_t lightFadeoutRange
Definition: C4Object.h:123
int32_t lightRange
Definition: C4Object.h:122
C4Real fix_x
Definition: C4Object.h:125
C4Real fix_y
Definition: C4Object.h:125
int fixtoi(const C4Fixed &x)
Definition: C4Real.h:259
int iSize
Definition: TstC4NetIO.cpp:32
C4FoWLight::~C4FoWLight ( )

Definition at line 46 of file C4FoWLight.cpp.

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

Member Function Documentation

float C4FoWLight::getB ( ) const
inline

Definition at line 68 of file C4FoWLight.h.

68 { return colorB; }
float C4FoWLight::getBrightness ( ) const
inline

Definition at line 65 of file C4FoWLight.h.

65 { return colorV * gBright; }
int32_t C4FoWLight::getFadeout ( ) const
inline

Definition at line 61 of file C4FoWLight.h.

Referenced by C4FoWLightSection::CalculateTriangles().

61 { return iFadeout; }

Here is the caller graph for this function:

float C4FoWLight::getG ( ) const
inline

Definition at line 67 of file C4FoWLight.h.

67 { return colorG; }
float C4FoWLight::getLightness ( ) const
inline

Definition at line 70 of file C4FoWLight.h.

70 { return colorL; }
C4FoWLight* C4FoWLight::getNext ( ) const
inline

Definition at line 71 of file C4FoWLight.h.

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

71 { return pNext; }

Here is the caller graph for this function:

int32_t C4FoWLight::getNormalSize ( ) const
inline

Definition at line 64 of file C4FoWLight.h.

64 { return iSize * 2; }
int iSize
Definition: TstC4NetIO.cpp:32
C4Object* C4FoWLight::getObj ( ) const
inline

Definition at line 72 of file C4FoWLight.h.

Referenced by C4FoW::Add().

72 { return pObj; }

Here is the caller graph for this function:

float C4FoWLight::getR ( ) const
inline

Definition at line 66 of file C4FoWLight.h.

66 { return colorR; }
int32_t C4FoWLight::getReach ( ) const
inline

Definition at line 60 of file C4FoWLight.h.

Referenced by C4FoWLightSection::Update().

60 { return iReach; }

Here is the caller graph for this function:

int32_t C4FoWLight::getSize ( ) const
inline

Definition at line 63 of file C4FoWLight.h.

References iSize.

Referenced by C4FoWLightSection::Update().

63 { return iSize; }
int iSize
Definition: TstC4NetIO.cpp:32

Here is the caller graph for this function:

int32_t C4FoWLight::getTotalReach ( ) const
inline

Definition at line 62 of file C4FoWLight.h.

Referenced by C4FoWLightSection::Update().

62 { return iReach + iFadeout; }

Here is the caller graph for this function:

float C4FoWLight::getValue ( ) const
inline

Definition at line 69 of file C4FoWLight.h.

69 { return colorV; }
int32_t C4FoWLight::getX ( ) const
inline

Definition at line 58 of file C4FoWLight.h.

58 { return iX; }
int32_t C4FoWLight::getY ( ) const
inline

Definition at line 59 of file C4FoWLight.h.

59 { return iY; }
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 }
bool C4FoWLight::IsVisibleForPlayer ( C4Player player) const

Definition at line 364 of file C4FoWLight.cpp.

References Hostile(), C4Player::Number, and C4Object::Owner.

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 Number
Definition: C4Player.h:86
int32_t Owner
Definition: C4Object.h:110
int32_t Hostile(int32_t plr1, int32_t plr2)

Here is the call graph for this function:

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 }
void C4FoWLight::SetColor ( uint32_t  iValue)

Sets the light's color in rgba format.

Definition at line 81 of file C4FoWLight.cpp.

References GetBlueValue, GetGreenValue, and GetRedValue.

Referenced by C4FoW::Add().

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 GetGreenValue(rgb)
Definition: StdColors.h:28
#define GetRedValue(rgb)
Definition: StdColors.h:29
#define GetBlueValue(rgb)
Definition: StdColors.h:27

Here is the caller graph for this function:

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.

Referenced by C4FoW::Add().

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 }

Here is the caller graph for this function:

void C4FoWLight::Update ( C4Rect  r)

Update all light beams within the given rectangle for this light

Definition at line 97 of file C4FoWLight.cpp.

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

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 }
const C4Value & GetItem(int32_t iElem) const
Definition: C4ValueArray.h:38
C4ValueArray * GetPropertyArray(C4PropertyName n) const
Definition: C4PropList.cpp:785
int32_t getInt() const
Definition: C4Value.h:112
C4Real fix_x
Definition: C4Object.h:125
C4Real fix_y
Definition: C4Object.h:125
int fixtoi(const C4Fixed &x)
Definition: C4Real.h:259

Here is the call graph for this function:

Friends And Related Function Documentation

friend class C4FoW
friend

Definition at line 36 of file C4FoWLight.h.


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