C++ Box2d 和 SFML 渲染指针问题?

问题描述 投票:0回答:0

因此,我有几个标头和 cpp 文件来制作游戏,但在全部设置完毕后,我无法将任何内容渲染或绘制到窗口。 这里是文件 BoxRunner 是主文件

HeadHunter.h

#pragma once
#define WINDOW_WIDTH 1600
#define WINDOW_HEIGHT 800
// Pixels per meter. Box2D uses metric units, so we need to define a conversion
#define PPM 30.0F
// SFML uses degrees for angles while Box2D uses radians
#define DEG_PER_RAD 57.2957795F
#include <iostream>
#include "box2d/box2d.h"
#include <SFML/Graphics.hpp>

Graphics.h

#pragma once
#include "HeadHunter.h"

class Graphics
{
    public:
    struct Rect {
        float width;
        float height;
        b2Body &body;
        sf::Color color;
    };
    Rect IntilizeBoxes(float width, float height, b2Body *body, b2Vec3 Color);
    void DrawBoxes(std::vector<Rect>rect, sf::RenderWindow& w);
};

World.h

#pragma once
#include "HeadHunter.h"

class World
{
private:
    b2World world;
    float timeStep;
public:
    struct Box {
        b2Vec3 ID;
        float width;
        float height;
        b2Body *body;
        b2Vec3 color;
    };
    World();
    Box createBox(b2Vec3 ID,float x,float y,float width, float height,b2Vec3 Color);
    void UpdateWorld();
};

Graphics.cpp

#include "Graphics.h"

Graphics::Rect Graphics::IntilizeBoxes(float width, float height, b2Body *body, b2Vec3 Color) {
    sf::Color col(Color.x, Color.y, Color.z);
    return Rect{width,height,*body,col};
}
void Graphics::DrawBoxes(std::vector<Graphics::Rect>rect, sf::RenderWindow& w) {
    for (const auto& Rect : rect)
    {
        sf::RectangleShape rect;

        // For the correct Y coordinate of our drawable rect, we must substract from WINDOW_HEIGHT
        // because SFML uses OpenGL coordinate system where X is right, Y is down
        // while Box2D uses traditional X is right, Y is up
        rect.setPosition(Rect.body.GetPosition().x * PPM, WINDOW_HEIGHT - (Rect.body.GetPosition().y * PPM));

        // We also need to set our drawable's origin to its center
        // because in SFML, "position" refers to the upper left corner
        // while in Box2D, "position" refers to the body's center
        rect.setOrigin(Rect.width / 2, Rect.height / 2);

        rect.setSize(sf::Vector2f(Rect.width, Rect.height));

        // For the rect to be rotated in the crrect direction, we have to multiply by -1
        rect.setRotation(-1 * Rect.body.GetAngle() * DEG_PER_RAD);

        rect.setFillColor(sf::Color::Cyan);
        w.draw(rect);
    }
}

World.cpp

#include "World.h"

World::World() : world(b2Vec2(0, -9.2f)), timeStep(1.f / 60.f) {}

World::Box World::createBox(b2Vec3 ID,float x, float y, float width, float height,b2Vec3 color) {
    b2BodyDef bodyDef;
    bodyDef.position.Set(x / PPM, y / PPM);
    b2PolygonShape BoxShape;
    BoxShape.SetAsBox(width,height);
    b2FixtureDef fixtureDef;
    fixtureDef.shape = &BoxShape;
    fixtureDef.density = 1.0f;
    fixtureDef.friction = 0.3f;

    if (ID.x == 1)
        bodyDef.type = b2_dynamicBody;
    if (ID.x == 0)
        bodyDef.type = b2_staticBody;

    b2Body* body = world.CreateBody(&bodyDef);
    body->CreateFixture(&fixtureDef);

    return Box{ ID,width,height,body,color };
}

void World::UpdateWorld() {
    world.Step(timeStep, 6, 2);
}

现在将所有这些都集中在 Game 标头中的 GameEngine 类中的主类

Game.h

#pragma once
#include "Graphics.h"
#include "World.h"

class GameEngine
{
public:
    GameEngine();
    void makeboxes(int NumofBoxes, float x, float y, float width, float height, b2Vec3 ID, b2Vec3 Color);
    void run();

private:
    sf::RenderWindow window;
    bool isRunning;

    //sf::View FollowCam;

    void processEvents();
    void update(sf::Time deltaTime);
    void render();
};

Game.cpp

#include "Game.h"

GameEngine::GameEngine() : window(sf::VideoMode(1600, 800), "Game"), isRunning(true) {}

World world;
Graphics graphics;

void GameEngine::makeboxes(int NumofBoxes,float x,float y,float width, float height, b2Vec3 ID, b2Vec3 Color) {
    std::vector<World::Box>boxes;
    // Generate a lot of boxes
    for (int i = 0; i < NumofBoxes; i++)
    {
        // Starting positions are randomly generated: x between 50 and 550, y between 70 and 550
        auto&& m_box = (world.createBox(ID,x,y, width, height, Color));
        boxes.push_back(m_box);
    }
    std::vector<Graphics::Rect>rects;
    // Generate a lot of boxes
    for (int i = 0; i < boxes.size(); i++)
    {
        // Starting positions are randomly generated: x between 50 and 550, y between 70 and 550
        auto&& m_rect = graphics.IntilizeBoxes(width, height, boxes[i].body, Color);
        rects.push_back(m_rect);
       // FollowCam.setCenter(m_rect.body.GetPosition().x * PPM, WINDOW_HEIGHT - (m_rect.body.GetPosition().y * PPM));
        std::cout << m_rect.body.GetPosition().x * PPM << ", " << WINDOW_HEIGHT - (m_rect.body.GetPosition().y * PPM) << "\n";
    }
    graphics.DrawBoxes(rects, window);
    std::cout << "Boxes Created\n";
}

void GameEngine::run() {
    sf::Clock clock;
    //window.setView(FollowCam);
    //std::cout << FollowCam.getCenter().x << ", " << FollowCam.getCenter().y << "\n";
    makeboxes(1, 0, 10, 10, 500, b2Vec3(0, 1, 1), b2Vec3(100, 20, 20));

    while (isRunning) {
        sf::Time deltaTime = clock.restart();
        processEvents();
        update(deltaTime);
        render();
    }
}

void GameEngine::processEvents() {
    sf::Event event;
    while (window.pollEvent(event)) {
        if (event.type == sf::Event::Closed) {
            isRunning = false;
        }
    }
}

void GameEngine::update(sf::Time deltaTime) {
    // Update game logic here
    //FollowCam
    world.UpdateWorld();
}

void GameEngine::render() {
    window.clear(sf::Color::Red);

    // Render game objects here
    //std::cout << "Running..\n";

    window.display();
}

主文件有 int main 函数。

BoxRunner.cpp

#include "Game.h"

int main() {
    GameEngine game;

    game.run();

    return 0;
}

我尝试使用 std::cout 打印框的位置,并将 SFML 窗口视图设置为块的中心,但仍然是黑屏。

c++ pointers sfml box2d
© www.soinside.com 2019 - 2024. All rights reserved.