SFML - dodawanie tekstur c++

0

Witam. Mam problem z SFML, gdyż nie ładuję on poprawnie tekstur. Oczywiście problem pojawia się przy przenoszeniu obiektów do vectora, ale przewaliłem milion postów na ten temat, a kod jak nie działał tak działać nie chce.

Nie wiem co robie źle, ale tekstury nie są poprawnie ładowane do obiektów Ball:

//BallTextureHolder.h

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

namespace Textures
{
	enum ID { czerwona , zielona , pomaranczowa ,  niebieska};
}

class BallTextureHolder
{

	sf::Texture				mBallTexture[5];

public:

	sf::Texture getTexture(int);
	BallTextureHolder();
	~BallTextureHolder();
};

 
//Ball.h

#pragma once
#include <SFML/Graphics.hpp>
#include "BallTextureHolder.h"
#include "Cannon.h"

class Ball
{
	public:
		friend class World;

		Ball(bool inwall);
		Ball::Ball(const Ball& copy);
		~Ball();

		void			setVelocity(sf::Vector2f velocity);
		void			setVelocity(float vx, float vy);
		sf::Vector2f	getVelocity() const;
	
		sf::Sprite		getSprite();


		bool			isRandomized();

		//friend void World::setBallTexture();


	private:

		sf::Vector2f			mVelocity;

		sf::Sprite				mBallSprite;

		bool					inWall;
		int					    color;
		bool					randomized;
};

 
//BallTextureHolder.cpp

#include "BallTextureHolder.h"
#include <iostream>

BallTextureHolder::BallTextureHolder()
{
	std::cout << "dsdasdad";

	if (!mBallTexture[0].loadFromFile("kulkaczerwona.png"))
	{
		std::cout << "Nie WCZYTANO TEKSTURY!";
	}
	if (!mBallTexture[1].loadFromFile("kulkazielona.png"))
	{
		std::cout << "Nie WCZYTANO TEKSTURY!";
	}
	if (!mBallTexture[2].loadFromFile("kulkapomaranczowa.png"))
	{
		std::cout << "Nie WCZYTANO TEKSTURY!";
	}
	if (!mBallTexture[3].loadFromFile("kulkaniebieska.png"))
	{
		std::cout << "Nie WCZYTANO TEKSTURY!";
	}
	if (!mBallTexture[4].loadFromFile("NoTexture.png"))
	{
		std::cout << "Nie WCZYTANO TEKSTURY!";
	}
}

sf::Texture BallTextureHolder::getTexture(int val)
{

	switch (val)
	{
	case 0:
		return mBallTexture[0];
		break;
	case 1:
		return mBallTexture[1];
		break;
	case 2:
		return mBallTexture[2];
		break;
	case 3:
		return mBallTexture[3];
		break;
	default:
		std::cout << "BRAK TEKSTURY!" << std::endl;
		return mBallTexture[4];
		break;

	}

}

BallTextureHolder::~BallTextureHolder()
{
}
 
//Ball.cpp

#include "World.h"
#include <cstdlib>
#include <iostream>


Ball::Ball(bool inwall) : mVelocity(0.f, 0.f), inWall(inwall), randomized(false)
{
	if (!randomized)
	{
		color = rand() % 4;
		randomized = true;
	}
    
	if (!inwall)
	{
	}
}

sf::Sprite Ball::getSprite()
{
	return mBallSprite;
}

Ball::~Ball()
{
}

bool	Ball::isRandomized()
{
	return randomized;
}
 
//World.h

#pragma once
#include <vector>
#include <memory>
#include "Cannon.h"
#include "Ball.h"

class World
{
	private:

	    Cannon cannon;
		BallTextureHolder BallTextures;
		std::vector<std::unique_ptr<Ball>> BallsHolder;

		sf::RenderWindow&			mWindow;
		bool						ballReady;

	public:
	
		World(sf::RenderWindow& window);
		~World();

		sf::Vector2f getCannonPosition();
		
		void draw();
		void update(sf::Time dt, bool mRotateRight,bool mRotateLeft,bool mShoot);

		void setBallTexture();
};

 
//World.cpp
#include "World.h"
#include <iostream>

World::World(sf::RenderWindow& window) :
mWindow(window),
ballReady(false),
BallTextures()
{
}


World::~World()
{
}

void World::draw()
{
	mWindow.setView(mWindow.getDefaultView());
	mWindow.draw(cannon.getSprite());

	for (std::vector<std::unique_ptr<Ball>>::iterator iter = BallsHolder.begin(); iter != BallsHolder.end(); iter++)
	{
		mWindow.draw((*iter)->getSprite());
	}
}

void World::update(sf::Time dt, bool mRotateRight,bool mRotateLeft,bool mShoot)
{
	if (mRotateRight)
		cannon.rotate(dt.asSeconds());

	if (mRotateLeft)
		cannon.rotate(-dt.asSeconds());

	else
	{
		sf::Time temp;
		cannon.rotate(temp.asSeconds());
	}

	if (ballReady)
	{
		//std::cout << "Jestem tu";
	}

	else
	{
		
		std::unique_ptr<Ball> temp(new Ball(false));
		
		BallsHolder.push_back(std::move(temp));
		
		std::vector<std::unique_ptr<Ball>>::iterator iter = BallsHolder.end() - 1;
		setBallTexture();

		(*iter)->getSprite().setPosition(cannon.getCannonPosition());

		ballReady = true;
	}
}

void World::setBallTexture()
{
	for (std::vector<std::unique_ptr<Ball>>::iterator iter = BallsHolder.begin(); iter != BallsHolder.end(); iter++)
	{
		(*iter)->mBallSprite.setTexture(BallTextures.getTexture((*iter)->color));
	}
}



 

Mam nadzieje, że ktoś przewali się przez ten kod i mi pomoże. Chce aby kulka miała losowo jedną z 4 tekstur. To nie są wszystkie pliki projektu, ale reszta nie gra tutaj większej roli.

Dziękuję.

0

Klasy zaprzyjaźnione to zuo.
Rule of three zna?
Jak nie, to spróbuj zdefiniować kopiujący operator przypisania dla klasy Ball.

0

W sf::Texture BallTextureHolder::getTexture(int val) zwracasz kopie tekstury co jest kompletną głupotą. Powinnienneś zwrócic wskaźnik do niej.
Poza tym jeśli wszystkie Ball są takie same a jedynie róznią się kolorem, wystarczy ci jedna tekstura i nastepnie manipulowanie jej kolorem

1 użytkowników online, w tym zalogowanych: 0, gości: 1