summaryrefslogtreecommitdiff
path: root/kon/konstage.c
blob: 1b958be39745b874855ed9b4886edab8797e8d18 (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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/* SPDX-License-Identifier: MIT */
/*
 * konstage.c
 *
 * Copyright (c) 2026 KonAki
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include "konstage.h"

#include <math.h>
#include <string.h>

static int valid(const kon_stage_t *stage, int index)
{
	return index >= 0 && index < stage->capacity && stage->sprites[index].used;
}

static uint32_t tintOf(float alpha, float brightness)
{
	uint32_t a, b;

	if (!(alpha > 0.0f)) alpha = 0.0f;
	if (alpha > 1.0f) alpha = 1.0f;
	if (!(brightness > 0.0f)) brightness = 0.0f;
	if (brightness > 1.0f) brightness = 1.0f;
	a = (uint32_t)(alpha * 255.0f + 0.5f);
	b = (uint32_t)(brightness * 255.0f + 0.5f);
	return (a << 24) | (b << 16) | (b << 8) | b;
}

static kon_rect_t wholeOf(const kon_image *image, const kon_rect_t *src)
{
	kon_rect_t whole = {0, 0, 0, 0};

	if (!image) return whole;
	if (src && src->w > 0 && src->h > 0) return *src;
	whole.w = image->width;
	whole.h = image->height;
	return whole;
}

/*** stage ***/

static void resetView(kon_stageBackground_t *back)
{
	kon_tweenSet(&back->zoom, 1.0f);
	kon_tweenSet(&back->focusX, 0.5f);
	kon_tweenSet(&back->focusY, 0.5f);
}

void kon_stageInit(kon_stage_t *stage, kon_stageSprite_t *sprites, int capacity, int width, int height)
{
	kon_stage_t zero = {0};
	int i;

	*stage = zero;
	stage->width = width;
	stage->height = height;
	stage->sprites = sprites;
	stage->capacity = capacity > 0 ? capacity : 0;
	for (i = 0; i < stage->capacity; i++) memset(&sprites[i], 0, sizeof sprites[i]);
	stage->slotX[KON_SLOT_FAR_LEFT] = 0.12f;
	stage->slotX[KON_SLOT_LEFT] = 0.3f;
	stage->slotX[KON_SLOT_CENTER] = 0.5f;
	stage->slotX[KON_SLOT_RIGHT] = 0.7f;
	stage->slotX[KON_SLOT_FAR_RIGHT] = 0.88f;
	stage->filter = KON_FILTER_NEAREST;
	kon_tweenSet(&stage->back.alpha, 1.0f);
	kon_tweenSet(&stage->old.alpha, 1.0f);
	resetView(&stage->back);
	resetView(&stage->old);
}

void kon_stageBackground(kon_stage_t *stage, const kon_image *image, kon_fit_t fit, float seconds)
{
	if (seconds > 0.0f && stage->back.image && image) {
		stage->old = stage->back;
		kon_tweenSet(&stage->old.alpha, 1.0f);
		stage->back.image = image;
		stage->back.fit = fit;
		resetView(&stage->back);
		kon_tweenStart(&stage->back.alpha, 0.0f, 1.0f, seconds, KON_EASE_LINEAR);
		return;
	}
	stage->old.image = NULL;
	stage->back.image = image;
	stage->back.fit = fit;
	resetView(&stage->back);
	if (seconds > 0.0f && image) kon_tweenStart(&stage->back.alpha, 0.0f, 1.0f, seconds, KON_EASE_LINEAR);
	else kon_tweenSet(&stage->back.alpha, 1.0f);
}

void kon_stageBackgroundView(kon_stage_t *stage, float zoom, float focusX, float focusY, float seconds, kon_ease_t ease)
{
	kon_tweenTo(&stage->back.zoom, zoom, seconds, ease);
	kon_tweenTo(&stage->back.focusX, focusX, seconds, ease);
	kon_tweenTo(&stage->back.focusY, focusY, seconds, ease);
}

int kon_stageAdd(kon_stage_t *stage, const kon_image *image, const kon_rect_t *src, kon_layer_t layer)
{
	int i;

	for (i = 0; i < stage->capacity; i++) {
		kon_stageSprite_t *s = &stage->sprites[i];
		int p;

		if (s->used) continue;
		memset(s, 0, sizeof *s);
		s->used = 1;
		s->image = image;
		s->src = wholeOf(image, src);
		s->anchorX = 0.5f;
		s->anchorY = 1.0f;
		s->layer = layer;
		for (p = 0; p < KON_PROP_COUNT; p++) kon_tweenSet(&s->prop[p], 0.0f);
		kon_tweenSet(&s->prop[KON_PROP_SCALE], 1.0f);
		kon_tweenSet(&s->prop[KON_PROP_BRIGHTNESS], 1.0f);
		return i;
	}
	return -1;
}

void kon_stageRemove(kon_stage_t *stage, int index)
{
	if (valid(stage, index)) stage->sprites[index].used = 0;
}

void kon_stageLayer(kon_stage_t *stage, int index, kon_layer_t layer)
{
	if (valid(stage, index)) stage->sprites[index].layer = layer;
}

void kon_stageFrame(kon_stage_t *stage, int index, const kon_image *image, const kon_rect_t *src)
{
	if (!valid(stage, index)) return;
	stage->sprites[index].image = image;
	stage->sprites[index].src = wholeOf(image, src);
}

void kon_stageTween(kon_stage_t *stage, int index, kon_prop_t prop, float to, float seconds, kon_ease_t ease)
{
	if ((int)prop < 0 || prop >= KON_PROP_COUNT || !valid(stage, index)) return;
	kon_tweenTo(&stage->sprites[index].prop[prop], to, seconds, ease);
}

void kon_stageMove(kon_stage_t *stage, int index, float x, float y, float seconds, kon_ease_t ease)
{
	kon_stageTween(stage, index, KON_PROP_X, x, seconds, ease);
	kon_stageTween(stage, index, KON_PROP_Y, y, seconds, ease);
}

void kon_stageFade(kon_stage_t *stage, int index, float alpha, float seconds, kon_ease_t ease)
{
	kon_stageTween(stage, index, KON_PROP_ALPHA, alpha, seconds, ease);
}

void kon_stagePlace(kon_stage_t *stage, int index, kon_slot_t slot, float seconds, kon_ease_t ease)
{
	if ((int)slot < 0 || slot >= KON_SLOT_COUNT) return;
	kon_stageMove(stage, index, stage->slotX[slot] * (float)stage->width, (float)stage->height, seconds, ease);
}

void kon_stageHide(kon_stage_t *stage, int index, float seconds)
{
	if (!valid(stage, index)) return;
	kon_stageFade(stage, index, 0.0f, seconds, KON_EASE_LINEAR);
	stage->sprites[index].hiding = 1;
}

void kon_stageUpdate(kon_stage_t *stage, float dt)
{
	int i, p;

	kon_tweenStep(&stage->back.alpha, dt);
	kon_tweenStep(&stage->back.zoom, dt);
	kon_tweenStep(&stage->back.focusX, dt);
	kon_tweenStep(&stage->back.focusY, dt);
	if (stage->old.image && kon_tweenDone(&stage->back.alpha)) stage->old.image = NULL;

	for (i = 0; i < stage->capacity; i++) {
		kon_stageSprite_t *s = &stage->sprites[i];

		if (!s->used) continue;
		for (p = 0; p < KON_PROP_COUNT; p++) kon_tweenStep(&s->prop[p], dt);
		if (s->hiding && kon_tweenDone(&s->prop[KON_PROP_ALPHA]) && kon_tweenValue(&s->prop[KON_PROP_ALPHA]) <= 0.0f) s->used = 0;
	}
}

int kon_stageBusy(const kon_stage_t *stage)
{
	int i, p;

	if (stage->old.image || !kon_tweenDone(&stage->back.alpha)) return 1;
	if (!kon_tweenDone(&stage->back.zoom) || !kon_tweenDone(&stage->back.focusX) || !kon_tweenDone(&stage->back.focusY)) return 1;
	for (i = 0; i < stage->capacity; i++) {
		if (!stage->sprites[i].used) continue;
		for (p = 0; p < KON_PROP_COUNT; p++) {
			if (!kon_tweenDone(&stage->sprites[i].prop[p])) return 1;
		}
	}
	return 0;
}

/*** drawing ***/

static float unit(float v)
{
	if (!(v >= 0.0f)) return v < 0.0f ? 0.0f : 0.5f;
	return v > 1.0f ? 1.0f : v;
}

/* where the picture starts along one axis. it is centred when it is smaller than the view, otherwise the focus is put in the middle and
   the picture is kept over the whole view */
static float startOf(float view, float size, float focus)
{
	float start;

	if (size < view) return (view - size) * 0.5f;
	start = view * 0.5f - focus * size;
	if (start > 0.0f) start = 0.0f;
	if (start < view - size) start = view - size;
	return start;
}

static void drawBackground(const kon_stage_t *stage, kon_framebuffer_t *fb, const kon_stageBackground_t *back)
{
	kon_rect_t src;
	float sx, sy, zoom, x, y, viewW = (float)stage->width, viewH = (float)stage->height;
	uint32_t tint;

	if (!back->image || back->fit == KON_FIT_NONE) return;
	src = wholeOf(back->image, NULL);
	if (src.w <= 0 || src.h <= 0 || stage->width <= 0 || stage->height <= 0) return;
	zoom = kon_tweenValue(&back->zoom);
	if (!(zoom > 0.0f)) return;

	sx = viewW / (float)src.w;
	sy = viewH / (float)src.h;
	switch (back->fit) {
	case KON_FIT_COVER: if (sx < sy) sx = sy; else sy = sx; break;
	case KON_FIT_CONTAIN: if (sx > sy) sx = sy; else sy = sx; break;
	case KON_FIT_CENTER: sx = sy = 1.0f; break;
	case KON_FIT_STRETCH:
	case KON_FIT_NONE: break;
	}
	sx *= zoom;
	sy *= zoom;
	x = startOf(viewW, (float)src.w * sx, unit(kon_tweenValue(&back->focusX)));
	y = startOf(viewH, (float)src.h * sy, unit(kon_tweenValue(&back->focusY)));
	if (back->fit == KON_FIT_CENTER && zoom == 1.0f) {
		x = floorf(x);
		y = floorf(y);
	}
	tint = tintOf(kon_tweenValue(&back->alpha), 1.0f);
	kon_drawImageTint(fb, back->image, src, x, y, 0.0f, 0.0f, sx, sy, 0.0f, stage->filter, tint);
}

void kon_stageDrawBackground(const kon_stage_t *stage, kon_framebuffer_t *fb)
{
	drawBackground(stage, fb, &stage->old);
	drawBackground(stage, fb, &stage->back);
}

/* sprites go out in order of z, then of index. each pass picks the next one after the last drawn, so nothing is sorted or allocated */
void kon_stageDraw(const kon_stage_t *stage, kon_framebuffer_t *fb, kon_layer_t layer)
{
	int lastZ = 0, lastIndex = -1, first = 1;

	for (;;) {
		int i, pick = -1;

		for (i = 0; i < stage->capacity; i++) {
			const kon_stageSprite_t *s = &stage->sprites[i];

			if (!s->used || s->layer != layer) continue;
			if (!first && (s->z < lastZ || (s->z == lastZ && i <= lastIndex))) continue;
			if (pick < 0 || s->z < stage->sprites[pick].z) pick = i;
		}
		if (pick < 0) return;

		{
			const kon_stageSprite_t *s = &stage->sprites[pick];
			float scale = kon_tweenValue(&s->prop[KON_PROP_SCALE]);
			float sx = s->flip ? -scale : scale;
			float ox = s->anchorX * (float)s->src.w, oy = s->anchorY * (float)s->src.h;

			if (s->flip) ox = (float)s->src.w - ox;
			if (s->image && scale > 0.0f) {
				kon_drawImageTint(fb, s->image, s->src, kon_tweenValue(&s->prop[KON_PROP_X]), kon_tweenValue(&s->prop[KON_PROP_Y]),
				    ox, oy, sx, scale, 0.0f, stage->filter,
				    tintOf(kon_tweenValue(&s->prop[KON_PROP_ALPHA]), kon_tweenValue(&s->prop[KON_PROP_BRIGHTNESS])));
			}
			lastZ = s->z;
			lastIndex = pick;
			first = 0;
		}
	}
}