root / lab5 / sprite.c @ 131
History | View | Annotate | Download (1014 Bytes)
1 |
#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 |
enum xpm_image_type type = XPM_INDEXED;
|
17 |
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 |
void sprite_draw(const sprite_t *p){ |
42 |
for (int i = 0; i < p->w; i++) { |
43 |
for (int j = 0; j < p->h; j++) { |
44 |
set_pixel(p->x + i, p->y + j, p->map[i + j * p->w]); |
45 |
} |
46 |
} |
47 |
} |