Project

General

Profile

Revision 192

added zoom/scale

View differences:

proj/include/graph.h
37 37
/// DRAW
38 38
int (graph_draw)(void);
39 39

  
40

  
41

  
42
/// RECTANGLE
43

  
44

  
45
int (graph_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color);
46
int (graph_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color);
47

  
48 40
#endif /* end of include guard: GRAPH_H_INCLUDED */
proj/include/proj_func.h
1 1
#ifndef PROJ_FUNC_H_INCLUDED
2 2
#define PROJ_FUNC_H_INCLUDED
3 3

  
4
#include "sprite.h"
4
#include "ent.h"
5 5

  
6 6
/**
7 7
 * @brief Cleans up all memory, unsubscribes interrupts.
......
15 15
/**
16 16
 * @brief Updates movement variables.
17 17
 */
18
void update_movement(sprite_t *p);
18
void update_movement(ent_t *p);
19 19

  
20 20
void update_mouse_position(struct packet *p);
21 21

  
......
30 30
 * @param
31 31
 * @return Angle
32 32
 */
33
double get_mouse_angle(sprite_t *p);
33
double get_mouse_angle(ent_t *p);
34 34

  
35 35
/**
36 36
 * @brief Get horizontal movement direction.
proj/include/sprite.h
1 1
#ifndef SPRITE_H_INCLUDED
2 2
#define SPRITE_H_INCLUDED
3 3

  
4
struct basic_sprite;
5
typedef struct basic_sprite basic_sprite_t;
6

  
7
basic_sprite_t* (basic_sprite_ctor)(const char **xpm, int16_t u0, int16_t v0);
8
void            (basic_sprite_dtor)(basic_sprite_t *p);
9

  
10
const uint8_t* (basic_sprite_get_map)(const basic_sprite_t *p);
11
uint16_t       (basic_sprite_get_w)  (const basic_sprite_t *p);
12
uint16_t       (basic_sprite_get_h)  (const basic_sprite_t *p);
13
int16_t        (basic_sprite_get_u0) (const basic_sprite_t *p);
14
int16_t        (basic_sprite_get_v0) (const basic_sprite_t *p);
15

  
4 16
struct sprite;
5 17
typedef struct sprite sprite_t;
6 18

  
7
sprite_t* (sprite_ctor)(const char **xpm, int u0, int v0);
19
sprite_t* (sprite_ctor)(const basic_sprite_t *bsp);
20
/*
21
 * /!\ WARNING: Entity destructor does not destruct the basic_sprite passed as
22
 * constructor arguments, since it is assumed the same basic_sprite may be used to
23
 * draw several sprites. It is thus YOUR responsibility to delete basic_sprite's.
24
 * @param   p   Pointer to sprite_t to be destructed
25
 */
8 26
void      (sprite_dtor)(sprite_t *p);
9 27

  
10 28
void (sprite_set_pos)   (sprite_t *p, int16_t x, int16_t y);
proj/src/font.c
72 72
        free(ret);
73 73
        return NULL;
74 74
    }
75
    int good = false;
75 76
    char filepath[1024];
76 77
    for(size_t i = 0; i < ret->nchars; ++i){
77 78
        sprintf(filepath, "%s/ascii%03d.xpm2", s, i);
78 79
        char **xpm = xpm_load_xpm2(filepath);
79 80
        ret->glyphs[i] = glyph_ctor((const char**)xpm);
81
        if(ret->glyphs[i] != NULL) good = true;
80 82
    }
81
    return ret;
83
    if(good) return ret;
84
    else{
85
        //font_dtor(ret);
86
        return NULL;
87
    }
82 88
}
83 89
void (font_dtor)(font_t *p){
84 90
    if(p == NULL) return;
proj/src/graph.c
301 301
    memcpy(video_mem, video_buf, graph_get_vram_size());
302 302
    return 0;
303 303
}
304

  
305
/// RECTANGLE
306
int (graph_draw_hline)(uint16_t x, uint16_t y, uint16_t len, uint32_t color){
307
    int r;
308
    for (uint16_t i = 0; i < len; i++)
309
        if ((r = graph_set_pixel(x + i, y, color))) return r;
310
    return SUCCESS;
311
}
312

  
313
int (graph_draw_rectangle)(uint16_t x, uint16_t y,uint16_t width, uint16_t height, uint32_t color)	{
314
    int r;
315
    for (uint16_t i = 0; i < height; i++)
316
        if ((r = graph_draw_hline(x, y + i, width, color))) return r;
317
    return SUCCESS;
318
}
proj/src/proj.c
17 17
#include "sprite.h"
18 18
#include "rectangle.h"
19 19
#include "font.h"
20
#include "ent.h"
20 21

  
21
#ifdef DIOGO
22
    #include "shooter.h"
