int main(int argc, char** argv) { int status; char forking; message_t message; pid_t pid, cpid; ipc_t ipc; grid_t grid = gnew(); LOGPID("Using IPC method: %s.\n", IPC_METHOD); /* Before doing anything else, map data should be loaded! */ /* (so as to know the number of ants beforehand, at least */ if((status = loadGrid(grid, "configurationFile")) != NO_ERRORS) { printf("An error occurred while loading the configuration file\n"); exit(status); } /* This process will act as IPC server/simulation control */ sid = 1; /* Control has simulation ID 1 */ pid = getpid(); /* and it's pid, obviously */ LOGPID("Control process started.\n"); ipc = initServer(); /* Good! IPC server working. Let's spawn those ants. */ if (cpid = fork()) { /* Control code here */ int aux; if((aux = launchControl(ipc, grid)) != NO_ERROR){ printf("Simulation fail: %d\n", aux ); }else{ LOGPID("Simulation ended succesfully!\n"); } } else { /* Ants here */ do { sid++; pid = getpid(); ipc = initClient(); if (forking = (sid - 1 < grid->antsQuant)) forking = ((cpid = fork()) == 0); /* Child will keep forking */ } while (forking); /* We can do our own stuff now */ status = antLoop(ipc, grid); exit(status); } freeGrid(grid); }
GameScreen::GameScreen() { //put all the equipments needed in this level. Mirror::loadTexture(); LaserSource::loadTexture(); Target::loadTexture(); Photon::loadTexture("Red_Light.png"); loadGrid(); loadEquipment(); }
void Audio3DTest::initialize() { setMultiTouch(true); _font = Font::create("res/common/arial18.gpb"); // Load game scene from file Bundle* bundle = Bundle::create("res/common/box.gpb"); _scene = bundle->loadScene(); SAFE_RELEASE(bundle); // Get light node Node* lightNode = _scene->findNode("directionalLight1"); Light* light = lightNode->getLight(); // Initialize box model Node* boxNode = _scene->findNode("box"); Model* boxModel = boxNode->getModel(); Material* boxMaterial = boxModel->setMaterial("res/common/box.material"); boxMaterial->getParameter("u_lightColor")->setValue(light->getColor()); boxMaterial->getParameter("u_lightDirection")->setValue(lightNode->getForwardVectorView()); // Remove the cube from the scene but keep a reference to it. _cubeNode = boxNode; _cubeNode->addRef(); _scene->removeNode(_cubeNode); loadGrid(_scene); // Initialize cameraa Vector3 cameraPosition(5, 5, 1); if (Camera* camera = _scene->getActiveCamera()) { camera->getNode()->getTranslation(&cameraPosition); } _fpCamera.initialize(); _fpCamera.setPosition(cameraPosition); _scene->addNode(_fpCamera.getRootNode()); _scene->setActiveCamera(_fpCamera.getCamera()); _gamepad = getGamepad(0); GP_ASSERT(_gamepad); _gamepad->getForm()->setConsumeInputEvents(false); }
// Called before the application loop begins. Load data here and // not in the constructor so that common exceptions will be // automatically caught. void App::onInit() { GApp::onInit(); m_rainbowMode = true; setFrameDuration(1.0f / 60.0f); m_showHelp = true; m_guiFont = GFont::fromFile(System::findDataFile("console.fnt")); initializeAudio(); loadGrid(); // Call setScene(shared_ptr<Scene>()) or setScene(MyScene::create()) to replace // the default scene here. showRenderingStats = true; makeGUI(); // For higher-quality screenshots: // developerWindow->videoRecordDialog->setScreenShotFormat("PNG"); // developerWindow->videoRecordDialog->setCaptureGui(false); developerWindow->cameraControlWindow->moveTo(Point2(developerWindow->cameraControlWindow->rect().x0(), 0)); loadScene("Test Scene"); }
// on "init" you need to initialize your instance bool Optics2::init() { // 1. super init first if ( !Layer::init() ) return false; Size visibleSize = Director::getInstance()->getVisibleSize(); Point origin = Director::getInstance()->getVisibleOrigin(); // 3. add your codes below... // add a label shows "Hello World" // create and initialize a label createStarsBackground("Pictures/bigStar.png",20); createStarsBackground("Pictures/smallStar.png",50); base = Point(50,50); loadGrid(); drawGrid(); createMenu(); return true; }
bool App::onEvent(const GEvent& event) { // Handle super-class events if ((event.type == GEventType::KEY_DOWN) && (event.key.keysym.sym == GKey::F5)) { loadGrid(); } if ((event.type == GEventType::KEY_DOWN) && (event.key.keysym.sym == GKey::SPACE)) { m_automata.setPaused(!m_automata.paused()); } if ((event.type == GEventType::KEY_DOWN) && (event.key.keysym.sym == GKey::LEFT)) { m_automata.m_bpm = max(m_automata.m_bpm-1, 1); } if ((event.type == GEventType::KEY_DOWN) && (event.key.keysym.sym == GKey::RIGHT)) { m_automata.m_bpm = min(m_automata.m_bpm+1, 10000); } if ((event.type == GEventType::KEY_DOWN) && (event.key.keysym.sym == GKey('h'))) { m_showHelp = !m_showHelp; } if ((event.type == GEventType::KEY_DOWN) && (event.key.keysym.sym == GKey('t'))) { m_automata.m_displayMode = CellularAutomata::DisplayMode(1 - m_automata.m_displayMode); } if ((event.type == GEventType::KEY_DOWN) && (event.key.keysym.sym == GKey('r'))) { m_rainbowMode = !m_rainbowMode; } if (GApp::onEvent(event)) { return true; } // If you need to track individual UI events, manage them here. // Return true if you want to prevent other parts of the system // from observing this specific event. // // For example, // if ((event.type == GEventType::GUI_ACTION) && (event.gui.control == m_button)) { ... return true; } return false; }
const Grid& GridManager_Class::getGrid(const std::string& grid) { if (m_grids.find(grid) == m_grids.end()) loadGrid(grid); return m_grids[grid]; }
void File_v2::loadChart(QXmlStreamReader *stream) { MainWindow *mw = mMainWindow; CrochetTab *tab = 0; QString tabName = "", defaultSt = ""; while(!(stream->isEndElement() && stream->name() == "chart")) { stream->readNext(); QString tag = stream->name().toString(); if(tag == "name") { tabName = stream->readElementText(); } else if(tag == "style") { int style = stream->readElementText().toInt(); tab = mw->createTab((Scene::ChartStyle)style); mParent->mTabWidget->addTab(tab, ""); mParent->mTabWidget->widget(mParent->mTabWidget->indexOf(tab))->hide(); } else if(tag == "defaultSt") { defaultSt = stream->readElementText(); tab->scene()->mDefaultStitch = defaultSt; } else if(tag == "chartCenter") { qreal x = stream->attributes().value("x").toString().toDouble(); qreal y = stream->attributes().value("y").toString().toDouble(); stream->readElementText(); tab->blockSignals(true); tab->setShowChartCenter(true); tab->scene()->mCenterSymbol->setPos(x, y); tab->blockSignals(false); } else if(tag == "grid") { loadGrid(stream, tab->scene()); } else if(tag == "rowSpacing") { qreal width = stream->attributes().value("width").toString().toDouble(); qreal height = stream->attributes().value("height").toString().toDouble(); tab->scene()->mDefaultSize.setHeight(height); tab->scene()->mDefaultSize.setWidth(width); stream->readElementText(); //move to the next tag. } else if(tag == "cell") { loadCell(tab, stream); } else if(tag == "indicator") { loadIndicator(tab, stream); } else if(tag == "chartimage") { loadChartImage(tab, stream); } else if(tag == "group") { stream->readElementText().toInt(); //create an empty group for future use. QList<QGraphicsItem*> items; tab->scene()->group(items); } else if(tag == "guidelines") { QString type = stream->attributes().value("type").toString(); int rows = stream->attributes().value("rows").toString().toInt(); int columns = stream->attributes().value("columns").toString().toInt(); int cellHeight = stream->attributes().value("cellHeight").toString().toInt(); int cellWidth = stream->attributes().value("cellWidth").toString().toInt(); tab->scene()->mGuidelines.setType(type); tab->scene()->mGuidelines.setColumns(columns); tab->scene()->mGuidelines.setRows(rows); tab->scene()->mGuidelines.setCellWidth(cellWidth); tab->scene()->mGuidelines.setCellHeight(cellHeight); stream->readElementText(); //move to the next tag tab->scene()->updateGuidelines(); emit tab->updateGuidelines(tab->scene()->guidelines()); } else if (tag == "chartLayer") { QString name = stream->attributes().value("name").toString(); unsigned int uid = stream->attributes().value("uid").toString().toUInt(); bool visible = stream->attributes().value("visible").toString().toInt(); tab->scene()->addLayer(name, uid); tab->scene()->getLayer(uid)->setVisible(visible); tab->scene()->selectLayer(uid); stream->readElementText(); //move to the next tag. } else if (tag == "size") { qreal x = stream->attributes().value("x").toString().toDouble(); qreal y = stream->attributes().value("y").toString().toDouble(); qreal width = stream->attributes().value("width").toString().toDouble(); qreal height = stream->attributes().value("height").toString().toDouble(); QRectF size = QRectF(x, y, width, height); tab->scene()->setSceneRect(size); stream->readElementText(); } else { qWarning() << "loadChart Unknown tag:" << tag; } } //refresh the layers so the visibility and selectability of items is correct tab->scene()->refreshLayers(); tab->updateRows(); int index = mParent->mTabWidget->indexOf(tab); mParent->mTabWidget->setTabText(index, tabName); mParent->mTabWidget->widget(mParent->mTabWidget->indexOf(tab))->show(); tab->scene()->updateSceneRect(); if(tab->scene()->hasChartCenter()) { tab->view()->centerOn(tab->scene()->mCenterSymbol->sceneBoundingRect().center()); } else { tab->view()->centerOn(tab->scene()->itemsBoundingRect().center()); } }
void File_v1::loadChart(QXmlStreamReader *stream) { MainWindow *mw = qobject_cast<MainWindow*>(mParent->mParent); CrochetTab *tab = 0; QString tabName = "", defaultSt = ""; while(!(stream->isEndElement() && stream->name() == "chart")) { stream->readNext(); QString tag = stream->name().toString(); if(tag == "name") { tabName = stream->readElementText(); } else if(tag == "style") { int style = stream->readElementText().toInt(); tab = mw->createTab((Scene::ChartStyle)style); mParent->mTabWidget->addTab(tab, ""); mParent->mTabWidget->widget(mParent->mTabWidget->indexOf(tab))->hide(); } else if(tag == "defaultSt") { defaultSt = stream->readElementText(); tab->scene()->mDefaultStitch = defaultSt; } else if(tag == "chartCenter") { qreal x = stream->attributes().value("x").toString().toDouble(); qreal y = stream->attributes().value("y").toString().toDouble(); stream->readElementText(); tab->blockSignals(true); tab->setShowChartCenter(true); tab->scene()->mCenterSymbol->setPos(x, y); tab->blockSignals(false); } else if(tag == "grid") { loadGrid(stream, tab->scene()); } else if(tag == "rowSpacing") { qreal width = stream->attributes().value("width").toString().toDouble(); qreal height = stream->attributes().value("height").toString().toDouble(); tab->scene()->mDefaultSize.setHeight(height); tab->scene()->mDefaultSize.setWidth(width); } else if(tag == "cell") { loadCell(tab, stream); } else if(tag == "indicator") { loadIndicator(tab, stream); } else if(tag == "group") { stream->readElementText().toInt(); //create an empty group for future use. QList<QGraphicsItem*> items; tab->scene()->group(items); } } tab->updateRows(); int index = mParent->mTabWidget->indexOf(tab); mParent->mTabWidget->setTabText(index, tabName); mParent->mTabWidget->widget(mParent->mTabWidget->indexOf(tab))->show(); tab->scene()->updateSceneRect(); if(tab->scene()->hasChartCenter()) { tab->view()->centerOn(tab->scene()->mCenterSymbol->sceneBoundingRect().center()); } else { tab->view()->centerOn(tab->scene()->itemsBoundingRect().center()); } }
//Execute par le thread principal (controleur) int main (int argc, char **argv) { if (argc != 6) return EXIT_FAILIURE; signal(SIGALRM, sigHandler); std::string pathGrilleVide = argv[1]; std::string pathGrilleSolution = argv[2]; std::string pathArrivee = argv[3]; int tempsMax = atoi(argv[4]); pathResultat = argv[5]; int grille[9][9]; int solution[9][9]; mainThread = pthread_self(); loadGrid(pathGrilleVide, grille); // printGrid(grille); loadGrid(pathGrilleSolution, solution); // printGrid(solution); for (int i = 0; i < 5; i++) { joueurs[i] == 0; } //creaation des thread joueur par defaut joueurs[0] = new Joueur(); joueurs[1] = new Joueur(); joueurs[2] = new Joueur(); joueurs[0]->thread = new pthread_t(); joueurs[1]->thread = new pthread_t(); joueurs[2]->thread = new pthread_t(); int un=1; int deux=2; int trois=3; pthread_create(joueurs[0]->thread, NULL, jouer, &un); pthread_create(joueurs[1]->thread, NULL, jouer, &deux); pthread_create(joueurs[2]->thread, NULL, jouer, &trois); joueurs[0]->tid = 1; joueurs[1]->tid = 2; joueurs[2]->tid = 3; joueurs[0]->etat = "Inconnu"; joueurs[1]->etat = "Inconnu"; joueurs[2]->etat = "Inconnu"; listeJoueurs.insert(std::pair<int, Joueur*>(joueurs[0]->tid, joueurs[0])); listeJoueurs.insert(std::pair<int, Joueur*>(joueurs[1]->tid, joueurs[1])); listeJoueurs.insert(std::pair<int, Joueur*>(joueurs[2]->tid, joueurs[2])); // Creation des deux autres thread pthread_t accueil_t; pthread_t alarm_t; pthread_create(&accueil_t, NULL, accueil, (void*)pathArrivee.c_str()); pthread_create(&alarm_t, NULL, minuterie, (void*)&tempsMax); sem_init(&file1_sem, 0, 0); int* empty = findEmpty(grille); int col = 0, ln = 0; do { //============================================================ // BOUCLE POUR TROUVER LES ZERO ET LES ENVOYER DANS LA FILE 1 if (empty == 0) break; if (pthread_mutex_trylock(&file1_lock) == 0) { // std::cout<<"je prend le mutex pour le broadcast"<<std::endl; if (file1.size() < 4) { MessageCJ* msg = new MessageCJ(); msg->colonne = empty[0]; msg->ligne = empty[1]; bool duplicate = false; std::queue<MessageCJ*> temp; while(!file1.empty()) { temp.push(new MessageCJ((*file1.front()))); file1.pop(); } while(!temp.empty()) { MessageCJ* tmpMsg = new MessageCJ((*temp.front())); file1.push(tmpMsg); temp.pop(); if (tmpMsg->colonne == msg->colonne && tmpMsg->ligne == msg->ligne) duplicate = true; } if (!duplicate) { std::list<int> opts = getOptions(grille, msg->colonne, msg->ligne); msg->choiceList = opts; file1.push(msg); pthread_cond_broadcast(&nonEmpty); // std::cout<<"je broadcast "<<file1.size()<<std::endl; // sem_post(&file1_sem); } delete empty; col = msg->colonne; ln = msg->ligne; } pthread_mutex_unlock( &file1_lock ); // std::cout<<"je rend le mutex pour le broadcast "<<file2.size()<<std::endl; } //========================== //BOUCLE POUR LIRE LA FILE 2 if(pthread_mutex_trylock(&file2_lock) == 0) { if (file2.size() > 0) { _MessageJC* msg = new _MessageJC((*file2.front())); file2.pop(); if (listeJoueurs.find(msg->tid)->second->etat != "Elimine") { // std::cout<<"ANS = "<<msg->choice<<" ? "<<solution[msg->colonne][msg->ligne]<<std::endl; // if (msg->choice == 0) // std::cout<<"LE CIEL NOUS TOMBE SUR LA TETE PAR TOUTATIS!!!"<<std::endl; //pthread_cond_broadcast(&nonFullFile2); if (solution[msg->colonne][msg->ligne] == msg->choice) { //If win! grille[msg->colonne][msg->ligne] = msg->choice; Joueur* vainqueur = listeJoueurs.find(msg->tid)->second; vainqueur->score++; } else { //if noob! Joueur* looser = listeJoueurs.find(msg->tid)->second; looser->score--; looser->nbErreur++; if (looser->score <= -10) { eliminateLooser(&listeJoueurs, msg->tid, joueurs); std::cout<<"Better luck next time, NOOB! "<<msg->tid<<std::endl; } } } pthread_mutex_unlock(&file2_lock); } else { pthread_mutex_unlock(&file2_lock); sleep(1); } } pthread_mutex_lock(&nouveauJoueurs_lock); if (nouveauxJoueurs.size() > 0) { if (nouveauxJoueurs.front() == 0) pthread_cancel(accueil_t); else if (playerCount < 5 ) { for (int i = 0; i < 5 && playerCount < 5 && nouveauxJoueurs.size() > 0; i++) { if (joueurs[i] == 0) { int id = nouveauxJoueurs.front(); if (id == 0) pthread_cancel(accueil_t); else { nouveauxJoueurs.pop(); joueurs[i] = listeJoueurs.find(id)->second; playerCount++; joueurs[i]->etat = "Inconnu"; pthread_create(joueurs[i]->thread, NULL, jouer, &joueurs[i]->tid); } } } } } pthread_mutex_unlock(&nouveauJoueurs_lock); empty = findEmpty(grille, col, ln); //printGrid(grille); // std::cout<<std::endl; } while (empty != 0 && nbJoueurActifs() > 0); // Si on se rend ici, soit tous les joueurs sont morts, soit on a fini la grille int max = 0; for (std::map<int, Joueur*>::iterator it = listeJoueurs.begin(); it != listeJoueurs.end(); it++) { if (it->second->etat == "Inconnu") { if (it->second->score > max) max = it->second->score; } } for (std::map<int, Joueur*>::iterator it = listeJoueurs.begin(); it != listeJoueurs.end(); it++) { if (it->second->etat == "Inconnu") { if (it->second->score == max) it->second->etat = "Vainqueur"; else it->second->etat = "Perdant"; } } pthread_cancel(accueil_t); pthread_cancel(alarm_t); writeResults(); dispose(); return EXIT_SUCCESS; }