Project

General

Profile

Revision 192

added zoom/scale

View differences:

sprite.c
7 7
#include "fast_math.h"
8 8
#include <math.h>
9 9

  
10
struct sprite{
11
    int16_t x, y;
10
struct basic_sprite{
11
    uint8_t *map;
12 12
    uint16_t w, h;
13 13
    int16_t u0, v0;
14
    double theta;
15
    double scale;
16
    uint8_t *map;
17 14
};
18 15

  
19
sprite_t* (sprite_ctor)(const char **xpm, int u0, int v0){
20
    sprite_t *ret = (sprite_t*)malloc(sizeof(sprite_t));
16
basic_sprite_t* (basic_sprite_ctor)(const char **xpm, int16_t u0, int16_t v0){
17
    basic_sprite_t *ret = malloc(sizeof(basic_sprite_t));
21 18
    if(ret == NULL) return NULL;
22 19
    enum xpm_image_type type = XPM_8_8_8_8;
23 20
    xpm_image_t img;
......
26 23
        free(ret);
27 24
        return NULL;
28 25
    }
29
    ret->x = 0;
30
    ret->y = 0;
31 26
    ret->w = img.width;
32 27
    ret->h = img.height;
33 28
    ret->u0 = u0;
34
    ret->v0 = u0;
35
    ret->theta = 0;
36
    ret->scale = 1;
29
    ret->v0 = v0;
37 30
    return ret;
38 31
}
39
void (sprite_dtor)(sprite_t *p){
32
void (basic_sprite_dtor)(basic_sprite_t *p){
40 33
    if(p == NULL) return;
41 34
    free(p->map);
42 35
    free(p);
43 36
}
44 37

  
38
const uint8_t* (basic_sprite_get_map)(const basic_sprite_t *p){ return p->map; }
39
uint16_t       (basic_sprite_get_w)  (const basic_sprite_t *p){ return p->w  ; }
40
uint16_t       (basic_sprite_get_h)  (const basic_sprite_t *p){ return p->h  ; }
41
int16_t        (basic_sprite_get_u0) (const basic_sprite_t *p){ return p->u0 ; }
42
int16_t        (basic_sprite_get_v0) (const basic_sprite_t *p){ return p->v0 ; }
43

  
44
struct sprite{
45
    const basic_sprite_t *bsp;
46
    int16_t x, y; //position in screen
47
    double theta;
48
    double scale;
49
};
50

  
51
sprite_t* (sprite_ctor)(const basic_sprite_t *bsp){
52
    sprite_t *ret = malloc(sizeof(sprite_t));
53
    if(ret == NULL) return NULL;
54
    ret->bsp = bsp;
55
    ret->x = 0;
56
    ret->y = 0;
57
    ret->theta = 0.0;
58
    ret->scale = 1.0;
59
    return ret;
60
}
61
void (sprite_dtor)(sprite_t *p){
62
    if(p == NULL) return;
63
    free(p);
64
}
65

  
45 66
void (sprite_set_pos)   (sprite_t *p, int16_t x , int16_t y ){ p->x = x; p->y = y; }
46
void (sprite_set_center)(sprite_t *p, int16_t u0, int16_t v0){ p->u0 = u0; p->v0 = v0; }
47 67
void (sprite_set_angle) (sprite_t *p, double angle          ){ p->theta = angle; }
48 68
void (sprite_set_scale) (sprite_t *p, double scale          ){ p->scale = scale; }
49 69
int16_t  (sprite_get_x)(const sprite_t *p){ return p->x; }
50 70
int16_t  (sprite_get_y)(const sprite_t *p){ return p->y; }
51
uint16_t (sprite_get_w)(const sprite_t *p){ return p->w; }
52
uint16_t (sprite_get_h)(const sprite_t *p){ return p->h; }
53 71

  
54 72
void (sprite_src2pic)(const sprite_t *p, int16_t x, int16_t y, int16_t *u, int16_t *v){
55 73
    double s = fm_sin(p->theta);
......
58 76
    double dy = (y - p->y)/p->scale;
59 77
    int16_t du = dx*c - dy*s + 0.5;
60 78
    int16_t dv = dx*s + dy*c + 0.5;
61
    *u = du + p->u0;
62
    *v = dv + p->v0;
79
    *u = du + basic_sprite_get_u0(p->bsp);
80
    *v = dv + basic_sprite_get_v0(p->bsp);
63 81
}
64 82

  
65 83
void (sprite_pic2src)(const sprite_t *p, int16_t u, int16_t v, int16_t *x, int16_t *y){
66 84
    double s = fm_sin(p->theta);
67 85
    double c = fm_cos(p->theta);
68
    int16_t du = u - p->u0;
69
    int16_t dv = v - p->v0;
86
    int16_t du = u - basic_sprite_get_u0(p->bsp);
87
    int16_t dv = v - basic_sprite_get_v0(p->bsp);
70 88
    double dx =  du*c + dv*s;
71 89
    double dy = -du*s + dv*c;
72 90
    *x = dx*p->scale + 0.5 + p->x;
......
74 92
}
75 93

  
76 94
void (sprite_draw)(const sprite_t *p){
95
    const uint16_t w = basic_sprite_get_w(p->bsp);
96
    const uint16_t h = basic_sprite_get_h(p->bsp);
77 97
    int16_t xmin, xmax, ymin, ymax; {
78 98
        int16_t x, y;
79
        sprite_pic2src(p, 0   , 0   , &x, &y);
99
        sprite_pic2src(p, 0, 0, &x, &y);
80 100
        xmin = x; xmax = x; ymin = y; ymax = y;
81
        sprite_pic2src(p, p->w, 0   , &x, &y);
101
        sprite_pic2src(p, w, 0, &x, &y);
82 102
        xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax);
83
        sprite_pic2src(p, 0   , p->h, &x, &y);
103
        sprite_pic2src(p, 0, h, &x, &y);
84 104
        xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax);
85
        sprite_pic2src(p, p->w, p->h, &x, &y);
105
        sprite_pic2src(p, w, h, &x, &y);
86 106
        xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax);
87 107
        xmin = max(xmin-2, 0); xmax = min(xmax+2, graph_get_XRes());
88 108
        ymin = max(ymin-2, 0); ymax = min(ymax+2, graph_get_YRes());
89 109
    }
110
    const uint8_t *map = basic_sprite_get_map(p->bsp);
90 111
    int16_t u, v;
91 112
    for(int16_t y = ymin; y < ymax; ++y){
92 113
        for(int16_t x = xmin; x < xmax; ++x){
93
    //const int16_t diag = p->scale*sqrt(p->w*p->w + p->h*p->h)+2;
94
    //for(int16_t y = max(0,p->y-diag); y < min(p->y+diag,graph_get_YRes()); ++y){
95
    //    for(int16_t x = max(0,p->x-diag); x < min(p->x+diag,graph_get_XRes()); ++x){
96 114
            sprite_src2pic(p, x, y, &u, &v);
97
            if(0 <= u && u < p->w && 0 <= v && v < p->h){
98
                uint32_t c = *(uint32_t*)(p->map + (v*p->w + u)*4);
115
            if(0 <= u && u < w && 0 <= v && v < h){
116
                uint32_t c = *(uint32_t*)(map + (v*w + u)*4);
99 117
                graph_set_pixel_alpha(x, y, GET_COLOR(c), GET_ALP(c));
100 118
            }
101 119
        }

Also available in: Unified diff