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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
inline int
OK(int x) {
return x == 0;
}
typedef uint8_t bool;
#define false 0
#define true 1
typedef int32_t i32;
typedef uint32_t u32;
////RGBImage////
typedef uint8_t byte;
typedef uint16_t byte2;
typedef uint32_t byte4;
typedef struct ARGB {
byte a;
byte r;
byte g;
byte b;
} ARGB;
void
ARGB_set(ARGB* rgb, byte4 color) {
rgb->a = (0xFF000000 & color) >> 24;
rgb->r = (0x00FF0000 & color) >> 16;
rgb->g = (0x0000FF00 & color) >> 8;
rgb->b = (0x000000FF & color);
}
void
ARGB_merge(ARGB* bottom, ARGB top) {
bottom->a = 255;
double perc = top.a / 255.0;
bottom->r = (bottom->r * (1.0 - perc)) + (top.r * perc);
bottom->g = (bottom->g * (1.0 - perc)) + (top.g * perc);
bottom->b = (bottom->b * (1.0 - perc)) + (top.b * perc);
}
typedef struct RGBImage {
u32 width;
u32 height;
ARGB* img;
} RGBImage;
RGBImage
RGBImage_new(u32 width, u32 height) {
return (RGBImage){
.width = width,
.height = height,
.img = malloc(sizeof(ARGB) * width * height)
};
}
void
RGBImage_free(RGBImage img) {
free(img.img);
}
void
RGBImage_delete(RGBImage* img) {
RGBImage_free(*img);
img->width = img->height = 0;
img->img = NULL;
}
ARGB*
RGBImage_at(RGBImage img, int row, int col) {
return img.img + (row % img.height) * img.width + col % img.width;
}
int
ppm6_write(FILE* f, RGBImage img) {
fprintf(f, "P6\n%d %d\n255\n", img.width, img.height);
size_t size = img.width * img.height;
for (size_t i = 0; i < size; ++i) {
fputc(img.img[i].r, f);
fputc(img.img[i].g, f);
fputc(img.img[i].b, f);
}
return fflush(f);
}
////Animation////
struct AnimationEventNode;
typedef struct AnimationEventNode AnimationEventNode;
typedef struct Animation {
u32 width;
u32 height;
u32 frameCount;
RGBImage frameBuffer;
AnimationEventNode* events;
} Animation;
typedef ARGB (*PixelRenderer)(const Animation* anim, u32 currentFrame, ARGB pixel, u32 row, u32 col, void* priv);
struct AnimationEventNode {
AnimationEventNode* next;
u32 startFrame;
u32 endFrame;
PixelRenderer renderCallback;
void* privateData;
};
AnimationEventNode*
AnimationEventNode_new(u32 startFrame, u32 endFrame, PixelRenderer renderCallback, void* privateData) {
AnimationEventNode* aen = (AnimationEventNode*)malloc(sizeof(AnimationEventNode));
aen->next = NULL;
aen->startFrame = startFrame;
aen->endFrame = endFrame;
aen->renderCallback = renderCallback;
aen->privateData = privateData;
return aen;
}
Animation
Animation_new(u32 width, u32 height, u32 frameCount) {
return (Animation){
.width = width,
.height = height,
.frameCount = frameCount,
.frameBuffer = RGBImage_new(width, height),
.events = NULL,
};
}
void
Animation_free(Animation anim) {
RGBImage_free(anim.frameBuffer);
while (anim.events != NULL) {
AnimationEventNode* toFree = anim.events;
anim.events = anim.events->next;
free(toFree);
}
}
void
Animation_delete(Animation* anim) {
Animation_free(*anim);
anim->width = anim->height = anim->frameCount = 0;
RGBImage_delete(&anim->frameBuffer);
}
void
Animation_pushEvent(Animation* anim, u32 startFrame, u32 endFrame, PixelRenderer renderer, void* privateData) {
if (anim->events == NULL) {
anim->events = AnimationEventNode_new(startFrame, endFrame, renderer, privateData);
return;
}
AnimationEventNode* prevNode = NULL, *currNode = anim->events;
while (currNode != NULL) {
if (startFrame < currNode->startFrame) {
prevNode->next = AnimationEventNode_new(startFrame, endFrame, renderer, privateData);
prevNode->next->next = currNode;
return;
}
prevNode = currNode;
currNode = currNode->next;
}
prevNode->next = AnimationEventNode_new(startFrame, endFrame, renderer, privateData);
}
void
Animation_render(Animation* anim) {
for (int currFrame = 0; currFrame < anim->frameCount; ++currFrame) {
if (anim->events != NULL)
for (int r = 0; r < anim->frameBuffer.width; ++r)
for (int c = 0; c < anim->frameBuffer.height; ++c) {
for (AnimationEventNode* aen = anim->events; aen != NULL && aen->startFrame <= currFrame; aen = aen->next) {
ARGB* pixel = RGBImage_at(anim->frameBuffer, r, c);
ARGB newPixel = aen->renderCallback(anim, currFrame, *pixel, r, c, aen->privateData);
ARGB_merge(pixel, newPixel);
}
AnimationEventNode* prevNode = NULL, *currNode = anim->events;
while (currNode != NULL && currNode->startFrame <= currFrame) {
if (currNode->endFrame <= currFrame) {
if (prevNode != NULL)
prevNode->next = currNode->next;
else
anim->events = NULL;
free(currNode);
}
else {
prevNode = currNode;
currNode = currNode->next;
}
}
}
ppm6_write(stdout, anim->frameBuffer);
}
}
////main////
struct CheckerPattern {
u32 squareSize;
byte4 colorA;
byte4 colorB;
};
ARGB
CheckerPattern(const Animation* anim, u32 frameIndex, ARGB pixel, u32 r, u32 c, void* priv) {
struct CheckerPattern chp = *(struct CheckerPattern*)priv;
r /= chp.squareSize;
c /= chp.squareSize;
if ((r + c) % 2 == 0) {
ARGB_set(&pixel, 0xFF000000);
}
else {
ARGB_set(&pixel, 0xFFFF00FF);
}
return pixel;
}
struct SquareSettings {
u32 width;
u32 height;
byte4 color;
};
ARGB
Square(const Animation* anim, u32 frameIndex, ARGB pixel, u32 r, u32 c, void* priv) {
struct SquareSettings* set = (struct SquareSettings*)priv;
if (0 <= r && r <= set->width && 0 <= c && c <= set->height) {
ARGB_set(&pixel, set->color);
}
return pixel;
}
struct MoveLinear {
u32 startRow;
u32 startCol;
float dRow;
float dCol;
PixelRenderer callback;
void* priv;
};
ARGB
MoveLinear(const Animation* anim, u32 frameIndex, ARGB pixel, u32 r, u32 c, void* priv) {
struct MoveLinear ml = *(struct MoveLinear*)priv;
r = (r + ml.startRow - (u32)(frameIndex * ml.dRow)) % anim->width;
c = (c + ml.startCol - (u32)(frameIndex * ml.dCol)) % anim->height;
return ml.callback(anim, frameIndex, pixel, r, c, ml.priv);
}
struct MoveRotate {
float radius;
float theta;
PixelRenderer callback;
void* priv;
};
ARGB
MoveRotate(const Animation* anim, u32 frameIndex, ARGB pixel, u32 r, u32 c, void* priv) {
struct MoveRotate mr = *(struct MoveRotate*)priv;
r = (r + (u32)(mr.radius * cos(mr.theta * frameIndex))) % anim->width;
c = (c + (u32)(mr.radius * sin(mr.theta * frameIndex))) % anim->height;
return mr.callback(anim, frameIndex, pixel, r, c, mr.priv);
}
int
main() {
Animation anim = Animation_new(512, 512, 120);
// Background
struct CheckerPattern chp1 = {
.squareSize = 32,
.colorA = 0xFF000000, .colorB = 0xFFFF00FF,
};
struct MoveLinear ml1 = {
.startRow = 0, .startCol = 0,
.dRow = 1, .dCol = 1,
.callback = CheckerPattern, .priv = &chp1,
};
Animation_pushEvent(&anim, 0, 120, MoveLinear, &ml1);
// Yellow square
struct SquareSettings s1 = {
.width = 20, .height = 20, .color = 0xFFFFFF00,
};
struct MoveRotate mr1 = {
.radius = 20, .theta = 3.14159 / 20,
.callback = Square, .priv = &s1,
};
struct MoveLinear ml3 = {
.startRow = 0, .startCol = 0,
.dRow = -3, .dCol = 3,
.callback = MoveRotate, .priv = &mr1,
};
Animation_pushEvent(&anim, 0, 80, MoveLinear, &ml3);
// Green square
struct SquareSettings s2 = {
.width = 25, .height = 40, .color = 0xA009FAA5,
};
struct MoveLinear ml2 = {
.startRow = 20, .startCol = 5,
.dRow = -4, .dCol = -10,
.callback = Square, .priv = &s2,
};
Animation_pushEvent(&anim, 60, 100, MoveLinear, &ml2);
Animation_render(&anim);
Animation_delete(&anim);
return 0;
}
|