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 36 of file C4FoWLight.h.

Constructor & Destructor Documentation

C4FoWLight::C4FoWLight ( C4Object pObj)

Definition at line 31 of file C4FoWLight.cpp.

32  : iX(fixtoi(pObj->fix_x)),
33  iY(fixtoi(pObj->fix_y)),
34  iReach(pObj->lightRange),
35  iFadeout(pObj->lightFadeoutRange),
36  iSize(20), gBright(0.5), colorR(1.0), colorG(1.0), colorB(1.0),
37  colorV(1.0), colorL(1.0),
38  pNext(nullptr),
39  pObj(pObj),
40  sections(4)
41 {
42  sections[0] = new C4FoWLightSection(this,0);
43  sections[1] = new C4FoWLightSection(this,90);
44  sections[2] = new C4FoWLightSection(this,180);
45  sections[3] = new C4FoWLightSection(this,270);
46 }
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:35
C4FoWLight::~C4FoWLight ( )

Definition at line 48 of file C4FoWLight.cpp.

49 {
50  for(size_t i = 0; i < sections.size(); ++i )
51  delete sections[i];
52 }

Member Function Documentation

float C4FoWLight::getB ( ) const
inline

Definition at line 70 of file C4FoWLight.h.

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

Definition at line 67 of file C4FoWLight.h.

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

Definition at line 63 of file C4FoWLight.h.

Referenced by C4FoWLightSection::CalculateTriangles().

63 { return iFadeout; }

Here is the caller graph for this function:

float C4FoWLight::getG ( ) const
inline

Definition at line 69 of file C4FoWLight.h.

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

Definition at line 72 of file C4FoWLight.h.

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

Definition at line 73 of file C4FoWLight.h.

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

73 { return pNext; }

Here is the caller graph for this function:

int32_t C4FoWLight::getNormalSize ( ) const
inline

Definition at line 66 of file C4FoWLight.h.

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

Definition at line 74 of file C4FoWLight.h.

Referenced by C4FoW::Add().

74 { return pObj; }

Here is the caller graph for this function:

float C4FoWLight::getR ( ) const
inline

Definition at line 68 of file C4FoWLight.h.

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

Definition at line 62 of file C4FoWLight.h.

Referenced by C4FoWLightSection::Update().

62 { return iReach; }

Here is the caller graph for this function:

int32_t C4FoWLight::getSize ( ) const
inline

Definition at line 65 of file C4FoWLight.h.

References iSize.

Referenced by C4FoWLightSection::Update().

65 { return iSize; }
int iSize
Definition: TstC4NetIO.cpp:35

Here is the caller graph for this function:

int32_t C4FoWLight::getTotalReach ( ) const
inline

Definition at line 64 of file C4FoWLight.h.

Referenced by C4FoWLightSection::Update().

64 { return iReach + iFadeout; }

Here is the caller graph for this function:

float C4FoWLight::getValue ( ) const
inline

Definition at line 71 of file C4FoWLight.h.

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

Definition at line 60 of file C4FoWLight.h.

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

Definition at line 61 of file C4FoWLight.h.

61 { 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 54 of file C4FoWLight.cpp.

55 {
56  for(size_t i = 0; i < sections.size(); ++i )
57  sections[i]->Invalidate(r);
58 }
void Invalidate(C4Rect r)
Definition: C4FoWLight.cpp:54
bool C4FoWLight::IsVisibleForPlayer ( C4Player player) const

Definition at line 370 of file C4FoWLight.cpp.

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

371 {
372  // check if attached to an object that is not hostile to the given player
373  if (!pObj || !player) return true;
374  return !::Hostile(pObj->Owner,player->Number);
375 }
int32_t Number
Definition: C4Player.h:88
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 122 of file C4FoWLight.cpp.

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

Sets the light's color in rgba format.

Definition at line 83 of file C4FoWLight.cpp.

References GetBlueValue, GetGreenValue, and GetRedValue.

Referenced by C4FoW::Add().

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

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 60 of file C4FoWLight.cpp.

Referenced by C4FoW::Add().

61 {
62  // Fadeout changes don't matter
63  iFadeout = iFadeout2;
64 
65  if (iReach == iReach2) return;
66 
67  if (iReach2 < iReach)
68  {
69  // Reach decreased? Prune beams
70  iReach = iReach2;
71  for(size_t i = 0; i < sections.size(); ++i )
72  sections[i]->Prune(iReach);
73 
74  } else {
75 
76  // Reach increased? Dirty beams that might get longer now
77  iReach = iReach2;
78  for(size_t i = 0; i < sections.size(); ++i )
79  sections[i]->Dirty(iReach);
80  }
81 }

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 99 of file C4FoWLight.cpp.

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

100 {
101  // Update position from object.
102  int32_t iNX = fixtoi(pObj->fix_x), iNY = fixtoi(pObj->fix_y);
103  // position may be affected by LightOffset property
104  C4ValueArray *light_offset = pObj->GetPropertyArray(P_LightOffset);
105  if (light_offset)
106  {
107  iNX += light_offset->GetItem(0).getInt();
108  iNY += light_offset->GetItem(1).getInt();
109  }
110  // Clear if we moved in any way
111  if (iNX != iX || iNY != iY)
112  {
113  for(size_t i = 0; i < sections.size(); ++i )
114  sections[i]->Prune(0);
115  iX = iNX; iY = iNY;
116  }
117 
118  for(size_t i = 0; i < sections.size(); ++i )
119  sections[i]->Update(Rec);
120 }
void Update(C4Rect r)
Definition: C4FoWLight.cpp:99
const C4Value & GetItem(int32_t iElem) const
Definition: C4ValueArray.h:38
C4ValueArray * GetPropertyArray(C4PropertyName n) const
Definition: C4PropList.cpp:766
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 38 of file C4FoWLight.h.


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