-
Notifications
You must be signed in to change notification settings - Fork 1
/
CRotozoom.cpp
74 lines (53 loc) · 1.64 KB
/
CRotozoom.cpp
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
#include "CRotozoom.h"
void CRotozoom::Init(SDL_Surface * texture)
{
this->texture = texture;
this->generateLookupSinCosTables();
this->ang = 0;
}
void CRotozoom::generateLookupSinCosTables()
{
for (int index = 0; index < LOOKUP_SIZE_SIN; index++)
this->sin_t[index] = std::sin(PI * 2 * index / LOOKUP_SIZE_SIN);
for (int index = 0; index < LOOKUP_SIZE_SIN; index++)
this->cos_t[index] = std::cos(PI * 2 * index / LOOKUP_SIZE_SIN);
}
void CRotozoom::Update(long deltaTime, SDL_Surface * buffer)
{
this->setAng(this->ang);
float zoom = 1 + this->sin_t[int(this->ang)%LOOKUP_SIZE_SIN];
this->setZoom(zoom);
this->RotoZoom(buffer, this->texture, this->ang, this->zoom, this->cos_t, this->sin_t);
this->ang += 1;
}
CRotozoom::CRotozoom()
{
}
CRotozoom::~CRotozoom()
{
}
void CRotozoom::RotoZoom(SDL_Surface *buffer, SDL_Surface *texture, float ang, float zoom, long double *cos_t, long double *sin_t)
{
int H = buffer->h;
int W = buffer->w;
int HTex = texture->h;
int WTex = texture->w;
unsigned int *pixelsBuffer = (unsigned int *)buffer->pixels;
unsigned int *pixelTex = (unsigned int *)texture->pixels;
int midW = W / 2;
int midH = H / 2;
for (int y = 0; y < H; y++)
for (int x = 0; x < W; x++)
{
int u = (x - midW) * cos_t[int(ang) % LOOKUP_SIZE_SIN] + (y - midH) * (-sin_t[int(ang) % LOOKUP_SIZE_SIN]);
int v = (x - midW) * sin_t[int(ang) % LOOKUP_SIZE_SIN] + (y - midH) * cos_t[int(ang) % LOOKUP_SIZE_SIN];
u = (u)* zoom;
v = (v)* zoom;
u += 32 + midW;
v += 32 + midH;
u = abs(u % WTex);
v = abs(v % HTex);
unsigned int color = pixelTex[u + WTex * v];
pixelsBuffer[x + W * y] = color;
}
}