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

Author Topic: Translating array into pixels for sf::Image::Create(...) [SOLVED]  (Read 1192 times)

0 Members and 1 Guest are viewing this topic.

Kim

  • Newbie
  • *
  • Posts: 14
    • View Profile
Solved, I forgot that it might be a good idea to switch from 32bit to 64bit.
Also optimized the code a little. If anyone is interested in something similar.
New problem: Bigger images (9000 x 9000 pixels) causes sf::Image::Create to fail. It returns "Failed to save image "img/cave_1.png"".

It sometimes manage to create 1 image, sometimes 2. But it start saying it cannot create the image.
Smaller images such as 6000 x 6000 pixels starts failing at ~25 images.
Is there something I am missing that I should do?


void MessyClass::SaveImage(int num, char** cave)
{
        sf::Uint8* temp;
        temp = new sf::Uint8[caveY * caveX * 4];
        memset(temp, 255, caveY * caveX * 4);

        for (int y = 0; y < caveY; y++) {
                for (int x = 0; x < caveX; x++) {
                        //if (cave[x][y] == '.') {
                        //      //temp[(y + x * caveY) * 4 + 0] = 255;
                        //      //temp[(y + x * caveY) * 4 + 1] = 255;
                        //      //temp[(y + x * caveY) * 4 + 2] = 255;
                        //      //temp[(y + x * caveY) * 4 + 3] = 255;
                        //}
                        if (cave[x][y] == '#') {
                                temp[(y + x * caveY) * 4 + 0] = 0;
                                temp[(y + x * caveY) * 4 + 1] = 0;
                                temp[(y + x * caveY) * 4 + 2] = 0;
                                //temp[(y + x * caveY) * 4 + 3] = 255;
                        }
                }
        }

        if (CreateDirectoryA("img", NULL))
        {
                std::cout << "Created \"img\" Directory!\n";
                Sleep(100);
        }

        img.create(caveX, caveY, temp);
        img.saveToFile("img/cave_" + std::to_string(num) + ".png");
        delete[] temp;
}

I am doing some procedural generation of caves and is using a 2D array to store each tile.
Now I am trying to use this array to create a png-file, I think I should use "sf::Image::create(...)" but I am not sure how to translate my array into an array that is acceptable to the create.


*EDIT* I got it to work (somewhat, the image is mirrored)
This is how I do it now:
void MessyClass::SaveImage()
{
        sf::Uint8* temp;
        temp = new sf::Uint8[DLA::GetInstance().GetSizeY() * DLA::GetInstance().GetSizeX() * 4];
        int cx = DLA::GetInstance().GetSizeX(), cy = DLA::GetInstance().GetSizeY();
        for (int y = 0; y < cy; y++) {
                for (int x = 0; x < cx; x++) {
                        if (drawFluff[x][y] == '.') {
                                temp[(y + x * cy) * 4 + 0] = 255;
                                temp[(y + x * cy) * 4 + 1] = 255;
                                temp[(y + x * cy) * 4 + 2] = 255;
                                temp[(y + x * cy) * 4 + 3] = 255;
                        }
                        if (drawFluff[x][y] == '#') {
                                temp[(y + x * cy) * 4 + 0] = 0;
                                temp[(y + x * cy) * 4 + 1] = 0;
                                temp[(y + x * cy) * 4 + 2] = 0;
                                temp[(y + x * cy) * 4 + 3] = 255;
                        }
                }
        }
        img.create(DLA::GetInstance().GetSizeX(), DLA::GetInstance().GetSizeY(), temp);
        img.saveToFile("img/cave_" + std::to_string(DLA::GetInstance().GetCavesGenerated()) + ".png");
}
« Last Edit: March 27, 2016, 03:30:16 am by GDHIS »

 

anything