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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - Grenthal

Pages: 1 [2]
16
General / Re: Sprite manager issue
« on: February 16, 2016, 09:44:29 am »
Internally the sprite doesn't copy the texture, but instead is just holding a pointer to it. Creating a bunch of sprites won't create a bunch of unnecessary textures. In this case they will all just make use of the same texture.

I was certain that line like sprite.setTexture( texture ) creates separate instance of texture objects, resisting in memory.

Because I like to see results on my own eyes (or on output console  :) ), I’ve done something I should from the very beginning – testing. Using only vector of sprites (std::vector < sf::Sprite > sprites in my code) I’ve compared program memory usage for: one and 1000 sprites objects. Results were like:
  • 1 sprite object - 22 412 K
  • 1000 objects - 22 676 K

As I see, difference in memory usage lost is very low.

Arcade, Hapax and ratzlaff - Thank You for You’re help and pointing out error in my logic of thinking  :).

17
General / Re: Sprite manager issue
« on: February 15, 2016, 11:21:29 pm »
Hi ratzlaff,

as it is, program compiles without errors. Running it on the other hand result's in crash (white screen with "the program stopped working" message) plus "Process returned -107374189 (0xC0000005)" message in debug console

18
General / Sprite manager issue
« on: February 15, 2016, 10:03:30 pm »
Hello,

I’ve been trying to create resource (sprites) manager, but ended up having problem. Idea was like this: image and texture are being loaded into memory only once (those files might be big), Sprites that are being created should point to those resources instead.

That’s for the vision, below something I’ve got so far:
Hpp class file
#ifndef OBJECTS_H
#define OBJECTS_H

#include <SFML/Graphics.hpp>
#include <memory>

class Objects {
    public:
        Objects();
        virtual ~Objects();
        void draw ( sf::RenderWindow *window );

    protected:
        void create ( void );
        sf::Sprite foo ( void );

    private:
        sf::Image image;
        sf::Texture texture;
        //std::vector < sf::Sprite > sprites;         // First approach -> Without pointers, it's working
        std::vector < sf::Sprite* > sprites;
        //std::vector < std::unique_ptr < sf::Sprite > > sprites; // Third approach - smart ptr's
};

#endif // OBJECTS_H

Cpp Class file:
#include "Objects.hpp"

Objects::Objects() {
    image.loadFromFile ( "image.png" ); // just some red 20x20 px square
    texture.loadFromImage( image );
    create();
}

Objects::~Objects() {
    //dtor
}

// Objects::create
void Objects::create ( void ) {
    sf::Sprite sprite;
    sprite.setTexture( texture );

    // sprites.push_back ( sprite ); // First approach - works, but we have unnecesary textures per sprite resisting in memory?
     sprites.push_back( &sprite ); // Second approach - not working
    // sprites.push_back( std::unique_ptr < sf::Sprite > ( &sprite ) ); // Third approach, not working as well

}// Objects::create

// Objects::draw
void Objects::draw ( sf::RenderWindow *window ) {
    //window->draw ( ( sprites[0] ) ); // First working approach
    window->draw ( ( *sprites[0] ) );  // Second and Third approach - not working
}// Objects::draw

In main.cpp, I’m, just creating class object and invoking draw function:
#include <SFML/Graphics.hpp>
#include "Objects.hpp"

int main() {
    Objects o;

    sf::RenderWindow window(sf::VideoMode( 800, 600 ), "Texture Loading Test!" );
    while ( window.isOpen() ) {
        sf::Event event;
        while ( window.pollEvent( event ) ) {
            if ( event.type == sf::Event::Closed ) {
                window.close();
            }
        }// while

        window.clear();
        o.draw( &window );
        window.display();

    }// while

    return 0;
}

As for Me... it should work  :), but (surprise)… it’s not. I believe we have some kind of memory leek here. I assume, the problem lies here (passing reference to object, pointing to null texture):
void Objects::create ( void ) {
    sf::Sprite sprite;
    sprite.setTexture( texture );
     sprites.push_back( &sprite );


I know I can ignore the pointers approach, and just stick to “first approach” mentioned above, but to my knowledge it will create unnecessary textures, resisting in memory (for example, creating 100 of sprites will create 100 of additional textures) – is my reasoning correct?

I must say, I’ve got no clue how to fix this... :( Any suggestions/hints/leads are most welcomed.

 
Thank You for help in advance.

19
General / Re: draw vector class - problem
« on: June 15, 2015, 10:50:20 am »
Looking at my previous post I see how little information I’ve provided (writing posts in a hurry is a bad idea  it seems  :) ).

The main idea of this exercise was to try using vector class congaing “RectangleShape” type objects.
I’ve managed to store them in array, but it’s not an efficient way (especially when object needs to be destroyed).

Thank You for time, effort and value tips – having them in mind, I’ll try to figure this out.

20
General / draw vector class - problem
« on: June 12, 2015, 04:22:28 pm »

Hello,

I have some issue with drawing vectror of class containing shape objects.
Below sample code:

#include <SFML/Graphics.hpp>
#include <iostream>

class Block
{
    private:
        sf::RectangleShape rectangle;
    public:
        sf::RectangleShape create ( int xPos, int yPos );
};

sf::RectangleShape Block::create ( int xPos, int yPos )
{
    sf::RectangleShape object( sf::Vector2f ( 120, 50 ) );
    object.setPosition( xPos, yPos );
    return object;
}

int main()
{
   sf::RenderWindow window;
   window.create(sf::VideoMode(800, 600), "Sample application");
   std::vector < Block > blocks;
   Block *ptr;
   for (int i = 0; i < 10; i++)
   {
        ptr = new Block;
        ptr->create( i*2, i*2 );
        blocks.push_back(*ptr);
   }
   delete ptr;
   ptr = NULL;
   //Windows
   while(window.isOpen())
   {
       //Event Handler
        sf::Event event;
        while(window.pollEvent(event)) {
            if(event.type == sf::Event::Closed) {
                window.close();
            }//if
        }//while

        window.clear();

        for ( int i = 0; i < blocks.size(); i++ ) {
            window.draw( blocks[i] ); /** ERROR */
        }

        window.display();
   }//while
}//main

Qustion is why am I getting an error?

In advance, thank You for answer   :)

Pages: 1 [2]