Elmub
Elmub

Reputation: 141

Iterating through the pixels of a .bmp in C++

NOTE: I changed the title from .png to .bmp due to a comment suggesting bitmaps instead.

I'm making this simple 2d grid based CMD-game, and I want to make .png levels and turn them into level data for my game.

So basically all I want to know is, how would I iterate through the pixels of a bmp to parse it to some level data.

This is how I did it with a .txt

int x = 0;
int y = 0;
std::ifstream file(filename);
std::string str;
while (std::getline(file, str))
{
    x++;
    for (char& c : str) {
        y++;
        updateTile(coordinate(x), coordinate(y), c);
    }
}

I couldn't find any helpful threads so I posted this new one, hope I'm not breaking any rules

Upvotes: 1

Views: 619

Answers (1)

Bass Guru
Bass Guru

Reputation: 26

I don't know if you still want to read png-files, but if you do, check this decoder: http://lodev.org/lodepng/

It loads a png-file into a vector where 4 chars (bytes) give one pixel(RGBA format). So by loading 4 chars at once, you will get one pixel.

I haven't used it before, but it looks easy to use.

Upvotes: 1

Related Questions