summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordon bright <hugh.m.bright@gmail.com>2013-01-26 05:30:02 (GMT)
committerdon bright <hugh.m.bright@gmail.com>2013-01-26 05:30:02 (GMT)
commit395628cb4ef7f8559c667af22fb273e21e4a5ad8 (patch)
tree9fc4cd53eda3370de06c872f9df299ee1d9629de /src
parenta66ad9c4bc10696cd3167d3217c9aad532167705 (diff)
refactor to remove duplicate code
Diffstat (limited to 'src')
-rw-r--r--src/OffscreenContext.h24
-rw-r--r--src/OffscreenContextCGL.mm22
-rw-r--r--src/OffscreenContextGLX.cc20
-rw-r--r--src/OffscreenContextWGL.cc22
4 files changed, 56 insertions, 32 deletions
diff --git a/src/OffscreenContext.h b/src/OffscreenContext.h
index a1792b4..3494898 100644
--- a/src/OffscreenContext.h
+++ b/src/OffscreenContext.h
@@ -2,13 +2,33 @@
#define OFFSCREENCONTEXT_H_
#include <iostream>
+#include <fstream>
#include <string>
struct OffscreenContext *create_offscreen_context(int w, int h);
-void bind_offscreen_context(OffscreenContext *ctx);
bool teardown_offscreen_context(OffscreenContext *ctx);
-bool save_framebuffer(OffscreenContext *ctx, const char *filename);
bool save_framebuffer(OffscreenContext *ctx, std::ostream &output);
std::string offscreen_context_getinfo(OffscreenContext *ctx);
+void bind_offscreen_context(OffscreenContext *ctx)
+{
+ if (ctx) fbo_bind(ctx->fbo);
+}
+
+/*
+ Capture framebuffer from OpenGL and write it to the given filename as PNG.
+*/
+inline bool save_framebuffer(OffscreenContext *ctx, const char *filename)
+{
+ std::ofstream fstream(filename);
+ if (!fstream.is_open()) {
+ std::cerr << "Can't open file " << filename << " for writing";
+ return false;
+ } else {
+ save_framebuffer(ctx, fstream);
+ fstream.close();
+ }
+ return true;
+}
+
#endif
diff --git a/src/OffscreenContextCGL.mm b/src/OffscreenContextCGL.mm
index a0995fa..b3a5501 100644
--- a/src/OffscreenContextCGL.mm
+++ b/src/OffscreenContextCGL.mm
@@ -111,6 +111,22 @@ bool teardown_offscreen_context(OffscreenContext *ctx)
*/
bool save_framebuffer(OffscreenContext *ctx, const char *filename)
{
+ std::ofstream fstream(filename);
+ if (!fstream.is_open()) {
+ PRINTB("Can't open file \"%s\" for writing", filename);
+ return false;
+ } else {
+ save_framebuffer(ctx, fstream);
+ fstream.close();
+ }
+ return true;
+}
+
+/*!
+ Capture framebuffer from OpenGL and write it to the given ostream
+*/
+bool save_framebuffer(OffscreenContext *ctx, std::ostream &output)
+{
if (!ctx || !filename) return false;
// Read pixels from OpenGL
int samplesPerPixel = 4; // R, G, B and A
@@ -132,7 +148,7 @@ bool save_framebuffer(OffscreenContext *ctx, const char *filename)
}
flip_image(bufferData, flippedBuffer, samplesPerPixel, ctx->width, ctx->height);
- bool writeok = write_png(filename, flippedBuffer, ctx->width, ctx->height);
+ bool writeok = write_png(output, flippedBuffer, ctx->width, ctx->height);
free(flippedBuffer);
free(bufferData);
@@ -140,7 +156,3 @@ bool save_framebuffer(OffscreenContext *ctx, const char *filename)
return writeok;
}
-void bind_offscreen_context(OffscreenContext *ctx)
-{
- fbo_bind(ctx->fbo);
-}
diff --git a/src/OffscreenContextGLX.cc b/src/OffscreenContextGLX.cc
index 73794cc..dee47ed 100644
--- a/src/OffscreenContextGLX.cc
+++ b/src/OffscreenContextGLX.cc
@@ -297,22 +297,6 @@ bool teardown_offscreen_context(OffscreenContext *ctx)
}
/*!
- Capture framebuffer from OpenGL and write it to the given filename as PNG.
-*/
-bool save_framebuffer(OffscreenContext *ctx, const char *filename)
-{
- std::ofstream fstream(filename);
- if (!fstream.is_open()) {
- PRINTB("Can't open file \"%s\" for writing", filename);
- return false;
- } else {
- save_framebuffer(ctx, fstream);
- fstream.close();
- }
- return true;
-}
-
-/*!
Capture framebuffer from OpenGL and write it to the given ostream
*/
bool save_framebuffer(OffscreenContext *ctx, std::ostream &output)
@@ -339,7 +323,3 @@ bool save_framebuffer(OffscreenContext *ctx, std::ostream &output)
return writeok;
}
-void bind_offscreen_context(OffscreenContext *ctx)
-{
- if (ctx) fbo_bind(ctx->fbo);
-}
diff --git a/src/OffscreenContextWGL.cc b/src/OffscreenContextWGL.cc
index 75a7ed2..b2b0e2a 100644
--- a/src/OffscreenContextWGL.cc
+++ b/src/OffscreenContextWGL.cc
@@ -219,6 +219,22 @@ bool teardown_offscreen_context(OffscreenContext *ctx)
*/
bool save_framebuffer(OffscreenContext *ctx, const char *filename)
{
+ std::ofstream fstream(filename);
+ if (!fstream.is_open()) {
+ PRINTB("Can't open file \"%s\" for writing", filename);
+ return false;
+ } else {
+ save_framebuffer(ctx, fstream);
+ fstream.close();
+ }
+ return true;
+}
+
+/*!
+ Capture framebuffer from OpenGL and write it to the given filename as PNG.
+*/
+bool save_framebuffer(OffscreenContext *ctx, std::ostream &output)
+{
wglSwapLayerBuffers( ctx->dev_context, WGL_SWAP_MAIN_PLANE );
if (!ctx || !filename) return false;
int samplesPerPixel = 4; // R, G, B and A
@@ -234,14 +250,10 @@ bool save_framebuffer(OffscreenContext *ctx, const char *filename)
}
flip_image(&pixels[0], flippedBuffer, samplesPerPixel, ctx->width, ctx->height);
- bool writeok = write_png(filename, flippedBuffer, ctx->width, ctx->height);
+ bool writeok = write_png(output, flippedBuffer, ctx->width, ctx->height);
free(flippedBuffer);
return writeok;
}
-void bind_offscreen_context(OffscreenContext *ctx)
-{
- if (ctx) fbo_bind(ctx->fbo);
-}
contact: Jan Huwald // Impressum