int main(int argc, char* argv[]) { int no_threads, no_operations; struct timeval start; struct timeval end; long diff; if(argc <= 2) { printf("Usage: %s <#threads> <#operations>\n", argv[0]); return 1; } no_threads = atoi(argv[1]); no_operations = atoi(argv[2]); assert( no_threads > 0 ); assert( no_operations > 0 ); initialize_queue(); printf("\n\n"); /**** TEST with random operations ***/ gettimeofday(&start, NULL); spawner(no_threads, no_operations); gettimeofday(&end, NULL); diff = MILL * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec; printf("\tElapsed %ld microseconds!\n", diff); /**** TEST FIFO ordering for each concurrent object ***/ //gettimeofday(&start, NULL); MPSC(no_threads, no_operations); printf("\n\n"); SPMC(no_threads, no_operations); // gettimeofday(&end, NULL); // diff = MILL * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec; // printf("\tElapsed %ld microseconds!\n", diff); printf("\n\n"); exit(0); }
void PlayerSpawnSystem::Update(double dt) { // If there are no CapturePointGameMode components we will just spawn immediately. // Should be able to support older maps with this. // TODO: In the future we might want to return instead, to avoid spawning in the menu for instance. auto pool = m_World->GetComponents("CapturePointGameMode"); if (pool != nullptr && pool->size() > 0) { // Take the first CapturePointGameMode component found. ComponentWrapper& modeComponent = *pool->begin(); // Increase timer. Field<double> timer = modeComponent["RespawnTime"]; timer += dt; if (m_DbgConfigForceRespawn) { (Field<double>)modeComponent["MaxRespawnTime"] = m_ForcedRespawnTime; } double maxRespawnTime = (double)modeComponent["MaxRespawnTime"]; EntityWrapper spectatorCam = m_World->GetFirstEntityByName("SpectatorCamera"); if (spectatorCam.Valid()) { EntityWrapper HUD = spectatorCam.FirstChildByName("SpectatorHUD"); if (HUD.Valid()) { EntityWrapper respawnTimer = spectatorCam.FirstChildByName("RespawnTimer"); if (respawnTimer.Valid()) { //Update respawn time in the HUD element. respawnTimer["Text"]["Content"] = std::to_string(1 + (int)(maxRespawnTime - timer)); } } } if (timer < maxRespawnTime) { return; } // If respawn time has passed, we spawn all players that have requested to be spawned. timer = 0; } // If there are no spawn requests, return immediately, if we are client the SpawnRequests should always be empty. if (m_SpawnRequests.size() == 0) { return; } auto playerSpawns = m_World->GetComponents("PlayerSpawn"); if (playerSpawns == nullptr) { return; } int numSpawnedPlayers = 0; int playersSpectating = 0; const int numRequestsToHandle = (int)m_SpawnRequests.size(); for (auto it = m_SpawnRequests.begin(); it != m_SpawnRequests.end(); ++it) { // It is valid if they didn't pick class yet // but don't spawn anything, goto next spawnrequest. if (it->Class == PlayerClass::None) { ++playersSpectating; continue; } for (auto& cPlayerSpawn : *playerSpawns) { EntityWrapper spawner(m_World, cPlayerSpawn.EntityID); if (!spawner.HasComponent("Spawner")) { continue; } // If the spawner has a team affiliation, check it if (spawner.HasComponent("Team")) { auto cSpawnerTeam = spawner["Team"]; if ((int)cSpawnerTeam["Team"] != it->Team) { // If they somehow has a valid class as spectator, don't spawn them. if (it->Team == cSpawnerTeam["Team"].Enum("Spectator")) { ++playersSpectating; break; } else { continue; } } } // TODO: Choose a different spawner depending on class picked? std::string playerEntityFile; if (it->Class == PlayerClass::Assault) { playerEntityFile = (const std::string&)cPlayerSpawn["AssaultFile"]; } else if (it->Class == PlayerClass::Defender) { playerEntityFile = (const std::string&)cPlayerSpawn["DefenderFile"]; } else if (it->Class == PlayerClass::Sniper) { playerEntityFile = (const std::string&)cPlayerSpawn["SniperFile"]; } // Spawn the player! EntityWrapper player = SpawnerSystem::SpawnEntityFile(playerEntityFile, spawner, EntityWrapper::Invalid, "Player"); // Set the player team affiliation player["Team"]["Team"] = it->Team; // Publish a PlayerSpawned event Events::PlayerSpawned e; e.PlayerID = it->PlayerID; e.Player = player; e.Spawner = spawner; m_EventBroker->Publish(e); ++numSpawnedPlayers; it = m_SpawnRequests.erase(it); break; } if (it == m_SpawnRequests.end()) { break; } } if (numSpawnedPlayers != numRequestsToHandle - playersSpectating) { LOG_DEBUG("%i players were supposed to be spawned, but only %i was successfully.", numRequestsToHandle - playersSpectating, numSpawnedPlayers); } else { std::string dbg = numSpawnedPlayers != 0 ? std::to_string(numSpawnedPlayers) + " players were spawned. " : ""; dbg += playersSpectating != 0 ? std::to_string(playersSpectating) + " players are spectating/picking class. " : ""; LOG_DEBUG(dbg.c_str()); } }
int main() { //state enum gameState { MENU, INGAME, EXITING } currentState(MENU); //launch into menu //window and framerate control sf::RenderWindow window(sf::VideoMode(800, 800), "SnakeFML"); window.setFramerateLimit(60); sf::Image icon; icon.loadFromFile("Icon.png"); window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr()); sf::Clock dtClock; //Objects Score score("KBZipaDeeDooDah.ttf"); World world("Grass.png", { 1000,1000 }); Snake snake(window,world,score,"SnakeHead.png"); MouseSpawner spawner(&window,"Mouse.png", snake); Button playButton([&]() {currentState = INGAME; }, "PlayButton.png"); Button quitButton([&]() {currentState = EXITING; }, "QuitButton.png"); Menu menu(window,{ playButton, quitButton }); while (window.isOpen()) { if (currentState == EXITING) { window.close(); continue; } auto dt(dtClock.restart().asSeconds()); sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) { currentState = EXITING; } switch (currentState) { case MENU: menu.handleEvent(event); break; case INGAME: if (event.type == sf::Event::KeyPressed) { switch (event.key.code) { //escape to pause case sf::Keyboard::Escape: currentState = MENU; break; default: break; } } break; default: break; } } window.clear(sf::Color(50,150, 255)); //because Sea! if (currentState == INGAME) { //update objects if in game (i.e. not paused) snake.update(dt); spawner.checkCollisions(); spawner.spawn(); } //draw the essentials (with the correct view) window.setView(snake.getView()); window.draw(world); spawner.draw(); window.draw(snake); //default view for score (and menu, if needed) window.setView(window.getDefaultView()); window.draw(score); if (currentState == MENU) window.draw(menu); window.display(); sf::sleep(sf::seconds(0)); } window.close(); return 0; };