Project

General

Profile

Revision 345

reorganizing

View differences:

proj/include/ent.h
329 329
typedef struct map map_t;
330 330
/**
331 331
 * @brief Construct map.
332
 * @param   backgroup   XPM describing map as it should be rendered
332
 * @param   background  XPM describing map as it should be rendered
333 333
 * @param   collide     XPM with transparency describing what parts of the map will cause collision
334 334
 * @return  Pointer to constructed map, or NULL if failed
335 335
 */
......
382 382
void   (map_draw)(map_t *p);
383 383
/**
384 384
 * @brief Evaluate if point collides with map.
385
 * @param   p   Pointer to map
385 386
 * @param   x   X-position of the point
386 387
 * @param   y   Y-position of the point
387 388
 * @return true if the point collides with the map, false otherwise
proj/include/interrupts_func.h
8 8
 * @{
9 9
 */
10 10

  
11
#include "timer.h"
12
#include "kbc.h"
13
#include "keyboard.h"
14
#include "mouse.h"
15

  
11 16
/**
12 17
 * @brief Subscribes all drivers used (timer->keyboard->mouse)
13 18
 * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
......
28 33
 */
29 34
void interrupt_handler(uint8_t handler);
30 35

  
36
/**
37
 * @brief Blocking function that waits for an interrupt to occur.
38
 *
39
 * Rewrites the provided argument with the interrupt vector it gets.
40
 *
41
 * This function decisively reduces the complexity of an interrupt cycle, allowing
42
 * for each menu to have its own, simplified interrupt cycle.
43
 * @param   p   Pointer where interrupt vector will be saved
44
 */
31 45
int get_interrupts_vector(uint64_t *p);
32 46

  
33 47
/**
proj/include/makecode_map.h
10 10

  
11 11
#include <stdint.h>
12 12

  
13
#define BASE_CODE    0x02
13
/// @brief Error code
14 14
#define ERROR_CODE      0
15 15

  
16 16
/**
17 17
 * @brief Maps make code into char
18 18
 * @param code      Make code to map.
19
 * @return 0 if the char isn't valid, otherwise the character.
19
 * @return ERROR_CODE if code char isn't valid, otherwise the character.
20 20
 */
21 21
char (map_makecode)(uint8_t code);
22 22

  
proj/include/proj.h
1
#ifndef PROJ_H_INCLUDED
2
#define PROJ_H_INCLUDED
3

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

  
7
basic_sprite_t       *bsp_crosshair;
8
basic_sprite_t       *bsp_shooter  ;
9
basic_sprite_t       *bsp_zombie   ;
10
basic_sprite_t       *bsp_pistol   ;
11
basic_sprite_t       *bsp_nothing  ;
12
basic_sprite_t       *bsp_bullet   ;
13
map_t                *map1         ;
14
sprite_t             *sp_crosshair ;
15

  
16
#endif //PROJ_H_INCLUDED
0 17

  
proj/include/proj_func.h
21 21
 */
22 22
int cleanup(void);
23 23

  
24
/**
25
 * @brief Update key presses.
26
 */
24 27
void update_key_presses(void);
28
/**
29
 * @brief Get key presses.
30
 * @return  keys_t containing key presses
31
 */
32
keys_t* (get_key_presses)(void);
25 33

  
26 34
/**
35
 * @brief Update mouse.
36
 */
37
void update_mouse(struct packet *p);
38
/**
39
 * @brief Get pointer to mouse X-position.
40
 * @return  Pointer to mouse X-position.
41
 */
42
int16_t* get_mouse_X(void);
43
/**
44
 * @brief Get pointer to mouse Y-position.
45
 * @return  Pointer to mouse Y-position.
46
 */
47
int16_t* get_mouse_Y(void);
48
/**
49
 * @brief Get cursor angle relative to a gunner in the screen.
50
 * @param   p   Pointer to gunner
51
 * @return      Angle of the mouse relative to the gunner
52
 */
53
double get_mouse_angle(gunner_t *p);
54

  
55
/**
27 56
 * @brief Updates movement variables.
28 57
 */
29 58
void update_movement(map_t *map, gunner_t *p, keys_t *keys, list_t *shooter_list);
......
39 68
 */
40 69
void (get_random_spawn)(const map_t *map, gunner_t *p, list_t *l);
41 70

  
42
void update_mouse(struct packet *p);
71
/**
72
 * @brief Update scale if the keys were pressed to change scale.
73
 */
74
void update_scale(void);
43 75

  
44
keys_t* (get_key_presses)(void);
45

  
76
/**
77
 * @brief Shoot a bullet.
78
 * @param   shooter     Pointer to gunner that shot the bullet
79
 * @param   bullet_list List of the bullets in the map
80
 * @param   bsp_bullet  Basic sprite that will be used to render the bullet
81
 */
