void Sprite2VertexArray(const sf::Sprite & sprite,sf::VertexArray & vertices)
    {
        sf::IntRect texBox = sprite.getTextureRect();
        sf::FloatRect box = sprite.getGlobalBounds();

		vertices.append(sf::Vertex(sf::Vector2f(box.left, box.top), sf::Vector2f((float)texBox.left, (float)texBox.top)));
		vertices.append(sf::Vertex(sf::Vector2f(box.left, box.top + box.height), sf::Vector2f((float)texBox.left, (float)(texBox.top + texBox.height))));
		vertices.append(sf::Vertex(sf::Vector2f(box.left + box.width, box.top + box.height), sf::Vector2f((float)(texBox.left + texBox.width), (float)(texBox.top + texBox.height))));
		vertices.append(sf::Vertex(sf::Vector2f(box.left + box.width, box.top), sf::Vector2f((float)(texBox.left + texBox.width), (float)texBox.top)));
    }
Exemple #2
0
void drawPoint( pair <sf::Vector2i, int> & pnt) {
	sf::Vertex vertex;
	vertex.color = sf::Color::Black;
	for (int i = pnt.first.x; i < pnt.first.x + pnt.second; i++) {
		for (int j = pnt.first.y; j < pnt.first.y + pnt.second; j++) {
			vertex.position = sf::Vector2f(i, j);
			draw_arr.append(vertex);
		}
	}
	cout << draw_arr.getVertexCount() << endl;
}
		/**
			Append a quad to a vertexarray. This version takes Texturecoordinates
		*/
		inline void AppendQuad(sf::VertexArray& vA, const sf::FloatRect& Pos, geom::irect coords, sf::Color _color) {
			sf::Vertex vs[4];
			SetQuadPos(vs, Pos);
			SetQuadTex(vs, coords);
			SetQuadColor(vs, _color);

			vA.append(vs[0]);
			vA.append(vs[1]);
			vA.append(vs[2]);
			vA.append(vs[3]);
		}
