summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonAki <konaki@anokonaki.org>2026-10-14 12:24:19 +0800
committerKonAki <konaki@anokonaki.org>2026-10-14 12:24:19 +0800
commit5fbcc854f47b6fa14abeb76434cea4ef0c713e8f (patch)
tree24606445b40d1d2ab35aac6a3d0d75509becd773
parent405945827d88e3ef8517fc73382f7357608ad750 (diff)
add picture module: RPG Maker style pictures
-rw-r--r--kon/konpicture.c264
-rw-r--r--kon/konpicture.h96
2 files changed, 360 insertions, 0 deletions
diff --git a/kon/konpicture.c b/kon/konpicture.c
new file mode 100644
index 0000000..ac4f91b
--- /dev/null
+++ b/kon/konpicture.c
@@ -0,0 +1,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);
+ }
+}
diff --git a/kon/konpicture.h b/kon/konpicture.h
new file mode 100644
index 0000000..0550dd8
--- /dev/null
+++ b/kon/konpicture.h
@@ -0,0 +1,96 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * konpicture.h
+ *
+ * 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.
+ */
+
+#ifndef KONPICTURE_H
+#define KONPICTURE_H
+
+#include "konsofren.h"
+#include "kon2d.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/*** definition ***/
+
+/* pictures drawn over the screen that can be moved, faded, turned and tinted, the way RPG Maker has them: a number of slots
+ drawn in order so a higher one is in front, each a picture with a place, a scale in percent, an opacity from 0 to 255,
+ a blend, an angle and a tone. all the times are in frames at 60 a second, like RPG Maker counts them.
+ a number that isn't a slot does nothing, so a bad script can't write outside of them */
+
+#define KON_PICTURES 100
+#define KON_PICTURE_NAME 64
+
+/* gives the image for a name, or NULL. the pictures do not own what it gives, whoever made the image keeps it until the picture is
+ erased or replaced */
+typedef const kon_image *(*kon_pictureLoad)(const char *name, void *user);
+
+typedef struct kon_picture {
+ int shown;
+ char name[KON_PICTURE_NAME];
+ const kon_image *image;
+ int origin; /* 0 top left, 1 middle */
+ int blend; /* a kon_blend_t: 0 normal, 1 add, 2 multiply, 3 screen */
+ kon_tween_t x, y, scaleX, scaleY, opacity;
+ kon_tween_t tone[4]; /* red, green, blue added from -255 to 255 and gray from 0 to 255 */
+ float angle; /* radians */
+ float turn; /* RPG Maker's rotation speed */
+ kon_image *toned; /* a copy with the tone in it, made when there is one to apply */
+ int tonedFor[4];
+} kon_picture_t;
+
+typedef struct kon_pictures {
+ kon_picture_t slot[KON_PICTURES];
+ kon_pictureLoad load;
+ void *user;
+} kon_pictures_t;
+
+/*** pictures ***/
+
+void kon_picturesInit(kon_pictures_t *pictures, kon_pictureLoad load, void *user);
+void kon_picturesFree(kon_pictures_t *pictures);
+
+/* id is 1 to KON_PICTURES. scale is a percent. this puts everything back as given, a picture that is already there is replaced */
+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);
+
+/* goes to the new values over frames, with easing 0 constant, 1 slow start, 2 slow end, 3 slow both. 0 frames is at once */
+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);
+
+/* turns clockwise, speed is RPG Maker's: half a degree a frame for each 1. 0 stops it */
+void kon_pictureRotate(kon_pictures_t *pictures, int id, int speed);
+void kon_pictureTint(kon_pictures_t *pictures, int id, int red, int green, int blue, int gray, int frames);
+void kon_pictureErase(kon_pictures_t *pictures, int id);
+
+void kon_picturesUpdate(kon_pictures_t *pictures, float dt);
+/* draws the shown ones in order, a made copy of a toned picture is kept in the slot, which is why this changes the pictures */
+void kon_picturesDraw(kon_pictures_t *pictures, kon_framebuffer_t *fb);
+
+/* 1 while any picture is still moving or changing tone, what a "wait" in a script waits for */
+int kon_picturesBusy(const kon_pictures_t *pictures);
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* KONPICTURE_H */