简体   繁体   中英

Sprite not showing sfml

I want to display my player in the window but my player sprite is not showing in the window. I am new to c++. I want to learn classes, inheritence, composition, etc in this way.I have 3 files Player.cpp, Game.cpp and main.cpp. I am using main.cpp to call Game.cpp using a fuction called Run().

Got nothing to try.

Player.cpp

#include "Player.hpp"
#include "string.h"
#include <iostream>


void Player::initPlayer()
{
    const char* playerTexturePath = "/Users/don/Desktop/sfmlgames/game1/img/MCmid.png";
    if(!mPlayerTexture.loadFromFile(playerTexturePath))
    {
        std::cout << "mPlayerTexturePath not found!!" << std::endl;
    }
    mPlayerSprite.setTexture(mPlayerTexture);
    mPlayerSprite.setPosition(100.f, 100.f);
    mPlayerSprite.setScale(1.f, 1.f);
}

void Player::draw_player(sf::RenderWindow &win)
{
    win.draw(mPlayerSprite);
}

Game.cpp

#include "Game.hpp"
#include <iostream>

Player player1;

//Constructor: Create a window and Player
Game::Game() : mWindow(sf::VideoMode(640, 480), "Game")
{
    //Frame rate 60
    mWindow.setFramerateLimit(60);  
    player1.initPlayer();
    }

//Game loop
void Game::Run()
{

    while(mWindow.isOpen())
    {
        
        render();
        events();
        update();
        
    }
}



void Game::events()
{
    sf::Event event;
    
    while (mWindow.pollEvent(event))
    {
        if (event.type == sf::Event::Closed)
        {
            mWindow.close();
        }

    }
}

void Game::update()
{
    
}


void Game::render()
{   
    mWindow.clear(sf::Color::Red);
    player1.draw_player(mWindow);
    mWindow.display();
}

main.cpp

#include "Game.hpp"

int main()
{
    Game game;
    game.Run();
}

I don't think I will need to give code to hpp files.

The problem was with image I dont know why. I used another image and it worked fine.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM