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

Author Topic: something on my screen in sprite  (Read 3140 times)

0 Members and 1 Guest are viewing this topic.

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
something on my screen in sprite
« on: April 19, 2015, 03:14:14 pm »
i am writing space invader game.
im creating multiple aliens from a single alien image , so i could save space.
heres my code:
sprite.setTextureRect(sf::IntRect(0, 0, texture.getSize().x*  2 , texture.getSize().y));
when i run it, i get this green thing, see picture below

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: something on my screen in sprite
« Reply #1 on: April 19, 2015, 03:24:32 pm »
Your texture rect is bigger than the texture, how should this work? (without texture repeating)

And please don't open those silly polls ::)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

lorence30

  • Full Member
  • ***
  • Posts: 124
    • View Profile
    • Email
Re: something on my screen in sprite
« Reply #2 on: April 19, 2015, 06:50:04 pm »
hello Mr. Nexus you are very active in this forum.


Your texture rect is bigger than the texture, how should this work? (without texture repeating)

do you mean the solution here is, i create an empty texture first then i will put there this
sprite.setTextureRect(sf::IntRect(0, 0, texture.getSize().x*  2 , texture.getSize().y));?

or any recommend? sorry im not good in english :(


And please don't open those silly polls ::)
what do you mean? i just think add poll will create a new topic in this forum, im new to this forum im sorry if im doing wrong

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: something on my screen in sprite
« Reply #3 on: April 19, 2015, 07:59:27 pm »
I'd advise making a class for the alien and putting a sprite and texture in it as a simple space invaders game shouldn't be too processor expensive pretty much however you do it. YOu then make an array of the alien class and they all have their own sprite. If you're certain about making all the aliens in a single texture, try looking at rendertextures and make one by drawing a grid of aliens to it. Also, use the NEW TOPIC button rather than the NEW POLL button to open a new thread.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: something on my screen in sprite
« Reply #4 on: April 19, 2015, 08:09:52 pm »
do you mean the solution here is, i create an empty texture first then i will put there this
sprite.setTextureRect(sf::IntRect(0, 0, texture.getSize().x*  2 , texture.getSize().y));?
No. Why getSize().x * 2? That is twice the texture's width... What do you want to achieve?
Have you read the tutorial about textures and understood what the texture rect exactly means?

what do you mean? i just think add poll will create a new topic in this forum, im new to this forum im sorry if im doing wrong
The button for that is "New Topic", not "New Poll" ;)

I'd advise making a class for the alien and putting a sprite and texture in it as a simple space invaders game shouldn't be too processor expensive pretty much however you do it.
You should not store the texture inside the alien class, because it is needlessly duplicated for every alien. Pass a texture reference to the alien's constructor instead.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: something on my screen in sprite
« Reply #5 on: April 19, 2015, 08:14:31 pm »
You should not store the texture inside the alien class, because it is needlessly duplicated for every alien. Pass a texture reference to the alien's constructor instead.
Is there any reason not to take this further and do the following? Pass a sprite reference in the constructor, then use float positions in the class, give the class a draw function that should be called between window.clear() and window.display() and then make each call of the draw function move the refenced sprite (which can be shared between objects) to the correct position and print it on the screen. Thus making many objects that use the same texture only uses one texture and one sprite.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: something on my screen in sprite
« Reply #6 on: April 19, 2015, 08:45:05 pm »
Yes, but like that you're just duplicating the information of sf::Sprite. Having many sprites is not a problem, because they're lightweight -- in contrast to textures.

It can pay off to follow such an approach, but I'd only do it for the sake of separating graphics and game logic. That is, the entity contains only position, velocity etc. as sf::Vector2f members, and it knows nothing about drawing. Then, another class takes care of drawing the entity, either by creating new sprites on the fly, or by using some map to cache them, or... There are many approaches, and there have been quite a few discussions about this in this forum, you might want to search a bit :)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: