Skip to content

Commit

Permalink
Added a parameter to name your image when you take screenshots.
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanVreeke committed Jul 20, 2021
1 parent 34a376d commit 788ff04
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
5 changes: 4 additions & 1 deletion install-ubuntu.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
#
# install-linux.sh is the installation script for TSGL on Linux.
# install-ubuntu.sh is the installation script for TSGL on Linux Ubuntu.
# Last updated: 06/30/26
#
# -SUBJECT TO CHANGE-
Expand Down Expand Up @@ -241,6 +241,9 @@ sudo rm -rf /usr/lib/libtsgl.*
#Create the following directories (Since they aren't included in github but are needed)
mkdir -p lib bin

#Make clean the library
sudo make clean

#Make the library
make prefix=$PREFIX

Expand Down
34 changes: 21 additions & 13 deletions src/TSGL/Canvas.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "Canvas.h"
#include <stdlib.h>

// From stb_image.h:
// Do this:
Expand All @@ -16,7 +17,7 @@ namespace tsgl {

// Shader sources

static const GLchar* shapeVertexShader =
static const GLchar* shapeVertexShader =
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;"
"layout (location = 1) in vec4 aColor;"
Expand All @@ -37,7 +38,7 @@ static const GLchar* shapeFragmentShader =
"FragColor = color;"
"}";

static const GLchar* textVertexShader =
static const GLchar* textVertexShader =
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;"
"layout (location = 1) in vec2 aTexCoord;"
Expand All @@ -62,7 +63,7 @@ static const GLchar* textFragmentShader =
"FragColor = textColor * sampled;"
"}";

static const GLchar* textureVertexShader =
static const GLchar* textureVertexShader =
"#version 330 core\n"
"layout (location = 0) in vec3 aPos;"
"layout (location = 1) in vec2 aTexCoord;"
Expand All @@ -75,7 +76,7 @@ static const GLchar* textureVertexShader =
"TexCoords = vec2(aTexCoord.x, aTexCoord.y);"
"}";

static const GLchar* textureFragmentShader =
static const GLchar* textureFragmentShader =
"#version 330 core\n"
"out vec4 FragColor;"
"in vec2 TexCoords;"
Expand Down Expand Up @@ -322,7 +323,7 @@ void Canvas::draw()
screenShot();
captureScreen = false;
}

// Update Screen
glfwSwapBuffers(window);

