aboutsummaryrefslogtreecommitdiff
path: root/subprojects/d2tk/test/mock.c
blob: 95ef66685f894ba77d31e6bcc122a5fce13c5459 (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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
 * Copyright (c) 2018-2019 Hanspeter Portner (dev@open-music-kontrollers.ch)
 *
 * This is free software: you can redistribute it and/or modify
 * it under the terms of the Artistic License 2.0 as published by
 * The Perl Foundation.
 *
 * This source 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
 * Artistic License 2.0 for more details.
 *
 * You should have received a copy of the Artistic License 2.0
 * along the source as a COPYING file. If not, obtain it from
 * http://www.perlfoundation.org/artistic_license_2_0.
 */

#include <assert.h>
#include <stdlib.h>

#include "mock.h"

static inline void
_d2tk_mock_pre(void *data, d2tk_core_t *core, d2tk_coord_t w, d2tk_coord_t h,
	unsigned pass)
{
	d2tk_mock_ctx_t *ctx = data;
	assert(ctx);

	assert(core);
	assert(w == DIM_W);
	assert(h == DIM_H);
	assert(pass == 0);
}

static inline bool
_d2tk_mock_post(void *data, d2tk_core_t *core, d2tk_coord_t w, d2tk_coord_t h,
	unsigned pass)
{
	d2tk_mock_ctx_t *ctx = data;
	assert(ctx);

	assert(core);
	assert(w == DIM_W);
	assert(h == DIM_H);
	assert(pass == 0);

	assert(d2tk_core_get_pixels(core));

	return false; // do NOT enter 3rd pass
}

static inline void
_d2tk_mock_sprite_free(void *data, uint8_t type, uintptr_t body)
{
	d2tk_mock_ctx_t *ctx = data;
	assert(ctx);

	assert(type == 1);

	uint32_t *dummy = (uint32_t *)body;
	assert(*dummy == 1234);
	free(dummy);
}

static inline void
_d2tk_mock_process(void *data, d2tk_core_t *core, const d2tk_com_t *com,
	d2tk_coord_t xo, d2tk_coord_t yo,
	const d2tk_clip_t *clip __attribute__((unused)), unsigned pass)
{
	d2tk_mock_ctx_t *ctx = data;
	assert(ctx);

	assert(core);
	assert(com);
	assert(xo == CLIP_X);
	assert(yo == CLIP_Y);
	assert(pass == 0);

	assert(com->instr == D2TK_INSTR_BBOX);

	const d2tk_body_bbox_t *body = &com->body->bbox;
	assert(body->dirty == false);
	assert(body->cached == true);
	assert(body->hash != 0x0); //FIXME

	uintptr_t *sprite = d2tk_core_get_sprite(core, body->hash, 1);
	assert(sprite);
	assert(*sprite == 0);

	uint32_t *dummy = calloc(1, sizeof(uint32_t));
	assert(dummy);
	*dummy = 1234;

	*sprite = (uintptr_t)dummy;

	assert(sprite == d2tk_core_get_sprite(core, body->hash, 1));
	assert(sprite != d2tk_core_get_sprite(core, body->hash, 0));
	assert(sprite != d2tk_core_get_sprite(core, ~body->hash, 1));
	assert(sprite != d2tk_core_get_sprite(core, ~body->hash, 0));

	unsigned num = 0;
	D2TK_COM_FOREACH_CONST(com, bbox)
	{
		if(ctx->check)
		{
			ctx->check(bbox, &body->clip);
		}
		num += 1;
	}
	assert(num == 1);
}

static inline void
_d2tk_mock_process_triple(void *data, d2tk_core_t *core, const d2tk_com_t *com,
	d2tk_coord_t xo, d2tk_coord_t yo,
	const d2tk_clip_t *clip __attribute__((unused)), unsigned pass)
{
	d2tk_mock_ctx_t *ctx = data;
	assert(ctx);

	assert(core);
	assert(com);
	assert(xo == CLIP_X);
	assert(yo == CLIP_Y);
	assert(pass == 0);

	assert(com->instr == D2TK_INSTR_BBOX);

	const d2tk_body_bbox_t *body = &com->body->bbox;
	//assert(body->dirty == false);
	assert(body->cached == true);
	assert(body->hash != 0x0); //FIXME

	uintptr_t *sprite = d2tk_core_get_sprite(core, body->hash, 1);
	assert(sprite);

	if(*sprite == 0)
	{
		uint32_t *dummy = calloc(1, sizeof(uint32_t));
		assert(dummy);
		*dummy = 1234;

		*sprite = (uintptr_t)dummy;
	}

	assert(sprite == d2tk_core_get_sprite(core, body->hash, 1));
	assert(sprite != d2tk_core_get_sprite(core, body->hash, 0));
	assert(sprite != d2tk_core_get_sprite(core, ~body->hash, 1));
	assert(sprite != d2tk_core_get_sprite(core, ~body->hash, 0));

	unsigned num = 0;
	D2TK_COM_FOREACH_CONST(com, bbox)
	{
		if(ctx->check)
		{
			ctx->check(bbox, &body->clip);
		}
		num += 1;
	}
	assert(num == 1);
}

static inline void
_d2tk_mock_process_lazy(void *data, d2tk_core_t *core, const d2tk_com_t *com,
	d2tk_coord_t xo __attribute__((unused)), d2tk_coord_t yo __attribute__((unused)),
	const d2tk_clip_t *clip __attribute__((unused)), unsigned pass)
{
	d2tk_mock_ctx_t *ctx = data;
	assert(ctx);

	assert(core);
	assert(com);
	assert(pass == 0);

	assert(com->instr == D2TK_INSTR_BBOX);

	const d2tk_body_bbox_t *body = &com->body->bbox;
	assert(body->cached == true);
	assert(body->hash != 0x0); //FIXME

	uintptr_t *sprite = d2tk_core_get_sprite(core, body->hash, 1);
	assert(sprite);

	if(*sprite == 0)
	{
		uint32_t *dummy = calloc(1, sizeof(uint32_t));
		assert(dummy);
		*dummy = 1234;

		*sprite = (uintptr_t)dummy;
	}

	assert(sprite == d2tk_core_get_sprite(core, body->hash, 1));
	assert(sprite != d2tk_core_get_sprite(core, body->hash, 0));
	assert(sprite != d2tk_core_get_sprite(core, ~body->hash, 1));
	assert(sprite != d2tk_core_get_sprite(core, ~body->hash, 0));

	unsigned num = 0;
	D2TK_COM_FOREACH_CONST(com, bbox)
	{
		if(ctx->check)
		{
			ctx->check(bbox, &body->clip);
		}
		num += 1;
	}
	assert(num > 0);
}

const d2tk_core_driver_t d2tk_mock_driver = {
	.new = NULL,
	.free = NULL,
	.pre = _d2tk_mock_pre,
	.process = _d2tk_mock_process,
	.post = _d2tk_mock_post,
	.sprite_free = _d2tk_mock_sprite_free
};

const d2tk_core_driver_t d2tk_mock_driver_triple = {
	.new = NULL,
	.free = NULL,
	.pre = _d2tk_mock_pre,
	.process = _d2tk_mock_process_triple,
	.post = _d2tk_mock_post,
	.sprite_free = _d2tk_mock_sprite_free
};

const d2tk_core_driver_t d2tk_mock_driver_lazy = {
	.new = NULL,
	.free = NULL,
	.pre = _d2tk_mock_pre,
	.process = _d2tk_mock_process_lazy,
	.post = _d2tk_mock_post,
	.sprite_free = _d2tk_mock_sprite_free
};