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

Author Topic: Why are my tiles not displayed?  (Read 2653 times)

0 Members and 1 Guest are viewing this topic.

Hymirth

  • Newbie
  • *
  • Posts: 3
    • View Profile
Why are my tiles not displayed?
« on: May 07, 2013, 07:58:23 pm »
Hi, I'm really new to SFML and objects and classes so please bare with me ^^.
I'm trying to just get started and display a set of 32x24 tiles depending on the value of the grid. My problem is that the sprite's just aren't displayed.

I know that the array 'grid' is full of 'G's so that shouldn't be a problem. SIZEX and SIZEY are both 10.

and as an added extra can anybody tell me why I get an unhandled exception error when I try to load from file ^^

Thanks.


void displayFloor(char grid[][SIZEY], sf::RenderWindow& window)
{
        sf::Texture grass;
        //if (!grass.loadFromFile("Grass.png"))
        //{
        //      cout << "Can't load grass";
        //}
        sf::Sprite grassSprite;
        //grassSprite.setTexture(grass);
        grassSprite.setColor(sf::Color(50, 255, 50, 0));
        grassSprite.setScale(32,24);
        for( int row(1); row<=SIZEY; ++row )
        {
                for( int col(1); col<=SIZEX; ++col )
                {
                        float x = 0 + col * 32;
                        float y = 0 + row * 24;
                        switch( grid[row][col] )
                        {
                        case 'G':
                                grassSprite.setPosition( x, y );
                                window.draw( grassSprite );
                                break;
                        }
                }
        }
}

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Why are my tiles not displayed?
« Reply #1 on: May 07, 2013, 10:50:05 pm »
Read this and post again. What you posted is not enough for us to help you.
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Raphman

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Why are my tiles not displayed?
« Reply #2 on: May 07, 2013, 10:58:49 pm »
Looks like you're not loading the texture, that's your problem.  Also refer to zsbzsb's post, and give us a nice minimal piece of code to work with.  You might want to reread the tutorials.

Hymirth

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Why are my tiles not displayed?
« Reply #3 on: May 07, 2013, 11:30:08 pm »
Well yeah i feel like a bit of an idiot for not realising that simply changing the colour doesn't do anything if there is no texture loaded.

When I run the code, attempting to load the texture, it breaks at the line:

if (!grass.loadFromFile("Grass.png"))

with the error:

"Unhandled exception at 0x71521f34 in First SFML work.exe: 0xC0000005: Access violation reading location 0x00304000."

I have the image, with the correct name, of course, in the debug folder for the project (Visual Studio 2010\Projects\First SFML work\Debug) which I believe is the correct place but just incase also stored it in the folder (Visual Studio 2010\Projects\First SFML work\First SFML work) as well.

Having looked at other forum posts I don't think this is a coding error but something to do with how i've set up SFML or where I've stored the image. However the only similar errors i can see have been when loading audio, not textures, if that's of any consequence.

zsbzsb

  • Hero Member
  • *****
  • Posts: 1409
  • Active Maintainer of CSFML/SFML.NET
    • View Profile
    • My little corner...
    • Email
Re: Why are my tiles not displayed?
« Reply #4 on: May 07, 2013, 11:35:58 pm »
Make sure that if your application is set to debug that you are loading the SFML debug libraries. And if you are compiling in release make sure you are linking to the SFML release libraries.

Also what compiler do you have and where did you get SFML from?
Motion / MotionNET - Complete video / audio playback for SFML / SFML.NET

NetEXT - An SFML.NET Extension Library based on Thor

Raphman

  • Newbie
  • *
  • Posts: 40
    • View Profile
    • Email
Re: Why are my tiles not displayed?
« Reply #5 on: May 07, 2013, 11:47:12 pm »
So if I'm understanding you correctly, you have the file in the project's directory as well?

If you don't specify a path or have a working directory set, the program will look in the project's directory for the file.

Secondly, it's pretty silly to load the texture each time you call that draw function, you should  handle that in a better way.

Also follow zsbzsb's advice.

Hymirth

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Why are my tiles not displayed?
« Reply #6 on: May 08, 2013, 12:03:50 am »
Okay so using the debug libraries fixed it, thank you.

Also thank you Raphman for pointing out that i was loading the texture too often ^^

 

anything