2013-12-20 23:41:56 +09:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 yvt
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-12-20 23:41:56 +09:00
|
|
|
This file is part of OpenSpades.
|
2016-12-03 18:23:47 +09:00
|
|
|
|
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.
|
2016-12-03 18:23:47 +09:00
|
|
|
|
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.
|
2016-12-03 18:23:47 +09:00
|
|
|
|
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/>.
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-12-20 23:41:56 +09:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "SWFlatMapRenderer.h"
|
|
|
|
#include "SWImage.h"
|
2016-12-03 18:23:47 +09:00
|
|
|
#include "SWMapRenderer.h"
|
2013-12-20 23:41:56 +09:00
|
|
|
#include "SWRenderer.h"
|
2016-12-03 18:23:47 +09:00
|
|
|
#include <Client/GameMap.h>
|
2013-12-20 23:41:56 +09:00
|
|
|
#include <Core/Debug.h>
|
2016-12-03 18:23:47 +09:00
|
|
|
#include <Core/Exception.h>
|
|
|
|
#include <algorithm>
|
2013-12-20 23:41:56 +09:00
|
|
|
|
|
|
|
namespace spades {
|
|
|
|
namespace draw {
|
2016-12-03 18:23:47 +09:00
|
|
|
SWFlatMapRenderer::SWFlatMapRenderer(SWRenderer *r, client::GameMap *map)
|
|
|
|
: map(map), r(r), needsUpdate(true), w(map->Width()), h(map->Height()) {
|
2013-12-20 23:41:56 +09:00
|
|
|
SPADES_MARK_FUNCTION();
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
if (w & 31) {
|
2013-12-20 23:41:56 +09:00
|
|
|
SPRaise("Map width must be a multiple of 32.");
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
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);
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-12-22 04:55:30 +09:00
|
|
|
Update(true);
|
2013-12-20 23:41:56 +09:00
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
SWFlatMapRenderer::~SWFlatMapRenderer() { SPADES_MARK_FUNCTION(); }
|
|
|
|
|
2013-12-22 04:55:30 +09:00
|
|
|
void SWFlatMapRenderer::Update(bool firstTime) {
|
2013-12-20 23:41:56 +09:00
|
|
|
SPADES_MARK_FUNCTION();
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock(updateInfoLock);
|
2016-12-03 18:23:47 +09:00
|
|
|
|
|
|
|
if (!needsUpdate)
|
2013-12-20 23:41:56 +09:00
|
|
|
return;
|
|
|
|
needsUpdate = false;
|
|
|
|
updateMap.swap(updateMap2);
|
2013-12-22 04:55:30 +09:00
|
|
|
std::fill(updateMap.begin(), updateMap.end(), 0);
|
2013-12-20 23:41:56 +09:00
|
|
|
}
|
|
|
|
auto *outPixels = img->GetRawBitmap();
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-12-22 04:55:30 +09:00
|
|
|
auto *mapRenderer = r->mapRenderer.get();
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-12-20 23:41:56 +09:00
|
|
|
int idx = 0;
|
2016-12-03 18:23:47 +09:00
|
|
|
for (int y = 0; y < h; y++) {
|
|
|
|
for (int x = 0; x < w; x += 32) {
|
2013-12-22 04:55:30 +09:00
|
|
|
uint32_t upd = updateMap2[idx];
|
2016-12-03 18:23:47 +09:00
|
|
|
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;
|
2016-12-03 18:23:47 +09:00
|
|
|
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-22 04:55:30 +09:00
|
|
|
}
|
2013-12-20 23:41:56 +09:00
|
|
|
}
|
|
|
|
upd >>= 1;
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
// updateMap[idx] = 0;
|
2013-12-20 23:41:56 +09:00
|
|
|
}
|
|
|
|
outPixels += 32;
|
|
|
|
idx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
2013-12-20 23:41:56 +09:00
|
|
|
uint32_t SWFlatMapRenderer::GeneratePixel(int x, int y) {
|
|
|
|
const int depth = map->Depth();
|
2016-12-03 18:23:47 +09:00
|
|
|
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);
|
2016-12-03 18:23:47 +09:00
|
|
|
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
|
|
|
|
}
|
2016-12-03 18:23:47 +09:00
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|