Beispiel #1
0
std::set<int> *MultiThreadFactorAlgorithm::GetFactor(unsigned int number)
{
	std::set<int> *resultOutput = getFactorByZeroOrOne(number);
	if (resultOutput != nullptr)
		return resultOutput;
	unsigned int numberInput = number / 2;
	int *result = new int[numberInput];
	std::thread firstThread(subThreadAlgorithm, result, number, 0, numberInput / 2);
	std::thread secondThread(subThreadAlgorithm, result, number, numberInput / 2, numberInput);
	firstThread.join();
	secondThread.join();
	resultOutput = marshalResult(result, numberInput);
	delete[] result;
	return resultOutput;
}
Beispiel #2
0
int main(int argc, const char * argv[]) {
    //argc siempre va a ser 1 porque el nombre del programa se pasa como primer parámetro
    if ( argc != 3 )//Si no se pasan dos argumentos al programa, hay un error
    {
      fprintf( stderr, "Progam takes 2 parameters\n");
      exit(-1);
    }
    if ( atoi(argv[1]) < atoi(argv[2]) )//En la combinatoria, n debe ser mayor a k
    {
      fprintf( stderr, "n debe ser >= k\n");
      exit(-1);
    }
    struct thread_args args;//Declarar el struct que empaqueta los dos argumentos
    args.n =  atoi(argv[1]);
    args.k = atoi(argv[2]);
    firstThread();
    /*
    Después del primer thread se debe hacer un fork, ya que la función execlp que se debe ejecutar
    crea un proceso hijo que ejecuta un bash y termina al padre, por lo que si se hiciera execlp
    dentro de una pthread, se ejecutaría el comando pero terminaría el programa padre. Al hacer un fork
    hacemos que un proceso hijo ejecute el comando y cuando termina ese proceso hijo, no le va a pasar
    nada al padre (principal)
    */
    int pid;
    pid = fork();//iniciar fork desde esta linea
    if (pid < 0)//si pid < 0 hubo un problema con el fork
    {
      fprintf( stderr, "Fork Failed");
      exit(-1);
    }
    else if (pid == 0)//es el primer fork
    {
      execlp( "/bin/ps", "ps", "-U", "root", "-u" ,"root" ,"u", (char *) NULL);//ejecutar el comando
      /*Si se pone cualquier código en este espacio no se va a ejecutar porque execlp
      va a haber terminado el fork padre (pid = 0)
      */
    }
    else//Seguir ejecutando el proceso padre
    {
      wait( NULL );//Esperar a que termina el fork hijo
      printf("==================\n");
      secondThread(args);
      printf("La combinatoria de %d en %d es %d\n", args.n, args.k, combRes);//Imprimir el resultado obtenido por el segundo thread
      exit(0);
    }
    return 0;
}
void MapEditor::editorWindow()
{
	sf::Thread secondThread(&chipsetWindow);

	sf::RenderWindow App(sf::VideoMode(800,600,32), "Eternal Elucidation Map Editor");
	App.SetFramerateLimit(FRAME_RATE);
	App.SetPosition(0,0);

	cp::cpGuiContainer myGUI;

	// *** Create a Selection Box and populate it with choices ***
	cp::cpSelectionBox terrainTypes(&App, &myGUI, 578, 55, 220, 150);

	std::string choices[] = {
		"normal",
		"water",
		"mud",
		"sand",
		"stairsLeftUp",
		"stairsLeftDown",
		"stairsRightUp",
		"stairsRightDown",
		"stairsUpUp",
		"stairsUpDown",
		"stairsDownUp",
		"stairsDownDown"};

		for(int t=0; t < 9; t++)
			terrainTypes.AddChoice(choices[t]);

		terrainTypes.SetFontSize(12);

		sf::String terrainTypesString("Terrain Types: ", sf::Font::GetDefaultFont(), 12);
		terrainTypesString.SetColor(sf::Color::Black);
		terrainTypesString.SetPosition(580, 35);

		cp::cpButton btnColGrid(&App, &myGUI, "Collsion Grid", 20, 20, 70, 30);
		cp::cpButton btnPlaceTile(&App, &myGUI, "Place Tile", 120, 20, 70, 30);
		cp::cpButton btnPlaceCol(&App, &myGUI, "Place Collision", 203, 20, 70, 30);
		cp::cpButton btnTerrainType(&App, &myGUI, "Assign Terrain Type", 314, 20, 70, 30);

		//Queue Buttons
		sf::Image leftTileQueueBtnImage;
		if(!leftTileQueueBtnImage.LoadFromFile("Assets/lefttilequeuebtn.png"))
			assert(99);
		sf::Image rightTileQueueBtnImage;
		if(!rightTileQueueBtnImage.LoadFromFile("Assets/righttilequeuebtn.png"))
			assert(99);

		cp::cpImageButton leftTileQueueBtn(&App, &myGUI, &leftTileQueueBtnImage, 0, 475);
		cp::cpImageButton rightTileQueueBtn(&App, &myGUI, &rightTileQueueBtnImage, 609, 475);

		btnColGrid.SetFontSize(12);
		btnPlaceTile.SetFontSize(12);
		btnPlaceCol.SetFontSize(12);
		btnTerrainType.SetFontSize(12);

		sf::Image mapBoxImage;
		sf::Image currentTilePlaceHolderImage;

		if( !mapBoxImage.LoadFromFile("Assets/mapbox.png") )
			assert(98);
		if( !currentTilePlaceHolderImage.LoadFromFile("Assets/currenttileplaceholder.png") )
			assert(97);

		sf::Sprite mapBoxSprite;
		sf::Sprite currentTilePlaceHolderSprite;

		mapBoxSprite.SetImage(mapBoxImage);
		mapBoxSprite.SetPosition(20, 55);
		currentTilePlaceHolderSprite.SetImage(currentTilePlaceHolderImage);
		currentTilePlaceHolderSprite.SetPosition(625, 210);

		//holds the five current tiles in the queue
		//offset the buttons so that the boarders of the left arrow 
		//dont overlap left most button. This is 29 units to the right
		//on the x axis.
		cp::cpImageButton tileQueuebtn1(&App, &myGUI, &currentTilePlaceHolderImage, (29 + (0 * 116)), 475);
		cp::cpImageButton tileQueuebtn2(&App, &myGUI, &currentTilePlaceHolderImage, (29 + (1 * 116)), 475);
		cp::cpImageButton tileQueuebtn3(&App, &myGUI, &currentTilePlaceHolderImage, (29 + (2 * 116)), 475);
		cp::cpImageButton tileQueuebtn4(&App, &myGUI, &currentTilePlaceHolderImage, (29 + (3 * 116)), 475);
		cp::cpImageButton tileQueuebtn5(&App, &myGUI, &currentTilePlaceHolderImage, (29 + (4 * 116)), 475);

		sf::Image current_tiles_chipset;
		if( !current_tiles_chipset.LoadFromFile("Assets/3.png") )
			assert(99);

		sf::Sprite current_tile_sprite;
		current_tile_sprite.SetImage( current_tiles_chipset );
		current_tile_sprite.SetPosition(630, 210);
		current_tile_sprite.SetSubRect( sf::IntRect(0, 0, 0, 0 ) );

		//After everything in this thread has loaded then run
		//the next thread. This is so that resource conflicts
		//opening and writing conflicts don't occure.
		secondThread.Launch(); 

		cp::cpTextInputBox chipSetNameTxb(&App, &myGUI, "", 660, 253, 135, 12);
		cp::cpTextInputBox tileIDTxb(&App, &myGUI, "", 660, 266, 135, 12);
		cp::cpTextInputBox srcXTxb(&App, &myGUI, "", 660, 279, 135, 12);
		cp::cpTextInputBox srcYTxb(&App, &myGUI, "", 660, 292, 135, 12);
		cp::cpTextInputBox tileWidthTxb(&App, &myGUI, "", 660, 305, 135, 12);
		cp::cpTextInputBox tileHeightTxb(&App, &myGUI, "", 660, 318, 135, 12);
		cp::cpTextInputBox rotationTxb(&App, &myGUI, "", 660, 331, 135, 12);
		cp::cpTextInputBox levelTxb(&App, &myGUI, "", 660, 344, 135, 12);
		cp::cpTextInputBox terrainTypeTxb(&App, &myGUI, "", 660, 357, 135, 12);

		sf::String tilePropertiesString("Tile Properties: ", sf::Font::GetDefaultFont(), 12);
		tilePropertiesString.SetColor(sf::Color::Black);
		tilePropertiesString.SetPosition(630, 325);

		sf::String chipSetNameString("Chipset Name: ", sf::Font::GetDefaultFont(), 12);
		sf::String tileIDString("Tile ID: ", sf::Font::GetDefaultFont(), 12);
		sf::String srcXString("Src X: ", sf::Font::GetDefaultFont(), 12);
		sf::String srcYString("Src Y: ", sf::Font::GetDefaultFont(), 12);
		sf::String tileWidthString("Tile Width: ", sf::Font::GetDefaultFont(), 12);
		sf::String tileHeightString("Tile Height: ", sf::Font::GetDefaultFont(), 12);
		sf::String rotationString("Rotation: ", sf::Font::GetDefaultFont(), 12);
		sf::String levelString("Level: ", sf::Font::GetDefaultFont(), 12);
		sf::String terrainTypeString("Terrain Type: ", sf::Font::GetDefaultFont(), 12);

		sf::String chipSetNameValueString("", sf::Font::GetDefaultFont(), 12);
		sf::String tileIDValueString("", sf::Font::GetDefaultFont(), 12);
		sf::String srcXValueString("", sf::Font::GetDefaultFont(), 12);
		sf::String srcYValueString("", sf::Font::GetDefaultFont(), 12);
		sf::String tileWidthValueString("", sf::Font::GetDefaultFont(), 12);
		sf::String tileHeightValueString("", sf::Font::GetDefaultFont(), 12);
		sf::String rotationValueString("", sf::Font::GetDefaultFont(), 12);
		sf::String levelValueString("", sf::Font::GetDefaultFont(), 12);
		sf::String terrainTypeValueString("", sf::Font::GetDefaultFont(), 12);

		chipSetNameString.SetColor(sf::Color::Black);
		tileIDString.SetColor(sf::Color::Black);
		srcXString.SetColor(sf::Color::Black);
		srcYString.SetColor(sf::Color::Black);
		tileWidthString.SetColor(sf::Color::Black);
		tileHeightString.SetColor(sf::Color::Black);
		rotationString.SetColor(sf::Color::Black);
		levelString.SetColor(sf::Color::Black);
		terrainTypeString.SetColor(sf::Color::Black);

		chipSetNameValueString.SetColor(sf::Color::Black);
		tileIDValueString.SetColor(sf::Color::Black);
		srcXValueString.SetColor(sf::Color::Black);
		srcYValueString.SetColor(sf::Color::Black);
		tileWidthValueString.SetColor(sf::Color::Black);
		tileHeightValueString.SetColor(sf::Color::Black);
		rotationValueString.SetColor(sf::Color::Black);
		levelValueString.SetColor(sf::Color::Black);
		terrainTypeValueString.SetColor(sf::Color::Black);

		chipSetNameString.SetPosition(580, 337);
		tileIDString.SetPosition(580, 350);
		srcXString.SetPosition(580, 363);
		srcYString.SetPosition(580, 376);
		tileWidthString.SetPosition(580, 389);
		tileHeightString.SetPosition(580, 402);
		rotationString.SetPosition(580, 415);
		levelString.SetPosition(580, 427);
		terrainTypeString.SetPosition(580, 440);

		chipSetNameValueString.SetPosition(700, 337);
		tileIDValueString.SetPosition(700, 350);
		srcXValueString.SetPosition(700, 363);
		srcYValueString.SetPosition(700, 376);
		tileWidthValueString.SetPosition(700, 389);
		tileHeightValueString.SetPosition(700, 402);
		rotationValueString.SetPosition(700, 415);
		levelValueString.SetPosition(700, 427);
		terrainTypeValueString.SetPosition(700, 440);

		const sf::Input& input = App.GetInput();
		//stores which button in the tile queue has been pressed.
		int selectedTileQueueBtn = 0;
		//tigers if a button in the tile queue has been pressed.
		bool tileQueueBtnPress = false;
		while(App.IsOpened())
		{
			// The standard Event loop
			sf::Event Event;
			const sf::Input& input = App.GetInput();
			while(App.GetEvent(Event))
			{
				if(Event.Type == sf::Event::Closed)
					App.Close();
				if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
					App.Close();

				// This is the function that takes care of which control
				// has focus out of all the controls registered to your
				// GuiContainer.
				myGUI.ProcessKeys(&Event);
			}

			int selection; // an int to store the selection choice from
			// our selection box & drop down box

			if(terrainTypes.CheckState(&input) == cp::CP_ST_MOUSE_LBUTTON_RELEASED)
			{
				selection = terrainTypes.GetSelection();
				if(selection == 10)
					terrainTypes.RemoveLastChoice();
			}

			if(leftTileQueueBtn.CheckState(&input) == cp::CP_ST_MOUSE_LBUTTON_RELEASED)
			{
				if( tileQueueIndex != 0 )
				{
					tileQueueIndex--;
					setTileQueueBtns( &tileQueuebtn1,
						&tileQueuebtn2,
						&tileQueuebtn3,
						&tileQueuebtn4,
						&tileQueuebtn5,
						currentTilePlaceHolderImage);
				}
			}
			if(rightTileQueueBtn.CheckState(&input) == cp::CP_ST_MOUSE_LBUTTON_RELEASED)
			{
				//if the tile queue size is greater than 5 tiles
				//then we can move to the right.
				if( tileQueueIndex + 5 < tileQueue.size())
				{
					tileQueueIndex++;
					setTileQueueBtns( &tileQueuebtn1,
						&tileQueuebtn2,
						&tileQueuebtn3,
						&tileQueuebtn4,
						&tileQueuebtn5,
						currentTilePlaceHolderImage);
				}
			}
			if( tileQueuebtn1.CheckState(&input) == cp::CP_ST_MOUSE_LBUTTON_RELEASED)
			{
				selectedTileQueueBtn = 0;
				tileQueueBtnPress = true;
			}
			if( tileQueuebtn2.CheckState(&input) == cp::CP_ST_MOUSE_LBUTTON_RELEASED)
			{
				selectedTileQueueBtn = 1;
				tileQueueBtnPress = true;
			}
			if( tileQueuebtn3.CheckState(&input) == cp::CP_ST_MOUSE_LBUTTON_RELEASED)
			{
				selectedTileQueueBtn = 2;
				tileQueueBtnPress = true;
			}
			if( tileQueuebtn4.CheckState(&input) == cp::CP_ST_MOUSE_LBUTTON_RELEASED)
			{
				selectedTileQueueBtn = 3;
				tileQueueBtnPress = true;
			}
			if( tileQueuebtn5.CheckState(&input) == cp::CP_ST_MOUSE_LBUTTON_RELEASED)
			{
				selectedTileQueueBtn = 4;
				tileQueueBtnPress = true;
			}

			if(btnColGrid.CheckState(&input) == cp::CP_ST_MOUSE_LBUTTON_RELEASED)
			{
				secondThread.Launch(); 
			}
			if(btnPlaceTile.CheckState(&input) == cp::CP_ST_MOUSE_LBUTTON_RELEASED)
				App.Close();
			if(btnPlaceCol.CheckState(&input) == cp::CP_ST_MOUSE_LBUTTON_RELEASED)
				App.Close();
			if(btnTerrainType.CheckState(&input) == cp::CP_ST_MOUSE_LBUTTON_RELEASED)
				App.Close();

			std::string name;

			if( addTilePushed )
			{
				if( tileQueue.size() > 5 )
				{
					tileQueueIndex = tileQueue.size() - 5;
				}
				setTileQueueBtns( &tileQueuebtn1,
					&tileQueuebtn2,
					&tileQueuebtn3,
					&tileQueuebtn4,
					&tileQueuebtn5,
					currentTilePlaceHolderImage);
				
				addTilePushed = false;
			}

			if( tileQueueBtnPress && tileQueue.size() > 0 )
			{
				Tile *tmptile;
				if( selectedTileQueueBtn < tileQueue.size() )
				{
					tmptile = &tileQueue.at(tileQueueIndex + selectedTileQueueBtn);
				}
				current_tile_sprite.SetImage( chipsetImages.at( getChipsetIndex(tmptile->chipSetName ) ) );
				//set current tile sub rect
				sf::IntRect tmprec(tmptile->srcX, tmptile->srcY, 
					tmptile->srcX + tmptile->width, tmptile->srcY + tmptile->height);
				current_tile_sprite.SetSubRect(tmprec);
				chipSetNameValueString.SetText( tmptile->chipSetName );
				tileIDValueString.SetText( to_string(tmptile->tileID) );
				srcXValueString.SetText( to_string(tmptile->srcX) );
				srcYValueString.SetText( to_string(tmptile->srcY) );
				tileWidthValueString.SetText( to_string(tmptile->width) );
				tileHeightValueString.SetText( to_string(tmptile->height) );
				rotationValueString.SetText("0");
				levelValueString.SetText("1");
				terrainTypeValueString.SetText("normal");

				tileQueueBtnPress = false;
			}

			App.Clear(sf::Color(97,216,67));

			// Every object you create should have the Draw() function
			// called on every cycle.  If you don't want an object to
			// be visible, set Show(false) for that object.
			App.Draw(mapBoxSprite);
			App.Draw(currentTilePlaceHolderSprite);

			terrainTypes.Draw();

			leftTileQueueBtn.Draw();
			rightTileQueueBtn.Draw();
			btnColGrid.Draw();
			btnPlaceTile.Draw();
			btnPlaceCol.Draw();
			btnTerrainType.Draw();
			App.Draw(tilePropertiesString);

			App.Draw(chipSetNameString);
			App.Draw(tileIDString);
			App.Draw(srcXString);
			App.Draw(srcYString);
			App.Draw(tileWidthString);
			App.Draw(tileHeightString);
			App.Draw(rotationString);
			App.Draw(levelString);
			App.Draw(terrainTypeString);

			App.Draw(chipSetNameValueString);
			App.Draw(tileIDValueString);
			App.Draw(srcXValueString);
			App.Draw(srcYValueString);
			App.Draw(tileWidthValueString);
			App.Draw(tileHeightValueString);
			App.Draw(rotationValueString);
			App.Draw(levelValueString);
			App.Draw(terrainTypeValueString);

			//Draw tile queue buttons
			tileQueuebtn1.Draw();
			tileQueuebtn2.Draw();
			tileQueuebtn3.Draw();
			tileQueuebtn4.Draw();
			tileQueuebtn5.Draw();

			App.Draw( current_tile_sprite );

			App.Display();
		}
}