Exemple #4
0
int main() {
	sf::TcpSocket socket;
	//sf::RenderWindow * win = new sf::RenderWindow(sf::VideoMode(200, 200), "QQ");
	
	draw_arr.setPrimitiveType(sf::LinesStrip);

	int port = 8000;
	string name = "admin";
	string pass = "******";
	
	std::size_t received = 0;

	socket.connect("127.0.0.1", port);

	sf::Thread th(&workOnWin,  &socket);

	char ns[1];
	ns[0] = to_string(name.length()).c_str()[0];
	
	socket.send(ns, 1);
	socket.send(name.c_str(), name.size() + 1);
	//system("pause");
	
	socket.send(to_string(pass.length()).c_str(), 1);
	socket.send(pass.c_str(), pass.size() + 1);

	char ans[1];
	sf::Int8 t;
	sf::Packet p;
	if (socket.receive(ans, sizeof(ans), received) == sf::Socket::Done) {
		if (static_cast <int> (ans[0]) == 0) {
			cout << "Welcome" << endl;
			int t;
			cin >> t;
			if (t == 5) {
				ans[0] = 5;
				if (socket.send(ans, 1) == sf::Socket::Done) {
					socket.receive(ans, 1, received);
					if (static_cast <int> (ans[0]) == 0) {
						cout << "BOARD CREATE" << endl;

						th.launch();
					}
				}
			}
			if (t == 6) {
				ans[0] = 6;
				if (socket.send(ans, 1) == sf::Socket::Done) {
					socket.send(to_string(pass.length()).c_str(), 1);
					socket.send(name.c_str(), name.size() + 1);
					socket.receive(ans, 1, received);
					if (ans[0] == server_ok_code) {
						th.launch();
					}
				}
			}
			
		}
		else {
Exemple #5
0
bool load(const std::string& tileset, sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height)
{
	// load the tileset texture
	if (!m_tileset.loadFromFile(tileset))
		return false;

	// resize the vertex array to fit the level size
	m_vertices.setPrimitiveType(sf::Quads);
	m_vertices.resize(width * height * 4);

	// populate the vertex array, with one quad per tile
	for (unsigned int i = 0; i < width; ++i)
		for (unsigned int j = 0; j < height; ++j)
		{
			/*
			// get the current tile number
			int tileNumber = tiles[i + j * width];

			// find its position in the tileset texture
			int tu = tileNumber % (m_tileset.getSize().x / tileSize.x);
			int tv = tileNumber / (m_tileset.getSize().x / tileSize.x);
			*/
			// get a pointer to the current tile's quad
			sf::Vertex* quad = &m_vertices[(i + j * width) * 4];

			// define its 4 corners
			quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
			quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
			quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
			quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);

			quad[0].texCoords = sf::Vector2f(0, 0);
			quad[1].texCoords = sf::Vector2f(0, 32);
			quad[2].texCoords = sf::Vector2f(32, 0);
			quad[3].texCoords = sf::Vector2f(32, 32);
			/*
			// define its 4 texture coordinates
			quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
			quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
			quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
			quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);
			*/
		}

		return true;
}
Exemple #6
0
void fTilemap::drawTile(int tileIndex, sf::VertexArray &vertices) const 
	{	
		// posición en coordenadas globales de la esquina superior izquierda del tile
		sf::Vector2f tilePos = sf::Vector2f((tileIndex%_width)*_tileWidth, (tileIndex/_width)*_tileHeight);
		
		// coordenadas de textura de la esquina superior izquierda del tile
		sf::Vector2f tileCoords = sf::Vector2f(((_tiles[tileIndex]-1)%(_texture->getSize().x/_tileWidth))*_tileWidth, ((_tiles[tileIndex]-1)/(_texture->getSize().x/_tileWidth))*_tileHeight);
		
		// posición de los vertices del triángulo superior del tile
		vertices.append(sf::Vertex(sf::Vector2f(tilePos.x, tilePos.y+_tileHeight), sf::Color::White, sf::Vector2f(tileCoords.x, tileCoords.y+_tileHeight)));
		vertices.append(sf::Vertex(sf::Vector2f(tilePos.x+_tileWidth, tilePos.y), sf::Color::White, sf::Vector2f(tileCoords.x + _tileWidth, tileCoords.y)));
		vertices.append(sf::Vertex(sf::Vector2f(tilePos.x, tilePos.y), sf::Color::White, sf::Vector2f(tileCoords.x, tileCoords.y)));
		
		// posición de los vertices del triángulo inferior del tile
		vertices.append(sf::Vertex(sf::Vector2f(tilePos.x, tilePos.y+_tileHeight), sf::Color::White, sf::Vector2f(tileCoords.x, tileCoords.y+_tileHeight)));
		vertices.append(sf::Vertex(sf::Vector2f(tilePos.x+_tileWidth, tilePos.y+_tileHeight), sf::Color::White, sf::Vector2f(tileCoords.x+_tileWidth, tileCoords.y+_tileHeight)));
		vertices.append(sf::Vertex(sf::Vector2f(tilePos.x+_tileWidth, tilePos.y), sf::Color::White,  sf::Vector2f(tileCoords.x + _tileWidth, tileCoords.y)));
	}
Exemple #7
0
void set_vertexarray_to_dvectors(sf::VertexArray &va, DVector &xs, DVector &ys) {
	unsigned int i;
	vector<double>::const_iterator xit, yit;

	for (i = 0, xit = xs.get().begin(), yit = ys.get().begin();
		 i < va.getVertexCount() && xit != xs.get().end() && yit != ys.get().end();
		 i++, xit++, yit++) {
		va[i].position = sf::Vector2f(*xit, *yit);
	}
}
  void reset() override
  {
    following = nullptr;
    path.clear();
    smoothedPath.clear();
    positionInPath = 0;

#ifdef _DEBUG
    debugDraw.clear();
#endif
  }
Exemple #9
0
void
computeCatmullRomPoints
    (const T& P1, const T& P2, const T& P3, const T& P4,
        unsigned int numSeg, sf::VertexArray & line_points){
    line_points.append(P2);
    for (unsigned int j = 0; j < numSeg; ++j)
    {
        float  t = (float)j/(float)numSeg;
        float t2 = t*t;
        float t3 = t*t2;
        T P = 0.5f*
        ((-t3+2.f*t2-t)*P1 +
         (3.f*t3-5.f*t2+2.f)*P2 +
         (-3.f*t3+4.f*t2+t)*P3 +
         (t3-t2)*P4);
        //        addpoint2(P);
        //        line_points.append(P);
        line_points.append(P);
    }
    line_points.append(P3);
}
static void plot(sf::VertexArray& va, int x, int y, float c, const sf::Color& color)
{
    const sf::Color newColor(static_cast<sf::Uint8>(color.r * c),
                             static_cast<sf::Uint8>(color.g * c),
                             static_cast<sf::Uint8>(color.b * c));

    const sf::Vertex vertex(sf::Vector2f(static_cast<float>(x),
                                         static_cast<float>(y)),
                            newColor);

    va.append(vertex);
}
void Renderer::draw(sf::VertexArray const& vs, ZIndex_t zindex, sf::RenderStates const& states)
{
    auto bounds = vs.getBounds();
    if(isInScreen(bounds))
    {
        m_drawCallbacks.push_back({
            zindex,
            [&vs, states] (sf::RenderTarget& renderer)
            {
                renderer.draw(vs, states);
            }
        });
    }
}
void draw_box_old(sf::VertexArray& verts, const sf::FloatRect& rect, float thickness = 4.0f, const sf::Color color = sf::Color::Green) {
	float left = rect.left;
	float top = rect.top;
	float right = rect.left + rect.width;
	float bottom = rect.top + rect.height;
	// Add a quad for the current character
	verts.append(Vertex(Vector2f(left, top), color)); verts.append(Vertex(Vector2f(left, bottom), color));
	verts.append(Vertex(Vector2f(left, top), color)); verts.append(Vertex(Vector2f(right, top), color));
	verts.append(Vertex(Vector2f(right, bottom), color)); verts.append(Vertex(Vector2f(left, bottom), color));
	verts.append(Vertex(Vector2f(right, bottom), color)); verts.append(Vertex(Vector2f(right, top), color));
}
void Renderer::drawHUD(sf::VertexArray const& vs, sf::RenderStates const& states)
{
    sf::VertexArray v = vs;
    for (unsigned int i = 0; i < vs.getVertexCount(); ++i)
    {
        auto px = vs[i].position / 100.f;
        auto pw = m_renderer->mapPixelToCoords({static_cast<int>(px.x * m_screenRect.width), static_cast<int>(px.y * m_screenRect.height)});
        v[i].position = pw;
    }
    m_drawCallbacks.push_back({
        ZIndex_t(ZIndex::HUD),
        [v, states] (sf::RenderTarget& renderer)
        {
            renderer.draw(v, states);
        }
    });
}
Exemple #14
0
void set_vertexarray_to_color(sf::VertexArray &va, sf::Color c) {
	for (unsigned i = 0; i < va.getVertexCount(); i++) {
		va[i].color = c;
	}
}
Exemple #15
0
int		main()
{
	sf::Clock			clock;
	double				s = 0;
	
	double				viewZoom = 1.0f;

	srand(time(0));

	for (u32 i = 0; i < PN; i++)
	{
		particule[i]._px = rand() % WINX;
		particule[i]._py = rand() % WINY;
		if (INITIAL_SPEED_ACTIVATE)
		{
			particule[i]._mx = ((rand() % INITIAL_SPEED) - INITIAL_SPEED / 2) / INITIAL_SPEED_DIV;
			particule[i]._my = ((rand() % INITIAL_SPEED) - INITIAL_SPEED / 2) / INITIAL_SPEED_DIV;
		}
		else
		{
			particule[i]._mx = 0;
			particule[i]._my = 0;
		}
		particule[i]._mass = PAR_MASS;
		particule[i]._color = PCOLOR;
	}

	planet.setPosition(sf::Vector2f(WINX/2, WINY/2));
	planet.setOrigin(sf::Vector2f(10.0f, 10.0f));
	planet.setFillColor(sf::Color::Blue);

	win.create(sf::VideoMode(WINX, WINY), "ORBITE");
	win.setFramerateLimit(MAXFPS);
	clock.restart();
	while (win.isOpen())
	{
		s = clock.restart().asSeconds();
		s *= SPEED;

		sf::View view = win.getDefaultView();
		view.zoom(viewZoom);

		while (win.pollEvent(eve))
		{
			if (eve.type == sf::Event::Closed)
				win.close();
			if (eve.type == sf::Event::KeyPressed)
			{
				if (eve.key.code == sf::Keyboard::Add)
					viewZoom -= 0.25f;
				else if (eve.key.code == sf::Keyboard::Subtract)
					viewZoom += 0.25f;
				else if (eve.key.code == sf::Keyboard::Space)
				{
					trace.clear();
					for (u32 i = 0; i < PN; i++)
					{
						particule[i]._px = rand() % WINX;
						particule[i]._py = rand() % WINY;
						if (INITIAL_SPEED_ACTIVATE)
						{
							particule[i]._mx = ((rand() % INITIAL_SPEED) - INITIAL_SPEED / 2) / INITIAL_SPEED_DIV;
							particule[i]._my = ((rand() % INITIAL_SPEED) - INITIAL_SPEED / 2) / INITIAL_SPEED_DIV;
						}
						else
						{
							particule[i]._mx = 0;
							particule[i]._my = 0;
						}
						particule[i]._mass = PAR_MASS;
						particule[i]._color = PCOLOR;
					}		
				}
			}
		}
		
		planet.setPosition((sf::Vector2f)sf::Mouse::getPosition(win));
		for (u32 i = 0; i < PN; i++)
		{
			particule[i].updateToPosition(planet.getPosition(), PL_MASS, s);
			part[i].position = sf::Vector2f(particule[i]._px, particule[i]._py);
			part[i].color = particule[i]._color;
			if (ACTIVATE_ORBIT_TRACE)
			{
				sf::Vertex cpy = part[i];
				trace.append(cpy);
			}
		}
		win.setView(view);
		win.clear(sf::Color::Black);
		win.draw(planet);
		win.draw(part);
		win.draw(trace);
		win.display();
	}
	return (0);
}
Exemple #16
0
int main(int argc, char** argv)
{
	sf::RenderWindow window(sf::VideoMode(1000, 1000), "Physics Game!");

  delete world;
  CreateWorld();

  int currentLevel = 1;

  #pragma region Background Graphics
  // Load the background images, create a sprite and assign the image.
  // Sky
  sf::Texture backgroundTexture;
  backgroundTexture.loadFromFile("assets/Sky.png");
  backgroundTexture.setSmooth(true);
  backgroundTexture.setRepeated(true);
  sf::Sprite background;
  background.setPosition(0,0);
  background.setScale((float)window.getSize().x,1.0f);
  background.setTexture(backgroundTexture);

  // Sun
  sf::Texture sunTexture;
  sunTexture.loadFromFile("assets/Sun.png");
  sunTexture.setSmooth(true);
  sunTexture.setRepeated(true);
  sf::Sprite sun;
  sun.setPosition((window.getSize().x - (250*0.6f)) - window.getSize().x * 0.1f , 100);
  sun.setScale(0.6f,0.6f);
  sun.setTexture(sunTexture);

  // Cloud
  sf::Texture cloud1Texture;
  cloud1Texture.loadFromFile("assets/Cloud1.png");
  cloud1Texture.setSmooth(true);
  cloud1Texture.setRepeated(false);
  sf::Sprite cloud1;
  cloud1.setPosition(window.getSize().x*0.15f,80);
  cloud1.setScale(1.0f,1.0f);
  cloud1.setTexture(cloud1Texture);

  // Hills
  sf::Texture hillsTexture;
  hillsTexture.loadFromFile("assets/Hills.png");
  hillsTexture.setSmooth(false);
  hillsTexture.setRepeated(true);
  sf::Sprite hills1;
  hills1.setPosition(0,(float)window.getSize().y - 184);
  hills1.setScale(1.0f ,1.0f);
  hills1.setTexture(hillsTexture);
  sf::Sprite hills2;
  hills2.setPosition(766,(float)window.getSize().y - 184);
  hills2.setScale(1.0f,1.0f);
  hills2.setTexture(hillsTexture);
  sf::Sprite hills3;
  hills3.setPosition(766*2,(float)window.getSize().y - 184);
  hills3.setScale(1.0f,1.0f);
  hills3.setTexture(hillsTexture);
  #pragma endregion

	// Prepare for simulation. Typically we use a time step of 1/60 of a
	// second (60Hz) and 10 iterations. This provides a high quality simulation
	// in most game scenarios.
	float32 timeStep = 1.0f / 30.0f;
	int32 velocityIterations = 8;
	int32 positionIterations = 3;

  std::vector<shape> lines;

  sf::Texture wheel;
  wheel.loadFromFile("assets/Wheel.png");
  wheel.setSmooth(true);

  sf::Texture bikeWheel;
  bikeWheel.loadFromFile("assets/BikeWheel.png");
  bikeWheel.setSmooth(true);

  sf::Texture bikeFrame_t;
  bikeFrame_t.loadFromFile("assets/BikeFrame.png");
  bikeFrame_t.setSmooth(true);

  #pragma region events
  while (window.isOpen()) {

    static sf::Clock deltaClock;
    sf::Time deltaTime = deltaClock.restart();

    static float time;
    time += deltaTime.asSeconds() * 5;

    sf::Event event;
    while (window.pollEvent(event))
    {
      // If the window is closed, close the SFML window
      switch (event.type) {

      case sf::Event::Closed:
          window.close();
          break;

      // Resize window : set new size
      case sf::Event::Resized:
          window.setView(sf::View(sf::FloatRect(0.0f, 0.0f, (float)event.size.width, (float)event.size.height)));

          // Rescale and Reposition the background elements
          background.setScale((float)window.getSize().x,1.0f);
          sun.setPosition((window.getSize().x - (250*0.6f)) - window.getSize().x * 0.1f , 100);
          hills1.setPosition(0,(float)window.getSize().y - 184);
          hills2.setPosition(766,(float)window.getSize().y - 184);
          hills3.setPosition(766*2,(float)window.getSize().y - 184);
          break;

      case sf::Event::MouseButtonPressed:
          static bool pressed = false;
          static vector2 start,end;

          // When we click save the positions to a Vector2
          if(event.mouseButton.button == sf::Mouse::Left) {

          //printf("%f  -  %f\n", (float)event.mouseButton.x, (float)event.mouseButton.y);

            // If we are drawing lines constantly, dont perform click drawing
            if(!pressed) {
              start.x = (float)event.mouseButton.x;
              start.y = (float)event.mouseButton.y;
              pressed = true;
            } else {
              end.x = (float)event.mouseButton.x;
              end.y = (float)event.mouseButton.y;

              if(start == end) {
                pressed = false;
                break;
              }

              // Create the SFML visual box based on click locations
              shape sf_shape = shape(start,end, 2);
              lines.push_back(sf_shape);
        
              vector2 center = center.GetCenter(start, end);

              // Create the Box2D physics box.
              static b2FixtureDef myFixture;
              static b2BodyDef myBody;
              myBody.type = b2_staticBody;
              myBody.position.Set(center.x, center.y);
              b2Body* body = world->CreateBody(&myBody);
              b2Vec2 vertices[4];

              // Caluclate the positions of the Box2D Verts
              sf_shape.Box2DVertPos();
              vertices[0].Set(sf_shape.a.x * SCALE -start.x*0.5f, sf_shape.a.y * SCALE -start.y*0.5f);
              vertices[3].Set(sf_shape.b.x * SCALE -start.x*0.5f, sf_shape.b.y * SCALE -start.y*0.5f);
              vertices[2].Set(sf_shape.c.x * SCALE -start.x*0.5f, sf_shape.c.y * SCALE -start.y*0.5f);
              vertices[1].Set(sf_shape.d.x * SCALE -start.x*0.5f, sf_shape.d.y * SCALE -start.y*0.5f);

              b2PolygonShape polyShape;
              polyShape.Set(vertices, 4);
                 
              myFixture.shape = &polyShape;
              myFixture.density = 1;
              myFixture.friction = 1;
              body->CreateFixture(&myFixture);

              pressed = false;
            }
          }
        }
    }
    #pragma endregion

    if(currentLevel == 2) l1.Update();

    #pragma region Keyboard_Input__Reset
    // RESET ALL
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape)) {
      // Clear all SFML Lines
      lines.clear();
      // Pause the game
      paused = true;
      // Reset the World
      delete world;
      LevelLoad(currentLevel);
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::R)) {
      // Reset the Level
      delete world;
      LevelLoad(currentLevel);

      // Recreate the physics on all the lines
      RecreateLines(lines);
      int x = 9;
      // Pause the game
      paused = true;
    }
    else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num1)) {
      // Clear all SFML Lines
      lines.clear();
      // Pause the game
      paused = true;
      // Reset the World
      delete world;
      LevelLoad(1);
      currentLevel = 1;
    } else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Num2)) {
      // Clear all SFML Lines
      lines.clear();
      // Pause the game
      paused = true;
      // Reset the World
      delete world;
      LevelLoad(2);
      currentLevel = 2;
    }
    #pragma endregion 

    #pragma region Pause
    static bool toggle = false;
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space)) {
      if(!toggle) {
        paused = !paused;
        toggle = true;
      }
    } else {
      toggle = false;
    }
    #pragma endregion

    if(time > 0.016f) {
      if(!paused) world->Step(time, velocityIterations, positionIterations);
      time = 0;
    }

    window.clear();

    #pragma region BackgroundArt
    window.draw(background);
    window.draw(sun);
    window.draw(cloud1);
    window.draw(hills1);
    window.draw(hills2);
    window.draw(hills3);
    #pragma endregion

    #pragma region DrawSFML
    size_t size = 0;
    sf::ConvexShape cShape;
    cShape.setFillColor(sf::Color::Red);
    //This loops through every single body in the game world
    for(b2Body* b = world->GetBodyList(); b; b = b->GetNext()) {
      //This loops through every fixture in the current body
      for(b2Fixture* f = b->GetFixtureList(); f; f = f->GetNext()) {
        //This checks to see if the type of the fixture is a polygon, if it is then draw the polygon
        if(f->GetType() == b2Shape::e_polygon && currentLevel != 2) {
          // Create the convex shape
          static sf::ConvexShape cShape;
          cShape.setFillColor(sf::Color::Red);
          //Stores a pointer to the shape stored in the fixture
          b2PolygonShape* s = (b2PolygonShape*)f->GetShape();
          //Get the amount of vertices stored in the shape
          size = s->GetVertexCount();
          //Set the size of the object in SFML so it knows how many vertices the shape should have
          cShape.setPointCount(size);
          //Loop through the vertices and send them from Box2D to the SFML shape
          for(int i = 0; i < size; i++) {
            //Stores the current vertex in v
            b2Vec2 v = s->GetVertex(i);
            //Converts the vertex from its local space to where it is in the world
            cShape.setPoint(i, sf::Vector2f((b->GetWorldVector(v).x + b->GetPosition().x), (b->GetWorldVector(v).y + b->GetPosition().y)));
          }
          //Draws the shape onto the window
          window.draw(cShape);
        }
        else if ((f->GetType() == b2Shape::e_polygon && b->GetType() == b2_dynamicBody)) {
          static sf::Sprite bikeFrame;
          bikeFrame.setTexture(bikeFrame_t);
          bikeFrame.setPosition(f->GetBody()->GetPosition().x, f->GetBody()->GetPosition().y);
          bikeFrame.setScale(0.5f,0.5f);
          bikeFrame.setRotation(b->GetAngle() * 57.2957795f);
          bikeFrame.setOrigin(0,0);
          window.draw(bikeFrame);
        }
        else if (f->GetType() == b2CircleShape::e_circle) {
          //  // Create A cricle to be drawn
          //  static sf::CircleShape circle;
          //  circle.setFillColor(sf::Color::Green);
          //  //Stores a pointer to the shape stored in the fixture
          //  b2PolygonShape* s = (b2PolygonShape*)f->GetShape();
          //  // Calculate the radius for the SFML circle
          //  circle.setRadius(s->m_radius);
          //  circle.setPosition(f->GetBody()->GetPosition().x - circle.getRadius(), f->GetBody()->GetPosition().y - circle.getRadius());
          //  window.draw(circle);

          b2PolygonShape* s = (b2PolygonShape*)f->GetShape();
          static sf::Sprite sWheel;
          sWheel.setScale((s->m_radius*0.01)*2,(s->m_radius*0.01)*2);
          sWheel.setPosition (f->GetBody()->GetPosition().x, f->GetBody()->GetPosition().y);
          
          if(currentLevel == 1) sWheel.setTexture(wheel);
          if(currentLevel == 2) sWheel.setTexture(bikeWheel);
          sWheel.setOrigin(50,50);
          sWheel.setRotation(b->GetAngle() * 57.2957795f);
          window.draw(sWheel);

        }
      }
    }
    #pragma endregion

    #pragma region DrawClickedLines
    // draw all elements in line vector
    for(int i = 0; i != lines.size(); ++i) {
      static sf::VertexArray quad(sf::Quads, 4);
      quad[0].position = sf::Vector2f(lines[i].a.x, lines[i].a.y);
      quad[1].position = sf::Vector2f(lines[i].b.x, lines[i].b.y);
      quad[2].position = sf::Vector2f(lines[i].c.x, lines[i].c.y);
      quad[3].position = sf::Vector2f(lines[i].d.x, lines[i].d.y);
      for(int j = 0; j != quad.getVertexCount(); ++j)
        quad[j].color = sf::Color::Green;
      window.draw(quad);

      // Create shadows under all the lines
      static sf::VertexArray shadow(sf::Quads, 4);
      shadow[0].position = sf::Vector2f(lines[i].a.x, lines[i].a.y);
      shadow[1].position = sf::Vector2f(lines[i].b.x, lines[i].b.y);
      shadow[2].position = sf::Vector2f(lines[i].c.x, lines[i].c.y+80);
      shadow[3].position = sf::Vector2f(lines[i].d.x, lines[i].d.y+80);
      shadow[0].color = sf::Color::Color(0,0,0,80);
      shadow[1].color = sf::Color::Color(0,0,0,80);
      shadow[2].color = sf::Color::Color(0,0,0,0);
      shadow[3].color = sf::Color::Color(0,0,0,0);
      window.draw(shadow);
    }
    #pragma endregion

    #pragma region Play/Pause
    // Change the play and pause Icon on screen based on state
    if(!paused) {
      static sf::VertexArray play(sf::Triangles, 3);
      vector2 location(10,10);
      play[0].position = sf::Vector2f(0  +location.x,  0 +location.y);
      play[1].position = sf::Vector2f(20 +location.x, 15 +location.y);
      play[2].position = sf::Vector2f(0  +location.x, 30 +location.y);
      
      play[0].color = sf::Color::Green;
      play[1].color = sf::Color::Green;
      play[2].color = sf::Color::Green;
      window.draw(play);
    } else {
      static sf::VertexArray pause1(sf::Quads, 4);
      static sf::VertexArray pause2(sf::Quads, 4);
      vector2 location(10,10);
      pause1[0].position = sf::Vector2f(0 +location.x, 0 +location.y);
      pause1[1].position = sf::Vector2f(5 +location.x, 0 +location.y);
      pause1[2].position = sf::Vector2f(5 +location.x, 30 +location.y);
      pause1[3].position = sf::Vector2f(0 +location.x, 30 +location.y);
      pause1[0].color = sf::Color::Blue;
      pause1[1].color = sf::Color::Blue;
      pause1[2].color = sf::Color::Blue;
      pause1[3].color = sf::Color::Blue;
      pause2[0].position = sf::Vector2f(10 +location.x, 0 +location.y);
      pause2[1].position = sf::Vector2f(15 +location.x, 0 +location.y);
      pause2[2].position = sf::Vector2f(15 +location.x, 30 +location.y);
      pause2[3].position = sf::Vector2f(10 +location.x, 30 +location.y);
      pause2[0].color = sf::Color::Blue;
      pause2[1].color = sf::Color::Blue;
      pause2[2].color = sf::Color::Blue;
      pause2[3].color = sf::Color::Blue;
      window.draw(pause1);
      window.draw(pause2);
    }
    #pragma endregion

  window.display();
  }

  
	return 0;
}
		/**
			Append a line to a vertexarray ( 2 vertices).
		*/
		inline void AppendLine(sf::VertexArray& vA, const sf::Vector2f& start, const sf::Vector2f& end, const sf::Color& c) {
			vA.append(sf::Vertex(start, c));
			vA.append(sf::Vertex(end, c));
		}
