Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 299

History | View | Annotate | Download (20.2 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
#include "pistol.h"
28
#include "nothing.h"
29 237 up20180642
#include "bullet.h"
30 216 up20180642
#include "map1.h"
31 183 up20180642
32 294 up20180642
#include "errors.h"
33
34 226 up20180642
#include "list.h"
35
36 144 up20180655
int main(int argc, char* argv[]) {
37
38
    lcf_set_language("EN-US");
39
40 235 up20180642
    lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
41 144 up20180655
42 189 up20180655
    lcf_log_output("/home/lcom/labs/proj/output.txt");
43 144 up20180655
44
    if (lcf_start(argc, argv)) return 1;
45
46
    lcf_cleanup();
47
48
    return 0;
49
}
50 147 up20180655
51 294 up20180642
font_t               *consolas      = NULL;
52
basic_sprite_t       *bsp_crosshair = NULL;
53
basic_sprite_t       *bsp_shooter   = NULL;
54
basic_sprite_t       *bsp_pistol    = NULL;
55
basic_sprite_t       *bsp_nothing   = NULL;
56
basic_sprite_t       *bsp_bullet    = NULL;
57
map_t                *map1          = NULL;
58
sprite_t             *sp_crosshair  = NULL;
59
60 296 up20180642
int (game)(void);
61
int (chat)(void);
62 294 up20180642
63 149 up20180655
int(proj_main_loop)(int argc, char *argv[]) {
64 170 up20180642
65
    int r;
66
67 294 up20180642
    consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
68 261 up20180642
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
69 192 up20180642
70 261 up20180642
    /// subscribe interrupts
71
    if (subscribe_all()) { return 1; }
72 170 up20180642
73 297 up20180642
    /// 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
    }
79 152 up20180642
80 261 up20180642
    /// Load stuff
81
    {
82 297 up20180642
        graph_clear_screen();
83
        text_t *txt = text_ctor(consolas, "Loading...");
84
        text_draw(txt);
85
        text_dtor(txt);
86
        graph_draw();
87 183 up20180642
88 261 up20180642
        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
89
        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
90
        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
91
        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
92
        bsp_bullet    = get_bullet   (); if(bsp_bullet    == NULL) printf("Failed to get bullet\n");
93
        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
94 226 up20180642
95 261 up20180642
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
96
    }
97 226 up20180642
98 297 up20180642
    menu_t *main_menu = menu_ctor(consolas);
99 299 up20180642
    menu_add_item(main_menu, "Single player");
100
    menu_add_item(main_menu, "Multiplayer");
101 297 up20180642
    menu_add_item(main_menu, "Chat");
102
    menu_add_item(main_menu, "Exit");
103 234 up20180655
104 297 up20180642
    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
105
    uint8_t last_lb = 0;
106
    struct packet pp;
107
    keys_t *keys = get_key_presses();
108
109 261 up20180642
    /// loop stuff
110
    int ipc_status;
111
    message msg;
112 267 up20180655
113 297 up20180642
    int click = 0;
114 267 up20180655
115 295 up20180642
    int good = true;
116
117
    while (good) {
118 261 up20180642
        /* Get a request message. */
119
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
120
            printf("driver_receive failed with %d", r);
121
            continue;
122
        }
123
        if (is_ipc_notify(ipc_status)) { /* received notification */
124
            switch (_ENDPOINT_P(msg.m_source)) {
125
                case HARDWARE: /* hardware interrupt notification */
126
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
127
                        if (msg.m_notify.interrupts & n) {
128
                            interrupt_handler(i);
129
                            switch (i) {
130
                            case TIMER0_IRQ:
131 192 up20180642
132 295 up20180642
                                graph_clear_screen();
133
                                switch(menu_update_state(main_menu, click)){
134
                                    case -1: break;
135
                                    case  0: game(); break;
136 299 up20180642
                                    case  1: break;
137
                                    case  2: chat(); break;
138
                                    case  3: good = false; break;
139 295 up20180642
                                }
140
                                menu_draw(main_menu);
141 192 up20180642
142 295 up20180642
                                click = 0;
143 202 up20180655
144 295 up20180642
                                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
145
                                sprite_draw(sp_crosshair);
146
                                graph_draw();
147 231 up20180655
148 261 up20180642
                                break;
149
                            case KBC_IRQ:
150 295 up20180642
                                if ((scancode[0]) == ESC_BREAK_CODE) good = false;
151 261 up20180642
                            case MOUSE_IRQ:
152
                                if (counter_mouse_ih >= 3) {
153
                                    mouse_parse_packet(packet_mouse_ih, &pp);
154
                                    update_mouse(&pp);
155 295 up20180642
                                    if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
156 261 up20180642
                                    last_lb = keys->lb_pressed;
157
                                    counter_mouse_ih = 0;
158 220 up20180655
                                }
159 261 up20180642
                                break;
160 298 up20180642
                            case COM1_IRQ: nctp_ih(); break;
161 220 up20180655
                            }
162 251 up20180655
                        }
163 261 up20180642
                    }
164 167 up20180655
165 261 up20180642
                    break;
166
                default:
167
                    break; /* no other notifications expected: do nothing */
168 147 up20180655
            }
169 261 up20180642
        } else { /* received standart message, not a notification */
170
            /* no standart message expected: do nothing */
171 147 up20180655
        }
