-
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.
-
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.
-
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.
-
Actually i fixed it. Apparently you need to create a sf::renderwindow first before you can do anything.
-
I thought it was fixed. Are you on Linux? Do you use the latest sources or the RC?
-
No, i'm on windows vista using the 2.0 release candidate.
-
Can you write a complete and minimal example that reproduces the problem? It shouldn't be hard since your code is already small.
-
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.
-
What's your graphics card? Are your drivers up-to-date?
-
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.