Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 300

History | View | Annotate | Download (23.3 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 "timer.h"
11
#include "keyboard.h"
12
#include "mouse.h"
13
#include "graph.h"
14
#include "menu.h"
15
#include "rtc.h"
16
#include "hltp.h"
17
#include "interrupts_func.h"
18
#include "makecode_map.h"
19

    
20
#include "graph.h"
21
#include "rectangle.h"
22
#include "font.h"
23
#include "ent.h"
24

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

    
32
#include "errors.h"
33

    
34
#include "list.h"
35

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

    
38
    lcf_set_language("EN-US");
39

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

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

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

    
46
    lcf_cleanup();
47

    
48
    return 0;
49
}
50

    
51
font_t               *consolas      = NULL;
52
basic_sprite_t       *bsp_crosshair = NULL;
53
basic_sprite_t       *bsp_shooter   = NULL;
54
basic_sprite_t       *bsp_pistol    = NULL;
55
basic_sprite_t       *bsp_nothing   = NULL;
56
basic_sprite_t       *bsp_bullet    = NULL;
57
map_t                *map1          = NULL;
58
sprite_t             *sp_crosshair  = NULL;
59

    
60
static int (singleplayer)(void);
61
static int (chat)(void);
62
int(proj_main_loop)(int argc, char *argv[]) {
63

    
64
    int r;
65

    
66
    consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
67
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
68

    
69
    /// subscribe interrupts
70
    if (subscribe_all()) { return 1; }
71

    
72
    /// initialize graphics
73
    if(graph_init(GRAPH_MODE)){
74
        printf("%s: failed to initalize graphics.\n", __func__);
75
        if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
76
        return 1;
77
    }
78

    
79
    /// Load stuff
80
    {
81
        graph_clear_screen();
82
        text_t *txt = text_ctor(consolas, "Loading...");
83
        text_draw(txt);
84
        text_dtor(txt);
85
        graph_draw();
86

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

    
94
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
95
    }
96

    
97
    menu_t *main_menu = menu_ctor(consolas);
98
    menu_add_item(main_menu, "Single player");
99
    menu_add_item(main_menu, "Multiplayer");
100
    menu_add_item(main_menu, "Chat");
101
    menu_add_item(main_menu, "Exit");
102

    
103
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
104
    uint8_t last_lb = 0;
105
    struct packet pp;
106
    keys_t *keys = get_key_presses();
107

    
108
    /// loop stuff
109
    int ipc_status;
110
    message msg;
111

    
112
    int click = 0;
113

    
114
    int good = true;
115

    
116
    while (good) {
117
        /* Get a request message. */
118
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
119
            printf("driver_receive failed with %d", r);
120
            continue;
121
        }
122
        if (is_ipc_notify(ipc_status)) { /* received notification */
123
            switch (_ENDPOINT_P(msg.m_source)) {
124
                case HARDWARE: /* hardware interrupt notification */
125
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
126
                        if (msg.m_notify.interrupts & n) {
127
                            interrupt_handler(i);
128
                            switch (i) {
129
                            case TIMER0_IRQ:
130

    
131
                                graph_clear_screen();
132
                                switch(menu_update_state(main_menu, click)){
133
                                    case -1: break;
134
                                    case  0: singleplayer(); break; //campaign(); break;
135
                                    case  1: break;
136
                                    case  2: chat(); break;
137
                                    case  3: good = false; break;
138
                                }
139
                                menu_draw(main_menu);
140

    
141
                                click = 0;
142

    
143
                                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
144
                                sprite_draw(sp_crosshair);
145
                                graph_draw();
146

    
147
                                break;
148
                            case KBC_IRQ:
149
                                if ((scancode[0]) == ESC_BREAK_CODE) good = false;
150
                            case MOUSE_IRQ:
151
                                if (counter_mouse_ih >= 3) {
152
                                    mouse_parse_packet(packet_mouse_ih, &pp);
153
                                    update_mouse(&pp);
154
                                    if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
155
                                    last_lb = keys->lb_pressed;
156
                                    counter_mouse_ih = 0;
157
                                }
158
                                break;
159
                            case COM1_IRQ: nctp_ih(); break;
160
                            }
161
                        }
162
                    }
163

    
164
                    break;
165
                default:
166
                    break; /* no other notifications expected: do nothing */
167
            }
168
        } else { /* received standart message, not a notification */
169
            /* no standart message expected: do nothing */
170
        }
171
    }
172

    
173
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
174
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
175
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
176
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
177
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
178
    map_dtor               (map1         ); map1          = NULL;
179
    font_dtor              (consolas     ); consolas      = NULL;
180

    
181
    // Unsubscribe interrupts
182
    if (unsubscribe_all()) {
183
        if (cleanup())
184
            printf("%s: failed to cleanup.\n", __func__);
185
        return 1;
186
    }
187

    
188
    if (cleanup()) {
189
        printf("%s: failed to cleanup.\n", __func__);
190
        return 1;
191
    }
192

    
193
    return 0;
194
}
195

    
196
static int (campaign)(void);
197

    
198
static int (singleplayer)(void) {
199

    
200
    int r;
201

    
202
    menu_t *main_menu = menu_ctor(consolas);
203
    menu_add_item(main_menu, "Campaign");
204
    menu_add_item(main_menu, "Zombies");
205
    menu_add_item(main_menu, "Back");
206

    
207
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
208
    uint8_t last_lb = 0;
209
    struct packet pp;
210
    keys_t *keys = get_key_presses();
211

    
212
    /// loop stuff
213
    int ipc_status;
214
    message msg;
215

    
216
    int click = 0;
217

    
218
    int good = true;
219

    
220
    while (good) {
221
        /* Get a request message. */
222
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
223
            printf("driver_receive failed with %d", r);
224
            continue;
225
        }
226
        if (is_ipc_notify(ipc_status)) { /* received notification */
227
            switch (_ENDPOINT_P(msg.m_source)) {
228
                case HARDWARE: /* hardware interrupt notification */
229
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
230
                        if (msg.m_notify.interrupts & n) {
231
                            interrupt_handler(i);
232
                            switch (i) {
233
                            case TIMER0_IRQ:
234

    
235
                                graph_clear_screen();
236
                                switch(menu_update_state(main_menu, click)){
237
                                    case -1: break;
238
                                    case  0: campaign(); break;
239
                                    case  1: break;
240
                                    case  2: good = false; break;
241
                                }
242
                                menu_draw(main_menu);
243

    
244
                                click = 0;
245

    
246
                                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
247
                                sprite_draw(sp_crosshair);
248
                                graph_draw();
249

    
250
                                break;
251
                            case KBC_IRQ:
252
                                if ((scancode[0]) == ESC_BREAK_CODE) good = false;
253
                            case MOUSE_IRQ:
254
                                if (counter_mouse_ih >= 3) {
255
                                    mouse_parse_packet(packet_mouse_ih, &pp);
256
                                    update_mouse(&pp);
257
                                    if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
258
                                    last_lb = keys->lb_pressed;
259
                                    counter_mouse_ih = 0;
260
                                }
261
                                break;
262
                            case COM1_IRQ: nctp_ih(); break;
263
                            }
264
                        }
265
                    }
266

    
267
                    break;
268
                default:
269
                    break; /* no other notifications expected: do nothing */
270
            }
271
        } else { /* received standart message, not a notification */
272
            /* no standart message expected: do nothing */
273
        }
274
    }
275

    
276
    return 0;
277
}
278

    
279
static int (campaign)(void){
280

    
281
    int r;
282

    
283
    ent_set_scale(DEFAULT_SCALE);
284
    text_timer_t *in_game_timer = timer_ctor(consolas);
285

    
286
    list_t *shooter_list = list_ctor();
287

    
288
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
289
    gunner_set_spawn(shooter1, 75, 75);
290
    gunner_set_pos(shooter1, 75, 75);
291

    
292
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
293
    gunner_set_spawn(shooter2, 975, 75);
294
    gunner_set_pos(shooter2, 775, 75);
295

    
296
    list_insert(shooter_list, list_end(shooter_list), shooter1);
297
    list_insert(shooter_list, list_end(shooter_list), shooter2);
298

    
299
    list_t *bullet_list  = list_ctor();
300

    
301
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
302
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
303

    
304
   //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
305
   uint8_t last_lb = 0;
306
   struct packet pp;
307
   keys_t *keys = get_key_presses();
308

    
309
    /// loop stuff
310
    int ipc_status;
311
    message msg;
312
    int good = true;
313

    
314
    while (good) {
315
       /* Get a request message. */
316
       if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
317
           printf("driver_receive failed with %d", r);
318
           continue;
319
       }
320
       if (is_ipc_notify(ipc_status)) { /* received notification */
321
           switch (_ENDPOINT_P(msg.m_source)) {
322
               case HARDWARE: /* hardware interrupt notification */
323
                   for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
324
                       if (msg.m_notify.interrupts & n) {
325
                           interrupt_handler(i);
326
                           switch (i) {
327
                           case TIMER0_IRQ:
328

    
329
                               if (no_interrupts % 60 == 0) timer_update(in_game_timer);
330
                               update_movement(map1, shooter1, keys, shooter_list);
331

    
332
                               update_game_state(map1, shooter_list, bullet_list);
333

    
334
                               //update_scale();
335
                               double angle = get_mouse_angle(shooter1);
336
                               gunner_set_angle(shooter1, angle - M_PI_2);
337

    
338
                               ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
339
                                              gunner_get_y(shooter1)-ent_get_YLength()/2.0);
340

    
341
                               graph_clear_screen();
342
                               map_draw   (map1);
343
                               gunner_draw_list(shooter_list);
344
                               bullet_draw_list(bullet_list);
345

    
346
                               text_draw(in_game_timer->text);
347

    
348
                               sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
349
                               sprite_draw(sp_crosshair);
350
                               graph_draw();
351

    
352
                               break;
353
                           case KBC_IRQ:
354
                               if ((scancode[0]) == ESC_BREAK_CODE) {
355
                                   good = false;
356
                                   // reset game
357
                                   while(list_size(bullet_list) > 0){
358
                                       bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
359
                                       bullet_dtor(p);
360
                                   }
361
                                   list_node_t *it = list_begin(shooter_list);
362
                                   while (it != list_end(shooter_list)) {
363
                                       gunner_t *p = *(gunner_t**)list_node_val(it);
364
                                       get_random_spawn(map1, p);
365
                                       gunner_set_curr_health(p, gunner_get_health(p));
366
                                       it = list_node_next(it);
367
                                   }
368
                                   timer_reset(in_game_timer);
369
                               }
370
                               break;
371
                           case MOUSE_IRQ:
372
                               if (counter_mouse_ih >= 3) {
373
                                   mouse_parse_packet(packet_mouse_ih, &pp);
374
                                   update_mouse(&pp);
375
                                   if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
376
                                       shoot_bullet(shooter1, bullet_list, bsp_bullet);
377
                                   last_lb = keys->lb_pressed;
378
                                   counter_mouse_ih = 0;
379

    
380
                               }
381
                               break;
382
                           case COM1_IRQ: nctp_ih(); break;
383
                           }
384
                       }
385
                   }
386
                   break;
387
               default:
388
                   break; /* no other notifications expected: do nothing */
389
           }
390
       } else { /* received standart message, not a notification */
391
           /* no standart message expected: do nothing */
392
       }
393
    }
394

    
395
    while(list_size(shooter_list) > 0){
396
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
397
        gunner_dtor(p);
398
    }
399
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
400

    
401
    while(list_size(bullet_list) > 0){
402
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
403
        bullet_dtor(p);
404
    }
405
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
406

    
407
    timer_dtor(in_game_timer); in_game_timer = NULL;
408

    
409
    return SUCCESS;
410
}
411

    
412
#define CHAT_MAX_SIZE   75
413
#define CHAT_MAX_NUM    19
414
text_t      *t_text[CHAT_MAX_NUM] = {NULL};
415
rectangle_t *r_text               =  NULL;
416
static void chat_process(const uint8_t *p, const size_t sz){
417
    char buffer2[CHAT_MAX_NUM+3];
418
    void *dest = NULL;
419
    hltp_type tp = hltp_interpret(p, sz, &dest);
420
    switch(tp){
421
        case hltp_type_string:
422
            strcpy(buffer2, dest);
423
            strncat(buffer2, " <", 2);
424
            for(size_t i = CHAT_MAX_NUM-1; i; --i)
425
                text_set_text(t_text[i], text_get_string(t_text[i-1]));
426
            text_set_text(t_text[0], buffer2);
427
            for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
428
                if(text_get_string(t_text[i])[0] == '>'){
429
                    text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
430
                    text_set_halign(t_text[i], text_halign_left);
431
                }else{
432
                    text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
433
                    text_set_halign(t_text[i], text_halign_right);
434
                }
435
            }
436
            break;
437
        default: break;
438
    }
439
}
440
static int (chat)(void){
441
    int r;
442

    
443
    nctp_dump();
444
    nctp_set_processor(chat_process);
445

    
446
    struct packet pp;
447

    
448
    char buffer[CHAT_MAX_SIZE] = "";
449
    rectangle_t *r_buffer = NULL; {
450
        r_buffer = rectangle_ctor(0,0,900,70);
451
        rectangle_set_pos(r_buffer, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
452
                                    graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2);
453
        rectangle_set_fill_color   (r_buffer, GRAPH_BLACK);
454
        rectangle_set_outline_width(r_buffer, 2);
455
        rectangle_set_outline_color(r_buffer, GRAPH_WHITE);
456
        rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT);
457
    }
458
    text_t      *t_buffer = NULL; {
459
        t_buffer = text_ctor(consolas, "");
460
        text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
461
                               rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
462
        text_set_halign(t_buffer, text_halign_left);
463
        text_set_valign(t_buffer, text_valign_center);
464
        text_set_color (t_buffer, TEXT_COLOR);
465
    }
466
    text_t      *t_size   = NULL; {
467
        t_size = text_ctor(consolas, "");
468
        text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
469
                             rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
470
        text_set_halign(t_size, text_halign_right);
471
        text_set_valign(t_size, text_valign_bottom);
472
        text_set_color (t_size, TEXT_COLOR);
473
        text_set_size  (t_size, 18);
474
        char buffer2[20];
475
        sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
476
        text_set_text(t_size, buffer2);
477
    }
478

    
479
    /** r_text */ {
480
        r_text = rectangle_ctor(0,0,900,550);
481
        rectangle_set_pos(r_text, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
482
                                  graph_get_YRes()*0.09);
483
        rectangle_set_fill_color   (r_text, GRAPH_BLACK);
484
        rectangle_set_outline_width(r_text, 2);
485
        rectangle_set_outline_color(r_text, GRAPH_WHITE);
486
        rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT);
487
    }
488
    /** t_text */ {
489
        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
490
            t_text[i] = text_ctor(consolas, " ");
491
            text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
492
                                       rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i);
493
            text_set_halign(t_text[i], text_halign_left);
494
            text_set_valign(t_text[i], text_valign_bottom);
495
            text_set_color (t_text[i], TEXT_COLOR);
496
        }
497
    }
498

    
499
    /// loop stuff
500
    int ipc_status;
501
    message msg;
502
    int good = true;
503
    while (good) {
504
        /* Get a request message. */
505
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
506
            printf("driver_receive failed with %d", r);
507
            continue;
508
        }
509
        if (is_ipc_notify(ipc_status)) { /* received notification */
510
            switch (_ENDPOINT_P(msg.m_source)) {
511
                case HARDWARE: /* hardware interrupt notification */
512
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
513
                        if (msg.m_notify.interrupts & n) {
514
                            interrupt_handler(i);
515
                            switch (i) {
516
                            case TIMER0_IRQ:
517
                                graph_clear_screen();
518
                                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
519

    
520
                                rectangle_draw(r_buffer);
521
                                text_draw(t_buffer);
522
                                text_draw(t_size);
523

    
524
                                rectangle_draw(r_text);
525
                                for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_draw(t_text[i]);
526

    
527
                                sprite_draw(sp_crosshair);
528
                                graph_draw();
529
                                break;
530
                            case KBC_IRQ:
531
                                if      (scancode[0] == ESC_BREAK_CODE) good = false;
532
                                else if (scancode[0] == ENTER_MAKE_CODE) {
533
                                    hltp_send_string(buffer);
534
                                    char buffer2[CHAT_MAX_SIZE+3] = "> ";
535
                                    strncat(buffer2, buffer, strlen(buffer));
536
                                    for(size_t i = CHAT_MAX_NUM-1; i; --i)
537
                                        text_set_text(t_text[i], text_get_string(t_text[i-1]));
538
                                    text_set_text(t_text[0], buffer2);
539
                                    for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
540
                                        if(text_get_string(t_text[i])[0] == '>'){
541
                                            text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
542
                                            text_set_halign(t_text[i], text_halign_left);
543
                                        }else{
544
                                            text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
545
                                            text_set_halign(t_text[i], text_halign_right);
546
                                        }
547
                                    }
548
                                    buffer[0] = '\0';
549
                                } else if(scancode[0] == BACKSPACE_MAKE_CODE){
550
                                    buffer[strlen(buffer)-1] = '\0';
551
                                } else {
552
                                    char c = map_makecode(scancode[0]);
553
                                    if (c == ERROR_CODE) break;
554
                                    if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1);
555
                                    else                               printf("Char limit exceeded\n");
556
                                }
557
                                text_set_text(t_buffer, buffer);
558
                                char buffer2[20];
559
                                sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
560
                                text_set_text(t_size, buffer2);
561
                            case MOUSE_IRQ:
562
                                if (counter_mouse_ih >= 3) {
563
                                    mouse_parse_packet(packet_mouse_ih, &pp);
564
                                    update_mouse(&pp);
565
                                    counter_mouse_ih = 0;
566
                                }
567
                                break;
568
                            case COM1_IRQ: nctp_ih(); break;
569
                            }
570
                        }
571
                    }
572
                    break;
573
                default:
574
                    break; /* no other notifications expected: do nothing */
575
            }
576
        } else { /* received standart message, not a notification */
577
            /* no standart message expected: do nothing */
578
        }
579
    }
580

    
581
    rectangle_dtor(r_buffer);
582
    text_dtor     (t_buffer);
583

    
584
    rectangle_dtor(r_text);
585
    for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
586

    
587
    nctp_set_processor(NULL);
588

    
589
    return SUCCESS;
590
}