Revision 163
changed stuff in sprite
proj/sprite.c | ||
---|---|---|
3 | 3 |
#include "sprite.h" |
4 | 4 |
|
5 | 5 |
#include "graphics.h" |
6 |
#include "utils.h" |
|
6 | 7 |
#include <math.h> |
7 | 8 |
|
8 | 9 |
struct sprite{ |
9 |
int x, y; |
|
10 |
int w, h;
|
|
11 |
int u0, v0; |
|
10 |
int16_t x, y;
|
|
11 |
uint16_t w, h;
|
|
12 |
int16_t u0, v0;
|
|
12 | 13 |
double theta; |
13 | 14 |
uint8_t *map; |
14 | 15 |
}; |
15 | 16 |
|
16 |
sprite_t* sprite_ctor(const char **xpm, int u0, int v0){
|
|
17 |
sprite_t* (sprite_ctor)(const char **xpm, int u0, int v0){
|
|
17 | 18 |
sprite_t *ret = (sprite_t*)malloc(sizeof(sprite_t)); |
18 | 19 |
if(ret == NULL) return NULL; |
19 | 20 |
enum xpm_image_type type = XPM_8_8_8_8; |
... | ... | |
32 | 33 |
ret->theta = 0; |
33 | 34 |
return ret; |
34 | 35 |
} |
35 |
void sprite_dtor(sprite_t *p){
|
|
36 |
void (sprite_dtor)(sprite_t *p){
|
|
36 | 37 |
if(p == NULL) return; |
37 | 38 |
if(p->map) free(p->map); |
38 | 39 |
free(p); |
39 | 40 |
} |
40 | 41 |
|
41 |
void sprite_set_x(sprite_t *p, int x){ p->x = x; }
|
|
42 |
void sprite_set_y(sprite_t *p, int y){ p->y = y; }
|
|
43 |
void sprite_set_pos(sprite_t *p, int x, int y){ sprite_set_x(p, x); sprite_set_y(p, y); }
|
|
44 |
void sprite_set_angle(sprite_t *p, double angle){ p->theta = angle; }
|
|
45 |
void sprite_set_center(sprite_t *p, int u0, int v0){ p->u0 = u0; p->v0 = v0; }
|
|
42 |
void (sprite_set_x) (sprite_t *p, int16_t x){ p->x = x; }
|
|
43 |
void (sprite_set_y) (sprite_t *p, int16_t y){ p->y = y; }
|
|
44 |
void (sprite_set_pos) (sprite_t *p, int16_t x, int16_t y){ sprite_set_x(p, x); sprite_set_y(p, y); }
|
|
45 |
void (sprite_set_angle) (sprite_t *p, double angle){ p->theta = angle; }
|
|
46 |
void (sprite_set_center)(sprite_t *p, int16_t u0, int16_t v0){ p->u0 = u0; p->v0 = v0; }
|
|
46 | 47 |
|
47 |
int sprite_get_w(const sprite_t *p){ return p->w; }
|
|
48 |
int sprite_get_h(const sprite_t *p){ return p->h; }
|
|
48 |
uint16_t (sprite_get_w)(const sprite_t *p){ return p->w; }
|
|
49 |
uint16_t (sprite_get_h)(const sprite_t *p){ return p->h; }
|
|
49 | 50 |
|
50 |
void sprite_src2pic(const sprite_t *p, int x, int y, int *u, int *v){ |
|
51 |
int dx = x - p->x; |
|
52 |
int dy = y - p->y; |
|
53 |
//int du = dx*cos(p->theta) - dy*sin(p->theta); |
|
54 |
//int dv = dx*sin(p->theta) + dy*cos(p->theta); |
|
55 |
//*u = du + p->u0; |
|
56 |
//*v = dv + p->v0; |
|
57 |
//*u = dx*cos(p->theta) - dy*sin(p->theta) + p->u0; |
|
58 |
//*v = dx*sin(p->theta) + dy*cos(p->theta) + p->v0; |
|
51 |
void (sprite_src2pic)(const sprite_t *p, int16_t x, int16_t y, int16_t *u, int16_t *v){ |
|
52 |
int16_t dx = x - p->x; |
|
53 |
int16_t dy = y - p->y; |
|
59 | 54 |
double s = sin(p->theta); |
60 | 55 |
double c = cos(p->theta); |
61 | 56 |
*u = dx*c - dy*s + p->u0; |
62 | 57 |
*v = dx*s + dy*c + p->v0; |
63 | 58 |
} |
64 | 59 |
|
65 |
void sprite_draw(const sprite_t *p){ |
|
66 |
const int diag = sqrt(p->w*p->w + p->h*p->h)+2; |
|
67 |
int u, v; |
|
68 |
for(int y = p->y - diag; y < p->y + diag; ++y){ |
|
69 |
for(int x = p->x - diag; x < p->x + diag; ++x){ |
|
70 |
if (p->x + x < get_XRes() && p->y + y < get_YRes()){ |
|
71 |
sprite_src2pic(p, x, y, &u, &v); |
|
72 |
if(0 <= u && u < p->w && 0 <= v && v < p->h){ |
|
73 |
uint8_t *m = p->map + (v*p->w + u)*4; |
|
74 |
uint32_t color = SET_RGB(*(m+2), *(m+1), *(m)); |
|
75 |
set_pixel_alpha(p->x + x, p->y + y, color, *(m+3)); |
|
76 |
} |
|
60 |
void (sprite_draw)(const sprite_t *p){ |
|
61 |
const int16_t diag = sqrt(p->w*p->w + p->h*p->h)+2; |
|
62 |
int16_t u, v; |
|
63 |
for(int16_t y = max(0,p->y-diag); y < min(p->y+diag,get_YRes()); ++y){ |
|
64 |
for(int16_t x = max(0,p->x-diag); x < min(p->x+diag,get_XRes()); ++x){ |
|
65 |
sprite_src2pic(p, x, y, &u, &v); |
|
66 |
if(0 <= u && u < p->w && 0 <= v && v < p->h){ |
|
67 |
uint8_t *m = p->map + (v*p->w + u)*4; |
|
68 |
uint32_t color = SET_RGB(*(m+2), *(m+1), *(m)); |
|
69 |
set_pixel_alpha(p->x + x, p->y + y, color, *(m+3)); |
|
77 | 70 |
} |
78 | 71 |
} |
79 | 72 |
} |
proj/sprite.h | ||
---|---|---|
4 | 4 |
struct sprite; |
5 | 5 |
typedef struct sprite sprite_t; |
6 | 6 |
|
7 |
sprite_t* sprite_ctor(const char **xpm, int u0, int v0);
|
|
8 |
void sprite_dtor(sprite_t *p);
|
|
7 |
sprite_t* (sprite_ctor)(const char **xpm, int u0, int v0);
|
|
8 |
void (sprite_dtor)(sprite_t *p);
|
|
9 | 9 |
|
10 |
void sprite_set_x (sprite_t *p, int x);
|
|
11 |
void sprite_set_y (sprite_t *p, int y);
|
|
12 |
void sprite_set_pos (sprite_t *p, int x, int y);
|
|
13 |
void sprite_set_angle (sprite_t *p, double angle);
|
|
14 |
void sprite_set_center(sprite_t *p, int u0, int v0);
|
|
10 |
void (sprite_set_x) (sprite_t *p, int16_t x);
|
|
11 |
void (sprite_set_y) (sprite_t *p, int16_t y);
|
|
12 |
void (sprite_set_pos) (sprite_t *p, int16_t x, int16_t y);
|
|
13 |
void (sprite_set_angle) (sprite_t *p, double angle);
|
|
14 |
void (sprite_set_center)(sprite_t *p, int16_t u0, int16_t v0);
|
|
15 | 15 |
|
16 |
int sprite_get_w(const sprite_t *p);
|
|
17 |
int sprite_get_h(const sprite_t *p);
|
|
16 |
uint16_t (sprite_get_w)(const sprite_t *p);
|
|
17 |
uint16_t (sprite_get_h)(const sprite_t *p);
|
|
18 | 18 |
|
19 |
void sprite_draw(const sprite_t *p);
|
|
19 |
void (sprite_draw)(const sprite_t *p);
|
|
20 | 20 |
|
21 | 21 |
#endif //SPRITE_H_INCLUDED |
Also available in: Unified diff