Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 303

History | View | Annotate | Download (30.2 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 "zombie.h"
28
#include "pistol.h"
29
#include "nothing.h"
30
#include "bullet.h"
31
#include "map1.h"
32

    
33
#include "errors.h"
34

    
35
#include "list.h"
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
font_t               *consolas      = NULL;
53
basic_sprite_t       *bsp_crosshair = NULL;
54
basic_sprite_t       *bsp_shooter   = NULL;
55
basic_sprite_t       *bsp_zombie    = NULL;
56
basic_sprite_t       *bsp_pistol    = NULL;
57
basic_sprite_t       *bsp_nothing   = NULL;
58
basic_sprite_t       *bsp_bullet    = NULL;
59
map_t                *map1          = NULL;
60
sprite_t             *sp_crosshair  = NULL;
61

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

    
66
    int r;
67

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

    
71
    /// subscribe interrupts
72
    if (subscribe_all()) { return 1; }
73

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

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

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

    
97
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
98
    }
99

    
100
    menu_t *main_menu = menu_ctor(consolas);
101
    menu_add_item(main_menu, "Single player");
102
    menu_add_item(main_menu, "Multiplayer");
103
    menu_add_item(main_menu, "Chat");
104
    menu_add_item(main_menu, "Exit");
105

    
106
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
107
    uint8_t last_lb = 0;
108
    struct packet pp;
109
    keys_t *keys = get_key_presses();
110

    
111
    /// loop stuff
112
    int ipc_status;
113
    message msg;
114

    
115
    int click = 0;
116

    
117
    int good = true;
118

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

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

    
144
                                click = 0;
145

    
146
                                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
147
                                sprite_draw(sp_crosshair);
148
                                graph_draw();
149

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

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

    
176
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
177
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
178
    /*basic_sprite_dtor      (bsp_zombie   );*/ bsp_zombie    = NULL;
179
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
180
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
181
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
182
    map_dtor               (map1         ); map1          = NULL;
183
    font_dtor              (consolas     ); consolas      = NULL;
184

    
185
    // Unsubscribe interrupts
186
    if (unsubscribe_all()) {
187
        if (cleanup())
188
            printf("%s: failed to cleanup.\n", __func__);
189
        return 1;
190
    }
191

    
192
    if (cleanup()) {
193
        printf("%s: failed to cleanup.\n", __func__);
194
        return 1;
195
    }
196

    
197
    return 0;
198
}
199

    
200
static int (campaign)(void);
201
static int (zombies)(void);
202
static int (singleplayer)(void) {
203

    
204
    int r;
205

    
206
    menu_t *main_menu = menu_ctor(consolas);
207
    menu_add_item(main_menu, "Campaign");
208
    menu_add_item(main_menu, "Zombies");
209
    menu_add_item(main_menu, "Back");
210

    
211
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
212
    uint8_t last_lb = 0;
213
    struct packet pp;
214
    keys_t *keys = get_key_presses();
215

    
216
    /// loop stuff
217
    int ipc_status;
218
    message msg;
219

    
220
    int click = 0;
221

    
222
    int good = true;
223

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

    
239
                                graph_clear_screen();
240
                                switch(menu_update_state(main_menu, click)){
241
                                    case -1: break;
242
                                    case  0: campaign(); break;
243
                                    case  1: zombies(); break;
244
                                    case  2: good = false; break;
245
                                }
246
                                menu_draw(main_menu);
247

    
248
                                click = 0;
249

    
250
                                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
251
                                sprite_draw(sp_crosshair);
252
                                graph_draw();
253

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

    
271
                    break;
272
                default:
273
                    break; /* no other notifications expected: do nothing */
274
            }
275
        } else { /* received standart message, not a notification */
276
            /* no standart message expected: do nothing */
277
        }
278
    }
279

    
280
    return 0;
281
}
282

    
283
static int (campaign)(void){
284

    
285
    int r;
286

    
287
    ent_set_scale(DEFAULT_SCALE);
288
    text_timer_t *in_game_timer = timer_ctor(consolas);
289

    
290
    list_t *shooter_list = list_ctor();
291

    
292
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
293
    gunner_set_spawn(shooter1, 75, 75);
294
    gunner_set_pos(shooter1, 75, 75);
295

    
296
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing, gunner_player, 2);
297
    gunner_set_spawn(shooter2, 975, 75);
298
    gunner_set_pos(shooter2, 775, 75);
299

    
300
    list_insert(shooter_list, list_end(shooter_list), shooter1);
301
    list_insert(shooter_list, list_end(shooter_list), shooter2);
302

    
303
    list_t *bullet_list  = list_ctor();
304

    
305
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
306
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
307

    
308
   //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
309
   uint8_t last_lb = 0;
310
   struct packet pp;
311
   keys_t *keys = get_key_presses();
312

    
313
    /// loop stuff
314
    int ipc_status;
315
    message msg;
316
    int good = true;
317

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

    
333
                               if (no_interrupts % 60 == 0) timer_update(in_game_timer);
334
                               update_movement(map1, shooter1, keys, shooter_list);
335

    
336
                               update_game_state(map1, shooter_list, bullet_list);
337

    
338
                               //update_scale();
339
                               double angle = get_mouse_angle(shooter1);
340
                               gunner_set_angle(shooter1, angle - M_PI_2);
341

    
342
                               ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
343
                                              gunner_get_y(shooter1)-ent_get_YLength()/2.0);
344

    
345
                               graph_clear_screen();
346
                               map_draw   (map1);
347
                               bullet_draw_list(bullet_list);
348
                               gunner_draw_list(shooter_list);
349

    
350
                               text_draw(in_game_timer->text);
351

    
352
                               sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
353
                               sprite_draw(sp_crosshair);
354
                               graph_draw();
355

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

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

    
399
    while(list_size(shooter_list) > 0){
400
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
401
        gunner_dtor(p);
402
    }
403
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
404

    
405
    while(list_size(bullet_list) > 0){
406
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
407
        bullet_dtor(p);
408
    }
409
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
410

    
411
    timer_dtor(in_game_timer); in_game_timer = NULL;
412

    
413
    return SUCCESS;
414
}
415

    
416
#define ZOMBIES_NUM             5
417
#define ZOMBIE_HEALTH_FACTOR    1.1
418

    
419
static int (zombies)(void){
420

    
421
    int r;
422

    
423
    ent_set_scale(DEFAULT_SCALE);
424
    text_timer_t *in_game_timer = timer_ctor(consolas);
425

    
426
    list_t *shooter_list = list_ctor();
427

    
428
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
429
    gunner_set_spawn(shooter1, 75, 75);
430
    gunner_set_pos(shooter1, 75, 75);
431

    
432
    list_insert(shooter_list, list_end(shooter_list), shooter1);
433

    
434
    list_t *bullet_list  = list_ctor();
435

    
436
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
437
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
438

    
439
   //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
440
   uint8_t last_lb = 0;
441
   struct packet pp;
442
   keys_t *keys = get_key_presses();
443

    
444
    /// loop stuff
445
    int ipc_status;
446
    message msg;
447
    int good = true;
448
    int dead = false;
449

    
450
    int health = 50;
451

    
452
    while (good && !dead) {
453
       /* Get a request message. */
454
       if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
455
           printf("driver_receive failed with %d", r);
456
           continue;
457
       }
458
       if (is_ipc_notify(ipc_status)) { /* received notification */
459
           switch (_ENDPOINT_P(msg.m_source)) {
460
               case HARDWARE: /* hardware interrupt notification */
461
                   for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
462
                       if (msg.m_notify.interrupts & n) {
463
                           interrupt_handler(i);
464
                           switch (i) {
465
                           case TIMER0_IRQ: //printf("L463\n");
466

    
467
                               if (no_interrupts % 60 == 0) timer_update(in_game_timer);//printf("L465\n");
468
                               update_movement(map1, shooter1, keys, shooter_list);//printf("L466\n");
469

    
470
                               update_game_state(map1, shooter_list, bullet_list);//printf("L468\n");
471

    
472
                               if(list_find(shooter_list, shooter1) == list_end(shooter_list)){ printf("YOU DIED\n");
473
                                   good = false;
474
                                   dead = true;
475
                                   break;
476
                               } //printf("L489\n");
477

    
478
                               //update_scale();
479
                               double angle = get_mouse_angle(shooter1);//printf("L471\n");
480
                               gunner_set_angle(shooter1, angle - M_PI_2); //printf("L472\n");
481

    
482
                               ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
483
                                              gunner_get_y(shooter1)-ent_get_YLength()/2.0);
484

    
485
                               while(list_size(shooter_list) < ZOMBIES_NUM){
486
                                   gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, gunner_meelee, 3);
487
                                   gunner_set_health(zombie, health);
488
                                   gunner_set_curr_health(zombie, health);
489
                                   health *= ZOMBIE_HEALTH_FACTOR;
490
                                   get_random_spawn(map1, zombie);
491
                                   list_push_back(shooter_list, zombie);
492
                               } //printf("L484\n");
493

    
494
                               graph_clear_screen();
495
                               map_draw   (map1);
496
                               bullet_draw_list(bullet_list);
497
                               gunner_draw_list(shooter_list);
498

    
499
                               text_draw(in_game_timer->text);
500

    
501
                               sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
502
                               sprite_draw(sp_crosshair);
503
                               graph_draw(); //printf("L508\n");
504

    
505
                               break;
506
                           case KBC_IRQ:
507
                               if ((scancode[0]) == ESC_BREAK_CODE) {
508
                                   good = false;
509
                                   // reset game
510
                                   while(list_size(bullet_list) > 0){
511
                                       bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
512
                                       bullet_dtor(p);
513
                                   }
514
                                   list_node_t *it = list_begin(shooter_list);
515
                                   while (it != list_end(shooter_list)) {
516
                                       gunner_t *p = *(gunner_t**)list_node_val(it);
517
                                       get_random_spawn(map1, p);
518
                                       gunner_set_curr_health(p, gunner_get_health(p));
519
                                       it = list_node_next(it);
520
                                   }
521
                                   timer_reset(in_game_timer);
522
                               }
523
                               break;
524
                           case MOUSE_IRQ:
525
                               if (counter_mouse_ih >= 3) {
526
                                   mouse_parse_packet(packet_mouse_ih, &pp);
527
                                   update_mouse(&pp);
528
                                   if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
529
                                       shoot_bullet(shooter1, bullet_list, bsp_bullet);
530
                                   last_lb = keys->lb_pressed;
531
                                   counter_mouse_ih = 0;
532

    
533
                               }
534
                               break;
535
                           case COM1_IRQ: nctp_ih(); break;
536
                           }
537
                       }
538
                   }
539
                   break;
540
               default:
541
                   break; /* no other notifications expected: do nothing */
542
           }
543
       } else { /* received standart message, not a notification */
544
           /* no standart message expected: do nothing */
545
       }
546
    }
547

    
548
    while(list_size(shooter_list) > 0){
549
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
550
        gunner_dtor(p);
551
    }
552
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
553

    
554
    while(list_size(bullet_list) > 0){
555
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
556
        bullet_dtor(p);
557
    }
558
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
559

    
560
    if(dead){
561
        printf("YOU DIED\n");
562
    }
563

    
564
    timer_dtor(in_game_timer); in_game_timer = NULL;
565

    
566
    return SUCCESS;
567
}
568

    
569
#define CHAT_MAX_SIZE   75
570
#define CHAT_MAX_NUM    19
571
text_t      *t_text[CHAT_MAX_NUM] = {NULL};
572
rectangle_t *r_text               =  NULL;
573
static void chat_process(const uint8_t *p, const size_t sz){
574
    char buffer2[CHAT_MAX_NUM+3];
575
    void *dest = NULL;
576
    hltp_type tp = hltp_interpret(p, sz, &dest);
577
    switch(tp){
578
        case hltp_type_string:
579
            strcpy(buffer2, dest);
580
            strncat(buffer2, " <", 2);
581
            for(size_t i = CHAT_MAX_NUM-1; i; --i)
582
                text_set_text(t_text[i], text_get_string(t_text[i-1]));
583
            text_set_text(t_text[0], buffer2);
584
            for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
585
                if(text_get_string(t_text[i])[0] == '>'){
586
                    text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
587
                    text_set_halign(t_text[i], text_halign_left);
588
                }else{
589
                    text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
590
                    text_set_halign(t_text[i], text_halign_right);
591
                }
592
            }
593
            break;
594
        default: break;
595
    }
596
}
597
static int (chat)(void){
598
    int r;
599

    
600
    nctp_dump();
601
    nctp_set_processor(chat_process);
602

    
603
    struct packet pp;
604

    
605
    char buffer[CHAT_MAX_SIZE] = "";
606
    rectangle_t *r_buffer = NULL; {
607
        r_buffer = rectangle_ctor(0,0,900,70);
608
        rectangle_set_pos(r_buffer, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
609
                                    graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2);
610
        rectangle_set_fill_color   (r_buffer, GRAPH_BLACK);
611
        rectangle_set_outline_width(r_buffer, 2);
612
        rectangle_set_outline_color(r_buffer, GRAPH_WHITE);
613
        rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT);
614
    }
615
    text_t      *t_buffer = NULL; {
616
        t_buffer = text_ctor(consolas, "");
617
        text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
618
                               rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
619
        text_set_halign(t_buffer, text_halign_left);
620
        text_set_valign(t_buffer, text_valign_center);
621
        text_set_color (t_buffer, TEXT_COLOR);
622
    }
623
    text_t      *t_size   = NULL; {
624
        t_size = text_ctor(consolas, "");
625
        text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
626
                             rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
627
        text_set_halign(t_size, text_halign_right);
628
        text_set_valign(t_size, text_valign_bottom);
629
        text_set_color (t_size, TEXT_COLOR);
630
        text_set_size  (t_size, 18);
631
        char buffer2[20];
632
        sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
633
        text_set_text(t_size, buffer2);
634
    }
635

    
636
    /** r_text */ {
637
        r_text = rectangle_ctor(0,0,900,550);
638
        rectangle_set_pos(r_text, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
639
                                  graph_get_YRes()*0.09);
640
        rectangle_set_fill_color   (r_text, GRAPH_BLACK);
641
        rectangle_set_outline_width(r_text, 2);
642
        rectangle_set_outline_color(r_text, GRAPH_WHITE);
643
        rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT);
644
    }
645
    /** t_text */ {
646
        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
647
            t_text[i] = text_ctor(consolas, " ");
648
            text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
649
                                       rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i);
650
            text_set_halign(t_text[i], text_halign_left);
651
            text_set_valign(t_text[i], text_valign_bottom);
652
            text_set_color (t_text[i], TEXT_COLOR);
653
        }
654
    }
655

    
656
    /// loop stuff
657
    int ipc_status;
658
    message msg;
659
    int good = true;
660
    while (good) {
661
        /* Get a request message. */
662
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
663
            printf("driver_receive failed with %d", r);
664
            continue;
665
        }
666
        if (is_ipc_notify(ipc_status)) { /* received notification */
667
            switch (_ENDPOINT_P(msg.m_source)) {
668
                case HARDWARE: /* hardware interrupt notification */
669
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
670
                        if (msg.m_notify.interrupts & n) {
671
                            interrupt_handler(i);
672
                            switch (i) {
673
                            case TIMER0_IRQ:
674
                                graph_clear_screen();
675
                                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
676

    
677
                                rectangle_draw(r_buffer);
678
                                text_draw(t_buffer);
679
                                text_draw(t_size);
680

    
681
                                rectangle_draw(r_text);
682
                                for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_draw(t_text[i]);
683

    
684
                                sprite_draw(sp_crosshair);
685
                                graph_draw();
686
                                break;
687
                            case KBC_IRQ:
688
                                if      (scancode[0] == ESC_BREAK_CODE) good = false;
689
                                else if (scancode[0] == ENTER_MAKE_CODE) {
690
                                    hltp_send_string(buffer);
691
                                    char buffer2[CHAT_MAX_SIZE+3] = "> ";
692
                                    strncat(buffer2, buffer, strlen(buffer));
693
                                    for(size_t i = CHAT_MAX_NUM-1; i; --i)
694
                                        text_set_text(t_text[i], text_get_string(t_text[i-1]));
695
                                    text_set_text(t_text[0], buffer2);
696
                                    for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
697
                                        if(text_get_string(t_text[i])[0] == '>'){
698
                                            text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
699
                                            text_set_halign(t_text[i], text_halign_left);
700
                                        }else{
701
                                            text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
702
                                            text_set_halign(t_text[i], text_halign_right);
703
                                        }
704
                                    }
705
                                    buffer[0] = '\0';
706
                                } else if(scancode[0] == BACKSPACE_MAKE_CODE){
707
                                    buffer[strlen(buffer)-1] = '\0';
708
                                } else {
709
                                    char c = map_makecode(scancode[0]);
710
                                    if (c == ERROR_CODE) break;
711
                                    if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1);
712
                                    else                               printf("Char limit exceeded\n");
713
                                }
714
                                text_set_text(t_buffer, buffer);
715
                                char buffer2[20];
716
                                sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
717
                                text_set_text(t_size, buffer2);
718
                            case MOUSE_IRQ:
719
                                if (counter_mouse_ih >= 3) {
720
                                    mouse_parse_packet(packet_mouse_ih, &pp);
721
                                    update_mouse(&pp);
722
                                    counter_mouse_ih = 0;
723
                                }
724
                                break;
725
                            case COM1_IRQ: nctp_ih(); break;
726
                            }
727
                        }
728
                    }
729
                    break;
730
                default:
731
                    break; /* no other notifications expected: do nothing */
732
            }
733
        } else { /* received standart message, not a notification */
734
            /* no standart message expected: do nothing */
735
        }
736
    }
737

    
738
    rectangle_dtor(r_buffer);
739
    text_dtor     (t_buffer);
740

    
741
    rectangle_dtor(r_text);
742
    for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
743

    
744
    nctp_set_processor(NULL);
745

    
746
    return SUCCESS;
747
}