openspades/Sources/Draw/SWFlatMapRenderer.cpp

114 lines
3.1 KiB
C++
Raw Normal View History

2013-12-20 23:41:56 +09:00
/*
Copyright (c) 2013 yvt
2013-12-20 23:41:56 +09:00
This file is part of OpenSpades.
2013-12-20 23:41:56 +09:00
OpenSpades 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 3 of the License, or
(at your option) any later version.
2013-12-20 23:41:56 +09:00
OpenSpades 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.
2013-12-20 23:41:56 +09:00
You should have received a copy of the GNU General Public License
along with OpenSpades. If not, see <http://www.gnu.org/licenses/>.
2013-12-20 23:41:56 +09:00
*/
#include <algorithm>
2013-12-20 23:41:56 +09:00
#include "SWFlatMapRenderer.h"
#include "SWImage.h"
#include "SWMapRenderer.h"
2013-12-20 23:41:56 +09:00
#include "SWRenderer.h"
#include <Client/GameMap.h>
2013-12-20 23:41:56 +09:00
#include <Core/Debug.h>
#include <Core/Exception.h>
2013-12-20 23:41:56 +09:00
namespace spades {
namespace draw {
SWFlatMapRenderer::SWFlatMapRenderer(SWRenderer *r, client::GameMap *map)
2017-11-18 21:15:07 +00:00
: r(r), map(map), w(map->Width()), h(map->Height()), needsUpdate(true) {
2013-12-20 23:41:56 +09:00
SPADES_MARK_FUNCTION();
if (w & 31) {
2013-12-20 23:41:56 +09:00
SPRaise("Map width must be a multiple of 32.");
}
2013-12-20 23:41:56 +09:00
img.Set(new SWImage(map->Width(), map->Height()), false);
updateMap.resize(w * h / 32);
std::fill(updateMap.begin(), updateMap.end(), 0xffffffff);
updateMap2.resize(w * h / 32);
std::fill(updateMap2.begin(), updateMap2.end(), 0xffffffff);
Update(true);
2013-12-20 23:41:56 +09:00
}
SWFlatMapRenderer::~SWFlatMapRenderer() { SPADES_MARK_FUNCTION(); }
void SWFlatMapRenderer::Update(bool firstTime) {
2013-12-20 23:41:56 +09:00
SPADES_MARK_FUNCTION();
{
std::lock_guard<std::mutex> lock(updateInfoLock);
if (!needsUpdate)
2013-12-20 23:41:56 +09:00
return;
needsUpdate = false;
updateMap.swap(updateMap2);
std::fill(updateMap.begin(), updateMap.end(), 0);
2013-12-20 23:41:56 +09:00
}
auto *outPixels = img->GetRawBitmap();
auto *mapRenderer = r->mapRenderer.get();
2013-12-20 23:41:56 +09:00
int idx = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x += 32) {
uint32_t upd = updateMap2[idx];
if (upd) {
for (int i = 0; i < 32; i++) {
if (upd & 1) {
2013-12-20 23:41:56 +09:00
auto c = GeneratePixel(x + i, y);
outPixels[i] = c;
if (!firstTime) {
mapRenderer->UpdateRle(x + i, y);
mapRenderer->UpdateRle((x + i + 1) & (w - 1), y);
mapRenderer->UpdateRle((x + i - 1) & (w - 1), y);
mapRenderer->UpdateRle(x + i, (y + 1) & (h - 1));
mapRenderer->UpdateRle(x + i, (y - 1) & (h - 1));
}
2013-12-20 23:41:56 +09:00
}
upd >>= 1;
}
// updateMap[idx] = 0;
2013-12-20 23:41:56 +09:00
}
outPixels += 32;
idx++;
}
}
}
2013-12-20 23:41:56 +09:00
uint32_t SWFlatMapRenderer::GeneratePixel(int x, int y) {
const int depth = map->Depth();
for (int z = 0; z < depth; z++) {
if (map->IsSolid(x, y, z)) {
2013-12-20 23:41:56 +09:00
uint32_t col = map->GetColor(x, y, z);
col = (col & 0xff00) | ((col & 0xff) << 16) | ((col & 0xff0000) >> 16);
2013-12-20 23:41:56 +09:00
col |= 0xff000000;
return col;
}
}
return 0; // shouldn't reach here for valid maps
}
2013-12-20 23:41:56 +09:00
void SWFlatMapRenderer::SetNeedsUpdate(int x, int y) {
std::lock_guard<std::mutex> lock(updateInfoLock);
needsUpdate = true;
updateMap[(x + y * w) >> 5] |= 1 << (x & 31);
}
}
}