summaryrefslogtreecommitdiffstats
path: root/crawl-ref/source/tilemcache.h
blob: 36db9552e7ffd0f7e42284301651b9511e3b0264 (plain) (blame)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * @file
 * @brief Monster cache support
**/

#ifdef USE_TILE
#ifndef TILEMCACHE_H
#define TILEMCACHE_H

#include <vector>

struct dolls_data;

// The monster cache is designed to hold extra information about monsters that
// can't be contained in a single tile.  This is usually for equipment,
// doll parts, or demon parts.
//
// Monster cache entries for monsters that are out of sight are ref-counted
// that they can be drawn even if that monster no longer exists.  When no
// out-of-sight tiles refer to them, they can be deleted.

class tile_draw_info
{
public:
    tile_draw_info() : idx(~0), ofs_x(0), ofs_y(0) {}

    void set(tileidx_t _idx, int _ofs_x = 0, int _ofs_y = 0)
        { idx = _idx; ofs_x = _ofs_x; ofs_y = _ofs_y; }

    tileidx_t idx;
    int ofs_x;
    int ofs_y;
};

class mcache_entry
{
public:
    mcache_entry() : m_ref_count(0) {}
    virtual ~mcache_entry() {}

    void inc_ref() { m_ref_count++; }
    void dec_ref() { m_ref_count--; if (m_ref_count < 0) m_ref_count = 0; }
    int ref_count() { return m_ref_count; }

    enum
    {
        // The maximum number of values written in the info function.
        MAX_INFO_COUNT = 4
    };

    virtual int info(tile_draw_info *dinfo) const { return 0; }
    virtual const dolls_data *doll() const { return NULL; }

    virtual bool transparent() const { return false; }

protected:

    // ref count in backstore
    int m_ref_count;
};

class mcache_manager
{
public:
    ~mcache_manager();

    unsigned int register_monster(const monster_info& mon);
    mcache_entry *get(tileidx_t idx);

    void clear_nonref();
    void clear_all();

    bool empty() { return m_entries.empty(); }

protected:
    vector<mcache_entry*> m_entries;
};

// The global monster cache.
extern mcache_manager mcache;

#endif
#endif