Project

General

Profile

Statistics
| Revision:

root / proj / sprite.c @ 162

History | View | Annotate | Download (2.31 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 162 up20180642
sprite_t* sprite_ctor(const char **xpm, int u0, int v0){
17 152 up20180642
    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 162 up20180642
    ret->map = xpm_load((xpm_map_t)xpm, type, &img);
22 152 up20180642
    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 162 up20180642
    ret->u0 = u0;
31
    ret->v0 = u0;
32 160 up20180642
    ret->theta = 0;
33 152 up20180642
    return ret;
34
}
35
void sprite_dtor(sprite_t *p){
36
    if(p == NULL) return;
37
    if(p->map) free(p->map);
38
    free(p);
39
}
40
41
void sprite_set_x(sprite_t *p, int x){ p->x = x; }
42
void sprite_set_y(sprite_t *p, int y){ p->y = y; }
43 162 up20180642
void sprite_set_pos(sprite_t *p, int x, int y){ sprite_set_x(p, x); sprite_set_y(p, y); }
44 160 up20180642
void sprite_set_angle(sprite_t *p, double angle){ p->theta = angle; }
45 162 up20180642
void sprite_set_center(sprite_t *p, int u0, int v0){ p->u0 = u0; p->v0 = v0; }
46 152 up20180642
47
int sprite_get_w(const sprite_t *p){ return p->w; }
48
int sprite_get_h(const sprite_t *p){ return p->h; }
49
50 160 up20180642
void sprite_src2pic(const sprite_t *p, int x, int y, int *u, int *v){
51
    int dx = x - p->x;
52
    int dy = y - p->y;
53 162 up20180642
    //int du = dx*cos(p->theta) - dy*sin(p->theta);
54
    //int dv = dx*sin(p->theta) + dy*cos(p->theta);
55
    //*u = du + p->u0;
56
    //*v = dv + p->v0;
57
    //*u = dx*cos(p->theta) - dy*sin(p->theta) + p->u0;
58
    //*v = dx*sin(p->theta) + dy*cos(p->theta) + p->v0;
59
    double s = sin(p->theta);
60
    double c = cos(p->theta);
61
    *u = dx*c - dy*s + p->u0;
62
    *v = dx*s + dy*c + p->v0;
63 160 up20180642
}
64
65 152 up20180642
void sprite_draw(const sprite_t *p){
66 160 up20180642
    const int diag = sqrt(p->w*p->w + p->h*p->h)+2;
67
    int u, v;
68
    for(int y = p->y - diag; y < p->y + diag; ++y){
69
        for(int x = p->x - diag; x < p->x + diag; ++x){
70
            if (p->x + x < get_XRes() && p->y + y < get_YRes()){
71
                sprite_src2pic(p, x, y, &u, &v);
72
                if(0 <= u && u < p->w && 0 <= v && v < p->h){
73
                    uint8_t *m = p->map + (v*p->w + u)*4;
74
                    uint32_t color = SET_RGB(*(m+2), *(m+1), *(m));
75
                    set_pixel_alpha(p->x + x, p->y + y, color, *(m+3));
76
                }
77 152 up20180642
            }
78
        }
79
    }
80
}