Revision 160
implemented sprite rotation
sprite.c | ||
---|---|---|
3 | 3 |
#include "sprite.h" |
4 | 4 |
|
5 | 5 |
#include "graphics.h" |
6 |
#include <math.h> |
|
6 | 7 |
|
7 | 8 |
struct sprite{ |
8 | 9 |
int x, y; |
9 | 10 |
int w, h; |
11 |
int u0, v0; |
|
12 |
double theta; |
|
10 | 13 |
uint8_t *map; |
11 | 14 |
}; |
12 | 15 |
|
13 | 16 |
sprite_t* sprite_ctor(const xpm_map_t xpm){ |
14 | 17 |
sprite_t *ret = (sprite_t*)malloc(sizeof(sprite_t)); |
15 | 18 |
if(ret == NULL) return NULL; |
16 |
enum xpm_image_type type = XPM_8_8_8; |
|
19 |
enum xpm_image_type type = XPM_8_8_8_8;
|
|
17 | 20 |
xpm_image_t img; |
18 | 21 |
ret->map = xpm_load(xpm, type, &img); |
19 | 22 |
if(ret->map == NULL){ |
... | ... | |
24 | 27 |
ret->y = 0; |
25 | 28 |
ret->w = img.width; |
26 | 29 |
ret->h = img.height; |
30 |
ret->u0 = 0; |
|
31 |
ret->v0 = 0; |
|
32 |
ret->theta = 0; |
|
27 | 33 |
return ret; |
28 | 34 |
} |
29 | 35 |
|
... | ... | |
39 | 45 |
sprite_set_x(p, x); |
40 | 46 |
sprite_set_y(p, y); |
41 | 47 |
} |
48 |
void sprite_set_angle(sprite_t *p, double angle){ p->theta = angle; } |
|
49 |
void sprite_set_center(sprite_t *p, int u0, int v0){ |
|
50 |
p->u0 = u0; |
|
51 |
p->v0 = v0; |
|
52 |
} |
|
42 | 53 |
|
43 | 54 |
int sprite_get_w(const sprite_t *p){ return p->w; } |
44 | 55 |
int sprite_get_h(const sprite_t *p){ return p->h; } |
45 | 56 |
|
57 |
void sprite_src2pic(const sprite_t *p, int x, int y, int *u, int *v){ |
|
58 |
int dx = x - p->x; |
|
59 |
int dy = y - p->y; |
|
60 |
int du = dx*cos(p->theta) - dy*sin(p->theta); |
|
61 |
int dv = dx*sin(p->theta) + dy*cos(p->theta); |
|
62 |
*u = du + p->u0; |
|
63 |
*v = dv + p->v0; |
|
64 |
} |
|
65 |
|
|
46 | 66 |
void sprite_draw(const sprite_t *p){ |
47 |
uint8_t *m = p->map; |
|
48 |
for (int i = 0; i < p->w; i++) { |
|
49 |
for (int j = 0; j < p->h; j++, m += 3) { |
|
50 |
if (p->x + i < get_XRes() && p->y + j < get_YRes()){ |
|
51 |
uint32_t color = SET_COLOR(*m,*(m+1),*(m+2)); |
|
52 |
set_pixel(p->x + i, p->y + j, color); |
|
67 |
const int diag = sqrt(p->w*p->w + p->h*p->h)+2; |
|
68 |
int u, v; |
|
69 |
for(int y = p->y - diag; y < p->y + diag; ++y){ |
|
70 |
for(int x = p->x - diag; x < p->x + diag; ++x){ |
|
71 |
if (p->x + x < get_XRes() && p->y + y < get_YRes()){ |
|
72 |
sprite_src2pic(p, x, y, &u, &v); |
|
73 |
if(0 <= u && u < p->w && 0 <= v && v < p->h){ |
|
74 |
uint8_t *m = p->map + (v*p->w + u)*4; |
|
75 |
uint32_t color = SET_RGB(*(m+2), *(m+1), *(m)); |
|
76 |
set_pixel_alpha(p->x + x, p->y + y, color, *(m+3)); |
|
77 |
} |
|
53 | 78 |
} |
54 | 79 |
} |
55 | 80 |
} |
Also available in: Unified diff