23
#endif
22
#include "crosshair.h"
23
#include "shooter.h"
24
#include "pistol.h"
25
#include "nothing.h"
24 26

  
25
#ifdef TELMO
26
    #include "crosshair.h"
27
    #include "shooter.h"
28
#endif
29

  
30 27
int main(int argc, char* argv[]) {
31 28

  
32 29
    lcf_set_language("EN-US");
......
46 43

  
47 44
    int r;
48 45

  
46
    font_t *consolas = font_ctor("/home/lcom/labs/proj/font/Consolas/xpm2");
47
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
48

  
49 49
    /// subscribe interrupts
50 50
    if (subscribe_all()) { return 1; }
51 51

  
......
56 56
        return 1;
57 57
    }
58 58

  
59
    font_t *consolas = font_ctor("/home/lcom/labs/proj/font/xpm2");
60

  
61 59
    /// Load stuff
60
    basic_sprite_t *bsp_crosshair = NULL;
61
    basic_sprite_t *bsp_shooter   = NULL;
62
    basic_sprite_t *bsp_pistol    = NULL;
63
    basic_sprite_t *bsp_nothing   = NULL;
64
    sprite_t       *sp_crosshair  = NULL;
62 65
    {
63 66
        graph_clear_screen();
64 67
        text_t *txt = text_ctor(consolas, "Loading...");
65 68
        text_draw(txt);
66 69
        text_dtor(txt);
67 70
        graph_draw();
71

  
72
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
73
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
74
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
75
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
76

  
77
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
68 78
    }
79

  
69 80
    #ifdef DIOGO
70 81
        graph_clear_screen();
71 82

  
......
96 107
    #endif
97 108

  
98 109
    #ifdef TELMO
99
        sprite_t *crosshair = get_crosshair();
100
        sprite_t *shooter1 = get_shooter();
101
        sprite_set_pos(shooter1, graph_get_XRes()/2, graph_get_YRes()/2);
102
        sprite_set_scale(shooter1, 4);
110
        ent_set_scale(2.0);
111

  
112
        ent_t *shooter1 = ent_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
113
        ent_set_pos(shooter1, 0, 0);
114
        ent_set_origin(ent_get_x(shooter1)-ent_get_XLength()/2.0,
115
                       ent_get_y(shooter1)-ent_get_YLength()/2.0);
116

  
117
        ent_t *shooter2 = ent_ctor(bsp_shooter, bsp_nothing);
118
        ent_set_pos(shooter2, -50, -50);
119

  
103 120
        graph_clear_screen();
104
        sprite_draw(crosshair);
105
        sprite_draw(shooter1);
121
        ent_draw(shooter1);
122
        sprite_draw(sp_crosshair);
106 123
        graph_draw();
107 124
    #endif
108 125

  
......
133 150
                                uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
134 151
                                if (no_interrupts % refresh_count_value == 0) {
135 152
                                    update_movement(shooter1);
136
                                    sprite_set_pos(crosshair, get_mouse_X(), get_mouse_Y());
153
                                    ent_set_origin(ent_get_x(shooter1)-ent_get_XLength()/2.0,
154
                                                   ent_get_y(shooter1)-ent_get_YLength()/2.0);
155

  
156
                                    sprite_set_pos(sp_crosshair, get_mouse_X(), get_mouse_Y());
137 157
                                    double angle = get_mouse_angle(shooter1);
138
                                    sprite_set_angle(shooter1, angle - M_PI_2);
158
                                    ent_set_angle(shooter1, angle - M_PI_2);
139 159
                                    graph_clear_screen();
140
                                    sprite_draw(crosshair);
141
                                    sprite_draw(shooter1);
160
                                    ent_draw(shooter2);
161
                                    ent_draw(shooter1);
162
                                    sprite_draw(sp_crosshair);
142 163
                                    graph_draw();
143 164
                                }
144 165
                            }
......
181 202
        #endif
182 203
    }
183 204

  
184
    font_dtor(consolas);
205
    #ifdef TELMO
206
        ent_dtor(shooter1); shooter1 = NULL;
