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.


Topics - MattF

Pages: [1]
1
Graphics / Freeing GPU memory.
« on: May 16, 2013, 12:21:33 pm »
Hi.

I have a problem with handling the textures. Simply put i have a level class, which has Texture and Sprite members in it. I load textures from files, set sprites, all is well. But now i want to switch to next level when this one is done. So i call delete from outside and create a new level. But the old textures stay in GPU memory.

So my question is, how can i destroy unused textures properly?

2
Graphics / Optimizing Texture and Sprite management
« on: November 08, 2012, 01:59:09 pm »
Hi!

I have a multi-part question regarding the proper way of loading, storing and accessing textures and sprites.

Currently i am working on a 2d side-scrolling brawler. Three major game elements, which use textures and sprites are hero, enemies and background/map. I'd like to implement parallax scrolling later on.

Anyway, i wanted to ask about a proper way to load and store gfx elements for each of those instances.

For hero class, i keep all textures and sprites as members, and load textures in the constructor. Since there is only one hero, and he is loaded with the map, it should just work fine. Here's relevant parts:


Hero::Hero (b2World *aWorld, float posX, float posY) : world(aWorld)
        {
                hero_run.loadFromFile("HeroTextures/run.png");
                hero_turn.loadFromFile("HeroTextures/turn.png");
                hero_jump.loadFromFile("HeroTextures/jump.png");
                hero_crouch.loadFromFile("HeroTextures/crouch.png");
                hero_roll.loadFromFile("HeroTextures/roll.png");
                hero_stop.loadFromFile("HeroTextures/stop.png");
                hero_stand.loadFromFile("HeroTextures/stand.png");
                hero_attack.loadFromFile("HeroTextures/attack.png");
                m_sprite.setPosition(posX, posY);
                m_sprite.setOrigin(250,250);
                frame = 0;
                set_b2_body(); // Box2d stuff
               
 }

void Hero::display(State state)
{
        switch(state)
        {
        case running:
                if( direction == right)
                m_sprite.setScale(1.0f,1.0f);
                else
                m_sprite.setScale(-1.0f,1.0f);

                m_sprite.setTexture(hero_run);
                m_sprite.setTextureRect(hero_run_clips[frame]);
                frame++;
                if (frame > 20 )
                        frame = 0;
                break;
}

 

That's the gist of it. I imagine, for enemy class, i'll have to load textures somewhere else, because there will be mutliple enemies constructed from the same texture. Other than that, setTexture seems the way to go.

So regarding this part, my question is: is this the optimal way of handling graphics for animated entities, or could i store it, or access it in more optimized way?


Second part of my post pertains to the background. After preparing elements of the map i just set em the standard way:


TestLevel::TestLevel(b2World *aWorld) : world(aWorld)

{
        backgTexture1.loadFromFile("background/testlevel1.jpg");
        backgTexture2.loadFromFile("background/testlevel2.jpg");
        backgTexture3.loadFromFile("background/testlevel3.jpg");
        background1.setTexture(backgTexture1);
        background2.setTexture(backgTexture2);
        background3.setTexture(backgTexture3);
        background1.setPosition(0,0);
        background2.setPosition(5000,0);
        background3.setPosition(10000,0);
        set_ground_bodies(); // box2d stuff
}

 

It runs nice and all, but loading time is now good couple seconds, and it's only a small part of an actual planned level. Those textures are pretty big though (5000x5000).

So my last question is: how to improve loading time?

Edit: only part of my post showed the 1st time, edited for full version.

3
General / Creating a Hero class
« on: September 14, 2012, 12:31:19 pm »
Hi guys!

I am a beginner when it comes to SFML, and since SFML2 does not have many tutorials i ran into some problems when designing my 2d game.

My question is this:

What is the best way to create a Hero class, considering the need of using sf::Sprite?

The hero would be animated based on a spritesheet, and would need to store data including states, frames, positions, hitpoints etc.

I came up with 3 possible solutions and i wonder which one would fit best with SFML design philosophy.

1) Hero inherits from sf::Sprite
2) Aggregation, so sf::Sprite is a member of Hero
3) Keeping sf::Sprite and Hero separate - use Hero class to store all the date and pass it to sf::Sprite

I would appreciate any answer, especially with some explanation, why any of those options might be the best.

Pages: [1]