Пули не отображаются на экране если на него нажать. c++ sfml

Рейтинг: 0Ответов: 0Опубликовано: 20.07.2023

При нажатии на экран должна появляться картинка пули, однако этого не происходит. Весь код создания пуль происходит при помощи динамического массива. Путь указан верно, так как если путь указан не верно, программа завершается с кодом 1.

Код main.cxx

#include <SFML/Graphics.hpp>
#include <vector>
#include <cmath>
#include "../include/Funcs.h"
#include <../include/sprites.h>


int main()
{
    Gun gun;
    gun.gun_load();
    gun.gunSetTex();
    gun.setRotation(90);
    Bullet bullet;
    bullet.bullet_load();
    bullet.bulletSetTex();
    bullet.setRotation(90);
    
    sf::Font font;
    if (!font.loadFromFile("resources/fonts/font.ttf"))
    {
        return 1;
    }

    sf::Text objs("", font, 20);
    sf::Text fpsT("", font, 20);
    fpsT.setPosition(10, 30);
    objs.setPosition(110, 30);

    std::vector<Bullet*> bulletList;
    sf::RenderWindow window(sf::VideoMode(360, 800), "Dynamic Instance");

    while (window.isOpen())
    {
        fpsT.setString(std::to_string(getFps()));

        for (Bullet* instance : bulletList)
        {
            if (instance != nullptr)
            {
                objs.setString("Bullets in game: " + std::to_string(bulletList.size()));
                
                
                    instance->setPosition(instance->getPosition());
                
            }
        }

        sf::Event event;
        sf::Vector2f pos;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }
            else if (event.type == sf::Event::TouchBegan)
            {
                pos = getTouchPos(0, window);
                Bullet* newBullet = new Bullet;
                createInstance(*newBullet, bulletList);
                newBullet->setPosition(pos);
            }
            
        }
        window.clear();

        for (Bullet* instance : bulletList)
        {
                instance->draw(window);
            
        }
        
        window.draw(fpsT);
        window.draw(objs);
        window.display();
    }

    for (Bullet* instance : bulletList)
    {
        delete instance;
    }

    return 0;
}

Код Funcs.h


#pragma once
#include <SFML/Graphics.hpp>

int getFps()
{
    static sf::Clock clock;
    static int frameCount = 0;
    frameCount++;
    sf::Time deltaTime = clock.restart();
    float fps = frameCount / deltaTime.asSeconds();
    frameCount = 0;
    return static_cast<int>(fps);
}

sf::Vector2f getTouchPos(unsigned const int idx, sf::RenderWindow& window)
{
    sf::Vector2i rpos{ sf::Touch::getPosition(idx, window) };
    sf::Vector2f pos = window.mapPixelToCoords(rpos);
    return pos;
}

template<typename T>
void createInstance(T& newObj, std::vector<T*>& objList)
{
    objList.push_back(&newObj);
}

Код sprites.h

#include <SFML/Graphics.hpp>
#pragma once

class Gun
{
  public:
    sf::Texture gun_base;
    sf::Sprite gun_sprite;
    int gun_load()
    {
        if (!gun_base.loadFromFile("../resources/textures/gun_base.png"))
        {
            return 1;
        }
    }
    void gunSetTex()
    {
        gun_sprite.setTexture(gun_base);
    }
    void setPosition(sf::Vector2f gun_pos)
    {
        gun_sprite.setPosition(gun_pos.x, gun_pos.y);
    }
    void setRotation(float rot)
    {
        gun_sprite.setRotation(rot);
    }
    void draw(sf::RenderWindow&win)
    {
        win.draw(gun_sprite);
    }
    void setScale(float gx, float gy)
    {
        gun_sprite.setScale(gx, gy);
    }
    
};

class Bullet
{
  public:
    sf::Texture bullet_tex;
    sf::Sprite bullet_sprite;
    int bullet_load()
    {
        if (!bullet_tex.loadFromFile("../resources/textures/bullet.png"))
        {
            return 1;
        }
    }
    void bulletSetTex()
    {
        bullet_sprite.setTexture(bullet_tex);
    }
    void setPosition(sf::Vector2f bullet_pos)
    {
        bullet_sprite.setPosition(bullet_pos.x, bullet_pos.y);
    }
    void setRotation(float rot)
    {
        bullet_sprite.setRotation(rot);
    }
    void draw(sf::RenderWindow&win)
    {
        win.draw(bullet_sprite);
    }
    void setScale(float bx, float by)
    {
        bullet_sprite.setScale(bx, by);
    }
    sf::Vector2f getPosition()
    {
        return bullet_sprite.getPosition();
    }
};

Повторяюсь: пути указаны верно, ведь в противном случае программа завершается с кодом 1, чего не происходит.

Ответы

Ответов пока нет.