Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 307

History | View | Annotate | Download (23.1 KB)

1 144 up20180655
#include <lcom/lcf.h>
2
#include <lcom/proj.h>
3
#include <lcom/liblm.h>
4 185 up20180655
#include <math.h>
5 144 up20180655
6 149 up20180655
#include "proj_macros.h"
7 179 up20180642
#include "proj_func.h"
8 147 up20180655
9 149 up20180655
#include "kbc.h"
10
#include "timer.h"
11
#include "keyboard.h"
12 150 up20180655
#include "mouse.h"
13 189 up20180655
#include "graph.h"
14 291 up20180642
#include "menu.h"
15 234 up20180655
#include "rtc.h"
16 287 up20180642
#include "hltp.h"
17 153 up20180655
#include "interrupts_func.h"
18 270 up20180655
#include "makecode_map.h"
19 149 up20180655
20 179 up20180642
#include "graph.h"
21
#include "rectangle.h"
22 182 up20180642
#include "font.h"
23 192 up20180642
#include "ent.h"
24 168 up20180642
25 192 up20180642
#include "crosshair.h"
26
#include "shooter.h"
27 303 up20180642
#include "zombie.h"
28 192 up20180642
#include "pistol.h"
29
#include "nothing.h"
30 237 up20180642
#include "bullet.h"
31 216 up20180642
#include "map1.h"
32 183 up20180642
33 294 up20180642
#include "errors.h"
34
35 226 up20180642
#include "list.h"
36
37 144 up20180655
int main(int argc, char* argv[]) {
38
39
    lcf_set_language("EN-US");
40
41 235 up20180642
    lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
42 144 up20180655
43 189 up20180655
    lcf_log_output("/home/lcom/labs/proj/output.txt");
44 144 up20180655
45
    if (lcf_start(argc, argv)) return 1;
46
47
    lcf_cleanup();
48
49
    return 0;
50
}
51 147 up20180655
52 294 up20180642
font_t               *consolas      = NULL;
53
basic_sprite_t       *bsp_crosshair = NULL;
54
basic_sprite_t       *bsp_shooter   = NULL;
55 301 up20180642
basic_sprite_t       *bsp_zombie    = NULL;
56 294 up20180642
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 300 up20180642
static int (singleplayer)(void);
63
static int (chat)(void);
64 149 up20180655
int(proj_main_loop)(int argc, char *argv[]) {
65 170 up20180642
66
    int r;
67
68 294 up20180642
    consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
69 261 up20180642
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
70 192 up20180642
71 261 up20180642
    /// subscribe interrupts
72
    if (subscribe_all()) { return 1; }
73 170 up20180642
74 297 up20180642
    /// 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 152 up20180642
81 261 up20180642
    /// Load stuff
82
    {
83 297 up20180642
        graph_clear_screen();
84
        text_t *txt = text_ctor(consolas, "Loading...");
85
        text_draw(txt);
86
        text_dtor(txt);
87
        graph_draw();
88 183 up20180642
89 261 up20180642
        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 303 up20180642
        bsp_zombie    = get_zombie   (); if(bsp_zombie    == NULL) printf("Failed to get zombie\n");
92 261 up20180642
        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 226 up20180642
97 261 up20180642
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
98
    }
99 226 up20180642
100 297 up20180642
    menu_t *main_menu = menu_ctor(consolas);
101 299 up20180642
    menu_add_item(main_menu, "Single player");
102
    menu_add_item(main_menu, "Multiplayer");
103 297 up20180642
    menu_add_item(main_menu, "Chat");
104
    menu_add_item(main_menu, "Exit");
105 234 up20180655
106 297 up20180642
    //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 261 up20180642
    /// loop stuff
112 297 up20180642
    int click = 0;
113 307 up20180642
    uint32_t int_vector = 0;
114 295 up20180642
    int good = true;
115
    while (good) {
116 261 up20180642
        /* Get a request message. */
117 307 up20180642
        if((r = get_interrupts_vector(&int_vector))) return r;
118
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
119
            if (int_vector & n) {
120
                interrupt_handler(i);
121
                switch (i) {
122
                    case TIMER0_IRQ:
123 192 up20180642
124 307 up20180642
                    graph_clear_screen();
125
                    switch(menu_update_state(main_menu, click)){
126
                        case -1: break;
127
                        case  0: singleplayer(); break; //campaign(); break;
128
                        case  1: break;
129
                        case  2: chat(); break;
130
                        case  3: good = false; break;
131
                    }
132
                    menu_draw(main_menu);
133 192 up20180642
134 307 up20180642
                    click = 0;
135 202 up20180655
136 307 up20180642
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
137
                    sprite_draw(sp_crosshair);
138
                    graph_draw();
139 231 up20180655
140 307 up20180642
                    break;
141
                    case KBC_IRQ:
142
                    if ((scancode[0]) == ESC_BREAK_CODE) good = false;
143
                    case MOUSE_IRQ:
144
                    if (counter_mouse_ih >= 3) {
145
                        mouse_parse_packet(packet_mouse_ih, &pp);
146
                        update_mouse(&pp);
147
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
148
                        last_lb = keys->lb_pressed;
149
                        counter_mouse_ih = 0;
150 261 up20180642
                    }
151 307 up20180642
                    break;
152
                    case COM1_IRQ: nctp_ih(); break;
153 305 up20180642
                }
154 147 up20180655
            }
155
        }
156 261 up20180642
    }
157 149 up20180655
158 261 up20180642
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
159
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
160 307 up20180642
    basic_sprite_dtor      (bsp_zombie   ); bsp_zombie    = NULL;
161 261 up20180642
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
162
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
163
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
164
    map_dtor               (map1         ); map1          = NULL;
165
    font_dtor              (consolas     ); consolas      = NULL;
166 243 up20180642
167 261 up20180642
    // Unsubscribe interrupts
168
    if (unsubscribe_all()) {
169
        if (cleanup())
170 305 up20180642
        printf("%s: failed to cleanup.\n", __func__);
171 261 up20180642
        return 1;
172
    }
173 188 up20180642
174 261 up20180642
    if (cleanup()) {
175
        printf("%s: failed to cleanup.\n", __func__);
176
        return 1;
177
    }
178 155 up20180655
179 149 up20180655
    return 0;
180 147 up20180655
}
181 294 up20180642
182 300 up20180642
static int (campaign)(void);
183 301 up20180642
static int (zombies)(void);
184 300 up20180642
static int (singleplayer)(void) {
185
186 295 up20180642
    int r;
187
188 300 up20180642
    menu_t *main_menu = menu_ctor(consolas);
189
    menu_add_item(main_menu, "Campaign");
190
    menu_add_item(main_menu, "Zombies");
191
    menu_add_item(main_menu, "Back");
192
193
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
194
    uint8_t last_lb = 0;
195
    struct packet pp;
196
    keys_t *keys = get_key_presses();
197
198
    /// loop stuff
199
    int click = 0;
200 304 up20180642
    uint32_t int_vector = 0;
201 300 up20180642
    int good = true;
202
    while (good) {
203
        /* Get a request message. */
204 304 up20180642
        if((r = get_interrupts_vector(&int_vector))) return r;
205
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
206
            if (int_vector & n) {
207
                interrupt_handler(i);
208
                switch (i) {
209 305 up20180642
                    case TIMER0_IRQ:
210 300 up20180642
211 304 up20180642
                    graph_clear_screen();
212
                    switch(menu_update_state(main_menu, click)){
213
                        case -1: break;
214
                        case  0: campaign(); break;
215
                        case  1: zombies(); break;
216
                        case  2: good = false; break;
217
                    }
218
                    menu_draw(main_menu);
219 300 up20180642
220 304 up20180642
                    click = 0;
221 300 up20180642
222 304 up20180642
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
223
                    sprite_draw(sp_crosshair);
224
                    graph_draw();
225 300 up20180642
226 304 up20180642
                    break;
227 305 up20180642
                    case KBC_IRQ:
228 304 up20180642
                    if ((scancode[0]) == ESC_BREAK_CODE) good = false;
229 305 up20180642
                    case MOUSE_IRQ:
230 304 up20180642
                    if (counter_mouse_ih >= 3) {
231
                        mouse_parse_packet(packet_mouse_ih, &pp);
232
                        update_mouse(&pp);
233
                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
234
                        last_lb = keys->lb_pressed;
235
                        counter_mouse_ih = 0;
236 300 up20180642
                    }
237
                    break;
238 305 up20180642
                    case COM1_IRQ: nctp_ih(); break;
239 304 up20180642
                }
240 300 up20180642
            }
241
        }
242
    }
