Example #1
0
bool
CompositeShape::hit ( const Ray& ray, interval_t& tmin, HitRecord& hitrec ) const
{
	bool is_hit = false;
	HitRecord tmprec(hitrec);
	
	// check bbox first
	float tmin_bbox = 0;
	// if ( ! Shape::bbox().hit(ray,tmin_bbox,tmprec) )
	// 	{
	// 		return false;
	// 	}
	// go through all shapes and check for hits
	for ( unsigned i=0; i < shapes_.size(); ++i )
	{
		if ( shapes_[i]->hit(ray, tmin, tmprec) )
		{
			is_hit = true;
		}
	}
	
	// we don't want to change the original hitrec for every shape,
	// so we only store the values there once to save time
	if ( is_hit )
		hitrec = tmprec;
	
	return is_hit;
}
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();
		}
}