Revision 162
each sprite should be loaded with the adequate function that sets its center correctly
proj/xpm/shooter.h | ||
---|---|---|
1 |
#include "shooter.xpm" |
|
2 |
#include "sprite.h" |
|
3 |
|
|
4 |
sprite_t* get_shooter(){ |
|
5 |
return sprite_ctor(shooter_xpm, 24, 24); |
|
6 |
} |
|
0 | 7 |
proj/proj.c | ||
---|---|---|
23 | 23 |
#include "proj_func.h" |
24 | 24 |
|
25 | 25 |
#ifdef DIOGO |
26 |
#include "shooter.xpm"
|
|
26 |
#include "shooter.h"
|
|
27 | 27 |
#include "pistol.xpm" |
28 | 28 |
#endif |
29 | 29 |
|
... | ... | |
62 | 62 |
|
63 | 63 |
#ifdef DIOGO |
64 | 64 |
paint_screen(0x777777); |
65 |
sprite_t *shooter = sprite_ctor((xpm_map_t)shooter_xpm); sprite_set_pos(shooter, 100, 100); sprite_set_center(shooter, 25, 25);
|
|
65 |
sprite_t *shooter1 = get_shooter(); sprite_set_pos(shooter1, 100, 100);
|
|
66 | 66 |
for(double angle = 0; angle < 6.29; angle += 0.01){ |
67 |
sprite_set_angle(shooter, angle); |
|
67 |
sprite_set_angle(shooter1, angle);
|
|
68 | 68 |
//paint_screen(0x777777); |
69 |
sprite_draw(shooter); |
|
69 |
sprite_draw(shooter1);
|
|
70 | 70 |
tickdelay(micros_to_ticks(10000)); |
71 | 71 |
} |
72 |
sprite_draw(shooter);
|
|
72 |
sprite_dtor(shooter1);
|
|
73 | 73 |
#endif |
74 | 74 |
|
75 | 75 |
/// loop stuff |
proj/sprite.c | ||
---|---|---|
13 | 13 |
uint8_t *map; |
14 | 14 |
}; |
15 | 15 |
|
16 |
sprite_t* sprite_ctor(const xpm_map_t xpm){
|
|
16 |
sprite_t* sprite_ctor(const char **xpm, int u0, int v0){
|
|
17 | 17 |
sprite_t *ret = (sprite_t*)malloc(sizeof(sprite_t)); |
18 | 18 |
if(ret == NULL) return NULL; |
19 | 19 |
enum xpm_image_type type = XPM_8_8_8_8; |
20 | 20 |
xpm_image_t img; |
21 |
ret->map = xpm_load(xpm, type, &img); |
|
21 |
ret->map = xpm_load((xpm_map_t)xpm, type, &img);
|
|
22 | 22 |
if(ret->map == NULL){ |
23 | 23 |
free(ret); |
24 | 24 |
return NULL; |
... | ... | |
27 | 27 |
ret->y = 0; |
28 | 28 |
ret->w = img.width; |
29 | 29 |
ret->h = img.height; |
30 |
ret->u0 = 0; |
|
31 |
ret->v0 = 0; |
|
30 |
ret->u0 = u0;
|
|
31 |
ret->v0 = u0;
|
|
32 | 32 |
ret->theta = 0; |
33 | 33 |
return ret; |
34 | 34 |
} |
35 |
|
|
36 | 35 |
void sprite_dtor(sprite_t *p){ |
37 | 36 |
if(p == NULL) return; |
38 | 37 |
if(p->map) free(p->map); |
... | ... | |
41 | 40 |
|
42 | 41 |
void sprite_set_x(sprite_t *p, int x){ p->x = x; } |
43 | 42 |
void sprite_set_y(sprite_t *p, int y){ p->y = y; } |
44 |
void sprite_set_pos(sprite_t *p, int x, int y){ |
|
45 |
sprite_set_x(p, x); |
|
46 |
sprite_set_y(p, y); |
|
47 |
} |
|
43 |
void sprite_set_pos(sprite_t *p, int x, int y){ sprite_set_x(p, x); sprite_set_y(p, y); } |
|
48 | 44 |
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 |
} |
|
45 |
void sprite_set_center(sprite_t *p, int u0, int v0){ p->u0 = u0; p->v0 = v0; } |
|
53 | 46 |
|
54 | 47 |
int sprite_get_w(const sprite_t *p){ return p->w; } |
55 | 48 |
int sprite_get_h(const sprite_t *p){ return p->h; } |
... | ... | |
57 | 50 |
void sprite_src2pic(const sprite_t *p, int x, int y, int *u, int *v){ |
58 | 51 |
int dx = x - p->x; |
59 | 52 |
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; |
|
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; |
|
59 |
double s = sin(p->theta); |
|
60 |
double c = cos(p->theta); |
|
61 |
*u = dx*c - dy*s + p->u0; |
|
62 |
*v = dx*s + dy*c + p->v0; |
|
64 | 63 |
} |
65 | 64 |
|
66 | 65 |
void sprite_draw(const sprite_t *p){ |
proj/sprite.h | ||
---|---|---|
4 | 4 |
struct sprite; |
5 | 5 |
typedef struct sprite sprite_t; |
6 | 6 |
|
7 |
sprite_t* sprite_ctor(const xpm_map_t xpm);
|
|
7 |
sprite_t* sprite_ctor(const char **xpm, int u0, int v0);
|
|
8 | 8 |
void sprite_dtor(sprite_t *p); |
9 | 9 |
|
10 | 10 |
void sprite_set_x (sprite_t *p, int x); |
... | ... | |
13 | 13 |
void sprite_set_angle (sprite_t *p, double angle); |
14 | 14 |
void sprite_set_center(sprite_t *p, int u0, int v0); |
15 | 15 |
|
16 |
int sprite_get_w(const sprite_t *p); |
|
16 | 17 |
int sprite_get_h(const sprite_t *p); |
17 |
int sprite_get_w(const sprite_t *p); |
|
18 | 18 |
|
19 | 19 |
void sprite_draw(const sprite_t *p); |
20 | 20 |
|
Also available in: Unified diff