Resize quadtree entity capacity as needed.

This commit is contained in:
Steve 2016-02-21 08:13:17 +00:00
parent 6b1e62dd38
commit b004dd95ab
5 changed files with 46 additions and 12 deletions

View File

@ -52,8 +52,6 @@ void initBattle(void)
app.delegate.draw = &draw;
memset(&app.keyboard, 0, sizeof(int) * MAX_KEYBOARD_KEYS);
battle.quadtree.w = BATTLE_AREA_WIDTH;
battle.quadtree.h = BATTLE_AREA_HEIGHT;
initQuadtree(&battle.quadtree);
initBullets();

View File

@ -30,14 +30,22 @@ static void addCandidate(Entity *e);
static int candidatesComparator(const void *a, const void *b);
static void getAllEntsWithinNode(int x, int y, int w, int h, Entity *ignore, Quadtree *root);
static void destroyQuadtreeNode(Quadtree *root);
static void resizeQTEntCapacity(Quadtree *root);
void initQuadtree(Quadtree *root)
{
Quadtree *node;
int i, w, h;
/* entire battlefield */
if (root->depth == 0)
{
root->w = BATTLE_AREA_WIDTH;
root->h = BATTLE_AREA_HEIGHT;
root->capacity = QT_INITIAL_CAPACITY;
root->ents = malloc(sizeof(Entity*) * root->capacity);
memset(root->ents, 0, sizeof(Entity*) * root->capacity);
memory = 0;
}
@ -55,6 +63,9 @@ void initQuadtree(Quadtree *root)
root->node[i] = node;
node->depth = root->depth + 1;
node->capacity = QT_INITIAL_CAPACITY;
node->ents = malloc(sizeof(Entity*) * node->capacity);
memset(node->ents, 0, sizeof(Entity*) * node->capacity);
if (i == 0)
{
@ -110,13 +121,35 @@ void addToQuadtree(Entity *e, Quadtree *root)
}
}
if (root->numEnts < QT_MAX_ENTS)
if (root->numEnts == root->capacity)
{
root->ents[root->numEnts++] = e;
return;
resizeQTEntCapacity(root);
}
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_WARN, "Couldn't add %s to quadtree - out of node ent space", e->name);
root->ents[root->numEnts++] = e;
}
static void resizeQTEntCapacity(Quadtree *root)
{
int i, n;
Entity **ents;
n = root->capacity + QT_INITIAL_CAPACITY;
SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_DEBUG, "Resizing QT node: %d -> %d\n", root->capacity, n);
ents = malloc(sizeof(Entity*) * n);
memset(ents, 0, sizeof(Entity*) * n);
for (i = 0 ; i < root->capacity ; i++)
{
ents[i] = root->ents[i];
}
free(root->ents);
root->ents = ents;
root->capacity = n;
}
static int getIndex(Quadtree *root, int x, int y, int w, int h)
@ -184,7 +217,7 @@ static void removeEntity(Entity *e, Quadtree *root)
n = root->numEnts;
for (i = 0 ; i < QT_MAX_ENTS ; i++)
for (i = 0 ; i < root->capacity ; i++)
{
if (root->ents[i] == e)
{
@ -279,6 +312,8 @@ static void destroyQuadtreeNode(Quadtree *root)
{
destroyQuadtreeNode(root->node[i]);
free(root->node[i]->ents);
free(root->node[i]);
root->node[i] = NULL;

View File

@ -20,4 +20,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#include "../common.h"
#define QT_MAX_DEPTH 6
#define QT_MAX_CANDIDATES 256
#define QT_INITIAL_CAPACITY 8
extern Battle battle;

View File

@ -60,10 +60,6 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#define MAX_FIGHTER_GUNS 12
#define MAX_TARGET_RANGE 65536
#define QT_MAX_DEPTH 5
#define QT_MAX_ENTS 96
#define QT_MAX_CANDIDATES 256
#define BATTLE_AREA_CELLS 50
#define BATTLE_AREA_WIDTH (640 * BATTLE_AREA_CELLS)
#define BATTLE_AREA_HEIGHT (360 * BATTLE_AREA_CELLS)

View File

@ -267,7 +267,8 @@ struct StarSystem {
struct Quadtree {
int depth;
int x, y, w, h;
Entity *ents[QT_MAX_ENTS];
Entity **ents;
int capacity;
int numEnts;
Quadtree *node[4];
};