172 261 up20180642
    }
173 149 up20180655
174 261 up20180642
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
175
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
176
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
177
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
178
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
179
    map_dtor               (map1         ); map1          = NULL;
180
    font_dtor              (consolas     ); consolas      = NULL;
181 243 up20180642
182 261 up20180642
    // Unsubscribe interrupts
183
    if (unsubscribe_all()) {
184
        if (cleanup())
185
            printf("%s: failed to cleanup.\n", __func__);
186
        return 1;
187
    }
188 188 up20180642
189 261 up20180642
    if (cleanup()) {
190
        printf("%s: failed to cleanup.\n", __func__);
191
        return 1;
192
    }
193 155 up20180655
194 149 up20180655
    return 0;
195 147 up20180655
}
196 294 up20180642
197 296 up20180642
int (game)(void){
198 295 up20180642
199
    int r;
200
201
    ent_set_scale(DEFAULT_SCALE);
202
    text_timer_t *in_game_timer = timer_ctor(consolas);
203
204
    list_t *shooter_list = list_ctor();
205
206
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
207
    gunner_set_spawn(shooter1, 75, 75);
208
    gunner_set_pos(shooter1, 75, 75);
209
210
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
211
    gunner_set_spawn(shooter2, 975, 75);
212
    gunner_set_pos(shooter2, 775, 75);
213
214
    list_insert(shooter_list, list_end(shooter_list), shooter1);
215
    list_insert(shooter_list, list_end(shooter_list), shooter2);
216
217
    list_t *bullet_list  = list_ctor();
218
219
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
220
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
221
222
   //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
223
   uint8_t last_lb = 0;
224
   struct packet pp;
225
   keys_t *keys = get_key_presses();
226
227
    /// loop stuff
228
    int ipc_status;
229
    message msg;
230 296 up20180642
    int good = true;
231 295 up20180642
232 296 up20180642
    while (good) {
233 295 up20180642
       /* Get a request message. */
234
       if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
235
           printf("driver_receive failed with %d", r);
236
           continue;
237
       }
238
       if (is_ipc_notify(ipc_status)) { /* received notification */
239
           switch (_ENDPOINT_P(msg.m_source)) {
240
               case HARDWARE: /* hardware interrupt notification */
241
                   for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
242
                       if (msg.m_notify.interrupts & n) {
243
                           interrupt_handler(i);
244
                           switch (i) {
245
                           case TIMER0_IRQ:
246
247
                               if (no_interrupts % 60 == 0) timer_update(in_game_timer);
248
                               update_movement(map1, shooter1, keys, shooter_list);
249
250
                               update_game_state(map1, shooter_list, bullet_list);
251
252
                               //update_scale();
253
                               double angle = get_mouse_angle(shooter1);
254
                               gunner_set_angle(shooter1, angle - M_PI_2);
255
256
                               ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
257
                                              gunner_get_y(shooter1)-ent_get_YLength()/2.0);
258
259
                               graph_clear_screen();
260
                               map_draw   (map1);
261
                               gunner_draw_list(shooter_list);
262
                               bullet_draw_list(bullet_list);
263
264
                               text_draw(in_game_timer->text);
265
266
                               sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
267
                               sprite_draw(sp_crosshair);
268
                               graph_draw();
269
270
                               break;
271
                           case KBC_IRQ:
272
                               if ((scancode[0]) == ESC_BREAK_CODE) {
273 296 up20180642
                                   good = false;
274 295 up20180642
                                   // reset game
275
                                   while(list_size(bullet_list) > 0){
276
                                       bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
277
                                       bullet_dtor(p);
278
                                   }
279
                                   list_node_t *it = list_begin(shooter_list);
280
                                   while (it != list_end(shooter_list)) {
281
                                       gunner_t *p = *(gunner_t**)list_node_val(it);
282
                                       get_random_spawn(map1, p);
283
                                       gunner_set_curr_health(p, gunner_get_health(p));
284
                                       it = list_node_next(it);
285
                                   }
286
                                   timer_reset(in_game_timer);
287
                               }
288
                               break;
289
                           case MOUSE_IRQ:
290
                               if (counter_mouse_ih >= 3) {
291
                                   mouse_parse_packet(packet_mouse_ih, &pp);
292
                                   update_mouse(&pp);
293
                                   if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
294
                                       shoot_bullet(shooter1, bullet_list, bsp_bullet);
295
                                   last_lb = keys->lb_pressed;
296
                                   counter_mouse_ih = 0;
297
298
                               }
299
                               break;
300 298 up20180642
                           case COM1_IRQ: nctp_ih(); break;
301 295 up20180642
                           }
302
                       }
303
                   }
304
                   break;
305
               default:
306
                   break; /* no other notifications expected: do nothing */
307
           }
