summaryrefslogtreecommitdiff
path: root/kon/konpicture.c
blob: ac4f91bb8c3efea05ffd343f2fdf24d789e9941d (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
/* SPDX-License-Identifier: MIT */
/*
 * konpicture.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 "konpicture.h"

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

#define FRAME (1.0f / 60.0f)

/*** helpers ***/

static kon_picture_t *slot(kon_pictures_t *pictures, int id)
{
	if (!pictures || id < 1 || id > KON_PICTURES) return NULL;
	return &pictures->slot[id - 1];
}

static float clampf(float v, float low, float high)
{
	if (!(v >= low)) return low;
	if (v > high) return high;
	return v;
}

static kon_ease_t easeFor(int easing)
{
	switch (easing) {
	case 1: return KON_EASE_IN;
	case 2: return KON_EASE_OUT;
	case 3: return KON_EASE_INOUT;
	default: return KON_EASE_LINEAR;
	}
}

static void glide(kon_tween_t *tween, float to, int frames, int easing)
{
	if (frames <= 0) kon_tweenSet(tween, to);
	else kon_tweenStart(tween, kon_tweenValue(tween), to, (float)frames * FRAME, easeFor(easing));
}

static void dropToned(kon_picture_t *p)
{
	if (p->toned) kon_freeImage(p->toned);
	p->toned = NULL;
}

/*** pictures ***/

void kon_picturesInit(kon_pictures_t *pictures, kon_pictureLoad load, void *user)
{
	memset(pictures, 0, sizeof *pictures);
	pictures->load = load;
	pictures->user = user;
}

void kon_picturesFree(kon_pictures_t *pictures)
{
	int i;

	if (!pictures) return;
	for (i = 0; i < KON_PICTURES; i++) dropToned(&pictures->slot[i]);
	memset(pictures->slot, 0, sizeof pictures->slot);
}

void kon_pictureShow(kon_pictures_t *pictures, int id, const char *name, int origin, int x, int y, int scaleX, int scaleY, int opacity, int blend)
{
	kon_picture_t *p = slot(pictures, id);
	size_t n;

	if (!p) return;
	dropToned(p);
	memset(p, 0, sizeof *p);
	p->shown = 1;
	if (!name) name = "";
	n = strlen(name);
	if (n >= KON_PICTURE_NAME) n = KON_PICTURE_NAME - 1;
	memcpy(p->name, name, n);
	p->name[n] = '\0';
	p->image = pictures->load && p->name[0] ? pictures->load(p->name, pictures->user) : NULL;
	p->origin = origin == 1 ? 1 : 0;
	p->blend = blend >= 0 && blend <= 3 ? blend : 0;
	kon_tweenSet(&p->x, (float)x);
	kon_tweenSet(&p->y, (float)y);
	kon_tweenSet(&p->scaleX, (float)scaleX);
	kon_tweenSet(&p->scaleY, (float)scaleY);
	kon_tweenSet(&p->opacity, clampf((float)opacity, 0.0f, 255.0f));
}

void kon_pictureMove(kon_pictures_t *pictures, int id, int origin, int x, int y, int scaleX, int scaleY, int opacity, int blend, int frames, int easing)
{
	kon_picture_t *p = slot(pictures, id);

	if (!p || !p->shown) return;
	p->origin = origin == 1 ? 1 : 0;
	p->blend = blend >= 0 && blend <= 3 ? blend : 0;
	glide(&p->x, (float)x, frames, easing);
	glide(&p->y, (float)y, frames, easing);
	glide(&p->scaleX, (float)scaleX, frames, easing);
	glide(&p->scaleY, (float)scaleY, frames, easing);
	glide(&p->opacity, clampf((float)opacity, 0.0f, 255.0f), frames, easing);
}

void kon_pictureRotate(kon_pictures_t *pictures, int id, int speed)
{
	kon_picture_t *p = slot(pictures, id);

	if (!p || !p->shown) return;
	p->turn = (float)speed;
}

void kon_pictureTint(kon_pictures_t *pictures, int id, int red, int green, int blue, int gray, int frames)
{
	kon_picture_t *p = slot(pictures, id);

	if (!p || !p->shown) return;
	glide(&p->tone[0], clampf((float)red, -255.0f, 255.0f), frames, 0);
	glide(&p->tone[1], clampf((float)green, -255.0f, 255.0f), frames, 0);
	glide(&p->tone[2], clampf((float)blue, -255.0f, 255.0f), frames, 0);
	glide(&p->tone[3], clampf((float)gray, 0.0f, 255.0f), frames, 0);
}

void kon_pictureErase(kon_pictures_t *pictures, int id)
{
	kon_picture_t *p = slot(pictures, id);

	if (!p) return;
	dropToned(p);
	memset(p, 0, sizeof *p);
}

/*** time ***/

void kon_picturesUpdate(kon_pictures_t *pictures, float dt)
{
	int i, k;

	if (!pictures || !(dt > 0.0f)) return;
	if (dt > 0.25f) dt = 0.25f;
	for (i = 0; i < KON_PICTURES; i++) {
		kon_picture_t *p = &pictures->slot[i];

		if (!p->shown) continue;
		kon_tweenStep(&p->x, dt);
		kon_tweenStep(&p->y, dt);
		kon_tweenStep(&p->scaleX, dt);
		kon_tweenStep(&p->scaleY, dt);
		kon_tweenStep(&p->opacity, dt);
		for (k = 0; k < 4; k++) kon_tweenStep(&p->tone[k], dt);
		if (p->turn != 0.0f) {
			p->angle += p->turn * 0.5f * (dt / FRAME) * (3.14159265f / 180.0f);
			if (p->angle > 6.2831853f || p->angle < -6.2831853f) p->angle = fmodf(p->angle, 6.2831853f);
		}
	}
}

int kon_picturesBusy(const kon_pictures_t *pictures)
{
	int i, k;

	if (!pictures) return 0;
	for (i = 0; i < KON_PICTURES; i++) {
		const kon_picture_t *p = &pictures->slot[i];

		if (!p->shown) continue;
		if (!kon_tweenDone(&p->x) || !kon_tweenDone(&p->y) || !kon_tweenDone(&p->scaleX) || !kon_tweenDone(&p->scaleY) || !kon_tweenDone(&p->opacity)) return 1;
		for (k = 0; k < 4; k++) {
			if (!kon_tweenDone(&p->tone[k])) return 1;
		}
	}
	return 0;
}

/*** drawing ***/

static int channel(int v)
{
	return v < 0 ? 0 : v > 255 ? 255 : v;
}

/* the copy of the picture with its tone in it, made again only when the tone has changed */
static const kon_image *tonedImage(kon_picture_t *p)
{
	int tone[4], k, i, count;

	for (k = 0; k < 4; k++) tone[k] = (int)floorf(kon_tweenValue(&p->tone[k]) + 0.5f);
	if (!tone[0] && !tone[1] && !tone[2] && !tone[3]) {
		dropToned(p);
		return p->image;
	}
	if (p->toned && !memcmp(tone, p->tonedFor, sizeof tone) && p->toned->width == p->image->width && p->toned->height == p->image->height) return p->toned;

	if (!p->toned || p->toned->width != p->image->width || p->toned->height != p->image->height) {
		dropToned(p);
		p->toned = kon_createFramebuffer(p->image->width, p->image->height);
		if (!p->toned) return p->image;
	}
	count = p->image->width * p->image->height;
	for (i = 0; i < count; i++) {
		uint32_t c = p->image->data[i];
		int r = (int)((c >> 16) & 255), g = (int)((c >> 8) & 255), b = (int)(c & 255);

		if (tone[3]) {
			int gray = (r * 30 + g * 59 + b * 11) / 100;

			r += (gray - r) * tone[3] / 255;
			g += (gray - g) * tone[3] / 255;
			b += (gray - b) * tone[3] / 255;
		}
		p->toned->data[i] = (c & 0xFF000000u) | (uint32_t)channel(r + tone[0]) << 16 | (uint32_t)channel(g + tone[1]) << 8 | (uint32_t)channel(b + tone[2]);
	}
	memcpy(p->tonedFor, tone, sizeof tone);
	return p->toned;
}

void kon_picturesDraw(kon_pictures_t *pictures, kon_framebuffer_t *fb)
{
	int i;

	if (!pictures || !fb) return;
	for (i = 0; i < KON_PICTURES; i++) {
		kon_picture_t *p = &pictures->slot[i];
		const kon_image *image;
		float sx, sy, alpha;
		kon_rect_t whole;

		if (!p->shown || !p->image || p->image->width <= 0 || p->image->height <= 0) continue;
		alpha = clampf(kon_tweenValue(&p->opacity), 0.0f, 255.0f);
		if (alpha < 0.5f) continue;
		sx = kon_tweenValue(&p->scaleX) / 100.0f;
		sy = kon_tweenValue(&p->scaleY) / 100.0f;
		image = tonedImage(p);
		whole.x = whole.y = 0;
		whole.w = image->width;
		whole.h = image->height;
		kon_drawImageBlend(fb, image, whole, kon_tweenValue(&p->x), kon_tweenValue(&p->y),
			p->origin ? (float)image->width * 0.5f : 0.0f, p->origin ? (float)image->height * 0.5f : 0.0f,
			sx, sy, p->angle, sx == 1.0f && sy == 1.0f && p->angle == 0.0f ? KON_FILTER_NEAREST : KON_FILTER_LINEAR,
			((uint32_t)alpha << 24) | 0x00FFFFFFu, (kon_blend_t)p->blend);
	}
}