46 82
void (shoot_bullet)(const gunner_t *shooter, list_t *bullet_list, const basic_sprite_t *bsp_bullet);
47

  
83
/**
84
 * @brief Update game state.
85
 *
86
 * This includes moving bullets accordingly, processing damages, collisions of
87
 * bullets with walls, melee damage for the gunners that have autonomous melee.
88
 * @param   map             Pointer to map
89
 * @param   shooter_list    List of shooters
90
 * @param   bullet_list     List of bullets in the map
91
 */
48 92
void (update_game_state)(const map_t *map, list_t *shooter_list, list_t *bullet_list);
49 93

  
50

  
51

  
52
void update_scale(void);
53

  
54
int16_t* get_mouse_X(void);
55

  
56
int16_t* get_mouse_Y(void);
57

  
94
/**
95
 * @brief Wrapper to easen filling of the host_info_t.
96
 * @param   p       Host info to fill
97
 * @param   host    Host gunner
98
 * @param   remote  Remote gunner
99
 */
58 100
void build_host_structure(host_info_t *p, gunner_t *host, gunner_t *remote);
59

  
101
/**
102
 * @brief Wrapper to easen filling of the remote_info_t.
103
 * @param   p       Remote info to fill
104
 * @param   keys    Key presses of remote
105
 * @param   angle   Mouse angle of remote
106
 */
60 107
void build_remote_structure(remote_info_t *p, keys_t *keys, double angle);
61 108

  
62
double get_mouse_angle(gunner_t *p);
63 109

  
110

  
64 111
/**
65 112
 * @}
66 113
 */
......
82 129
    /// @brief Text.
83 130
    text_t *text;
84 131
} text_timer_t;
132
/**
133
 * @brief Construct timer
134
 * @param   fnt Font used to render timer text
135
 * @return      Pointer to constructed timer, or NULL if failed
136
 */
137
text_timer_t* (text_timer_ctor)(const font_t *fnt);
138
/**
139
 * @brief Destruct timer.
140
 * @param   p   Pointer to timer to be destructed
141
 */
142
void (text_timer_dtor)(text_timer_t *p);
143
/**
144
 * @brief   Update timer, by incrementing the number of seconds.
145
 * @param   p   Pointer to timer
146
 */
147
void (text_timer_update)(text_timer_t *p);
148
/**
149
 * @brief   Reset timer to 0.
150
 * @param   p   Pointer to timer
151
 */
152
void (text_timer_reset)(text_timer_t *p);
85 153

  
86
text_timer_t* (timer_ctor)(const font_t *fnt);
87

  
88
void (timer_dtor)(text_timer_t *p);
89

  
90
void (timer_update)(text_timer_t *p);
91

  
92
void (timer_reset)(text_timer_t *p);
93

  
94 154
/**
95 155
 * @}
96 156
 */
proj/include/zombies.h
1
#ifndef ZOMBIES_H_INCLUDED
2
#define ZOMBIES_H_INCLUDED
3

  
4
int (zombies)(void);
5

  
6
#endif //ZOMBIES_H_INCLUDED
0 7

  
proj/libs/graph/include/sprite.h
13 13

  
14 14
#include "basic_sprite.h"
15 15

  
16
/**
17
 * @brief Basic sprite.
18
 */
16 19
typedef struct sprite sprite_t;
17

  
18 20
/**
19 21
 * @brief Construct sprite.
20 22
 * @param   bsp Basic sprite from which to build the sprite
proj/libs/kbc/include/mouse.h
70 70
 * Polls the mouse till data is available for reading
71 71
 * </summary>
72 72
 * @param data Pointer to variable where byte read from mouse will be stored
73
 * @param   period  Time that the process should wait before polling again, in microseconds
73 74
 * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
74 75
 * @see {_ERRORS_H_::errors}
75 76
 */
proj/libs/rtc/include/rtc.h
19 19
 */
20 20
int (subscribe_rtc_interrupt)(uint8_t interrupt_bit, int *interrupt_id);
21 21

  
22
/**
23
 * @brief Read RTC register.
24
 * @param   reg     Register to read from
25
 * @param   data    Pointer to save read data
26
 * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
27
 * @see {_ERRORS_H_::errors}
28
 */
22 29
int (rtc_read_register)(uint32_t reg, uint8_t *data);
23

  
30
/**
31
 * @brief Write RTC register.
32
 * @param   reg     Register to write to
33
 * @param   data    Data to write in register
34
 * @return ERROR_CODE code representing the result of the operation, SUCCESS code is returned if everything is OK
35
 * @see {_ERRORS_H_::errors}
36
 */
24 37
int (rtc_write_register)(uint32_t reg, uint8_t data);
25 38

  
26 39
/**
proj/src/makecode_map.c
1 1
#include "makecode_map.h"
2 2

  
3
static const char makecode_map[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\'', ERROR_CODE /*«*/, ERROR_CODE /*escape*/,
3
static const char makecode_map[] = {ERROR_CODE, ERROR_CODE, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\'', ERROR_CODE /*«*/, ERROR_CODE /*escape*/,
4 4
                                    ERROR_CODE /*tab*/, 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P', '+', ERROR_CODE /* ´ */, ERROR_CODE /*enter*/,
5 5
                                    ERROR_CODE /*ctrl*/, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L', ERROR_CODE /* Ç */, ERROR_CODE /* º */, '\\', ERROR_CODE /*lshift*/, '~',
6 6
                                    'Z', 'X', 'C', 'V', 'B', 'N', 'M', ',', '.', '-', ERROR_CODE /*rshift*/, ERROR_CODE /*unkown*/, ERROR_CODE /*alt*/, ' ' /*space*/};
