-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscene.c
153 lines (142 loc) · 3.89 KB
/
scene.c
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
#include "scene.h"
#include "timer.h"
#include <raylib.h>
#include <stdio.h>
#include <stdlib.h>
void SceneManagerInit(SceneManager* m) {
m->transition.startedAt = -1;
m->transition.endedAt = 0;
m->transition.ctx = NULL;
m->transition.cb = NULL;
m->transition.timer = TimerCreate((TimerConfig){
.direction = TimerDirection_Forward,
.iterations = 1,
});
m->indexes.curr = 0;
m->indexes.next = -1;
m->indexes.prev = -1;
m->frame = 0;
}
Scene SceneCreate(
int width,
int height,
SceneCallback enter,
SceneCallback exit,
SceneCallback draw,
SceneCallback update) {
return (Scene){
.active = false,
.entering = false,
.leaving = false,
.enteredAt = -1,
.leftAt = -1,
.canvas = LoadRenderTexture(width, height),
.src = (Rectangle){0, 0, width, -height},
.dst = (Rectangle){0, 0, width, height},
.origin = (Vector2){0, 0},
.rotation = 0,
.tint = WHITE,
.enter = enter,
.exit = exit,
.draw = draw,
.update = update,
};
}
void SceneDestroy(Scene scene) {
UnloadRenderTexture(scene.canvas);
}
void SceneAppend(SceneManager* m, Scene scene) {
Scene* entries = m->scenes.entries;
int* size = &m->scenes.size;
if (*size == 0) scene.active = true;
entries[*size] = scene;
*size += 1;
}
float SceneGetTransitionProgress(SceneManager* m) {
return TimerGetProgress(&m->transition.timer);
}
bool SceneTransitionTo(SceneManager* m, int next, int duration, SceneTransitionCallback cb, void* ctx) {
if (m->indexes.next != -1) return true;
m->scenes.entries[next].active = true;
m->scenes.entries[next].entering = true;
m->indexes.next = next;
m->transition.startedAt = m->frame + 1;
m->transition.cb = cb;
m->transition.ctx = ctx;
m->transition.timer.config.duration = duration;
TimerReset(&m->transition.timer);
return false;
}
void SceneDraw(SceneManager* m, RenderTexture2D target, void* ctx) {
Scene* entries = m->scenes.entries;
int size = m->scenes.size;
for (int i = 0; i < size; i++) {
Scene* s = &entries[i];
if (s->active) {
BeginTextureMode(s->canvas);
{
if (s->draw) s->draw(ctx);
}
EndTextureMode();
BeginTextureMode(target);
{
DrawTexturePro(
s->canvas.texture,
s->src,
s->dst,
s->origin,
s->rotation,
s->tint);
}
EndTextureMode();
}
}
}
void SceneUpdate(SceneManager* m, void* ctx) {
Scene* entries = m->scenes.entries;
if (m->frame == 0) {
Scene* curr = &entries[m->indexes.curr];
if (curr->enter) {
curr->enter(ctx);
}
}
//* Update active scenes
int size = m->scenes.size;
for (int i = 0; i < size; i++) {
Scene* s = &entries[i];
if (s->active && s->update) {
s->update(ctx);
}
}
//* Update transition timer
Timer* t = &m->transition.timer;
if (m->indexes.next != -1) {
//* Run transition callback
SceneTransitionCallback cb = m->transition.cb;
if (cb) {
cb(m->indexes.curr, m->indexes.next, m->transition.ctx);
}
Scene* from = &entries[m->indexes.curr];
Scene* to = &entries[m->indexes.next];
float progress = TimerGetProgress(t);
if (progress == 1) {
//* Transition complete
from->active = false;
from->entering = false;
m->indexes.prev = m->indexes.curr;
m->indexes.curr = m->indexes.next;
m->indexes.next = -1;
m->transition.endedAt = m->frame;
TimerReset(t);
} else {
if (progress == 0) {
if (from->exit) from->exit(ctx);
if (to->enter) to->enter(ctx);
}
//* Transition in-progress
TimerTick(t);
}
}
//* Increment frame
m->frame++;
}