Hi,
I’m working on a game project. The game is a Quizz where a name of a country appear on the screen and you have to click on the right country on a map.
I’m having trouble with a module of my game : I want that when your mouse is passing over a country, it changes its color, and when the country is found, its color is green etc.
Here is a pic to demonstrate what i’ve achieved (the mouse is over the France) :
Here is how my program works :
• Each country has a pixel array in the Country class :
std::vector<sf::Vector2u> PixelsArray
It is charged at the start of the game using a Boundary-Fill-like algorithm.
The pixel array countains the position of each pixel of the country.
• When the mouse is over a country, a fonction in the Map class is called.
Here is how it works :
void Map::fill(std::vector <sf::Vector2u>& pixelsArray, sf::Color newColor)
{
//Exit the function if we fill with the color of the boundaries
if (newColor == borderColor)
return;
for (int i = 0; i < pixelsArray.size(); i++)
imageMap.setPixel(pixelsArray[i].x, pixelsArray[i].y, newColor);
texture.loadFromImage(imageMap);
map.setTexture(texture);
}
I use an image of the map to transform the pixels and then i am updating the texture of map’s sprite with this image.
The problem is that my map is 3958 * 2596 and it takes about a second to update the texture. So each time i move my mouse over a country, when it wants to update the color to the selected color, my game freeze for one second. I noticed that it doesn’t come from the pixel Array loop but more from the part :
texture.loadFromImage(imageMap);
map.setTexture(texture);
I've tried to reduce the Map's texture to what the player is able to see (the view) so it is by exemple 1700*1050 when the player is on the Europe continent, but it's too slow too.
I've thinked about update texture with :
void sf::Texture::update ( const Uint8 * pixels,
unsigned int width,
unsigned int height,
unsigned int x,
unsigned int y
)
but it would be very difficult to set an IntRect for each country etc.
Actually what could be perfect, is to use something like
void sf::Texture::update ( const Uint8 * pixels )
with the position of each pixels (wich I already have). In other words, updating the texture with only the pixels of a country (with my country's sf::Vector2u pixel array)
Thank you for your attention, I hope you will understand : P