Preparing for more cleaned up design but that'll involve finally learning C++ again since I will want to make a class or struct to hold game state and other things.
This commit is contained in:
parent
6735355e28
commit
321e4ddbf0
1 changed files with 73 additions and 74 deletions
|
@ -70,18 +70,67 @@ struct BoxTest Box2d_setup(b2World &world) {
|
||||||
return box;
|
return box;
|
||||||
}
|
}
|
||||||
|
|
||||||
int main() {
|
|
||||||
fmt::print("Setting up a window for you...\n");
|
|
||||||
sf::ContextSettings settings;
|
|
||||||
settings.antialiasingLevel = 8;
|
|
||||||
|
|
||||||
sf::RenderWindow window(sf::VideoMode(1920, 1080), "Simple Game Demo", sf::Style::Default, settings);
|
void Handle_events(sf::RenderWindow &window, BoxTest &box) {
|
||||||
|
sf::Event event;
|
||||||
|
|
||||||
// window.setFramerateLimit(60);
|
// is this a main event loop
|
||||||
window.setVerticalSyncEnabled(true);
|
while (window.pollEvent(event)) {
|
||||||
|
ImGui::SFML::ProcessEvent(window, event);
|
||||||
|
|
||||||
ImGui_setup(window);
|
switch(event.type) {
|
||||||
|
|
||||||
|
case sf::Event::Closed:
|
||||||
|
fmt::print("Exiting...\n");
|
||||||
|
window.close();
|
||||||
|
break;
|
||||||
|
case sf::Event::KeyPressed:
|
||||||
|
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
|
||||||
|
b2Vec2 force(-200, 1000);
|
||||||
|
box.body->ApplyForceToCenter(force, true);
|
||||||
|
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
|
||||||
|
b2Vec2 force(200, 1000);
|
||||||
|
box.body->ApplyForceToCenter(force, true);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case sf::Event::MouseButtonPressed:
|
||||||
|
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
|
||||||
|
b2Vec2 force(-200, 1000);
|
||||||
|
box.body->ApplyForceToCenter(force, true);
|
||||||
|
box.body->ApplyTorque(100.0f, true);
|
||||||
|
} else if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
|
||||||
|
b2Vec2 force(200, 1000);
|
||||||
|
box.body->ApplyForceToCenter(force, true);
|
||||||
|
box.body->ApplyTorque(-100.0f, true);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sf::Time Update_entities(sf::RenderWindow &window, b2World &world, sf::Clock &clock, sf::Clock &deltaClock, sf::Time &tick, BoxTest &box, sf::Shape &shape) {
|
||||||
|
sf::Vector2u winSize = window.getSize();
|
||||||
|
float timeStep = 1.0f / 60.0f;
|
||||||
|
int velocityIterations = 6;
|
||||||
|
int positionIterations = 2;
|
||||||
|
sf::Time since = clock.getElapsedTime();
|
||||||
|
sf::Time nextTick = since - tick > sf::seconds(1) ? since : tick;
|
||||||
|
|
||||||
|
world.Step(timeStep, velocityIterations, positionIterations);
|
||||||
|
b2Vec2 position = box.body->GetPosition();
|
||||||
|
float angle = box.body->GetAngle();
|
||||||
|
|
||||||
|
shape.setPosition(position.x * 100.0f, winSize.y - position.y * 100.0f);
|
||||||
|
shape.setRotation(angle * 180.0f / M_PI);
|
||||||
|
|
||||||
|
ImGui_update(window, deltaClock, tick);
|
||||||
|
Window_update(window, shape);
|
||||||
|
|
||||||
|
return nextTick;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
sf::CircleShape Create_player(sf::RenderWindow &window) {
|
||||||
sf::CircleShape shape(50.f, 4);
|
sf::CircleShape shape(50.f, 4);
|
||||||
sf::Vector2u winSize = window.getSize();
|
sf::Vector2u winSize = window.getSize();
|
||||||
shape.setPosition(winSize.x / 2, winSize.y / 2);
|
shape.setPosition(winSize.x / 2, winSize.y / 2);
|
||||||
|
@ -90,82 +139,32 @@ int main() {
|
||||||
shape.setOutlineThickness(10.f);
|
shape.setOutlineThickness(10.f);
|
||||||
shape.setOutlineColor(sf::Color(250, 150, 100));
|
shape.setOutlineColor(sf::Color(250, 150, 100));
|
||||||
|
|
||||||
|
return shape;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
fmt::print("Setting up a window for you...\n");
|
||||||
|
sf::ContextSettings settings;
|
||||||
|
settings.antialiasingLevel = 8;
|
||||||
|
sf::RenderWindow window(sf::VideoMode(1920, 1080), "Simple Game Demo", sf::Style::Default, settings);
|
||||||
|
window.setFramerateLimit(60);
|
||||||
|
window.setVerticalSyncEnabled(true);
|
||||||
|
ImGui_setup(window);
|
||||||
|
|
||||||
sf::Clock deltaClock;
|
sf::Clock deltaClock;
|
||||||
sf::Clock clock;
|
sf::Clock clock;
|
||||||
sf::Time tick = clock.getElapsedTime();
|
sf::Time tick = clock.getElapsedTime();
|
||||||
|
|
||||||
// very cool, c++11 lambdas doing a 1 second slept thread
|
sf::CircleShape shape = Create_player(window);
|
||||||
sf::Thread thread([](){
|
|
||||||
for(int i = 0; i < 10; i++) {
|
|
||||||
fmt::print("I'm a thread. {}\n", i);
|
|
||||||
sf::sleep(sf::seconds(1));
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
thread.launch();
|
|
||||||
|
|
||||||
b2Vec2 gravity(0.0f, -10.0f);
|
b2Vec2 gravity(0.0f, -10.0f);
|
||||||
b2World world(gravity);
|
b2World world(gravity);
|
||||||
BoxTest box = Box2d_setup(world);
|
BoxTest box = Box2d_setup(world);
|
||||||
|
|
||||||
float timeStep = 1.0f / 60.0f;
|
|
||||||
int velocityIterations = 6;
|
|
||||||
int positionIterations = 2;
|
|
||||||
|
|
||||||
while (window.isOpen()) {
|
while (window.isOpen()) {
|
||||||
sf::Event event;
|
Handle_events(window, box);
|
||||||
|
// preparing for refactoring this into a class or struct for everything
|
||||||
// is this a main event loop
|
tick = Update_entities(window, world, clock, deltaClock, tick, box, shape);
|
||||||
while (window.pollEvent(event)) {
|
|
||||||
ImGui::SFML::ProcessEvent(window, event);
|
|
||||||
|
|
||||||
switch(event.type) {
|
|
||||||
|
|
||||||
case sf::Event::Closed:
|
|
||||||
fmt::print("Exiting...\n");
|
|
||||||
window.close();
|
|
||||||
thread.terminate();
|
|
||||||
break;
|
|
||||||
case sf::Event::KeyPressed:
|
|
||||||
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
|
|
||||||
b2Vec2 force(-200, 1000);
|
|
||||||
box.body->ApplyForceToCenter(force, true);
|
|
||||||
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
|
|
||||||
b2Vec2 force(200, 1000);
|
|
||||||
box.body->ApplyForceToCenter(force, true);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case sf::Event::MouseButtonPressed:
|
|
||||||
if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
|
|
||||||
b2Vec2 force(-200, 1000);
|
|
||||||
box.body->ApplyForceToCenter(force, true);
|
|
||||||
box.body->ApplyTorque(100.0f, true);
|
|
||||||
} else if (sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
|
|
||||||
b2Vec2 force(200, 1000);
|
|
||||||
box.body->ApplyForceToCenter(force, true);
|
|
||||||
box.body->ApplyTorque(-100.0f, true);
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// should this move up to the pollEvent loop?
|
|
||||||
sf::Time since = clock.getElapsedTime();
|
|
||||||
if(since - tick > sf::seconds(1)) {
|
|
||||||
tick = since;
|
|
||||||
}
|
|
||||||
|
|
||||||
world.Step(timeStep, velocityIterations, positionIterations);
|
|
||||||
b2Vec2 position = box.body->GetPosition();
|
|
||||||
float angle = box.body->GetAngle();
|
|
||||||
|
|
||||||
shape.setPosition(position.x * 100.0f, winSize.y - position.y * 100.0f);
|
|
||||||
shape.setRotation(angle * 180.0f / M_PI);
|
|
||||||
|
|
||||||
|
|
||||||
ImGui_update(window, deltaClock, tick);
|
|
||||||
Window_update(window, shape);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ImGui::SFML::Shutdown();
|
ImGui::SFML::Shutdown();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue