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

Author Topic: Drawing sprites :-\  (Read 2524 times)

0 Members and 1 Guest are viewing this topic.

pogrady

  • Newbie
  • *
  • Posts: 21
    • View Profile
Drawing sprites :-\
« on: December 12, 2012, 04:29:46 am »
Hello,

I am trying to draw a sprite array, but the screen comes white with no textures.  I'm wondering what I am doing wring:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <fstream>
#include <iostream>



int main()
{
        std::ifstream file("hills.map");
        if(!file.good())
        {
                std::cout<<"failed to load map."<<std::endl;
                return 0 ;
        }
        int width = 0;
        int length = 0;

        file>>width;
        file>>length;

        char** Map = new char*[width];
        for(int i = 0; i < length; i++)
        {
                Map[i] = new char[length];
        }

        for(int x = 0; x < width; x++)
        {
                for(int y = 0; y < length; y++)
                {
                        Map[x][y] = file.get();
                }

                file.get(); //get the \n character
        }

        sf::Sprite** SpriteArray = new sf::Sprite*[width];
        for(int x = 0; x < width; x++)
        {
                SpriteArray[x] = new sf::Sprite[length];
        }

        //load the sprites into memory

        sf::Texture grass;
        sf::Texture road;

        if(!grass.loadFromFile("grass.png"))
                std::cout<<"failed to load grass from file."<<std::endl;

        if(!road.loadFromFile("road.png"))
                std::cout<<"Failed to load road from file."<<std::endl;

        for(int x = 0; x < width; x++)
        {
                for(int y = 0; y < length; y++)
                {
                        if(Map[x][y] == '#')
                                SpriteArray[x][y].setTexture(grass);
                        else
                                SpriteArray[x][y].setTexture(road);

                        SpriteArray[x][y].setPosition(float(x*64), float(y*64));
                }
        }


        file.close();

    sf::RenderWindow window(sf::VideoMode(800, 800), "SFML works!");
       
    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

                //clear the backbuffer
        window.clear();

                sf::Sprite t;
                //draw stuff
                for(int x = 0; x < width; x++)
                {
                        for(int y = 0; y < length; y++)
                        {
                                t = SpriteArray[x][y];
                                window.draw(t);
                        }
                }

                //display the contents
        window.display();
    }

        //free the memory that the program used
        for(int x = 0; x < width; x++)
        {
                delete[] Map[x];
                delete[] SpriteArray[x];
        }
        delete[] Map;
        delete[] SpriteArray;

    return 0;
}
 

thanks for any help you can provide.

masskiller

  • Sr. Member
  • ****
  • Posts: 284
  • Pointers to Functions rock!
    • MSN Messenger - kyogre_jb@hotmail.com
    • View Profile
    • Email
Re: Drawing sprites :-\
« Reply #1 on: December 12, 2012, 05:09:48 am »
You should have a smaller amount of code, by locating the problem and posting only that piece.

The problem you described is almost always due to having the sprite's texture destroyed before the sprite is drawn. I'll check the code further probably tomorrow as it is quite late here and I am going to sleep, but check the lifetime of the texture, that might solve it.
Programmer, Artist, Composer and Storyline/Script Writer of "Origin of Magic". If all goes well this could turn into a commercial project!

Finally back into the programming world!

pogrady

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Drawing sprites :-\
« Reply #2 on: December 12, 2012, 06:10:16 am »
Thanks for replying.  The texture object is local to the main.  I tried another way, thinking that it might be the dynamic memory, but it results in the same white window:
for(int x = 0; x < width; x++)
                {
                        for(int y = 0; y < length; y++)
                        {
                                if(Map[x][y] == '#')
                                        t.setTexture(grass);
                                else
                                        t.setTexture(road);

                                xpos = x*64;
                                ypos = y*64;
                                t.setPosition(float(xpos), float(ypos));
                                window.draw(t);
                        }
                }
 

here i'm just using the same sprite object over and over again.  Before I was using an array of sprites.  However the result is still the same. 

pogrady

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Drawing sprites :-\
« Reply #3 on: December 12, 2012, 06:25:53 am »
Actually i fixed it. Apparently you need to create a sf::renderwindow first before you can do anything. 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Drawing sprites :-\
« Reply #4 on: December 12, 2012, 07:38:28 am »
I thought it was fixed. Are you on Linux? Do you use the latest sources or the RC?
Laurent Gomila - SFML developer

pogrady

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Drawing sprites :-\
« Reply #5 on: December 13, 2012, 05:59:18 am »
No, i'm on windows vista using the 2.0 release candidate.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Drawing sprites :-\
« Reply #6 on: December 13, 2012, 07:50:59 am »
Can you write a complete and minimal example that reproduces the problem? It shouldn't be hard since your code is already small.
Laurent Gomila - SFML developer

pogrady

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Drawing sprites :-\
« Reply #7 on: December 14, 2012, 07:13:17 am »
This was taken from the sample given by the documentation on the main page.  The comments explain the situation.
#include <SFML/Graphics.hpp>
#include <SFML/System.hpp>

int main()
{
        //if you uncomment this code, the sprite will appear white.
         /*sf::Texture texture;
     if (!texture.loadFromFile("battlemage.gif"))
         return EXIT_FAILURE;
     sf::Sprite sprite(texture);
*/

         // Create the main window
     sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");
 
     // Load a sprite to display

         //if you uncomment this code, the sprite will appear normally.
         /*sf::Texture texture;
     if (!texture.loadFromFile("battlemage.gif"))
         return EXIT_FAILURE;
     sf::Sprite sprite(texture);*/
   
 
     // Start the game loop
     while (window.isOpen())
     {
         // Process events
         sf::Event event;
         while (window.pollEvent(event))
         {
             // Close window : exit
             if (event.type == sf::Event::Closed)
                 window.close();
         }
 
         // Clear screen
         window.clear();
 
         // Draw the sprite
         window.draw(sprite);
 
         // Update the window
         window.display();
     }
 
     return EXIT_SUCCESS;
 }
 

Thanks for writing this library. If find it much easier to use then SDL, and it is much more intuitive.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Drawing sprites :-\
« Reply #8 on: December 14, 2012, 08:07:17 am »
What's your graphics card? Are your drivers up-to-date?
Laurent Gomila - SFML developer

pogrady

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Drawing sprites :-\
« Reply #9 on: December 15, 2012, 02:00:33 pm »
I'm on a laptop with an integrated graphics chip.  I was under the impression that windows update was installing new versions of my hardware drivers, but i know not now.

Thanks for the suggestion, that fixed the problem.

Thanks fro your help.
« Last Edit: December 15, 2012, 02:06:32 pm by pogrady »