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

Author Topic: Making object draw itself issue.  (Read 2674 times)

0 Members and 1 Guest are viewing this topic.

Luponius

  • Newbie
  • *
  • Posts: 22
    • View Profile
Making object draw itself issue.
« on: April 25, 2013, 11:30:30 pm »
Allright I'm trying to draw a character sprite from a sprite sheet (3 wide, 4 deep), only drawing IntRect(32,0,32,32) specifically for now, just want some valid output.  To quickly describe my current setup this is what I have...

main.cpp  Supposed to do the standard stuff.  Build a render window, basic initialization and some simple event checking, then just clearing to grey and drawing the object's sprite.
#include <SFML/Graphics.hpp>
#include <iostream>
#include "creature.hpp"

#define screenWidth 800
#define screenHeight 600

int main ( void )
{
        // ############################### VARIABLE DECLARATION ############################### //
       
        // Window creation
        sf::VideoMode vMode(screenWidth, screenHeight, 32);
        sf::RenderWindow App (vMode,"Test");
        App.setFramerateLimit(60);
        App.setVerticalSyncEnabled(true);
        std::cout << "App Initialized" << std::endl;

        // Texture loading / Sprite Creation
        sf::Texture playerSheet;
        if (!playerSheet.loadFromFile("player.png"))
                return EXIT_FAILURE;
       
        // Create creature object
        creature player(playerSheet,sf::IntRect(32,0,32,32));

        // ############################### GAME LOOP ############################### //
        while (App.isOpen())
        {
                // ############################### EVENTS ############################### //
                sf::Event event;
                while (App.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                App.close();
                                break;

                        case sf::Event::KeyPressed:
                                switch (event.key.code)
                                {
                                case sf::Keyboard::Escape:
                                        App.close();
                                        break;
                                }
                        break;
                        }
                }


                // ############################### DRAW ############################### //#
                App.clear(sf::Color(125,125,125));

                player.draw(App);

                App.display();
        }      
}

creature.hpp Now as for the object's draw function:
#pragma once // Avoid multiple includes

#include <SFML/Graphics.hpp>

class creature
{
public:
        creature(sf::Texture tex, sf::IntRect zone);            // Constructor
        ~creature(void);                                                                        // Destructor
        void draw (sf::RenderWindow& App);

private:
        // Visual
        sf::Sprite creatureSpr;
};

creature.cpp  I've cropped away unecessary crap from both these files since they're not even implemented yet, just a bunch of declared variables which are set to default values and not really used for now.  But as you can see it's designed to be constructed with a texture and a specified zone, which is applied in the constructor, and then displayed by reference using the draw function of the app passed by reference.
#include "creature.hpp"

creature::creature(sf::Texture tex, sf::IntRect zone)
{
        // Set texture and zone
        creatureSpr.setTexture(tex);
        creatureSpr.setTextureRect(zone);
}

void creature::draw (sf::RenderWindow& App)
{
        App.draw(creatureSpr);
}

creature::~creature(void)
{
}
 

Output results in an empty 32*32 white opaque rectangle.  Leads me to believe the creatureSpr.setTexture() is failing for some reason, however the setTextureRect appears to work... what's going on? :O

Luponius

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Making object draw itself issue.
« Reply #1 on: April 25, 2013, 11:31:54 pm »
... Talk about idiocy i uhh changed the texture by reference and it worked... still... WHY does that happen lol? 

Doesn't it just clone the texture into the object's sprite and use it anyways by local reference or does it somehow die and lose the asset when drawing it from the object itself as it goes back out to the main function?

I felt i had a decent grasp of OOP but this is a bit puzzling =/

Jebbs

  • Sr. Member
  • ****
  • Posts: 358
  • DSFML Developer
    • View Profile
    • Email
Re: Making object draw itself issue.
« Reply #2 on: April 26, 2013, 12:12:35 am »
The reason it doesn't work if the Texture isn't passed by reference is because Sprite.setTexture takes a reference to the Texture.

If the Texture is a copy, when the function ends and the copy has its destructor called, the Sprite no longer has a valid texture.
DSFML - SFML for the D Programming Language.

Luponius

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Making object draw itself issue.
« Reply #3 on: April 26, 2013, 01:05:10 am »
Noted.  Didn't notice the setTexture was by reference!

Thanks =]

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: Making object draw itself issue.
« Reply #4 on: April 26, 2013, 01:27:12 am »
Didn't notice the setTexture was by reference!
Documentation for the win. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Luponius

  • Newbie
  • *
  • Posts: 22
    • View Profile
Re: Making object draw itself issue.
« Reply #5 on: April 27, 2013, 02:44:55 am »
Sometimes, stuff just slips and mistakes happen, no matter how hard you try  ::)