Project

General

Profile

Revision 216

many changes. implemented collision detection

View differences:

proj/xpm/map.h
1
#ifndef MAP_H_INCLUDED
2
#define MAP_H_INCLUDED
3

  
4
#include "map.xpm"
5
#include "sprite.h"
6

  
7
basic_sprite_t* get_map(void){
8
    return basic_sprite_ctor((const char **)map_xpm, 0, 0);
9
}
10

  
11
#endif /* end of include guard: MAP_H_INCLUDED */
12 0

  
proj/include/ent.h
14 14
typedef struct gunner gunner_t;
15 15
gunner_t* (gunner_ctor)(basic_sprite_t *dude, basic_sprite_t *weapon);
16 16
void      (gunner_dtor)(gunner_t *p);
17
void (gunner_set_pos)  (gunner_t *p, int16_t x, int16_t y);
18
void (gunner_set_angle)(gunner_t *p, double angle        );
17
void (gunner_set_pos)  (gunner_t *p, double x, double y);
18
void (gunner_set_angle)(gunner_t *p, double angle      );
19 19
double  (gunner_get_x)       (const gunner_t *p);
20 20
double  (gunner_get_y)       (const gunner_t *p);
21 21
int16_t (gunner_get_x_screen)(const gunner_t *p);
......
26 26
typedef struct bullet bullet_t;
27 27
bullet_t* (bullet_ctor)(basic_sprite_t *b);
28 28
void      (bullet_dtor)(bullet_t *p);
29
void (bullet_set_pos)  (bullet_t *p, int16_t x, int16_t y);
29
void (bullet_set_pos)  (bullet_t *p, double x, double y);
30 30
void (bullet_set_angle)(bullet_t *p, double angle        );
31 31
double  (bullet_get_x)       (const bullet_t *p);
32 32
double  (bullet_get_y)       (const bullet_t *p);
......
34 34
int16_t (bullet_get_y_screen)(const bullet_t *p);
35 35
void (bullet_draw)(bullet_t *p);
36 36

  
37
struct map;
38
typedef struct map map_t;
39
map_t* (map_ctor)(const char **background, const char **collide);
40
void   (map_dtor)(map_t *p);
41
int    (map_collides)(const map_t *p, double x, double y);
42
void   (map_draw)(map_t *p);
43

  
37 44
#endif //ENT_H_INCLUDED
proj/include/graph_macros.h
31 31
#define BLACK               0x000000
32 32
#define WHITE               0xFFFFFF
33 33

  
34
// Alpha
35
#define ALPHA_THRESHOLD     0x7F
36

  
34 37
#endif /* end of include guard: GRAPHICS_MACROS_H_INCLUDED */
proj/include/proj_macros.h
15 15
#define CTRL_MAKE_CODE      0x1D    /** @brief CTRL Make Code */
16 16
#define CTRL_BREAK_CODE     0x9D    /** @brief CTRL Break Code */
17 17
#define PLUS_MAKE_CODE      0x1A    /** @brief Plus (+) Make Code */
18
#define PLUS_BREAK_CODE      0x9A   /** @brief Plus (+) Break Code */
18
#define PLUS_BREAK_CODE     0x9A    /** @brief Plus (+) Break Code */
19 19
#define MINUS_MAKE_CODE     0x35    /** @brief Minus (-) Make Code */
20 20
#define MINUS_BREAK_CODE    0xB5    /** @brief Minus (-) Break Code */
21 21

  
proj/include/sprite.h
6 6

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

  
10 9
const uint8_t* (basic_sprite_get_map)(const basic_sprite_t *p);
11 10
uint16_t       (basic_sprite_get_w)  (const basic_sprite_t *p);
12 11
uint16_t       (basic_sprite_get_h)  (const basic_sprite_t *p);
13 12
int16_t        (basic_sprite_get_u0) (const basic_sprite_t *p);
14 13
int16_t        (basic_sprite_get_v0) (const basic_sprite_t *p);
14
/*
15
struct basic_sprite_alpha;
16
typedef struct basic_sprite_alpha basic_sprite_alpha_t;
15 17

  
18
basic_sprite_alpha_t* (basic_sprite_alpha_ctor)(const char **xpm, int16_t u0, int16_t v0);
19
void                  (basic_sprite_alpha_dtor)(basic_sprite_alpha_t *p);
20
const uint8_t*        (basic_sprite_alpha_get_map)(const basic_sprite_alpha_t *p);
21
uint16_t              (basic_sprite_alpha_get_w)  (const basic_sprite_alpha_t *p);
22
uint16_t              (basic_sprite_alpha_get_h)  (const basic_sprite_alpha_t *p);
23
int16_t               (basic_sprite_alpha_get_u0) (const basic_sprite_alpha_t *p);
24
int16_t               (basic_sprite_alpha_get_v0) (const basic_sprite_alpha_t *p);
25
*/
16 26
struct sprite;
17 27
typedef struct sprite sprite_t;
18 28

  
proj/maps/map1.h
1
#ifndef MAP1_H_INCLUDED
2
#define MAP1_H_INCLUDED
3

  
4
#include "map1.xpm"
5
#include "map1_collide.xpm"
6
#include "sprite.h"
7

  
8
map_t* get_map1(void){
9
    return map_ctor((const char **)map1_xpm, (const char **)map1_collide_xpm);
10
}
11

  
12
#endif /* end of include guard: MAP_H_INCLUDED */
0 13

  
proj/src/ent.c
3 3
#include "ent.h"
4 4

  
5 5
#include "graph.h"
6
#include "sprite.h"
6 7

  
7 8
static double scale = 1.0;
8 9
static int16_t x_origin = 0;
......
37 38
    sprite_dtor(p->weapon);
