Index: proj.c
===================================================================
--- proj.c	(revision 294)
+++ proj.c	(revision 295)
@@ -98,32 +98,10 @@
         sp_crosshair = sprite_ctor(bsp_crosshair); if(sp_crosshair == NULL) printf("Failed to get crosshair sprite\n");
     }
 
-    ent_set_scale(DEFAULT_SCALE);
-
-    text_timer_t *in_game_timer = timer_ctor(consolas);
-
     #ifndef DIOGO
         menu_t *main_menu = menu_ctor(consolas);
     #endif
 
-    list_t *shooter_list = list_ctor();
-
-    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);
-
-    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
-    gunner_set_spawn(shooter2, 975, 75);
-    gunner_set_pos(shooter2, 775, 75);
-
-    list_insert(shooter_list, list_end(shooter_list), shooter1);
-    list_insert(shooter_list, list_end(shooter_list), shooter2);
-
-    list_t *bullet_list  = list_ctor();
-
-    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
-                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
-
     #ifndef DIOGO
         //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
         uint8_t last_lb = 0;
@@ -133,7 +111,6 @@
     /// loop stuff
     int ipc_status;
     message msg;
-    int game_state = MENU;
 
     #ifdef DIOGO
         char buffer[1024]; // buffer
@@ -147,7 +124,9 @@
         char *s = NULL;
     #endif
 
-    while (game_state != EXIT) {
+    int good = true;
+
+    while (good) {
         /* Get a request message. */
         if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
             printf("driver_receive failed with %d", r);
@@ -162,107 +141,49 @@
                             switch (i) {
                             #ifndef DIOGO
                             case TIMER0_IRQ:
-                                switch (game_state) {
-                                case MENU:
-                                    graph_clear_screen();
-                                    switch(menu_update_state(main_menu, click)){
-                                        case -1: game_state = MENU; break;
-                                        case  0: game_state = GAME; game(); break;
-                                        case  1: game_state = TEST; break;
-                                        case  2: game_state = EXIT; break;
-                                    }
-                                    menu_draw(main_menu);
 
-                                    click = 0;
+                                graph_clear_screen();
+                                switch(menu_update_state(main_menu, click)){
+                                    case -1: break;
+                                    case  0: game(); break;
+                                    case  1: good = false; break;
+                                    case  2: good = false; break;
+                                }
+                                menu_draw(main_menu);
 
-                                    sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_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);
+                                click = 0;
 
-                                    update_game_state(map1, shooter_list, bullet_list);
+                                sprite_set_pos(sp_crosshair, *get_mouse_X(), *get_mouse_Y());
+                                sprite_draw(sp_crosshair);
+                                graph_draw();
 
-                                    //update_scale();
-                                    double 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, *get_mouse_X(), *get_mouse_Y());
-                                    sprite_draw(sp_crosshair);
-                                    graph_draw();
-                                    break;
-                                }
                                 break;
                             #endif
                             case KBC_IRQ:
-                                switch (game_state) {
-                                case MENU:
-                                    if ((scancode[0]) == ESC_BREAK_CODE) game_state = EXIT;
-                                    #ifdef DIOGO
-                                    else if ((scancode[0]) == ENTER_MAKE_CODE) {
-                                        buffer[buffer_pos] = '\0';
-                                        printf("\nSending string -%s-", buffer);
-                                        printf(" (output: %d)\n",
-                                            hltp_send_string(buffer));
-                                        buffer_pos = 0;
-                                    }
-                                    else {
-                                        char c = map_makecode(scancode[0]);
-                                        if (c == ERROR_CODE) break;
-                                        buffer[buffer_pos++] = c;
-                                        printf("%c", c);
-                                    }
-                                    #endif
-                                    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);
-                                    }
-                                    break;
+                                if ((scancode[0]) == ESC_BREAK_CODE) good = false;
+                                #ifdef DIOGO
+                                else if ((scancode[0]) == ENTER_MAKE_CODE) {
+                                    buffer[buffer_pos] = '\0';
+                                    printf("\nSending string -%s-", buffer);
+                                    printf(" (output: %d)\n",
+                                        hltp_send_string(buffer));
+                                    buffer_pos = 0;
                                 }
-                                break;
+                                else {
+                                    char c = map_makecode(scancode[0]);
+                                    if (c == ERROR_CODE) break;
+                                    buffer[buffer_pos++] = c;
+                                    printf("%c", c);
+                                }
+                                #endif
                             #ifndef DIOGO
                             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;
-                                    }
+                                    if (!click) click = last_lb ^ keys->lb_pressed && keys->lb_pressed;
                                     last_lb = keys->lb_pressed;
                                     counter_mouse_ih = 0;
-
                                 }
                                 break;
                             #endif
@@ -284,24 +205,6 @@
         }
     }
 
-    #ifdef DIOGO
-        free(s);
-    #endif
-
-    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");
-
-    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;
@@ -327,5 +230,137 @@
 }
 
 static int (game)(void){
+
+    int r;
+
+    ent_set_scale(DEFAULT_SCALE);
+    text_timer_t *in_game_timer = timer_ctor(consolas);
+
+    list_t *shooter_list = list_ctor();
+
+    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);
+
+    gunner_t *shooter2 = gunner_ctor(bsp_shooter, bsp_nothing);
+    gunner_set_spawn(shooter2, 975, 75);
+    gunner_set_pos(shooter2, 775, 75);
+
+    list_insert(shooter_list, list_end(shooter_list), shooter1);
+    list_insert(shooter_list, list_end(shooter_list), shooter2);
+
+    list_t *bullet_list  = list_ctor();
+
+    ent_set_origin(gunner_get_x(shooter1)-ent_get_XLength()/2.0,
+                   gunner_get_y(shooter1)-ent_get_YLength()/2.0);
+
+   //uint32_t refresh_count_value = sys_hz() / REFRESH_RATE;
+   uint8_t last_lb = 0;
+   struct packet pp;
+   keys_t *keys = get_key_presses();
+
+    /// loop stuff
+    int ipc_status;
+    message msg;
+    int game_state = GAME;
+
+    while (game_state == GAME) {
+       /* 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:
+
+                               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();
+                               double 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, *get_mouse_X(), *get_mouse_Y());
+                               sprite_draw(sp_crosshair);
+                               graph_draw();
+
+                               break;
+                           case KBC_IRQ:
+                               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);
+                               }
+                               break;
+                           case MOUSE_IRQ:
+                               if (counter_mouse_ih >= 3) {
+                                   mouse_parse_packet(packet_mouse_ih, &pp);
+                                   update_mouse(&pp);
+                                   if (last_lb ^ keys->lb_pressed && keys->lb_pressed)
+                                       shoot_bullet(shooter1, bullet_list, bsp_bullet);
+                                   last_lb = keys->lb_pressed;
+                                   counter_mouse_ih = 0;
+
+                               }
+                               break;
+                           }
+                       }
+                   }
+                   break;
+               default:
+                   break; /* no other notifications expected: do nothing */
+           }
+       } else { /* received standart message, not a notification */
+           /* no standart message expected: do nothing */
+       }
+    }
+
+    #ifdef DIOGO
+        free(s);
+    #endif
+
+    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");
+
+    timer_dtor(in_game_timer); in_game_timer = NULL;
+
     return SUCCESS;
 }