243
244
    return 0;
245
}
246
247
static int (campaign)(void){
248
249
    int r;
250
251 295 up20180642
    ent_set_scale(DEFAULT_SCALE);
252
    text_timer_t *in_game_timer = timer_ctor(consolas);
253
254
    list_t *shooter_list = list_ctor();
255
256 302 up20180642
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
257 295 up20180642
    gunner_set_spawn(shooter1, 75, 75);
258
    gunner_set_pos(shooter1, 75, 75);
259
260 302 up20180642
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing, gunner_player, 2);
261 295 up20180642
    gunner_set_spawn(shooter2, 975, 75);
262
    gunner_set_pos(shooter2, 775, 75);
263
264
    list_insert(shooter_list, list_end(shooter_list), shooter1);
265
    list_insert(shooter_list, list_end(shooter_list), shooter2);
266
267
    list_t *bullet_list  = list_ctor();
268
269
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
270 305 up20180642
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
271 295 up20180642
272 305 up20180642
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
273
    uint8_t last_lb = 0;
274
    struct packet pp;
275
    keys_t *keys = get_key_presses();
276 295 up20180642
277
    /// loop stuff
278 304 up20180642
    uint32_t int_vector = 0;
279 296 up20180642
    int good = true;
280
    while (good) {
281 304 up20180642
        /* Get a request message. */
282
        if((r = get_interrupts_vector(&int_vector))) return r;
283
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
284 305 up20180642
            if (int_vector & n) {
285
                interrupt_handler(i);
286
                switch (i) {
287
                    case TIMER0_IRQ:
288 295 up20180642
289 305 up20180642
                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
290
                    update_movement(map1, shooter1, keys, shooter_list);
291 295 up20180642
292 305 up20180642
                    update_game_state(map1, shooter_list, bullet_list);
293 295 up20180642
294 305 up20180642
                    //update_scale();
295
                    double angle = get_mouse_angle(shooter1);
296
                    gunner_set_angle(shooter1, angle - M_PI_2);
297 295 up20180642
298 305 up20180642
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
299
                    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
300 295 up20180642
301 305 up20180642
                    graph_clear_screen();
302
                    map_draw   (map1);
303
                    bullet_draw_list(bullet_list);
304
                    gunner_draw_list(shooter_list);
305 295 up20180642
306 305 up20180642
                    text_draw(in_game_timer->text);
307 295 up20180642
308 305 up20180642
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
309
                    sprite_draw(sp_crosshair);
310
                    graph_draw();
311 295 up20180642
312 305 up20180642
                    break;
313
                    case KBC_IRQ:
314
                    if ((scancode[0]) == ESC_BREAK_CODE) {
315
                        good = false;
316
                        // reset game
317
                        while(list_size(bullet_list) > 0){
318
                            bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
319
                            bullet_dtor(p);
320
                        }
321
                        list_node_t *it = list_begin(shooter_list);
322
                        while (it != list_end(shooter_list)) {
323
                            gunner_t *p = *(gunner_t**)list_node_val(it);
324 307 up20180642
                            get_random_spawn(map1, p, shooter_list);
325 305 up20180642
                            gunner_set_curr_health(p, gunner_get_health(p));
326
                            it = list_node_next(it);
327
                        }
328
                        timer_reset(in_game_timer);
329
                    }
330
                    break;
331
                    case MOUSE_IRQ:
332
                    if (counter_mouse_ih >= 3) {
333
                        mouse_parse_packet(packet_mouse_ih, &pp);
334
                        update_mouse(&pp);
335
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
336
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
337
                        last_lb = keys->lb_pressed;
338
                        counter_mouse_ih = 0;
339 304 up20180642
340 305 up20180642
                    }
341
                    break;
342
                    case COM1_IRQ: nctp_ih(); break;
343
                }
344
            }
345 304 up20180642
        }
346 295 up20180642
    }
347
348
    while(list_size(shooter_list) > 0){
349
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
350
        gunner_dtor(p);
351
    }
352 296 up20180642
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
353 295 up20180642
354 301 up20180642
    while(list_size(bullet_list) > 0){
355
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
356
        bullet_dtor(p);
357
    }
358
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
359
360
    timer_dtor(in_game_timer); in_game_timer = NULL;
361
362
    return SUCCESS;
363
}
364
365
#define ZOMBIES_NUM             5
366
#define ZOMBIE_HEALTH_FACTOR    1.1
367
368
static int (zombies)(void){
369
370
    int r;
371
372
    ent_set_scale(DEFAULT_SCALE);
373
    text_timer_t *in_game_timer = timer_ctor(consolas);
374
375
    list_t *shooter_list = list_ctor();
376
377 302 up20180642
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol, gunner_player, 1); if(shooter1 == NULL) printf("Failed to get shooter1\n");
378 305 up20180642
    gunner_set_spawn(shooter1, 980, 790);
379
    gunner_set_pos(shooter1, 980, 790);
380 301 up20180642
381
    list_insert(shooter_list, list_end(shooter_list), shooter1);
382
383
    list_t *bullet_list  = list_ctor();
384
385
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
386 305 up20180642
    gunner_get_y(shooter1)-ent_get_YLength()/2.0);
387 301 up20180642
388 305 up20180642
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
389
    uint8_t last_lb = 0;
390
    struct packet pp;
391
    keys_t *keys = get_key_presses();
392 301 up20180642
393
    /// loop stuff
394 304 up20180642
    uint32_t int_vector = 0;
395 301 up20180642
    int good = true;
396 302 up20180642
    int dead = false;
397 301 up20180642
398
    int health = 50;
399
400 302 up20180642
    while (good && !dead) {
401 304 up20180642
        /* Get a request message. */
402
        if((r = get_interrupts_vector(&int_vector))) return r;
403
        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
404
            if (int_vector & n) {
405 305 up20180642
                interrupt_handler(i);
406
                switch (i) {
407
                    case TIMER0_IRQ:
408
                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
409 301 up20180642
410 305 up20180642
                    update_movement(map1, shooter1, keys, shooter_list);
411 301 up20180642
412 305 up20180642
                    update_game_state(map1, shooter_list, bullet_list);
413 302 up20180642
414 305 up20180642
                    if(list_find(shooter_list, shooter1) == list_end(shooter_list)){
415
                        good = false;
416
                        dead = true;
417
                        break;
418
                    }
419 301 up20180642
420 305 up20180642
                    double angle = get_mouse_angle(shooter1);
421
                    gunner_set_angle(shooter1, angle - M_PI_2);
422 301 up20180642
423 305 up20180642
                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
424
                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
425 301 up20180642
426 305 up20180642
                    while(list_size(shooter_list) < ZOMBIES_NUM){
427
                        gunner_t *zombie = gunner_ctor(bsp_zombie, bsp_nothing, gunner_melee | gunner_follow, 3);
428
                        gunner_set_health(zombie, health);
429
                        gunner_set_curr_health(zombie, health);
430
                        health *= ZOMBIE_HEALTH_FACTOR;
431 307 up20180642
                        get_random_spawn(map1, zombie, shooter_list);
432 305 up20180642
                        list_push_back(shooter_list, zombie);
433
                    }
434 301 up20180642
435 305 up20180642
                    graph_clear_screen();
436
                    map_draw   (map1);
437
                    bullet_draw_list(bullet_list);
438
                    gunner_draw_list(shooter_list);
439 301 up20180642
440 305 up20180642
                    text_draw(in_game_timer->text);
441 301 up20180642
442 305 up20180642
                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
443
                    sprite_draw(sp_crosshair);
444
                    graph_draw();
445 304 up20180642
446 305 up20180642
                    break;
447
                    case KBC_IRQ:
448
                    if ((scancode[0]) == ESC_BREAK_CODE) {
449
                        good = false;
450
                    }
451
                    break;
452
                    case MOUSE_IRQ:
453
                    if (counter_mouse_ih >= 3) {
454
                        mouse_parse_packet(packet_mouse_ih, &pp);
455
                        update_mouse(&pp);
456
                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
457
                        shoot_bullet(shooter1, bullet_list, bsp_bullet);
458
                        last_lb = keys->lb_pressed;
459
                        counter_mouse_ih = 0;
460
461
                    }
462
                    break;
463
                    case COM1_IRQ: nctp_ih(); break;
464
                }
465
            }
466
        }
467 301 up20180642
    }
468
469
    while(list_size(shooter_list) > 0){
470
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
471
        gunner_dtor(p);
472
    }
473
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
474
475 295 up20180642
    while(list_size(bullet_list) > 0){
476
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
477
        bullet_dtor(p);
478
    }
479
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
480
481 302 up20180642
    if(dead){
482
        printf("YOU DIED\n");
483
    }
484
485 295 up20180642
    timer_dtor(in_game_timer); in_game_timer = NULL;
486
487 294 up20180642
    return SUCCESS;
488
}
489 296 up20180642
490 298 up20180642
#define CHAT_MAX_SIZE   75
491
#define CHAT_MAX_NUM    19
492 297 up20180642
text_t      *t_text[CHAT_MAX_NUM] = {NULL};
493
rectangle_t *r_text               =  NULL;
494
static void chat_process(const uint8_t *p, const size_t sz){
495
    char buffer2[CHAT_MAX_NUM+3];
496
    void *dest = NULL;
497
    hltp_type tp = hltp_interpret(p, sz, &dest);
498
    switch(tp){
499
        case hltp_type_string:
500 305 up20180642
        strcpy(buffer2, dest);
501
        strncat(buffer2, " <", 2);
502
        for(size_t i = CHAT_MAX_NUM-1; i; --i)
503
        text_set_text(t_text[i], text_get_string(t_text[i-1]));
504
        text_set_text(t_text[0], buffer2);
505
        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
506
            if(text_get_string(t_text[i])[0] == '>'){
507
                text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
508
                text_set_halign(t_text[i], text_halign_left);
509
            }else{
510
                text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
511
                text_set_halign(t_text[i], text_halign_right);
512 297 up20180642
            }
513 305 up20180642
        }
514
        break;
515 297 up20180642
        default: break;
516
    }
517
}
518 300 up20180642
static int (chat)(void){
519 297 up20180642
    int r;
520
521 298 up20180642
    nctp_dump();
522 297 up20180642
    nctp_set_processor(chat_process);
523
524
    struct packet pp;
525
526 298 up20180642
    char buffer[CHAT_MAX_SIZE] = "";
527 297 up20180642
    rectangle_t *r_buffer = NULL; {
528 298 up20180642
        r_buffer = rectangle_ctor(0,0,900,70);
529 297 up20180642
        rectangle_set_pos(r_buffer, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
530 305 up20180642
        graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2);
531 297 up20180642
        rectangle_set_fill_color   (r_buffer, GRAPH_BLACK);
532
        rectangle_set_outline_width(r_buffer, 2);
533
        rectangle_set_outline_color(r_buffer, GRAPH_WHITE);
534
        rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT);
535
    }
536
    text_t      *t_buffer = NULL; {
537
        t_buffer = text_ctor(consolas, "");
538
        text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
539 305 up20180642
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
540 297 up20180642
        text_set_halign(t_buffer, text_halign_left);
541
        text_set_valign(t_buffer, text_valign_center);
542
        text_set_color (t_buffer, TEXT_COLOR);
543
    }
544 298 up20180642
    text_t      *t_size   = NULL; {
545
        t_size = text_ctor(consolas, "");
546
        text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
547 305 up20180642
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
548 298 up20180642
        text_set_halign(t_size, text_halign_right);
549
        text_set_valign(t_size, text_valign_bottom);
550
        text_set_color (t_size, TEXT_COLOR);
551
        text_set_size  (t_size, 18);
552
        char buffer2[20];
553
        sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
554
        text_set_text(t_size, buffer2);
555
    }
556 297 up20180642
557
    /** r_text */ {
558 305 up20180642
    r_text = rectangle_ctor(0,0,900,550);
559
    rectangle_set_pos(r_text, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
560
    graph_get_YRes()*0.09);
561
    rectangle_set_fill_color   (r_text, GRAPH_BLACK);
562
    rectangle_set_outline_width(r_text, 2);
563
    rectangle_set_outline_color(r_text, GRAPH_WHITE);
564
    rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT);
565
}
566
/** t_text */ {
567
for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
568
    t_text[i] = text_ctor(consolas, " ");
569
    text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
570
    rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i);
571
    text_set_halign(t_text[i], text_halign_left);
572
    text_set_valign(t_text[i], text_valign_bottom);
573
    text_set_color (t_text[i], TEXT_COLOR);
574
}
575
}
576 297 up20180642
577 305 up20180642
/// loop stuff
578
uint32_t int_vector = 0;
579
int good = true;
580
while (good) {
581
    /* Get a request message. */
582
    if((r = get_interrupts_vector(&int_vector))) return r;
583
    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
584
        if (int_vector & n) {
585
            interrupt_handler(i);
586
            switch (i) {
587 304 up20180642
                case TIMER0_IRQ:
588 305 up20180642
                graph_clear_screen();
589
                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
590 297 up20180642
591 305 up20180642
                rectangle_draw(r_buffer);
592
                text_draw(t_buffer);
593
                text_draw(t_size);
594 297 up20180642
595 305 up20180642
                rectangle_draw(r_text);
596
                for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_draw(t_text[i]);
597 297 up20180642
598 305 up20180642
                sprite_draw(sp_crosshair);
599
                graph_draw();
600
                break;
601 304 up20180642
                case KBC_IRQ:
602 305 up20180642
                if      (scancode[0] == ESC_BREAK_CODE) good = false;
603
                else if (scancode[0] == ENTER_MAKE_CODE) {
604
                    hltp_send_string(buffer);
605
                    char buffer2[CHAT_MAX_SIZE+3] = "> ";
606
                    strncat(buffer2, buffer, strlen(buffer));
607
                    for(size_t i = CHAT_MAX_NUM-1; i; --i)
608
                    text_set_text(t_text[i], text_get_string(t_text[i-1]));
609
                    text_set_text(t_text[0], buffer2);
610
                    for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
611
                        if(text_get_string(t_text[i])[0] == '>'){
612
                            text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
613
                            text_set_halign(t_text[i], text_halign_left);
614
                        }else{
615
                            text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
616
                            text_set_halign(t_text[i], text_halign_right);
617 297 up20180642
                        }
618
                    }
619 305 up20180642
                    buffer[0] = '\0';
620
                } else if(scancode[0] == BACKSPACE_MAKE_CODE){
621
                    buffer[strlen(buffer)-1] = '\0';
622
                } else {
623
                    char c = map_makecode(scancode[0]);
624
                    if (c == ERROR_CODE) break;
625
                    if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1);
626
                    else                               printf("Char limit exceeded\n");
627
                }
628
                text_set_text(t_buffer, buffer);
629
                char buffer2[20];
630
                sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
631
                text_set_text(t_size, buffer2);
632 304 up20180642
                case MOUSE_IRQ:
633 305 up20180642
                if (counter_mouse_ih >= 3) {
634
                    mouse_parse_packet(packet_mouse_ih, &pp);
635
                    update_mouse(&pp);
636
                    counter_mouse_ih = 0;
637
                }
638
                break;
639 304 up20180642
                case COM1_IRQ: nctp_ih(); break;
640 297 up20180642
            }
641
        }
642
    }
643 305 up20180642
}
644 297 up20180642
645 305 up20180642
rectangle_dtor(r_buffer);
646
text_dtor     (t_buffer);
647 297 up20180642
648 305 up20180642
rectangle_dtor(r_text);
649
for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
650 297 up20180642
651 305 up20180642
nctp_set_processor(NULL);
652 298 up20180642
653 305 up20180642
return SUCCESS;
654 296 up20180642
}