38 39
    free(p);
39 40
}
40
void (gunner_set_pos)  (gunner_t *p, int16_t x, int16_t y){ p->x = x; p->y = y; }
41
void (gunner_set_angle)(gunner_t *p, double angle        ){
41
void (gunner_set_pos)  (gunner_t *p, double x, double y){ p->x = x; p->y = y; }
42
void (gunner_set_angle)(gunner_t *p, double angle      ){
42 43
    sprite_set_angle(p->dude  , angle);
43 44
    sprite_set_angle(p->weapon, angle);
44 45
}
......
77 78
    sprite_dtor(p->b);
78 79
    free(p);
79 80
}
80
void (bullet_set_pos)  (bullet_t *p, int16_t x, int16_t y){ p->x = x; p->y = y; }
81
void (bullet_set_angle)(bullet_t *p, double angle        ){ sprite_set_angle(p->b, angle); }
81
void (bullet_set_pos)  (bullet_t *p, double x, double y){ p->x = x; p->y = y; }
82
void (bullet_set_angle)(bullet_t *p, double angle      ){ sprite_set_angle(p->b, angle); }
82 83
double  (bullet_get_x)       (const bullet_t *p){ return p->x; }
83 84
double  (bullet_get_y)       (const bullet_t *p){ return p->y; }
84 85
int16_t (bullet_get_x_screen)(const bullet_t *p){ return (p->x-x_origin)*scale; }
......
90 91
    sprite_set_scale(p->b, scale);
91 92
    sprite_draw     (p->b);
92 93
}
94

  
95
struct map{
96
    basic_sprite_t *bsp_background;
97
    sprite_t *background;
98
    uint8_t *collide;
99
};
100
map_t* (map_ctor)(const char **background, const char **collide){
101
    map_t *ret = malloc(sizeof(map_t));
102
    if(ret == NULL) return NULL;
103

  
104
    ret->bsp_background = NULL;
105
    ret->background     = NULL;
106
    ret->collide        = NULL;
107

  
108
    ret->bsp_background = basic_sprite_ctor(background, 0, 0);
109
    ret->background     = sprite_ctor(ret->bsp_background);
110
    if(ret->bsp_background == NULL ||
111
        ret->background     == NULL){ map_dtor(ret); return NULL; }
112

  
113
    basic_sprite_t *bsp_collide = basic_sprite_ctor(collide, 0, 0);
114
    if(bsp_collide == NULL){ map_dtor(ret); return NULL; }
115
    const uint16_t W = basic_sprite_get_w(bsp_collide);
116
    const uint16_t H = basic_sprite_get_h(bsp_collide);
117
    ret->collide = malloc(W*H*sizeof(uint8_t));
118
    if(ret->collide == NULL){ map_dtor(ret); return NULL; }
119
    const uint8_t *m = basic_sprite_get_map(bsp_collide);
120
    for(unsigned i = 0; i < W*H; ++i){
121
        ret->collide[i] = (m[4*i+3] < ALPHA_THRESHOLD ? 1 : 0);
122
    }
123
    basic_sprite_dtor(bsp_collide);
124

  
125
    return ret;
126
}
127
void (map_dtor)(map_t *p){
128
    if(p == NULL) return;
129
    sprite_dtor(p->background);
130
    basic_sprite_dtor(p->bsp_background);
131
    free(p->collide);
132
    free(p);
133
}
134
int16_t (map_get_x_screen)(const map_t *p){ return (-x_origin)*scale; }
135
int16_t (map_get_y_screen)(const map_t *p){ return (-y_origin)*scale; }
136
int (map_collides)(const map_t *p, double x, double y){
137
    const uint16_t w = sprite_get_w(p->background), h = sprite_get_h(p->background);
138
    int16_t x_ = x, y_ = y;
139
    if(x_ < 0 || w <= x_ || y_ < 0 || h <= y_) return 0;
140
    uint32_t pos = x_ + y_*w;
141
    return p->collide[pos];
142
}
143
void   (map_draw)(map_t *p){
144
    const int16_t x_screen = map_get_x_screen(p);
145
    const int16_t y_screen = map_get_y_screen(p);
146
    sprite_set_pos  (p->background, x_screen, y_screen);
147
    sprite_set_scale(p->background, scale);
148
    sprite_draw     (p->background);
149
}
proj/src/graph.c
271 271
    uint16_t w, h;
272 272
    int16_t u0, v0;
273 273
};
274

  
275 274
basic_sprite_t* (basic_sprite_ctor)(const char **xpm, int16_t u0, int16_t v0){
276 275
    basic_sprite_t *ret = malloc(sizeof(basic_sprite_t));
277 276
    if(ret == NULL) return NULL;
......
279 278
    xpm_image_t img;
280 279
    ret->map = xpm_load((xpm_map_t)xpm, type, &img);
281 280
    if(ret->map == NULL){
282
        free(ret);
281
        basic_sprite_dtor(ret);
283 282
        return NULL;
284 283
    }
285 284
    ret->w = img.width;
......
293 292
    free(p->map);
294 293
    free(p);
295 294
}
296

  
297 295
const uint8_t* (basic_sprite_get_map)(const basic_sprite_t *p){ return p->map; }
298 296
uint16_t       (basic_sprite_get_w)  (const basic_sprite_t *p){ return p->w  ; }
299 297
uint16_t       (basic_sprite_get_h)  (const basic_sprite_t *p){ return p->h  ; }
300 298
int16_t        (basic_sprite_get_u0) (const basic_sprite_t *p){ return p->u0 ; }
301 299
int16_t        (basic_sprite_get_v0) (const basic_sprite_t *p){ return p->v0 ; }
302 300

  
301
/*
302
struct basic_sprite_alpha{
303
    uint8_t *map;
304
    uint16_t w, h;
305
    int16_t u0, v0;
306
};
307
basic_sprite_alpha_t* (basic_sprite_alpha_ctor)(const char **xpm, int16_t u0, int16_t v0){
308
    basic_sprite_alpha_t *ret = malloc(sizeof(basic_sprite_t));
309
    if(ret == NULL) return NULL;
310
    enum xpm_image_type type = XPM_8_8_8_8;
311
    xpm_image_t img;
312
    ret->map = NULL;
313
    uint8_t *m = xpm_load((xpm_map_t)xpm, type, &img);
314
    if(m == NULL){
315
        basic_sprite_alpha_dtor(ret);
316
        return NULL;
317
    }
318
    ret->map = m;
319
    if(ret->map == NULL){
320
        basic_sprite_alpha_dtor(ret);
321
        return NULL;
322
    }
323
    ret->w = img.width;
324
    ret->h = img.height;
325
    ret->u0 = u0;
326
    ret->v0 = v0;
327
    return ret;
328
}
329
void (basic_sprite_alpha_dtor)(basic_sprite_alpha_t *p){
330
    if(p == NULL) return;
331
    free(p->map);
332
    free(p);
333
}
334
const uint8_t* (basic_sprite_alpha_get_map)(const basic_sprite_alpha_t *p){ return p->map; }
335
uint16_t       (basic_sprite_alpha_get_w)  (const basic_sprite_alpha_t *p){ return p->w  ; }
336
uint16_t       (basic_sprite_alpha_get_h)  (const basic_sprite_alpha_t *p){ return p->h  ; }
337
int16_t        (basic_sprite_alpha_get_u0) (const basic_sprite_alpha_t *p){ return p->u0 ; }
338
int16_t        (basic_sprite_alpha_get_v0) (const basic_sprite_alpha_t *p){ return p->v0 ; }
339
*/
340

  
303 341
struct sprite{
304 342
    const basic_sprite_t *bsp;
305 343
    int16_t x, y; //position in screen
306 344
    double theta, s, c;
307 345
    double scale;
308 346
};
309

  
310 347
sprite_t* (sprite_ctor)(const basic_sprite_t *bsp){
311 348
    sprite_t *ret = malloc(sizeof(sprite_t));
312 349
    if(ret == NULL) return NULL;
......
321 358
    if(p == NULL) return;
322 359
    free(p);
323 360
}
324

  
325 361
void (sprite_set_pos)   (sprite_t *p, int16_t x , int16_t y ){ p->x = x; p->y = y; }
326 362
void (sprite_set_angle) (sprite_t *p, double angle          ){ p->theta = angle; p->c = fm_cos(p->theta); p->s = fm_sin(p->theta); }
327 363
void (sprite_set_scale) (sprite_t *p, double scale          ){ p->scale = scale; }
328 364
int16_t  (sprite_get_x)(const sprite_t *p){ return p->x; }
329 365
int16_t  (sprite_get_y)(const sprite_t *p){ return p->y; }
330

  
366
uint16_t (sprite_get_w)(const sprite_t *p){ return basic_sprite_get_w(p->bsp); }
367
uint16_t (sprite_get_h)(const sprite_t *p){ return basic_sprite_get_h(p->bsp); }
331 368
void (sprite_src2pic)(const sprite_t *p, int16_t x, int16_t y, int16_t *u, int16_t *v){
332 369
    double dx = (x - p->x)/p->scale;
333 370
    double dy = (y - p->y)/p->scale;
......
336 373
    *u = du + basic_sprite_get_u0(p->bsp);
337 374
    *v = dv + basic_sprite_get_v0(p->bsp);
338 375
}
339

  
340 376
void (sprite_pic2src)(const sprite_t *p, int16_t u, int16_t v, int16_t *x, int16_t *y){
341 377
    int16_t du = u - basic_sprite_get_u0(p->bsp);
342 378
    int16_t dv = v - basic_sprite_get_v0(p->bsp);
......
345 381
    *x = dx*p->scale + 0.5 + p->x;
346 382
    *y = dy*p->scale + 0.5 + p->y;
347 383
}
348

  
349 384
void (sprite_draw)(const sprite_t *p){
350 385
    const uint16_t w = basic_sprite_get_w(p->bsp);
351 386
    const uint16_t h = basic_sprite_get_h(p->bsp);
......
370 405
            sprite_src2pic(p, x, y, &u, &v);
371 406
            if(0 <= u && u < w && 0 <= v && v < h){
372 407
                const uint8_t *c_p = map+(v*w+u)*4;
373
                if(*(c_p+3) < 0x7F) //alpha
408
                if(*(c_p+3) < ALPHA_THRESHOLD) //alpha
374 409
                    memcpy(place, c_p, bytes_pixel);
375 410
            }
376 411
        }
proj/src/proj.c
24 24
#include "pistol.h"
25 25
#include "nothing.h"
26 26
#include "bullet.h"
27
#include "map.h"
27
#include "map1.h"
28 28

  
29 29
int main(int argc, char* argv[]) {
30 30

  
......
59 59
    }
60 60

  
61 61
    /// Load stuff
62
    basic_sprite_t *bsp_crosshair = NULL;
63
    basic_sprite_t *bsp_shooter   = NULL;
64
    basic_sprite_t *bsp_pistol    = NULL;
65
    basic_sprite_t *bsp_nothing   = NULL;
66
    sprite_t       *sp_crosshair  = NULL;
62
    basic_sprite_t       *bsp_crosshair = NULL;
63
    basic_sprite_t       *bsp_shooter   = NULL;
64
    basic_sprite_t       *bsp_pistol    = NULL;
65
    basic_sprite_t       *bsp_nothing   = NULL;
66
    map_t                *map1      = NULL;
67
    sprite_t             *sp_crosshair  = NULL;
67 68
    {
68 69
        graph_clear_screen();
69 70
        text_t *txt = text_ctor(consolas, "Loading...");
......
75 76
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
76 77
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
77 78
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
79
        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
78 80

  
79 81
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
80 82
    }
......
109 111
    #endif
110 112

  
111 113
    #ifdef TELMO
112
        ent_set_scale(10.0);
114
        ent_set_scale(DEFAULT_SCALE);
113 115

  
114 116
        gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
115 117
        gunner_set_pos(shooter1, 0, 0);
......
120 122
        bullet_t *bullet = bullet_ctor(get_bullet());
121 123
        bullet_set_pos(bullet, 400, 400);
122 124

  
123
        ent_set_origin(bullet_get_x(bullet)-ent_get_XLength()/2.0,
124
                       bullet_get_y(bullet)-ent_get_YLength()/2.0);
125
        ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
126
                       gunner_get_y(shooter1)-ent_get_YLength()/2.0);
125 127

  
126
        graph_clear_screen();
127
        gunner_draw(shooter1);
128
        sprite_draw(sp_crosshair);
129
        graph_draw();
130

  
131 128
        uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
132

  
133
        basic_sprite_t *bsp_pink = get_map();
134
        sprite_t *pink = sprite_ctor(bsp_pink);
135
        basic_sprite_dtor(bsp_pink);
136 129
    #endif
137 130

  
138 131
    /// loop stuff
......
161 154
                            if (i == 0) {
162 155
                                if (no_interrupts % refresh_count_value == 0) {
163 156
                                    update_movement(shooter1);
157

  
158
                                    if(map_collides(map1, gunner_get_x(shooter1), gunner_get_y(shooter1))){
159
                                        printf("COLLIDING\n");
160
                                    }
161

  
164 162
                                    update_scale();
165 163

  
166
                                    ent_set_origin(bullet_get_x(bullet)-ent_get_XLength()/2.0,
167
                                                   bullet_get_y(bullet)-ent_get_YLength()/2.0);
164
                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
165
                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
168 166

  
169 167
                                    sprite_set_pos(sp_crosshair, get_mouse_X(), get_mouse_Y());
170 168
                                    double angle = get_mouse_angle(shooter1);
......
173 171

  
174 172
                                    clock_t t = clock();
175 173

  
176
                                    sprite_draw(pink);
174
                                    map_draw   (map1);
177 175
                                    gunner_draw(shooter2);
178 176
                                    gunner_draw(shooter1);
179 177
                                    bullet_draw(bullet);
......
207 205

  
208 206
    #ifdef TELMO
209 207
        gunner_dtor(shooter1); shooter1 = NULL;
210
        sprite_dtor(pink); pink = NULL;
208
        gunner_dtor(shooter2); shooter2 = NULL;
209
        bullet_dtor(bullet); bullet = NULL;
211 210
    #endif
212 211

  
213
    basic_sprite_dtor(bsp_crosshair); bsp_crosshair = NULL;
214
    basic_sprite_dtor(bsp_shooter  ); bsp_shooter   = NULL;
215
    sprite_dtor      (sp_crosshair ); sp_crosshair  = NULL;
216
    font_dtor        (consolas     ); consolas      = NULL;
212
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
213
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
214
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
215
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
216
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
217
    map_dtor               (map1         ); map1          = NULL;
218
    font_dtor              (consolas     ); consolas      = NULL;
217 219

  
218 220
    // Unsubscribe interrupts
219 221
    if (unsubscribe_all()) {
proj/DR.mk
4 4

  
5 5
SRCS= proj.c graph.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
CPPFLAGS += -pedantic -I./include -I./bmp -I./xpm -D LCOM_MACRO -D DIOGO -D __LCOM_OPTIMIZED_
7
CPPFLAGS += -pedantic -I./include -I./maps -I./xpm -D LCOM_MACRO -D DIOGO -D __LCOM_OPTIMIZED_
8 8

  
9 9
DPADD += ${LIBLCF}
10 10
LDADD += -llcf
proj/Makefile
4 4

  
5 5
SRCS= proj.c graph.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
CPPFLAGS += -pedantic -I./include -I./bmp -I./xpm -D LCOM_MACRO -D __LCOM_OPTIMIZED_
7
CPPFLAGS += -pedantic -I./include -I./maps -I./xpm -D LCOM_MACRO -D __LCOM_OPTIMIZED_
8 8

  
9 9
DPADD += ${LIBLCF}
10 10
LDADD += -llcf
proj/TB.mk
4 4

  
5 5
SRCS= proj.c graph.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
CPPFLAGS += -pedantic -I./include -I./bmp -I./xpm -D LCOM_MACRO -D TELMO #-D __LCOM_OPTIMIZED_
7
CPPFLAGS += -pedantic -I./include -I./maps -I./xpm -D LCOM_MACRO -D TELMO #-D __LCOM_OPTIMIZED_
8 8

  
9 9
DPADD += ${LIBLCF}
10 10
LDADD += -llcf

Also available in: Unified diff