Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 299

History | View | Annotate | Download (20.2 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 "pistol.h"
28
#include "nothing.h"
29
#include "bullet.h"
30
#include "map1.h"
31

    
32
#include "errors.h"
33

    
34
#include "list.h"
35

    
36
int main(int argc, char* argv[]) {
37

    
38
    lcf_set_language("EN-US");
39

    
40
    lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
41

    
42
    lcf_log_output("/home/lcom/labs/proj/output.txt");
43

    
44
    if (lcf_start(argc, argv)) return 1;
45

    
46
    lcf_cleanup();
47

    
48
    return 0;
49
}
50

    
51
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
int (game)(void);
61
int (chat)(void);
62

    
63
int(proj_main_loop)(int argc, char *argv[]) {
64

    
65
    int r;
66

    
67
    consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
68
    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
69

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

    
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
    }
79

    
80
    /// Load stuff
81
    {
82
        graph_clear_screen();
83
        text_t *txt = text_ctor(consolas, "Loading...");
84
        text_draw(txt);
85
        text_dtor(txt);
86
        graph_draw();
87

    
88
        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

    
95
        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
96
    }
97

    
98
    menu_t *main_menu = menu_ctor(consolas);
99
    menu_add_item(main_menu, "Single player");
100
    menu_add_item(main_menu, "Multiplayer");
101
    menu_add_item(main_menu, "Chat");
102
    menu_add_item(main_menu, "Exit");
103

    
104
    //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
    /// loop stuff
110
    int ipc_status;
111
    message msg;
112

    
113
    int click = 0;
114

    
115
    int good = true;
116

    
117
    while (good) {
118
        /* 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

    
132
                                graph_clear_screen();
133
                                switch(menu_update_state(main_menu, click)){
134
                                    case -1: break;
135
                                    case  0: game(); break;
136
                                    case  1: break;
137
                                    case  2: chat(); break;
138
                                    case  3: good = false; break;
139
                                }
140
                                menu_draw(main_menu);
141

    
142
                                click = 0;
143

    
144
                                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
145
                                sprite_draw(sp_crosshair);
146
                                graph_draw();
147

    
148
                                break;
149
                            case KBC_IRQ:
150
                                if ((scancode[0]) == ESC_BREAK_CODE) good = false;
151
                            case MOUSE_IRQ:
152
                                if (counter_mouse_ih >= 3) {
153
                                    mouse_parse_packet(packet_mouse_ih, &pp);
154
                                    update_mouse(&pp);
155
                                    if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
156
                                    last_lb = keys->lb_pressed;
157
                                    counter_mouse_ih = 0;
158
                                }
159
                                break;
160
                            case COM1_IRQ: nctp_ih(); break;
161
                            }
162
                        }
163
                    }
164

    
165
                    break;
166
                default:
167
                    break; /* no other notifications expected: do nothing */
168
            }
169
        } else { /* received standart message, not a notification */
170
            /* no standart message expected: do nothing */
171
        }
172
    }
173

    
174
    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

    
182
    // Unsubscribe interrupts
183
    if (unsubscribe_all()) {
184
        if (cleanup())
185
            printf("%s: failed to cleanup.\n", __func__);
186
        return 1;
187
    }
188

    
189
    if (cleanup()) {
190
        printf("%s: failed to cleanup.\n", __func__);
191
        return 1;
192
    }
193

    
194
    return 0;
195
}
196

    
197
int (game)(void){
198

    
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
    int good = true;
231

    
232
    while (good) {
233
       /* 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
                                   good = false;
274
                                   // 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
                           case COM1_IRQ: nctp_ih(); break;
301
                           }
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
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
318

    
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
    return SUCCESS;
328
}
329

    
330
#define CHAT_MAX_SIZE   75
331
#define CHAT_MAX_NUM    19
332

    
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
int (chat)(void){
362
    int r;
363

    
364
    nctp_dump();
365
    nctp_set_processor(chat_process);
366

    
367
    struct packet pp;
368

    
369
    char buffer[CHAT_MAX_SIZE] = "";
370
    rectangle_t *r_buffer = NULL; {
371
        r_buffer = rectangle_ctor(0,0,900,70);
372
        rectangle_set_pos(r_buffer, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
373
                                    graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2);
374
        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
    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

    
400
    /** r_text */ {
401
        r_text = rectangle_ctor(0,0,900,550);
402
        rectangle_set_pos(r_text, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
403
                                  graph_get_YRes()*0.09);
404
        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
                                       rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i);
414
            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
                                text_draw(t_size);
444

    
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
                                if      (scancode[0] == ESC_BREAK_CODE) good = false;
453
                                else if (scancode[0] == ENTER_MAKE_CODE) {
454
                                    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
                                } else if(scancode[0] == BACKSPACE_MAKE_CODE){
471
                                    buffer[strlen(buffer)-1] = '\0';
472
                                } else {
473
                                    char c = map_makecode(scancode[0]);
474
                                    if (c == ERROR_CODE) break;
475
                                    if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1);
476
                                    else                               printf("Char limit exceeded\n");
477
                                }
478
                                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
                            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
                            case COM1_IRQ: nctp_ih(); break;
490
                            }
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
    nctp_set_processor(NULL);
509

    
510
    return SUCCESS;
511
}