Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 261

History | View | Annotate | Download (11 KB)

1
#include <lcom/lcf.h>
2
#include <lcom/proj.h>
3
#include <lcom/liblm.h>
4
#include <math.h>
5

    
6
#include "proj_macros.h"
7
#include "proj_func.h"
8

    
9
#include "kbc.h"
10
#include "i8254.h"
11
#include "timer.h"
12
#include "keyboard.h"
13
#include "mouse.h"
14
#include "graph.h"
15
#include "rtc.h"
16
#include "interrupts_func.h"
17

    
18
#include "graph.h"
19
#include "sprite.h"
20
#include "rectangle.h"
21
#include "font.h"
22
#include "ent.h"
23

    
24
#include "crosshair.h"
25
#include "shooter.h"
26
#include "pistol.h"
27
#include "nothing.h"
28
#include "bullet.h"
29
#include "map1.h"
30

    
31
#include "list.h"
32

    
33
#ifdef DIOGO
34
    #include "test7.h"
35
#endif
36

    
37
int main(int argc, char* argv[]) {
38

    
39
    lcf_set_language("EN-US");
40

    
41
    lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
42

    
43
    lcf_log_output("/home/lcom/labs/proj/output.txt");
44

    
45
    if (lcf_start(argc, argv)) return 1;
46

    
47
    lcf_cleanup();
48

    
49
    return 0;
50
}
51

    
52
int(proj_main_loop)(int argc, char *argv[]) {
53

    
54
    int r;
55

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

    
59
    /// subscribe interrupts
60
    if (subscribe_all()) { return 1; }
61

    
62
    /// initialize graphics
63
    if(graph_init(GRAPH_MODE)){
64
        printf("%s: failed to initalize graphics.\n", __func__);
65
        if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
66
        return 1;
67
    }
68

    
69
    /// Load stuff
70
    basic_sprite_t       *bsp_crosshair = NULL;
71
    basic_sprite_t       *bsp_shooter   = NULL;
72
    basic_sprite_t       *bsp_pistol    = NULL;
73
    basic_sprite_t       *bsp_nothing   = NULL;
74
    basic_sprite_t       *bsp_bullet    = NULL;
75
    map_t                *map1          = NULL;
76
    sprite_t             *sp_crosshair  = NULL;
77
    {
78
        graph_clear_screen();
79
        text_t *txt = text_ctor(consolas, "Loading...");
80
        text_draw(txt);
81
        text_dtor(txt);
82
        graph_draw();
83

    
84
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
85
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
86
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
87
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
88
        bsp_bullet    = get_bullet   (); if(bsp_bullet    == NULL) printf("Failed to get bullet\n");
89
        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
90

    
91
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
92
    }
93

    
94
    ent_set_scale(DEFAULT_SCALE);
95

    
96
    text_timer_t *in_game_timer = timer_ctor(consolas);
97

    
98
    menu_t *main_menu = menu_ctor(consolas);
99

    
100
    list_t *shooter_list = list_ctor();
101

    
102
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
103
    gunner_set_spawn(shooter1, 75, 75);
104
    gunner_set_pos(shooter1, 75, 75);
105

    
106
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
107
    gunner_set_spawn(shooter2, 975, 75);
108
    gunner_set_pos(shooter2, 775, 75);
109

    
110
    list_insert(shooter_list, list_end(shooter_list), shooter1);
111
    list_insert(shooter_list, list_end(shooter_list), shooter2);
112

    
113
    list_t *bullet_list  = list_ctor();
114

    
115
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
116
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
117

    
118
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
119
    double angle; // mouse angle
120
    int32_t *mouse_x = get_mouse_X(), *mouse_y = get_mouse_Y();
121
    uint8_t last_lb = 0;
122
    struct packet pp;
123
    keys_t *keys = get_key_presses();
124
    /// loop stuff
125
    int ipc_status;
126
    message msg;
127
    int game_state = MENU, click = 0; (void)click;
128

    
129
    while (game_state != EXIT) {
130
        /* Get a request message. */
131
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
132
            printf("driver_receive failed with %d", r);
133
            continue;
134
        }
135
        if (is_ipc_notify(ipc_status)) { /* received notification */
136
            switch (_ENDPOINT_P(msg.m_source)) {
137
                case HARDWARE: /* hardware interrupt notification */
138
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
139
                        if (msg.m_notify.interrupts & n) {
140
                            interrupt_handler(i);
141
                            switch (i) {
142
                            #ifndef DIOGO
143
                            case TIMER0_IRQ:
144
                                switch (game_state) {
145
                                case MENU:
146
                                    if (no_interrupts % 2 == 0){
147
                                        graph_clear_screen();
148
                                        game_state = menu_update_state(main_menu, click);
149
                                        menu_draw(main_menu);
150

    
151
                                        click = 0;
152

    
153
                                        sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
154
                                        sprite_draw(sp_crosshair);
155
                                        graph_draw();
156
                                    }
157
                                    break;
158
                                case GAME:
159
                                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
160
                                    update_movement(map1, shooter1, keys, shooter_list);
161

    
162
                                    update_game_state(map1, shooter_list, bullet_list);
163

    
164
                                    //update_scale();
165
                                    angle = get_mouse_angle(shooter1);
166
                                    gunner_set_angle(shooter1, angle - M_PI_2);
167

    
168
                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
169
                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
170

    
171
                                    graph_clear_screen();
172
                                    map_draw   (map1);
173
                                    gunner_draw_list(shooter_list);
174
                                    bullet_draw_list(bullet_list);
175

    
176
                                    text_draw(in_game_timer->text);
177

    
178
                                    sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
179
                                    sprite_draw(sp_crosshair);
180
                                    graph_draw();
181
                                    break;
182
                                }
183
                                break;
184
                            case KBC_IRQ:
185
                                switch (game_state) {
186
                                case MENU:
187
                                    if ((scancode[0]) == ESC_BREAK_CODE) game_state = EXIT;
188
                                    break;
189
                                case GAME:
190
                                    if ((scancode[0]) == ESC_BREAK_CODE) {
191
                                        game_state = MENU;
192
                                        // reset game
193
                                        while(list_size(bullet_list) > 0){
194
                                            bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
195
                                            bullet_dtor(p);
196
                                        }
197
                                        list_node_t *it = list_begin(shooter_list);
198
                                        while (it != list_end(shooter_list)) {
199
                                            gunner_t *p = *(gunner_t**)list_node_val(it);
200
                                            get_random_spawn(map1, p);
201
                                            gunner_set_curr_health(p, gunner_get_health(p));
202
                                            it = list_node_next(it);
203
                                        }
204
                                        timer_reset(in_game_timer);
205
                                    }
206
                                    break;
207
                                }
208
                                break;
209
                            case MOUSE_IRQ:
210
                                if (counter_mouse_ih >= 3) {
211
                                    mouse_parse_packet(packet_mouse_ih, &pp);
212
                                    update_mouse(&pp);
213
                                    switch (game_state) {
214
                                    case MENU:
215
                                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
216
                                        break;
217
                                    case GAME:
218
                                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
219
                                            shoot_bullet(shooter1, bullet_list, bsp_bullet);
220
                                        break;
221
                                    }
222
                                    last_lb = keys->lb_pressed;
223
                                    counter_mouse_ih = 0;
224

    
225
                                }
226
                                break;
227
                            #endif
228
                            #ifdef DIOGO
229
                            case COM1_IRQ:
230
                                printf("You've got mail.\n");
231
                                break;
232
                            #endif
233
                            }
234
                        }
235
                    }
236

    
237
                    break;
238
                default:
239
                    break; /* no other notifications expected: do nothing */
240
            }
241
        } else { /* received standart message, not a notification */
242
            /* no standart message expected: do nothing */
243
        }
244
    }
245

    
246
    while(list_size(shooter_list) > 0){
247
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
248
        gunner_dtor(p);
249
    }
250

    
251
    while(list_size(bullet_list) > 0){
252
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
253
        bullet_dtor(p);
254
    }
255
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
256
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
257

    
258
    timer_dtor(in_game_timer); in_game_timer = NULL;
259

    
260
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
261
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
262
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
263
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
264
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
265
    map_dtor               (map1         ); map1          = NULL;
266
    font_dtor              (consolas     ); consolas      = NULL;
267

    
268
    // Unsubscribe interrupts
269
    if (unsubscribe_all()) {
270
        if (cleanup())
271
            printf("%s: failed to cleanup.\n", __func__);
272
        return 1;
273
    }
274

    
275

    
276
    if (cleanup()) {
277
        printf("%s: failed to cleanup.\n", __func__);
278
        return 1;
279
    }
280

    
281
    return 0;
282
}