Write the screenshot all at once

This commit is contained in:
2024-02-20 11:19:29 -05:00
parent 47cf9787f7
commit 79ba5d8933
2 changed files with 26 additions and 28 deletions

View File

@@ -95,51 +95,49 @@ float Engine::framerate() const
return 1.f / this->frameDelta;
}
//This is ultra slow. Write into a buffer.
//TODO: Speed this up. It literally takes like 30 seconds LOL.
void Engine::takeScreenshot() const {
auto *frameBuffer = new unsigned char[3 * (int) engine->window->getSize().x * (int) engine->window->getSize().y];
glReadPixels(0, 0, (int) engine->window->getSize().x, (int) engine->window->getSize().y, GL_RGB, GL_UNSIGNED_BYTE, frameBuffer);
std::string fileName;
fileName.append(std::to_string(engine->frameCount));
fileName.append(".bmp");
int width = (int) engine->window->getSize().x;
int height = (int) engine->window->getSize().y;
std::vector<unsigned char> frameBuffer(3 * width * height);
int fileSize = 54 + 3 * engine->window->getSize().x * engine->window->getSize().y;
glReadPixels(0, 0, width, height, GL_RGB, GL_UNSIGNED_BYTE, frameBuffer.data());
std::string fileName = std::to_string(engine->frameCount) + ".bmp";
std::ofstream file(fileName, std::ios::out | std::ios::binary);
int fileSize = 54 + 3 * width * height;
unsigned char bmpfileheader[14] = {'B', 'M', 0, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0};
unsigned char bmpinfoheader[40] = {40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0};
bmpfileheader[2] = (unsigned char) (fileSize);
bmpfileheader[3] = (unsigned char) (fileSize >> 8);
bmpfileheader[4] = (unsigned char) (fileSize >> 16);
bmpfileheader[5] = (unsigned char) (fileSize >> 24);
bmpinfoheader[4] = (unsigned char) (width);
bmpinfoheader[5] = (unsigned char) (width >> 8);
bmpinfoheader[6] = (unsigned char) (width >> 16);
bmpinfoheader[7] = (unsigned char) (width >> 24);
bmpinfoheader[8] = (unsigned char) (height);
bmpinfoheader[9] = (unsigned char) (height >> 8);
bmpinfoheader[10] = (unsigned char) (height >> 16);
bmpinfoheader[11] = (unsigned char) (height >> 24);
bmpinfoheader[4] = (unsigned char) ((int) engine->window->getSize().x);
bmpinfoheader[5] = (unsigned char) ((int) engine->window->getSize().x >> 8);
bmpinfoheader[6] = (unsigned char) ((int) engine->window->getSize().x >> 16);
bmpinfoheader[7] = (unsigned char) ((int) engine->window->getSize().x >> 24);
bmpinfoheader[8] = (unsigned char) ((int) engine->window->getSize().y);
bmpinfoheader[9] = (unsigned char) ((int) engine->window->getSize().y >> 8);
bmpinfoheader[10] = (unsigned char) ((int) engine->window->getSize().y >> 16);
bmpinfoheader[11] = (unsigned char) ((int) engine->window->getSize().y >> 24);
std::ofstream file(fileName, std::ios::out | std::ios::binary);
file.write((char*)bmpfileheader, 14);
file.write((char*)bmpinfoheader, 40);
int padding = (4 - ((int) engine->window->getSize().x * 3) % 4) % 4;
for (int y = (int) engine->window->getSize().y - 1; y >= 0; --y) {
for (int x = 0; x < (int) engine->window->getSize().x; ++x) {
int padding = (4 - (width * 3) % 4) % 4;
for (int y = height - 1; y >= 0; --y) {
for (int x = 0; x < width; ++x) {
unsigned char pixel[3];
pixel[0] = frameBuffer[(y * (int) engine->window->getSize().x + x) * 3 + 2];
pixel[1] = frameBuffer[(y * (int) engine->window->getSize().x + x) * 3 + 1];
pixel[2] = frameBuffer[(y * (int) engine->window->getSize().x + x) * 3];
pixel[0] = frameBuffer[(y * width + x) * 3 + 2];
pixel[1] = frameBuffer[(y * width + x) * 3 + 1];
pixel[2] = frameBuffer[(y * width + x) * 3];
file.write((char*)pixel, 3);
}
for (int p = 0; p < padding; ++p) {
file.write((char*)"\0", 1);
}
}
delete[] frameBuffer;
file.close();
}
void Engine::loadConfig() {

View File

@@ -93,8 +93,8 @@ void Render::post_render() {
glPushMatrix();
glPopMatrix();
ReWindow::RWindow::glSwapBuffers();
//if (engine->frameCount == 1000)
//engine->takeScreenshot();
if (engine->frameCount == 1000)
engine->takeScreenshot();
}
void Render::render_loop() {