......
8 8
static const int size = sizeof(makecode_map);
9 9

  
10 10
char (map_makecode)(uint8_t code) {
11
    int key = code - BASE_CODE;
12
    if (key < 0 || key >= size) return ERROR_CODE;
13
    return makecode_map[key];
11
    if (code < 0 || code >= size) return ERROR_CODE;
12
    return makecode_map[code];
14 13
}
proj/src/proj.c
6 6
#include "proj_macros.h"
7 7
#include "proj_func.h"
8 8

  
9
#include "kbc.h"
10
#include "timer.h"
11
#include "keyboard.h"
12
#include "mouse.h"
13 9
#include "graph.h"
14 10
#include "menu.h"
15 11
#include "rtc.h"
......
18 14
#include "makecode_map.h"
19 15

  
20 16
#include "graph.h"
21
#include "sprite.h"
17

  
22 18
#include "rectangle.h"
23 19
#include "font.h"
24
#include "ent.h"
25 20

  
26 21
#include "crosshair.h"
27 22
#include "shooter.h"
......
35 30

  
36 31
#include "list.h"
37 32

  
33
#include "proj.h"
34

  
38 35
int main(int argc, char* argv[]) {
39 36

  
40 37
    lcf_set_language("EN-US");
......
50 47
    return 0;
51 48
}
52 49

  
53
static basic_sprite_t       *bsp_crosshair = NULL;
54
static basic_sprite_t       *bsp_shooter   = NULL;
55
static basic_sprite_t       *bsp_zombie    = NULL;
56
static basic_sprite_t       *bsp_pistol    = NULL;
57
static basic_sprite_t       *bsp_nothing   = NULL;
58
static basic_sprite_t       *bsp_bullet    = NULL;
59
static map_t                *map1          = NULL;
60
static sprite_t             *sp_crosshair  = NULL;
61 50

  
51

  
62 52
static int (singleplayer)(void);
63 53
static int (multiplayer)(void);
64 54
static int (chat)(void);
......
278 268
    nctp_set_processor(multiplayer_process);
279 269

  
280 270
    ent_set_scale(DEFAULT_SCALE);
281
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
271
    text_timer_t *in_game_timer = text_timer_ctor(font_get_default());
282 272

  
283 273
    list_t *shooter_list = list_ctor();
284 274

  
......
323 313
                interrupt_handler(i);
324 314
                switch (i) {
325 315
                    case TIMER0_IRQ:
326
                    if (timer_get_no_interrupts() % 60 == 0) timer_update(in_game_timer);
316
                    if (timer_get_no_interrupts() % 60 == 0) text_timer_update(in_game_timer);
327 317
                    update_movement(map1, shooter1, keys, shooter_list);
328 318
                    update_movement(map1, shooter2, &(remote_info->remote_keys_pressed), shooter_list);
329 319

  
......
409 399

  
410 400
    nctp_set_processor(NULL);
411 401

  
412
    timer_dtor(in_game_timer); in_game_timer = NULL;
402
    text_timer_dtor(in_game_timer); in_game_timer = NULL;
413 403

  
414 404
    return 0;
415 405
}
......
420 410
    nctp_set_processor(multiplayer_process);
421 411

  
422 412
    ent_set_scale(DEFAULT_SCALE);
423
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
413
    text_timer_t *in_game_timer = text_timer_ctor(font_get_default());
424 414

  
425 415
    list_t *shooter_list = list_ctor();
426 416

  
......
458 448
                interrupt_handler(i);
