Project

General

Profile

Statistics
| Revision:

root / proj / src / proj.c @ 297

History | View | Annotate | Download (19.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, "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
                            }
159
                        }
160
                    }
161

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

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

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

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

    
191
    return 0;
192
}
193

    
194
int (game)(void){
195

    
196
    int r;
197

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

    
201
    list_t *shooter_list = list_ctor();
202

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

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

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

    
214
    list_t *bullet_list  = list_ctor();
215

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

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

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

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

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

    
247
                               update_game_state(map1, shooter_list, bullet_list);
248

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

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

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

    
261
                               text_draw(in_game_timer->text);
262

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

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

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

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

    
315
    while(list_size(bullet_list) > 0){
316
        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
317
        bullet_dtor(p);
318
    }
319
    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
320

    
321
    timer_dtor(in_game_timer); in_game_timer = NULL;
322

    
323
    return SUCCESS;
324
}
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

    
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

    
494
    return SUCCESS;
495
}