Index: proj.c
===================================================================
--- proj.c	(revision 260)
+++ proj.c	(revision 261)
@@ -52,306 +52,231 @@
 int(proj_main_loop)(int argc, char *argv[]) {
 
     int r;
-    #ifndef DIOGO
-        font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
-        if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
 
-        /// subscribe interrupts
-        if (subscribe_all()) { return 1; }
+    font_t *consolas = font_ctor("/home/lcom/labs/proj/media/font/Consolas/xpm2");
+    if(consolas == NULL){ printf("Failed to load consolas\n"); return 1; }
 
-        /// initialize graphics
-        if(graph_init(GRAPH_MODE)){
-            printf("%s: failed to initalize graphics.\n", __func__);
-            if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
-            return 1;
-        }
+    /// subscribe interrupts
+    if (subscribe_all()) { return 1; }
 
-        /// Load stuff
-        basic_sprite_t       *bsp_crosshair = NULL;
-        basic_sprite_t       *bsp_shooter   = NULL;
-        basic_sprite_t       *bsp_pistol    = NULL;
-        basic_sprite_t       *bsp_nothing   = NULL;
-        basic_sprite_t       *bsp_bullet    = NULL;
-        map_t                *map1          = NULL;
-        sprite_t             *sp_crosshair  = NULL;
-        {
-            graph_clear_screen();
-            text_t *txt = text_ctor(consolas, "Loading...");
-            text_draw(txt);
-            text_dtor(txt);
-            graph_draw();
+    /// initialize graphics
+    if(graph_init(GRAPH_MODE)){
+        printf("%s: failed to initalize graphics.\n", __func__);
+        if (cleanup()) printf("%s: failed to cleanup.\n", __func__);
+        return 1;
+    }
 
-            bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
-            bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
-            bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
-            bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
-            bsp_bullet    = get_bullet   (); if(bsp_bullet    == NULL) printf("Failed to get bullet\n");
-            map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
-
-            sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
-        }
-    #endif
-
-    #ifdef DIOGO
-        /*
+    /// Load stuff
+    basic_sprite_t       *bsp_crosshair = NULL;
+    basic_sprite_t       *bsp_shooter   = NULL;
+    basic_sprite_t       *bsp_pistol    = NULL;
+    basic_sprite_t       *bsp_nothing   = NULL;
+    basic_sprite_t       *bsp_bullet    = NULL;
+    map_t                *map1          = NULL;
+    sprite_t             *sp_crosshair  = NULL;
+    {
         graph_clear_screen();
-
-        rectangle_t *rect = rectangle_ctor(0,0,400,100);
-        rectangle_set_pos(rect,
-                          graph_get_XRes()/2    - rectangle_get_w(rect)/2,
-                          graph_get_YRes()*0.25 - rectangle_get_h(rect)/2);
-        rectangle_set_fill_color(rect, BLACK);
-        rectangle_set_outline_width(rect, 2);
-        rectangle_set_outline_color(rect, WHITE);
-        rectangle_draw(rect);
-
-        text_t *txt  = text_ctor(consolas, "Hello world!");
-        text_set_color(txt, 0x888888);
-
-        text_set_pos(txt, rectangle_get_x(rect)+rectangle_get_w(rect)/2,
-                          rectangle_get_y(rect)+rectangle_get_h(rect)/2);
-        text_set_valign(txt, text_valign_center);
-        text_set_halign(txt, text_halign_center);
+        text_t *txt = text_ctor(consolas, "Loading...");
         text_draw(txt);
         text_dtor(txt);
-
         graph_draw();
-        rectangle_dtor(rect);
 
-        list_t *l = list_ctor();
-        int *p = NULL;
-        for(int i = 10; i < 20; ++i){
-            p = malloc(sizeof(int));
-            *p = i;
-            printf("INSERTING %d\n", i);
-            list_insert(l, list_end(l), p);
-            printf("INSERTED, SIZE=%d\n", list_size(l));
-        }
-        list_node_t *it = list_begin(l);
-        while(it != list_end(l)){
-            printf("%d\n", **(int**)list_node_val(it));
-            it = list_node_next(it);
-        }
-        while(list_size(l) > 0){
-            printf("ERASING\n");
-            void *p = list_erase(l, list_begin(l));
-            printf("ERASED %d, SIZE=%d\n", *(int*)p, list_size(l));
-            free(p);
-        }
-        printf("DONE\n");
-        if(list_dtor(l)) printf("COULD NOT DESTRUCT LIST\n");
+        bsp_crosshair = get_crosshair(); if(bsp_crosshair == NULL) printf("Failed to get crosshair\n");
+        bsp_shooter   = get_shooter  (); if(bsp_shooter   == NULL) printf("Failed to get shooter\n");
+        bsp_pistol    = get_pistol   (); if(bsp_pistol    == NULL) printf("Failed to get pistol\n");
+        bsp_nothing   = get_nothing  (); if(bsp_nothing   == NULL) printf("Failed to get nothing\n");
+        bsp_bullet    = get_bullet   (); if(bsp_bullet    == NULL) printf("Failed to get bullet\n");
+        map1          = get_map1     (); if(map1          == NULL) printf("Failed to get map1\n");
 
+        sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
+    }
 
-        tickdelay(micros_to_ticks(1000000));*/
+    ent_set_scale(DEFAULT_SCALE);
 
-        // RTC
-        /*
-        uint8_t date[4], time[3];
+    text_timer_t *in_game_timer = timer_ctor(consolas);
 
-        if (rtc_read_time(time)) return 1;
+    menu_t *main_menu = menu_ctor(consolas);
 
-        if (rtc_read_date(date)) return 1;
+    list_t *shooter_list = list_ctor();
 
-        printf("Hour: %02d:%02d:%02d\n", time[2], time[1], time[0]);
+    gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
+    gunner_set_spawn(shooter1, 75, 75);
+    gunner_set_pos(shooter1, 75, 75);
 
-        printf("Date: %d, %02d/%02d/%02d\n", date[0], date[1], date[2], date[3]);
-        */
-        //UART
-        unsigned long bits = 8;
-        unsigned long stop = 2;
-        long parity = 0;
-        unsigned long rate = 9600;
-        unsigned char tx = 0; (void)tx;
-        int stringc = 2; (void)stringc;
-        char *strings[] = {"Hello", "world"}; (void)strings;
-        //if((r = ser_test_set(COM1_ADDR, bits, stop, parity, rate))) return r;
-        //if((r = ser_test_conf(COM1_ADDR))) return r;
-        if((r = ser_test_poll(COM1_ADDR, tx, bits, stop, parity, rate, stringc, strings))) return r;
-        printf("DONE\n");
-    #endif
-    #ifndef DIOGO
-        ent_set_scale(DEFAULT_SCALE);
+    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
+    gunner_set_spawn(shooter2, 975, 75);
+    gunner_set_pos(shooter2, 775, 75);
 
-        text_timer_t *in_game_timer = timer_ctor(consolas);
+    list_insert(shooter_list, list_end(shooter_list), shooter1);
+    list_insert(shooter_list, list_end(shooter_list), shooter2);
 
-        menu_t *main_menu = menu_ctor(consolas);
+    list_t *bullet_list  = list_ctor();
 
-        list_t *shooter_list = list_ctor();
+    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
+                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
 
-        gunner_t *shooter1 = gunner_ctor(bsp_shooter, bsp_pistol); if(shooter1 == NULL) printf("Failed to get shooter1\n");
-        gunner_set_spawn(shooter1, 75, 75);
-        gunner_set_pos(shooter1, 75, 75);
+    //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
+    double angle; // mouse angle
+    int32_t *mouse_x = get_mouse_X(), *mouse_y = get_mouse_Y();
+    uint8_t last_lb = 0;
+    struct packet pp;
+    keys_t *keys = get_key_presses();
+    /// loop stuff
+    int ipc_status;
+    message msg;
+    int game_state = MENU, click = 0; (void)click;
 
-        gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
-        gunner_set_spawn(shooter2, 975, 75);
-        gunner_set_pos(shooter2, 775, 75);
+    while (game_state != EXIT) {
+        /* Get a request message. */
+        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
+            printf("driver_receive failed with %d", r);
+            continue;
+        }
+        if (is_ipc_notify(ipc_status)) { /* received notification */
+            switch (_ENDPOINT_P(msg.m_source)) {
+                case HARDWARE: /* hardware interrupt notification */
+                    for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
+                        if (msg.m_notify.interrupts & n) {
+                            interrupt_handler(i);
+                            switch (i) {
+                            #ifndef DIOGO
+                            case TIMER0_IRQ:
+                                switch (game_state) {
+                                case MENU:
+                                    if (no_interrupts % 2 == 0){
+                                        graph_clear_screen();
+                                        game_state = menu_update_state(main_menu, click);
+                                        menu_draw(main_menu);
 
-        list_insert(shooter_list, list_end(shooter_list), shooter1);
-        list_insert(shooter_list, list_end(shooter_list), shooter2);
+                                        click = 0;
 
-        list_t *bullet_list  = list_ctor();
+                                        sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
+                                        sprite_draw(sp_crosshair);
+                                        graph_draw();
+                                    }
+                                    break;
+                                case GAME:
+                                    if (no_interrupts % 60 == 0) timer_update(in_game_timer);
+                                    update_movement(map1, shooter1, keys, shooter_list);
 
-        ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
-                       gunner_get_y(shooter1)-ent_get_YLength()/2.0);
+                                    update_game_state(map1, shooter_list, bullet_list);
 
-        //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
-        double angle; // mouse angle
-        int32_t *mouse_x = get_mouse_X(), *mouse_y = get_mouse_Y();
-        uint8_t last_lb = 0;
-        struct packet pp;
-        keys_t *keys = get_key_presses();
+                                    //update_scale();
+                                    angle = get_mouse_angle(shooter1);
+                                    gunner_set_angle(shooter1, angle - M_PI_2);
 
-        /// loop stuff
-        int ipc_status;
-        message msg;
-        int game_state = MENU, click = 0;
+                                    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
+                                                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
 
-        while (game_state != EXIT) {
-            /* Get a request message. */
-            if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
-                printf("driver_receive failed with %d", r);
-                continue;
-            }
-            if (is_ipc_notify(ipc_status)) { /* received notification */
-                switch (_ENDPOINT_P(msg.m_source)) {
-                    case HARDWARE: /* hardware interrupt notification */
-                        for (uint32_t i = 0, n = 1; i < 32; i++, n <<= 1) {
-                            if (msg.m_notify.interrupts & n) {
-                                interrupt_handler(i);
-                                switch (i) {
-                                case TIMER0_IRQ:
-                                    switch (game_state) {
-                                    case MENU:
-                                        if (no_interrupts % 2 == 0){
-                                            graph_clear_screen();
-                                            game_state = menu_update_state(main_menu, click);
-                                            menu_draw(main_menu);
+                                    graph_clear_screen();
+                                    map_draw   (map1);
+                                    gunner_draw_list(shooter_list);
+                                    bullet_draw_list(bullet_list);
 
-                                            click = 0;
+                                    text_draw(in_game_timer->text);
 
-                                            sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
-                                            sprite_draw(sp_crosshair);
-                                            graph_draw();
+                                    sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
+                                    sprite_draw(sp_crosshair);
+                                    graph_draw();
+                                    break;
+                                }
+                                break;
+                            case KBC_IRQ:
+                                switch (game_state) {
+                                case MENU:
+                                    if ((scancode[0]) == ESC_BREAK_CODE) game_state = EXIT;
+                                    break;
+                                case GAME:
+                                    if ((scancode[0]) == ESC_BREAK_CODE) {
+                                        game_state = MENU;
+                                        // reset game
+                                        while(list_size(bullet_list) > 0){
+                                            bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
+                                            bullet_dtor(p);
                                         }
-                                        break;
-                                    case GAME:
-                                        if (no_interrupts % 60 == 0) timer_update(in_game_timer);
-                                        update_movement(map1, shooter1, keys, shooter_list);
-
-                                        update_game_state(map1, shooter_list, bullet_list);
-
-                                        //update_scale();
-                                        angle = get_mouse_angle(shooter1);
-                                        gunner_set_angle(shooter1, angle - M_PI_2);
-
-                                        ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
-                                                       gunner_get_y(shooter1)-ent_get_YLength()/2.0);
-
-                                        graph_clear_screen();
-                                        map_draw   (map1);
-                                        gunner_draw_list(shooter_list);
-                                        bullet_draw_list(bullet_list);
-
-                                        text_draw(in_game_timer->text);
-
-                                        sprite_set_pos(sp_crosshair, *mouse_x, *mouse_y);
-                                        sprite_draw(sp_crosshair);
-                                        graph_draw();
-                                        break;
+                                        list_node_t *it = list_begin(shooter_list);
+                                        while (it != list_end(shooter_list)) {
+                                            gunner_t *p = *(gunner_t**)list_node_val(it);
+                                            get_random_spawn(map1, p);
+                                            gunner_set_curr_health(p, gunner_get_health(p));
+                                            it = list_node_next(it);
+                                        }
+                                        timer_reset(in_game_timer);
                                     }
                                     break;
-                                case KBC_IRQ:
+                                }
+                                break;
+                            case MOUSE_IRQ:
+                                if (counter_mouse_ih >= 3) {
+                                    mouse_parse_packet(packet_mouse_ih, &pp);
+                                    update_mouse(&pp);
                                     switch (game_state) {
                                     case MENU:
-                                        if ((scancode[0]) == ESC_BREAK_CODE) game_state = EXIT;
+                                        if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
                                         break;
                                     case GAME:
-                                        if ((scancode[0]) == ESC_BREAK_CODE) {
-                                            game_state = MENU;
-                                            // reset game
-                                            while(list_size(bullet_list) > 0){
-                                                bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
-                                                bullet_dtor(p);
-                                            }
-                                            list_node_t *it = list_begin(shooter_list);
-                                            while (it != list_end(shooter_list)) {
-                                                gunner_t *p = *(gunner_t**)list_node_val(it);
-                                                get_random_spawn(map1, p);
-                                                gunner_set_curr_health(p, gunner_get_health(p));
-                                                it = list_node_next(it);
-                                            }
-                                            timer_reset(in_game_timer);
-                                        }
+                                        if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
+                                            shoot_bullet(shooter1, bullet_list, bsp_bullet);
                                         break;
                                     }
-                                    break;
-                                case MOUSE_IRQ:
-                                    if (counter_mouse_ih >= 3) {
-                                        mouse_parse_packet(packet_mouse_ih, &pp);
-                                        update_mouse(&pp);
-                                        switch (game_state) {
-                                        case MENU:
-                                            if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
-                                            break;
-                                        case GAME:
-                                            if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
-                                                shoot_bullet(shooter1, bullet_list, bsp_bullet);
-                                            break;
-                                        }
-                                        last_lb = keys->lb_pressed;
-                                        counter_mouse_ih = 0;
+                                    last_lb = keys->lb_pressed;
+                                    counter_mouse_ih = 0;
 
-                                    }
-                                    break;
                                 }
+                                break;
+                            #endif
+                            #ifdef DIOGO
+                            case COM1_IRQ:
+                                printf("You've got mail.\n");
+                                break;
+                            #endif
                             }
                         }
+                    }
 
-                        break;
-                    default:
-                        break; /* no other notifications expected: do nothing */
-                }
-            } else { /* received standart message, not a notification */
-                /* no standart message expected: do nothing */
+                    break;
+                default:
+                    break; /* no other notifications expected: do nothing */
             }
+        } else { /* received standart message, not a notification */
+            /* no standart message expected: do nothing */
         }
+    }
 
-        while(list_size(shooter_list) > 0){
-            gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
-            gunner_dtor(p);
-        }
+    while(list_size(shooter_list) > 0){
+        gunner_t *p = list_erase(shooter_list, list_begin(shooter_list));
+        gunner_dtor(p);
+    }
 
-        while(list_size(bullet_list) > 0){
-            bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
-            bullet_dtor(p);
-        }
-        if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
-        if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
+    while(list_size(bullet_list) > 0){
+        bullet_t *p = (bullet_t*)list_erase(bullet_list, list_begin(bullet_list));
+        bullet_dtor(p);
+    }
+    if(list_dtor(shooter_list)) printf("COULD NOT DESTRUCT SHOOTER LIST\n");
+    if(list_dtor(bullet_list)) printf("COULD NOT DESTRUCT BULLET LIST\n");
 
-        timer_dtor(in_game_timer); in_game_timer = NULL;
+    timer_dtor(in_game_timer); in_game_timer = NULL;
 
-        basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
-        basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
-        sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
-        basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
-        basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
-        map_dtor               (map1         ); map1          = NULL;
-        font_dtor              (consolas     ); consolas      = NULL;
+    basic_sprite_dtor      (bsp_crosshair); bsp_crosshair = NULL;
+    basic_sprite_dtor      (bsp_shooter  ); bsp_shooter   = NULL;
+    sprite_dtor            (sp_crosshair ); sp_crosshair  = NULL;
+    basic_sprite_dtor      (bsp_pistol   ); bsp_pistol    = NULL;
+    basic_sprite_dtor      (bsp_nothing  ); bsp_nothing   = NULL;
+    map_dtor               (map1         ); map1          = NULL;
+    font_dtor              (consolas     ); consolas      = NULL;
 
-        // Unsubscribe interrupts
-        if (unsubscribe_all()) {
-            if (cleanup())
-                printf("%s: failed to cleanup.\n", __func__);
-            return 1;
-        }
+    // Unsubscribe interrupts
+    if (unsubscribe_all()) {
+        if (cleanup())
+            printf("%s: failed to cleanup.\n", __func__);
+        return 1;
+    }
 
 
-        if (cleanup()) {
-            printf("%s: failed to cleanup.\n", __func__);
-            return 1;
-        }
-    #endif
+    if (cleanup()) {
+        printf("%s: failed to cleanup.\n", __func__);
+        return 1;
+    }
 
-
     return 0;
 }