459 449
                switch (i) {
460 450
                    case TIMER0_IRQ:
461
                    if (timer_get_no_interrupts() % 60 == 0) timer_update(in_game_timer);
451
                    if (timer_get_no_interrupts() % 60 == 0) text_timer_update(in_game_timer);
462 452

  
463 453
                    double angle = get_mouse_angle(shooter1) - M_PI_2;
464 454

  
......
552 542

  
553 543
    nctp_set_processor(NULL);
554 544

  
555
    timer_dtor(in_game_timer); in_game_timer = NULL;
545
    text_timer_dtor(in_game_timer); in_game_timer = NULL;
556 546

  
557 547
    return 0;
558 548
}
559 549

  
560 550
static int (campaign)(void);
561
static int (zombies)(void);
551
#include "zombies.h"
562 552
static int (singleplayer)(void) {
563 553

  
564 554
    int r;
......
628 618
    int r;
629 619

  
630 620
    ent_set_scale(DEFAULT_SCALE);
631
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
621
    text_timer_t *in_game_timer = text_timer_ctor(font_get_default());
632 622

  
633 623
    list_t *shooter_list = list_ctor();
634 624

  
......
667 657
                switch (i) {
668 658
                    case TIMER0_IRQ:
669 659

  
670
                    if (timer_get_no_interrupts() % 60 == 0) timer_update(in_game_timer);
660
                    if (timer_get_no_interrupts() % 60 == 0) text_timer_update(in_game_timer);
671 661
                    update_movement(map1, shooter1, keys, shooter_list);
672 662

  
673 663
                    update_game_state(map1, shooter_list, bullet_list);
......
725 715
    }
726 716
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
727 717

  
728
    timer_dtor(in_game_timer); in_game_timer = NULL;
718
    text_timer_dtor(in_game_timer); in_game_timer = NULL;
729 719

  
730 720
    return SUCCESS;
731 721
}
732 722

  
733
#define ZOMBIES_NUM             5
734
#define ZOMBIE_HEALTH_FACTOR    1.1
735
static int (zombies)(void){
736 723

  
737
    int r;
738 724

  
739
    ent_set_scale(DEFAULT_SCALE);
740
    text_timer_t *in_game_timer = timer_ctor(font_get_default());
741

  
742
    list_t *shooter_list = list_ctor();
743

  
744
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
745
    gunner_set_spawn(shooter1, 980, 790);
746
    gunner_set_pos(shooter1, 980, 790);
747

  
748
    list_insert(shooter_list, list_end(shooter_list), shooter1);
749

  
750
    list_t *bullet_list  = list_ctor();
751

  
752
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
753
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
754

  
755
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
756
    uint8_t last_lb = 0;
757
    struct packet pp;
758
    keys_t *keys = get_key_presses();
759

  
760
    /// loop stuff
761
    uint64_t int_vector = 0;
762
    int good = true;
763
    int dead = false;
764

  
765
    int health = 50;
766

  
767
    map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
768

  
769
    while (good && !dead) {
770
        /* Get a request message. */
771
        if((r = get_interrupts_vector(&int_vector))) return r;
772
        uint32_t n = 1;
773
        for (uint8_t i = 0; i < 32; i++, n <<= 1) {
774
            if(!good || dead) break;
775
            if (int_vector & n) {
776
                interrupt_handler(i);
777
                switch (i) {
778
                    case TIMER0_IRQ:
779
                    if (timer_get_no_interrupts() % 60 == 0) timer_update(in_game_timer);
780
                    if (timer_get_no_interrupts() %  6 == 0){
781
                        map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
782
                    }
783

  
784
                    update_movement(map1, shooter1, keys, shooter_list);
785

  
786
                    update_game_state(map1, shooter_list, bullet_list);
787

  
788
                    if(list_find(shooter_list, shooter1) == list_end(shooter_list)){
789
                        good = false;
790
                        dead = true;
791
                        break;
792
                    }
793

  
794
                    double angle = get_mouse_angle(shooter1);
795
                    gunner_set_angle(shooter1, angle - M_PI_2);
796

  
797
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
798
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
799

  
800
                    while(list_size(shooter_list) < ZOMBIES_NUM+1){
801
                        gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, GUNNER_MELEE | GUNNER_FOLLOW, 3);
802
                        gunner_set_health(zombie, health);
803
                        gunner_set_curr_health(zombie, health);
804
                        health *= ZOMBIE_HEALTH_FACTOR;
805
                        get_random_spawn(map1, zombie, shooter_list);
806
                        list_push_back(shooter_list, zombie);
807
                    }
808

  
809
                    graph_clear_screen();
810
                    map_draw   (map1);
811
                    bullet_draw_list(bullet_list);
812
                    gunner_draw_list(shooter_list);
813

  
814
                    text_draw(in_game_timer->text);
815

  
816
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
817
                    sprite_draw(sp_crosshair);
818
                    graph_draw();
819

  
820
                    break;
821
                    case KBC_IRQ:
822
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) {
823
                        good = false;
824
                    }
825
                    break;
826
                    case MOUSE_IRQ:
827
                    if (mouse_get_counter_mouse_ih() >= 3) {
828
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
829
                        update_mouse(&pp);
830
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
831
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
832
                        last_lb = keys->lb_pressed;
833
                        mouse_set_counter_mouse_ih(0);
834

  
835
                    }
836
                    break;
837
                    case COM1_IRQ: nctp_ih(); break;
838
                }
839
            }
840
        }
841
    }
842

  
843
    while(list_size(shooter_list) > 0){
844
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
845
        gunner_dtor(p);
846
    }
847
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
848

  
849
    while(list_size(bullet_list) > 0){
850
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
851
        bullet_dtor(p);
852
    }
853
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
854

  
855
    if(dead){
856
        printf("YOU DIED\n");
857
    }
858

  
859
    timer_dtor(in_game_timer); in_game_timer = NULL;
860

  
861
    return SUCCESS;
