Add a fun little meh face guy for a sprite.

master
Zed A. Shaw 5 months ago
parent 321e4ddbf0
commit 8f3840b602
  1. 44
      sfmldemo/main.cpp
  2. BIN
      sfmldemo/sprite.png

@ -4,7 +4,8 @@
#include <math.h> #include <math.h>
#include <fmt/core.h> #include <fmt/core.h>
#include <box2d/box2d.h> #include <box2d/box2d.h>
#include <SFML/Graphics/CircleShape.hpp> #include <SFML/Graphics/Sprite.hpp>
#include <SFML/Graphics/Texture.hpp>
#include <SFML/Graphics/RenderWindow.hpp> #include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System.hpp> #include <SFML/System.hpp>
#include <SFML/Window/Event.hpp> #include <SFML/Window/Event.hpp>
@ -30,9 +31,9 @@ void ImGui_update(sf::RenderWindow &window, sf::Clock &deltaClock, sf::Time &tic
ImGui::End(); ImGui::End();
} }
void Window_update(sf::RenderWindow &window, sf::Shape &shape) { void Window_update(sf::RenderWindow &window, sf::Sprite &player) {
window.clear(); window.clear();
window.draw(shape); window.draw(player);
ImGui::SFML::Render(window); ImGui::SFML::Render(window);
window.display(); window.display();
} }
@ -88,9 +89,11 @@ void Handle_events(sf::RenderWindow &window, BoxTest &box) {
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) { if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)) {
b2Vec2 force(-200, 1000); b2Vec2 force(-200, 1000);
box.body->ApplyForceToCenter(force, true); box.body->ApplyForceToCenter(force, true);
box.body->ApplyTorque(100.0f, true);
} else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) { } else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)) {
b2Vec2 force(200, 1000); b2Vec2 force(200, 1000);
box.body->ApplyForceToCenter(force, true); box.body->ApplyForceToCenter(force, true);
box.body->ApplyTorque(-100.0f, true);
} }
break; break;
case sf::Event::MouseButtonPressed: case sf::Event::MouseButtonPressed:
@ -108,7 +111,7 @@ void Handle_events(sf::RenderWindow &window, BoxTest &box) {
} }
} }
sf::Time Update_entities(sf::RenderWindow &window, b2World &world, sf::Clock &clock, sf::Clock &deltaClock, sf::Time &tick, BoxTest &box, sf::Shape &shape) { sf::Time Update_entities(sf::RenderWindow &window, b2World &world, sf::Clock &clock, sf::Clock &deltaClock, sf::Time &tick, BoxTest &box, sf::Sprite &player) {
sf::Vector2u winSize = window.getSize(); sf::Vector2u winSize = window.getSize();
float timeStep = 1.0f / 60.0f; float timeStep = 1.0f / 60.0f;
int velocityIterations = 6; int velocityIterations = 6;
@ -120,26 +123,29 @@ sf::Time Update_entities(sf::RenderWindow &window, b2World &world, sf::Clock &cl
b2Vec2 position = box.body->GetPosition(); b2Vec2 position = box.body->GetPosition();
float angle = box.body->GetAngle(); float angle = box.body->GetAngle();
shape.setPosition(position.x * 100.0f, winSize.y - position.y * 100.0f); player.setPosition(position.x * 100.0f, winSize.y - position.y * 100.0f);
shape.setRotation(angle * 180.0f / M_PI); player.setRotation(angle * 180.0f / M_PI);
ImGui_update(window, deltaClock, tick); ImGui_update(window, deltaClock, tick);
Window_update(window, shape); Window_update(window, player);
return nextTick; return nextTick;
} }
sf::CircleShape Create_player(sf::RenderWindow &window) { void Create_player(sf::RenderWindow &window, sf::Sprite &player, sf::Texture &texture) {
sf::CircleShape shape(50.f, 4); if(!texture.loadFromFile("sprite.png")) {
sf::Vector2u winSize = window.getSize(); fmt::print("Error loading sprite!");
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; texture.setSmooth(true);
player.setTexture(texture);
// position the prite
sf::Vector2u winSize = window.getSize();
player.setPosition(winSize.x / 2, winSize.y / 2);
player.setOrigin(50.f, 50.f);
} }
int main() { int main() {
@ -155,7 +161,9 @@ int main() {
sf::Clock clock; sf::Clock clock;
sf::Time tick = clock.getElapsedTime(); sf::Time tick = clock.getElapsedTime();
sf::CircleShape shape = Create_player(window); sf::Sprite player;
sf::Texture texture;
Create_player(window, player, texture);
b2Vec2 gravity(0.0f, -10.0f); b2Vec2 gravity(0.0f, -10.0f);
b2World world(gravity); b2World world(gravity);
@ -164,7 +172,7 @@ int main() {
while (window.isOpen()) { while (window.isOpen()) {
Handle_events(window, box); Handle_events(window, box);
// preparing for refactoring this into a class or struct for everything // preparing for refactoring this into a class or struct for everything
tick = Update_entities(window, world, clock, deltaClock, tick, box, shape); tick = Update_entities(window, world, clock, deltaClock, tick, box, player);
} }
ImGui::SFML::Shutdown(); ImGui::SFML::Shutdown();

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.7 KiB

Loading…
Cancel
Save