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

Author Topic: Alternative to drawing all of an array.  (Read 2229 times)

0 Members and 1 Guest are viewing this topic.

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Alternative to drawing all of an array.
« on: October 25, 2022, 04:12:41 am »
I have an array of sprites that I want to have all of it drawn every frame but I heard somewhere that you cant do that.Am I correct in understanding that its not possible to draw an entire array?If so,what is the alternative?Help would be much appreciated.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Alternative to drawing all of an array.
« Reply #1 on: October 25, 2022, 08:12:45 am »
Not sure what exactly you've heard, but you can't draw an array directly, but you can iterate over the array and draw one sprite at a time, so you do end up drawing the whole array's contents.
« Last Edit: October 25, 2022, 03:54:09 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Alternative to drawing all of an array.
« Reply #2 on: October 25, 2022, 03:27:42 pm »
That's actually the problem.Because its iterating through the array its not displaying all of it at once.It only shows the last array number chosen.Any ideas on how to iterate through this in a way that it displays all of the array at once?

#include <SFML\Graphics.hpp>
#include <iostream>
#include "declare.h"
#include <fstream>
using namespace sf;
using namespace std;

int beforetilenum;
int tilenum;
int usedtiles;


int main()
{
       
        Texture floor000;
Sprite tileguide;
Sprite tile[50];
floor000.loadFromFile("image/floor000.png",IntRect(120,0,120,60));
tileguide.setTexture(floor000);
       
        RenderWindow window(VideoMode(600, 480),"Blupi Empire");
    while (window.isOpen())
    {
            Event event;
            while (window.pollEvent(event))
            {
                if (event.type == Event::Closed)
                    window.close();
            }  
         
         
         
         
         
         
                ///go through tile[] array number////
                if(beforetilenum<50)
                {
                        beforetilenum+=1;
                }      
                if(beforetilenum==50)
                {
                        beforetilenum=0;
                }      
                /////tilenum = unused sprites number/////////
                if(tile[beforetilenum].getTexture()==NULL)
                {
                        tilenum=beforetilenum;
                }      
                else
                {
                        usedtiles=beforetilenum;
                }
         
       
         
         
         
         
         
         
         
         
         
         
         
         
         
          //////GRID WEB//////////////  
          Vector2i Position=Mouse::getPosition(window);
          if(tileguide.getPosition().y<Position.y&&tileguide.getPosition().x<Position.x)
          {
            //bottom right
                       
            tileguide.setPosition(tileguide.getPosition().x+60,tileguide.getPosition().y);
            tileguide.setPosition(tileguide.getPosition().x,tileguide.getPosition().y+30);

          }  
          if(tileguide.getPosition().y>Position.y&&tileguide.getPosition().x>Position.x)
          {
            //topleft
            tileguide.setPosition(tileguide.getPosition().x-60,tileguide.getPosition().y);
            tileguide.setPosition(tileguide.getPosition().x,tileguide.getPosition().y-30);

          }  
         
          if(tileguide.getPosition().y<Position.y&&tileguide.getPosition().x>Position.x)
          {
            //bottomleft
            tileguide.setPosition(tileguide.getPosition().x-60,tileguide.getPosition().y);
            tileguide.setPosition(tileguide.getPosition().x,tileguide.getPosition().y+30);

                       
          }    
       
          if(tileguide.getPosition().y>Position.y&&tileguide.getPosition().x<Position.x)
          {
            //top right
            tileguide.setPosition(tileguide.getPosition().x+60,tileguide.getPosition().y);
            tileguide.setPosition(tileguide.getPosition().x,tileguide.getPosition().y-30);

                       
          }
         
         
         
         
         
         
         
         
         
         
         
         
         
          ////////PLACE SELECTION////////////////
          if(Mouse::isButtonPressed(Mouse::Left))
          {
                tile[tilenum].setTexture(floor000);
                tile[tilenum].setPosition(tileguide.getPosition().x,tileguide.getPosition().y);
                window.draw(tile[tilenum]);
                ofstream myfile;
                myfile.open ("map.txt");
                myfile << "tile["<<tilenum<<"].setTexture(floor000);"<<endl;
                myfile << "tile["<<tilenum<<"].setPosition("<<tileguide.getPosition().x<<","<<tileguide.getPosition().y<<");"<<endl;
                myfile.close();
               
               
          }
          window.display();//The only way that I see to display every part of the array is to display it twice,which flickers of course.
         
         
         
         
         
         
         

         
               
         
         
         
         
         
         
         
         
         
         
          window.clear();
          window.draw(tileguide);
          window.display();
        }
       
}
 

euscar

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Alternative to drawing all of an array.
« Reply #3 on: October 25, 2022, 03:48:41 pm »
Usually a for loop is used with arrays.

Isn't it enough to draw each frame with the window.draw () function? In reality this function places them only in the frame being created, then the window.display () function intervenes to display the result.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Alternative to drawing all of an array.
« Reply #4 on: October 25, 2022, 03:56:52 pm »
As euscar said, you shouldn't iterate over the sprites one sprite per frame, but have a separate for / for-range loop to go over all sprites and call draw for them.

while(window.isOpen())
{
// ...
window.clear();
// ...
for (auto& sprite : tile)
{
    window.draw(sprite);
}
// ...
window.display();
// ...
}
 
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Alternative to drawing all of an array.
« Reply #5 on: October 25, 2022, 06:02:46 pm »
Thanks a bunch!That definitely worked.I'm not sure how it works because i'm just a beginner in c++ but i'll get it eventually.Thanks again!

euscar

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Alternative to drawing all of an array.
« Reply #6 on: October 25, 2022, 10:37:18 pm »
If you are just a beginner in C++ I recommend the following book which introduces you to both C++ and SFML:

https://www.packtpub.com/product/beginning-c-game-programming-second-edition/9781838648572