308
       } else { /* received standart message, not a notification */
309
           /* no standart message expected: do nothing */
310
       }
311
    }
312
313
    while(list_size(shooter_list) > 0){
314
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
315
        gunner_dtor(p);
316
    }
317 296 up20180642
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
318 295 up20180642
319
    while(list_size(bullet_list) > 0){
320
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
321
        bullet_dtor(p);
322
    }
323
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
324
325
    timer_dtor(in_game_timer); in_game_timer = NULL;
326
327 294 up20180642
    return SUCCESS;
328
}
329 296 up20180642
330 298 up20180642
#define CHAT_MAX_SIZE   75
331
#define CHAT_MAX_NUM    19
332 297 up20180642
333
text_t      *t_text[CHAT_MAX_NUM] = {NULL};
334
rectangle_t *r_text               =  NULL;
335
336
static void chat_process(const uint8_t *p, const size_t sz){
337
    char buffer2[CHAT_MAX_NUM+3];
338
    void *dest = NULL;
339
    hltp_type tp = hltp_interpret(p, sz, &dest);
340
    switch(tp){
341
        case hltp_type_string:
342
            strcpy(buffer2, dest);
343
            strncat(buffer2, " <", 2);
344
            for(size_t i = CHAT_MAX_NUM-1; i; --i)
345
                text_set_text(t_text[i], text_get_string(t_text[i-1]));
346
            text_set_text(t_text[0], buffer2);
347
            for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
348
                if(text_get_string(t_text[i])[0] == '>'){
349
                    text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
350
                    text_set_halign(t_text[i], text_halign_left);
351
                }else{
352
                    text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
353
                    text_set_halign(t_text[i], text_halign_right);
354
                }
355
            }
356
            break;
357
        default: break;
358
    }
359
}
360
361 296 up20180642
int (chat)(void){
362 297 up20180642
    int r;
363
364 298 up20180642
    nctp_dump();
365 297 up20180642
    nctp_set_processor(chat_process);
366
367
    struct packet pp;
368
369 298 up20180642
    char buffer[CHAT_MAX_SIZE] = "";
370 297 up20180642
    rectangle_t *r_buffer = NULL; {
371 298 up20180642
        r_buffer = rectangle_ctor(0,0,900,70);
372 297 up20180642
        rectangle_set_pos(r_buffer, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
373 298 up20180642
                                    graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2);
374 297 up20180642
        rectangle_set_fill_color   (r_buffer, GRAPH_BLACK);
375
        rectangle_set_outline_width(r_buffer, 2);
376
        rectangle_set_outline_color(r_buffer, GRAPH_WHITE);
377
        rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT);
378
    }
379
    text_t      *t_buffer = NULL; {
380
        t_buffer = text_ctor(consolas, "");
381
        text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
382
                               rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
383
        text_set_halign(t_buffer, text_halign_left);
384
        text_set_valign(t_buffer, text_valign_center);
385
        text_set_color (t_buffer, TEXT_COLOR);
386
    }
387 298 up20180642
    text_t      *t_size   = NULL; {
388
        t_size = text_ctor(consolas, "");
389
        text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
390
                             rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
391
        text_set_halign(t_size, text_halign_right);
392
        text_set_valign(t_size, text_valign_bottom);
393
        text_set_color (t_size, TEXT_COLOR);
394
        text_set_size  (t_size, 18);
395
        char buffer2[20];
396
        sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
397
        text_set_text(t_size, buffer2);
398
    }
