summaryrefslogtreecommitdiff
path: root/src/uqm/displist.h
blob: d9c2fc0ba77136bb444a6fdc4750b6226c40ced2 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//Copyright Paul Reiche, Fred Ford. 1992-2002

/*
 *  This program 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 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program 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.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef UQM_DISPLIST_H_
#define UQM_DISPLIST_H_

#include <assert.h>
#include "port.h"
#include "libs/compiler.h"
#include "libs/memlib.h"

#if defined(__cplusplus)
extern "C" {
#endif

// Note that we MUST use the QUEUE_TABLE variant at this time, because
// certain gameplay elements depend on it. Namely, the maximum number
// of HyperSpace encounter globes chasing the player is defined by the
// allocated size of the encounter_q. If switched to non-table variant,
// the max number of encounters will be virtually unlimited the way the
// code works now.
#define QUEUE_TABLE

typedef void* QUEUE_HANDLE;

typedef UWORD OBJ_SIZE;
typedef QUEUE_HANDLE HLINK;

typedef struct link
{
	// Every queue element of any queue must have these
	// two as the first members
	HLINK pred;
	HLINK succ;
} LINK;

typedef struct /* queue */
{
	HLINK head;
	HLINK tail;
#ifdef QUEUE_TABLE
	BYTE  *pq_tab;
	HLINK free_list;
#endif
	COUNT object_size;
#ifdef QUEUE_TABLE
	BYTE num_objects;
#endif /* QUEUE_TABLE */
} QUEUE;

#ifdef QUEUE_TABLE

extern HLINK AllocLink (QUEUE *pq);
extern void FreeLink (QUEUE *pq, HLINK hLink);

static inline LINK *
LockLink (const QUEUE *pq, HLINK h)
{
	if (h) // Apparently, h==0 is OK
	{	// Make sure the link is actually in our queue!
		assert (pq->pq_tab && (BYTE*)h >= pq->pq_tab &&
				(BYTE*)h < pq->pq_tab + pq->object_size * pq->num_objects);
	}
	return (LINK*)h;
}

static inline void
UnlockLink (const QUEUE *pq, HLINK h)
{
	if (h) // Apparently, h==0 is OK
	{	// Make sure the link is actually in our queue!
		assert (pq->pq_tab && (BYTE*)h >= pq->pq_tab &&
				(BYTE*)h < pq->pq_tab + pq->object_size * pq->num_objects);
	}
}

#define GetFreeList(pq) (pq)->free_list
#define SetFreeList(pq, h) (pq)->free_list = (h)
#define AllocQueueTab(pq,n) \
		((pq)->pq_tab = HMalloc (((COUNT)(pq)->object_size * \
		(COUNT)((pq)->num_objects = (BYTE)(n)))))
#define FreeQueueTab(pq) HFree ((pq)->pq_tab); (pq)->pq_tab = NULL
#define SizeQueueTab(pq) (COUNT)((pq)->num_objects)
#define GetLinkAddr(pq,i) (HLINK)((pq)->pq_tab + ((pq)->object_size * ((i) - 1)))
#else /* !QUEUE_TABLE */
#define AllocLink(pq)     (HLINK)HMalloc ((pq)->object_size)
#define LockLink(pq, h)   ((LINK*)(h))
#define UnlockLink(pq, h) ((void)(h))
#define FreeLink(pq,h)    HFree (h)
#endif /* QUEUE_TABLE */

#define SetLinkSize(pq,s) ((pq)->object_size = (COUNT)(s))
#define GetLinkSize(pq) (COUNT)((pq)->object_size)
#define GetHeadLink(pq) ((pq)->head)
#define SetHeadLink(pq,h) ((pq)->head = (h))
#define GetTailLink(pq) ((pq)->tail)
#define SetTailLink(pq,h) ((pq)->tail = (h))
#define _GetPredLink(lpE) ((lpE)->pred)
#define _SetPredLink(lpE,h) ((lpE)->pred = (h))
#define _GetSuccLink(lpE) ((lpE)->succ)
#define _SetSuccLink(lpE,h) ((lpE)->succ = (h))

extern BOOLEAN InitQueue (QUEUE *pq, COUNT num_elements, OBJ_SIZE size);
extern BOOLEAN UninitQueue (QUEUE *pq);
extern void ReinitQueue (QUEUE *pq);
extern void PutQueue (QUEUE *pq, HLINK hLink);
extern void InsertQueue (QUEUE *pq, HLINK hLink, HLINK hRefLink);
extern void RemoveQueue (QUEUE *pq, HLINK hLink);
extern COUNT CountLinks (QUEUE *pq);
void ForAllLinks(QUEUE *pq, void (*callback)(LINK *, void *), void *arg);

#if defined(__cplusplus)
}
#endif

#endif /* UQM_DISPLIST_H_ */