summaryrefslogtreecommitdiff
path: root/newgrf_spritegroup.c
blob: b796c7946d8498de9aaa52594d7ad7c6660cc3b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/* $Id$ */

#include "stdafx.h"
#include "openttd.h"
#include "pool.h"
#include "sprite.h"

enum {
	SPRITEGROUP_POOL_BLOCK_SIZE_BITS = 4, /* (1 << 4) == 16 items */
	SPRITEGROUP_POOL_MAX_BLOCKS      = 8000,
};

static uint _spritegroup_count = 0;
static MemoryPool _spritegroup_pool;


static void SpriteGroupPoolCleanBlock(uint start_item, uint end_item)
{
	uint i;

	for (i = start_item; i <= end_item; i++) {
		SpriteGroup *group = (SpriteGroup*)GetItemFromPool(&_spritegroup_pool, i);

		/* Free dynamically allocated memory */
		switch (group->type) {
			case SGT_REAL:
				free(group->g.real.loaded);
				free(group->g.real.loading);
				break;

			case SGT_DETERMINISTIC:
				free(group->g.determ.adjusts);
				free(group->g.determ.ranges);
				break;

			case SGT_RANDOMIZED:
				free(group->g.random.groups);
				break;

			default:
				break;
		}
	}
}


/* Initialize the SpriteGroup pool */
static MemoryPool _spritegroup_pool = { "SpriteGr", SPRITEGROUP_POOL_MAX_BLOCKS, SPRITEGROUP_POOL_BLOCK_SIZE_BITS, sizeof(SpriteGroup), NULL, &SpriteGroupPoolCleanBlock, 0, 0, NULL };


/* Allocate a new SpriteGroup */
SpriteGroup *AllocateSpriteGroup(void)
{
	/* This is totally different to the other pool allocators, as we never remove an item from the pool. */
	if (_spritegroup_count == _spritegroup_pool.total_items) {
		if (!AddBlockToPool(&_spritegroup_pool)) return NULL;
	}

	return (SpriteGroup*)GetItemFromPool(&_spritegroup_pool, _spritegroup_count++);
}


void InitializeSpriteGroupPool(void)
{
	CleanPool(&_spritegroup_pool);
	AddBlockToPool(&_spritegroup_pool);

	_spritegroup_count = 0;
}