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

Author Topic: Problem with TileMap  (Read 1980 times)

0 Members and 1 Guest are viewing this topic.

smiaro

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Problem with TileMap
« on: January 26, 2014, 01:07:55 pm »
Hello, could you correct my code?;> Nothing display now...I know there should be positions but I don't know what to do... please help :)
void LevelClass::Load()
{
    LevelClass::ground_texture.loadFromFile( "data/sources/ground.png" );
    LevelClass::wall_texture.loadFromFile( "data/sources/wall.png" );
    LevelClass::ground_sprite.setTexture( ground_texture );
    LevelClass::wall_sprite.setTexture( wall_texture );
    ground_sprite.setPosition( 0, 0 );
    wall_sprite.setPosition( 50, 50 );
   
    std::fstream file( "data/sources/level1.txt", std::ios::in );
    for( int i = 0; i < 14; i++ ) {
        for( int j = 0; j < 14; j++ ) {
            file >> map[ i ][ j ];
        }
    }
    file.close();
   
}

void LevelClass::Draw( void )
{
    for( int i = 0; i < 14; i++ ) {
        for( int j = 0; j < 14; j++ ) {
            if( map[ i ][ j ] == 0 )
                 game.window.draw( LevelClass::wall_sprite );
           
            if( map[ i ][ j ] == 1 )
                 game.window.draw( LevelClass::ground_sprite );
           
        }
    }
}
 

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Problem with TileMap
« Reply #1 on: January 26, 2014, 01:42:34 pm »
Are you calling the clear() function before you draw and display() (both member functions of sf::Renderwindow) after you drew everything you want to show?

Also,

LevelClass::ground_texture.loadFromFile( "data/sources/ground.png" );
LevelClass::wall_texture.loadFromFile( "data/sources/wall.png" );
LevelClass::ground_sprite.setTexture( ground_texture );
LevelClass::wall_sprite.setTexture( wall_texture );


Using LevelClass:: is unnecessary. You are already in the LevelClass scope because of
void LevelClass::Load()


Also, it appears you have a copy of the sf::Renderwindow in your LevelClass object, which won't work either. It is a seperate window from the actual window you are probably trying to use. Instead, you need to have a pointer reference in your level class and pass the sf::Renderwindow object you make to use for your game by reference. Right now it looks like you are trying to use an uncreated window to draw, which won't work. You actually have TWO sf::Renderwindows right now, if I am inferencing from your code correctly.

If it were me though, I would generate and return vector of the sprites I want drawn inside the LevelClass object and pass that vector into the game class. That way your level class need not know anything about the sf::Renderwindow at all. Or even better, I would follow this tutorial - http://www.sfml-dev.org/tutorials/2.1/graphics-vertex-array.php.
« Last Edit: January 26, 2014, 01:58:31 pm by Azaral »

smiaro

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Problem with TileMap
« Reply #2 on: January 26, 2014, 01:57:39 pm »
No, no ;) Everything should be good:) I just want to know how to manipulate textures position in fors :)
Because now I don't have sprite positions :)

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Problem with TileMap
« Reply #3 on: January 26, 2014, 02:01:16 pm »
No, no ;) Everything should be good:) I just want to know how to manipulate textures position in fors :)
Because now I don't have sprite positions :)

Well, you're never setting the position of the sprites. Even if you weren't setting positions, you should be getting all the sprites drawn on top of each other in the upper left hand corner because the sprite position defaults to x = 0, y = 0;

If your not getting that, then you have a problem with your drawing method.

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Problem with TileMap
« Reply #4 on: January 26, 2014, 02:20:08 pm »
You should be getting all of your ground sprites drawn at 0,0
ground_sprite.setPosition( 0, 0 );
and all of your wall sprites at 50,50
wall_sprite.setPosition( 50, 50 );
. If you're not getting that, then there is a problem with how you are going about drawing to the window.

With your current method of drawing (which I don't recommend) you need to set the position of your sprites before
game.window.draw( LevelClass::wall_sprite );
and vice versa for wall sprites.

smiaro

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Problem with TileMap
« Reply #5 on: January 26, 2014, 02:26:41 pm »
This is my C++ project on university that I have to create :(
Could I e-mail you and show my code ?;>
I just started my adventure with SFML and classes in C++ so this is hard for me :(
I just want to create working game and gain good note :)

Azaral

  • Full Member
  • ***
  • Posts: 110
    • View Profile
Re: Problem with TileMap
« Reply #6 on: January 26, 2014, 02:40:19 pm »
PM sent.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10997
    • View Profile
    • development blog
    • Email
AW: Problem with TileMap
« Reply #7 on: January 26, 2014, 06:02:41 pm »
Since it's an university project, why don't you ask your assistants? They at least are getting paid for helping students...  ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/