コード例 #1
0
ファイル: main.cpp プロジェクト: in0x/go_on_proto
int main(int argc, char** argv)
{
	RenderWindow window(sf::VideoMode(W_WIDTH,W_HEIGHT),"Hello",sf::Style::Close);
		
	View view{ FloatRect{0,0,V_WIDTH,V_HEIGHT} };
	View defaultView = View{ FloatRect{ 0,0, 1600, 1200 } };

	//window.setView(view);

	Physics physics{ {0.f, 9.8f}, 10.f };

	//LightMap lights{view};
	LightMap lights{ defaultView };

	auto playerLight = lights.createLight("../assets/lightmask.png", 400, 370, 3.f);

	//lights.createLight("../assets/lightmask.png", 200, 100, 3.f);

	Clock clock;

	physics.spawnStaticBox(40.f, 60.f, 800.f, 40.f);
	physics.spawnStaticBox(80.f, 110.f, 800.f, 40.f);
	
	auto pPlayerBody = physics.spawnDynamicCircle(3.f, 400.f, 0.f);
	
	Texture tex;
	tex.loadFromFile("../assets/tile.png");
	tex.setSmooth(true);

	Shader blendShader;

	if (!blendShader.loadFromFile("../src/test.frag", Shader::Fragment))
		std::cout << "Failed to load blend fragment shader" << std::endl;

	Texture playerTex;
	playerTex.loadFromFile("../assets/player.png");
	playerTex.setSmooth(true);

	sf::Event ev;

	Texture worldTex;
	worldTex.create(W_WIDTH, W_HEIGHT);
	Sprite displaySprite;

	Font font;
	font.loadFromFile("../assets/kenney_bold.ttf");

	Text framerate;
	framerate.setFont(font);
	framerate.setCharacterSize(40);
	framerate.setPosition(1100, 200);
	framerate.setColor(sf::Color::White);

	while (window.isOpen())
	{
		while (window.pollEvent(ev))
		{
			if (ev.type == Event::Closed)
			{
				window.close();
			}

			if (ev.type == Event::KeyPressed)
			{
				if (ev.key.code == sf::Keyboard::Left)
				{	
					if (pPlayerBody->GetLinearVelocity().x - (MS_X / 100) > -(2 * MS_X / 100))
						pPlayerBody->ApplyForceToCenter({-MS_X, 0.f},true);
					else
						pPlayerBody->ApplyForceToCenter({ -(2 * MS_X / 100) - pPlayerBody->GetLinearVelocity().x , 0.f }, true);
				}

				if (ev.key.code == sf::Keyboard::Right)
				{
					if (pPlayerBody->GetLinearVelocity().x + (MS_X / 100) < (2 * MS_X / 100))
						pPlayerBody->ApplyForceToCenter({ MS_X, 0.f }, true);
					else
						pPlayerBody->ApplyForceToCenter({ (2 * MS_X / 100) - pPlayerBody->GetLinearVelocity().x , 0.f }, true);
				}

				if (ev.key.code == sf::Keyboard::Space)
				{
					if (pPlayerBody->GetLinearVelocity().y + (MS_X / 100)  < (2 * MS_X / 100))
						pPlayerBody->ApplyForceToCenter({ 0.f, -MS_X * 2 }, true);
					//else
						//pPlayerBody->ApplyForceToCenter({ 0.f, (3 * MS_X / 100) - pPlayerBody->GetLinearVelocity().y }, true);
				}
			}

			if (ev.type == Event::JoystickButtonPressed)
			{
				std::cout << ev.joystickButton.button << std::endl;
			}

			if (ev.type == Event::JoystickMoved)
			{
				std::cout << ev.joystickMove.axis << std::endl;
			}
		}

		physics.step(1.f / 30.f);
		
		window.clear();

		auto playerPos = pPlayerBody->GetPosition();
		view.setCenter(SCALE * playerPos.x, SCALE * playerPos.y);
		//window.setView(view);

		auto lightSize = playerLight->getSprite().getTexture()->getSize();

		// 3.f is the player body radius
		playerLight->setPosition(
			playerPos.x * SCALE - lightSize.x - 3.f * SCALE * 2
			, playerPos.y * SCALE - lightSize.y - 3.f * SCALE * 2);

		auto bodyIt = physics.getBodyList();

		//lights.updateView(view);
		lights.render();

		while (bodyIt != nullptr)
		{
			auto pos = bodyIt->GetPosition();

			if (bodyIt->GetType() == b2_dynamicBody)
			{
				CircleShape sprite;
							
				sprite.setTexture(&playerTex);

				auto playerShape = *(b2CircleShape*)(bodyIt->GetFixtureList()->GetShape());

				sprite.setRadius(playerShape.m_radius * SCALE);
				
				sprite.setOrigin(playerShape.m_radius * SCALE, playerShape.m_radius * SCALE);
				
				sprite.setPosition(SCALE * pos.x, SCALE * pos.y);

				sprite.setRotation(bodyIt->GetAngle() * 180 / b2_pi);
				
				window.draw(sprite);
			}

			else // ground
			{
				RectangleShape sprite;
				
				sprite.setSize({800.f, 40.f});
				
				sprite.setOrigin(400, 20);

				sprite.setPosition(pos.x * SCALE, pos.y * SCALE);

				sprite.setRotation(bodyIt->GetAngle() * 180 / b2_pi);

				sprite.setTexture(&tex);

				window.draw(sprite);
			}
			
			bodyIt = bodyIt->GetNext();
		}

		window.draw(Sprite{ lights.getLightMapTexture() }, BlendMultiply);

		float frameTime = 1.f / clock.getElapsedTime().asSeconds();
		clock.restart();
		framerate.setString(std::to_string((int)frameTime) + " fps");

		window.draw(framerate);

		window.display();
	}
}