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

Author Topic: Tile map help  (Read 2176 times)

0 Members and 1 Guest are viewing this topic.

Code_Black2she

  • Newbie
  • *
  • Posts: 3
    • View Profile
Tile map help
« on: September 04, 2016, 04:12:03 am »
Yes I'm having trouble with my code. I don't understand why it is not displayed right. I've tryed sfml tutorials on youtube, but no help to me.
Beginner self thaugh programmer.





Code: [Select]

class Tile{
    sf::Sprite spr;
public:
    void LoadTile(sf::Texture& tex){

spr.setTexture(tex);

}

void draw(sf::RenderWindow* rw){

//spr.SetPosition(x,y);
(*rw).draw(spr);


}

sf::Vector2f getsize(){}

};


class Level{
    std::vector<std::vector<Tile*> > map1;
    int h,w;
public:
Level(int x,int y){

map1.resize(x);
for(int i=0;i<x;i++)
    map1.at(i).resize(y,0);
this->h=x;
this->w=y;


}
void AddTile(int x,int y,Tile* tile){

map1[x][y]=tile;


}
Tile* GetTile(int x,int y){return map1[x][y];}


};


















void LoadLevel(int*level){
    Level*p=new Level(2,2);
    Tile*tile;
    int currentTile=0;
    sf::Texture grass,brick;
    grass.loadFromFile("321.jpg");
brick.loadFromFile("123.jpg");

for(int i=0;i<2;i++)
    for(int j=0;j<2;j++){

    currentTile= level[i+j];

    std::cout<<currentTile;
    if(currentTile==1){
        tile=new Tile;
        (*tile).LoadTile(brick);
    }
    else if(currentTile==0){
        tile=new Tile;
        (*tile).LoadTile(grass);}



        (*p).AddTile(i,j,tile);
        }

for(int i=0;i<2;i++)
    for(int j=0;j<2;j++){

      Tile* tile1 =(*p).GetTile(i,j);

      tile1->draw(&window);




    }


}




int main(){

int level[4]={0,1,0,1};
sf::RenderWindow window(sf::ViderMode(800,600)"game");
while(window.isOpen()){
   sf::Event eve;
   while(window.pollEvent(eve)){
      if(eve.type==sf::Event::Closed)
        window.close();
       
     
LoadLevel(level);
     
    window.display();

   }




}

std::cout<<"Hello World!  "<<count;

return 0;

Gambit

  • Sr. Member
  • ****
  • Posts: 283
    • View Profile
Re: Tile map help
« Reply #1 on: September 04, 2016, 07:20:04 am »
Are you calling your draw method anywhere? Additionally if you intend to draw anything with sfml you should consider deriving sf::Drawable.

Code_Black2she

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Tile map help
« Reply #2 on: September 04, 2016, 07:42:38 am »
Yes I am but I need help on understanding how to display tiles on screen

Mr_Blame

  • Full Member
  • ***
  • Posts: 192
    • View Profile
    • Email
Re: Tile map help
« Reply #3 on: September 04, 2016, 11:29:28 am »
First thing, that you should do is redesigning your classes, tiles must be held in chucks, in order to reducd draw call amount, and your implementation doesn't provide that.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Tile map help
« Reply #4 on: September 05, 2016, 10:05:11 pm »
I see that you are calling "draw" but only once, during "LoadLevel". In every cycle/frame/tick of your main loop, you need to clear the window, draw the things on the window, and display the window. Every. Time.
while (window.isOpen())
{
    // process events

    // perform updates

    window.clear();
    window.draw(things); // draw everything to the window here
    window.display();
}
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything