Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 304

History | View | Annotate | Download (25.4 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 click = 0;
218
    uint32_t int_vector = 0;
219
    int good = true;
220
    while (good) {
221
        /* Get a request message. */
222
        if((r = get_interrupts_vector(&int_vector))) return r;
223
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
224
            if (int_vector & n) {
225
                interrupt_handler(i);
226
                switch (i) {
227
                case TIMER0_IRQ:
228

    
229
                    graph_clear_screen();
230
                    switch(menu_update_state(main_menu, click)){
231
                        case -1: break;
232
                        case  0: campaign(); break;
233
                        case  1: zombies(); break;
234
                        case  2: good = false; break;
235
                    }
236
                    menu_draw(main_menu);
237

    
238
                    click = 0;
239

    
240
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
241
                    sprite_draw(sp_crosshair);
242
                    graph_draw();
243

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

    
262
    return 0;
263
}
264

    
265
static int (campaign)(void){
266

    
267
    int r;
268

    
269
    ent_set_scale(DEFAULT_SCALE);
270
    text_timer_t *in_game_timer = timer_ctor(consolas);
271

    
272
    list_t *shooter_list = list_ctor();
273

    
274
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
275
    gunner_set_spawn(shooter1, 75, 75);
276
    gunner_set_pos(shooter1, 75, 75);
277

    
278
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing, gunner_player, 2);
279
    gunner_set_spawn(shooter2, 975, 75);
280
    gunner_set_pos(shooter2, 775, 75);
281

    
282
    list_insert(shooter_list, list_end(shooter_list), shooter1);
283
    list_insert(shooter_list, list_end(shooter_list), shooter2);
284

    
285
    list_t *bullet_list  = list_ctor();
286

    
287
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
288
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
289

    
290
   //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
291
   uint8_t last_lb = 0;
292
   struct packet pp;
293
   keys_t *keys = get_key_presses();
294

    
295
    /// loop stuff
296
    uint32_t int_vector = 0;
297
    int good = true;
298
    while (good) {
299
        /* Get a request message. */
300
        if((r = get_interrupts_vector(&int_vector))) return r;
301
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
302
           if (int_vector & n) {
303
               interrupt_handler(i);
304
               switch (i) {
305
               case TIMER0_IRQ:
306

    
307
                   if (no_interrupts % 60 == 0) timer_update(in_game_timer);
308
                   update_movement(map1, shooter1, keys, shooter_list);
309

    
310
                   update_game_state(map1, shooter_list, bullet_list);
311

    
312
                   //update_scale();
313
                   double angle = get_mouse_angle(shooter1);
314
                   gunner_set_angle(shooter1, angle - M_PI_2);
315

    
316
                   ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
317
                                  gunner_get_y(shooter1)-ent_get_YLength()/2.0);
318

    
319
                   graph_clear_screen();
320
                   map_draw   (map1);
321
                   bullet_draw_list(bullet_list);
322
                   gunner_draw_list(shooter_list);
323

    
324
                   text_draw(in_game_timer->text);
325

    
326
                   sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
327
                   sprite_draw(sp_crosshair);
328
                   graph_draw();
329

    
330
                   break;
331
               case KBC_IRQ:
332
                   if ((scancode[0]) == ESC_BREAK_CODE) {
333
                       good = false;
334
                       // reset game
335
                       while(list_size(bullet_list) > 0){
336
                           bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
337
                           bullet_dtor(p);
338
                       }
339
                       list_node_t *it = list_begin(shooter_list);
340
                       while (it != list_end(shooter_list)) {
341
                           gunner_t *p = *(gunner_t**)list_node_val(it);
342
                           get_random_spawn(map1, p);
343
                           gunner_set_curr_health(p, gunner_get_health(p));
344
                           it = list_node_next(it);
345
                       }
346
                       timer_reset(in_game_timer);
347
                   }
348
                   break;
349
               case MOUSE_IRQ:
350
                   if (counter_mouse_ih >= 3) {
351
                       mouse_parse_packet(packet_mouse_ih, &pp);
352
                       update_mouse(&pp);
353
                       if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
354
                           shoot_bullet(shooter1, bullet_list, bsp_bullet);
355
                       last_lb = keys->lb_pressed;
356
                       counter_mouse_ih = 0;
357

    
358
                   }
359
                   break;
360
               case COM1_IRQ: nctp_ih(); break;
361
               }
362
           }
363
        }
364
    }
365

    
366
    while(list_size(shooter_list) > 0){
367
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
368
        gunner_dtor(p);
369
    }
370
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
371

    
372
    while(list_size(bullet_list) > 0){
373
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
374
        bullet_dtor(p);
375
    }
376
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
377

    
378
    timer_dtor(in_game_timer); in_game_timer = NULL;
379

    
380
    return SUCCESS;
381
}
382

    
383
#define ZOMBIES_NUM             5
384
#define ZOMBIE_HEALTH_FACTOR    1.1
385

    
386
static int (zombies)(void){
387

    
388
    int r;
389

    
390
    ent_set_scale(DEFAULT_SCALE);
391
    text_timer_t *in_game_timer = timer_ctor(consolas);
392

    
393
    list_t *shooter_list = list_ctor();
394

    
395
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
396
    gunner_set_spawn(shooter1, 75, 75);
397
    gunner_set_pos(shooter1, 75, 75);
398

    
399
    list_insert(shooter_list, list_end(shooter_list), shooter1);
400

    
401
    list_t *bullet_list  = list_ctor();
402

    
403
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
404
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
405

    
406
   //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
407
   uint8_t last_lb = 0;
408
   struct packet pp;
409
   keys_t *keys = get_key_presses();
410

    
411
    /// loop stuff
412
    uint32_t int_vector = 0;
413
    int good = true;
414
    int dead = false;
415

    
416
    int health = 50;
417

    
418
    while (good && !dead) {
419
        /* Get a request message. */
420
        if((r = get_interrupts_vector(&int_vector))) return r;
421
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
422
            if (int_vector & n) {
423
               interrupt_handler(i);
424
               switch (i) {
425
               case TIMER0_IRQ: //printf("L463\n");
426

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

    
430
                   update_game_state(map1, shooter_list, bullet_list);//printf("L468\n");
431

    
432
                   if(list_find(shooter_list, shooter1) == list_end(shooter_list)){
433
                       good = false;
434
                       dead = true;
435
                       break;
436
                   } //printf("L489\n");
437

    
438
                   //update_scale();
439
                   double angle = get_mouse_angle(shooter1);//printf("L471\n");
440
                   gunner_set_angle(shooter1, angle - M_PI_2); //printf("L472\n");
441

    
442
                   ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
443
                                  gunner_get_y(shooter1)-ent_get_YLength()/2.0);
444

    
445
                   while(list_size(shooter_list) < ZOMBIES_NUM){
446
                       gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, gunner_meelee, 3);
447
                       gunner_set_health(zombie, health);
448
                       gunner_set_curr_health(zombie, health);
449
                       health *= ZOMBIE_HEALTH_FACTOR;
450
                       get_random_spawn(map1, zombie);
451
                       list_push_back(shooter_list, zombie);
452
                   } //printf("L484\n");
453

    
454
                   graph_clear_screen();
455
                   map_draw   (map1);
456
                   bullet_draw_list(bullet_list);
457
                   gunner_draw_list(shooter_list);
458

    
459
                   text_draw(in_game_timer->text);
460

    
461
                   sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
462
                   sprite_draw(sp_crosshair);
463
                   graph_draw(); //printf("L508\n");
464

    
465
                   break;
466
               case KBC_IRQ:
467
                   if ((scancode[0]) == ESC_BREAK_CODE) {
468
                       good = false;
469
                       // reset game
470
                       while(list_size(bullet_list) > 0){
471
                           bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
472
                           bullet_dtor(p);
473
                       }
474
                       list_node_t *it = list_begin(shooter_list);
475
                       while (it != list_end(shooter_list)) {
476
                           gunner_t *p = *(gunner_t**)list_node_val(it);
477
                           get_random_spawn(map1, p);
478
                           gunner_set_curr_health(p, gunner_get_health(p));
479
                           it = list_node_next(it);
480
                       }
481
                       timer_reset(in_game_timer);
482
                   }
483
                   break;
484
               case MOUSE_IRQ:
485
                   if (counter_mouse_ih >= 3) {
486
                       mouse_parse_packet(packet_mouse_ih, &pp);
487
                       update_mouse(&pp);
488
                       if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
489
                           shoot_bullet(shooter1, bullet_list, bsp_bullet);
490
                       last_lb = keys->lb_pressed;
491
                       counter_mouse_ih = 0;
492

    
493
                   }
494
                   break;
495
               case COM1_IRQ: nctp_ih(); break;
496
               }
497
           }
