root / proj / sprite.c @ 161
History | View | Annotate | Download (2.06 KB)
1 | 152 | up20180642 | #include <lcom/lcf.h> |
---|---|---|---|
2 | |||
3 | #include "sprite.h" |
||
4 | |||
5 | #include "graphics.h" |
||
6 | 160 | up20180642 | #include <math.h> |
7 | 152 | up20180642 | |
8 | struct sprite{
|
||
9 | int x, y;
|
||
10 | int w, h;
|
||
11 | 160 | up20180642 | int u0, v0;
|
12 | double theta;
|
||
13 | 152 | up20180642 | uint8_t *map; |
14 | }; |
||
15 | |||
16 | sprite_t* sprite_ctor(const xpm_map_t xpm){
|
||
17 | sprite_t *ret = (sprite_t*)malloc(sizeof(sprite_t));
|
||
18 | if(ret == NULL) return NULL; |
||
19 | 160 | up20180642 | enum xpm_image_type type = XPM_8_8_8_8;
|
20 | 152 | up20180642 | xpm_image_t img; |
21 | ret->map = xpm_load(xpm, type, &img); |
||
22 | if(ret->map == NULL){ |
||
23 | free(ret); |
||
24 | return NULL; |
||
25 | } |
||
26 | 159 | up20180642 | ret->x = 0;
|
27 | ret->y = 0;
|
||
28 | 152 | up20180642 | ret->w = img.width; |
29 | ret->h = img.height; |
||
30 | 160 | up20180642 | ret->u0 = 0;
|
31 | ret->v0 = 0;
|
||
32 | ret->theta = 0;
|
||
33 | 152 | up20180642 | return ret;
|
34 | } |
||
35 | |||
36 | void sprite_dtor(sprite_t *p){
|
||
37 | if(p == NULL) return; |
||
38 | if(p->map) free(p->map);
|
||
39 | free(p); |
||
40 | } |
||
41 | |||
42 | void sprite_set_x(sprite_t *p, int x){ p->x = x; } |
||
43 | 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 | } |
||
48 | 160 | up20180642 | 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 | } |
||
53 | 152 | up20180642 | |
54 | int sprite_get_w(const sprite_t *p){ return p->w; } |
||
55 | int sprite_get_h(const sprite_t *p){ return p->h; } |
||
56 | |||
57 | 160 | up20180642 | 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 | |||
66 | 152 | up20180642 | void sprite_draw(const sprite_t *p){ |
67 | 160 | up20180642 | 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 | } |
||
78 | 152 | up20180642 | } |
79 | } |
||
80 | } |
||
81 | } |