862
}
863

  
864 725
#define CHAT_MAX_SIZE   75
865 726
#define CHAT_MAX_NUM    19
866 727
static text_t      *t_text[CHAT_MAX_NUM] = {NULL};
proj/src/proj_func.c
26 26
}
27 27

  
28 28
static keys_t key_presses;
29

  
30
host_info_t* host_info_ctor(gunner_t *host, gunner_t *remote) {
31
    host_info_t *ret = (host_info_t*)malloc(sizeof(host_info_t));
32
    if (ret == NULL) return ret;
33

  
34
    ret->host_x               = (int16_t)gunner_get_x          (host);
35
    ret->host_y               = (int16_t)gunner_get_y          (host);
36
    ret->host_angle           = (int16_t)gunner_get_angle      (host);
37
    ret->host_health          = (int16_t)gunner_get_health     (host);
38
    ret->host_current_health  = (int16_t)gunner_get_curr_health(host);
39

  
40
    // remote
41
    ret->remote_x               = (int16_t)gunner_get_x          (remote);
42
    ret->remote_y               = (int16_t)gunner_get_y          (remote);
43
    ret->remote_angle           = (int16_t)gunner_get_angle      (remote);
44
    ret->remote_health          = (int16_t)gunner_get_health     (remote);
45
    ret->remote_current_health  = (int16_t)gunner_get_curr_health(remote);
46

  
47
    //ret->no_bullets = 0;
48

  
49
    return ret;
50
}
51

  
52
void host_info_dtor(host_info_t *p) {
53
    if (p==NULL) return;
54
    /*
55
    if ((p->bullets_x) != NULL){ free(p->bullets_x); p->bullets_x = NULL; }
56

  
57
    if ((p->bullets_y) != NULL){ free(p->bullets_y); p->bullets_y = NULL; }
58

  
59
    if ((p->bullets_vx) != NULL){ free(p->bullets_vx); p->bullets_vx = NULL; }
60

  
61
    if ((p->bullets_vy) != NULL){ free(p->bullets_vy); p->bullets_vy = NULL; }
62

  
63
    if ((p->bullets_shooter) != NULL){ free(p->bullets_shooter); p->bullets_shooter = NULL; }
64
    */
65
    free(p);
66
}
67

  
68
remote_info_t* remote_info_ctor(void) {
69
    remote_info_t *ret = (remote_info_t*)malloc(sizeof(remote_info_t));
70
    if (ret == NULL) return ret;
71

  
72
    memset(&(ret->remote_keys_pressed), 0, sizeof(keys_t));
73

  
74
    ret->remote_angle = 0;
75

  
76
    return ret;
77
}
78

  
79
void remote_info_dtor(remote_info_t *p) {
80
    if (p==NULL) return;
81
    free(p);
82
}
83

  
84
bullet_info_t* bullet_info_ctor(void) {
85
    bullet_info_t *ret = (bullet_info_t*)malloc(sizeof(bullet_info_t));
86
    if (ret == NULL) return ret;
87

  
88
    ret->new_bullet = false;
89

  
90
    return ret;
91
}
92

  
93
void bullet_info_dtor(bullet_info_t *p) {
94
    if (p == NULL) return;
95
    free(p);
96
}
97

  
98 29
void update_key_presses(void) {
99 30
    if (keyboard_get_size() == 1) {
100 31
        switch(keyboard_get_scancode()[0]) {
......
115 46
        }
116 47
    }
117 48
}
49
keys_t* (get_key_presses)(void) {
50
    return &key_presses;
51
}
118 52

  
53
static int16_t mouse_x = 0, mouse_y = 0;
54
void (update_mouse)(struct packet *p) {
55
    mouse_x = max_16(0, mouse_x + p->delta_x);
56
    mouse_x = min_16(mouse_x, (int16_t)graph_get_XRes() - 1);
57

  
58
    mouse_y = max_16(0, mouse_y - p->delta_y);
59
    mouse_y = min_16(mouse_y, (int16_t)graph_get_YRes() - 1);
60

  
61
    key_presses.lb_pressed = p->lb;
62
}
63
int16_t* get_mouse_X(void) { return &mouse_x; }
64
int16_t* get_mouse_Y(void) { return &mouse_y; }
65
double get_mouse_angle(gunner_t *p) {
66
    return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
67
}
68

  
119 69
void update_movement(map_t *map, gunner_t *p, keys_t *keys, list_t *shooter_list) {
120 70
    /** Player movement */{
121 71
        int ver_mov = keys->s_pressed - keys->w_pressed;
......
183 133
    }
184 134
}
185 135

  
136
void (get_random_spawn)(const map_t *map, gunner_t *p, list_t *l) {
137
    uint16_t w = map_get_width(map), h = map_get_height(map);
138
    double x, y;
139

  
140
    while(true){
141
        x = rand() % w;
142
        y = rand() % h;
143
        gunner_set_pos(p, x, y);
144
        if(map_collides_gunner(map, p)) continue;
145
        int collides = false;
146
        list_node_t *it = list_begin(l);
147
        while(it != list_end(l)){
148
            if(gunner_collides_gunner(p, *list_node_val(it))){
149
                collides = true;
150
                break;
151
            }
152
            it = list_node_next(it);
153
        }
154
        if(!collides) return;
155
    }
156
}
157

  
158
void update_scale(void) {
159
    static uint8_t last_plus = 0, last_minus = 0;
160
    if (key_presses.ctrl_pressed) {
161
        if (key_presses.plus_pressed && !last_plus) {
162
            double scale = ent_get_scale();
163
            scale *= 1.1;
164
            if (scale <= MAX_SCALE) ent_set_scale(scale);
165
        }
166
        else if (key_presses.minus_pressed && !last_minus) {
167
            double scale = ent_get_scale();
168
            scale /= 1.1;
169
            if (scale >= MIN_SCALE) ent_set_scale(scale);
170
        }
171

  
172
        //printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
173
    }
174

  
175
    last_plus = key_presses.plus_pressed;
176
    last_minus = key_presses.minus_pressed;
177
}
178

  
186 179
void (shoot_bullet)(const gunner_t *shooter, list_t *bullet_list, const basic_sprite_t *bsp_bullet) {
187 180
    double angle = gunner_get_angle(shooter);
188 181
    double vx = -BULLET_SPEED * fm_sin(angle);
......
251 244
    }
