Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/simulation2/helpers/Grid.h
/* Copyright (C) 2018 Wildfire Games. | /* Copyright (C) 2019 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 175 Lines • ▼ Show 20 Lines | class SparseGrid | ||||
enum { BucketBits = 4, BucketSize = 1 << BucketBits }; | enum { BucketBits = 4, BucketSize = 1 << BucketBits }; | ||||
T* GetBucket(int i, int j) | T* GetBucket(int i, int j) | ||||
{ | { | ||||
size_t b = (j >> BucketBits) * m_BW + (i >> BucketBits); | size_t b = (j >> BucketBits) * m_BW + (i >> BucketBits); | ||||
if (!m_Data[b]) | if (!m_Data[b]) | ||||
{ | { | ||||
m_Data[b] = new T[BucketSize*BucketSize]; | m_Data[b] = new T[BucketSize*BucketSize](); | ||||
memset(m_Data[b], 0, BucketSize*BucketSize*sizeof(T)); | |||||
} | } | ||||
return m_Data[b]; | return m_Data[b]; | ||||
} | } | ||||
public: | public: | ||||
SparseGrid(u16 w, u16 h) : m_W(w), m_H(h), m_DirtyID(0) | SparseGrid(u16 w, u16 h) : m_W(w), m_H(h), m_DirtyID(0) | ||||
{ | { | ||||
ENSURE(m_W && m_H); | ENSURE(m_W && m_H); | ||||
m_BW = (u16)((m_W + BucketSize-1) >> BucketBits); | m_BW = (u16)((m_W + BucketSize-1) >> BucketBits); | ||||
m_BH = (u16)((m_H + BucketSize-1) >> BucketBits); | m_BH = (u16)((m_H + BucketSize-1) >> BucketBits); | ||||
m_Data = new T*[m_BW*m_BH]; | m_Data = new T*[m_BW*m_BH](); | ||||
memset(m_Data, 0, m_BW*m_BH*sizeof(T*)); | |||||
} | } | ||||
~SparseGrid() | ~SparseGrid() | ||||
{ | { | ||||
reset(); | reset(); | ||||
delete[] m_Data; | delete[] m_Data; | ||||
} | } | ||||
void reset() | void reset() | ||||
{ | { | ||||
for (size_t i = 0; i < (size_t)(m_BW*m_BH); ++i) | for (size_t i = 0; i < (size_t)(m_BW*m_BH); ++i) | ||||
delete[] m_Data[i]; | delete[] m_Data[i]; | ||||
memset(m_Data, 0, m_BW*m_BH*sizeof(T*)); | m_Data = new T*[m_BW*m_BH](); | ||||
} | } | ||||
void set(int i, int j, const T& value) | void set(int i, int j, const T& value) | ||||
{ | { | ||||
#if GRID_BOUNDS_DEBUG | #if GRID_BOUNDS_DEBUG | ||||
ENSURE(0 <= i && i < m_W && 0 <= j && j < m_H); | ENSURE(0 <= i && i < m_W && 0 <= j && j < m_H); | ||||
#endif | #endif | ||||
GetBucket(i, j)[(j % BucketSize)*BucketSize + (i % BucketSize)] = value; | GetBucket(i, j)[(j % BucketSize)*BucketSize + (i % BucketSize)] = value; | ||||
▲ Show 20 Lines • Show All 61 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator