Separate Screenshot class header

This commit is contained in:
brunoherbelin
2020-04-05 10:55:21 +02:00
parent c23586df27
commit 370e680e91
4 changed files with 153 additions and 102 deletions

View File

@@ -2,7 +2,6 @@
#include <thread>
#include <mutex>
#include <chrono>
#include <assert.h>
// Desktop OpenGL function loader
#include <glad/glad.h> // Initialized with gladLoadGLLoader()
@@ -37,7 +36,6 @@
// standalone image loader
#include <stb_image.h>
#include <stb_image_write.h>
// vmix
#include "defines.h"
@@ -234,9 +232,7 @@ bool Rendering::Begin()
}
// handle window resize
glfwGetFramebufferSize(window, &render_width, &render_height);
// handle window resize
glfwGetFramebufferSize(window, &render_width, &render_height);
glViewport(0, 0, render_width, render_height);
// GL Colors
@@ -286,7 +282,8 @@ void Rendering::Close()
}
glm::mat4 Rendering::Projection() {
glm::mat4 Rendering::Projection()
{
glm::mat4 projection = glm::ortho(-5.0, 5.0, -5.0, 5.0);
glm::mat4 scale = glm::scale(glm::mat4(1.0f), glm::vec3(1.f, AspectRatio(), 1.f));
@@ -351,82 +348,6 @@ void Rendering::RequestScreenshot()
request_screenshot = true;
}
void Screenshot::CreateEmpty(int w, int h)
{
Clear();
Width = w;
Height = h;
Data = (unsigned int*) malloc(Width * Height * 4 * sizeof(unsigned int));
memset(Data, 0, Width * Height * 4);
}
void Screenshot::CreateFromCaptureGL(int x, int y, int w, int h)
{
Clear();
Width = w;
Height = h;
Data = (unsigned int*) malloc(Width * Height * 4);
// actual capture of frame buffer
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, Data);
// make it usable
RemoveAlpha();
FlipVertical();
}
void Screenshot::SaveFile(const char* filename)
{
if (Data)
stbi_write_png(filename, Width, Height, 4, Data, Width * 4);
}
void Screenshot::RemoveAlpha()
{
unsigned int* p = Data;
int n = Width * Height;
while (n-- > 0)
{
*p |= 0xFF000000;
p++;
}
}
void Screenshot::BlitTo(Screenshot* dst, int src_x, int src_y, int dst_x, int dst_y, int w, int h) const
{
const Screenshot* src = this;
assert(dst != src);
assert(dst != NULL);
assert(src_x >= 0 && src_y >= 0);
assert(src_x + w <= src->Width);
assert(src_y + h <= src->Height);
assert(dst_x >= 0 && dst_y >= 0);
assert(dst_x + w <= dst->Width);
assert(dst_y + h <= dst->Height);
for (int y = 0; y < h; y++)
memcpy(dst->Data + dst_x + (dst_y + y) * dst->Width, src->Data + src_x + (src_y + y) * src->Width, w * 4);
}
void Screenshot::FlipVertical()
{
int comp = 4;
int stride = Width * comp;
unsigned char* line_tmp = new unsigned char[stride];
unsigned char* line_a = (unsigned char*)Data;
unsigned char* line_b = (unsigned char*)Data + (stride * (Height - 1));
while (line_a < line_b)
{
memcpy(line_tmp, line_a, stride);
memcpy(line_a, line_b, stride);
memcpy(line_b, line_tmp, stride);
line_a += stride;
line_b -= stride;
}
delete[] line_tmp;
}
//
// Discarded because not working under OSX - kept in case it would become useful

View File

@@ -7,28 +7,16 @@
#include <gst/gl/gl.h>
#include <glm/glm.hpp>
class Screenshot
#include "Screenshot.h"
struct RenderingAttrib
{
int Width, Height;
unsigned int * Data;
public:
Screenshot() { Width = Height = 0; Data = nullptr; }
~Screenshot() { Clear(); }
bool IsFull() { return Data != nullptr; }
void Clear() { if (IsFull()) free(Data); Data = nullptr; }
void CreateEmpty(int w, int h);
void CreateFromCaptureGL(int x, int y, int w, int h);
void RemoveAlpha();
void FlipVertical();
void SaveFile(const char* filename);
void BlitTo(Screenshot* dst, int src_x, int src_y, int dst_x, int dst_y, int w, int h) const;
RenderingAttrib() {}
glm::ivec4 viewport;
glm::vec3 clear_color;
};
class Rendering
{
friend class UserInterface;
@@ -70,10 +58,14 @@ public:
void PushFrontDrawCallback(RenderingCallback function);
void PushBackDrawCallback(RenderingCallback function);
// push and pop rendering attributes
void PushAttrib(glm::ivec4 viewport, glm::vec3 color);
void PopAttrib();
// request screenshot
void RequestScreenshot();
// get Screenshot
Screenshot *CurrentScreenshot();
class Screenshot *CurrentScreenshot();
// request fullscreen
void ToggleFullscreen();
@@ -97,6 +89,9 @@ private:
// loop update end frame
void End();
// list of rendering attributes
std::list<RenderingAttrib> drawAttributes;
// list of functions to call at each Draw
std::list<RenderingCallback> drawCallbacks;

109
Screenshot.cpp Normal file
View File

@@ -0,0 +1,109 @@
#include "Screenshot.h"
#include <memory.h>
#include <assert.h>
#include <glad/glad.h>
// standalone image loader
#include <stb_image.h>
#include <stb_image_write.h>
Screenshot::Screenshot()
{
Width = Height = 0;
Data = nullptr;
}
Screenshot::~Screenshot()
{
Clear();
}
bool Screenshot::IsFull()
{
return Data != nullptr;
}
void Screenshot::Clear()
{
if (IsFull())
free(Data);
Data = nullptr;
}
void Screenshot::CreateEmpty(int w, int h)
{
Clear();
Width = w;
Height = h;
Data = (unsigned int*) malloc(Width * Height * 4 * sizeof(unsigned int));
memset(Data, 0, Width * Height * 4);
}
void Screenshot::CreateFromCaptureGL(int x, int y, int w, int h)
{
Clear();
Width = w;
Height = h;
Data = (unsigned int*) malloc(Width * Height * 4);
// actual capture of frame buffer
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glReadPixels(x, y, w, h, GL_RGBA, GL_UNSIGNED_BYTE, Data);
// make it usable
RemoveAlpha();
FlipVertical();
}
void Screenshot::SaveFile(const char* filename)
{
if (Data)
stbi_write_png(filename, Width, Height, 4, Data, Width * 4);
}
void Screenshot::RemoveAlpha()
{
unsigned int* p = Data;
int n = Width * Height;
while (n-- > 0)
{
*p |= 0xFF000000;
p++;
}
}
void Screenshot::BlitTo(Screenshot* dst, int src_x, int src_y, int dst_x, int dst_y, int w, int h) const
{
const Screenshot* src = this;
assert(dst != src);
assert(dst != NULL);
assert(src_x >= 0 && src_y >= 0);
assert(src_x + w <= src->Width);
assert(src_y + h <= src->Height);
assert(dst_x >= 0 && dst_y >= 0);
assert(dst_x + w <= dst->Width);
assert(dst_y + h <= dst->Height);
for (int y = 0; y < h; y++)
memcpy(dst->Data + dst_x + (dst_y + y) * dst->Width, src->Data + src_x + (src_y + y) * src->Width, w * 4);
}
void Screenshot::FlipVertical()
{
int comp = 4;
int stride = Width * comp;
unsigned char* line_tmp = new unsigned char[stride];
unsigned char* line_a = (unsigned char*)Data;
unsigned char* line_b = (unsigned char*)Data + (stride * (Height - 1));
while (line_a < line_b)
{
memcpy(line_tmp, line_a, stride);
memcpy(line_a, line_b, stride);
memcpy(line_b, line_tmp, stride);
line_a += stride;
line_b -= stride;
}
delete[] line_tmp;
}

26
Screenshot.h Normal file
View File

@@ -0,0 +1,26 @@
#ifndef SCREENSHOT_H
#define SCREENSHOT_H
class Screenshot
{
int Width, Height;
unsigned int * Data;
public:
Screenshot();
~Screenshot();
bool IsFull();
void Clear();
void CreateEmpty(int w, int h);
void CreateFromCaptureGL(int x, int y, int w, int h);
void RemoveAlpha();
void FlipVertical();
void SaveFile(const char* filename);
void BlitTo(Screenshot* dst, int src_x, int src_y, int dst_x, int dst_y, int w, int h) const;
};
#endif // SCREENSHOT_H