bool init() { ALLEGRO_MONITOR_INFO monitor; if (!al_get_monitor_info(0, &monitor)) return false; //al_set_new_display_flags(ALLEGRO_FULLSCREEN); _display = al_create_display(monitor.x2 / 4, monitor.y2 / 4); if (!_display) return false; sub("setWaitingImage", [&](std::string path){ setWaitingImage(path); }); sub("setCaptureDelay", [&](int d){ _delay = (float)d / 1000.0f; }); sub("newFrame", [&](FrameInfo infos){ newFrame(infos); }); sub("toggleTestMode", [&](){ _testing = !_testing; if (_testing) { _testingImage = std::unique_ptr<Image>(new Image("./assets/test-mode.png")); } else _testingImage = std::unique_ptr<Image>(new Image()); }); return true; }
void LL_SHARED init_allegro() { al_init(); ALLEGRO_MONITOR_INFO monitor_info; if(al_get_monitor_info(0,&monitor_info)) { desktop_size_x=monitor_info.x2-monitor_info.x1; desktop_size_y=monitor_info.y2-monitor_info.y1; } }
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; }
Display *Framework::create_display(Display::resolution_t resolution) { int w, h; int screen_flags = ALLEGRO_FLAGS_EMPTY; int display_adapter_to_use = 0; switch(resolution) { case Display::RESOLUTION_XGA: w = 1024; h = 768; break; case Display::RESOLUTION_WXGA: w = 1280; h = 800; break; case Display::RESOLUTION_WXGA_PLUS: w = 1440; h = 900; break; case Display::RESOLUTION_HD_1080: w = 1920; h = 1080; break; case Display::RESOLUTION_HD_720: w = 1280; h = 720; break; case Display::RESOLUTION_RETINA: w = 2880; h = 1800; break; case Display::FULLSCREEN_AUTO: { ALLEGRO_MONITOR_INFO monitor_info; al_get_monitor_info(display_adapter_to_use, &monitor_info); w = monitor_info.x2 - monitor_info.x1; h = monitor_info.y2 - monitor_info.y1; screen_flags = ALLEGRO_FULLSCREEN; } break; default: w = 1024; h = 768; break; } return create_display(w, h, screen_flags, display_adapter_to_use); }
int _al_win_determine_adapter(void) { int a = al_get_new_display_adapter(); if (a == -1) { int num_screens = al_get_num_video_adapters(); int cScreen = 0; ALLEGRO_MONITOR_INFO temp_info; for (cScreen = 0; cScreen < num_screens; cScreen++) { al_get_monitor_info(cScreen, &temp_info); if (temp_info.x1 == 0 && temp_info.y1 == 0) { // ..probably found primary display return cScreen; } } return 0; // safety measure, probably not necessary } return a; }
static void _al_win_get_window_center( ALLEGRO_DISPLAY_WIN *win_display, int width, int height, int *out_x, int *out_y) { int a = win_display->adapter; bool *is_fullscreen; ALLEGRO_MONITOR_INFO info; RECT win_size; ALLEGRO_SYSTEM *sys = al_get_system_driver(); unsigned int num; unsigned int i; unsigned int fullscreen_found = 0; num = al_get_num_video_adapters(); is_fullscreen = al_calloc(num, sizeof(bool)); for (i = 0; i < sys->displays._size; i++) { ALLEGRO_DISPLAY **dptr = _al_vector_ref(&sys->displays, i); ALLEGRO_DISPLAY *d = *dptr; if (d->flags & ALLEGRO_FULLSCREEN) { ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *)d; is_fullscreen[win_display->adapter] = true; fullscreen_found++; } } if (fullscreen_found && fullscreen_found < num) { for (i = 0; i < num; i++) { if (is_fullscreen[i] == false) { a = i; break; } } } al_free(is_fullscreen); al_get_monitor_info(a, &info); win_size.left = info.x1 + (info.x2 - info.x1 - width) / 2; win_size.top = info.y1 + (info.y2 - info.y1 - height) / 2; *out_x = win_size.left; *out_y = win_size.top; }
int main(void) { ALLEGRO_MONITOR_INFO info; int num_adapters; int i, j; if (!al_init()) { abort_example("Could not init Allegro.\n"); } open_log(); num_adapters = al_get_num_video_adapters(); log_printf("%d adapters found...\n", num_adapters); for (i = 0; i < num_adapters; i++) { al_get_monitor_info(i, &info); log_printf("Adapter %d: ", i); log_printf("(%d, %d) - (%d, %d)\n", info.x1, info.y1, info.x2, info.y2); al_set_new_display_adapter(i); log_printf(" Available fullscreen display modes:\n"); for (j = 0; j < al_get_num_display_modes(); j++) { ALLEGRO_DISPLAY_MODE mode; al_get_display_mode(j, &mode); log_printf(" Mode %3d: %4d x %4d, %d Hz\n", j, mode.width, mode.height, mode.refresh_rate); } } close_log(true); return 0; }
int main(short int argc, char** argv) { al_init(); al_install_keyboard(); al_install_mouse(); al_init_image_addon(); al_init_font_addon(); al_init_ttf_addon(); ALLEGRO_MONITOR_INFO oMonitorInfo; al_get_monitor_info(0, &oMonitorInfo); short int iDisplayWidth = oMonitorInfo.x2 * 0.70f; short int iDisplayHeight = oMonitorInfo.y2 * 0.70f; short int iAppWidth = iDisplayWidth * 0.95f; short int iAppHeight = iDisplayHeight * 0.95f; short int iMarginHorizontal = (iDisplayWidth - iAppWidth) / 2; short int iMarginVertical = ((iDisplayHeight - iAppHeight) / 2); int iGenerations = 0; short int iFPS = 30; float iLifeMin = 3.0f; float iLifeMax = 37.0f; short int iFontSize = (iDisplayWidth > 1024) ? 12 : 10; if (iDisplayWidth < 800) { iFontSize = 8; } long int iSimulations = 1; std::random_device rd; std::mt19937 mt(rd()); std::uniform_real_distribution<double> dist(iLifeMin, iLifeMax); int iLifeScarcity = std::round(dist(mt)); bool** pCells = new bool*[iAppWidth]; bool** pNextGenCells = new bool*[iAppWidth]; initCells(pCells, pNextGenCells, iAppWidth, iAppHeight, iLifeScarcity); ALLEGRO_DISPLAY* pDisplay = al_create_display(iDisplayWidth, iDisplayHeight); ALLEGRO_EVENT_QUEUE* pQueue = al_create_event_queue(); ALLEGRO_TIMER* pTimer = al_create_timer(1.0f / iFPS); ALLEGRO_TIMER* pSecondBySecondTimer = al_create_timer(1.0f); ALLEGRO_BITMAP* pBuffer = al_create_bitmap(iAppWidth, iAppHeight); ALLEGRO_COLOR oBackgroundColor = al_map_rgb(0, 0, 0); ALLEGRO_COLOR oCellColor = al_map_rgb(randr(150, 255), randr(150, 255), randr(150, 255)); ALLEGRO_FONT* oFont = al_load_ttf_font("VeraMono.ttf", iFontSize, 0); ALLEGRO_FONT* oFontLarge = al_load_ttf_font("VeraMono.ttf", (iFontSize * 3), 0); al_inhibit_screensaver(true); al_register_event_source(pQueue, al_get_keyboard_event_source()); al_register_event_source(pQueue, al_get_mouse_event_source()); al_register_event_source(pQueue, al_get_timer_event_source(pTimer)); al_register_event_source(pQueue, al_get_timer_event_source(pSecondBySecondTimer)); al_set_target_backbuffer(pDisplay); al_clear_to_color(oBackgroundColor); al_flip_display(); al_start_timer(pTimer); al_start_timer(pSecondBySecondTimer); ALLEGRO_EVENT oEvent; short int iBufferUsed = 0; short int iBufferDrawn = 0; bool bRedraw = false; std::string sHeaderStatistics = "GEN [GENXXXXX] FPS [FPSXXXXX] CELLS [CELLSXXXXX] GENS/S [GENSSXXXXX] SCARCTY [SCARXXXXX] TIME [TIMEXXXXX]"; std::string sHeaderStats = ""; /* std::string sHeaderText_2 = ""; std::string sHeaderText_3 = ""; std::string sHeaderText_4 = ""; std::string sHeaderText_5 = ""; std::string sHeaderText_6 = ""; */ std::string sCountdownText = ""; std::string sSimulations = ""; std::string sStats = "CELLS: "; sStats.append(std::to_string((iAppWidth * iAppHeight))); sStats.append(", MAP SIZE (KB): "); sStats.append(std::to_string((iAppWidth * iAppHeight * sizeof(bool)) / 1024)); sStats.append(" (SPACE) Pause (C)olor, (R)eload, (S)carcity, (F) +1 FPS, (G) -1 FPS, (ESC) Exit"); long int iTotalAlive = 0; int iPatternStableBuffer = (iFPS * 4); long int* iTotalPatternStable = new long int[iPatternStableBuffer]; short int iTotalPatternCounter = 0; long int iSecondsRunning = 0; float fPosText2 = (iAppWidth * 0.15); float fPosText3 = (iAppWidth * 0.30); float fPosText4 = (iAppWidth * 0.50); float fPosText5 = (iAppWidth * 0.70); float fPosText6 = (iAppWidth * 0.85); float fPosTextSim = (iAppWidth * 0.75); bool bPatternIsStable = false; int iCountdownSeconds = 10; bool bDrawingOn = false; bool bTimerStopped = false; ALLEGRO_COLOR oRandColor = al_map_rgb(randr(0, 255), randr(0, 255), randr(0, 255)); while (true) { al_wait_for_event(pQueue, &oEvent); if (oEvent.type == ALLEGRO_EVENT_TIMER) { if (!bTimerStopped) { if (oEvent.timer.source == pTimer) { iTotalAlive = 0; redrawCells(pBuffer, pCells, pNextGenCells, iAppWidth, iAppHeight, oCellColor, oBackgroundColor); nextGeneration(pCells, pNextGenCells, iAppWidth, iAppHeight, iTotalAlive); al_set_target_backbuffer(pDisplay); al_clear_to_color(oBackgroundColor); al_draw_bitmap(pBuffer, iMarginHorizontal, iMarginVertical, 0); sHeaderStats = ReplaceString(sHeaderStatistics, "[GENXXXXX]", std::to_string(iGenerations)); sHeaderStats = ReplaceString(sHeaderStats, "[FPSXXXXX]", std::to_string(iFPS)); sHeaderStats = ReplaceString(sHeaderStats, "[CELLSXXXXX]", std::to_string(iTotalAlive)); sHeaderStats = ReplaceString(sHeaderStats, "[SCARXXXXX]", std::to_string(iLifeScarcity)); sHeaderStats = ReplaceString(sHeaderStats, "[TIMEXXXXX]", std::to_string(iSecondsRunning)); if (iGenerations > 0 && iSecondsRunning > 0) { sHeaderStats = ReplaceString(sHeaderStats, "[GENSSXXXXX]", std::to_string(iGenerations / iSecondsRunning)); } else { sHeaderStats = ReplaceString(sHeaderStats, "[GENSSXXXXX]", "0"); } sSimulations = "SIMS "; sSimulations.append(std::to_string(iSimulations)); int iLengthSims = al_get_text_width(oFont, sSimulations.c_str()); int iLengthStats = al_get_text_width(oFont, sHeaderStats.c_str()); al_draw_text(oFont, oCellColor, ((iAppWidth - iLengthStats) / 2), 1.0f, 0, sHeaderStats.c_str()); al_draw_text(oFont, oCellColor, (iDisplayWidth - (iLengthSims + 25.0f)), (iAppHeight + iMarginVertical + 5.0f), 0, sSimulations.c_str()); al_draw_text(oFont, oCellColor, 25.0f, (iAppHeight + iMarginVertical + 5.0f), 0, sStats.c_str()); if (bPatternIsStable == true) { sCountdownText.clear(); sCountdownText.append("PATTERN STABILIZED, RESTARTING IN... "); int iLengthStr = al_get_text_width(oFontLarge, sCountdownText.c_str()); sCountdownText.append(std::to_string(iCountdownSeconds)); al_draw_text(oFontLarge, oRandColor, ((iAppWidth - iLengthStr) / 2), (iAppHeight * 0.45f), 0, sCountdownText.c_str()); } al_flip_display(); ++iGenerations; copyCells(pCells, pNextGenCells, iAppWidth, iAppHeight); if (iTotalPatternCounter == iPatternStableBuffer) { bPatternIsStable = isPatternStable(iTotalPatternStable, iPatternStableBuffer); delete iTotalPatternStable; iTotalPatternStable = new long int[iPatternStableBuffer]; iTotalPatternCounter = 0; } iTotalPatternStable[iTotalPatternCounter] = iTotalAlive; ++iTotalPatternCounter; } if (oEvent.timer.source == pSecondBySecondTimer) { if (bPatternIsStable == true) { if (iCountdownSeconds > 1) { --iCountdownSeconds; } else { bPatternIsStable = false; iTotalPatternCounter = 0; iGenerations = 0; iSecondsRunning = 0; iCountdownSeconds = 10; ++iSimulations; clearCells(pCells, pNextGenCells, iAppWidth, iAppHeight); randomCells(pCells, iAppWidth, iAppHeight, iLifeScarcity); } } else { iCountdownSeconds = 10; } ++iSecondsRunning; oRandColor = al_map_rgb(randr(0, 255), randr(0, 255), randr(0, 255)); } } } if (oEvent.type == ALLEGRO_EVENT_KEY_DOWN) { if (oEvent.keyboard.keycode == ALLEGRO_KEY_ESCAPE) { break; } if (oEvent.keyboard.keycode == ALLEGRO_KEY_SPACE) { if (!bTimerStopped) { bTimerStopped = true; al_stop_timer(pTimer); } else { bTimerStopped = false; al_start_timer(pTimer); } } if (oEvent.keyboard.keycode == ALLEGRO_KEY_R) { bPatternIsStable = false; iTotalPatternCounter = 0; iGenerations = 0; iSecondsRunning = 0; iCountdownSeconds = 10; ++iSimulations; clearCells(pCells, pNextGenCells, iAppWidth, iAppHeight); randomCells(pCells, iAppWidth, iAppHeight, iLifeScarcity); } if (oEvent.keyboard.keycode == ALLEGRO_KEY_S) { bPatternIsStable = false; iTotalPatternCounter = 0; iGenerations = 0; iSecondsRunning = 0; iCountdownSeconds = 10; iLifeScarcity = randr(iLifeMin, iLifeMax); ++iSimulations; clearCells(pCells, pNextGenCells, iAppWidth, iAppHeight); randomCells(pCells, iAppWidth, iAppHeight, iLifeScarcity); } if (oEvent.keyboard.keycode == ALLEGRO_KEY_M) { bPatternIsStable = false; iTotalPatternCounter = 0; iGenerations = 0; iSecondsRunning = 0; iCountdownSeconds = 10; iLifeScarcity = iLifeMin; ++iSimulations; clearCells(pCells, pNextGenCells, iAppWidth, iAppHeight); randomCells(pCells, iAppWidth, iAppHeight, iLifeScarcity); } if (oEvent.keyboard.keycode == ALLEGRO_KEY_N) { bPatternIsStable = false; iTotalPatternCounter = 0; iGenerations = 0; iSecondsRunning = 0; iCountdownSeconds = 10; iLifeScarcity = iLifeMax; ++iSimulations; clearCells(pCells, pNextGenCells, iAppWidth, iAppHeight); randomCells(pCells, iAppWidth, iAppHeight, iLifeScarcity); } if (oEvent.keyboard.keycode == ALLEGRO_KEY_F) { ++iFPS; al_set_timer_speed(pTimer, (1.0f / iFPS)); } if (oEvent.keyboard.keycode == ALLEGRO_KEY_G) { if(iFPS > 3) { --iFPS; } al_set_timer_speed(pTimer, (1.0f / iFPS)); } if (oEvent.keyboard.keycode == ALLEGRO_KEY_C) { int iRCell = randr(0, 255); int iGCell = randr(0, 255); int iBCell = randr(0, 255); oCellColor = al_map_rgb(iRCell, iGCell, iBCell); } } } // End main loop al_destroy_event_queue(pQueue); al_destroy_display(pDisplay); delete iTotalPatternStable; for (short int i = 0; i < iAppWidth; i++) { delete pCells[i]; delete pNextGenCells[i]; } delete[] pCells; delete[] pNextGenCells; return 0; }
int engine_init(struct Engine_Conf *conf) { if (engine.initialized) { puts("WARNING: Calling game_init() more than once"); return 1; } // Initialize Allegro and stuff al_init(); if (!al_install_keyboard()) { puts("ERROR: Could not initialize the keyboard..."); return 0; } if (conf->audio) { if (!al_install_audio()) { puts("ERROR: Could not initialize audio..."); return 0; } if (!al_init_acodec_addon()) { puts("ERROR: Could not initialize acodec addon..."); return 0; } } // Add-ons if (!al_init_image_addon()) { puts("ERROR: Could not initialize image addon..."); return 0; } al_init_font_addon(); al_init_primitives_addon(); // Find how much the game will be scaled when conf->scale <= 0 if (conf->scale <= 0) { ALLEGRO_MONITOR_INFO info; al_get_monitor_info(0, &info); int monitor_w = info.x2 - info.x1; int monitor_h = info.y2 - info.y1; float new_monitor_w = (monitor_w - (monitor_w * SCREEN_RES_OVERRIDE)); float new_monitor_h = (monitor_h - (monitor_h * SCREEN_RES_OVERRIDE)); conf->scale = 2; // Keep scaling until a suitable scale factor is found while (1) { int scale_w = conf->width * conf->scale; int scale_h = conf->height * conf->scale; if (scale_w > new_monitor_w || scale_h > new_monitor_h) { --conf->scale; break; } ++conf->scale; } } else if (conf->scale < 2) { conf->scale = 2; } engine.display = al_create_display(conf->width * conf->scale, conf->height * conf->scale); if (!engine.display) { puts("ERROR: Could not create a display window..."); return 0; } al_set_window_title(engine.display, conf->title); font = al_create_builtin_font(); engine.timer = al_create_timer(1.0 / conf->framerate); engine.event_queue = al_create_event_queue(); mainconf = conf; set_bg_color(BG_COLOR_DEFAULT); ALLEGRO_TRANSFORM trans; al_identity_transform(&trans); al_scale_transform(&trans, conf->scale, conf->scale); al_use_transform(&trans); engine.initialized = TRUE; return 1; }
int main(void) { ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_MONITOR_INFO info; int w = 640, h = 480; bool done = false; bool need_redraw = true; bool background = false; if (!al_init()) { abort_example("Failed to init Allegro.\n"); return 1; } if (!al_init_image_addon()) { abort_example("Failed to init IIO addon.\n"); return 1; } al_init_font_addon(); if (!al_init_ttf_addon()) { abort_example("Failed to init TTF addon.\n"); return 1; } al_get_num_video_adapters(); al_get_monitor_info(0, &info); #ifdef ALLEGRO_IPHONE al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW); #endif al_set_new_display_option(ALLEGRO_SUPPORTED_ORIENTATIONS, ALLEGRO_DISPLAY_ORIENTATION_ALL, ALLEGRO_SUGGEST); al_set_new_display_option(ALLEGRO_DEPTH_SIZE, 8, ALLEGRO_SUGGEST); al_set_new_bitmap_flags(ALLEGRO_MIN_LINEAR | ALLEGRO_MAG_LINEAR); example.display = al_create_display(w, h); if (!example.display) { abort_example("Error creating display.\n"); return 1; } if (!al_install_keyboard()) { abort_example("Error installing keyboard.\n"); return 1; } example.font = al_load_font("data/DejaVuSans.ttf", 40, 0); if (!example.font) { abort_example("Error loading data/DejaVuSans.ttf\n"); return 1; } example.font2 = al_load_font("data/DejaVuSans.ttf", 12, 0); if (!example.font2) { abort_example("Error loading data/DejaVuSans.ttf\n"); return 1; } example.mysha = al_load_bitmap("data/mysha.pcx"); if (!example.mysha) { abort_example("Error loading data/mysha.pcx\n"); return 1; } example.obp = al_load_bitmap("data/obp.jpg"); if (!example.obp) { abort_example("Error loading data/obp.jpg\n"); return 1; } init(); timer = al_create_timer(1.0 / FPS); queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_timer_event_source(timer)); al_register_event_source(queue, al_get_display_event_source(example.display)); al_start_timer(timer); while (!done) { ALLEGRO_EVENT event; w = al_get_display_width(example.display); h = al_get_display_height(example.display); if (!background && need_redraw && al_is_event_queue_empty(queue)) { double t = -al_get_time(); redraw(); t += al_get_time(); example.direct_speed_measure = t; al_flip_display(); need_redraw = false; } al_wait_for_event(queue, &event); switch (event.type) { case ALLEGRO_EVENT_KEY_CHAR: if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) done = true; break; case ALLEGRO_EVENT_DISPLAY_CLOSE: done = true; break; case ALLEGRO_EVENT_DISPLAY_HALT_DRAWING: background = true; al_acknowledge_drawing_halt(event.display.source); break; case ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING: background = false; break; case ALLEGRO_EVENT_DISPLAY_RESIZE: al_acknowledge_resize(event.display.source); break; case ALLEGRO_EVENT_TIMER: update(); need_redraw = true; break; } } return 0; }
int main(void) { initialize(); int nva = al_get_num_video_adapters(); assert(nva); ALLEGRO_MONITOR_INFO aminfo; al_get_monitor_info(0, &aminfo); screen_w = aminfo.x2 - aminfo.x1 + 1; screen_h = aminfo.y2 - aminfo.y1 + 1; ALLEGRO_DISPLAY *display = al_create_display(screen_w, screen_h); ALLEGRO_EVENT_QUEUE *evqueue = al_create_event_queue(); ALLEGRO_TIMER *fps_timer = al_create_timer(1.0 / 120.0); al_set_display_flag(display, ALLEGRO_FULLSCREEN_WINDOW, true); if (!display) { printf("Failed to create display!\n"); exit(EXIT_FAILURE); } al_register_event_source(evqueue, al_get_keyboard_event_source()); al_register_event_source(evqueue, al_get_mouse_event_source()); al_register_event_source(evqueue, al_get_display_event_source(display)); al_register_event_source(evqueue, al_get_timer_event_source(fps_timer)); al_start_timer(fps_timer); //////////////////////////////////////////////////////////////////////////////////////////////// // Local Variables //////////////////////////////////////////////////////////////////////////////////////////////// //#ifdef DEBUG float fps = 0, delta_time = 0, current_time = 0, last_time = al_get_time(); //#endif //DEBUG bool render = true, executing = true; unsigned int spawn_counter = 0; Assets* assets = new Assets(); Stage stage = Menu; Stats stats; Button *play = new Button("Play", assets->fnt_menu, screen_w / 2, 300, [&stage, &stats]() -> void { stage = Game; stats.start_time = al_get_time(); }), *leaderboard = new Button("Leaderboard", assets->fnt_menu, screen_w / 2, 400, [&stage]() -> void { stage = Leaderboard; }), *options = new Button("Options", assets->fnt_menu, screen_w / 2, 500, [&stage]() -> void { stage = Options; }), *quit = new Button("Quit", assets->fnt_menu, screen_w / 2, 600, [&executing]() -> void { executing = false; }); b2Vec2 gravity(0.0f, 0.0f); b2World world(gravity); Projectile *player = new Projectile(assets->png_player, &world, screen_w / 2, screen_h - 200); std::vector<Projectile*> Meteors; //////////////////////////////////////////////////////////////////////////////////////////////// // Game Loop //////////////////////////////////////////////////////////////////////////////////////////////// while (executing) { ALLEGRO_EVENT event; al_wait_for_event(evqueue, &event); //#ifdef DEBUG current_time = al_get_time(); delta_time = current_time - last_time; fps = 1/(delta_time); last_time = current_time; //#endif //DEBUG world.Step(delta_time, 8, 3); switch (event.type) { // HANDLE ALLEGRO EVENTS case ALLEGRO_EVENT_TIMER: render = true; if (stage == Game) spawn_counter++; break; case ALLEGRO_EVENT_DISPLAY_CLOSE: executing = false; break; case ALLEGRO_EVENT_KEY_DOWN: executing = event.keyboard.keycode != ALLEGRO_KEY_ESCAPE; switch(event.keyboard.keycode) { case ALLEGRO_KEY_A: case ALLEGRO_KEY_LEFT: if (stage == Game) player->Velocity.x = -5; break; case ALLEGRO_KEY_D: case ALLEGRO_KEY_RIGHT: if (stage == Game) player->Velocity.x = 5; break; } break; case ALLEGRO_EVENT_KEY_UP: if (stage == Game) { player->Velocity.x = 0; player->Velocity.y = 0; } break; default: break; } switch (stage) { // UPDATE case Menu: play->Update(&event); leaderboard->Update(&event); options->Update(&event); quit->Update(&event); break; case Game: if (player->m_body->GetPosition().x < 1) player->m_body->SetTransform(b2Vec2(screen_w - 2, player->m_body->GetPosition().y), 0); if (player->m_body->GetPosition().x > screen_w - 1) player->m_body->SetTransform(b2Vec2(2, player->m_body->GetPosition().y), 0); if (spawn_counter > 75) { spawn_counter = 0; ALLEGRO_BITMAP *meteor_png; switch (rand() % 4) { // Size case 0: switch (rand() % 4) { // Selection case 0: meteor_png = rand() % 2 ? assets->png_meteor_brown_big1 : assets->png_meteor_grey_big1; break; case 1: meteor_png = rand() % 2 ? assets->png_meteor_brown_big2 : assets->png_meteor_grey_big2; break; case 2: meteor_png = rand() % 2 ? assets->png_meteor_brown_big3 : assets->png_meteor_grey_big3; break; case 3: meteor_png = rand() % 2 ? assets->png_meteor_brown_big4 : assets->png_meteor_grey_big4; break; default: break; } break; case 1: switch (rand() % 2) { // Selection case 0: meteor_png = rand() % 2 ? assets->png_meteor_brown_med1 : assets->png_meteor_grey_med1; break; case 1: meteor_png = rand() % 2 ? assets->png_meteor_brown_med2 : assets->png_meteor_grey_med2; break; default: break; } break; case 2: switch (rand() % 2) { // Selection case 0: meteor_png = rand() % 2 ? assets->png_meteor_brown_small1 : assets->png_meteor_grey_small1; break; case 1: meteor_png = rand() % 2 ? assets->png_meteor_brown_small2 : assets->png_meteor_grey_small2; break; default: break; } break; case 3: switch (rand() % 2) { // Selection case 0: meteor_png = rand() % 2 ? assets->png_meteor_brown_tiny1 : assets->png_meteor_grey_tiny1; break; case 1: meteor_png = rand() % 2 ? assets->png_meteor_brown_tiny2 : assets->png_meteor_grey_tiny2; break; default: break; } break; default: break; } auto it = Meteors.end(); Projectile *meteor = new Projectile(meteor_png, &world, rand() % screen_w, -75); meteor->Velocity = b2Vec2(-5 + (rand() % 10), rand() % 15); Meteors.insert(it, meteor); } for (std::vector<Projectile*>::iterator it = Meteors.begin(); it != Meteors.end(); ++it) { if (((*it)->m_body->GetPosition().x >= screen_w + 500 || (*it)->m_body->GetPosition().x <= -500) || ((*it)->m_body->GetPosition().y >= screen_h + 500 || (*it)->m_body->GetPosition().y <= -500) || (((*it)->m_body->GetTransform().p.x == 0.0) && (*it)->m_body->GetTransform().p.y == 0.0)) { delete *it; Meteors.erase(it); } } player->Update(&event); for (auto& x : Meteors) { x->Update(&event); } break; case Leaderboard: break; case Options: break; case End: break; default: break; } if (render && al_is_event_queue_empty(evqueue)) { render = false; al_clear_to_color(al_map_rgb(0, 0, 0)); al_set_target_bitmap(al_get_backbuffer(display)); for (float x = 0; x < screen_w; x += al_get_bitmap_width(assets->png_background)) { for (float y = 0; y < screen_h; y += al_get_bitmap_height(assets->png_background)) { al_draw_bitmap(assets->png_background, x, y, 0); } } #ifdef DEBUG //al_draw_textf(assets->fnt_menu, al_map_rgb(255, 0, 255), 10, 5, ALLEGRO_ALIGN_LEFT, "Debug"); //al_draw_textf(assets->fnt_menu, al_map_rgb(255, 0, 255), 10, 35, ALLEGRO_ALIGN_LEFT, "FPS: %i", (int)fps); //al_draw_textf(assets->fnt_menu, al_map_rgb(255, 0, 255), 10, 65, ALLEGRO_ALIGN_LEFT, "Meteor Count: %i", Meteors.size()); #endif // DEBUG switch (stage) { // RENDER default: break; case Menu: al_draw_text(assets->fnt_title, al_map_rgb(255, 255, 255), screen_w/2, 100, ALLEGRO_ALIGN_CENTRE, "SPACE SHOOTER III"); play->Render(); leaderboard->Render(); options->Render(); quit->Render(); break; case Game: player->Render(); for (auto& x : Meteors) { x->Render(); } break; case Leaderboard: break; case Options: break; case End: break; } al_flip_display(); } render = false; } //////////////////////////////////////////////////////////////////////////////////////////////// // Deinitialization //////////////////////////////////////////////////////////////////////////////////////////////// delete player; al_unregister_event_source(evqueue, al_get_keyboard_event_source()); al_unregister_event_source(evqueue, al_get_mouse_event_source()); al_unregister_event_source(evqueue, al_get_display_event_source(display)); al_unregister_event_source(evqueue, al_get_timer_event_source(fps_timer)); al_destroy_display(display); al_destroy_event_queue(evqueue); al_destroy_timer(fps_timer); delete assets; shutdown(); return EXIT_SUCCESS; }
int main(int argc, char **argv) { ALLEGRO_DISPLAY *displays[2]; ALLEGRO_MONITOR_INFO *info; int adapter_count; int x, y; ALLEGRO_FONT *myfont; ALLEGRO_EVENT_QUEUE *events; ALLEGRO_EVENT event; int i; (void)argc; (void)argv; srand(time(NULL)); if (!al_init()) { abort_example("Could not init Allegro.\n"); } al_install_mouse(); al_init_font_addon(); al_init_image_addon(); adapter_count = al_get_num_video_adapters(); info = malloc(adapter_count * sizeof(ALLEGRO_MONITOR_INFO)); for (i = 0; i < adapter_count; i++) { al_get_monitor_info(i, &info[i]); } x = ((info[0].x2 - info[0].x1) / 3) - (W / 2); y = ((info[0].y2 - info[0].y1) / 2) - (H / 2); al_set_new_window_position(x, y); displays[0] = al_create_display(W, H); x *= 2; al_set_new_window_position(x, y); displays[1] = al_create_display(W, H); if (!displays[0] || !displays[1]) { abort_example("Could not create displays.\n"); } al_set_new_bitmap_flags(ALLEGRO_MEMORY_BITMAP); myfont = al_load_font("data/fixed_font.tga", 0, 0); if (!myfont) { abort_example("Could not load font.\n"); } events = al_create_event_queue(); al_register_event_source(events, al_get_mouse_event_source()); al_register_event_source(events, al_get_display_event_source(displays[0])); al_register_event_source(events, al_get_display_event_source(displays[1])); for (;;) { for (i = 0; i < 2; i++) { al_set_target_backbuffer(displays[i]); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); if (i == 0) al_clear_to_color(al_map_rgb(255, 0, 255)); else al_clear_to_color(al_map_rgb(155, 255, 0)); al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA); al_draw_textf(myfont, al_map_rgb(0, 0, 0), 50, 50, ALLEGRO_ALIGN_CENTRE, "Click me.."); al_flip_display(); } if (al_wait_for_event_timed(events, &event, 1)) { if (event.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } else if (event.type == ALLEGRO_EVENT_MOUSE_BUTTON_DOWN) { int a = rand() % adapter_count; int w = info[a].x2 - info[a].x1; int h = info[a].y2 - info[a].y1; int margin = 20; x = margin + info[a].x1 + (rand() % (w - W - margin)); y = margin + info[a].y1 + (rand() % (h - H - margin)); al_set_window_position(event.mouse.display, x, y); } } } al_destroy_event_queue(events); al_destroy_display(displays[0]); al_destroy_display(displays[1]); free(info); return 0; }
bool _al_win_set_display_flag(ALLEGRO_DISPLAY *display, int flag, bool onoff) { ALLEGRO_DISPLAY_WIN *win_display = (void*)display; //double timeout; ALLEGRO_MONITOR_INFO mi; memset(&mi, 0, sizeof(mi)); switch (flag) { case ALLEGRO_FRAMELESS: { if (onoff) { display->flags |= ALLEGRO_FRAMELESS; } else { display->flags &= ~ALLEGRO_FRAMELESS; } _al_win_set_window_frameless(display, win_display->window, (display->flags & ALLEGRO_FRAMELESS)); return true; } case ALLEGRO_FULLSCREEN_WINDOW: if ((display->flags & ALLEGRO_FULLSCREEN_WINDOW) && onoff) { ALLEGRO_DEBUG("Already a fullscreen window\n"); return true; } if (!(display->flags & ALLEGRO_FULLSCREEN_WINDOW) && !onoff) { ALLEGRO_DEBUG("Already a non-fullscreen window\n"); return true; } if (onoff) { /* Switch off frame in fullscreen window mode. */ _al_win_set_window_frameless(display, win_display->window, true); } else { /* Respect display flag in windowed mode. */ _al_win_set_window_frameless(display, win_display->window, (display->flags & ALLEGRO_FRAMELESS)); } if (onoff) { int adapter = win_display->adapter; 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; } ASSERT(!!(display->flags & ALLEGRO_FULLSCREEN_WINDOW) == onoff); // Hide the window temporarily SetWindowPos(win_display->window, 0, 0, 0, 0, 0, SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOZORDER | SWP_NOMOVE); al_resize_display(display, display->w, display->h); if (onoff) { // Re-set the TOPMOST flag and move to position SetWindowPos(win_display->window, HWND_TOPMOST, mi.x1, mi.y1, 0, 0, SWP_NOSIZE); // Hide the taskbar if fullscreening on primary monitor if (win_display->adapter == 0) { SetWindowPos( FindWindow("Shell_traywnd", ""), 0, 0, 0, 0, 0, SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOZORDER | SWP_NOMOVE); } } else { int pos_x = 0; int pos_y = 0; WINDOWINFO wi; int bw, bh; // Unset the topmost flag SetWindowPos(win_display->window, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); // Show the taskbar SetWindowPos( FindWindow("Shell_traywnd", ""), 0, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOZORDER | SWP_NOMOVE); // Center the window _al_win_get_window_center(win_display, display->w, display->h, &pos_x, &pos_y); GetWindowInfo(win_display->window, &wi); bw = (wi.rcClient.left - wi.rcWindow.left) + (wi.rcWindow.right - wi.rcClient.right), bh = (wi.rcClient.top - wi.rcWindow.top) + (wi.rcWindow.bottom - wi.rcClient.bottom), SetWindowPos( win_display->window, HWND_TOP, 0, 0, display->w+bw, display->h+bh, SWP_NOMOVE ); SetWindowPos( win_display->window, HWND_TOP, pos_x-bw/2, pos_y-bh/2, 0, 0, SWP_NOSIZE ); } // Show the window again SetWindowPos(win_display->window, 0, 0, 0, 0, 0, SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOZORDER | SWP_NOMOVE); ASSERT(!!(display->flags & ALLEGRO_FULLSCREEN_WINDOW) == onoff); return true; } return false; }
int main(int argc, char *argv[]) { bool windowed = false; float rescale = 1.0f; std::string arg; if(argc > 1) { arg = argv[1]; if(arg == "--help") { std::cout << "-s {scale} for running in windowed mode and scaled (standart size is 1440*810)" << std::endl; return 0; } else if(arg == "-s") { if( argc > 2) { arg = argv[2]; rescale = atof(argv[2]); } windowed = true; } } /**initialize allegro*/ if(!al_init()){error_message("al_init()");return 33;} if(!al_init_primitives_addon()){error_message("al_init_primitives_addon()");return 33;} //if(!al_install_keyboard()){error_message("al_install_keyboard()");return 33;} //no use for keyboard in this game if(!al_install_mouse()){error_message("al_install_mouse()");return 33;} if(!al_init_image_addon()){error_message("al_init_image_addon()");return 33;} al_init_font_addon(); // returns void if(!al_init_ttf_addon()){error_message("al_init_ttf_addon()");return 33;} //audio if(al_install_audio() == true) { if(al_init_acodec_addon() == true){} else { error_message("al_init_acodec_addon() - cant initialize audio codec"); global::audio = false; global::sound_card = false; } } else { error_message("al_install_audio() - cant found sound device"); global::audio = false; global::sound_card = false; } /**Some allegro variables*/ ALLEGRO_DISPLAY *display = nullptr; ALLEGRO_EVENT_QUEUE *event_queue = nullptr; ALLEGRO_TIMER *timer = nullptr; ALLEGRO_BITMAP *logo = nullptr; /**Display preparation*/ bool supported_ratio = true; ALLEGRO_MONITOR_INFO mon_info; al_get_monitor_info(0, &mon_info); global::sHeight = mon_info.y2 - mon_info.y1; //gets monitor size in pixels global::sWidth = mon_info.x2 - mon_info.x1; global::aspectratio = round( ((float)global::sWidth / (float)global::sHeight) * 100.0f) / 100.0f; //gets aspectratio if(global::aspectratio == 1.78f){global::xratio = 16; global::yratio = 9;} // 16:9 screen ration else if(global::aspectratio == 1.6f){global::xratio = 16; global::yratio = 10;} // 16:10 else if(global::aspectratio == 1.33f){global::xratio = 4; global::yratio = 3;} // 4:3 else{supported_ratio = false;} global::dHeight = global::dWidth / global::xratio * global::yratio; global::xscale = (float)global::sWidth / (float)global::dWidth; global::yscale = (float)global::sHeight / (float)global::dHeight; /**display creation*/ al_set_new_bitmap_flags(ALLEGRO_MAG_LINEAR | ALLEGRO_MIN_LINEAR); // Thanks to this, magnified fonts dont look retarted, and game is fast (hopefully) :D if(windowed == true || supported_ratio == false) { supported_ratio = true; al_set_new_display_flags(ALLEGRO_WINDOWED | ALLEGRO_OPENGL); global::xscale = rescale; global::yscale = rescale; global::dWidth = 1440; global::dHeight = 810; display = al_create_display(global::dWidth*rescale, global::dHeight*rescale); } else { al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW | ALLEGRO_OPENGL); display = al_create_display(global::dWidth, global::dHeight); } if(display == nullptr){error_message("al_create_display()"); return 1;} al_set_window_title(display, "Este neviem meno, ale asi neco so zemiakom"); /**logo*/ logo = al_load_bitmap("resources/graphics/logo.png"); if(logo == nullptr){error_message("resources/graphics/logo.png not found");} else{ al_set_display_icon(display, logo);} /**Transformation*/ al_identity_transform(&global::trans); if(supported_ratio == true) { al_scale_transform(&global::trans, global::xscale, global::yscale); } else { error_message("Unsupported monitor type - upgrade you monitor pls"); float scale_backup_plan = (global::xscale > global::yscale ? global::yscale : global::xscale); global::xscale = scale_backup_plan; global::yscale = scale_backup_plan; al_scale_transform(&global::trans, global::xscale, global::yscale); } al_use_transform(&global::trans); /**timer*/ timer = al_create_timer(1.0f/global::FPS); if(timer == nullptr){error_message("al_create_timer()"); return 44;} bool redraw = true; /**even que*/ event_queue = al_create_event_queue(); if(event_queue == nullptr){error_message("al_create_event_queue()"); return 44;} /**registering event sources*/ al_register_event_source(event_queue, al_get_display_event_source(display)); al_register_event_source(event_queue, al_get_timer_event_source(timer)); al_register_event_source(event_queue, al_get_mouse_event_source()); al_start_timer(timer); rguil::mouse_state = &global::mouse_state; global::audio_player = new AudioHandler(10); #ifdef _SOUND_TEST ALLEGRO_SAMPLE *s = al_load_sample("resources/music/Fuck_This_Shit_Im_Out.wav"); ALLEGRO_SAMPLE_INSTANCE *si = al_create_sample_instance(s); global::audio_player->global_sounds.push_back(si); global::audio_player->Play_sample_instance(&si, ALLEGRO_PLAYMODE_LOOP); #endif // _SOUND_TEST #ifdef _FPS ALLEGRO_FONT *fps_font = nullptr; fps_font = al_load_font("resources/fonts/Asimov.otf", 12,0); int counter = 0; time_t tsttme2 = time(nullptr), tsttme = time(nullptr); int fps = 0; #endif // FPS global::save = new GameSave(); ScreenMain *SCMain = new ScreenMain(); global::audio_b = new Button("resources/fonts/Calibri.ttf", 1240, global::dHeight -65, 1240 + 40, global::dHeight - 25, "", al_map_rgba(0,0,0,0), ( global::audio == true ? MusicON : MusicOFF)); /**Main loop*/ //forced 30 FPS, drawing and computing in same thread while(global::loop == true) { ALLEGRO_EVENT ev; al_wait_for_event(event_queue, &ev); al_get_mouse_state(&global::mouse_state); if(ev.type == ALLEGRO_EVENT_TIMER) { redraw = true; } else if(ev.type == ALLEGRO_EVENT_DISPLAY_CLOSE) { break; } /**Take event input here*/ if(global::audio_b->Input(ev, global::xscale, global::yscale) == 2) { global::audio_b->unclick(); global::audio = (global::audio == true ? false : true); if(global::audio == true) { al_destroy_bitmap(global::audio_b->bmp); global::audio_b->bmp = al_load_bitmap(MusicON); global::audio_player->Mute_sample_instances(false); } else { al_destroy_bitmap(global::audio_b->bmp); global::audio_b->bmp = al_load_bitmap(MusicOFF); global::audio_player->Mute_sample_instances(true); } } SCMain->Input(ev, global::xscale, global::yscale); /**---------------------*/ #ifdef _FPS tsttme2 = time(&tsttme2); if(difftime(tsttme2,tsttme) >= 1.0f) { tsttme = time(&tsttme); fps = counter; counter = 0; } #endif // FPS if(redraw == true && al_is_event_queue_empty(event_queue)) { redraw = false; al_clear_to_color(al_map_rgb(0,0,0)); /**Draw and compute here*/ SCMain->Print(); global::audio_b->Print(); /**---------------------*/ #ifdef _FPS counter++; al_draw_text(fps_font, al_map_rgb(255,0,0), 0.0f,0.0f, 0, std::to_string(fps).c_str()); #endif // FPS al_flip_display(); } } #ifdef _SOUND_TEST global::audio_player->Stop_sample_instances(); global::audio_player->global_sounds.erase(global::audio_player->global_sounds.begin()); al_destroy_sample_instance(si); al_destroy_sample(s); #endif // _SOUND_TEST delete global::audio_b; delete SCMain; delete global::save; delete global::audio_player; al_destroy_timer(timer); al_destroy_display(display); al_destroy_event_queue(event_queue); if(logo != nullptr) al_destroy_bitmap(logo); #ifdef _FPS al_destroy_font(fps_font); #endif // FPS return 0; }
/* * The window must be created in the same thread that * runs the message loop. */ static void display_thread_proc(void *arg) { WGL_DISPLAY_PARAMETERS *ndp = arg; ALLEGRO_DISPLAY *disp = (ALLEGRO_DISPLAY*)ndp->display; ALLEGRO_DISPLAY_WGL *wgl_disp = (void*)disp; ALLEGRO_DISPLAY_WIN *win_disp = (void*)disp; MSG msg; al_set_new_window_position(ndp->window_x, ndp->window_y); /* So that we can call the functions using TLS from this thread. */ al_set_new_display_flags(disp->flags); if (disp->flags & ALLEGRO_FULLSCREEN) { if (!change_display_mode(disp)) { win_disp->thread_ended = true; destroy_display_internals(wgl_disp); SetEvent(ndp->AckEvent); return; } } else if (disp->flags & ALLEGRO_FULLSCREEN_WINDOW) { ALLEGRO_MONITOR_INFO mi; int adapter = win_disp->adapter; al_get_monitor_info(adapter, &mi); win_disp->toggle_w = disp->w; win_disp->toggle_h = disp->h; disp->w = mi.x2 - mi.x1; disp->h = mi.y2 - mi.y1; } else { win_disp->toggle_w = disp->w; win_disp->toggle_h = disp->h; } win_disp->window = _al_win_create_window(disp, disp->w, disp->h, disp->flags); if (!win_disp->window) { win_disp->thread_ended = true; destroy_display_internals(wgl_disp); SetEvent(ndp->AckEvent); return; } /* FIXME: can't _al_win_create_window() do this? */ if ((disp->flags & ALLEGRO_FULLSCREEN) || (disp->flags & ALLEGRO_FULLSCREEN_WINDOW)) { RECT rect; rect.left = 0; rect.right = disp->w; rect.top = 0; rect.bottom = disp->h; SetWindowPos(win_disp->window, 0, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, SWP_NOZORDER | SWP_FRAMECHANGED); } if (disp->flags & ALLEGRO_FULLSCREEN_WINDOW) { bool frameless = true; _al_win_set_window_frameless(disp, win_disp->window, disp->w, disp->h, frameless); } /* Yep, the following is really needed sometimes. */ /* ... Or is it now that we have dumped DInput? */ /* <rohannessian> Win98/2k/XP's window forground rules don't let us * make our window the topmost window on launch. This causes issues on * full-screen apps, as DInput loses input focus on them. * We use this trick to force the window to be topmost, when switching * to full-screen only. Note that this only works for Win98 and greater. * Win95 will ignore our SystemParametersInfo() calls. * * See http://support.microsoft.com:80/support/kb/articles/Q97/9/25.asp * for details. */ { DWORD lock_time; HWND wnd = win_disp->window; #define SPI_GETFOREGROUNDLOCKTIMEOUT 0x2000 #define SPI_SETFOREGROUNDLOCKTIMEOUT 0x2001 if (disp->flags & ALLEGRO_FULLSCREEN) { SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, (LPVOID)&lock_time, 0); SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (LPVOID)0, SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE); } ShowWindow(wnd, SW_SHOWNORMAL); SetForegroundWindow(wnd); /* In some rare cases, it doesn't seem to work without the loop. And we * absolutely need this to succeed, else we trap the user in a * fullscreen window without input. */ while (GetForegroundWindow() != wnd) { al_rest(0.01); SetForegroundWindow(wnd); } UpdateWindow(wnd); if (disp->flags & ALLEGRO_FULLSCREEN) { SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, (LPVOID)(DWORD)lock_time, SPIF_SENDWININICHANGE | SPIF_UPDATEINIFILE); } #undef SPI_GETFOREGROUNDLOCKTIMEOUT #undef SPI_SETFOREGROUNDLOCKTIMEOUT } #if 0 if (disp->flags & ALLEGRO_FULLSCREEN && al_is_mouse_installed()) { RAWINPUTDEVICE rid[1]; rid[0].usUsagePage = 0x01; rid[0].usUsage = 0x02; rid[0].dwFlags = RIDEV_NOLEGACY; rid[0].hwndTarget = 0; if (RegisterRawInputDevices(rid, 1, sizeof(rid[0])) == FALSE) { ALLEGRO_ERROR( "Failed to init mouse. %s\n", get_error_desc(GetLastError())); } } #endif /* get the device context of our window */ wgl_disp->dc = GetDC(win_disp->window); win_disp->thread_ended = false; win_disp->end_thread = false; ndp->init_failed = false; SetEvent(ndp->AckEvent); while (!win_disp->end_thread) { /* get a message from the queue */ if (GetMessage(&msg, NULL, 0, 0) != 0) DispatchMessage(&msg); else break; /* WM_QUIT received or error (GetMessage returned -1) */ } if (wgl_disp->glrc) { wglDeleteContext(wgl_disp->glrc); wgl_disp->glrc = NULL; } if (wgl_disp->dc) { ReleaseDC(win_disp->window, wgl_disp->dc); wgl_disp->dc = NULL; } if (disp->flags & ALLEGRO_FULLSCREEN && !_wgl_do_not_change_display_mode) { ChangeDisplaySettings(NULL, 0); } if (win_disp->window) { DestroyWindow(win_disp->window); win_disp->window = NULL; } ALLEGRO_INFO("wgl display thread exits\n"); win_disp->thread_ended = true; }
int main(void) { ALLEGRO_TIMER *timer; ALLEGRO_EVENT_QUEUE *queue; ALLEGRO_MONITOR_INFO info; int w = 640, h = 480; bool done = false; bool need_redraw = true; bool background = false; example.show_help = true; if (!al_init()) { abort_example("Failed to init Allegro.\n"); return 1; } if (!al_init_image_addon()) { abort_example("Failed to init IIO addon.\n"); return 1; } al_init_font_addon(); al_get_num_video_adapters(); al_get_monitor_info(0, &info); #ifdef ALLEGRO_IPHONE al_set_new_display_flags(ALLEGRO_FULLSCREEN_WINDOW); #endif al_set_new_display_option(ALLEGRO_SUPPORTED_ORIENTATIONS, ALLEGRO_DISPLAY_ORIENTATION_ALL, ALLEGRO_SUGGEST); example.display = al_create_display(w, h); w = al_get_display_width(example.display); h = al_get_display_height(example.display); if (!example.display) { abort_example("Error creating display.\n"); return 1; } if (!al_install_keyboard()) { abort_example("Error installing keyboard.\n"); return 1; } if (!al_install_mouse()) { abort_example("Error installing mouse.\n"); return 1; } example.font = al_load_font("data/fixed_font.tga", 0, 0); if (!example.font) { abort_example("Error loading data/fixed_font.tga\n"); return 1; } example.mysha = al_load_bitmap("data/mysha256x256.png"); if (!example.mysha) { abort_example("Error loading data/mysha256x256.png\n"); return 1; } example.white = al_map_rgb_f(1, 1, 1); example.half_white = al_map_rgba_f(1, 1, 1, 0.5); example.dark = al_map_rgb(15, 15, 15); example.red = al_map_rgb_f(1, 0.2, 0.1); change_size(256); add_sprite(); add_sprite(); timer = al_create_timer(1.0 / FPS); queue = al_create_event_queue(); al_register_event_source(queue, al_get_keyboard_event_source()); al_register_event_source(queue, al_get_mouse_event_source()); al_register_event_source(queue, al_get_timer_event_source(timer)); if (al_install_touch_input()) al_register_event_source(queue, al_get_touch_input_event_source()); al_register_event_source(queue, al_get_display_event_source(example.display)); al_start_timer(timer); while (!done) { float x, y; ALLEGRO_EVENT event; w = al_get_display_width(example.display); h = al_get_display_height(example.display); if (!background && need_redraw && al_is_event_queue_empty(queue)) { double t = -al_get_time(); add_time(); al_clear_to_color(al_map_rgb_f(0, 0, 0)); redraw(); t += al_get_time(); example.direct_speed_measure = t; al_flip_display(); need_redraw = false; } al_wait_for_event(queue, &event); switch (event.type) { case ALLEGRO_EVENT_KEY_CHAR: /* includes repeats */ if (event.keyboard.keycode == ALLEGRO_KEY_ESCAPE) done = true; else if (event.keyboard.keycode == ALLEGRO_KEY_UP) { add_sprites(1); } else if (event.keyboard.keycode == ALLEGRO_KEY_DOWN) { remove_sprites(1); } else if (event.keyboard.keycode == ALLEGRO_KEY_LEFT) { change_size(example.bitmap_size - 1); } else if (event.keyboard.keycode == ALLEGRO_KEY_RIGHT) { change_size(example.bitmap_size + 1); } else if (event.keyboard.keycode == ALLEGRO_KEY_F1) { example.show_help ^= 1; } else if (event.keyboard.keycode == ALLEGRO_KEY_SPACE) { example.use_memory_bitmaps ^= 1; change_size(example.bitmap_size); } else if (event.keyboard.keycode == ALLEGRO_KEY_B) { example.blending++; if (example.blending == 4) example.blending = 0; } break; case ALLEGRO_EVENT_DISPLAY_CLOSE: done = true; break; case ALLEGRO_EVENT_DISPLAY_HALT_DRAWING: background = true; al_acknowledge_drawing_halt(event.display.source); break; case ALLEGRO_EVENT_DISPLAY_RESUME_DRAWING: background = false; break; case ALLEGRO_EVENT_DISPLAY_RESIZE: al_acknowledge_resize(event.display.source); break; case ALLEGRO_EVENT_TIMER: update(); need_redraw = true; break; case ALLEGRO_EVENT_TOUCH_BEGIN: x = event.touch.x; y = event.touch.y; goto click; case ALLEGRO_EVENT_MOUSE_BUTTON_UP: x = event.mouse.x; y = event.mouse.y; goto click; click: { int fh = al_get_font_line_height(example.font); if (x < 80 && y >= h - fh * 10) { int button = (y - (h - fh * 10)) / (fh * 2); if (button == 0) { example.use_memory_bitmaps ^= 1; change_size(example.bitmap_size); } if (button == 1) { example.blending++; if (example.blending == 4) example.blending = 0; } if (button == 3) { if (x < 40) remove_sprites(example.sprite_count / 2); else add_sprites(example.sprite_count); } if (button == 2) { int s = example.bitmap_size * 2; if (x < 40) s = example.bitmap_size / 2; change_size(s); } if (button == 4) { example.show_help ^= 1; } } break; } } } al_destroy_bitmap(example.bitmap); return 0; }
HWND _al_win_create_window(ALLEGRO_DISPLAY *display, int width, int height, int flags) { HWND my_window; RECT win_size; DWORD style; DWORD ex_style; int pos_x, pos_y; bool center = false; ALLEGRO_MONITOR_INFO info; ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *)display; WINDOWINFO wi; int bw, bh; wi.cbSize = sizeof(WINDOWINFO); if (!(flags & ALLEGRO_FULLSCREEN)) { if (flags & ALLEGRO_RESIZABLE) { style = WS_OVERLAPPEDWINDOW; ex_style = WS_EX_APPWINDOW|WS_EX_OVERLAPPEDWINDOW; } else { style = WS_SYSMENU | WS_MINIMIZEBOX; ex_style = WS_EX_APPWINDOW; } } else { style = WS_POPUP; ex_style = WS_EX_APPWINDOW; } al_get_new_window_position(&pos_x, &pos_y); if (pos_x == INT_MAX) { pos_x = pos_y = 0; if (!(flags & ALLEGRO_FULLSCREEN)) { center = true; } } if (center) { //int a = al_get_current_video_adapter(); int a = win_display->adapter; if (a == -1) { ALLEGRO_SYSTEM *sys = al_get_system_driver(); unsigned int num; bool *is_fullscreen; unsigned int i; unsigned int fullscreen_found = 0; num = al_get_num_video_adapters(); is_fullscreen = _AL_MALLOC(sizeof(bool)*num); memset(is_fullscreen, 0, sizeof(bool)*num); for (i = 0; i < sys->displays._size; i++) { ALLEGRO_DISPLAY **dptr = _al_vector_ref(&sys->displays, i); ALLEGRO_DISPLAY *d = *dptr; if (d->flags & ALLEGRO_FULLSCREEN) { ALLEGRO_DISPLAY_WIN *win_display = (ALLEGRO_DISPLAY_WIN *)d; is_fullscreen[win_display->adapter] = true; fullscreen_found++; } } if (fullscreen_found && fullscreen_found < num) { for (i = 0; i < num; i++) { if (is_fullscreen[i] == false) { a = i; break; } } } else a = 0; _AL_FREE(is_fullscreen); } al_set_current_video_adapter(a); al_get_monitor_info(a, &info); win_size.left = info.x1 + (info.x2 - info.x1 - width) / 2; win_size.right = win_size.left + width; win_size.top = info.y1 + (info.y2 - info.y1 - height) / 2; win_size.bottom = win_size.top + height; AdjustWindowRectEx(&win_size, style, false, ex_style); pos_x = win_size.left; pos_y = win_size.top; } my_window = CreateWindowEx(ex_style, "ALEX", "Allegro", style, pos_x, pos_y, width, height, NULL,NULL,window_class.hInstance,0); GetWindowInfo(my_window, &wi); bw = (wi.rcClient.left - wi.rcWindow.left) + (wi.rcWindow.right - wi.rcClient.right), bh = (wi.rcClient.top - wi.rcWindow.top) + (wi.rcWindow.bottom - wi.rcClient.bottom), SetWindowPos(my_window, 0, pos_x-bw/2, pos_y-bh/2, width+bw, height+bh, SWP_NOMOVE | SWP_NOZORDER); if (flags & ALLEGRO_NOFRAME) { SetWindowLong(my_window, GWL_STYLE, WS_VISIBLE); SetWindowLong(my_window, GWL_EXSTYLE, WS_EX_APPWINDOW); SetWindowPos(my_window, 0, 0, 0, width, height, SWP_NOMOVE | SWP_NOZORDER | SWP_FRAMECHANGED); } ShowWindow(my_window, SW_SHOW); if (!(flags & ALLEGRO_RESIZABLE) && !(flags & ALLEGRO_FULLSCREEN)) { /* Make the window non-resizable */ HMENU menu; menu = GetSystemMenu(my_window, false); DeleteMenu(menu, SC_SIZE, MF_BYCOMMAND); DeleteMenu(menu, SC_MAXIMIZE, MF_BYCOMMAND); DrawMenuBar(my_window); } return my_window; }
static bool wgl_resize_helper(ALLEGRO_DISPLAY *d, int width, int height) { ALLEGRO_DISPLAY_WGL *wgl_disp = (ALLEGRO_DISPLAY_WGL *)d; ALLEGRO_DISPLAY *ogl_disp = (ALLEGRO_DISPLAY *)d; ALLEGRO_DISPLAY_WIN *win_disp = (ALLEGRO_DISPLAY_WIN *)d; int full_w, full_h; ALLEGRO_MONITOR_INFO mi; int adapter = al_get_new_display_adapter(); if (adapter < 0) adapter = 0; al_get_monitor_info(adapter, &mi); full_w = mi.x2 - mi.x1; full_h = mi.y2 - mi.y1; if ((d->flags & ALLEGRO_FULLSCREEN_WINDOW) && (full_w != width || full_h != height)) { win_disp->toggle_w = width; win_disp->toggle_h = height; return true; } win_disp->can_acknowledge = false; if (d->flags & ALLEGRO_FULLSCREEN) { ALLEGRO_BITMAP *target_bmp; _AL_VECTOR disp_bmps; bool was_backbuffer = false; size_t i; target_bmp = al_get_target_bitmap(); if (target_bmp->vt) was_backbuffer = ((ALLEGRO_BITMAP_OGL*)target_bmp)->is_backbuffer; /* Remeber display bitmaps. */ _al_vector_init(&disp_bmps, sizeof(ALLEGRO_BITMAP*)); for (i = 0; i < _al_vector_size(&d->bitmaps); i++) { ALLEGRO_BITMAP **dis = _al_vector_ref(&d->bitmaps, i); ALLEGRO_BITMAP **mem = _al_vector_alloc_back(&disp_bmps); *mem = *dis; } /* This flag prevents from switching to desktop resolution in between. */ _wgl_do_not_change_display_mode = true; destroy_display_internals(wgl_disp); _wgl_do_not_change_display_mode = false; d->w = width; d->h = height; if (!create_display_internals(wgl_disp)) return false; /* Reupload bitmaps. */ while (_al_vector_is_nonempty(&disp_bmps)) { ALLEGRO_BITMAP **back = _al_vector_ref_back(&disp_bmps); _al_convert_to_display_bitmap(*back); _al_vector_delete_at(&disp_bmps, _al_vector_size(&disp_bmps) - 1); } /* We have a new backbuffer now. */ if (was_backbuffer) al_set_target_bitmap(al_get_backbuffer(d)); } else { RECT win_size; WINDOWINFO wi; win_size.left = 0; win_size.top = 0; win_size.right = width; win_size.bottom = height; wi.cbSize = sizeof(WINDOWINFO); GetWindowInfo(win_disp->window, &wi); AdjustWindowRectEx(&win_size, wi.dwStyle, false, wi.dwExStyle); if (!SetWindowPos(win_disp->window, HWND_TOP, 0, 0, win_size.right - win_size.left, win_size.bottom - win_size.top, SWP_NOMOVE|SWP_NOZORDER)) return false; PostMessage(win_disp->window, WM_USER+0, 0, 0); d->w = width; d->h = height; if (!(d->flags & ALLEGRO_FULLSCREEN_WINDOW)) { win_disp->toggle_w = width; win_disp->toggle_h = height; } _al_ogl_resize_backbuffer(ogl_disp->ogl_extras->backbuffer, width, height); setup_gl(d); } return true; }
void Gamestate_ProcessEvent(struct Game *game, struct MenuResources* data, ALLEGRO_EVENT *ev) { if ((data->menustate == MENUSTATE_ABOUT) && (ev->type == ALLEGRO_EVENT_KEY_DOWN)) { ChangeMenuState(game, data, MENUSTATE_MAIN); return; } if (ev->type != ALLEGRO_EVENT_KEY_DOWN) return; if (data->starting) return; if (ev->keyboard.keycode==ALLEGRO_KEY_UP) { data->selected--; if ((data->selected == 2) && ((data->menustate==MENUSTATE_VIDEO) || (data->menustate==MENUSTATE_OPTIONS) || (data->menustate==MENUSTATE_AUDIO))) { data->selected --; } if ((data->menustate==MENUSTATE_VIDEO) && (data->selected==1) && (data->options.fullscreen)) data->selected--; al_play_sample_instance(data->click); } else if (ev->keyboard.keycode==ALLEGRO_KEY_DOWN) { data->selected++; if ((data->menustate==MENUSTATE_VIDEO) && (data->selected==1) && (data->options.fullscreen)) data->selected++; if ((data->selected == 2) && ((data->menustate==MENUSTATE_VIDEO) || (data->menustate==MENUSTATE_OPTIONS) || (data->menustate==MENUSTATE_AUDIO))) { data->selected ++; } al_play_sample_instance(data->click); } if (ev->keyboard.keycode==ALLEGRO_KEY_ENTER) { char *text; al_play_sample_instance(data->click); switch (data->menustate) { case MENUSTATE_MAIN: switch (data->selected) { case 0: StartGame(game, data); break; case 1: ChangeMenuState(game,data,MENUSTATE_OPTIONS); break; case 2: ChangeMenuState(game,data,MENUSTATE_ABOUT); break; case 3: UnloadGamestate(game, "menu"); break; } break; case MENUSTATE_HIDDEN: ChangeMenuState(game,data,MENUSTATE_MAIN); break; case MENUSTATE_AUDIO: text = malloc(255*sizeof(char)); switch (data->selected) { case 0: game->config.music--; if (game->config.music<0) game->config.music=10; snprintf(text, 255, "%d", game->config.music); SetConfigOption(game, "SuperDerpy", "music", text); al_set_mixer_gain(game->audio.music, game->config.music/10.0); break; case 1: game->config.fx--; if (game->config.fx<0) game->config.fx=10; snprintf(text, 255, "%d", game->config.fx); SetConfigOption(game, "SuperDerpy", "fx", text); al_set_mixer_gain(game->audio.fx, game->config.fx/10.0); break; case 2: game->config.voice--; if (game->config.voice<0) game->config.voice=10; snprintf(text, 255, "%d", game->config.voice); SetConfigOption(game, "SuperDerpy", "voice", text); al_set_mixer_gain(game->audio.voice, game->config.voice/10.0); break; case 3: ChangeMenuState(game,data,MENUSTATE_OPTIONS); break; } free(text); break; case MENUSTATE_OPTIONS: switch (data->selected) { case 0: ChangeMenuState(game,data,MENUSTATE_VIDEO); break; case 1: ChangeMenuState(game,data,MENUSTATE_AUDIO); break; case 3: ChangeMenuState(game,data,MENUSTATE_MAIN); break; default: break; } break; case MENUSTATE_VIDEO: switch (data->selected) { case 0: data->options.fullscreen = !data->options.fullscreen; if (data->options.fullscreen) SetConfigOption(game, "SuperDerpy", "fullscreen", "1"); else SetConfigOption(game, "SuperDerpy", "fullscreen", "0"); al_set_display_flag(game->display, ALLEGRO_FULLSCREEN_WINDOW, data->options.fullscreen); SetupViewport(game); PrintConsole(game, "Fullscreen toggled"); break; case 1: data->options.resolution++; int max = 0, i = 0; for (i=0; i < al_get_num_video_adapters(); i++) { ALLEGRO_MONITOR_INFO aminfo; al_get_monitor_info(i , &aminfo); int desktop_width = aminfo.x2 - aminfo.x1 + 1; int desktop_height = aminfo.y2 - aminfo.y1 + 1; int localmax = desktop_width / 320; if (desktop_height / 180 < localmax) localmax = desktop_height / 180; if (localmax > max) max = localmax; } if (data->options.resolution > max) data->options.resolution = 1; text = malloc(255*sizeof(char)); snprintf(text, 255, "%d", data->options.resolution * 320); SetConfigOption(game, "SuperDerpy", "width", text); snprintf(text, 255, "%d", data->options.resolution * 180); SetConfigOption(game, "SuperDerpy", "height", text); free(text); al_resize_display(game->display, data->options.resolution * 320, data->options.resolution * 180); if ((al_get_display_width(game->display) < (data->options.resolution * 320)) || (al_get_display_height(game->display) < (data->options.resolution * 180))) { SetConfigOption(game, "SuperDerpy", "width", "320"); SetConfigOption(game, "SuperDerpy", "height", "180"); data->options.resolution = 1; al_resize_display(game->display, 320, 180); } SetupViewport(game); PrintConsole(game, "Resolution changed"); break; case 3: ChangeMenuState(game,data,MENUSTATE_OPTIONS); break; default: break; } break; case MENUSTATE_ABOUT: break; default: UnloadGamestate(game, "menu"); return; break; } } else if (ev->keyboard.keycode==ALLEGRO_KEY_ESCAPE) { switch (data->menustate) { case MENUSTATE_OPTIONS: ChangeMenuState(game,data,MENUSTATE_MAIN); break; case MENUSTATE_ABOUT: ChangeMenuState(game,data,MENUSTATE_MAIN); break; case MENUSTATE_HIDDEN: UnloadGamestate(game, "menu"); break; case MENUSTATE_VIDEO: ChangeMenuState(game,data,MENUSTATE_OPTIONS); break; case MENUSTATE_AUDIO: ChangeMenuState(game,data,MENUSTATE_OPTIONS); break; default: ChangeMenuState(game,data,MENUSTATE_HIDDEN); data->selected = -1; data->title_pos = 0; return; } } if (data->selected==-1) data->selected=3; if (data->selected==4) data->selected=0; return; }