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

Author Topic: Extending the Sprite class?  (Read 4986 times)

0 Members and 1 Guest are viewing this topic.

TheSambassador

  • Newbie
  • *
  • Posts: 2
    • View Profile
Extending the Sprite class?
« on: May 20, 2010, 05:51:10 am »
Hello!

I'm working on a basic platformer, trying to get used to using C++ instead of Python, and I was going to make a tile-based system.

I had no problem creating a two-dimensional array of Sprites and drawing those onto the screen.  It's when I started to try and create a "Tile" class to have a bit more functionality that I started running into problems.

My C++ is pretty rusty, but I think that I'm doing everything right in terms of C++ syntax.  I want to create my "Tile" class and make it extend Sprite.  I assumed that I should be able to simply call renderwindow.Draw(someTile) and that it would correctly draw things.  

Here is my tile class:

Code: [Select]
#ifndef TILE_H_INCLUDED
#define TILE_H_INCLUDED

#include <SFML/Graphics.hpp>

class Tile : public sf::Sprite{
    public:
        Tile() : sf::Sprite(), solid(false){};

        Tile(sf::Image img, sf::Vector2f pos, bool sol) : sf::Sprite(img, pos), solid(sol){};

        bool solid;

};
#endif // TILE_H_INCLUDED


However, every time I try to draw a tile, it draws the correct size (in this case the image is 32x32) but all of the pixels are always white!  

I'm creating my tile object like this:
Code: [Select]
Tile test(tileset[1], sf::Vector2f(0,0), true);
And drawing it like this:
Code: [Select]
window.Draw(test);

tileset is an array of Images, this seems to be working correctly, because if I just create a Sprite from tileset[1] it displays correctly.

Am I doing something wrong?  Should I just include a Sprite in my Tile class, instead of extending the Sprite class?  

Thanks!

-Sam

WitchD0ctor

  • Full Member
  • ***
  • Posts: 100
    • View Profile
    • http://www.teleforce-blogspot.com
Extending the Sprite class?
« Reply #1 on: May 20, 2010, 10:20:02 am »
can you post the rest of your code, like where your load your image, and draw it to the screen?
John Carmack can Divide by zer0.

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Extending the Sprite class?
« Reply #2 on: May 20, 2010, 10:20:09 am »
That's because you are copying the image source of your tile class in the constructor. You must pass sf::Image by reference:
Code: [Select]
Tile(const sf::Image &img, const sf::Vector2f &pos, bool sol) : sf::Sprite(img, pos), solid(sol){};
Now it should work.

Note that I passed the vector as const reference, too. You should find some info about reference, it's so useful!

TheSambassador

  • Newbie
  • *
  • Posts: 2
    • View Profile
Extending the Sprite class?
« Reply #3 on: May 20, 2010, 05:35:26 pm »
Thank you!  That's exactly what was wrong!

I know about reference... but, like I said, my C++ is really rusty, and when you don't have to worry about that stuff (Python!) it sorta gets lost.

Thanks again!

aFictitiousForce

  • Newbie
  • *
  • Posts: 4
    • View Profile
Extending the Sprite class?
« Reply #4 on: May 21, 2010, 05:50:14 am »
I have a question about tile-based systems in general.  How do you know which tiles you should draw?  Does SFML automatically cull things that aren't within view?

I can't find a way to get the position of an sf::View so I wasn't too sure of the "correct" way to do this.

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Extending the Sprite class?
« Reply #5 on: May 21, 2010, 07:07:17 am »
Everything is done relative to screen/window size (or at least it can be).

You know what you set and what you build and it's those same numbers you use to decide what to draw and what not to draw.

Spodi

  • Full Member
  • ***
  • Posts: 150
    • View Profile
    • http://www.netgore.com/
Extending the Sprite class?
« Reply #6 on: May 21, 2010, 08:44:27 am »
Quote from: "aFictitiousForce"
I have a question about tile-based systems in general.  How do you know which tiles you should draw?  Does SFML automatically cull things that aren't within view?


Just draw whatever is in the screen, even if its just a pixel or two. Other than that, its pretty much all dependent on your game code and not SFML. That is, how you go about querying your spatial manager or iterating over the tiles in view to draw them.