From 495a7ac166ee3d7ee4bdc5768c66547724cb4562 Mon Sep 17 00:00:00 2001 From: whitequark Date: Mon, 23 Jan 2017 00:24:18 +0000 Subject: [PATCH] =?UTF-8?q?Refactor=20the=20"File=20=E2=86=92=20Export=20I?= =?UTF-8?q?mage"=20command.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The existing code is horrible and needlessly platform-dependent. Even worse, it causes a freeze on GTK. Instead of propping that up with a few more crutches, just fix the root cause. --- src/CMakeLists.txt | 2 +- src/draw.cpp | 34 +++++++++++++++++++++------------- src/export.cpp | 35 ++--------------------------------- src/solvespace.h | 1 + 4 files changed, 25 insertions(+), 47 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1d4d3ddc..45b475f5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -154,6 +154,7 @@ set(solvespace_core_SOURCES drawconstraint.cpp drawentity.cpp entity.cpp + export.cpp exportstep.cpp exportvector.cpp expr.cpp @@ -191,7 +192,6 @@ set(solvespace_core_SOURCES srf/triangulate.cpp) set(solvespace_core_gl_SOURCES - export.cpp solvespace.cpp) add_library(solvespace-core STATIC diff --git a/src/draw.cpp b/src/draw.cpp index b6776173..849c2484 100644 --- a/src/draw.cpp +++ b/src/draw.cpp @@ -885,26 +885,34 @@ void GraphicsWindow::Paint() { /*outlineColor=*/Style::Color(Style::HOVERED)); } + // If we've had a screenshot requested, take it now, before the UI is overlaid. + if(!SS.screenshotFile.empty()) { + FILE *f = ssfopen(SS.screenshotFile, "wb"); + if(!f || !canvas->ReadFrame()->WritePng(f, /*flip=*/true)) { + Error("Couldn't write to '%s'", SS.screenshotFile.c_str()); + } + if(f) fclose(f); + SS.screenshotFile.clear(); + } + // And finally the toolbar. if(SS.showToolbar) { canvas->SetCamera(camera); ToolbarDraw(&uiCanvas); } - // If we display UI elements, also display an fps counter. - if(SS.showToolbar) { - RgbaColor renderTimeColor; - if(1000 / renderTime.count() < 60) { - // We aim for a steady 60fps; draw the counter in red when we're slower. - renderTimeColor = { 255, 0, 0, 255 }; - } else { - renderTimeColor = { 255, 255, 255, 255 }; - } - uiCanvas.DrawBitmapText(ssprintf("rendered in %ld ms (%ld 1/s)", - (long)renderTime.count(), - (long)(1000/renderTime.count())), - 5, 5, renderTimeColor); + // Also display an fps counter. + RgbaColor renderTimeColor; + if(1000 / renderTime.count() < 60) { + // We aim for a steady 60fps; draw the counter in red when we're slower. + renderTimeColor = { 255, 0, 0, 255 }; + } else { + renderTimeColor = { 255, 255, 255, 255 }; } + uiCanvas.DrawBitmapText(ssprintf("rendered in %ld ms (%ld 1/s)", + (long)renderTime.count(), + (long)(1000/renderTime.count())), + 5, 5, renderTimeColor); canvas->FlushFrame(); canvas->Clear(); diff --git a/src/export.cpp b/src/export.cpp index 50f017cd..4c34811d 100644 --- a/src/export.cpp +++ b/src/export.cpp @@ -1117,39 +1117,8 @@ void SolveSpaceUI::ExportMeshAsThreeJsTo(FILE *f, const std::string &filename, // rendering the view in the usual way and then copying the pixels. //----------------------------------------------------------------------------- void SolveSpaceUI::ExportAsPngTo(const std::string &filename) { -#if !defined(HEADLESS) - // No guarantee that the back buffer contains anything valid right now, - // so repaint the scene. And hide the toolbar too. - bool prevShowToolbar = SS.showToolbar; - SS.showToolbar = false; - - // Somewhat hacky way to invoke glReadPixels without dragging in all OpenGL headers. - std::shared_ptr canvas = CreateRenderer(); - canvas->SetCamera(SS.GW.GetCamera()); - std::shared_ptr screenshot; -#if !defined(WIN32) - GlOffscreen offscreen; - offscreen.Render((int)SS.GW.width, (int)SS.GW.height, [&] { - SS.GW.Paint(); - screenshot = canvas->ReadFrame(); - }); -#else + screenshotFile = filename; + // The rest of the work is done in the next redraw. SS.GW.Paint(); - screenshot = canvas->ReadFrame(); -#endif - SS.showToolbar = prevShowToolbar; - - FILE *f = ssfopen(filename, "wb"); - if(!f || !screenshot->WritePng(f, /*flip=*/true)) { - Error("Couldn't write to '%s'", filename.c_str()); - } - if(f) fclose(f); - -#if !defined(WIN32) - offscreen.Clear(); -#endif - - return; -#endif } diff --git a/src/solvespace.h b/src/solvespace.h index 7468eed3..32a07e34 100644 --- a/src/solvespace.h +++ b/src/solvespace.h @@ -676,6 +676,7 @@ public: bool drawBackFaces; bool checkClosedContour; bool showToolbar; + std::string screenshotFile; RgbaColor backgroundColor; bool exportShadedTriangles; bool exportPwlCurves;