int main(void) { ALLEGRO_DISPLAY *display; ALLEGRO_EVENT_QUEUE *events; ALLEGRO_EVENT event; double last_resize; int rs = 100; /* Initialize Allegro and create an event queue. */ if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } events = al_create_event_queue(); /* Setup a display driver and register events from it. */ al_set_new_display_flags(ALLEGRO_RESIZABLE); display = al_create_display(rs, rs); al_register_event_source(events, al_get_display_event_source(display)); /* Setup a keyboard driver and regsiter events from it. */ al_install_keyboard(); al_register_event_source(events, al_get_keyboard_event_source()); /* Display a pulsating window until a key or the closebutton is pressed. */ redraw(); last_resize = 0; while (true) { if (al_get_next_event(events, &event)) { if (event.type == ALLEGRO_EVENT_DISPLAY_RESIZE) { ALLEGRO_DISPLAY_EVENT *de = &event.display; al_acknowledge_resize(de->source); redraw(); } if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } if (event.type == ALLEGRO_EVENT_KEY_DOWN) { break; } } if (al_current_time() - last_resize > 0.1) { int s; last_resize = al_current_time(); rs += 10; if (rs == 300) rs = 100; s = rs; if (s > 200) s = 400 - s; al_resize_display(s, s); } } return 0; }
Client::~Client() { ZCom_disconnectAll(NULL); printf("Disconnecting Client\n"); double start_time = al_current_time(); while(al_current_time()-start_time < 1) { ZCom_processInput(); ZCom_processOutput(); } }
/* Run the FPS test. */ static void run(void) { ALLEGRO_EVENT event; int frames = 0; double start; target = al_create_bitmap(W, H); al_set_target_bitmap(target); al_clear_to_color(al_map_rgba_f(1, 1, 0, 1)); al_set_target_bitmap(al_get_backbuffer()); dx = 81; dy = 63; start = al_current_time(); while (true) { /* Check for ESC key or close button event and quit in either case. */ if (!al_event_queue_is_empty(queue)) { while (al_get_next_event(queue, &event)) { switch (event.type) { case ALLEGRO_EVENT_DISPLAY_CLOSE: quit = true; goto done; case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) quit = true; goto done; if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) goto done; break; } } } draw(); print(0, 0, "FPS: %.1f", frames / (al_current_time() - start)); if (al_get_new_bitmap_flags() & ALLEGRO_FORCE_LOCKING) { print(0, al_get_font_line_height(myfont), "using forced bitmap locking"); } else { print(0, al_get_font_line_height(myfont), "drawing directly to bitmap"); } print(0, al_get_font_line_height(myfont) * 2, "Press SPACE to toggle drawing method."); al_flip_display(); frames++; } done: al_destroy_bitmap(target); }
bool _al_win_toggle_display_flag(ALLEGRO_DISPLAY *display, int flag, bool onoff) { ALLEGRO_DISPLAY_WIN *win_display = (void*)display; double timeout; switch(flag) { case ALLEGRO_NOFRAME: _al_win_toggle_window_frame(display, win_display->window, display->w, display->h, onoff); return true; case ALLEGRO_FULLSCREEN_WINDOW: if (onoff == ((display->flags & ALLEGRO_FULLSCREEN_WINDOW) != 0)) return true; _al_win_toggle_display_flag(display, ALLEGRO_NOFRAME, !onoff); if (onoff) { ALLEGRO_MONITOR_INFO mi; int adapter = al_get_current_video_adapter(); if (adapter == -1) adapter = 0; al_get_monitor_info(adapter, &mi); display->flags |= ALLEGRO_FULLSCREEN_WINDOW; display->w = mi.x2 - mi.x1; display->h = mi.y2 - mi.y1; } else { display->flags &= ~ALLEGRO_FULLSCREEN_WINDOW; display->w = win_display->toggle_w; display->h = win_display->toggle_h; } al_resize_display(display->w, display->h); timeout = al_current_time() + 3; // 3 seconds... while (al_current_time() < timeout) { if (win_display->can_acknowledge) { al_acknowledge_resize(display); break; } } if (onoff) { al_set_window_position(display, 0, 0); // Pop it to the front // FIXME: HOW?! } /* FIXME: else center the window? */ return true; } return false; }
void c_monster_character::next_instruction() { if (this->path_length == 0) return; this->current_path_instruction = (this->current_path_instruction + 1) % this->path_length; switch(this->path_directions[this->current_path_instruction]) { case DIRECTION_NORTH: this->goes_to = c_character::square_to_position(this->get_square_y() - this->path_steps[this->current_path_instruction],false); break; case DIRECTION_EAST: this->goes_to = c_character::square_to_position(this->get_square_x() + this->path_steps[this->current_path_instruction],true); break; case DIRECTION_SOUTH: this->goes_to = c_character::square_to_position(this->get_square_y() + this->path_steps[this->current_path_instruction],false); break; case DIRECTION_WEST: this->goes_to = c_character::square_to_position(this->get_square_x() - this->path_steps[this->current_path_instruction],true); break; case DIRECTION_NONE: this->waiting = true; this->waiting_end = al_current_time() + 1.0 * this->path_steps[this->current_path_instruction]; break; } }
void CTetrisController::HandleEvent(const ALLEGRO_EVENT& ev) { if (ev.type == ALLEGRO_EVENT_KEY_DOWN || ev.type == ALLEGRO_EVENT_KEY_CHAR) { double curTime = al_current_time(); if ((curTime - m_lastTimeKeyPressed) > 1.0f / 100.0f) { m_lastTimeKeyPressed = curTime; if (ev.keyboard.keycode == ALLEGRO_KEY_A) m_board->MoveLeft(); else if (ev.keyboard.keycode == ALLEGRO_KEY_D) m_board->MoveRight(); else if (ev.keyboard.keycode == ALLEGRO_KEY_S) m_board->ForceGravity(true); else if (ev.keyboard.keycode == ALLEGRO_KEY_R) m_board->RotateRight(); else if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_SPACE) m_board->HardDrop(); else if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_N) m_board->StartNewGame(); else if (ev.type == ALLEGRO_EVENT_KEY_DOWN && ev.keyboard.keycode == ALLEGRO_KEY_P) { if (m_board->GetGameState() == CTetris::GamePaused) m_board->UnpauseGame(); else if (m_board->GetGameState() == CTetris::GameRunning) m_board->PauseGame(); } } } if (ev.type == ALLEGRO_EVENT_KEY_UP) { double curTime = al_current_time(); { m_lastTimeKeyPressed = curTime; if (ev.keyboard.keycode == ALLEGRO_KEY_S) m_board->ForceGravity(false); } } }
void addOmnipotentText(std::string text, int cx, int cy, MCOLOR color) { current_time = al_current_time(); for (int i = 0; i < MAX_PARTY; i++) { if (omnipotentTexts[i].isDisplayed()) continue; omnipotentTexts[i].start(text, cx, cy, color); break; } }
/* Called a fixed amount of times per second. */ static void tick(void) { /* Count frames during the last second or so. */ double t = al_current_time(); if (t >= ex.last_second + 1) { ex.fps = ex.frames_accum / (t - ex.last_second); ex.frames_accum = 0; ex.last_second = t; } draw(); al_flip_display(); ex.frames_accum++; }
void CEngine::OnUpdate(void) { const double ct = al_current_time(); const double dt = ct - m_timeLastUpdated; m_timeLastUpdated = ct; if (GetGameBoard()) GetGameBoard()->Update(dt); for (auto iter = m_gameObjects.begin(); iter != m_gameObjects.end(); ++iter) (*iter)->Update(dt); // Game objects have been updated, therefore // they will need to be redrawn. m_needRedraw = true; }
void GameEngine::run() { al_start_timer(m_RedrawTimer); m_GameActive = true; m_LastSecondTime = al_current_time(); bool redrawScene = true; DEBUG_WINDOW("DEBUG MAIN", 300, 180); while(m_GameActive) { ALLEGRO_EVENT event; al_wait_for_event(m_EventQueue, &event); switch(event.type) { case ALLEGRO_EVENT_DISPLAY_CLOSE: m_GameActive = false; break; case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) m_GameActive = false; break; case ALLEGRO_EVENT_TIMER: redrawScene = true; break; } if (redrawScene && al_is_event_queue_empty(m_EventQueue)) { GameStateInterface* currentState = getCurrentState(); currentState->update(this); currentState->draw(this); al_flip_display(); calculateFrameRate(); redrawScene = false; DEBUG_REFRESH_ALL(); } } }
void GameEngine::calculateFrameRate() { m_FrameCount += 1; m_FrameCountThisSecond += 1; double currentTime = al_current_time(); if (currentTime - m_LastSecondTime >= 1.0) { m_LastFrameRate = m_FrameCountThisSecond / (currentTime - m_LastSecondTime); m_LastSecondTime = currentTime; m_FrameCountThisSecond = 0; DEBUG_SHOW("DEBUG MAIN", "Frame Rate", std::to_string(m_LastFrameRate)); DEBUG_SHOW("DEBUG MAIN", "Frame Total", std::to_string(m_FrameCount)); } }
/* ljoy_generate_axis_event: [fdwatch thread] * * Helper to generate an event after an axis is moved. * The joystick must be locked BEFORE entering this function. */ static void ljoy_generate_axis_event(ALLEGRO_JOYSTICK_LINUX *joy, int stick, int axis, float pos) { ALLEGRO_EVENT event; if (!_al_event_source_needs_to_generate_event(&joy->parent.es)) return; event.joystick.type = ALLEGRO_EVENT_JOYSTICK_AXIS; event.joystick.timestamp = al_current_time(); event.joystick.stick = stick; event.joystick.axis = axis; event.joystick.pos = pos; event.joystick.button = 0; _al_event_source_emit_event(&joy->parent.es, &event); }
/* ljoy_generate_button_event: [fdwatch thread] * * Helper to generate an event after a button is pressed or released. * The joystick must be locked BEFORE entering this function. */ static void ljoy_generate_button_event(ALLEGRO_JOYSTICK_LINUX *joy, int button, ALLEGRO_EVENT_TYPE event_type) { ALLEGRO_EVENT event; if (!_al_event_source_needs_to_generate_event(&joy->parent.es)) return; event.joystick.type = event_type; event.joystick.timestamp = al_current_time(); event.joystick.stick = 0; event.joystick.axis = 0; event.joystick.pos = 0.0; event.joystick.button = button; _al_event_source_emit_event(&joy->parent.es, &event); }
Game::Game (CharacterID _character) : paused(false), n_frame(0), d_frame(0) { gamedata.character = _character; gamedata.cur_pos = Position (); for (int i = 0; i < FPS; i ++) global.time_previous[i] = al_current_time () - 1.0 + (i + 1) / (double) FPS; gamedata.n_pbullet = 0; gamedata.n_ebullet = 0; gamedata.n_enemy = 0; gamedata.power = 0; gamedata.hp = HP_PER_LIFE * 3; gamedata.graze = 0; gamedata.fl_shooting = false; gamedata.fl_invincible = true; gamedata.invincible_count = INVINCIBLE_TIME; }
/* Draw our example scene. */ static void draw(void) { float xs, ys, a; double dt = 0; double t = al_current_time(); if (last_time > 0) { dt = t - last_time; } last_time = t; al_set_target_bitmap(target); al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgba_f(1, 1, 1, 1)); al_draw_filled_rectangle(x, y, x + RW, y + RH, al_map_rgba_f(1, 0, 0, 1)); al_draw_filled_rectangle(0, 0, W, H, al_map_rgba_f(1, 1, 0, 0.1)); x += dx * dt; if (x < 0) { x = 0; dx = -dx; } if (x + RW > W) { x = W - RW; dx = -dx; } y += dy * dt; if (y < 0) { y = 0; dy = -dy; } if (y + RH > H) { y = H - RH; dy = -dy; } al_set_target_bitmap(al_get_backbuffer()); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_ZERO, al_map_rgba_f(1, 1, 1, 1)); al_clear_to_color(al_map_rgba_f(0, 0, 1, 1)); xs = 1 + 0.2 * sin(t * ALLEGRO_PI * 2); ys = 1 + 0.2 * sin(t * ALLEGRO_PI * 2); a = t * ALLEGRO_PI * 2 / 3; al_draw_rotated_scaled_bitmap(target, W / 2, H / 2, 320, 240, xs, ys, a, 0); }
bool Whack_a_Skunk_Loop::logic(void) { engine->set_touch_input_type(TOUCHINPUT_GUI); for (int i = 0; i < 9; i++) { if (holes[i].miss_count >= 0) { holes[i].miss_count -= General::LOGIC_MILLIS; } } if (done) { done_count += General::LOGIC_MILLIS; if (done_count > 2000) { /* Stop automatic comeback to this game */ Area_Loop *a = General::find_in_vector<Area_Loop *, Loop *>(engine->get_loops()); if (a) { std::vector<Player *> players = a->get_players(); for (size_t i = 0; i < players.size(); i++) { players[i]->reset(); } } if (hits < 0) { hits = 0; } area_loop->set_whack_a_skunk_played(true); area_loop->set_whack_a_skunk_score(hits); engine->set_loops(std::vector<Loop *>(), true); } return false; } double now = al_current_time(); timer = (now - start_time) * 1000; for (int i = 0; i < 9; i++) { if (holes[i].status == ALIVE || holes[i].status == GOING_DOWN) { int old_count = holes[i].count; holes[i].count += General::LOGIC_MILLIS; int len = regular_skunk->get_length("popup"); if (old_count < len/2 && holes[i].count >= len/2) { if (holes[i].type != FAKE && General::rand() % 3 == 0) { Sound::play(taunt_sample); holes[i].status = TAUNTING; holes[i].taunting_count = 0; } } } else if (holes[i].status == DYING) { holes[i].dying_count += General::LOGIC_MILLIS; if (holes[i].dying_count >= regular_skunk->get_length("hit")) { holes[i].status = GOING_DOWN; } } else if (holes[i].status == TAUNTING) { holes[i].taunting_count += General::LOGIC_MILLIS; if (holes[i].taunting_count >= regular_skunk->get_length("taunt")) { holes[i].status = GOING_DOWN; } } } std::list<Star>::iterator star_it; for (star_it = stars.begin(); star_it != stars.end();) { Star &s = *star_it; s.x += s.dx; s.y += s.dy; s.angle += s.da; s.count += General::LOGIC_MILLIS; if (s.count > STAR_LIFETIME) star_it = stars.erase(star_it); else star_it++; } std::list<Pow>::iterator pow_it; for (pow_it = pows.begin(); pow_it != pows.end();) { Pow &p = *pow_it; p.count += General::LOGIC_MILLIS; if (p.count > POW_LIFETIME) pow_it = pows.erase(pow_it); else pow_it++; } next_count += General::LOGIC_MILLIS; if (next_count > min_next) { bool go = ((int)(((float)(General::rand()%1000)/1000 * next_diff)) < General::LOGIC_MILLIS) || next_count >= max_next; if (go) { std::vector<int> available; for (int i = 0; i < 9; i++) { if (holes[i].status == NIL) { available.push_back(i); } } if (available.size() > 0) { int i = available[General::rand()%available.size()]; holes[i].status = ALIVE; holes[i].count = 0; next_count = 0; int r = General::rand() % 100; if (r < 10) { holes[i].type = FAKE; } else if (r < 25) { holes[i].type = GOLD; } else if (r < 40) { holes[i].type = SILVER; } else { holes[i].type = REGULAR; } } } } if (timer >= GAME_LENGTH*1000) { done = true; } if (bashing) { hand->update(); if (hand->get_current_animation()->is_finished()) { bashing = false; hand->reset(); #if defined ALLEGRO_IPHONE || defined ALLEGRO_ANDROID curr_hole = -1; #endif } } return false; }
int main() { al_init(); al_install_mouse(); al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); ALLEGRO_DISPLAY *display; al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_OPENGL); al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 24, ALLEGRO_REQUIRE); display = al_create_display(800, 600); if(!display) { std::cout<<"Failed to create display"<<std::endl; return 0; } ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); al_register_event_source(event_queue, (ALLEGRO_EVENT_SOURCE *)display); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_mouse_event_source()); if(!Init()) return 0; double last_time = al_current_time(); bool quit = false; while(1) { ALLEGRO_EVENT event; while (al_get_next_event(event_queue, &event)) { if (ALLEGRO_EVENT_KEY_DOWN == event.type) { if (ALLEGRO_KEY_ESCAPE == event.keyboard.keycode) { quit = true; } } if (ALLEGRO_EVENT_DISPLAY_CLOSE == event.type) { quit = true; } Event(event); } if (quit) break; double current_time = al_current_time(); double dt = current_time - last_time; last_time = current_time; Update(dt); al_clear_to_color(al_map_rgb(0, 0, 0)); Render(); al_flip_display(); al_rest(0.001); } al_destroy_event_queue(event_queue); al_destroy_display(display); return 0; }
// 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; }
int main(int argc, char **argv) { bool done = false; bool render = false; float gameTime = 0; int frames = 0; int gameFPS = 0; float evTimer = 0; tractor = new Tractor(); Xml = new xml(); int state = -1; ALLEGRO_BITMAP *icon; ALLEGRO_BITMAP *map = NULL; ALLEGRO_BITMAP *panel = NULL; ALLEGRO_BITMAP *tractorImage = NULL; ALLEGRO_BITMAP *titleImage = NULL; ALLEGRO_BITMAP *lostImage = NULL; ALLEGRO_SAMPLE *titleSong = NULL; ALLEGRO_SAMPLE *gameSong = NULL; ALLEGRO_SAMPLE *lostSong = NULL; ALLEGRO_SAMPLE *cash = NULL; ALLEGRO_BITMAP *L1 = NULL; ALLEGRO_BITMAP *L2 = NULL; ALLEGRO_BITMAP *L3 = NULL; ALLEGRO_BITMAP *L4 = NULL; ALLEGRO_BITMAP *L5 = NULL; ALLEGRO_BITMAP *L6 = NULL; ALLEGRO_BITMAP *L7 = NULL; ALLEGRO_DISPLAY *display = NULL; ALLEGRO_DISPLAY_MODE disp_data; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer; ALLEGRO_FONT *font; ALLEGRO_FONT *score; if (!al_init()) return -1; al_install_keyboard(); al_install_mouse(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); al_init_primitives_addon(); al_install_audio(); al_init_acodec_addon(); al_get_display_mode(al_get_num_display_modes() - 1, &disp_data); //al_set_new_display_flags(ALLEGRO_FULLSCREEN); al_set_new_display_option(ALLEGRO_VSYNC, 1, ALLEGRO_REQUIRE); display = al_create_display(disp_data.width, disp_data.height); icon = al_load_bitmap("icon.png"); al_set_display_icon(display, icon); float sx = (float)disp_data.width / WIDTH; float sy = (float)disp_data.height / HEIGHT; ALLEGRO_TRANSFORM trans; al_identity_transform(&trans); al_scale_transform(&trans, sx, sy); al_use_transform(&trans); if (!display) return -1; font = al_load_font("arial.ttf", 20, 0); score = al_load_font("score.ttf", 45, 0); al_reserve_samples(15); map = al_load_bitmap("map2.png"); panel = al_load_bitmap("panel.png"); L1 = al_load_bitmap("l1.png"); L2 = al_load_bitmap("l2.png"); L3 = al_load_bitmap("l3.png"); L4 = al_load_bitmap("l4.png"); L5 = al_load_bitmap("l5.png"); L6 = al_load_bitmap("l6.png"); L7 = al_load_bitmap("l7.png"); Background *Map = new Background(map); objects.push_back(Map); TextBox *Task = new TextBox; Field *field1 = new Field(L1, L2, L3, L4, L5, L6, L7, 50, 50); objects.push_back(field1); Field *field2 = new Field(L1, L2, L3, L4, L5, L6, L7, 450, 50); objects.push_back(field2); Field *field3 = new Field(L1, L2, L3, L4, L5, L6, L7, 50, 450); objects.push_back(field3); Field *field4 = new Field(L1, L2, L3, L4, L5, L6, L7, 450, 450); objects.push_back(field4); tractorImage = al_load_bitmap("tractor.png"); cash = al_load_sample("cash.ogg"); tractor->Init(tractorImage, cash); objects.push_back(tractor); titleImage = al_load_bitmap("screen_Title.png"); lostImage = al_load_bitmap("screen_Lost.png"); titleScreen = new Background(titleImage); lostScreen = new Background(lostImage); titleSong = al_load_sample("title.ogg"); gameSong = al_load_sample("game.ogg"); lostSong = al_load_sample("lost.ogg"); songInstance = al_create_sample_instance(titleSong); al_set_sample_instance_playmode(songInstance, ALLEGRO_PLAYMODE_LOOP); songInstance2 = al_create_sample_instance(gameSong); al_set_sample_instance_playmode(songInstance2, ALLEGRO_PLAYMODE_LOOP); songInstance3 = al_create_sample_instance(lostSong); al_set_sample_instance_playmode(songInstance3, ALLEGRO_PLAYMODE_LOOP); al_attach_sample_instance_to_mixer(songInstance, al_get_default_mixer()); al_attach_sample_instance_to_mixer(songInstance2, al_get_default_mixer()); al_attach_sample_instance_to_mixer(songInstance3, al_get_default_mixer()); ChangeState(state, TITLE); event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / 60); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_mouse_event_source()); al_start_timer(timer); gameTime = al_current_time(); while (!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_UP: keys[UP] = true; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = true; break; case ALLEGRO_KEY_ENTER: keys[ENTER] = true; if (state == TITLE) ChangeState(state, PLAYING); else if (state == PLAYING && Task->CheckText()) { TextBox *text = new TextBox(); text->SetText(Task->Send()); history.push_back(text); for (iter2 = history.begin(); iter2 != history.end(); iter2++) { if ((*iter2)->GetY() < 400) { delete (*iter2); iter2 = history.erase(iter2); } (*iter2)->UpdateY(); } Xml->interpreter(Task->GetLast(), tractor); TextBox *txtxml = new TextBox(); txtxml->SetText(Xml->wyslij()); history.push_back(txtxml); for (iter2 = history.begin(); iter2 != history.end(); iter2++) { if ((*iter2)->GetY() < 300) { delete (*iter2); iter2 = history.erase(iter2); } (*iter2)->UpdateY(); } } else if (state == LOST) ChangeState(state, PLAYING); break; case ALLEGRO_KEY_TAB: keys[TAB] = true; if (state == PLAYING) { Task->SetStatus(); if (Task->GetStatus()) { TextBox *text = new TextBox(); text->SetText("Konsola zostala wlaczona"); history.push_back(text); } else { TextBox *text = new TextBox(); text->SetText("Konsola zostala wylaczona"); history.push_back(text); } for (iter2 = history.begin(); iter2 != history.end(); iter2++) { if ((*iter2)->GetY() < 300) { delete (*iter2); iter2 = history.erase(iter2); } (*iter2)->UpdateY(); } setTimer(evTimer); } tractor->Sell(); break; case ALLEGRO_KEY_SPACE: keys[SPC] = true; if (state == PLAYING) Task->Add(" "); break; case ALLEGRO_KEY_BACKSPACE: if (state == PLAYING && Task->CheckText()) Task->Backspace(); break; case ALLEGRO_KEY_COMMA: keys[COM] = true; if (state == PLAYING && Task->GetStatus()) Task->Add(","); break; case ALLEGRO_KEY_0: numb[N0] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("0"); break; case ALLEGRO_KEY_1: numb[N1] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("1"); break; case ALLEGRO_KEY_2: numb[N2] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("2"); break; case ALLEGRO_KEY_3: numb[N3] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("3"); break; case ALLEGRO_KEY_4: numb[N4] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("4"); break; case ALLEGRO_KEY_5: numb[N5] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("5"); break; case ALLEGRO_KEY_6: numb[N6] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("6"); break; case ALLEGRO_KEY_7: numb[N7] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("7"); break; case ALLEGRO_KEY_8: numb[N8] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("8"); break; case ALLEGRO_KEY_9: numb[N9] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("9"); break; case ALLEGRO_KEY_A: letters[A] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("a"); break; case ALLEGRO_KEY_B: letters[B] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("b"); break; case ALLEGRO_KEY_C: letters[C] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("c"); break; case ALLEGRO_KEY_D: letters[D] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("d"); break; case ALLEGRO_KEY_E: letters[E] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("e"); break; case ALLEGRO_KEY_F: letters[F] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("f"); break; case ALLEGRO_KEY_G: letters[G] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("g"); break; case ALLEGRO_KEY_H: letters[H] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("h"); break; case ALLEGRO_KEY_I: letters[I] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("i"); break; case ALLEGRO_KEY_J: letters[J] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("j"); break; case ALLEGRO_KEY_K: letters[K] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("k"); break; case ALLEGRO_KEY_L: letters[L] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("l"); break; case ALLEGRO_KEY_M: letters[M] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("m"); break; case ALLEGRO_KEY_N: letters[N] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("n"); break; case ALLEGRO_KEY_O: letters[O] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("o"); break; case ALLEGRO_KEY_P: letters[P] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("p"); break; case ALLEGRO_KEY_Q: letters[Q] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("q"); break; case ALLEGRO_KEY_R: letters[R] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("r"); break; case ALLEGRO_KEY_S: letters[S] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("s"); break; case ALLEGRO_KEY_T: letters[T] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("t"); break; case ALLEGRO_KEY_U: letters[U] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("u"); break; case ALLEGRO_KEY_V: letters[V] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("v"); break; case ALLEGRO_KEY_W: letters[W] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("w"); break; case ALLEGRO_KEY_X: letters[X] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("x"); break; case ALLEGRO_KEY_Y: letters[Y] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("y"); break; case ALLEGRO_KEY_Z: letters[Z] = true; if (state == PLAYING && Task->GetStatus()) Task->Add("z"); break; } } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: done = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_UP: keys[UP] = false; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = false; break; case ALLEGRO_KEY_ENTER: keys[ENTER] = false; break; case ALLEGRO_KEY_TAB: keys[TAB] = false; break; case ALLEGRO_KEY_BACKSPACE: keys[BSPC] = false; break; case ALLEGRO_KEY_COMMA: keys[COM] = false; break; case ALLEGRO_KEY_0: numb[N0] = false; break; case ALLEGRO_KEY_1: numb[N1] = false; break; case ALLEGRO_KEY_2: numb[N2] = false; break; case ALLEGRO_KEY_3: numb[N3] = false; break; case ALLEGRO_KEY_4: numb[N4] = false; break; case ALLEGRO_KEY_5: numb[N5] = false; break; case ALLEGRO_KEY_6: numb[N6] = false; break; case ALLEGRO_KEY_7: numb[N7] = false; break; case ALLEGRO_KEY_8: numb[N8] = false; break; case ALLEGRO_KEY_9: numb[N9] = false; break; case ALLEGRO_KEY_A: letters[A] = false; break; case ALLEGRO_KEY_B: letters[B] = false; break; case ALLEGRO_KEY_C: letters[C] = false; break; case ALLEGRO_KEY_D: letters[D] = false; break; case ALLEGRO_KEY_E: letters[E] = false; break; case ALLEGRO_KEY_F: letters[F] = false; break; case ALLEGRO_KEY_G: letters[G] = false; break; case ALLEGRO_KEY_H: letters[H] = false; break; case ALLEGRO_KEY_I: letters[I] = false; break; case ALLEGRO_KEY_J: letters[J] = false; break; case ALLEGRO_KEY_K: letters[K] = false; break; case ALLEGRO_KEY_L: letters[L] = false; break; case ALLEGRO_KEY_M: letters[M] = false; break; case ALLEGRO_KEY_N: letters[N] = false; break; case ALLEGRO_KEY_O: letters[O] = false; break; case ALLEGRO_KEY_P: letters[P] = false; break; case ALLEGRO_KEY_Q: letters[Q] = false; break; case ALLEGRO_KEY_R: letters[R] = false; break; case ALLEGRO_KEY_S: letters[S] = false; break; case ALLEGRO_KEY_T: letters[T] = false; break; case ALLEGRO_KEY_U: letters[U] = false; break; case ALLEGRO_KEY_V: letters[V] = false; break; case ALLEGRO_KEY_W: letters[W] = false; break; case ALLEGRO_KEY_X: letters[X] = false; break; case ALLEGRO_KEY_Y: letters[Y] = false; break; case ALLEGRO_KEY_Z: letters[Z] = false; break; } } else if (ev.type == ALLEGRO_EVENT_TIMER) { render = true; frames++; if (al_current_time() - gameTime >= 1) { gameTime = al_current_time(); gameFPS = frames; frames = 0; } if (state == PLAYING) { if (keys[UP]) { if (Map->GetY() + Map->frameHeight > disp_data.height) { for (iter = objects.begin(); iter != objects.end(); ++iter) { (*iter)->SetY((*iter)->GetY() - 10); } tractor->SetDistY((tractor->GetDistY() - 10)); } } else if (keys[DOWN]) { if (Map->GetY() < 0) { for (iter = objects.begin(); iter != objects.end(); ++iter) { (*iter)->SetY((*iter)->GetY() + 10); } tractor->SetDistY(tractor->GetDistY() + 10); } } if (keys[LEFT]) { if (Map->GetWidth() > (disp_data.width - al_get_bitmap_width(panel))) { for (iter = objects.begin(); iter != objects.end(); ++iter) { (*iter)->SetX((*iter)->GetX() - 10); } tractor->SetDistX(tractor->GetDistX() - 10); } } else if (keys[RIGHT]) { if (Map->GetX() < 0) { for (iter = objects.begin(); iter != objects.end(); ++iter) { (*iter)->SetX((*iter)->GetX() + 10); } tractor->SetDistX(tractor->GetDistX() + 10); } } for (iter = objects.begin(); iter != objects.end(); ++iter) (*iter)->Update(); if (tractor->GetStatus()) tractor->Move(); field1->Change_Field(); field1->Grow_Field(); field2->Change_Field(); field2->Grow_Field(); field3->Change_Field(); field3->Grow_Field(); field4->Change_Field(); field4->Grow_Field(); field1->Action_On_Field(tractor); field2->Action_On_Field(tractor); field3->Action_On_Field(tractor); field4->Action_On_Field(tractor); if (!tractor->Get_Iminwork()){ Xml->ZKolejki(field1, field2, field3, field4, tractor); if (Xml->wyslij() != ""){ TextBox *txtxml = new TextBox(); txtxml->SetText(Xml->wyslij()); history.push_back(txtxml); for (iter2 = history.begin(); iter2 != history.end(); iter2++) { if ((*iter2)->GetY() < 300) { delete (*iter2); iter2 = history.erase(iter2); } (*iter2)->UpdateY(); } } } if (evTimer < 60) { evTimer += 0.1; } else { if (tractor->GetPodpowiedz() == 0) { Xml->podpowiedz(field1, field2, field3, field4, tractor); evTimer = 0; TextBox *txtxml = new TextBox(); txtxml->SetText(Xml->wyslij()); history.push_back(txtxml); for (iter2 = history.begin(); iter2 != history.end(); iter2++) { if ((*iter2)->GetY() < 300) { delete (*iter2); iter2 = history.erase(iter2); } (*iter2)->UpdateY(); } } } } if (tractor->GetMoney() <= 0) ChangeState(state, LOST); } for (iter = objects.begin(); iter != objects.end();) { if (!(*iter)->GetAlive()) { delete (*iter); iter = objects.erase(iter); } else iter++; } if (render && al_is_event_queue_empty(event_queue)) { render = false; if (state == TITLE) { titleScreen->Render(); } else if (state == PLAYING) { for (iter = objects.begin(); iter != objects.end(); ++iter) (*iter)->Render(); al_draw_bitmap(panel, WIDTH - al_get_bitmap_width(panel), 0, 0); al_draw_textf(font, al_map_rgb(255, 255, 255), Task->GetX(), Task->GetY(), 0, Task->ShowText()); for (iter2 = history.begin(); iter2 != history.end(); iter2++) { al_draw_textf(font, al_map_rgb(255, 255, 255), (*iter2)->GetX(), (*iter2)->GetY(), 0, (*iter2)->ShowText()); } if (tractor->GetHealth() < 20) al_draw_textf(score, RED, WIDTH - 430, 15, 0, "%i", tractor->GetHealth()); else al_draw_textf(score, BLACK, WIDTH - 430, 15, 0, "%i", tractor->GetHealth()); if (tractor->GetFuel() < 20) al_draw_textf(score, RED, WIDTH - 260, 15, 0, "%i", tractor->GetFuel()); else al_draw_textf(score, BLACK, WIDTH - 260, 15, 0, "%i", tractor->GetFuel()); if (tractor->GetMoney() < 200) al_draw_textf(score, RED, WIDTH - 400, 100, 0, "%i", tractor->GetMoney()); else al_draw_textf(score, BLACK, WIDTH - 400, 100, 0, "%i", tractor->GetMoney()); al_draw_textf(score, BLACK, WIDTH - 70, 15, 0, "%i", tractor->GetWater()); for (int j = 0; j < 5; j++) { al_draw_textf(font, BLACK, WIDTH - 170, 85 + j * 20, 0, "%i", tractor->GetSupply(0, j)); } for (int j = 0; j < 5; j++) { al_draw_textf(font, BLACK, WIDTH - 150, 85 + j * 20, 0, "%i", tractor->GetSupply(1, j)); } al_draw_textf(font, al_map_rgb(255, 0, 255), 5, 5, 0, "FPS: %i", WIDTH - al_get_bitmap_width(panel) /*gameFPS*/); } else if (state == LOST) lostScreen->Render(); al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); } } for (iter = objects.begin(); iter != objects.end();) { (*iter)->Destroy(); delete (*iter); iter = objects.erase(iter); } for (iter2 = history.begin(); iter2 != history.end();) { (*iter2)->Destroy(); delete (*iter2); iter2 = history.erase(iter2); } //tractor->Destroy(); Task->Destroy(); titleScreen->Destroy(); lostScreen->Destroy(); delete titleScreen; delete lostScreen; al_destroy_sample(cash); al_destroy_sample_instance(songInstance); al_destroy_sample_instance(songInstance2); al_destroy_sample_instance(songInstance3); al_destroy_font(score); al_destroy_font(font); al_destroy_timer(timer); al_destroy_event_queue(event_queue); al_destroy_display(display); return 0; }
int main(int argc, char **argv) { //============================================== //SHELL VARIABLES //============================================== bool done = false; bool render = false; float gameTime = 0; int frames = 0; int gameFPS = 0; //============================================== //PROJECT VARIABLES //============================================== int state = MENU; //============================================== //ALLEGRO VARIABLES //============================================== ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_TIMER *timer; ALLEGRO_FONT *font18 = NULL; //============================================== //ALLEGRO INIT FUNCTIONS //============================================== if(!al_init()) //initialize Allegro return -1; display = al_create_display(WIDTH, HEIGHT); //create our display object if(!display) //test display object return -1; //============================================== //ADDON INSTALL //============================================== al_install_keyboard(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); al_init_primitives_addon(); //============================================== //PROJECT INIT //============================================== font18 = al_load_font("arial.ttf", 18, 0); //============================================== //TIMER INIT AND STARTUP //============================================== event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / 60); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_start_timer(timer); gameTime = al_current_time(); while(!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); //============================================== //INPUT //============================================== if(ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: keys[ESCAPE] = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_UP: keys[UP] = true; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = true; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = true; break; } } else if(ev.type == ALLEGRO_EVENT_KEY_UP) { switch(ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: keys[ESCAPE] = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_UP: keys[UP] = false; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = false; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = false; break; } } //============================================== //GAME UPDATE //============================================== else if(ev.type == ALLEGRO_EVENT_TIMER) { render = true; //UPDATE FPS============ frames ++; if (al_current_time() - gameTime >= 1){ gameTime = al_current_time(); gameFPS = frames; frames = 0; } //============ if(state == MENU){ if(keys[SPACE]){ state = PLAYING; } } else if(state == PLAYING){ if(keys[ESCAPE]){ state = GAMEOVER; } } else if (state == GAMEOVER){ if(keys[SPACE]){ done = true; } } } //============================================== //RENDER //============================================== if(render && al_is_event_queue_empty(event_queue)) { render = false; al_draw_textf(font18, al_map_rgb(255, 0, 255), 5, 5, 0, "FPS: %i", gameFPS); // display game FPS on screen //BEGIN PROJECT RENDER============= if(state == MENU){ al_draw_text(font18, al_map_rgb(255,255,255), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Press space to play"); } else if(state == PLAYING){ al_draw_text(font18, al_map_rgb(255,255,255), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Press escape to end"); } else if (state == GAMEOVER){ al_draw_text(font18, al_map_rgb(255,255,255), WIDTH / 2, HEIGHT / 2, ALLEGRO_ALIGN_CENTRE, "Press space to exit the game"); } //FLIP BUFFERS============= al_flip_display(); al_clear_to_color(al_map_rgb(0,0,0)); } } //============================================== //DESTROY ALLEGRO OBJECTS //============================================== al_destroy_font(font18); al_destroy_timer(timer); al_destroy_event_queue(event_queue); al_destroy_display(display); return 0; }
void grafika::timer() { while ( al_current_time() < czas+0.05 ); czas = al_current_time(); }
int main() { int state = MENU; float TimeGame = 0; int frame = 0; int GameFPS = 0; //=========== INFO ACHIEVEMENTS int Points = 0; int Golds = 0; int TimeInGame = 0; //=========== Allegro variable ALLEGRO_DISPLAY *display = NULL; ALLEGRO_EVENT_QUEUE *event_queue = NULL; ALLEGRO_BITMAP *background = NULL; ALLEGRO_BITMAP *background_game = NULL; ALLEGRO_BITMAP *wall1 = NULL; ALLEGRO_BITMAP *wall = NULL; ALLEGRO_TIMER *timer = NULL; //=========== Program init if (!al_init()) return -1; //=========== FullScreen al_set_new_display_flags(ALLEGRO_FULLSCREEN); //=========== Create our display object display = al_create_display(Width, Height); if (!display) return -1; //=========== Addon init al_init_font_addon(); al_init_ttf_addon(); al_install_keyboard(); al_init_primitives_addon(); al_create_display; al_init_image_addon(); event_queue = al_create_event_queue(); timer = al_create_timer(1.0 / FPS); al_register_event_source(event_queue, al_get_keyboard_event_source()); al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); ALLEGRO_FONT *subtitles = al_load_font("BuxtonSketch.ttf", 40, NULL); ALLEGRO_FONT *titles = al_load_font("BuxtonSketch.ttf", 100, NULL); ALLEGRO_FONT *setting_titles = al_load_font("BuxtonSketch.ttf", 55, NULL); ALLEGRO_FONT *options_titles = al_load_font("BuxtonSketch.ttf", 10, NULL); ALLEGRO_FONT *fps = al_load_font("arial.ttf", 18, NULL); al_set_window_position(display, 200, 200); background = al_load_bitmap("tlo1.bmp"); background_game = al_load_bitmap("tlo_gry.png"); wall1 = al_load_bitmap("wall1.bmp"); int x = 0; int y = 0; //=========== MENU al_draw_bitmap(background, x, y, 0); al_draw_textf(titles, al_map_rgb(0, 0, 0), Width/2, Height/4.5, ALLEGRO_ALIGN_CENTRE, "SNAKE !"); al_draw_textf(subtitles, al_map_rgb(0, 0, 0), 400, Height / 2 + 50, ALLEGRO_ALIGN_CENTRE, "Nowa gra"); al_draw_textf(options_titles, al_map_rgb(0, 0, 0), 495, Height / 2 + 70, ALLEGRO_ALIGN_CENTRE, "[ENTER]"); al_draw_textf(options_titles, al_map_rgb(0, 0, 0), 495, Height / 2 + 120, ALLEGRO_ALIGN_CENTRE, "[TAB]"); al_draw_textf(options_titles, al_map_rgb(0, 0, 0), 495, Height / 2 + 170, ALLEGRO_ALIGN_CENTRE, "[ESC]"); al_draw_textf(subtitles, al_map_rgb(0, 0, 0), 400, Height / 2 + 100, ALLEGRO_ALIGN_CENTRE, "Ustawienia"); al_draw_textf(subtitles, al_map_rgb(0, 0, 0), 400, Height / 2 + 150, ALLEGRO_ALIGN_CENTRE, "Zamknij gre"); al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); TimeGame = al_current_time(); al_start_timer(timer); while (!done) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); //=========== INPUT if (ev.type == ALLEGRO_EVENT_KEY_DOWN) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: keys[ESCAPE] = true; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = true; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = true; break; case ALLEGRO_KEY_UP: keys[UP] = true; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = true; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = true; break; case ALLEGRO_KEY_TAB: keys[TAB] = true; break; } } else if (ev.type == ALLEGRO_EVENT_KEY_UP) { switch (ev.keyboard.keycode) { case ALLEGRO_KEY_ESCAPE: keys[ESCAPE] = false; break; case ALLEGRO_KEY_RIGHT: keys[RIGHT] = false; break; case ALLEGRO_KEY_LEFT: keys[LEFT] = false; break; case ALLEGRO_KEY_UP: keys[UP] = false; break; case ALLEGRO_KEY_DOWN: keys[DOWN] = false; break; case ALLEGRO_KEY_SPACE: keys[SPACE] = false; break; case ALLEGRO_KEY_TAB: keys[TAB] = false; break; } } else if (ev.type == ALLEGRO_EVENT_TIMER) { Return = true; frame++; if (al_current_time() - GameFPS >= 1) { TimeGame = al_current_time(); GameFPS = frame; frame = 0; } if (state == MENU) { if (keys[ESCAPE]) { done = true; } al_draw_bitmap(background, x, y, 0); al_draw_textf(titles, al_map_rgb(0, 0, 0), Width / 2, Height / 4.5, ALLEGRO_ALIGN_CENTRE, "SNAKE !"); al_draw_textf(subtitles, al_map_rgb(0, 0, 0), 400, Height / 2 + 50, ALLEGRO_ALIGN_CENTRE, "Nowa gra"); al_draw_textf(subtitles, al_map_rgb(0, 0, 0), 400, Height / 2 + 100, ALLEGRO_ALIGN_CENTRE, "Ustawienia"); al_draw_textf(subtitles, al_map_rgb(0, 0, 0), 400, Height / 2 + 150, ALLEGRO_ALIGN_CENTRE, "Zamknij gre"); al_draw_textf(options_titles, al_map_rgb(0, 0, 0), 495, Height / 2 + 70, ALLEGRO_ALIGN_CENTRE, "[ENTER]"); al_draw_textf(options_titles, al_map_rgb(0, 0, 0), 495, Height / 2 + 120, ALLEGRO_ALIGN_CENTRE, "[TAB]"); al_draw_textf(options_titles, al_map_rgb(0, 0, 0), 495, Height / 2 + 170, ALLEGRO_ALIGN_CENTRE, "[ESC]"); } else if (state == PLAY) { al_clear_to_color(al_map_rgb(0, 0, 0)); al_draw_bitmap(background_game, x, y, 0); //WallFunction(wall1, 0, 0, ); al_flip_display(); } else if (state == GAMEOVER) { al_clear_to_color(al_map_rgb(0, 0, 0)); } else if (state = SETTING) { al_clear_to_color(al_map_rgb(0, 0, 0)); al_draw_bitmap(background, x, y, 0); al_draw_textf(setting_titles, al_map_rgb(0, 0, 0), Width / 2, Height / 4.5, ALLEGRO_ALIGN_CENTRE, "Ustawienia"); al_draw_textf(subtitles, al_map_rgb(0, 0, 0), 250, Height / 2 + 50, ALLEGRO_ALIGN_CENTRE, "Dzwiek: "); al_flip_display(); } } if (Return && al_is_event_queue_empty(event_queue)) { Return = false; al_draw_textf(fps, al_map_rgb(255, 0, 255), 5, 5, 0, "FPS: %d", GameFPS); { if (state == MENU) { if (keys[ENTER]) { state = PLAY; } } else if (state == MENU) { if (keys[TAB]) { state = SETTING; } } else if (state == PLAY) { if (keys[ESCAPE]) { state = MENU; } } else if (state == SETTING) { if (keys[ESCAPE]) { state = MENU; } } else if (state == GAMEOVER) { if (keys[ESCAPE]) { state = MENU; } } else if (state = MENU) { if (keys[ESCAPE]) { done = true; } } } al_flip_display(); al_clear_to_color(al_map_rgb(0, 0, 0)); } } al_destroy_font(fps); al_destroy_timer(timer); al_destroy_event_queue(event_queue); al_destroy_display(display); return 0; }
static LRESULT CALLBACK window_callback(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) { ALLEGRO_DISPLAY *d = NULL; ALLEGRO_DISPLAY_WIN *win_display = NULL; WINDOWINFO wi; int w; int h; int x; int y; unsigned int i; ALLEGRO_EVENT_SOURCE *es = NULL; ALLEGRO_SYSTEM *system = al_get_system_driver(); wi.cbSize = sizeof(WINDOWINFO); if (message == _al_win_msg_call_proc) { ((void (*)(void*))wParam) ((void*)lParam); return 0; } if (!system) { return DefWindowProc(hWnd,message,wParam,lParam); } if (message == _al_win_msg_suicide && wParam) { win_display = (ALLEGRO_DISPLAY_WIN*)wParam; win_display->end_thread = true; DestroyWindow(hWnd); return 0; } for (i = 0; i < system->displays._size; i++) { ALLEGRO_DISPLAY **dptr = _al_vector_ref(&system->displays, i); d = *dptr; win_display = (void*)d; if (win_display->window == hWnd) { es = &d->es; break; } } if (i == system->displays._size) return DefWindowProc(hWnd,message,wParam,lParam); if (message == _al_win_msg_suicide) { win_display->end_thread = true; DestroyWindow(hWnd); return 0; } switch (message) { case WM_INPUT: { UINT dwSize; LPBYTE lpb; RAWINPUT* raw; /* We can't uninstall WM_INPUT mesages. */ if (!al_is_mouse_installed()) break; GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &dwSize, sizeof(RAWINPUTHEADER)); lpb = malloc(sizeof(BYTE)*dwSize); if (lpb == NULL) break; GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpb, &dwSize, sizeof(RAWINPUTHEADER)); raw = (RAWINPUT*)lpb; if (raw->header.dwType != RIM_TYPEMOUSE) { free(lpb); break; } { RAWMOUSE *rm = &raw->data.mouse; int x = raw->data.mouse.lLastX; int y = raw->data.mouse.lLastY; bool abs = rm->usFlags & (MOUSE_MOVE_ABSOLUTE || MOUSE_VIRTUAL_DESKTOP); if (abs || x || y) _al_win_mouse_handle_move(x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_1_DOWN) _al_win_mouse_handle_button(1, true, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_1_UP) _al_win_mouse_handle_button(1, false, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_2_DOWN) _al_win_mouse_handle_button(2, true, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_2_UP) _al_win_mouse_handle_button(2, false, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_3_DOWN) _al_win_mouse_handle_button(3, true, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_3_UP) _al_win_mouse_handle_button(3, false, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_4_DOWN) _al_win_mouse_handle_button(4, true, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_4_UP) _al_win_mouse_handle_button(4, false, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_5_DOWN) _al_win_mouse_handle_button(5, true, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_BUTTON_5_UP) _al_win_mouse_handle_button(5, false, x, y, abs, win_display); if (rm->usButtonFlags & RI_MOUSE_WHEEL) { SHORT z = (SHORT)rm->usButtonData; _al_win_mouse_handle_wheel(z / WHEEL_DELTA, false, win_display); } } free(lpb); break; } case WM_LBUTTONDOWN: case WM_LBUTTONUP: { int mx = GET_X_LPARAM(lParam); int my = GET_Y_LPARAM(lParam); bool down = (message == WM_LBUTTONDOWN); _al_win_mouse_handle_button(1, down, mx, my, true, win_display); handle_mouse_capture(down, hWnd); break; } case WM_MBUTTONDOWN: case WM_MBUTTONUP: { int mx = GET_X_LPARAM(lParam); int my = GET_Y_LPARAM(lParam); bool down = (message == WM_MBUTTONDOWN); _al_win_mouse_handle_button(3, down, mx, my, true, win_display); handle_mouse_capture(down, hWnd); break; } case WM_RBUTTONDOWN: case WM_RBUTTONUP: { int mx = GET_X_LPARAM(lParam); int my = GET_Y_LPARAM(lParam); bool down = (message == WM_RBUTTONDOWN); _al_win_mouse_handle_button(2, down, mx, my, true, win_display); handle_mouse_capture(down, hWnd); break; } case WM_XBUTTONDOWN: case WM_XBUTTONUP: { int mx = GET_X_LPARAM(lParam); int my = GET_Y_LPARAM(lParam); int button = HIWORD(wParam); bool down = (message == WM_XBUTTONDOWN); if (button == XBUTTON1) _al_win_mouse_handle_button(4, down, mx, my, true, win_display); else if (button == XBUTTON2) _al_win_mouse_handle_button(5, down, mx, my, true, win_display); handle_mouse_capture(down, hWnd); return TRUE; } case WM_MOUSEWHEEL: { int d = GET_WHEEL_DELTA_WPARAM(wParam); _al_win_mouse_handle_wheel(d / WHEEL_DELTA, false, win_display); return TRUE; } case WM_MOUSEMOVE: { TRACKMOUSEEVENT tme; int mx = GET_X_LPARAM(lParam); int my = GET_Y_LPARAM(lParam); if (win_display->mouse_cursor_shown && we_hid_the_mouse) { we_hid_the_mouse = false; win_display->display.vt->hide_mouse_cursor((void*)win_display); } _al_win_mouse_handle_move(mx, my, true, win_display); if (mx >= 0 && my >= 0 && mx < d->w && my < d->h) { tme.cbSize = sizeof(tme); tme.dwFlags = TME_QUERY; if (TrackMouseEvent(&tme) && !tme.hwndTrack) { tme.dwFlags = TME_LEAVE; tme.hwndTrack = hWnd; tme.dwHoverTime = 0; TrackMouseEvent(&tme); _al_win_mouse_handle_enter(win_display); } } break; } case WM_MOUSELEAVE: { _al_win_mouse_handle_leave(win_display); break; } case WM_CAPTURECHANGED: if (al_is_mouse_installed()) { int i; ALLEGRO_MOUSE_STATE state; if (!lParam || (HWND)lParam == hWnd) break; al_get_mouse_state(&state); for (i = 1; i <= 5; i++) { if (al_mouse_button_down(&state, i)) _al_win_mouse_handle_button(i, 0, 0, 0, true, win_display); } break; } case WM_NCMOUSEMOVE: { if (!win_display->mouse_cursor_shown) { we_hid_the_mouse = true; win_display->display.vt->show_mouse_cursor((void*)win_display); } break; } case WM_SYSKEYDOWN: { int vcode = wParam; bool repeated = (lParam >> 30) & 0x1; _al_win_kbd_handle_key_press(0, vcode, repeated, win_display); break; } case WM_KEYDOWN: { int vcode = wParam; int scode = (lParam >> 16) & 0xff; bool repeated = (lParam >> 30) & 0x1; /* We can't use TranslateMessage() because we don't know if it will produce a WM_CHAR or not. */ _al_win_kbd_handle_key_press(scode, vcode, repeated, win_display); break; } case WM_SYSKEYUP: case WM_KEYUP: { int vcode = wParam; _al_win_kbd_handle_key_release(vcode, win_display); break; } case WM_SYSCOMMAND: { if (_al_win_disable_screensaver && ((wParam & 0xfff0) == SC_MONITORPOWER || (wParam & 0xfff0) == SC_SCREENSAVE)) { return 0; } else if ((wParam & 0xfff0) == SC_KEYMENU) { /* Prevent Windows from intercepting the ALT key. (Disables opening menus via the ALT key.) */ return 0; } break; } case WM_PAINT: { if ((win_display->display.flags & ALLEGRO_GENERATE_EXPOSE_EVENTS) && _al_event_source_needs_to_generate_event(es)) { RECT r; HRGN hrgn; GetWindowRect(win_display->window, &r); hrgn = CreateRectRgn(r.left, r.top, r.right, r.bottom); if (GetUpdateRgn(win_display->window, hrgn, false) != ERROR) { PAINTSTRUCT ps; DWORD size; LPRGNDATA rgndata; int n; int i; RECT *rects; BeginPaint(win_display->window, &ps); size = GetRegionData(hrgn, 0, NULL); rgndata = _AL_MALLOC(size); GetRegionData(hrgn, size, rgndata); n = rgndata->rdh.nCount; rects = (RECT *)rgndata->Buffer; //GetWindowInfo(win_display->window, &wi); _al_event_source_lock(es); for (i = 0; i < n; i++) { ALLEGRO_EVENT event; event.display.type = ALLEGRO_EVENT_DISPLAY_EXPOSE; event.display.timestamp = al_current_time(); event.display.x = rects[i].left; event.display.y = rects[i].top; event.display.width = rects[i].right - rects[i].left; event.display.height = rects[i].bottom - rects[i].top; _al_event_source_emit_event(es, &event); } _al_event_source_unlock(es); _AL_FREE(rgndata); EndPaint(win_display->window, &ps); DeleteObject(hrgn); } return 0; } break; } case WM_SETCURSOR: switch (LOWORD(lParam)) { case HTLEFT: case HTRIGHT: SetCursor(LoadCursor(NULL, IDC_SIZEWE)); break; case HTBOTTOM: case HTTOP: SetCursor(LoadCursor(NULL, IDC_SIZENS)); break; case HTBOTTOMLEFT: case HTTOPRIGHT: SetCursor(LoadCursor(NULL, IDC_SIZENESW)); break; case HTBOTTOMRIGHT: case HTTOPLEFT: SetCursor(LoadCursor(NULL, IDC_SIZENWSE)); break; default: if (win_display->mouse_cursor_shown) { SetCursor(win_display->mouse_selected_hcursor); } else { SetCursor(NULL); } break; } return 1; case WM_ACTIVATE: if (LOWORD(wParam) != WA_INACTIVE) { /* This SetWindowPos is for faux-fullscreen windows that lost focus * so they can get placed back on top */ // FIXME: this doesn't seem to work //SetWindowPos(win_display->window, HWND_TOP, 0, 0, 0, 0, // SWP_NOMOVE | SWP_NOSIZE); if (d->vt->switch_in) d->vt->switch_in(d); _al_event_source_lock(es); if (_al_event_source_needs_to_generate_event(es)) { ALLEGRO_EVENT event; event.display.type = ALLEGRO_EVENT_DISPLAY_SWITCH_IN; event.display.timestamp = al_current_time(); _al_event_source_emit_event(es, &event); } _al_event_source_unlock(es); _al_win_grab_input(win_display); return 0; } else { if (d->flags & ALLEGRO_FULLSCREEN) { d->vt->switch_out(d); } _al_event_source_lock(es); if (_al_event_source_needs_to_generate_event(es)) { ALLEGRO_EVENT event; event.display.type = ALLEGRO_EVENT_DISPLAY_SWITCH_OUT; event.display.timestamp = al_current_time(); _al_event_source_emit_event(es, &event); } _al_event_source_unlock(es); return 0; } break; case WM_MENUCHAR : return (MNC_CLOSE << 16) | (wParam & 0xffff); case WM_CLOSE: _al_event_source_lock(es); if (_al_event_source_needs_to_generate_event(es)) { ALLEGRO_EVENT event; event.display.type = ALLEGRO_EVENT_DISPLAY_CLOSE; event.display.timestamp = al_current_time(); _al_event_source_emit_event(es, &event); } _al_event_source_unlock(es); return 0; case WM_SIZE: if (wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED || wParam == SIZE_MINIMIZED) { /* * Delay the resize event so we don't get bogged down with them */ if (!resize_postponed) { resize_postponed = true; postpone_resize(win_display->window); } } return 0; case WM_USER+0: /* Generate a resize event if the size has changed. We cannot asynchronously * change the display size here yet, since the user will only know about a * changed size after receiving the resize event. Here we merely add the * event to the queue. */ GetWindowInfo(win_display->window, &wi); x = wi.rcClient.left; y = wi.rcClient.top; w = wi.rcClient.right - wi.rcClient.left; h = wi.rcClient.bottom - wi.rcClient.top; if (d->w != w || d->h != h) { _al_event_source_lock(es); if (_al_event_source_needs_to_generate_event(es)) { ALLEGRO_EVENT event; event.display.type = ALLEGRO_EVENT_DISPLAY_RESIZE; event.display.timestamp = al_current_time(); event.display.x = x; event.display.y = y; event.display.width = w; event.display.height = h; _al_event_source_emit_event(es, &event); } /* Generate an expose event. */ if (_al_event_source_needs_to_generate_event(es)) { ALLEGRO_EVENT event; event.display.type = ALLEGRO_EVENT_DISPLAY_EXPOSE; event.display.timestamp = al_current_time(); event.display.x = x; event.display.y = y; event.display.width = w; event.display.height = h; _al_event_source_emit_event(es, &event); } _al_event_source_unlock(es); } resize_postponed = false; win_display->can_acknowledge = true; return 0; } return DefWindowProc(hWnd,message,wParam,lParam); }
static int allua_current_time(lua_State * L) { lua_pushnumber(L, al_current_time()); return 1; }
int main(){ float* terrain = (float*) malloc(RES*RES*sizeof(float)); genterrain(terrain, RES, 40000, 2.3, true); al_init(); al_install_keyboard(); ALLEGRO_DISPLAY *display; al_set_new_display_flags(ALLEGRO_OPENGL); display = al_create_display(1280, 960); ALLEGRO_EVENT_QUEUE *event_queue = al_create_event_queue(); al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_keyboard_event_source()); bool quit = false; glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); float* verts = (float*) malloc(RES*RES*4*4*3); float* colors = (float*) malloc(RES*RES*4*4*3); float* vp = verts; float* cp = colors; for(int x = 0; x < RES-1; x++){ for(int z = 0; z < RES-1; z++){ *(vp++) = (x-RES/2.0+0.5)*1024.0/RES; *(vp++) = terrain[x*RES+z]; *(vp++) = (z-RES/2.0+0.5)*1024.0/RES; *(cp++) = 0.5*terrain[x*RES+z]; *(cp++) = 1-terrain[x*RES+z]; *(cp++) = 0.5*terrain[x*RES+z]; *(vp++) = (x-RES/2.0+1.5)*1024.0/RES; *(vp++) = terrain[(x+1)*RES+z]; *(vp++) = (z-RES/2.0+0.5)*1024.0/RES; *(cp++) = 0.5*terrain[(x+1)*RES+z]; *(cp++) = 1-terrain[(x+1)*RES+z]; *(cp++) = 0.5*terrain[(x+1)*RES+z]; *(vp++) = (x-RES/2.0+1.5)*1024.0/RES; *(vp++) = terrain[(x+1)*RES+z+1]; *(vp++) = (z-RES/2.0+1.5)*1024.0/RES; *(cp++) = 0.5*terrain[(x+1)*RES+z+1]; *(cp++) = 1-terrain[(x+1)*RES+z+1]; *(cp++) = 0.5*terrain[(x+1)*RES+z+1]; *(vp++) = (x-RES/2.0+0.5)*1024.0/RES; *(vp++) = terrain[x*RES+z+1]; *(vp++) = (z-RES/2.0+1.5)*1024.0/RES; *(cp++) = 0.5*terrain[x*RES+z+1]; *(cp++) = 1-terrain[x*RES+z+1]; *(cp++) = 0.5*terrain[x*RES+z+1]; } } glVertexPointer(3, GL_FLOAT, 0, verts); glColorPointer(3, GL_FLOAT, 0, colors); glEnable(GL_CULL_FACE); glCullFace(GL_FRONT); glEnable(GL_DEPTH_TEST); glDepthMask(GL_TRUE); /*glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST); glEnable(GL_POLYGON_SMOOTH);*/ while(!quit){ double ms = al_current_time(); ALLEGRO_EVENT event; if (al_get_next_event(event_queue, &event)){ if (ALLEGRO_EVENT_KEY_DOWN == event.type && ALLEGRO_KEY_Q == event.keyboard.keycode || ALLEGRO_EVENT_DISPLAY_CLOSE == event.type){ break; } } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glPushMatrix(); glLoadIdentity(); gluPerspective(45, 640.0/480.0,.1,1000); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glRotatef(30, 1, 0, 0); glTranslatef(0, -100, -200); glScalef(1,20,1); glRotatef(ms*12, 0, 1, 0); glPolygonMode(GL_FRONT, GL_LINES); glDrawArrays(GL_QUADS, 0, RES*RES*4); al_flip_display(); } return 0; }
bool Whack_a_Skunk_Loop::init(void) { engine->clear_touches(); if (inited) { return true; } Loop::init(); regular_skunk = new Animation_Set(); regular_skunk->load("mini_games/whack_a_skunk/skunk"); silver_skunk = new Animation_Set(); silver_skunk->load("mini_games/whack_a_skunk/silver_skunk"); gold_skunk = new Animation_Set(); gold_skunk->load("mini_games/whack_a_skunk/gold_skunk"); fake_skunk = new Animation_Set(); fake_skunk->load("mini_games/whack_a_skunk/mallard"); skunk_size.w = regular_skunk->get_current_animation()->get_current_frame()->get_bitmap()->get_width(); skunk_size.h = regular_skunk->get_current_animation()->get_current_frame()->get_bitmap()->get_height(); bg_bitmap = Wrap::load_bitmap("mini_games/whack_a_skunk/board.png"); hand = new Animation_Set(); hand->load("mini_games/whack_a_skunk/hands"); hand_size.w = hand->get_current_animation()->get_current_frame()->get_bitmap()->get_width(); hand_size.h = hand->get_current_animation()->get_current_frame()->get_bitmap()->get_height(); font_w = 15; font_h = 19; font = new Animation_Set(); font->load("mini_games/whack_a_skunk/font"); XMLData *xml = new XMLData("mini_games/whack_a_skunk/board.xml"); for (int i = 0; i < 9; i++) { char buf[2]; buf[0] = '0'+i; buf[1] = 0; XMLData *node = xml->find(std::string(buf)); XMLData *x_node = node->find("x"); XMLData *y_node = node->find("y"); Skunk_Hole h; h.x = atoi(x_node->get_value().c_str()); h.y = atoi(y_node->get_value().c_str()); h.status = NIL; h.miss_count = -1; holes.push_back(h); } delete xml; bg_w = al_get_bitmap_width(bg_bitmap->bitmap); bg_h = al_get_bitmap_height(bg_bitmap->bitmap); top_offset.x = (cfg.screen_w-bg_w)/2; top_offset.y = cfg.screen_h-bg_h; next_diff = max_next - min_next; next_count = 0; misses = 0; hits = 0; star_bmp = Wrap::load_bitmap("mini_games/whack_a_skunk/star.png"); pow_bmp = Wrap::load_bitmap("mini_games/whack_a_skunk/pow.png"); kratch_bmp = Wrap::load_bitmap("mini_games/whack_a_skunk/kratch.png"); mask_front = Wrap::load_bitmap("mini_games/whack_a_skunk/mask-front.png"); mask_middle = Wrap::load_bitmap("mini_games/whack_a_skunk/mask-middle.png"); mask_back = Wrap::load_bitmap("mini_games/whack_a_skunk/mask-back.png"); highlight_front = Wrap::load_bitmap("mini_games/whack_a_skunk/highlight-front.png"); highlight_middle = Wrap::load_bitmap("mini_games/whack_a_skunk/highlight-middle.png"); highlight_back = Wrap::load_bitmap("mini_games/whack_a_skunk/highlight-back.png"); mask_fronthighlight = Wrap::load_bitmap("mini_games/whack_a_skunk/mask-fronthighlight.png"); mask_middlehighlight = Wrap::load_bitmap("mini_games/whack_a_skunk/mask-middlehighlight.png"); mask_backhighlight = Wrap::load_bitmap("mini_games/whack_a_skunk/mask-backhighlight.png"); start_time = al_current_time(); // Load SFX hit_sample = Sound::load("mini_games/whack_a_skunk/sfx/bash.ogg"); miss_sample = Sound::load("mini_games/whack_a_skunk/sfx/bash_no_skunk.ogg"); taunt_sample = Sound::load_set("mini_games/whack_a_skunk/sfx/taunt", "ogg"); return true; }
t_direction c_monster_character::get_next_move() { bool next_instruction; if (this->path_length == 0) return DIRECTION_NONE; if (this->waiting) // do nothing if waiting { if (al_current_time() >= this->waiting_end) { this->waiting = false; this->next_instruction(); } return DIRECTION_NONE; } next_instruction = false; // whether to go to the next instruction switch(this->path_directions[this->current_path_instruction]) { case DIRECTION_NORTH: if (this->position_y <= this->goes_to) next_instruction = true; break; case DIRECTION_EAST: if (this->position_x >= this->goes_to) next_instruction = true; break; case DIRECTION_SOUTH: if (this->position_y >= this->goes_to) next_instruction = true; break; case DIRECTION_WEST: if (this->position_x <= this->goes_to) next_instruction = true; break; case DIRECTION_NONE: this->waiting = true; this->goes_to = this->position_x; this->waiting_end = al_current_time() + this->path_steps[this->current_path_instruction] * 1.0; // set waiting time, 1 sec. for each step break; } if (next_instruction) { this->next_instruction(); } return this->path_directions[this->current_path_instruction]; }
void Game::run () { bool to_paint; ALLEGRO_EVENT event; while (1) { do { al_wait_for_event(global.queue, &event); switch (event.type) { case ALLEGRO_EVENT_DISPLAY_CLOSE: Utils::terminate(); break; case ALLEGRO_EVENT_TIMER: to_paint = true; if (!paused) time_walk (); break; case ALLEGRO_EVENT_KEY_DOWN: case ALLEGRO_EVENT_KEY_REPEAT: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) paused = !paused; }; } while (!al_event_queue_is_empty(global.queue)); if (to_paint) { al_clear_to_color(global.color_black); al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgba_f(1.0, 1.0, 1.0, 0.3)); al_draw_bitmap(global.bitmap_bga[0], GAME_X1, n_frame - al_get_bitmap_height(global.bitmap_bgd[0]) - al_get_bitmap_height(global.bitmap_bgc[0]) - al_get_bitmap_height(global.bitmap_bgb[0]) - al_get_bitmap_height(global.bitmap_bga[0]) + GAME_H, 0); al_draw_bitmap(global.bitmap_bgb[0], GAME_X1, n_frame - al_get_bitmap_height(global.bitmap_bgd[0]) - al_get_bitmap_height(global.bitmap_bgc[0]) - al_get_bitmap_height(global.bitmap_bgb[0]) + GAME_H, 0); al_draw_bitmap(global.bitmap_bgc[0], GAME_X1, n_frame - al_get_bitmap_height(global.bitmap_bgd[0]) - al_get_bitmap_height(global.bitmap_bgc[0]) + GAME_H, 0); al_draw_bitmap(global.bitmap_bgd[0], GAME_X1, n_frame - al_get_bitmap_height(global.bitmap_bgd[0]) + GAME_H, 0); if (gamedata.fl_invincible && n_frame % 6 < 3) { al_set_blender(ALLEGRO_ALPHA, ALLEGRO_ONE, al_map_rgba_f(0.0, 0.0, 1.0, 1.0)); al_draw_bitmap(GAME_POS_C(gamedata.cur_pos, global.bitmap_char[gamedata.character]), 0); al_draw_bitmap(GAME_POS_C(gamedata.cur_pos, global.bitmap_char[gamedata.character]), 0); al_draw_bitmap(GAME_POS_C(gamedata.cur_pos, global.bitmap_char[gamedata.character]), 0); } else { /* if (gamedata.character == CHAR_SILK) { al_set_blender(ALLEGRO_ALPHA, ALLEGRO_ONE, al_map_rgba_f(1.0, 1.0, 1.0, 1.0)); al_draw_bitmap(GAME_POS_C(gamedata.cur_pos, global.bitmap_char[gamedata.character]), 0); al_draw_bitmap(GAME_POS_C(gamedata.cur_pos, global.bitmap_char[gamedata.character]), 0); } else */ { al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgba_f(1.0, 1.0, 1.0, 1.0)); al_draw_bitmap(GAME_POS_C(gamedata.cur_pos, global.bitmap_char[gamedata.character]), 0); } } al_set_blender(ALLEGRO_ALPHA, ALLEGRO_ONE, al_map_rgba_f(1.0, 1.0, 1.0, 1.0)); al_draw_bitmap(GAME_POS_C(gamedata.cur_pos, global.bitmap_crd[gamedata.character]), 0); al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgba_f(1.0, 1.0, 1.0, 1.0)); for (int i = 0; i < gamedata.n_pbullet; i ++) gamedata.pbullet[i] -> draw (); for (int i = 0; i < gamedata.n_ebullet; i ++) gamedata.ebullet[i] -> draw (); for (int i = 0; i < gamedata.n_enemy; i ++) gamedata.enemy[i] -> draw (); al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgba_f(1.0, 1.0, 1.0, 1.0)); al_draw_bitmap(global.bitmap_game, 1, 1, 0); al_set_blender(ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA, al_map_rgba_f(0.7, 0.7, 1.0, 0.9)); al_draw_textf(global.font_game, GAME_X2 + 20, GAME_Y1 + 50, ALLEGRO_ALIGN_LEFT, "HISCORE"); al_draw_textf(global.font_game_score, SCREEN_W - 20, GAME_Y1 + 50, ALLEGRO_ALIGN_RIGHT, "0"); al_draw_textf(global.font_game, GAME_X2 + 20, GAME_Y1 + 75, ALLEGRO_ALIGN_LEFT, "SCORE"); al_draw_textf(global.font_game_score, SCREEN_W - 20, GAME_Y1 + 75, ALLEGRO_ALIGN_RIGHT, "0"); al_draw_textf(global.font_game, GAME_X2 + 20, GAME_Y1 + 110, ALLEGRO_ALIGN_LEFT, "Life"); { char buf[128]; int tmp = 0, bp = 0; while (tmp + HP_PER_LIFE <= gamedata.hp) { buf[bp ++] = '@'; tmp += HP_PER_LIFE; } while (tmp + 1 <= gamedata.hp) { buf[bp ++] = '.'; tmp += HP_PER_LIFE; } buf[bp] = 0; al_draw_textf(global.font_game_score, GAME_X2 + 90, GAME_Y1 + 110, ALLEGRO_ALIGN_LEFT, buf); } al_draw_textf(global.font_game, GAME_X2 + 20, GAME_Y1 + 135, ALLEGRO_ALIGN_LEFT, "Power"); al_draw_textf(global.font_game_score, GAME_X2 + 90, GAME_Y1 + 135, ALLEGRO_ALIGN_LEFT, "%d.%02d \\ 5.00", gamedata.power / 100, gamedata.power % 100); al_draw_textf(global.font_game, GAME_X2 + 20, GAME_Y1 + 160, ALLEGRO_ALIGN_LEFT, "Skill"); al_draw_textf(global.font_game_score, GAME_X2 + 90, GAME_Y1 + 160, ALLEGRO_ALIGN_LEFT, "17234 \\ 1500"); al_draw_textf(global.font_game, GAME_X2 + 20, GAME_Y1 + 195, ALLEGRO_ALIGN_LEFT, "Point"); al_draw_textf(global.font_game_score, GAME_X2 + 90, GAME_Y1 + 195, ALLEGRO_ALIGN_LEFT, "20000"); al_draw_textf(global.font_game, GAME_X2 + 20, GAME_Y1 + 220, ALLEGRO_ALIGN_LEFT, "Graze"); al_draw_textf(global.font_game_score, GAME_X2 + 90, GAME_Y1 + 220, ALLEGRO_ALIGN_LEFT, "%d", gamedata.graze); { char buf[128]; sprintf(buf, "%2.2lf", global.calculated_fps); al_draw_textf(global.font_game_score, SCREEN_W, SCREEN_H - 20, ALLEGRO_ALIGN_RIGHT, buf); } { double cur_time = al_current_time (); global.calculated_fps = FPS / (cur_time - global.time_previous[d_frame % FPS]); global.time_previous[d_frame % FPS] = cur_time; d_frame ++; } al_flip_display(); to_paint = false; } } }
int main(void) { GLuint pid; ALLEGRO_DISPLAY *d; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_EVENT event; int frames = 0; double start; if (!al_init()) { abort_example("Could not init Allegro.\n"); return 1; } al_set_new_display_flags(ALLEGRO_OPENGL); al_set_new_display_option(ALLEGRO_SAMPLE_BUFFERS, 1, ALLEGRO_SUGGEST); al_set_new_display_option(ALLEGRO_SAMPLES, 4, ALLEGRO_SUGGEST); d = al_create_display(WINDOW_W, WINDOW_H); if (!d) { abort_example("Unable to open a OpenGL display.\n"); return -1; } if (al_get_display_option(ALLEGRO_SAMPLE_BUFFERS)) { printf("With multisampling, level %i\n", al_get_display_option(ALLEGRO_SAMPLES)); } else { printf("Without multisampling.\n"); } al_install_keyboard(); queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_display_event_source(d)); if (al_get_opengl_extension_list()->ALLEGRO_GL_ARB_multisample) { glEnable(GL_MULTISAMPLE_ARB); } if (!al_get_opengl_extension_list()->ALLEGRO_GL_ARB_vertex_program) { abort_example("This example requires a video card that supports " " the ARB_vertex_program extension.\n"); return -1; } glEnable(GL_DEPTH_TEST); glShadeModel(GL_SMOOTH); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glPolygonMode(GL_FRONT_AND_BACK, GL_LINE); glDisable(GL_CULL_FACE); /* Setup projection and modelview matrices */ glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluPerspective(45.0, WINDOW_W/(float)WINDOW_H, 0.1, 100.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); /* Position the camera to look at our mesh from a distance */ gluLookAt(0.0f, 20.0f, -45.0f, 0.0f, 0.0f, 0.0f, 0, 1, 0); create_mesh(); /* Define the vertex program */ glEnable(GL_VERTEX_PROGRAM_ARB); glGenProgramsARB(1, &pid); glBindProgramARB(GL_VERTEX_PROGRAM_ARB, pid); glGetError(); if (al_get_opengl_extension_list()->ALLEGRO_GL_NV_vertex_program2_option) { glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(program_nv), program_nv); } else { glProgramStringARB(GL_VERTEX_PROGRAM_ARB, GL_PROGRAM_FORMAT_ASCII_ARB, strlen(program), program); } /* Check for errors */ if (glGetError()) { const char *pgm = al_get_opengl_extension_list()->ALLEGRO_GL_NV_vertex_program2_option ? program_nv : program; GLint error_pos; const GLubyte *error_str = glGetString(GL_PROGRAM_ERROR_STRING_ARB); glGetIntegerv(GL_PROGRAM_ERROR_POSITION_ARB, &error_pos); abort_example("Error compiling the vertex program:\n%s\n\nat " "character: %i\n%s\n", error_str, (int)error_pos, pgm + error_pos); return -1; } start = al_current_time(); while (1) { if (!al_event_queue_is_empty(queue)) { while (al_get_next_event(queue, &event)) { switch (event.type) { case ALLEGRO_EVENT_DISPLAY_CLOSE: goto done; case ALLEGRO_EVENT_KEY_DOWN: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) goto done; break; } } } draw_mesh(); al_flip_display(); frames++; } done: printf("%.1f FPS\n", frames / (al_current_time() - start)); glDeleteProgramsARB(1, &pid); al_destroy_event_queue(queue); return 0; }
static void xglx_background_thread(_AL_THREAD *self, void *arg) { ALLEGRO_SYSTEM_XGLX *s = arg; XEvent event; double last_reset_screensaver_time = 0.0; while (!_al_get_thread_should_stop(self)) { /* Note: * Most older X11 implementations are not thread-safe no matter what, so * we simply cannot sit inside a blocking XNextEvent from another thread * if another thread also uses X11 functions. * * The usual use of XNextEvent is to only call it from the main thread. We * could of course do this for A5, just needs some slight adjustments to * the events system (polling for an Allegro event would call a function * of the system driver). * * As an alternative, we can use locking. This however can never fully * work, as for example OpenGL implementations also will access X11, in a * way we cannot know and cannot control (and we can't require users to * only call graphics functions inside a lock). * * However, most X11 implementations are somewhat thread safe, and do * use locking quite a bit themselves, so locking mostly does work. * * (Yet another alternative might be to use a separate X11 display * connection for graphics output.) * */ _al_mutex_lock(&s->lock); while (XEventsQueued(s->x11display, QueuedAfterFlush)) { XNextEvent(s->x11display, &event); process_x11_event(s, event); } /* The Xlib manual is particularly useless about the XResetScreenSaver() * function. Nevertheless, this does seem to work to inhibit the native * screensaver facility. Probably it won't do anything for other * systems, though. */ if (s->inhibit_screensaver) { double now = al_current_time(); if (now - last_reset_screensaver_time > 10.0) { XResetScreenSaver(s->x11display); last_reset_screensaver_time = now; } } _al_mutex_unlock(&s->lock); /* If no X11 events are there, unlock so other threads can run. We use * a select call to wake up when as soon as anything is available on * the X11 connection - and just for safety also wake up 10 times * a second regardless. */ int x11_fd = ConnectionNumber(s->x11display); fd_set fdset; FD_ZERO(&fdset); FD_SET(x11_fd, &fdset); struct timeval small_time = {0, 100000}; /* 10 times a second */ select(x11_fd + 1, &fdset, NULL, NULL, &small_time); } }