Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: I don't understand why it won't render my sprite.  (Read 1598 times)

0 Members and 1 Guest are viewing this topic.

abdpheonix77

  • Newbie
  • *
  • Posts: 2
    • View Profile
I don't understand why it won't render my sprite.
« on: May 10, 2019, 06:17:10 pm »
It's my first time coding a game in SFML and I need some help cause I really don't know what the problem is.

types.cpp
PlayingCard::PlayingCard()
{
    sf::Texture temp;
    std::string path="Graphics/Cards/";
    ++counter_num;
    if(counter_num>13){
        counter_num=1;
        ++counter_suit;
    }
    number=counter_num;
    suit=counter_suit;
    if(number==1){
        path = path + "A";
    }
    else
        if(number==11){
            path = path + "J";
        }
        else
            if(number==12){
                path = path + "Q";
            }
            else
                if(number==13){
                    path = path + "K";
                }
                else
                    if(number==10)
                        path = path + '1'+'0';
                    else
                        path = path + (char)(number+'0');
    path = path + "_of_";
    if(suit==1)
        path = path + "diamonds";
    else
        if(suit==2)
            path = path + "clubs";
        else
            if(suit==3)
                path = path + "hearts";
            else
                path = path + "spades";
    path = path + ".png";
    temp.loadFromFile(resourcePath()+path);
    _sprite.setTexture(temp);
    //std::cout<<path<<"\n";
}

sf::Sprite PlayingCard::get_sprite()
{
    return _sprite;
}

game.cpp
#include "game.hpp"

PlayingCard x;

Game::Game() : _window(sf::VideoMode(1200, 700),"POKER")
{
    sf::Texture temp;
    temp.loadFromFile(resourcePath()+"Graphics/background.png");
    _background.setTexture(temp);
    Game::run(240);
}

///Game related functions

void Game::run(int minimum_frame_per_seconds) {
    sf::Clock clock;
    sf::Time timeSinceLastUpdate;
    sf::Time TimePerFrame = sf::seconds(1.f/minimum_frame_per_seconds);
    while (_window.isOpen()) {
        processEvents();
        timeSinceLastUpdate = clock.restart();
        while (timeSinceLastUpdate > TimePerFrame) {
            timeSinceLastUpdate -= TimePerFrame;
            update(TimePerFrame);
        }
        update(timeSinceLastUpdate);
        render();
    }
}

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

void Game::update(sf::Time deltaTime){
}

void Game::render() {
    _window.clear();
    _window.draw(_background);
    _window.draw(x.get_sprite());
    _window.display();
}

It should render my sprite but only renders a white sized rectangle.
« Last Edit: May 10, 2019, 08:58:56 pm by Laurent »

Elias Daler

  • Hero Member
  • *****
  • Posts: 599
    • View Profile
    • Blog
    • Email
Re: I don't understand why it won't render my sprite.
« Reply #1 on: May 10, 2019, 06:52:58 pm »
sf::Texture gets destroyed when your PlayingCard constructor finishes. You need to store it somewhere for sprites to use it.
Tomb Painter, Re:creation dev (abandoned, doing other things) | edw.is | @EliasDaler

abdpheonix77

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: I don't understand why it won't render my sprite.
« Reply #2 on: May 10, 2019, 07:03:31 pm »
Oh my god you a real life saver thank you I have spent too much time not knowing what was wrong :)))!

 

anything