399 297 up20180642
400
    /** r_text */ {
401 298 up20180642
        r_text = rectangle_ctor(0,0,900,550);
402 297 up20180642
        rectangle_set_pos(r_text, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
403 298 up20180642
                                  graph_get_YRes()*0.09);
404 297 up20180642
        rectangle_set_fill_color   (r_text, GRAPH_BLACK);
405
        rectangle_set_outline_width(r_text, 2);
406
        rectangle_set_outline_color(r_text, GRAPH_WHITE);
407
        rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT);
408
    }
409
    /** t_text */ {
410
        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
411
            t_text[i] = text_ctor(consolas, " ");
412
            text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
413 298 up20180642
                                       rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i);
414 297 up20180642
            text_set_halign(t_text[i], text_halign_left);
415
            text_set_valign(t_text[i], text_valign_bottom);
416
            text_set_color (t_text[i], TEXT_COLOR);
417
        }
418
    }
419
420
    /// loop stuff
421
    int ipc_status;
422
    message msg;
423
    int good = true;
424
    while (good) {
425
        /* Get a request message. */
426
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
427
            printf("driver_receive failed with %d", r);
428
            continue;
429
        }
430
        if (is_ipc_notify(ipc_status)) { /* received notification */
431
            switch (_ENDPOINT_P(msg.m_source)) {
432
                case HARDWARE: /* hardware interrupt notification */
433
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
434
                        if (msg.m_notify.interrupts & n) {
435
                            interrupt_handler(i);
436
                            switch (i) {
437
                            case TIMER0_IRQ:
438
                                graph_clear_screen();
439
                                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
440
441
                                rectangle_draw(r_buffer);
442
                                text_draw(t_buffer);
443 298 up20180642
                                text_draw(t_size);
444 297 up20180642
445
                                rectangle_draw(r_text);
446
                                for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_draw(t_text[i]);
447
448
                                sprite_draw(sp_crosshair);
449
                                graph_draw();
450
                                break;
451
                            case KBC_IRQ:
452 298 up20180642
                                if      (scancode[0] == ESC_BREAK_CODE) good = false;
453
                                else if (scancode[0] == ENTER_MAKE_CODE) {
454 297 up20180642
                                    hltp_send_string(buffer);
455
                                    char buffer2[CHAT_MAX_SIZE+3] = "> ";
456
                                    strncat(buffer2, buffer, strlen(buffer));
457
                                    for(size_t i = CHAT_MAX_NUM-1; i; --i)
458
                                        text_set_text(t_text[i], text_get_string(t_text[i-1]));
459
                                    text_set_text(t_text[0], buffer2);
460
                                    for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
461
                                        if(text_get_string(t_text[i])[0] == '>'){
462
                                            text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
463
                                            text_set_halign(t_text[i], text_halign_left);
464
                                        }else{
465
                                            text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
466
                                            text_set_halign(t_text[i], text_halign_right);
467
                                        }
468
                                    }
469
                                    buffer[0] = '\0';
470 298 up20180642
                                } else if(scancode[0] == BACKSPACE_MAKE_CODE){
471
                                    buffer[strlen(buffer)-1] = '\0';
472
                                } else {
473 297 up20180642
                                    char c = map_makecode(scancode[0]);
474
                                    if (c == ERROR_CODE) break;
475
                                    if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1);
476 298 up20180642
                                    else                               printf("Char limit exceeded\n");
477 297 up20180642
                                }
478 298 up20180642
                                text_set_text(t_buffer, buffer);
479
                                char buffer2[20];
480
                                sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
481
                                text_set_text(t_size, buffer2);
482 297 up20180642
                            case MOUSE_IRQ:
483
                                if (counter_mouse_ih >= 3) {
484
                                    mouse_parse_packet(packet_mouse_ih, &pp);
485
                                    update_mouse(&pp);
486
                                    counter_mouse_ih = 0;
487
                                }
488
                                break;
489 298 up20180642
                            case COM1_IRQ: nctp_ih(); break;
490 297 up20180642
                            }
491
                        }
492
                    }
493
                    break;
494
                default:
495
                    break; /* no other notifications expected: do nothing */
496
            }
497
        } else { /* received standart message, not a notification */
498
            /* no standart message expected: do nothing */
499
        }
500
    }
501
502
    rectangle_dtor(r_buffer);
503
    text_dtor     (t_buffer);
504
505
    rectangle_dtor(r_text);
506
    for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
507
508 298 up20180642
    nctp_set_processor(NULL);
509
510 296 up20180642
    return SUCCESS;
511
}