2011-08-03 20:05:33 +00:00
|
|
|
/*
|
|
|
|
Copyright (C) 2007, 2010 - Bit-Blot
|
|
|
|
|
|
|
|
This file is part of Aquaria.
|
|
|
|
|
|
|
|
Aquaria is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
#include "Core.h"
|
2016-07-09 02:18:40 +00:00
|
|
|
#include "RenderBase.h"
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
#define BASE_ARRAY_SIZE 100 // Size of an object array in a new layer
|
|
|
|
|
|
|
|
RenderObjectLayer::RenderObjectLayer()
|
|
|
|
: renderObjects(BASE_ARRAY_SIZE)
|
2016-05-05 17:40:28 +00:00
|
|
|
{
|
2011-08-03 20:05:33 +00:00
|
|
|
followCamera = NO_FOLLOW_CAMERA;
|
|
|
|
visible = true;
|
|
|
|
startPass = endPass = 0;
|
|
|
|
followCameraLock = FCL_NONE;
|
|
|
|
update = true;
|
|
|
|
|
|
|
|
const int size = renderObjects.size();
|
|
|
|
for (int i = 0; i < size; i++)
|
|
|
|
renderObjects[i] = 0;
|
|
|
|
objectCount = 0;
|
|
|
|
firstFreeIdx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderObjectLayer::~RenderObjectLayer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderObjectLayer::add(RenderObject* r)
|
|
|
|
{
|
2017-01-14 17:10:20 +00:00
|
|
|
size_t size = renderObjects.size();
|
2011-08-03 20:05:33 +00:00
|
|
|
if (firstFreeIdx >= size)
|
|
|
|
{
|
|
|
|
size += size/2; // Increase size by 50% each time we fill up.
|
|
|
|
renderObjects.resize(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderObjects[firstFreeIdx] = r;
|
|
|
|
objectCount++;
|
|
|
|
r->setIdx(firstFreeIdx);
|
|
|
|
|
|
|
|
for (; firstFreeIdx < size; firstFreeIdx++)
|
|
|
|
{
|
|
|
|
if (!renderObjects[firstFreeIdx])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderObjectLayer::remove(RenderObject* r)
|
|
|
|
{
|
2017-01-14 17:10:20 +00:00
|
|
|
const size_t idx = r->getIdx();
|
|
|
|
if (idx >= renderObjects.size())
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
|
|
|
errorLog("Trying to remove RenderObject with invalid index");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (renderObjects[idx] != r)
|
|
|
|
{
|
|
|
|
errorLog("RenderObject pointer doesn't match array");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
renderObjects[idx] = 0;
|
|
|
|
objectCount--;
|
|
|
|
if (idx < firstFreeIdx)
|
|
|
|
firstFreeIdx = idx;
|
|
|
|
r->setIdx(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderObjectLayer::moveToFront(RenderObject *r)
|
|
|
|
{
|
2017-01-14 17:10:20 +00:00
|
|
|
const size_t size = renderObjects.size();
|
|
|
|
const size_t curIdx = r->getIdx();
|
|
|
|
size_t lastUsed;
|
2011-08-03 20:05:33 +00:00
|
|
|
for (lastUsed = size-1; lastUsed > curIdx; lastUsed--)
|
|
|
|
{
|
|
|
|
if (renderObjects[lastUsed])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (curIdx == lastUsed)
|
|
|
|
{
|
|
|
|
// Already at the front, so nothing to do.
|
|
|
|
}
|
|
|
|
else if (lastUsed < size-1)
|
|
|
|
{
|
2017-01-14 17:10:20 +00:00
|
|
|
const size_t newIdx = lastUsed + 1;
|
2011-08-03 20:05:33 +00:00
|
|
|
renderObjects[curIdx] = 0;
|
|
|
|
renderObjects[newIdx] = r;
|
|
|
|
r->setIdx(newIdx);
|
2011-11-20 14:44:17 +00:00
|
|
|
if (firstFreeIdx > curIdx)
|
|
|
|
firstFreeIdx = curIdx;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
else if (objectCount == size)
|
|
|
|
{
|
|
|
|
// Expand the array so future calls have a bit of breathing room.
|
2017-01-14 17:10:20 +00:00
|
|
|
const size_t newSize = size + 10;
|
2011-08-03 20:05:33 +00:00
|
|
|
renderObjects.resize(newSize);
|
|
|
|
renderObjects[curIdx] = 0;
|
|
|
|
renderObjects[size] = r;
|
|
|
|
r->setIdx(size);
|
2017-01-14 17:10:20 +00:00
|
|
|
for (size_t i = size+1; i < newSize; i++)
|
2011-08-03 20:05:33 +00:00
|
|
|
renderObjects[i] = 0;
|
2012-02-05 19:31:17 +00:00
|
|
|
if (firstFreeIdx > curIdx)
|
|
|
|
firstFreeIdx = curIdx;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Need to shift elements downward to make room for the new one.
|
|
|
|
renderObjects[curIdx] = 0;
|
2017-01-14 17:10:20 +00:00
|
|
|
size_t lastFree;
|
2011-08-03 20:05:33 +00:00
|
|
|
for (lastFree = lastUsed-1; lastFree > curIdx; lastFree--)
|
|
|
|
{
|
|
|
|
if (!renderObjects[lastFree])
|
|
|
|
break;
|
|
|
|
}
|
2017-01-14 17:10:20 +00:00
|
|
|
for (size_t i = lastFree + 1; i <= lastUsed; i++)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
|
|
|
renderObjects[i-1] = renderObjects[i];
|
2011-11-20 14:44:17 +00:00
|
|
|
renderObjects[i-1]->setIdx(i-1); // Known to be non-NULL.
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
renderObjects[lastUsed] = r;
|
|
|
|
r->setIdx(lastUsed);
|
2012-02-05 19:31:17 +00:00
|
|
|
firstFreeIdx = 0;
|
|
|
|
// Known to have at least one NULL-element
|
2011-11-20 14:44:17 +00:00
|
|
|
while (renderObjects[firstFreeIdx])
|
|
|
|
firstFreeIdx++;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RenderObjectLayer::moveToBack(RenderObject *r)
|
|
|
|
{
|
2017-01-14 17:10:20 +00:00
|
|
|
const size_t size = renderObjects.size();
|
|
|
|
const size_t curIdx = r->getIdx();
|
|
|
|
size_t firstUsed;
|
2011-08-03 20:05:33 +00:00
|
|
|
for (firstUsed = 0; firstUsed < curIdx; firstUsed++)
|
|
|
|
{
|
|
|
|
if (renderObjects[firstUsed])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (curIdx == firstUsed)
|
|
|
|
{
|
|
|
|
// Already at the back, so nothing to do.
|
|
|
|
}
|
|
|
|
else if (firstUsed > 0)
|
|
|
|
{
|
|
|
|
const int newIdx = firstUsed - 1;
|
|
|
|
renderObjects[curIdx] = 0;
|
|
|
|
renderObjects[newIdx] = r;
|
|
|
|
r->setIdx(newIdx);
|
2011-11-20 14:44:17 +00:00
|
|
|
// firstFreeIdx must be 0 here; if we filled slot 0, then
|
|
|
|
// scan forward for the next empty element.
|
|
|
|
while (renderObjects[firstFreeIdx])
|
2011-08-03 20:05:33 +00:00
|
|
|
firstFreeIdx++;
|
|
|
|
}
|
|
|
|
else if (objectCount == size)
|
|
|
|
{
|
2017-01-14 17:10:20 +00:00
|
|
|
const size_t newSize = size + 10;
|
|
|
|
const size_t sizeDiff = newSize - size;
|
|
|
|
const size_t newIdx = sizeDiff - 1;
|
2011-08-03 20:05:33 +00:00
|
|
|
|
|
|
|
renderObjects.resize(newSize);
|
|
|
|
renderObjects[curIdx] = 0;
|
2017-01-14 17:10:20 +00:00
|
|
|
for (size_t i = newSize - 1; i >= sizeDiff; i--)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
|
|
|
renderObjects[i] = renderObjects[i - sizeDiff];
|
2012-10-08 22:57:34 +00:00
|
|
|
if(renderObjects[i])
|
|
|
|
renderObjects[i]->setIdx(i);
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
2017-01-14 17:10:20 +00:00
|
|
|
for (size_t i = 0; i < newIdx; i++)
|
2011-08-03 20:05:33 +00:00
|
|
|
renderObjects[i] = 0;
|
|
|
|
renderObjects[newIdx] = r;
|
|
|
|
r->setIdx(newIdx);
|
|
|
|
firstFreeIdx = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
renderObjects[curIdx] = 0;
|
|
|
|
if (curIdx < firstFreeIdx)
|
|
|
|
firstFreeIdx = curIdx;
|
|
|
|
for (int i = firstFreeIdx; i > 0; i--)
|
|
|
|
{
|
|
|
|
renderObjects[i] = renderObjects[i-1];
|
2011-11-20 14:44:17 +00:00
|
|
|
renderObjects[i]->setIdx(i); // Known to be non-NULL.
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
renderObjects[0] = r;
|
|
|
|
r->setIdx(0);
|
|
|
|
for (firstFreeIdx++; firstFreeIdx < size; firstFreeIdx++)
|
|
|
|
{
|
|
|
|
if (!renderObjects[firstFreeIdx])
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-22 05:38:23 +00:00
|
|
|
void RenderObjectLayer::reloadDevice()
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-05-22 05:38:23 +00:00
|
|
|
void RenderObjectLayer::prepareRender()
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2022-05-22 05:38:23 +00:00
|
|
|
toRender.clear();
|
|
|
|
|
|
|
|
size_t n = 0;
|
|
|
|
for (const RenderObject *robj = getFirst(); robj; robj = getNext())
|
|
|
|
{
|
|
|
|
++n;
|
|
|
|
if(robj->shouldTryToRender())
|
|
|
|
toRender.push_back(robj);
|
|
|
|
}
|
|
|
|
core->renderObjectCount += toRender.size();
|
|
|
|
toRender.push_back(NULL); // terminate
|
|
|
|
core->totalRenderObjectCount += n;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 05:38:23 +00:00
|
|
|
void RenderObjectLayer::render() const
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2022-05-22 05:38:23 +00:00
|
|
|
if(toRender.size() <= 1)
|
2011-08-03 20:05:33 +00:00
|
|
|
return;
|
|
|
|
|
2022-05-22 05:38:23 +00:00
|
|
|
size_t proc = 0;
|
|
|
|
CombinedRenderAndGPUState rs;
|
|
|
|
|
|
|
|
if (startPass == endPass)
|
2011-08-03 20:05:33 +00:00
|
|
|
{
|
2022-05-22 05:38:23 +00:00
|
|
|
rs.pass = RenderObject::RENDER_ALL;
|
|
|
|
const RenderObject * const * rlist = &toRender[0]; // known to have at least one element
|
|
|
|
while(const RenderObject *ro = *rlist++)
|
|
|
|
ro->render(rs);
|
|
|
|
proc += toRender.size() - 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (int pass = startPass; pass <= endPass; pass++)
|
|
|
|
{
|
|
|
|
rs.pass = pass;
|
|
|
|
const RenderObject * const * rlist = &toRender[0]; // known to have at least one element
|
|
|
|
while(const RenderObject *ro = *rlist++)
|
|
|
|
if(ro->isVisibleInPass(pass))
|
|
|
|
{
|
|
|
|
ro->render(rs);
|
|
|
|
++proc;
|
|
|
|
}
|
|
|
|
}
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|
2022-05-22 05:38:23 +00:00
|
|
|
core->processedRenderObjectCount += proc;
|
2011-08-03 20:05:33 +00:00
|
|
|
}
|