Project

General

Profile

Statistics
| Revision:

root / proj / project / src / chat.c @ 365

History | View | Annotate | Download (6.94 KB)

1
#include <lcom/lcf.h>
2

    
3
#include "chat.h"
4

    
5
#include "proj.h"
6

    
7
#include "proj_macros.h"
8
#include "proj_func.h"
9
#include "ent.h"
10
#include "interrupts_func.h"
11
#include "libs.h"
12
#include "makecode_map.h"
13
#include <math.h>
14

    
15
#include "hltp.h"
16

    
17
#define CHAT_MAX_SIZE   75
18
#define CHAT_MAX_NUM    19
19
static text_t      *t_text[CHAT_MAX_NUM] = {NULL};
20
static rectangle_t *r_text               =  NULL;
21

    
22
static void chat_process(const uint8_t *p, const size_t sz){
23
    char buffer2[CHAT_MAX_NUM+3];
24
    void *dest = NULL;
25
    hltp_type tp = hltp_interpret(p, sz, &dest);
26
    switch(tp){
27
        case hltp_type_string:
28
        strcpy(buffer2, dest);
29
        strncat(buffer2, " <", 2);
30
        for(size_t i = CHAT_MAX_NUM-1; i; --i)
31
        text_set_string(t_text[i], text_get_string(t_text[i-1]));
32
        text_set_string(t_text[0], buffer2);
33
        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
34
            if(text_get_string(t_text[i])[0] == '>'){
35
                text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
36
                text_set_halign(t_text[i], text_halign_left);
37
            }else{
38
                text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
39
                text_set_halign(t_text[i], text_halign_right);
40
            }
41
        }
42
        break;
43
        case hltp_type_invalid: break;
44
        case hltp_type_bullet : break;
45
        case hltp_type_host   : break;
46
        case hltp_type_remote : break;
47
    }
48
}
49

    
50
int (chat)(void){
51
    int r;
52

    
53
    nctp_dump();
54
    nctp_set_processor(chat_process);
55

    
56
    struct packet pp;
57

    
58
    char buffer[CHAT_MAX_SIZE] = "";
59
    rectangle_t *r_buffer = NULL; {
60
        r_buffer = rectangle_ctor(0,0,900,70);
61
        rectangle_set_pos(r_buffer, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
62
        (int16_t)(graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2));
63
        rectangle_set_fill_color   (r_buffer, GRAPH_BLACK);
64
        rectangle_set_outline_width(r_buffer, 2);
65
        rectangle_set_outline_color(r_buffer, GRAPH_WHITE);
66
        rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT);
67
    }
68
    text_t      *t_buffer = NULL; {
69
        t_buffer = text_ctor(font_get_default(), "");
70
        text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
71
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
72
        text_set_halign(t_buffer, text_halign_left);
73
        text_set_valign(t_buffer, text_valign_center);
74
        text_set_color (t_buffer, TEXT_COLOR);
75
    }
76
    text_t      *t_size   = NULL; {
77
        t_size = text_ctor(font_get_default(), "");
78
        text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
79
        rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
80
        text_set_halign(t_size, text_halign_right);
81
        text_set_valign(t_size, text_valign_bottom);
82
        text_set_color (t_size, TEXT_COLOR);
83
        text_set_size  (t_size, 18);
84
        char buffer2[20];
85
        sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
86
        text_set_string(t_size, buffer2);
87
    }
88

    
89
    /** r_text */ {
90
    r_text = rectangle_ctor(0,0,900,550);
91
    rectangle_set_pos(r_text, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
92
    (int16_t)(graph_get_YRes()*0.09));
93
    rectangle_set_fill_color   (r_text, GRAPH_BLACK);
94
    rectangle_set_outline_width(r_text, 2);
95
    rectangle_set_outline_color(r_text, GRAPH_WHITE);
96
    rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT);
97
    }
98
    /** t_text */ {
99
    for(uint16_t i = 0; i < CHAT_MAX_NUM; ++i){
100
        t_text[i] = text_ctor(font_get_default(), " ");
101
        text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
102
        rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i);
103
        text_set_halign(t_text[i], text_halign_left);
104
        text_set_valign(t_text[i], text_valign_bottom);
105
        text_set_color (t_text[i], TEXT_COLOR);
106
    }
107
}
108

    
109
/// loop stuff
110
uint64_t int_vector = 0;
111
int good = true;
112
while (good) {
113
    /* Get a request message. */
114
    if((r = get_interrupts_vector(&int_vector))) return r;
115
    uint32_t n = 1;
116
    for (uint8_t i = 0; i < 32; i++, n <<= 1) {
117
        if (int_vector & n) {
118
            interrupt_handler(i);
119
            switch (i) {
120
                case TIMER0_IRQ:
121
                graph_clear_screen();
122
                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
123

    
124
                rectangle_draw(r_buffer);
125
                text_draw(t_buffer);
126
                text_draw(t_size);
127

    
128
                rectangle_draw(r_text);
129
                for(size_t j = 0; j < CHAT_MAX_NUM; ++j) text_draw(t_text[j]);
130

    
131
                sprite_draw(sp_crosshair);
132
                graph_draw();
133
                break;
134
                case KBC_IRQ:
135
                if      (keyboard_get_scancode()[0] == ESC_BREAK_CODE) good = false;
136
                else if (keyboard_get_scancode()[0] == ENTER_MAKE_CODE) {
137
                    hltp_send_string(buffer);
138
                    char buffer2[CHAT_MAX_SIZE+3] = "> ";
139
                    strncat(buffer2, buffer, strlen(buffer));
140
                    for(size_t j = CHAT_MAX_NUM-1; j; --j){
141
                        text_set_string(t_text[j], text_get_string(t_text[j-1]));
142
                    }
143
                    text_set_string(t_text[0], buffer2);
144
                    for(size_t j = 0; j < CHAT_MAX_NUM; ++j){
145
                        if(text_get_string(t_text[j])[0] == '>'){
146
                            text_set_pos(t_text[j], rectangle_get_x(r_text)+50, text_get_y(t_text[j]));
147
                            text_set_halign(t_text[j], text_halign_left);
148
                        }else{
149
                            text_set_pos(t_text[j], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[j]));
150
                            text_set_halign(t_text[j], text_halign_right);
151
                        }
152
                    }
153
                    buffer[0] = '\0';
154
                } else if(keyboard_get_scancode()[0] == BACKSPACE_MAKE_CODE){
155
                    buffer[strlen(buffer)-1] = '\0';
156
                } else {
157
                    char c = map_makecode(keyboard_get_scancode()[0]);
158
                    if (c == ERROR_CODE) break;
159
                    if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1);
160
                    else                               printf("Char limit exceeded\n");
161
                }
162
                text_set_string(t_buffer, buffer);
163
                char buffer2[20];
164
                sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
165
                text_set_string(t_size, buffer2);
166
                case MOUSE_IRQ:
167
                if (mouse_get_counter_mouse_ih() >= 3) {
168
                    mouse_parse_packet(mouse_get_packet_mouse_ih(), &pp);
169
                    update_mouse(&pp);
170
                    mouse_set_counter_mouse_ih(0);
171
                }
172
                break;
173
                case COM1_IRQ: nctp_ih(); break;
174
            }
175
        }
176
    }
177
}
178

    
179
rectangle_dtor(r_buffer);
180
text_dtor     (t_buffer);
181

    
182
rectangle_dtor(r_text);
183
for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
184

    
185
nctp_set_processor(NULL);
186

    
187
return SUCCESS;
188
}