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

Author Topic: pixel map question  (Read 3351 times)

0 Members and 1 Guest are viewing this topic.

starkhorn

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
pixel map question
« on: March 09, 2016, 04:42:02 am »
Hi Folks,

I am trying to do a RISK type game where I have regions and you move armies onto each region etc.

Now I have large images which 5680 X 4178 pixels. However it only contains an outline of the region. I've tried uploading to tinypic but it doesn't display very well.



So I have created another image with the interior of the region as well as just the border



So I then use the 2nd image which the pixels within the region set to white to create a pixel vector. The below code, basically checks each pixel for color. If it is white, then I add a sf::Vector2i entry to a vector for that region.


vector<sf::Vector2i> regionLandPixelList; //stores a list of pixels for this region that denotes a land pixel

void drawEngine::initRegionPixelVector(Region *passed_Region)
{
        sf::Image image;
        sf::Color color;

        image.loadFromFile(passed_Region->getRegionImageFileName());

        if (passed_Region->isRegionPlayable())
        {
                for (int x = 0; x < image.getSize().x; x++)
                {
                        for (int y = 0; y < image.getSize().y; y++)
                        {
                                color = image.getPixel(x,y);

                                if (color == sf::Color::White)
                                {
                                        passed_Region->addPosToRegionLandPixelList(x,y);
                                }
                        }
                }
        }
}


void Region::addPosToRegionLandPixelList(sf::Vector2i pos)
{
        regionLandPixelList.push_back(pos);
}
 

So then I use the regionLandPixelList vector to determine if the user has moved an army into this region. Now this works well but I've found that the regionLandPixelList grows to very large sizes. For example the region above, the size of regionLandPixelList had over 40K entries. And I eventually plan to have around 60 regions....some of them much larger than the one above.

I am concerned about performance as I have to check the regionlandpixellist of each region, every time the user moves an army piece.

So I was wondering, is there any way/calc that I can use with the region outline to determine if the mouse position is within that region? Obviously each region outline is irregular which makes this very tricky.

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: pixel map question
« Reply #1 on: March 09, 2016, 05:41:05 am »
You could merge all your region pictures into a single one, but where each region has its own color. (region 1 is red, region 2 is blue, etc.)
Load this picture into an sf::Image, then when you want to know on what region your mouse is a single image.getPixel(x, y) should do the trick fast enough. (that's how I did once, with a smaller picture than you though)

starkhorn

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
Re: pixel map question
« Reply #2 on: March 10, 2016, 07:14:21 am »
Awesome idea, works a treat. Thank you

starkhorn

  • Jr. Member
  • **
  • Posts: 55
    • View Profile
    • Email
Re: pixel map question
« Reply #3 on: March 15, 2016, 12:10:00 am »
Ok so the pixel map is working really well. It detects the RGB with no issues and I define each region via a txtfile with the matching RGB value in the sf::image.

Now I am playing with different ideas. One of the things I need to do is to change the colour of each region border and land to the nation colour. My plan had been to create a texture for each region with the border and land defined. However each image is 5680 X 4178 pixels.

So after 14-15 regions (which is 2 textures per region (I want border and land to have different colours), I actually am finding it quite slow and I even have some memory issues.

So I was wondering if changing the sf::image itself would be fast enough.

(1) - create a sf::image of each region land. Each region land has an unique RGB.
(2) - create another sf::image of each region border. Again each region border has an unique RGB.
(3) - then when a region changes ownership, I need to chk each pixel of the sf::image for the region's RGB and change the colour to the nation's RGB. I am assuming

So I am bit concerned that checking each pixel is going to be quite time consuming. Is there any way to speed that up or make that more efficient? Right now I am just doing to do a massive for loop.

Also from point 3, I guess I will need 4 * sf::image's? 2 which have the original pixel map colours? Then the 2 working copies which is what I am running the sf::Image setPixel on?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: pixel map question
« Reply #4 on: March 15, 2016, 07:42:54 am »
If you can narrow down the position of the collision object, you would just need to check a subsection of the sf::Image.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/