root / proj / sprite.c @ 172
History | View | Annotate | Download (2.25 KB)
1 |
#include <lcom/lcf.h> |
---|---|
2 |
|
3 |
#include "sprite.h" |
4 |
|
5 |
#include "graph.h" |
6 |
#include "utils.h" |
7 |
#include "fast_math.h" |
8 |
#include <math.h> |
9 |
|
10 |
struct sprite{
|
11 |
int16_t x, y; |
12 |
uint16_t w, h; |
13 |
int16_t u0, v0; |
14 |
float theta;
|
15 |
uint8_t *map; |
16 |
}; |
17 |
|
18 |
sprite_t* (sprite_ctor)(const char **xpm, int u0, int v0){ |
19 |
sprite_t *ret = (sprite_t*)malloc(sizeof(sprite_t));
|
20 |
if(ret == NULL) return NULL; |
21 |
enum xpm_image_type type = XPM_8_8_8_8;
|
22 |
xpm_image_t img; |
23 |
ret->map = xpm_load((xpm_map_t)xpm, type, &img); |
24 |
if(ret->map == NULL){ |
25 |
free(ret); |
26 |
return NULL; |
27 |
} |
28 |
ret->x = 0;
|
29 |
ret->y = 0;
|
30 |
ret->w = img.width; |
31 |
ret->h = img.height; |
32 |
ret->u0 = u0; |
33 |
ret->v0 = u0; |
34 |
ret->theta = 0;
|
35 |
return ret;
|
36 |
} |
37 |
void (sprite_dtor)(sprite_t *p){
|
38 |
if(p == NULL) return; |
39 |
if(p->map) free(p->map);
|
40 |
free(p); |
41 |
} |
42 |
|
43 |
void (sprite_set_x) (sprite_t *p, int16_t x){ p->x = x; }
|
44 |
void (sprite_set_y) (sprite_t *p, int16_t y){ p->y = y; }
|
45 |
void (sprite_set_pos) (sprite_t *p, int16_t x, int16_t y){ sprite_set_x(p, x); sprite_set_y(p, y); }
|
46 |
void (sprite_set_angle) (sprite_t *p, double angle){ p->theta = angle; } |
47 |
void (sprite_set_center)(sprite_t *p, int16_t u0, int16_t v0){ p->u0 = u0; p->v0 = v0; }
|
48 |
|
49 |
int sprite_get_x(const sprite_t *p){ return p->x; } |
50 |
int sprite_get_y(const sprite_t *p){ return p->y; } |
51 |
int sprite_get_w(const sprite_t *p){ return p->w; } |
52 |
int sprite_get_h(const sprite_t *p){ return p->h; } |
53 |
|
54 |
void (sprite_src2pic)(const sprite_t *p, int16_t x, int16_t y, int16_t *u, int16_t *v){ |
55 |
int16_t dx = x - p->x; |
56 |
int16_t dy = y - p->y; |
57 |
double s = fm_sin(p->theta);
|
58 |
double c = fm_cos(p->theta);
|
59 |
*u = dx*c - dy*s + p->u0; |
60 |
*v = dx*s + dy*c + p->v0; |
61 |
} |
62 |
|
63 |
void (sprite_draw)(const sprite_t *p){ |
64 |
const int16_t diag = sqrt(p->w*p->w + p->h*p->h)+2; |
65 |
int16_t u, v; |
66 |
for(int16_t y = max(0,p->y-diag); y < min(p->y+diag,graph_get_YRes()); ++y){ |
67 |
for(int16_t x = max(0,p->x-diag); x < min(p->x+diag,graph_get_XRes()); ++x){ |
68 |
sprite_src2pic(p, x, y, &u, &v); |
69 |
if(0 <= u && u < p->w && 0 <= v && v < p->h){ |
70 |
uint8_t *m = p->map + (v*p->w + u)*4;
|
71 |
uint32_t color = SET_RGB(*(m+2), *(m+1), *(m)); |
72 |
graph_set_pixel_alpha(p->x + x, p->y + y, color, *(m+3));
|
73 |
} |
74 |
} |
75 |
} |
76 |
} |