summaryrefslogtreecommitdiff
path: root/kon/kon2d.h
blob: badaed0fb27fb6f07ca3dc1a14cbff0250915298 (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
/* SPDX-License-Identifier: MIT */
/*
 * kon2d.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 KON2D_H
#define KON2D_H

#include "konsofren.h"

/*** camera ***/

/* x, y is the map pixel at the top left of the view. bounds is the size of the map in pixels, the view never leaves it
   (and a map smaller than the view is centred) */
typedef struct kon_camera2 {
	float x, y;
	int viewWidth, viewHeight;
	float boundsWidth, boundsHeight;
	float stiffness; /* how quickly it catches up, around 6 to 12 feels right, 0 or less follows exactly */
} kon_camera2_t;

void kon_camera2Init(kon_camera2_t *cam, int viewWidth, int viewHeight, float boundsWidth, float boundsHeight);

/* puts the target in the middle of the view at once */
void kon_camera2Snap(kon_camera2_t *cam, float targetX, float targetY);

/* eases towards having the target in the middle */
void kon_camera2Follow(kon_camera2_t *cam, float targetX, float targetY, float dt);

/* whole pixels, what to pass to kon_drawTilemap so nothing shimmers */
int kon_camera2X(const kon_camera2_t *cam);
int kon_camera2Y(const kon_camera2_t *cam);

/*** animation ***/

/* plays count frames starting at frame first of a sheet at fps frames a second */
typedef struct kon_anim {
	int first, count;
	float fps;
	int loop;
	float time;
} kon_anim_t;

/* starts over, unless it is already playing the same thing */
void kon_animPlay(kon_anim_t *anim, int first, int count, float fps, int loop);
void kon_animStep(kon_anim_t *anim, float dt);

/* the frame to draw, the last one when a non looping animation has finished */
int kon_animFrame(const kon_anim_t *anim);
int kon_animDone(const kon_anim_t *anim);

/* draws frame index of a sheet of frameWidth by frameHeight frames with its top left at x, y */
void kon_drawFrame(kon_framebuffer_t *fb, const kon_image *sheet, int frameWidth, int frameHeight, int index, int x, int y, int flip);

/*** tweens ***/

typedef enum kon_ease {
	KON_EASE_LINEAR,
	KON_EASE_IN, /* starts slow */
	KON_EASE_OUT, /* ends slow */
	KON_EASE_INOUT
} kon_ease_t;

/* a number that glides from one value to another. start one with kon_tweenStart, feed it time with kon_tweenStep and read it with
   kon_tweenValue. a tween that was never started just holds 0, kon_tweenSet gives it a value to hold */
typedef struct kon_tween {
	float from, to;
	float duration, time;
	kon_ease_t ease;
} kon_tween_t;

/* t from 0 to 1, out of range is clamped */
float kon_easeValue(kon_ease_t ease, float t);

void kon_tweenSet(kon_tween_t *tween, float value);
void kon_tweenStart(kon_tween_t *tween, float from, float to, float seconds, kon_ease_t ease);

/* glides from wherever it is now, so it can be redirected in the middle of a move */
void kon_tweenTo(kon_tween_t *tween, float to, float seconds, kon_ease_t ease);

/* returns the new value */
float kon_tweenStep(kon_tween_t *tween, float dt);
float kon_tweenValue(const kon_tween_t *tween);
int kon_tweenDone(const kon_tween_t *tween);

#endif