Performance improvements, added flickering cursor light.

develop
Warmist 2013-06-23 20:45:05 +03:00
parent 14c494dbfe
commit f92e4c91b8
3 changed files with 87 additions and 21 deletions

@ -65,6 +65,7 @@ void lightingEngineViewscreen::reinit()
size_t size=w*h;
lightMap.resize(size,lightCell(1,1,1));
ocupancy.resize(size);
lights.resize(size);
}
void plotCircle(int xm, int ym, int r,std::function<void(int,int)> setPixel)
{
@ -108,18 +109,22 @@ bool lightingEngineViewscreen::lightUpCell(lightCell& power,int dx,int dy,int tx
size_t tile=getIndex(tx,ty);
float dsq=dx*dx+dy*dy;
lightCell& v=ocupancy[tile];
lightSource& ls=lights[tile];
bool wallhack=false;
bool outsidehack=false;
if(v.r+v.g+v.b==0)
wallhack=true;
if(v.r<0)
outsidehack=true;
if (dsq>0 && !wallhack && !outsidehack)
if (dsq>0 && !wallhack)
{
power.r=power.r*(pow(v.r,dsq));
power.g=power.g*(pow(v.g,dsq));
power.b=power.b*(pow(v.b,dsq));
}
if(ls.radius>0 && dsq>0)
{
if(power<ls.power)
return false;
}
//float dt=sqrt(dsq);
lightCell oldCol=lightMap[tile];
lightCell ncol=blend(power,oldCol);
@ -127,8 +132,6 @@ bool lightingEngineViewscreen::lightUpCell(lightCell& power,int dx,int dy,int tx
if(wallhack)
return false;
if(dsq>0 && outsidehack)
return false;
float pwsq=power.r*power.r+power.g*power.g+power.b*power.b;
return pwsq>levelDim*levelDim;
}
@ -145,17 +148,32 @@ void lightingEngineViewscreen::doFovs()
{
mapPort=getMapViewport();
using namespace std::placeholders;
for(size_t i=0;i<lights.size();i++)
{
lightSource& csource=lights[i];
plotCircle(csource.pos.x,csource.pos.y,csource.radius,std::bind(&lightingEngineViewscreen::doRay,this,csource.power,csource.pos.x,csource.pos.y,_1,_2));
}
for(int i=mapPort.first.x;i<mapPort.second.x;i++)
for(int j=mapPort.first.y;j<mapPort.second.y;j++)
{
lightSource& csource=lights[getIndex(i,j)];
if(csource.radius>0)
{
lightCell power=csource.power;
int radius =csource.radius;
if(csource.flicker)
{
float flicker=(rand()/(float)RAND_MAX)/2.0f+0.5f;
radius*=flicker;
power=power*flicker;
}
plotCircle(i,j,radius,
std::bind(&lightingEngineViewscreen::doRay,this,power,i,j,_1,_2));
}
}
}
void lightingEngineViewscreen::calculate()
{
rect2d vp=getMapViewport();
const lightCell dim(levelDim,levelDim,levelDim);
lightMap.assign(lightMap.size(),lightCell(1,1,1));
lights.assign(lights.size(),lightSource());
for(int i=vp.first.x;i<vp.second.x;i++)
for(int j=vp.first.y;j<vp.second.y;j++)
{
@ -183,9 +201,22 @@ void lightingEngineViewscreen::updateWindow()
static size_t max_list_size = 100000; // Avoid iterating over huge lists
void lightSource::combine(const lightSource& other)
{
power=blend(power,other.power);
radius=std::max(other.radius,radius);//hack... but who cares
}
bool lightingEngineViewscreen::addLight(int tileId,const lightSource& light)
{
bool wasLight=lights[tileId].radius>0;
lights[tileId].combine(light);
if(light.flicker)
lights[tileId].flicker=true;
return wasLight;
}
void lightingEngineViewscreen::doOcupancyAndLights()
{
lights.clear();
rect2d vp=getMapViewport();
int window_x=*df::global::window_x;
@ -270,8 +301,9 @@ void lightingEngineViewscreen::doOcupancyAndLights()
{
int wx=x-window_x+vp.first.x;
int wy=y-window_y+vp.first.y;
lightCell& curCell=ocupancy[getIndex(wx,wy)];
curCell=lightCell(0.8f,0.8f,0.8f);
int tile=getIndex(wx,wy);
lightCell& curCell=ocupancy[tile];
curCell=lightCell(0.85f,0.85f,0.85f);
df::tiletype* type = Maps::getTileType(x,y,window_z);
if(!type)
continue;
@ -328,14 +360,19 @@ void lightingEngineViewscreen::doOcupancyAndLights()
//todo constructions
//lights
if((d->bits.liquid_type && d->bits.flow_size>0)|| (d2 && d2->bits.liquid_type && d2->bits.flow_size>0))
if((d->bits.liquid_type && d->bits.flow_size>0)||
(
(shape==df::tiletype_shape::EMPTY || shape==df::tiletype_shape::RAMP_TOP || shape==df::tiletype_shape::STAIR_DOWN || shape==df::tiletype_shape::STAIR_UPDOWN )
&& d2 && d2->bits.liquid_type && d2->bits.flow_size>0)
)
{
lightSource lava={lightCell(0.8f,0.2f,0.2f),5,coord2d(wx,wy)};
lights.push_back(lava);
lightSource lava(lightCell(0.8f,0.2f,0.2f),5);
addLight(tile,lava);
}
if(d->bits.outside && d->bits.flow_size==0)
{
curCell=lightCell(-1,-1,-1);//Marking as outside so no calculation is done on it
lightSource sun(lightCell(1,1,1),15);
addLight(tile,sun);
}
}
@ -354,6 +391,7 @@ void lightingEngineViewscreen::doOcupancyAndLights()
df::coord2d pos=f->pos;
int wx=pos.x-window_x+vp.first.x;
int wy=pos.y-window_y+vp.first.y;
int tile=getIndex(wx,wy);
if(wx>=vp.first.x && wy>=vp.first.y && wx<=vp.second.x && wy<=vp.second.y)
{
lightCell fireColor;
@ -369,10 +407,19 @@ void lightingEngineViewscreen::doOcupancyAndLights()
{
fireColor=lightCell(0.64f,0.0f,0.0f);
}
lightSource fire={fireColor,f->density/5,coord2d(wx,wy)};
lights.push_back(fire);
lightSource fire(fireColor,f->density/5);
addLight(tile,fire);
}
}
}
}
if(df::global::cursor->x>-30000)
{
lightSource cursor(lightCell(0.96f,0.84f,0.03f),11);
cursor.flicker=true;
int wx=df::global::cursor->x-window_x+vp.first.x;
int wy=df::global::cursor->y-window_y+vp.first.y;
int tile=getIndex(wx,wy);
addLight(tile,cursor);
}
}

@ -79,7 +79,21 @@ struct lightSource
{
lightCell power;
int radius;
df::coord2d pos;
bool flicker;
lightSource():power(0,0,0),radius(0),flicker(false)
{
}
lightSource(lightCell power,int radius):power(power),radius(radius),flicker(false)
{
}
float powerSquared()const
{
return power.r*power.r+power.g*power.g+power.b*power.b;
}
void combine(const lightSource& other);
};
class lightingEngineViewscreen:public lightingEngine
{
@ -96,6 +110,7 @@ private:
void doRay(lightCell power,int cx,int cy,int tx,int ty);
void doFovs();
bool lightUpCell(lightCell& power,int dx,int dy,int tx,int ty);
bool addLight(int tileId,const lightSource& light);
size_t inline getIndex(int x,int y)
{
return x*h+y;

@ -222,6 +222,10 @@ struct lightCell
{
return lightCell(r+other.r,g+other.g,b+other.b);
}
bool operator<(const lightCell& other)
{
return r<other.r && g<other.g && b<other.b;
}
};
struct renderer_test : public renderer_wrap {
private: