Project

General

Profile

Revision 297

chat working

View differences:

proj.c
70 70
    /// subscribe interrupts
71 71
    if (subscribe_all()) { return 1; }
72 72

  
73
    #ifndef DIOGO
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
    #endif
73
    /// initialize graphics
74
    if(graph_init(GRAPH_MODE)){
75
        printf("%s: failed to initalize graphics.\n", __func__);
76
        if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
77
        return 1;
78
    }
81 79

  
82 80
    /// Load stuff
83 81
    {
84
        #ifndef DIOGO
85
            graph_clear_screen();
86
            text_t *txt = text_ctor(consolas, "Loading...");
87
            text_draw(txt);
88
            text_dtor(txt);
89
            graph_draw();
90
        #endif
82
        graph_clear_screen();
83
        text_t *txt = text_ctor(consolas, "Loading...");
84
        text_draw(txt);
85
        text_dtor(txt);
86
        graph_draw();
91 87

  
92 88
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
93 89
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
......
99 95
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
100 96
    }
101 97

  
102
    #ifndef DIOGO
103
        menu_t *main_menu = menu_ctor(consolas);
104
        menu_add_item(main_menu, "Play");
105
        menu_add_item(main_menu, "Chat");
106
        menu_add_item(main_menu, "Exit");
107
    #endif
98
    menu_t *main_menu = menu_ctor(consolas);
99
    menu_add_item(main_menu, "Play");
100
    menu_add_item(main_menu, "Chat");
101
    menu_add_item(main_menu, "Exit");
108 102

  
109
    #ifndef DIOGO
110
        //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
111
        uint8_t last_lb = 0;
112
        struct packet pp;
113
        keys_t *keys = get_key_presses();
114
    #endif
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

  
115 108
    /// loop stuff
116 109
    int ipc_status;
117 110
    message msg;
118 111

  
119
    #ifdef DIOGO
120
        char buffer[1024]; // buffer
121
        int buffer_pos = 0;
122
    #endif
112
    int click = 0;
123 113

  
124
    #ifndef DIOGO
125
        int click = 0;
126
    #endif
127

  
128 114
    int good = true;
129 115

  
130 116
    while (good) {
......
140 126
                        if (msg.m_notify.interrupts & n) {
141 127
                            interrupt_handler(i);
142 128
                            switch (i) {
143
                            #ifndef DIOGO
144 129
                            case TIMER0_IRQ:
145 130

  
146 131
                                graph_clear_screen();
......
159 144
                                graph_draw();
160 145

  
161 146
                                break;
162
                            #endif
163 147
                            case KBC_IRQ:
164 148
                                if ((scancode[0]) == ESC_BREAK_CODE) good = false;
165
                                #ifdef DIOGO
166
                                else if ((scancode[0]) == ENTER_MAKE_CODE) {
167
                                    buffer[buffer_pos] = '\0';
168
                                    printf("\nSending string -%s-", buffer);
169
                                    printf(" (output: %d)\n",
170
                                        hltp_send_string(buffer));
171
                                    buffer_pos = 0;
172
                                }
173
                                else {
174
                                    char c = map_makecode(scancode[0]);
175
                                    if (c == ERROR_CODE) break;
176
                                    buffer[buffer_pos++] = c;
177
                                    printf("%c", c);
178
                                }
179
                                #endif
180
                            #ifndef DIOGO
181 149
                            case MOUSE_IRQ:
182 150
                                if (counter_mouse_ih >= 3) {
183 151
                                    mouse_parse_packet(packet_mouse_ih, &pp);
......
187 155
                                    counter_mouse_ih = 0;
188 156
                                }
189 157
                                break;
190
                            #endif
191
                            #ifdef DIOGO
192
                            case COM1_IRQ:
193
                                nctp_ih();
194
                                break;
195
                            #endif
196 158
                            }
197 159
                        }
198 160
                    }
......
221 183
        return 1;
222 184
    }
223 185

  
224

  
225 186
    if (cleanup()) {
226 187
        printf("%s: failed to cleanup.\n", __func__);
227 188
        return 1;
......
362 323
    return SUCCESS;
363 324
}
364 325

  
326
#define CHAT_MAX_SIZE   64
327
#define CHAT_MAX_NUM    10
328

  
329
text_t      *t_text[CHAT_MAX_NUM] = {NULL};
330
rectangle_t *r_text               =  NULL;
331

  
332
static void chat_process(const uint8_t *p, const size_t sz){
333
    char buffer2[CHAT_MAX_NUM+3];
334
    void *dest = NULL;
335
    hltp_type tp = hltp_interpret(p, sz, &dest);
336
    switch(tp){
337
        case hltp_type_string:
338
            strcpy(buffer2, dest);
339
            strncat(buffer2, " <", 2);
340
            for(size_t i = CHAT_MAX_NUM-1; i; --i)
341
                text_set_text(t_text[i], text_get_string(t_text[i-1]));
342
            text_set_text(t_text[0], buffer2);
343
            for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
344
                if(text_get_string(t_text[i])[0] == '>'){
345
                    text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
346
                    text_set_halign(t_text[i], text_halign_left);
347
                }else{
348
                    text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
349
                    text_set_halign(t_text[i], text_halign_right);
350
                }
351
            }
352
            break;
353
        default: break;
354
    }
355
}
356

  
365 357
int (chat)(void){
358
    int r;
359

  
360
    nctp_set_processor(chat_process);
361

  
362
    struct packet pp;
363

  
364
    rectangle_t *r_buffer = NULL; {
365
        r_buffer = rectangle_ctor(0,0,750,70);
366
        rectangle_set_pos(r_buffer, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
367
                                    graph_get_YRes()*0.8-rectangle_get_h(r_buffer)/2);
368
        rectangle_set_fill_color   (r_buffer, GRAPH_BLACK);
369
        rectangle_set_outline_width(r_buffer, 2);
370
        rectangle_set_outline_color(r_buffer, GRAPH_WHITE);
371
        rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT);
372
    }
373
    text_t      *t_buffer = NULL; {
374
        t_buffer = text_ctor(consolas, "");
375
        text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
376
                               rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
377
        text_set_halign(t_buffer, text_halign_left);
378
        text_set_valign(t_buffer, text_valign_center);
379
        text_set_color (t_buffer, TEXT_COLOR);
380
    }
381

  
382
    /** r_text */ {
383
        r_text = rectangle_ctor(0,0,750,500);
384
        rectangle_set_pos(r_text, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
385
                                  graph_get_YRes()*0.1-rectangle_get_h(r_buffer)/2);
386
        rectangle_set_fill_color   (r_text, GRAPH_BLACK);
387
        rectangle_set_outline_width(r_text, 2);
388
        rectangle_set_outline_color(r_text, GRAPH_WHITE);
389
        rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT);
390
    }
391
    /** t_text */ {
392
        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
393
            t_text[i] = text_ctor(consolas, " ");
394
            text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
395
                                       rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-30*i);
396
            text_set_halign(t_text[i], text_halign_left);
397
            text_set_valign(t_text[i], text_valign_bottom);
398
            text_set_color (t_text[i], TEXT_COLOR);
399
        }
400
    }
401

  
402
    /// loop stuff
403
    int ipc_status;
404
    message msg;
405

  
406
    char buffer[CHAT_MAX_SIZE] = "";
407

  
408
    int good = true;
409

  
410
    while (good) {
411
        /* Get a request message. */
412
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
413
            printf("driver_receive failed with %d", r);
414
            continue;
415
        }
416
        if (is_ipc_notify(ipc_status)) { /* received notification */
417
            switch (_ENDPOINT_P(msg.m_source)) {
418
                case HARDWARE: /* hardware interrupt notification */
419
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
420
                        if (msg.m_notify.interrupts & n) {
421
                            interrupt_handler(i);
422
                            switch (i) {
423
                            case TIMER0_IRQ:
424
                                graph_clear_screen();
425
                                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
426

  
427
                                rectangle_draw(r_buffer);
428
                                text_draw(t_buffer);
429

  
430
                                rectangle_draw(r_text);
431
                                for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_draw(t_text[i]);
432

  
433
                                sprite_draw(sp_crosshair);
434
                                graph_draw();
435
                                break;
436
                            case KBC_IRQ:
437
                                if ((scancode[0]) == ESC_BREAK_CODE) good = false;
438
                                else if ((scancode[0]) == ENTER_MAKE_CODE) {
439
                                    hltp_send_string(buffer);
440
                                    char buffer2[CHAT_MAX_SIZE+3] = "> ";
441
                                    strncat(buffer2, buffer, strlen(buffer));
442
                                    for(size_t i = CHAT_MAX_NUM-1; i; --i)
443
                                        text_set_text(t_text[i], text_get_string(t_text[i-1]));
444
                                    text_set_text(t_text[0], buffer2);
445
                                    for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
446
                                        if(text_get_string(t_text[i])[0] == '>'){
447
                                            text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
448
                                            text_set_halign(t_text[i], text_halign_left);
449
                                        }else{
450
                                            text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
451
                                            text_set_halign(t_text[i], text_halign_right);
452
                                        }
453
                                    }
454

  
455
                                    buffer[0] = '\0';
456
                                    text_set_text(t_buffer, buffer);
457
                                }
458
                                else {
459
                                    char c = map_makecode(scancode[0]);
460
                                    if (c == ERROR_CODE) break;
461
                                    if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1);
462
                                    else                    printf("Char limit exceeded\n");
463
                                    text_set_text(t_buffer, buffer);
464
                                }
465
                            case MOUSE_IRQ:
466
                                if (counter_mouse_ih >= 3) {
467
                                    mouse_parse_packet(packet_mouse_ih, &pp);
468
                                    update_mouse(&pp);
469
                                    counter_mouse_ih = 0;
470
                                }
471
                                break;
472
                            case COM1_IRQ:
473
                                nctp_ih();
474
                                break;
475
                            }
476
                        }
477
                    }
478

  
479
                    break;
480
                default:
481
                    break; /* no other notifications expected: do nothing */
482
            }
483
        } else { /* received standart message, not a notification */
484
            /* no standart message expected: do nothing */
485
        }
486
    }
487

  
488
    rectangle_dtor(r_buffer);
489
    text_dtor     (t_buffer);
490

  
491
    rectangle_dtor(r_text);
492
    for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
493

  
366 494
    return SUCCESS;
367 495
}

Also available in: Unified diff