root / proj / sprite.c @ 165
History | View | Annotate | Download (2.14 KB)
1 | 152 | up20180642 | #include <lcom/lcf.h> |
---|---|---|---|
2 | |||
3 | #include "sprite.h" |
||
4 | |||
5 | #include "graphics.h" |
||
6 | 163 | up20180642 | #include "utils.h" |
7 | 160 | up20180642 | #include <math.h> |
8 | 152 | up20180642 | |
9 | struct sprite{
|
||
10 | 163 | up20180642 | int16_t x, y; |
11 | uint16_t w, h; |
||
12 | int16_t u0, v0; |
||
13 | 160 | up20180642 | double theta;
|
14 | 152 | up20180642 | uint8_t *map; |
15 | }; |
||
16 | |||
17 | 163 | up20180642 | sprite_t* (sprite_ctor)(const char **xpm, int u0, int v0){ |
18 | 152 | up20180642 | sprite_t *ret = (sprite_t*)malloc(sizeof(sprite_t));
|
19 | if(ret == NULL) return NULL; |
||
20 | 160 | up20180642 | enum xpm_image_type type = XPM_8_8_8_8;
|
21 | 152 | up20180642 | xpm_image_t img; |
22 | 162 | up20180642 | ret->map = xpm_load((xpm_map_t)xpm, type, &img); |
23 | 152 | up20180642 | if(ret->map == NULL){ |
24 | free(ret); |
||
25 | return NULL; |
||
26 | } |
||
27 | 159 | up20180642 | ret->x = 0;
|
28 | ret->y = 0;
|
||
29 | 152 | up20180642 | ret->w = img.width; |
30 | ret->h = img.height; |
||
31 | 162 | up20180642 | ret->u0 = u0; |
32 | ret->v0 = u0; |
||
33 | 160 | up20180642 | ret->theta = 0;
|
34 | 152 | up20180642 | return ret;
|
35 | } |
||
36 | 163 | up20180642 | void (sprite_dtor)(sprite_t *p){
|
37 | 152 | up20180642 | if(p == NULL) return; |
38 | if(p->map) free(p->map);
|
||
39 | free(p); |
||
40 | } |
||
41 | |||
42 | 163 | up20180642 | 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; }
|
||
47 | 152 | up20180642 | |
48 | 163 | up20180642 | 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; } |
||
50 | 152 | up20180642 | |
51 | 163 | up20180642 | 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; |
||
54 | 162 | up20180642 | double s = sin(p->theta);
|
55 | double c = cos(p->theta);
|
||
56 | *u = dx*c - dy*s + p->u0; |
||
57 | *v = dx*s + dy*c + p->v0; |
||
58 | 160 | up20180642 | } |
59 | |||
60 | 163 | up20180642 | 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 | 165 | up20180642 | for(int16_t y = max(0,p->y-diag); y < min(p->y+diag,graph_get_YRes()); ++y){ |
64 | for(int16_t x = max(0,p->x-diag); x < min(p->x+diag,graph_get_XRes()); ++x){ |
||
65 | 163 | up20180642 | 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 | 165 | up20180642 | graph_set_pixel_alpha(p->x + x, p->y + y, color, *(m+3));
|
70 | 152 | up20180642 | } |
71 | } |
||
72 | } |
||
73 | } |