Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 298

History | View | Annotate | Download (20.1 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, "Play");
100
    menu_add_item(main_menu, "Chat");
101
    menu_add_item(main_menu, "Exit");
102

    
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

    
108
    /// loop stuff
109
    int ipc_status;
110
    message msg;
111

    
112
    int click = 0;
113

    
114
    int good = true;
115

    
116
    while (good) {
117
        /* Get a request message. */
118
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
119
            printf("driver_receive failed with %d", r);
120
            continue;
121
        }
122
        if (is_ipc_notify(ipc_status)) { /* received notification */
123
            switch (_ENDPOINT_P(msg.m_source)) {
124
                case HARDWARE: /* hardware interrupt notification */
125
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
126
                        if (msg.m_notify.interrupts & n) {
127
                            interrupt_handler(i);
128
                            switch (i) {
129
                            case TIMER0_IRQ:
130

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

    
140
                                click = 0;
141

    
142
                                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
143
                                sprite_draw(sp_crosshair);
144
                                graph_draw();
145

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

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

    
172
    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
173
    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
174
    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
175
    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
176
    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
177
    map_dtor               (map1         ); map1          = NULL;
178
    font_dtor              (consolas     ); consolas      = NULL;
179

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

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

    
192
    return 0;
193
}
194

    
195
int (game)(void){
196

    
197
    int r;
198

    
199
    ent_set_scale(DEFAULT_SCALE);
200
    text_timer_t *in_game_timer = timer_ctor(consolas);
201

    
202
    list_t *shooter_list = list_ctor();
203

    
204
    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
205
    gunner_set_spawn(shooter1, 75, 75);
206
    gunner_set_pos(shooter1, 75, 75);
207

    
208
    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
209
    gunner_set_spawn(shooter2, 975, 75);
210
    gunner_set_pos(shooter2, 775, 75);
211

    
212
    list_insert(shooter_list, list_end(shooter_list), shooter1);
213
    list_insert(shooter_list, list_end(shooter_list), shooter2);
214

    
215
    list_t *bullet_list  = list_ctor();
216

    
217
    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
218
                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
219

    
220
   //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
221
   uint8_t last_lb = 0;
222
   struct packet pp;
223
   keys_t *keys = get_key_presses();
224

    
225
    /// loop stuff
226
    int ipc_status;
227
    message msg;
228
    int good = true;
229

    
230
    while (good) {
231
       /* Get a request message. */
232
       if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
233
           printf("driver_receive failed with %d", r);
234
           continue;
235
       }
236
       if (is_ipc_notify(ipc_status)) { /* received notification */
237
           switch (_ENDPOINT_P(msg.m_source)) {
238
               case HARDWARE: /* hardware interrupt notification */
239
                   for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
240
                       if (msg.m_notify.interrupts & n) {
241
                           interrupt_handler(i);
242
                           switch (i) {
243
                           case TIMER0_IRQ:
244

    
245
                               if (no_interrupts % 60 == 0) timer_update(in_game_timer);
246
                               update_movement(map1, shooter1, keys, shooter_list);
247

    
248
                               update_game_state(map1, shooter_list, bullet_list);
249

    
250
                               //update_scale();
251
                               double angle = get_mouse_angle(shooter1);
252
                               gunner_set_angle(shooter1, angle - M_PI_2);
253

    
254
                               ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
255
                                              gunner_get_y(shooter1)-ent_get_YLength()/2.0);
256

    
257
                               graph_clear_screen();
258
                               map_draw   (map1);
259
                               gunner_draw_list(shooter_list);
260
                               bullet_draw_list(bullet_list);
261

    
262
                               text_draw(in_game_timer->text);
263

    
264
                               sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
265
                               sprite_draw(sp_crosshair);
266
                               graph_draw();
267

    
268
                               break;
269
                           case KBC_IRQ:
270
                               if ((scancode[0]) == ESC_BREAK_CODE) {
271
                                   good = false;
272
                                   // reset game
273
                                   while(list_size(bullet_list) > 0){
274
                                       bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
275
                                       bullet_dtor(p);
276
                                   }
277
                                   list_node_t *it = list_begin(shooter_list);
278
                                   while (it != list_end(shooter_list)) {
279
                                       gunner_t *p = *(gunner_t**)list_node_val(it);
280
                                       get_random_spawn(map1, p);
281
                                       gunner_set_curr_health(p, gunner_get_health(p));
282
                                       it = list_node_next(it);
283
                                   }
284
                                   timer_reset(in_game_timer);
285
                               }
286
                               break;
287
                           case MOUSE_IRQ:
288
                               if (counter_mouse_ih >= 3) {
289
                                   mouse_parse_packet(packet_mouse_ih, &pp);
290
                                   update_mouse(&pp);
291
                                   if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
292
                                       shoot_bullet(shooter1, bullet_list, bsp_bullet);
293
                                   last_lb = keys->lb_pressed;
294
                                   counter_mouse_ih = 0;
295

    
296
                               }
297
                               break;
298
                           case COM1_IRQ: nctp_ih(); break;
299
                           }
300
                       }
301
                   }
302
                   break;
303
               default:
304
                   break; /* no other notifications expected: do nothing */
305
           }
306
       } else { /* received standart message, not a notification */
307
           /* no standart message expected: do nothing */
308
       }
309
    }
310

    
311
    while(list_size(shooter_list) > 0){
312
        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
313
        gunner_dtor(p);
314
    }
315
    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
316

    
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
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
322

    
323
    timer_dtor(in_game_timer); in_game_timer = NULL;
324

    
325
    return SUCCESS;
326
}
327

    
328
#define CHAT_MAX_SIZE   75
329
#define CHAT_MAX_NUM    19
330

    
331
text_t      *t_text[CHAT_MAX_NUM] = {NULL};
332
rectangle_t *r_text               =  NULL;
333

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

    
359
int (chat)(void){
360
    int r;
361

    
362
    nctp_dump();
363
    nctp_set_processor(chat_process);
364

    
365
    struct packet pp;
366

    
367
    char buffer[CHAT_MAX_SIZE] = "";
368
    rectangle_t *r_buffer = NULL; {
369
        r_buffer = rectangle_ctor(0,0,900,70);
370
        rectangle_set_pos(r_buffer, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
371
                                    graph_get_YRes()*0.87-rectangle_get_h(r_buffer)/2);
372
        rectangle_set_fill_color   (r_buffer, GRAPH_BLACK);
373
        rectangle_set_outline_width(r_buffer, 2);
374
        rectangle_set_outline_color(r_buffer, GRAPH_WHITE);
375
        rectangle_set_fill_trans(r_buffer, GRAPH_TRANSPARENT);
376
    }
377
    text_t      *t_buffer = NULL; {
378
        t_buffer = text_ctor(consolas, "");
379
        text_set_pos(t_buffer, rectangle_get_x(r_buffer)+50,
380
                               rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)/2);
381
        text_set_halign(t_buffer, text_halign_left);
382
        text_set_valign(t_buffer, text_valign_center);
383
        text_set_color (t_buffer, TEXT_COLOR);
384
    }
385
    text_t      *t_size   = NULL; {
386
        t_size = text_ctor(consolas, "");
387
        text_set_pos(t_size, rectangle_get_x(r_buffer)+rectangle_get_w(r_buffer)-5,
388
                             rectangle_get_y(r_buffer)+rectangle_get_h(r_buffer)-5);
389
        text_set_halign(t_size, text_halign_right);
390
        text_set_valign(t_size, text_valign_bottom);
391
        text_set_color (t_size, TEXT_COLOR);
392
        text_set_size  (t_size, 18);
393
        char buffer2[20];
394
        sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
395
        text_set_text(t_size, buffer2);
396
    }
397

    
398
    /** r_text */ {
399
        r_text = rectangle_ctor(0,0,900,550);
400
        rectangle_set_pos(r_text, graph_get_XRes()/2  -rectangle_get_w(r_buffer)/2,
401
                                  graph_get_YRes()*0.09);
402
        rectangle_set_fill_color   (r_text, GRAPH_BLACK);
403
        rectangle_set_outline_width(r_text, 2);
404
        rectangle_set_outline_color(r_text, GRAPH_WHITE);
405
        rectangle_set_fill_trans(r_text, GRAPH_TRANSPARENT);
406
    }
407
    /** t_text */ {
408
        for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
409
            t_text[i] = text_ctor(consolas, " ");
410
            text_set_pos(t_text[i], rectangle_get_x(r_text)+50,
411
                                       rectangle_get_y(r_text)+rectangle_get_h(r_text)-30-25*i);
412
            text_set_halign(t_text[i], text_halign_left);
413
            text_set_valign(t_text[i], text_valign_bottom);
414
            text_set_color (t_text[i], TEXT_COLOR);
415
        }
416
    }
417

    
418
    /// loop stuff
419
    int ipc_status;
420
    message msg;
421
    int good = true;
422
    while (good) {
423
        /* Get a request message. */
424
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
425
            printf("driver_receive failed with %d", r);
426
            continue;
427
        }
428
        if (is_ipc_notify(ipc_status)) { /* received notification */
429
            switch (_ENDPOINT_P(msg.m_source)) {
430
                case HARDWARE: /* hardware interrupt notification */
431
                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
432
                        if (msg.m_notify.interrupts & n) {
433
                            interrupt_handler(i);
434
                            switch (i) {
435
                            case TIMER0_IRQ:
436
                                graph_clear_screen();
437
                                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
438

    
439
                                rectangle_draw(r_buffer);
440
                                text_draw(t_buffer);
441
                                text_draw(t_size);
442

    
443
                                rectangle_draw(r_text);
444
                                for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_draw(t_text[i]);
445

    
446
                                sprite_draw(sp_crosshair);
447
                                graph_draw();
448
                                break;
449
                            case KBC_IRQ:
450
                                if      (scancode[0] == ESC_BREAK_CODE) good = false;
451
                                else if (scancode[0] == ENTER_MAKE_CODE) {
452
                                    hltp_send_string(buffer);
453
                                    char buffer2[CHAT_MAX_SIZE+3] = "> ";
454
                                    strncat(buffer2, buffer, strlen(buffer));
455
                                    for(size_t i = CHAT_MAX_NUM-1; i; --i)
456
                                        text_set_text(t_text[i], text_get_string(t_text[i-1]));
457
                                    text_set_text(t_text[0], buffer2);
458
                                    for(size_t i = 0; i < CHAT_MAX_NUM; ++i){
459
                                        if(text_get_string(t_text[i])[0] == '>'){
460
                                            text_set_pos(t_text[i], rectangle_get_x(r_text)+50, text_get_y(t_text[i]));
461
                                            text_set_halign(t_text[i], text_halign_left);
462
                                        }else{
463
                                            text_set_pos(t_text[i], rectangle_get_x(r_text)+rectangle_get_w(r_text)-50, text_get_y(t_text[i]));
464
                                            text_set_halign(t_text[i], text_halign_right);
465
                                        }
466
                                    }
467
                                    buffer[0] = '\0';
468
                                } else if(scancode[0] == BACKSPACE_MAKE_CODE){
469
                                    buffer[strlen(buffer)-1] = '\0';
470
                                } else {
471
                                    char c = map_makecode(scancode[0]);
472
                                    if (c == ERROR_CODE) break;
473
                                    if(strlen(buffer) < CHAT_MAX_SIZE) strncat(buffer, &c, 1);
474
                                    else                               printf("Char limit exceeded\n");
475
                                }
476
                                text_set_text(t_buffer, buffer);
477
                                char buffer2[20];
478
                                sprintf(buffer2, "%d/%d", strlen(buffer), CHAT_MAX_SIZE);
479
                                text_set_text(t_size, buffer2);
480
                            case MOUSE_IRQ:
481
                                if (counter_mouse_ih >= 3) {
482
                                    mouse_parse_packet(packet_mouse_ih, &pp);
483
                                    update_mouse(&pp);
484
                                    counter_mouse_ih = 0;
485
                                }
486
                                break;
487
                            case COM1_IRQ: nctp_ih(); break;
488
                            }
489
                        }
490
                    }
491
                    break;
492
                default:
493
                    break; /* no other notifications expected: do nothing */
494
            }
495
        } else { /* received standart message, not a notification */
496
            /* no standart message expected: do nothing */
497
        }
498
    }
499

    
500
    rectangle_dtor(r_buffer);
501
    text_dtor     (t_buffer);
502

    
503
    rectangle_dtor(r_text);
504
    for(size_t i = 0; i < CHAT_MAX_NUM; ++i) text_dtor(t_text[i]);
505

    
506
    nctp_set_processor(NULL);
507

    
508
    return SUCCESS;
509
}