498
       }
499
    }
500

    
501
    while(list_size(shooter_list) > 0){
502
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
503
        gunner_dtor(p);
504
    }
505
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
506

    
507
    while(list_size(bullet_list) > 0){
508
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
509
        bullet_dtor(p);
510
    }
511
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
512

    
513
    if(dead){
514
        printf("YOU DIED\n");
515
    }
516

    
517
    timer_dtor(in_game_timer); in_game_timer = NULL;
518

    
519
    return SUCCESS;
520
}
521

    
522
#define CHAT_MAX_SIZE   75
523
#define CHAT_MAX_NUM    19
524
text_t      *t_text[CHAT_MAX_NUM] = {NULL};
525
rectangle_t *r_text               =  NULL;
526
static void chat_process(const uint8_t *p, const size_t sz){
527
    char buffer2[CHAT_MAX_NUM+3];
528
    void *dest = NULL;
529
    hltp_type tp = hltp_interpret(p, sz, &dest);
530
    switch(tp){
531
        case hltp_type_string:
532
            strcpy(buffer2, dest);
533
            strncat(buffer2, " <", 2);
534
            for(size_t i = CHAT_MAX_NUM-1; i; --i)
535
                text_set_text(t_text[i], text_get_string(t_text[i-1]));
536
            text_set_text(t_text[0], buffer2);
537
            for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
538
                if(text_get_string(t_text[i])[0] == '>'){
539
                    text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
540
                    text_set_halign(t_text[i], text_halign_left);
541
                }else{
542
                    text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
543
                    text_set_halign(t_text[i], text_halign_right);
544
                }
545
            }
546
            break;
547
        default: break;
548
    }
549
}
550
static int (chat)(void){
551
    int r;
552

    
553
    nctp_dump();
554
    nctp_set_processor(chat_process);
555

    
556
    struct packet pp;
557

    
558
    char buffer[CHAT_MAX_SIZE] = "";
559
    rectangle_t *r_buffer = NULL; {
560
        r_buffer = rectangle_ctor(0,0,900,70);
561
        rectangle_set_pos(r_buffer, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
562
                                    graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2);
563
        rectangle_set_fill_color   (r_buffer, GRAPH_BLACK);
564
        rectangle_set_outline_width(r_buffer, 2);
565
        rectangle_set_outline_color(r_buffer, GRAPH_WHITE);
566
        rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT);
567
    }
568
    text_t      *t_buffer = NULL; {
569
        t_buffer = text_ctor(consolas, "");
570
        text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
571
                               rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
572
        text_set_halign(t_buffer, text_halign_left);
573
        text_set_valign(t_buffer, text_valign_center);
574
        text_set_color (t_buffer, TEXT_COLOR);
575
    }
576
    text_t      *t_size   = NULL; {
577
        t_size = text_ctor(consolas, "");
578
        text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
579
                             rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
580
        text_set_halign(t_size, text_halign_right);
581
        text_set_valign(t_size, text_valign_bottom);
582
        text_set_color (t_size, TEXT_COLOR);
583
        text_set_size  (t_size, 18);
584
        char buffer2[20];
585
        sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
586
        text_set_text(t_size, buffer2);
587
    }
588

    
589
    /** r_text */ {
590
        r_text = rectangle_ctor(0,0,900,550);
591
        rectangle_set_pos(r_text, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
592
                                  graph_get_YRes()*0.09);
593
        rectangle_set_fill_color   (r_text, GRAPH_BLACK);
594
        rectangle_set_outline_width(r_text, 2);
595
        rectangle_set_outline_color(r_text, GRAPH_WHITE);
596
        rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT);
597
    }
598
    /** t_text */ {
599
        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
600
            t_text[i] = text_ctor(consolas, " ");
601
            text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
602
                                       rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i);
603
            text_set_halign(t_text[i], text_halign_left);
604
            text_set_valign(t_text[i], text_valign_bottom);
605
            text_set_color (t_text[i], TEXT_COLOR);
606
        }
607
    }
608

    
609
    /// loop stuff
610
    uint32_t int_vector = 0;
611
    int good = true;
612
    while (good) {
613
        /* Get a request message. */
614
        if((r = get_interrupts_vector(&int_vector))) return r;
615
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
616
            if (int_vector & n) {
617
                interrupt_handler(i);
618
                switch (i) {
619
                case TIMER0_IRQ:
620
                    graph_clear_screen();
621
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
622

    
623
                    rectangle_draw(r_buffer);
624
                    text_draw(t_buffer);
625
                    text_draw(t_size);
626

    
627
                    rectangle_draw(r_text);
628
                    for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_draw(t_text[i]);
629

    
630
                    sprite_draw(sp_crosshair);
631
                    graph_draw();
632
                    break;
633
                case KBC_IRQ:
634
                    if      (scancode[0] == ESC_BREAK_CODE) good = false;
635
                    else if (scancode[0] == ENTER_MAKE_CODE) {
636
                        hltp_send_string(buffer);
637
                        char buffer2[CHAT_MAX_SIZE+3] = "> ";
638
                        strncat(buffer2, buffer, strlen(buffer));
639
                        for(size_t i = CHAT_MAX_NUM-1; i; --i)
640
                            text_set_text(t_text[i], text_get_string(t_text[i-1]));
641
                        text_set_text(t_text[0], buffer2);
642
                        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
643
                            if(text_get_string(t_text[i])[0] == '>'){
644
                                text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
645
                                text_set_halign(t_text[i], text_halign_left);
646
                            }else{
647
                                text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
648
                                text_set_halign(t_text[i], text_halign_right);
649
                            }
650
                        }
651
                        buffer[0] = '\0';
652
                    } else if(scancode[0] == BACKSPACE_MAKE_CODE){
653
                        buffer[strlen(buffer)-1] = '\0';
654
                    } else {
655
                        char c = map_makecode(scancode[0]);
656
                        if (c == ERROR_CODE) break;
657
                        if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1);
658
                        else                               printf("Char limit exceeded\n");
659
                    }
660
                    text_set_text(t_buffer, buffer);
661
                    char buffer2[20];
662
                    sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
663
                    text_set_text(t_size, buffer2);
664
                case MOUSE_IRQ:
665
                    if (counter_mouse_ih >= 3) {
666
                        mouse_parse_packet(packet_mouse_ih, &pp);
667
                        update_mouse(&pp);
668
                        counter_mouse_ih = 0;
669
                    }
670
                    break;
671
                case COM1_IRQ: nctp_ih(); break;
672
                }
673
            }
674
        }
675
    }
676

    
677
    rectangle_dtor(r_buffer);
678
    text_dtor     (t_buffer);
679

    
680
    rectangle_dtor(r_text);
681
    for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
682

    
683
    nctp_set_processor(NULL);
684

    
685
    return SUCCESS;
686
}