Expand Down Expand Up @@ -643,7 +644,7 @@ void Canvas::initGlew() {

textShader = new Shader(textVertexShader, textFragmentShader);

shapeShader = new Shader(shapeVertexShader, shapeFragmentShader);
shapeShader = new Shader(shapeVertexShader, shapeFragmentShader);

textureShader = new Shader(textureVertexShader, textureFragmentShader);

Expand Down Expand Up @@ -739,7 +740,7 @@ void Canvas::initWindow() {
screenBuffer[i] = 0;
}
screenBufferMutex.unlock();

// Get info of GPU and supported OpenGL version
// printf("Renderer: %s\n", glGetString(GL_RENDERER));
// printf("OpenGL version supported %s\n", glGetString(GL_VERSION));
Expand Down Expand Up @@ -789,8 +790,11 @@ void Canvas::pauseDrawing() {
* \details The function automatically terminates after num_frames cycles have completed.
* \param num_frames The number of frames to dump screenshots for.
*/
void Canvas::recordForNumFrames(unsigned int num_frames) {
void Canvas::recordForNumFrames(unsigned int num_frames, const std::string& newCapturePrefix) {
toRecord = num_frames;
if(newCapturePrefix != "") {
capturePrefix = newCapturePrefix;
}
}

/*!
Expand Down Expand Up @@ -945,8 +949,9 @@ void Canvas::run(void (*myFunction)(Canvas&, int, char**), int argc, char* argv[
}

void Canvas::screenShot() {
char filename[25];
sprintf(filename, "Image%06d.png", frameCounter); // TODO: Make this save somewhere not in root
char sufix[20];
sprintf(sufix, "%06d.png", frameCounter);
std::string filename = capturePrefix + sufix;

screenBufferMutex.lock();
// loader.saveImageToFile(filename, screenBuffer, framebufferWidth, winHeight);
Expand All @@ -959,7 +964,7 @@ void Canvas::screenShot() {
screenBuffer[s2] = tmp;
}
}
stbi_write_png(filename, framebufferWidth, framebufferHeight, 3, screenBuffer, 0);
stbi_write_png(filename.c_str(), framebufferWidth, framebufferHeight, 3, screenBuffer, 0);
screenBufferMutex.unlock();
}

Expand Down Expand Up @@ -1099,8 +1104,11 @@ void Canvas::stopRecording() {
* \details Images are saved as ImageXXXXXX.png, where XXXXXX is the current frame number.
* \bug Multiple calls to this function in rapid succession render the FPS counter inaccurate.
*/
void Canvas::takeScreenShot() {
void Canvas::takeScreenShot(const std::string& newCapturePrefix) {
if (toRecord == 0) toRecord = 1;
if(newCapturePrefix != "") {
capturePrefix = newCapturePrefix;
}
}

void Canvas::selectShaders(unsigned int sType) {
Expand Down Expand Up @@ -1140,7 +1148,7 @@ void Canvas::selectShaders(unsigned int sType) {
glVertexAttribPointer(texAttrib, 2, GL_FLOAT, GL_FALSE, 5 * sizeof(float), (void*)(3 * sizeof(float)));
program->use();
}

// Recompute the camera matrices
uniModel = glGetUniformLocation(program->ID, "model");
uniView = glGetUniformLocation(program->ID, "view");
Expand Down
15 changes: 8 additions & 7 deletions src/TSGL/Canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ class Canvas {
GLint framebufferHeight;
int frameCounter; // Counter for the number of frames that have elapsed in the current session (for animations)
bool isFinished; // If the rendering is done, which will signal the window to close
bool keyDown; // If a key is being pressed. Prevents an action from happening twice
bool keyDown;
std::string capturePrefix = "Image"; // If a key is being pressed. Prevents an action from happening twice
int monitorX, monitorY; // Monitor position for upper left corner
double mouseX, mouseY; // Location of the mouse once HandleIO() has been called
Background * myBackground; // Pointer to the Background drawn each frame
Expand Down Expand Up @@ -150,10 +151,10 @@ class Canvas {
static void errorCallback(int error, const char* string); // Display where an error is coming from
void glDestroy(); // Destroys the GL and GLFW things that are specific for this canvas
void init(int xx,int yy,int ww,int hh,
std::string title,
ColorFloat backgroundColor, Background * background,
std::string title,
ColorFloat backgroundColor, Background * background,
double timerLength); // Method for initializing the canvas
void initBackground(Background * background,
void initBackground(Background * background,
ColorFloat bgcolor); // Initializes myBackground
void initGl(); // Initializes the GL things specific to the Canvas
void initGlew(); // Initialized the GLEW things specific to the Canvas
Expand Down Expand Up @@ -231,7 +232,7 @@ class Canvas {

void pauseDrawing();

void recordForNumFrames(unsigned int num_frames);
void recordForNumFrames(unsigned int num_frames, const std::string& newCaputurePrefix = "");

void remove(Drawable * shapePtr);

Expand All @@ -246,7 +247,7 @@ class Canvas {
virtual void run(void (*myFunction)(Canvas&, unsigned), unsigned u);

virtual void run(void (*myFunction)(Canvas&, int, int), int i1, int i2);

virtual void run(void (*myFunction)(Canvas&, int, int, int), int i1, int i2, int i3);

virtual void run(void (*myFunction)(Canvas&, unsigned, unsigned), unsigned u1, unsigned u2);
Expand Down Expand Up @@ -279,7 +280,7 @@ class Canvas {

void stopRecording();

void takeScreenShot();
void takeScreenShot(const std::string& newCapturePrefix = "");

int wait();
};
Expand Down
1 change: 1 addition & 0 deletions src/tests/testScreenshot/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ $(TARGET): $(OBJ)

clean:
$(RM) $(ODIR)/*.o $(ODIR) $(TARGET)
$(RM) *.png
@echo ""
@tput setaf 5;
@echo "*************** All output files removed from $(DIR)! ***************"
Expand Down
3 changes: 3 additions & 0 deletions src/tests/testScreenshot/testScreenshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ void screenShotFunction(Cart& can) {
Background * bg = can.getBackground();
int xNew = can.getCartWidth() / 2, yNew = can.getCartHeight() / 2, xMid = xNew, yMid = yNew, xOld, yOld;
can.recordForNumFrames(FPS * 30);
// can.takeScreenShot();
// can.sleep();
// can.takeScreenShot();
while (can.isOpen()) { // Checks to see if the window has been closed
can.sleep();
xOld = xMid;
Expand Down

0 comments on commit 788ff04

Please sign in to comment.