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.

master
Zed A. Shaw 7 months ago
parent 6735355e28
commit 321e4ddbf0
  1. 97
      sfmldemo/main.cpp

@ -70,49 +70,8 @@ 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);
// window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
ImGui_setup(window);
sf::CircleShape shape(50.f, 4); void Handle_events(sf::RenderWindow &window, BoxTest &box) {
sf::Vector2u winSize = window.getSize();
shape.setPosition(winSize.x / 2, winSize.y / 2);
shape.setOrigin(50.f, 50.f);
shape.setFillColor(sf::Color(150, 50, 250));
shape.setOutlineThickness(10.f);
shape.setOutlineColor(sf::Color(250, 150, 100));
sf::Clock deltaClock;
sf::Clock clock;
sf::Time tick = clock.getElapsedTime();
// very cool, c++11 lambdas doing a 1 second slept thread
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);
b2World world(gravity);
BoxTest box = Box2d_setup(world);
float timeStep = 1.0f / 60.0f;
int velocityIterations = 6;
int positionIterations = 2;
while (window.isOpen()) {
sf::Event event; sf::Event event;
// is this a main event loop // is this a main event loop
@ -124,7 +83,6 @@ int main() {
case sf::Event::Closed: case sf::Event::Closed:
fmt::print("Exiting...\n"); fmt::print("Exiting...\n");
window.close(); window.close();
thread.terminate();
break; break;
case sf::Event::KeyPressed: case sf::Event::KeyPressed:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
@ -148,13 +106,15 @@ int main() {
break; break;
} }
} }
}
sf::Time Update_entities(sf::RenderWindow &window, b2World &world, sf::Clock &clock, sf::Clock &deltaClock, sf::Time &tick, BoxTest &box, sf::Shape &shape) {
// should this move up to the pollEvent loop? sf::Vector2u winSize = window.getSize();
float timeStep = 1.0f / 60.0f;
int velocityIterations = 6;
int positionIterations = 2;
sf::Time since = clock.getElapsedTime(); sf::Time since = clock.getElapsedTime();
if(since - tick > sf::seconds(1)) { sf::Time nextTick = since - tick > sf::seconds(1) ? since : tick;
tick = since;
}
world.Step(timeStep, velocityIterations, positionIterations); world.Step(timeStep, velocityIterations, positionIterations);
b2Vec2 position = box.body->GetPosition(); b2Vec2 position = box.body->GetPosition();
@ -163,9 +123,48 @@ int main() {
shape.setPosition(position.x * 100.0f, winSize.y - position.y * 100.0f); shape.setPosition(position.x * 100.0f, winSize.y - position.y * 100.0f);
shape.setRotation(angle * 180.0f / M_PI); shape.setRotation(angle * 180.0f / M_PI);
ImGui_update(window, deltaClock, tick); ImGui_update(window, deltaClock, tick);
Window_update(window, shape); Window_update(window, shape);
return nextTick;
}
sf::CircleShape Create_player(sf::RenderWindow &window) {
sf::CircleShape shape(50.f, 4);
sf::Vector2u winSize = window.getSize();
shape.setPosition(winSize.x / 2, winSize.y / 2);
shape.setOrigin(50.f, 50.f);
shape.setFillColor(sf::Color(150, 50, 250));
shape.setOutlineThickness(10.f);
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 clock;
sf::Time tick = clock.getElapsedTime();
sf::CircleShape shape = Create_player(window);
b2Vec2 gravity(0.0f, -10.0f);
b2World world(gravity);
BoxTest box = Box2d_setup(world);
while (window.isOpen()) {
Handle_events(window, box);
// preparing for refactoring this into a class or struct for everything
tick = Update_entities(window, world, clock, deltaClock, tick, box, shape);
} }
ImGui::SFML::Shutdown(); ImGui::SFML::Shutdown();

Loading…
Cancel
Save