-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrotozoom.c
89 lines (70 loc) · 2.46 KB
/
rotozoom.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
/*
MIT No Attribution
Copyright (c) 2020-2023 Mika Tuupola
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.
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.
-cut-
SPDX-License-Identifier: MIT-0
*/
#include <stdlib.h>
#include <math.h>
#include <hagl.h>
#include "head.h"
static const uint8_t SPEED = 2;
static const uint8_t PIXEL_SIZE = 2;
static uint16_t angle;
// static float sinlut[360];
// static float coslut[360];
void
rotozoom_init()
{
/* Generate look up tables. */
// for (uint16_t i = 0; i < 360; i++) {
// sinlut[i] = sin(i * M_PI / 180);
// coslut[i] = cos(i * M_PI / 180);
// }
}
void
rotozoom_render(hagl_backend_t const *display)
{
float s, c, z;
s = sin(angle * M_PI / 180);
c = cos(angle * M_PI / 180);
// s = sinlut[angle];
// c = coslut[angle];
z = s * 1.2;
for (uint16_t x = 0; x < display->width; x = x + PIXEL_SIZE) {
for (uint16_t y = 0; y < display->height; y = y + PIXEL_SIZE) {
/* Get a rotated pixel from the head image. */
int16_t u = (int16_t)((x * c - y * s) * z) % HEAD_WIDTH;
int16_t v = (int16_t)((x * s + y * c) * z) % HEAD_HEIGHT;
u = abs(u);
if (v < 0) {
v += HEAD_HEIGHT;
}
hagl_color_t *color = (hagl_color_t *) (head + HEAD_WIDTH * sizeof(hagl_color_t) * v + sizeof(hagl_color_t) * u);
if (1 == PIXEL_SIZE) {
hagl_put_pixel(display, x, y, *color);
} else {
hagl_fill_rectangle(display, x, y, x + PIXEL_SIZE - 1, y + PIXEL_SIZE - 1, *color);
}
// hagl_put_pixel(x, y, *color);
}
}
}
void
rotozoom_animate()
{
angle = (angle + SPEED) % 360;
}