root / lab5 / sprite.c @ 392
History | View | Annotate | Download (1.17 KB)
1 | 125 | up20180642 | #include <lcom/lcf.h> |
---|---|---|---|
2 | |||
3 | #include "sprite.h" |
||
4 | |||
5 | #include "graphics.h" |
||
6 | |||
7 | struct sprite{
|
||
8 | int x, y;
|
||
9 | int w, h;
|
||
10 | uint8_t *map; |
||
11 | }; |
||
12 | |||
13 | sprite_t* sprite_ctor(const xpm_map_t xpm){
|
||
14 | sprite_t *ret = (sprite_t*)malloc(sizeof(sprite_t));
|
||
15 | if(ret == NULL) return NULL; |
||
16 | 131 | up20180642 | enum xpm_image_type type = XPM_INDEXED;
|
17 | 125 | up20180642 | xpm_image_t img; |
18 | ret->map = xpm_load(xpm, type, &img); |
||
19 | if(ret->map == NULL){ |
||
20 | free(ret); |
||
21 | return NULL; |
||
22 | } |
||
23 | ret->w = img.width; |
||
24 | ret->h = img.height; |
||
25 | return ret;
|
||
26 | } |
||
27 | |||
28 | void sprite_dtor(sprite_t *p){
|
||
29 | if(p == NULL) return; |
||
30 | if(p->map) free(p->map);
|
||
31 | free(p); |
||
32 | } |
||
33 | |||
34 | void sprite_set_x(sprite_t *p, int x){ p->x = x; } |
||
35 | void sprite_set_y(sprite_t *p, int y){ p->y = y; } |
||
36 | void sprite_set_pos(sprite_t *p, int x, int y){ |
||
37 | sprite_set_x(p, x); |
||
38 | sprite_set_y(p, y); |
||
39 | } |
||
40 | |||
41 | 134 | up20180642 | int sprite_get_w(const sprite_t *p){ return p->w; } |
42 | int sprite_get_h(const sprite_t *p){ return p->h; } |
||
43 | |||
44 | 125 | up20180642 | void sprite_draw(const sprite_t *p){ |
45 | for (int i = 0; i < p->w; i++) { |
||
46 | for (int j = 0; j < p->h; j++) { |
||
47 | 138 | up20180655 | if (p->x + i < get_XRes() && p->y + j < get_YRes()) {
|
48 | set_pixel(p->x + i, p->y + j, p->map[i + j * p->w]); |
||
49 | } |
||
50 | 125 | up20180642 | } |
51 | } |
||
52 | } |