Project

General

Profile

Statistics
| Revision:

root / proj / sprite.c @ 159

History | View | Annotate | Download (1.28 KB)

1 152 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 159 up20180642
    enum xpm_image_type type = XPM_8_8_8;
17 152 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 159 up20180642
    ret->x = 0;
24
    ret->y = 0;
25 152 up20180642
    ret->w = img.width;
26
    ret->h = img.height;
27
    return ret;
28
}
29
30
void sprite_dtor(sprite_t *p){
31
    if(p == NULL) return;
32
    if(p->map) free(p->map);
33
    free(p);
34
}
35
36
void sprite_set_x(sprite_t *p, int x){ p->x = x; }
37
void sprite_set_y(sprite_t *p, int y){ p->y = y; }
38
void sprite_set_pos(sprite_t *p, int x, int y){
39
    sprite_set_x(p, x);
40
    sprite_set_y(p, y);
41
}
42
43
int sprite_get_w(const sprite_t *p){ return p->w; }
44
int sprite_get_h(const sprite_t *p){ return p->h; }
45
46
void sprite_draw(const sprite_t *p){
47 159 up20180642
    uint8_t *m = p->map;
48 152 up20180642
    for (int i = 0; i < p->w; i++) {
49 159 up20180642
        for (int j = 0; j < p->h; j++, m += 3) {
50
            if (p->x + i < get_XRes() && p->y + j < get_YRes()){
51
                uint32_t color = SET_COLOR(*m,*(m+1),*(m+2));
52
                set_pixel(p->x + i, p->y + j, color);
53 152 up20180642
            }
54
        }
55
    }
56
}