Project

General

Profile

Statistics
| Revision:

root / proj / src / sprite.c @ 186

History | View | Annotate | Download (2.2 KB)

1 152 up20180642
#include <lcom/lcf.h>
2
3
#include "sprite.h"
4
5 166 up20180642
#include "graph.h"
6 163 up20180642
#include "utils.h"
7 168 up20180642
#include "fast_math.h"
8 160 up20180642
#include <math.h>
9 152 up20180642
10
struct sprite{
11 163 up20180642
    int16_t x, y;
12
    uint16_t w, h;
13
    int16_t u0, v0;
14 182 up20180642
    double theta;
15 152 up20180642
    uint8_t *map;
16
};
17
18 163 up20180642
sprite_t* (sprite_ctor)(const char **xpm, int u0, int v0){
19 152 up20180642
    sprite_t *ret = (sprite_t*)malloc(sizeof(sprite_t));
20
    if(ret == NULL) return NULL;
21 160 up20180642
    enum xpm_image_type type = XPM_8_8_8_8;
22 152 up20180642
    xpm_image_t img;
23 162 up20180642
    ret->map = xpm_load((xpm_map_t)xpm, type, &img);
24 152 up20180642
    if(ret->map == NULL){
25
        free(ret);
26
        return NULL;
27
    }
28 159 up20180642
    ret->x = 0;
29
    ret->y = 0;
30 152 up20180642
    ret->w = img.width;
31
    ret->h = img.height;
32 162 up20180642
    ret->u0 = u0;
33
    ret->v0 = u0;
34 160 up20180642
    ret->theta = 0;
35 152 up20180642
    return ret;
36
}
37 163 up20180642
void (sprite_dtor)(sprite_t *p){
38 152 up20180642
    if(p == NULL) return;
39 178 up20180642
    free(p->map);
40 152 up20180642
    free(p);
41
}
42
43 186 up20180655
void (sprite_set_pos)   (sprite_t *p, int16_t x, int16_t y) {
44
    p->x = max(0, x); p->y = max(0, y);
45
    p->x = min(p->x, graph_get_XRes() - 1); p->y = min(p->y, graph_get_YRes() - 1);
46
}
47 163 up20180642
void (sprite_set_angle) (sprite_t *p, double angle){ p->theta = angle; }
48
void (sprite_set_center)(sprite_t *p, int16_t u0, int16_t v0){ p->u0 = u0; p->v0 = v0; }
49 152 up20180642
50 178 up20180642
int16_t  (sprite_get_x)(const sprite_t *p){ return p->x; }
51
int16_t  (sprite_get_y)(const sprite_t *p){ return p->y; }
52
uint16_t (sprite_get_w)(const sprite_t *p){ return p->w; }
53
uint16_t (sprite_get_h)(const sprite_t *p){ return p->h; }
54 152 up20180642
55 163 up20180642
void (sprite_src2pic)(const sprite_t *p, int16_t x, int16_t y, int16_t *u, int16_t *v){
56
    int16_t dx = x - p->x;
57
    int16_t dy = y - p->y;
58 168 up20180642
    double s = fm_sin(p->theta);
59
    double c = fm_cos(p->theta);
60 162 up20180642
    *u = dx*c - dy*s + p->u0;
61
    *v = dx*s + dy*c + p->v0;
62 160 up20180642
}
63
64 163 up20180642
void (sprite_draw)(const sprite_t *p){
65
    const int16_t diag = sqrt(p->w*p->w + p->h*p->h)+2;
66
    int16_t u, v;
67 165 up20180642
    for(int16_t y = max(0,p->y-diag); y < min(p->y+diag,graph_get_YRes()); ++y){
68
        for(int16_t x = max(0,p->x-diag); x < min(p->x+diag,graph_get_XRes()); ++x){
69 163 up20180642
            sprite_src2pic(p, x, y, &u, &v);
70
            if(0 <= u && u < p->w && 0 <= v && v < p->h){
71 183 up20180642
                uint32_t c = *(uint32_t*)(p->map + (v*p->w + u)*4);
72
                graph_set_pixel_alpha(p->x + x, p->y + y, GET_COLOR(c), GET_ALP(c));
73 152 up20180642
            }
74
        }
75
    }
76
}