207
    #endif
185 208

  
209
    basic_sprite_dtor(bsp_crosshair); bsp_crosshair = NULL;
210
    basic_sprite_dtor(bsp_shooter  ); bsp_shooter   = NULL;
211
    sprite_dtor      (sp_crosshair ); sp_crosshair  = NULL;
212
    font_dtor        (consolas     ); consolas      = NULL;
213

  
186 214
    // Unsubscribe interrupts
187 215
    if (unsubscribe_all()) {
188 216
        if (cleanup())
proj/src/proj_func.c
43 43
    hor_mov = d_pressed - a_pressed;
44 44
}
45 45

  
46
void update_movement(sprite_t *p) {
47

  
46
void update_movement(ent_t *p) {
48 47
    static const int speed = 5;
49
    sprite_set_pos(p, sprite_get_x(p) + speed * hor_mov, sprite_get_y(p) + speed * ver_mov);
48
    ent_set_pos(p, ent_get_x(p) + speed * hor_mov, ent_get_y(p) + speed * ver_mov);
50 49
}
51 50

  
52 51
static int32_t mouse_x = 0, mouse_y = 0;
......
63 62

  
64 63
int32_t get_mouse_Y(void) { return mouse_y; }
65 64

  
66
double get_mouse_angle(sprite_t *p) {
67
    return atan2(sprite_get_y(p) - mouse_y, mouse_x - sprite_get_x(p));
65
double get_mouse_angle(ent_t *p) {
66
    return atan2(ent_get_y_screen(p) - mouse_y, mouse_x - ent_get_x_screen(p));
68 67
}
69 68

  
70 69
int get_hor_movement(void) { return hor_mov; }
proj/src/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
        }
proj/src/xpm_utils.c
21 21
char** xpm_load_xpm2(const char *fpath){
22 22
    FILE *f = fopen(fpath, "r");
23 23
    if(f == NULL) return NULL;
24
    char *line_buf = malloc(1024*sizeof(char)); size_t len = 1024;
24
    size_t len = 1024; char *line_buf = malloc(len*sizeof(char));
25 25
    int sz;
26 26

  
27 27
    char **ret = NULL;
proj/xpm/crosshair.h
1 1
#include "crosshair.xpm"
2 2
#include "sprite.h"
3 3

  
4
sprite_t* get_crosshair(){
5
    return sprite_ctor(crosshair_xpm, 16, 16);
4
basic_sprite_t* get_crosshair(){
5
    return basic_sprite_ctor(crosshair_xpm, 16, 16);
6 6
}
proj/xpm/shooter.h
1
#ifndef SHOOTER_H_INCLUDED
2
#define SHOOTER_H_INCLUDED
3

  
1 4
#include "shooter.xpm"
2 5
#include "sprite.h"
3 6

  
4
sprite_t* get_shooter(){
5
    return sprite_ctor((const char **)shooter_xpm, 34, 34);
7
basic_sprite_t* get_shooter(void){
8
    return basic_sprite_ctor((const char **)shooter_xpm, 34, 34);
6 9
}
10

  
11
#endif //SHOOTER_H_INCLUDED
proj/DR.mk
2 2

  
3 3
.PATH: ${.CURDIR}/src
4 4

  
5
SRCS= proj.c graph.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c
5
SRCS= proj.c graph.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c
6 6

  
7 7
CPPFLAGS += -pedantic -I./include -I./bmp -I./xpm -D LCOM_MACRO -D DIOGO -D __LCOM_OPTIMIZED_
8 8

  
proj/Makefile
2 2

  
3 3
.PATH: ${.CURDIR}/src
4 4

  
5
SRCS= proj.c graph.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c
5
SRCS= proj.c graph.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c
6 6

  
7 7
CPPFLAGS += -pedantic -I./include -I./bmp -I./xpm -D LCOM_MACRO -D __LCOM_OPTIMIZED_
8 8

  
proj/TB.mk
2 2

  
3 3
.PATH: ${.CURDIR}/src
4 4

  
5
SRCS= proj.c graph.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c
5
SRCS= proj.c graph.c sprite.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c proj_func.c fast_math.c rectangle.c font.c xpm_utils.c ent.c
6 6

  
7 7
CPPFLAGS += -pedantic -I./include -I./bmp -I./xpm -D LCOM_MACRO -D TELMO #-D __LCOM_OPTIMIZED_
8 8

  

Also available in: Unified diff