Jump to content

  • Log In with Google      Sign In   
  • Create Account

FREE SOFTWARE GIVEAWAY

We have 4 x Pro Licences (valued at $59 each) for 2d modular animation software Spriter to give away in this Thursday's GDNet Direct email newsletter.


Read more in this forum topic or make sure you're signed up (from the right-hand sidebar on the homepage) and read Thursday's newsletter to get in the running!


PhantomRonin

Member Since 12 Feb 2012
Offline Last Active Jan 29 2013 09:58 AM

Posts I've Made

In Topic: SFML help

16 December 2012 - 09:14 AM

This is what my main looks like

[source lang="cpp"]int main(){ sf::RenderWindow window(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT), WINDOW_TITLE); window.setTitle(WINDOW_TITLE); GraphicsManager graphicsManager; graphicsManager.loadTextures(); Player player; player.setSprite( graphicsManager.getSprite(1) ); player.setWeaponSprite( graphicsManager.getSprite(3) ); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) window.close(); } player.getInput(); window.clear(); window.draw(player.getSprite()); window.display(); } return 0;}[/source]

In Topic: SFML help

16 December 2012 - 08:59 AM

As JTippets mentioned, just give each object its own sprite. You could share textures between similar types of objects, but you don't need to do all the hassle of a "graphics manager", especially when that "manager" doesn't actually manage the lifetime of your graphics.

But hey, if every Enemy has the same appearance, you could even share their texture statically:

class Enemy
{
	 public:
		  static bool LoadResources(); //Loads all the static textures. Called once at program startup.
	
	 	 enum class Appearance {Normal, Fast, Powerful, Dead};

	 public:
	 Enemy();

	 void SetAppearance(Appearance appearance)
	 {
		  if(appearance == Appearance::Fast)
		  {
			   this->sprite.setTexture(Enemy::fastTexture);
		  }
		  else if(appearance == Appearance::Powerful)
		  {
			   this->sprite.setTexture(Enemy::powerfulTexture);
		  }
		  else if(appearance == Appearance::Dead)
		  {
			   this->sprite.setTexture(Enemy::deadTexture);
		  }
		  else //Normal appearance.
		  {
			   //Randomly choose one of the two 'normal' enemy textures.

			   if( random choice 1 )
			   {
			   	 this->sprite.setTexture(Enemy::normalTexture1);
			   }
			   else
			   {
					this->sprite.setTexture(Enemy::normalTexture2);
			   }
		  }

	 }

	 private:
	 sf::Sprite sprite; //Each enemy has it's own sprite.

	 static sf::Texture normalTexture1; //Normal enemy texture, shared between all 'Enemy' instances.
	 static sf::Texture normalTexture2; //An alternative texture for normal enemies.
	 static sf::Texture fastTexture; //Texture for 'fast' enemies.
	 static sf::Texture powerfulTexture; //Texture for 'powerful' enemies.
	 static sf::Texture deadTexture; //Texture for dead enemies.
	
};



Each object does have its own sprite. I load up all the textures in the graphics class. Then I create a sprite and send it over to another class. I only do this once per class. Anyway thanks for the advice everyone

In Topic: SFML help

15 December 2012 - 10:26 PM

As a general rule of thumb, I pass structs and classes by const reference or non-const references, and only copy something when I actually want a duplicate.
Basic variables like int and float I pass by value, normally.

Could you show a real piece of code as an example, to clarify your question?


Basically how I have it set up right now I have Player class, Enemy class, and Graphics class.

The graphics calls loads all the textures. Then I send over the sprite using the function below to the class that needs it.

[source lang="cpp"]sf::Sprite GraphicsManager::getSprite(int textureID){ sf::Sprite sprite; switch(textureID) { case 1: sprite.setTexture(player); return sprite; case 2: sprite.setTexture(enemie); return sprite; case 3: sprite.setTexture(ball); return sprite; }}[/source]
Thinking about it now would it be better to keep everything in the graphics class and do something like this when drawing?

[source lang="java"]window.draw( graphics.getsprite(player.x,player.y,player_spriteID);[/source]

In Topic: SFML help

15 December 2012 - 09:38 PM

Depends on what you want. It's safe to not use pointers, but you have to realize that the two classes will each have their own copy of the sf::Sprite if you don't use references/pointers. If you want them to share the same sf::Sprite, you have to use references/pointers.


Im talking about from an efficacy point of view. Will my code run slowly if I am sending a hand full of sprites around at start up?

In Topic: Perlin Noise help

19 November 2012 - 11:03 PM

The thing about Perlin noise is that at frequency=1, there is roughly 1 feature per unit. At frequency=3, in your case, you have roughly 3 features per unit. However, you are sampling an area 64x48 units in size, so you will end up with a mapping containing roughly 192x144 features, which is going to look a whole lot like white noise. You should try disconnecting your pixel size from the actual size of the area of noise that you map, if you want to get anything useful.


Thanks I think I figured it out

PARTNERS