Exemple #18
0
void FogOfWar::initializeVertexsGiven(const sf::VertexArray& vertices,
	const sf::Texture* tileSet)
{
	//const sf::VertexArray& vertices = vertexNode->getVertices();
	Utility::CircleGeometry circle(mCenterPoint, mSightRadius);
	for (int i = 0; i < vertices.getVertexCount(); i += 4){
		const sf::Vertex& topLeft = vertices[i];
		const sf::Vertex& topRight = vertices[i + 1];
		const sf::Vertex& bottomRight = vertices[i + 2];
		const sf::Vertex& bottomLeft = vertices[i + 3];

		sf::FloatRect floatRect(topLeft.position.x, topLeft.position.y, 
			topRight.position.x - topLeft.position.x, bottomLeft.position.y - topLeft.position.y);

		/*if (!Utility::isPointInsideImaginaryCircle(mCenterPoint, mSightRadius, topLeft.position) &&
			!Utility::isPointInsideImaginaryCircle(mCenterPoint, mSightRadius, topRight.position) &&
			!Utility::isPointInsideImaginaryCircle(mCenterPoint, mSightRadius, bottomRight.position) &&
			!Utility::isPointInsideImaginaryCircle(mCenterPoint, mSightRadius, bottomLeft.position))
			continue;*/
		
		if (!Utility::isCircleIntersectsWithFloatRect(circle, floatRect))
			continue;

		//get the texture that the vertex is using
		//const sf::Texture* tileSet = vertexNode->getTileSet();
		//get the corresponding img also
		sf::Image* tileImg = mTexturesInt.textureKeyToImg(tileSet);
		
		if (!tileImg)
			continue;

		sf::Vector2u imgSize = tileImg->getSize();

		//is the size of the current vertex's texture size
		sf::Vector2f textureRectSize;
		textureRectSize.x = topRight.texCoords.x - topLeft.texCoords.x;
		textureRectSize.y = bottomLeft.texCoords.y - topLeft.texCoords.y;

		//the top left position of the 4 vertexs combined together
		sf::Vector2f vertexPosInGame = topLeft.position;
		

		const sf::Uint8* tilePixels = tileImg->getPixelsPtr();

		//img we use to put into our mMapTexture
		/*sf::Image finalImageToTxt;
		finalImageToTxt.create(textureRectSize.x, textureRectSize.y);

		

	

		//for (float txCoord = 0.f; txCoord < textureRectSize.x; txCoord++){

		for (float tyCoord = 0.f; tyCoord < textureRectSize.y; tyCoord++){
			for (float txCoord = 0.f; txCoord < textureRectSize.x; txCoord++){

				int a = (topLeft.texCoords.x + txCoord) +
					(imgSize.x *  (topLeft.texCoords.y + tyCoord));

				a *= 4;

				finalImageToTxt.setPixel(txCoord, tyCoord,
					sf::Color(
					tilePixels[a],
					tilePixels[a + 1],
					tilePixels[a + 2],
					20));
			}
		}
		mMapTexture.update(finalImageToTxt, vertexPosInGame.x, vertexPosInGame.y);*/

		float finalSize = textureRectSize.x * textureRectSize.y * 4.f;
		int totalIndex = static_cast<int>(finalSize);
		std::unique_ptr<sf::Uint8> newPixels(new sf::Uint8[totalIndex]);
		sf::Uint8* ptrToPixel = newPixels.get();

		int newI = 0;
		for (float tyCoord = 0.f; tyCoord < textureRectSize.y; tyCoord++){
			for (float txCoord = 0.f; txCoord < textureRectSize.x; txCoord++){

				int a = (topLeft.texCoords.x + txCoord) +
					(imgSize.x *  (topLeft.texCoords.y + tyCoord));

				a *= 4;
				ptrToPixel[newI] = tilePixels[a];
				ptrToPixel[newI + 1] = tilePixels[a + 1];
				ptrToPixel[newI + 2] = tilePixels[a + 2];
				ptrToPixel[newI + 3] = 20;

				newI += 4;
			}
		}
		mMapTexture.update(ptrToPixel, textureRectSize.x, textureRectSize.y, vertexPosInGame.x, vertexPosInGame.y);
		
	}
}