Screenshot

This commit is contained in:
2024-02-20 09:43:10 -05:00
parent 9b22587b93
commit 47cf9787f7
5 changed files with 57 additions and 29 deletions

View File

@@ -54,11 +54,11 @@ CPMAddPackage(
NAME J3ML
URL https://git.redacted.cc/josh/j3ml/archive/Prerelease-18.zip
)
CPMAddPackage(
NAME JGL
URL https://git.redacted.cc/josh/JGL/archive/Prerelease-3.zip
)
#Causes multiple definition
#CPMAddPackage(
#NAME JGL
#URL https://git.redacted.cc/josh/JGL/archive/Prerelease-3.zip
#)
CPMAddPackage(
NAME SOIL
@@ -91,8 +91,8 @@ set_target_properties(Re3D PROPERTIES LINKER_LANGUAGE CXX)
find_package(OpenGL REQUIRED)
target_include_directories(Re3D PRIVATE ${JGL_SOURCE_DIR}/include)
target_link_libraries(Re3D PRIVATE JGL)
#target_include_directories(Re3D PRIVATE ${JGL_SOURCE_DIR}/include)
#target_link_libraries(Re3D PRIVATE JGL)
add_executable(Re3D_Demo "include/demo/main.cpp")
#Link based on the relative path *so you can copy the game around*

View File

@@ -5,8 +5,8 @@
#include <J3ML/LinearAlgebra/Matrix4x4.h>
#include <J3ML/LinearAlgebra/Vector3.h>
#include <archive.h>
#include <JGL/JGL.h>
#include <JGL/Colors.h>
//#include <JGL/JGL.h>
//#include <JGL/Colors.h>
using J3ML::LinearAlgebra::Matrix4x4;
using J3ML::LinearAlgebra::Vector3;

View File

@@ -95,27 +95,53 @@ 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 {
#ifdef FREEIMAGE_FOUND
//Prevent taking a ton of screenshots really fast.
std::string fName = std::to_string(time(nullptr)) + ".bmp";
std::ifstream f (fName, std::ifstream::in);
if(f.good()) {
f.close();
return;
}
f.close();
FreeImage_Initialise();
auto* fbData = new GLubyte[this->windowWidth * this->windowHeight * 3];
glReadPixels(0, 0, this->windowWidth, this->windowHeight, GL_RGB, GL_UNSIGNED_BYTE, fbData);
FIBITMAP* image = FreeImage_ConvertFromRawBits(fbData, this->windowWidth, this->windowHeight, this->windowWidth * 3, 24, 0xFF0000, 0x00FF00, 0x0000FF, false);
FreeImage_Save(FIF_BMP, image, (fName.c_str()), 0);
FreeImage_Unload(image);
delete[] fbData;
FreeImage_DeInitialise();
#endif
}
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 fileSize = 54 + 3 * engine->window->getSize().x * engine->window->getSize().y;
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) ((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) {
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];
file.write((char*)pixel, 3);
}
for (int p = 0; p < padding; ++p) {
file.write((char*)"\0", 1);
}
}
delete[] frameBuffer;
file.close();
}
void Engine::loadConfig() {
std::ifstream file("cfg/engine.cfg");
if (!file.is_open()) {

View File

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

View File

@@ -136,7 +136,7 @@ VertexArray* Entity::getGeometry() {
void Entity::render() {
JGL::J3D::DrawString3D(JGL::Colors::Yellow, "ENTITY", position, 1.f);
//JGL::J3D::DrawString3D(JGL::Colors::Yellow, "ENTITY", position, 1.f);
bool cameraInside = false;