Project

General

Profile

Revision 190

fixed sprite, rotation. added scale

View differences:

proj/include/sprite.h
8 8
void      (sprite_dtor)(sprite_t *p);
9 9

  
10 10
void (sprite_set_pos)   (sprite_t *p, int16_t x, int16_t y);
11
void (sprite_set_center)(sprite_t *p, int16_t u0, int16_t v0);
11 12
void (sprite_set_angle) (sprite_t *p, double angle);
12
void (sprite_set_center)(sprite_t *p, int16_t u0, int16_t v0);
13
void (sprite_set_scale) (sprite_t *p, double scale);
13 14

  
14 15
int16_t  (sprite_get_x)(const sprite_t *p);
15 16
int16_t  (sprite_get_y)(const sprite_t *p);
proj/src/proj.c
99 99
        sprite_t *crosshair = get_crosshair();
100 100
        sprite_t *shooter1 = get_shooter();
101 101
        sprite_set_pos(shooter1, graph_get_XRes()/2, graph_get_YRes()/2);
102
        sprite_set_scale(shooter1, 0.5);
102 103
        graph_clear_screen();
103 104
        sprite_draw(crosshair);
104 105
        sprite_draw(shooter1);
proj/src/sprite.c
12 12
    uint16_t w, h;
13 13
    int16_t u0, v0;
14 14
    double theta;
15
    double scale;
15 16
    uint8_t *map;
16 17
};
17 18

  
......
32 33
    ret->u0 = u0;
33 34
    ret->v0 = u0;
34 35
    ret->theta = 0;
36
    ret->scale = 1;
35 37
    return ret;
36 38
}
37 39
void (sprite_dtor)(sprite_t *p){
......
40 42
    free(p);
41 43
}
42 44

  
43
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
void (sprite_set_angle) (sprite_t *p, double angle){ p->theta = angle; }
45
void (sprite_set_pos)   (sprite_t *p, int16_t x , int16_t y ){ p->x = x; p->y = y; }
48 46
void (sprite_set_center)(sprite_t *p, int16_t u0, int16_t v0){ p->u0 = u0; p->v0 = v0; }
49

  
47
void (sprite_set_angle) (sprite_t *p, double angle          ){ p->theta = angle; }
48
void (sprite_set_scale) (sprite_t *p, double scale          ){ p->scale = scale; }
50 49
int16_t  (sprite_get_x)(const sprite_t *p){ return p->x; }
51 50
int16_t  (sprite_get_y)(const sprite_t *p){ return p->y; }
52 51
uint16_t (sprite_get_w)(const sprite_t *p){ return p->w; }
53 52
uint16_t (sprite_get_h)(const sprite_t *p){ return p->h; }
54 53

  
55 54
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 55
    double s = fm_sin(p->theta);
59 56
    double c = fm_cos(p->theta);
60
    *u = dx*c - dy*s + p->u0;
61
    *v = dx*s + dy*c + p->v0;
57
    double dx = (x - p->x)/p->scale;
58
    double dy = (y - p->y)/p->scale;
59
    int16_t du = dx*c - dy*s + 0.5;
60
    int16_t dv = dx*s + dy*c + 0.5;
61
    *u = du + p->u0;
62
    *v = dv + p->v0;
62 63
}
63 64

  
65
void (sprite_pic2src)(const sprite_t *p, int16_t u, int16_t v, int16_t *x, int16_t *y){
66
    double s = fm_sin(p->theta);
67
    double c = fm_cos(p->theta);
68
    int16_t du = u - p->u0;
69
    int16_t dv = v - p->v0;
70
    double dx =  du*c + dv*s;
71
    double dy = -du*s + dv*c;
72
    *x = dx*p->scale + 0.5 + p->x;
73
    *y = dy*p->scale + 0.5 + p->y;
74
}
75

  
64 76
void (sprite_draw)(const sprite_t *p){
65
    const int16_t diag = sqrt(p->w*p->w + p->h*p->h)+2;
77
    int16_t xmin, xmax, ymin, ymax; {
78
        int16_t x, y;
79
        sprite_pic2src(p, 0   , 0   , &x, &y);
80
        xmin = x; xmax = x; ymin = y; ymax = y;
81
        sprite_pic2src(p, p->w, 0   , &x, &y);
82
        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);
84
        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);
86
        xmin = min(x, xmin); xmax = max(x, xmax); ymin = min(y, ymin); ymax = max(y, ymax);
87
        xmin = max(xmin-2, 0); xmax = min(xmax+2, graph_get_XRes());
88
        ymin = max(ymin-2, 0); ymax = min(ymax+2, graph_get_YRes());
89
    }
66 90
    int16_t u, v;
67
    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){
91
    for(int16_t y = ymin; y < ymax; ++y){
92
        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){
69 96
            sprite_src2pic(p, x, y, &u, &v);
70 97
            if(0 <= u && u < p->w && 0 <= v && v < p->h){
71 98
                uint32_t c = *(uint32_t*)(p->map + (v*p->w + u)*4);
proj/xpm/shooter.h
2 2
#include "sprite.h"
3 3

  
4 4
sprite_t* get_shooter(){
5
    return sprite_ctor(shooter_xpm, 24, 24);
5
    return sprite_ctor((const char **)shooter_xpm, 34, 34);
6 6
}

Also available in: Unified diff