void game() { /* MAIN LOOP, DO NOT BREAK */ while (1) { /* GETKEY */ keyupdate(); // handle [menu] if (PRGM_GetKey()==48) { GetKey(&key); } // direction keys if (keydownlast(KEY_PRGM_LEFT) && cursor_pos[0]>33) { cursor_pos[0] -= cursor_speed; } else if (keydownlast(KEY_PRGM_RIGHT) && cursor_pos[0]<LCD_WIDTH_PX-33) { cursor_pos[0] += cursor_speed; } if (keydownlast(KEY_PRGM_UP) && cursor_pos[1]>0) { cursor_pos[1] -= cursor_speed; } else if (keydownlast(KEY_PRGM_DOWN) && cursor_pos[1]<(LCD_HEIGHT_PX-dash_height-cart_height-man_height-10)) { cursor_pos[1] += cursor_speed; } // control keys // shift if (keydownlast(KEY_PRGM_SHIFT) && man_is_hanging) { // drop man man_is_hanging = false; man_pos[0] = copter_pos[0]; man_pos[1] = copter_pos[1]+9; } /* OPERATIONS */ // move copter if (cursor_pos[0] > copter_pos[0]) { copter_pos[0]+=copter_speed; } else if (cursor_pos[0] < copter_pos[0]) { copter_pos[0]-=copter_speed; } if (cursor_pos[1] > copter_pos[1]) { copter_pos[1]+=copter_speed; } else if (cursor_pos[1] < copter_pos[1]) { copter_pos[1]-=copter_speed; } // if man is falling, drop him if (!man_is_hanging) { man_pos[1] += man_speed; } // if man is below cart level, check if he is in the cart if (!man_is_hanging && man_pos[1]>(LCD_HEIGHT_PX-dash_height-cart_height-5)) { if (!man_is_dead && man_pos[0]>cart_pos && man_pos[0]<cart_pos+35) { next_try(); } else { die_animation(man_pos[0], man_pos[1]); } } // move cart cart_counter = (cart_counter+1) % LCD_WIDTH_PX; cart_pos = cart_counter-70; // update frame cart_frame = (cart_frame+1) % 2; /* GRAPHICS */ // clear screen Bdisp_AllClr_VRAM(); // display functions draw_copter(copter_pos[0], copter_pos[1], man_is_hanging); draw_cart(cart_pos, LCD_HEIGHT_PX-dash_height-cart_height, cart_frame); // if man is falling, draw him if (!man_is_hanging /* && !man_is_dead */) { draw_man(man_pos[0], man_pos[1]); } // draw dash draw_dash(); // draw cursor on top of everything draw_cursor(cursor_pos[0], cursor_pos[1]); // copy VRAM to screen Bdisp_PutDisp_DD(); } }
// The main Allegro loop, all input handling, animation and drawing is done here _Bool main_loop(void) { // Flag for drawing _Bool needredraw = true; // Declare primitive data types float old_time = 0.0, current_time = 0, dt = 0; while(!data->exit) { if(needredraw && al_event_queue_is_empty(data->queue) && al_event_queue_is_empty(data->queue2)) { // Clear, draw, flip al_clear_to_color(data->background_color); render(); if(get_fps_status()) al_draw_textf(data->font, data->text_color, 0, res_height-30, 0, "fps: %.2f", 1/dt); al_flip_display(); needredraw = false; } // Block until an event enters the queue al_wait_for_event(data->queue, &(data->event)); while(!al_event_queue_is_empty(data->queue2)) { al_get_next_event(data->queue2, &(data->event2)); switch(data->event2.type) { case ALLEGRO_EVENT_DISPLAY_CLOSE: { // If x button is pressed on window data->exit = true; data->gamestarted = false; } break; case ALLEGRO_EVENT_DISPLAY_RESIZE: { al_acknowledge_resize(data->event2.display.source); scale(res_width, res_height); } break; case ALLEGRO_EVENT_MOUSE_AXES: { // Stores the mouse's new position and change in position mouseaxes(&(data->event2.mouse)); } case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: { // Stores the mouse button pressed mousedown(&(data->event2.mouse)); } break; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: { // Stores the mouse button released mouseup(&(data->event2.mouse)); } break; case ALLEGRO_EVENT_KEY_DOWN: { // Stores keydown keycode into keycode array for processing keydown(&(data->event2.keyboard)); } break; case ALLEGRO_EVENT_KEY_UP: { // Stores keycode into keycode array for processing keyup(&(data->event2.keyboard)); } break; default: break; } } switch (data->event.type) { case ALLEGRO_EVENT_TIMER: { // Determine the change in time between frames, in seconds current_time = al_current_time(); dt = current_time-old_time; // If the computer lags for some reason, don't penalize the player // Cap dt at 0.5 seconds if(dt > 0.25) { dt = 0.25; } // Handle Mouse and Keyboard events and Button Events buttoncheck(&buttonhandler, data); keycheck(&keyhandler, data); mousecheck(&mousehandler, data); keyupdate(); mouseupdate(); // Check if data->quit has been set before updating and drawing if(data->exit) break; // Update the game, always update(); // Skip drawing frames if computer lags if(current_time - data->event.timer.timestamp <= 1.0/al_get_display_refresh_rate(data->display)) { needredraw = true; } // Make the time at this frame the old time, for the next frame old_time = current_time; } break; default: break; } } return true; }