252 245
}
253 246

  
254
void update_scale(void) {
255
    static uint8_t last_plus = 0, last_minus = 0;
256
    if (key_presses.ctrl_pressed) {
257
        if (key_presses.plus_pressed && !last_plus) {
258
            double scale = ent_get_scale();
259
            scale *= 1.1;
260
            if (scale <= MAX_SCALE) ent_set_scale(scale);
261
        }
262
        else if (key_presses.minus_pressed && !last_minus) {
263
            double scale = ent_get_scale();
264
            scale /= 1.1;
265
            if (scale >= MIN_SCALE) ent_set_scale(scale);
266
        }
247
text_timer_t* (text_timer_ctor)(const font_t *fnt){
248
    if(fnt == NULL) return NULL;
249
    text_timer_t *ret = malloc(sizeof(timer_t));
250
    if (ret == NULL) return NULL;
251
    ret->time = 0;
252
    ret->text = text_ctor(fnt, "000s");
253
    text_set_color(ret->text, TEXT_COLOR);
254
    return ret;
255
}
256
void (text_timer_update)(text_timer_t *p){
257
    if (p->time >= 999) return;
258
    p->time++;
259
    char buffer[100];
260
    sprintf(buffer, "%03ds", p->time);
261
    text_set_string(p->text, buffer);
262
}
263
void (text_timer_reset)(text_timer_t *p){
264
    text_set_string(p->text, "000s");
265
}
266
void (text_timer_dtor)(text_timer_t *p){
267
    if (p == NULL) return;
268
    text_dtor(p->text);
269
    free(p);
270
}
267 271

  
268
        //printf("SCALE: %d\n", (int)(ent_get_scale()*1000));
269
    }
272
host_info_t* host_info_ctor(gunner_t *host, gunner_t *remote) {
273
    host_info_t *ret = (host_info_t*)malloc(sizeof(host_info_t));
274
    if (ret == NULL) return ret;
270 275

  
271
    last_plus = key_presses.plus_pressed;
272
    last_minus = key_presses.minus_pressed;
273
}
276
    ret->host_x               = (int16_t)gunner_get_x          (host);
277
    ret->host_y               = (int16_t)gunner_get_y          (host);
278
    ret->host_angle           = (int16_t)gunner_get_angle      (host);
279
    ret->host_health          = (int16_t)gunner_get_health     (host);
280
    ret->host_current_health  = (int16_t)gunner_get_curr_health(host);
274 281

  
275
void (get_random_spawn)(const map_t *map, gunner_t *p, list_t *l) {
276
    uint16_t w = map_get_width(map), h = map_get_height(map);
277
    double x, y;
282
    // remote
283
    ret->remote_x               = (int16_t)gunner_get_x          (remote);
284
    ret->remote_y               = (int16_t)gunner_get_y          (remote);
285
    ret->remote_angle           = (int16_t)gunner_get_angle      (remote);
286
    ret->remote_health          = (int16_t)gunner_get_health     (remote);
287
    ret->remote_current_health  = (int16_t)gunner_get_curr_health(remote);
278 288

  
279
    while(true){
280
        x = rand() % w;
281
        y = rand() % h;
282
        gunner_set_pos(p, x, y);
283
        if(map_collides_gunner(map, p)) continue;
284
        int collides = false;
285
        list_node_t *it = list_begin(l);
286
        while(it != list_end(l)){
287
            if(gunner_collides_gunner(p, *list_node_val(it))){
288
                collides = true;
289
                break;
290
            }
291
            it = list_node_next(it);
292
        }
293
        if(!collides) return;
294
    }
289
    //ret->no_bullets = 0;
290

  
291
    return ret;
295 292
}
293
void host_info_dtor(host_info_t *p) {
294
    if (p==NULL) return;
295
    /*
296
    if ((p->bullets_x) != NULL){ free(p->bullets_x); p->bullets_x = NULL; }
296 297

  
297
static int16_t mouse_x = 0, mouse_y = 0;
298
    if ((p->bullets_y) != NULL){ free(p->bullets_y); p->bullets_y = NULL; }
298 299

  
299
void (update_mouse)(struct packet *p) {
300
    mouse_x = max_16(0, mouse_x + p->delta_x);
301
    mouse_x = min_16(mouse_x, (int16_t)graph_get_XRes() - 1);
300
    if ((p->bullets_vx) != NULL){ free(p->bullets_vx); p->bullets_vx = NULL; }
302 301

  
303
    mouse_y = max_16(0, mouse_y - p->delta_y);
304
    mouse_y = min_16(mouse_y, (int16_t)graph_get_YRes() - 1);
302
    if ((p->bullets_vy) != NULL){ free(p->bullets_vy); p->bullets_vy = NULL; }
305 303

  
306
    key_presses.lb_pressed = p->lb;
304
    if ((p->bullets_shooter) != NULL){ free(p->bullets_shooter); p->bullets_shooter = NULL; }
305
    */
306
    free(p);
307 307
}
308 308

  
309
keys_t* (get_key_presses)(void) {
310
    return &key_presses;
309
remote_info_t* remote_info_ctor(void) {
310
    remote_info_t *ret = (remote_info_t*)malloc(sizeof(remote_info_t));
311
    if (ret == NULL) return ret;
312

  
313
    memset(&(ret->remote_keys_pressed), 0, sizeof(keys_t));
314

  
315
    ret->remote_angle = 0;
316

  
317
    return ret;
311 318
}
319
void remote_info_dtor(remote_info_t *p) {
320
    if (p==NULL) return;
321
    free(p);
322
}
312 323

  
313
int16_t* get_mouse_X(void) { return &mouse_x; }
324
bullet_info_t* bullet_info_ctor(void) {
325
    bullet_info_t *ret = (bullet_info_t*)malloc(sizeof(bullet_info_t));
326
    if (ret == NULL) return ret;
314 327

  
315
int16_t* get_mouse_Y(void) { return &mouse_y; }
328
    ret->new_bullet = false;
316 329

  
317
double get_mouse_angle(gunner_t *p) {
318
    return atan2(gunner_get_y_screen(p) - mouse_y, mouse_x - gunner_get_x_screen(p));
330
    return ret;
319 331
}
332
void bullet_info_dtor(bullet_info_t *p) {
333
    if (p == NULL) return;
334
    free(p);
335
}
320 336

  
321 337
void build_host_structure(host_info_t *p, gunner_t *host, gunner_t *remote) {
322 338
    // host
......
356 372
    }
357 373
    */
358 374
}
359

  
360 375
void build_remote_structure(remote_info_t *p, keys_t *keys, double angle) {
361 376
    memcpy(&(p->remote_keys_pressed), keys, sizeof(keys_t));
362 377
    p->remote_angle = angle;
363 378
}
364

  
365
text_timer_t* (timer_ctor)(const font_t *fnt){
366
    if(fnt == NULL) return NULL;
367
    text_timer_t *ret = malloc(sizeof(timer_t));
368
    if (ret == NULL) return NULL;
369
    ret->time = 0;
370
    ret->text = text_ctor(fnt, "000s");
371
    text_set_color(ret->text, TEXT_COLOR);
372
    return ret;
373
}
374

  
375
void (timer_update)(text_timer_t *p){
376
    if (p->time >= 999) return;
377
    p->time++;
378
    char buffer[100];
379
    sprintf(buffer, "%03ds", p->time);
380
    text_set_string(p->text, buffer);
381
}
382

  
383
void (timer_reset)(text_timer_t *p){
384
    text_set_string(p->text, "000s");
385
}
386

  
387
void (timer_dtor)(text_timer_t *p){
388
    if (p == NULL) return;
389
    text_dtor(p->text);
390
    free(p);
391
}
proj/src/zombies.c
1
#include <lcom/lcf.h>
2

  
3
#include "zombies.h"
4

  
5
#include "proj.h"
6

  
7
#include "proj_macros.h"
8
#include "proj_func.h"
9
#include "ent.h"
10
#include "interrupts_func.h"
11
#include "graph.h"
12
#include "hltp.h"
13
#include "errors.h"
14
#include <math.h>
15

  
16
#define ZOMBIES_NUM             5
17
#define ZOMBIE_HEALTH_FACTOR    1.1
18
int (zombies)(void){
19

  
20
    int r;
21

  
22
    ent_set_scale(DEFAULT_SCALE);
23
    text_timer_t *in_game_timer = text_timer_ctor(font_get_default());
24

  
25
    list_t *shooter_list = list_ctor();
26

  
27
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, GUNNER_PLAYER, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
28
    gunner_set_spawn(shooter1, 980, 790);
29
    gunner_set_pos(shooter1, 980, 790);
30

  
31
    list_insert(shooter_list, list_end(shooter_list), shooter1);
32

  
33
    list_t *bullet_list  = list_ctor();
34

  
35
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
36
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
37

  
38
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
39
    uint8_t last_lb = 0;
40
    struct packet pp;
41
    keys_t *keys = get_key_presses();
42

  
43
    /// loop stuff
44
    uint64_t int_vector = 0;
45
    int good = true;
46
    int dead = false;
47

  
48
    int health = 50;
49

  
50
    map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
51

  
52
    while (good && !dead) {
53
        /* Get a request message. */
54
        if((r = get_interrupts_vector(&int_vector))) return r;
55
        uint32_t n = 1;
56
        for (uint8_t i = 0; i < 32; i++, n <<= 1) {
57
            if(!good || dead) break;
58
            if (int_vector & n) {
59
                interrupt_handler(i);
60
                switch (i) {
61
                    case TIMER0_IRQ:
62
                    if (timer_get_no_interrupts() % 60 == 0) text_timer_update(in_game_timer);
63
                    if (timer_get_no_interrupts() %  6 == 0){
64
                        map_make_dijkstra(map1, gunner_get_x(shooter1), gunner_get_y(shooter1));
65
                    }
66

  
67
                    update_movement(map1, shooter1, keys, shooter_list);
68

  
69
                    update_game_state(map1, shooter_list, bullet_list);
70

  
71
                    if(list_find(shooter_list, shooter1) == list_end(shooter_list)){
72
                        good = false;
73
                        dead = true;
74
                        break;
75
                    }
76

  
77
                    double angle = get_mouse_angle(shooter1);
78
                    gunner_set_angle(shooter1, angle - M_PI_2);
79

  
80
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
81
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
82

  
83
                    while(list_size(shooter_list) < ZOMBIES_NUM+1){
84
                        gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, GUNNER_MELEE | GUNNER_FOLLOW, 3);
85
                        gunner_set_health(zombie, health);
86
                        gunner_set_curr_health(zombie, health);
87
                        health *= ZOMBIE_HEALTH_FACTOR;
88
                        get_random_spawn(map1, zombie, shooter_list);
89
                        list_push_back(shooter_list, zombie);
90
                    }
91

  
92
                    graph_clear_screen();
93
                    map_draw   (map1);
94
                    bullet_draw_list(bullet_list);
95
                    gunner_draw_list(shooter_list);
96

  
97
                    text_draw(in_game_timer->text);
98

  
99
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
100
                    sprite_draw(sp_crosshair);
101
                    graph_draw();
102

  
103
                    break;
104
                    case KBC_IRQ:
105
                    if (keyboard_get_scancode()[0] == ESC_BREAK_CODE) {
106
                        good = false;
107
                    }
108
                    break;
109
                    case MOUSE_IRQ:
110
                    if (mouse_get_counter_mouse_ih() >= 3) {
111
                        mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
112
                        update_mouse(&pp);
113
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
114
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
115
                        last_lb = keys->lb_pressed;
116
                        mouse_set_counter_mouse_ih(0);
117

  
118
                    }
119
                    break;
120
                    case COM1_IRQ: nctp_ih(); break;
121
                }
122
            }
123
        }
124
    }
125

  
126
    while(list_size(shooter_list) > 0){
127
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
128
        gunner_dtor(p);
129
    }
130
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
131

  
132
    while(list_size(bullet_list) > 0){
133
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
134
        bullet_dtor(p);
135
    }
136
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
137

  
138
    if(dead){
139
        printf("YOU DIED\n");
140
    }
141

  
142
    text_timer_dtor(in_game_timer); in_game_timer = NULL;
143

  
144
    return SUCCESS;
145
}
0 146

  
proj/Makefile
9 9
.PATH: ${.CURDIR}/libs/classes/src
10 10
.PATH: ${.CURDIR}/libs/utils/src
11 11

  
12
SRCS= list.c queue.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c fast_math.c xpm_utils.c rtc.c uart.c makecode_map.c menu.c proj_func.c rectangle.c font.c ent.c proj.c hltp.c
12
SRCS= list.c queue.c graph.c kbc.c keyboard.c mouse.c utils.c timer.c interrupts_func.c fast_math.c xpm_utils.c rtc.c uart.c makecode_map.c menu.c proj_func.c rectangle.c font.c ent.c proj.c hltp.c zombies.c
13 13
IPATHS=-I./include -I./libs/graph/include -I./libs/kbc/include -I./libs/rtc/include -I./libs/timer/include -I./libs/uart/include -I./libs/classes/include -I./libs/utils/include -I./maps -I./media/xpm
14 14

  
15 15
CPPFLAGS += -pedantic ${IPATHS} -D LCOM_MACRO #-D __LCOM_OPTIMIZED__ -Wall -Wextra -Wshadow -Wunreachable-code #-Weverything -Wno-padded -Wno-unused-macros

Also available in: Unified diff