/// Set the same tie to all parameters. /// @param tie :: A tie string. void EditLocalParameterDialog::setTieAll(QString tie) { for (int i = 0; i < m_ties.size(); ++i) { m_ties[i] = tie; m_fixes[i] = false; updateRoleColumn(i); } redrawCells(); }
/// Fix/unfix all parameters. /// @param fix :: Fix (true) or unfix (false). void EditLocalParameterDialog::setAllFixed(bool fix) { if (m_fixes.empty()) return; for (int i = 0; i < m_fixes.size(); ++i) { m_fixes[i] = fix; m_ties[i] = ""; updateRoleColumn(i); } redrawCells(); }
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; }