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

Author Topic: Trying to draw my redundant sprites with a function that loops through an array  (Read 920 times)

0 Members and 1 Guest are viewing this topic.


  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Hi guys,

I am pretty new at programming and definitely new at SFML. I have been trying to create a function that helps me to draw repetitive things like a floor etc. It is supposed to be dynamic. For the floor drawing e.g. you can say X is my dynamic axis that will be drawn onto. You can then give a fixed starting position at the Y axis. You also pass a pointer to the window and the shape you created beforehand and optimally already bound a texture to. You then pass sf::Vector2f 's for shapeSizeand shapeOrigin.

My result unfortunately that the resulting rectangular structure is drawn but WITHOUT TEXTURE.  It is just white.

Inside the game loop after window.clear() I call the function to draw the floor with suiting parameters passed:
Code: [Select]
drawRectangleShapesRep(&window, &floorShape, sf::Vector2f(50.0f, 50.0f), sf::Vector2f(25.0f, 25.0f), 400.0f, 0.0f, 'X');

Before the game loop of course I did this:
Code: [Select]
//Create floor
sf::RectangleShape floorShape;
sf::Texture floorTexture;
floorShape.setSize(sf::Vector2f(50.0f, 50.0f));
floorShape.setOrigin(0.0f, 25.0f);

The function looks like this:

Code: [Select]
//Draw rectangle shapes repetitively
void drawRectangleShapesRep(sf::RenderWindow* window, sf::RectangleShape* rectShape, sf::Vector2f shapeSize, sf::Vector2f shapeOrigin, float posOnFixedAxis, float startPosOnDynamicAxis, char dynamicAxis) {

int arraySize = 0;

//Dynamically calculate array elements / size
if(dynamicAxis == 'X' || dynamicAxis == 'x') {
arraySize = std::ceil(((float) WINDOW_WIDTH / rectShape->getSize().x)) + 1;
} else if(dynamicAxis == 'Y' || dynamicAxis == 'y') {
arraySize = std::ceil(((float) WINDOW_HEIGHT / rectShape->getSize().y)) + 1;

//Create array
sf::RectangleShape shapeArray[arraySize] = { *rectShape };

//Start for loop
for(int i = 0; i < arraySize; i++) {
sf::RectangleShape* currentTile = &(shapeArray[i]);

if(dynamicAxis == 'X' || dynamicAxis == 'x') {
currentTile->setPosition(((startPosOnDynamicAxis + currentTile->getSize().x) * float(i)), posOnFixedAxis);
} else if(dynamicAxis == 'Y' || dynamicAxis == 'y') {
currentTile->setPosition(posOnFixedAxis, ((startPosOnDynamicAxis + currentTile->getSize().y) * float(i)));
} else {
std::cout << "Choice of char for dynamic axis was wrong. Only X, x, Y and y are allowed" << std::endl;



« Last Edit: April 27, 2017, 09:30:19 pm by overflyer »


  • Jr. Member
  • **
  • Posts: 95
    • View Profile
Firstly, put the code inside the code tag please.

As to the problem of yours, the most common reason for seeing a white space instead of the respective texture is that the texture is out of scope. I suggest you to write/use a ResourceManager class, which will take care of it and you won't be bothered by this issue again.