From 97d68957f07e8246f680e089d6c6c9555d34085d Mon Sep 17 00:00:00 2001 From: Ted John Date: Thu, 8 Jun 2017 23:02:39 +0100 Subject: [PATCH] Fix segfault in screenshot command --- .../engines/opengl/OpenGLDrawingEngine.cpp | 3 +- src/openrct2/Imaging.h | 10 ++++- src/openrct2/cmdline/ScreenshotCommands.cpp | 6 +-- src/openrct2/drawing/NewDrawing.cpp | 2 +- src/openrct2/drawing/X8DrawingEngine.cpp | 2 +- src/openrct2/game.c | 2 +- .../{screenshot.c => Screenshot.cpp} | 42 ++++++++++++------- .../interface/{screenshot.h => Screenshot.h} | 32 +++++++------- src/openrct2/interface/keyboard_shortcut.c | 2 +- src/openrct2/title/TitleScreen.cpp | 2 +- src/openrct2/ui/DummyUiContext.cpp | 8 +++- src/openrct2/windows/top_toolbar.c | 2 +- 12 files changed, 69 insertions(+), 44 deletions(-) rename src/openrct2/interface/{screenshot.c => Screenshot.cpp} (95%) rename src/openrct2/interface/{screenshot.h => Screenshot.h} (64%) diff --git a/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp b/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp index 5f80b7fd2f..3ec17a8420 100644 --- a/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp +++ b/src/openrct2-ui/drawing/engines/opengl/OpenGLDrawingEngine.cpp @@ -28,12 +28,11 @@ #include #include #include -#include +#include #include extern "C" { - #include #include #include #include diff --git a/src/openrct2/Imaging.h b/src/openrct2/Imaging.h index 268c6a71fe..efd3cebc4a 100644 --- a/src/openrct2/Imaging.h +++ b/src/openrct2/Imaging.h @@ -17,7 +17,15 @@ #pragma once #include "common.h" -#include "drawing/drawing.h" + +#ifdef __cplusplus +extern "C" +{ +#endif + #include "drawing/drawing.h" +#ifdef __cplusplus +} +#endif #ifdef __cplusplus diff --git a/src/openrct2/cmdline/ScreenshotCommands.cpp b/src/openrct2/cmdline/ScreenshotCommands.cpp index ab02eca0f9..97b7e0bfa4 100644 --- a/src/openrct2/cmdline/ScreenshotCommands.cpp +++ b/src/openrct2/cmdline/ScreenshotCommands.cpp @@ -14,11 +14,7 @@ *****************************************************************************/ #pragma endregion -extern "C" -{ - #include "../interface/screenshot.h" -} - +#include "../interface/Screenshot.h" #include "CommandLine.hpp" static exitcode_t HandleScreenshot(CommandLineArgEnumerator *argEnumerator); diff --git a/src/openrct2/drawing/NewDrawing.cpp b/src/openrct2/drawing/NewDrawing.cpp index 14feacd67c..4727df0a50 100644 --- a/src/openrct2/drawing/NewDrawing.cpp +++ b/src/openrct2/drawing/NewDrawing.cpp @@ -19,6 +19,7 @@ #include "../ui/UiContext.h" #include "../core/Exception.hpp" #include "../core/Registration.hpp" +#include "../interface/Screenshot.h" #include "IDrawingContext.h" #include "IDrawingEngine.h" #include "NewDrawing.h" @@ -27,7 +28,6 @@ extern "C" { #include "../config/Config.h" #include "../drawing/drawing.h" - #include "../interface/screenshot.h" #include "../localisation/string_ids.h" #include "../platform/platform.h" #include "../rct2.h" diff --git a/src/openrct2/drawing/X8DrawingEngine.cpp b/src/openrct2/drawing/X8DrawingEngine.cpp index dce5fb3b35..fb539a8ab4 100644 --- a/src/openrct2/drawing/X8DrawingEngine.cpp +++ b/src/openrct2/drawing/X8DrawingEngine.cpp @@ -20,6 +20,7 @@ #include "../core/Guard.hpp" #include "../core/Math.hpp" #include "../core/Memory.hpp" +#include "../interface/Screenshot.h" #include "IDrawingContext.h" #include "IDrawingEngine.h" #include "Rain.h" @@ -28,7 +29,6 @@ extern "C" { #include "../game.h" - #include "../interface/screenshot.h" #include "../interface/viewport.h" #include "../interface/window.h" #include "../intro.h" diff --git a/src/openrct2/game.c b/src/openrct2/game.c index 1b954e7f7a..a93d00ad50 100644 --- a/src/openrct2/game.c +++ b/src/openrct2/game.c @@ -21,7 +21,7 @@ #include "editor.h" #include "game.h" #include "input.h" -#include "interface/screenshot.h" +#include "interface/Screenshot.h" #include "interface/viewport.h" #include "interface/widget.h" #include "interface/window.h" diff --git a/src/openrct2/interface/screenshot.c b/src/openrct2/interface/Screenshot.cpp similarity index 95% rename from src/openrct2/interface/screenshot.c rename to src/openrct2/interface/Screenshot.cpp index 0b5bd9a500..a6db3913b5 100644 --- a/src/openrct2/interface/screenshot.c +++ b/src/openrct2/interface/Screenshot.cpp @@ -17,19 +17,27 @@ #include "../audio/audio.h" #include "../config/Config.h" #include "../Context.h" -#include "../drawing/drawing.h" -#include "../game.h" #include "../Imaging.h" -#include "../intro.h" -#include "../localisation/localisation.h" #include "../OpenRCT2.h" -#include "../platform/platform.h" -#include "../rct2.h" -#include "../util/util.h" -#include "../windows/error.h" -#include "screenshot.h" -#include "viewport.h" +#include "Screenshot.h" +extern "C" +{ + #include "../drawing/drawing.h" + #include "../game.h" + #include "../intro.h" + #include "../localisation/localisation.h" + #include "../platform/platform.h" + #include "../rct2.h" + #include "../util/util.h" + #include "../windows/error.h" + #include "viewport.h" +} + +using namespace OpenRCT2; + +extern "C" +{ uint8 gScreenshotCountdown = 0; /** @@ -211,7 +219,7 @@ void screenshot_giant() dpi.height = resolutionHeight; dpi.pitch = 0; dpi.zoom_level = 0; - dpi.bits = malloc(dpi.width * dpi.height); + dpi.bits = (uint8 *)malloc(dpi.width * dpi.height); viewport_render(&dpi, &viewport, 0, 0, viewport.width, viewport.height); @@ -283,7 +291,9 @@ sint32 cmdline_for_screenshot(const char **argv, sint32 argc) } gOpenRCT2Headless = true; - // if (openrct2_initialise()) { + auto context = CreateContext(); + if (context->Initialise()) + { drawing_engine_init(); rct2_open_file(inputPath); @@ -357,7 +367,7 @@ sint32 cmdline_for_screenshot(const char **argv, sint32 argc) dpi.height = resolutionHeight; dpi.pitch = 0; dpi.zoom_level = 0; - dpi.bits = malloc(dpi.width * dpi.height); + dpi.bits = (uint8 *)malloc(dpi.width * dpi.height); viewport_render(&dpi, &viewport, 0, 0, viewport.width, viewport.height); @@ -368,7 +378,9 @@ sint32 cmdline_for_screenshot(const char **argv, sint32 argc) free(dpi.bits); drawing_engine_dispose(); - // } - // openrct2_dispose(); + } + delete context; return 1; } + +} diff --git a/src/openrct2/interface/screenshot.h b/src/openrct2/interface/Screenshot.h similarity index 64% rename from src/openrct2/interface/screenshot.h rename to src/openrct2/interface/Screenshot.h index 2c99809e87..a4c9b8dcb6 100644 --- a/src/openrct2/interface/screenshot.h +++ b/src/openrct2/interface/Screenshot.h @@ -14,19 +14,23 @@ *****************************************************************************/ #pragma endregion -#ifndef _SCREENSHOT_H_ -#define _SCREENSHOT_H_ - -#include "../drawing/drawing.h" - -extern uint8 gScreenshotCountdown; - -void screenshot_check(); -sint32 screenshot_dump(); -sint32 screenshot_dump_png(rct_drawpixelinfo *dpi); -sint32 screenshot_dump_png_32bpp(sint32 width, sint32 height, const void *pixels); - -void screenshot_giant(); -sint32 cmdline_for_screenshot(const char **argv, sint32 argc); +#pragma once +#ifdef __cplusplus +extern "C" +{ +#endif + #include "../drawing/drawing.h" + + extern uint8 gScreenshotCountdown; + + void screenshot_check(); + sint32 screenshot_dump(); + sint32 screenshot_dump_png(rct_drawpixelinfo *dpi); + sint32 screenshot_dump_png_32bpp(sint32 width, sint32 height, const void *pixels); + + void screenshot_giant(); + sint32 cmdline_for_screenshot(const char **argv, sint32 argc); +#ifdef __cplusplus +} #endif diff --git a/src/openrct2/interface/keyboard_shortcut.c b/src/openrct2/interface/keyboard_shortcut.c index d2137c17c1..486bc5d3f0 100644 --- a/src/openrct2/interface/keyboard_shortcut.c +++ b/src/openrct2/interface/keyboard_shortcut.c @@ -20,7 +20,7 @@ #include "../game.h" #include "../input.h" #include "../interface/chat.h" -#include "../interface/screenshot.h" +#include "../interface/Screenshot.h" #include "../localisation/localisation.h" #include "../network/network.h" #include "../platform/platform.h" diff --git a/src/openrct2/title/TitleScreen.cpp b/src/openrct2/title/TitleScreen.cpp index cfa6cc65bb..9db015fe33 100644 --- a/src/openrct2/title/TitleScreen.cpp +++ b/src/openrct2/title/TitleScreen.cpp @@ -16,6 +16,7 @@ #include "../core/Console.hpp" #include "../Context.h" +#include "../interface/Screenshot.h" #include "../network/network.h" #include "../OpenRCT2.h" #include "../scenario/ScenarioRepository.h" @@ -31,7 +32,6 @@ extern "C" #include "../drawing/drawing.h" #include "../game.h" #include "../input.h" - #include "../interface/screenshot.h" #include "../interface/viewport.h" #include "../interface/window.h" #include "../localisation/localisation.h" diff --git a/src/openrct2/ui/DummyUiContext.cpp b/src/openrct2/ui/DummyUiContext.cpp index bc4346e015..52b893198a 100644 --- a/src/openrct2/ui/DummyUiContext.cpp +++ b/src/openrct2/ui/DummyUiContext.cpp @@ -14,8 +14,11 @@ *****************************************************************************/ #pragma endregion +#include "../drawing/X8DrawingEngine.h" #include "UiContext.h" +using namespace OpenRCT2::Drawing; + namespace OpenRCT2 { namespace Ui { /** @@ -53,7 +56,10 @@ namespace OpenRCT2 { namespace Ui virtual void SetKeysPressed(uint32 keysym, uint8 scancode) override { } // Drawing - virtual Drawing::IDrawingEngine * CreateDrawingEngine(Drawing::DRAWING_ENGINE_TYPE type) override { return nullptr; } + virtual Drawing::IDrawingEngine * CreateDrawingEngine(Drawing::DRAWING_ENGINE_TYPE type) override + { + return new X8DrawingEngine(); + } // Text input virtual bool IsTextInputActive() override { return false; } diff --git a/src/openrct2/windows/top_toolbar.c b/src/openrct2/windows/top_toolbar.c index 53b16550ef..c58eda009a 100644 --- a/src/openrct2/windows/top_toolbar.c +++ b/src/openrct2/windows/top_toolbar.c @@ -22,7 +22,7 @@ #include "../game.h" #include "../input.h" #include "../interface/console.h" -#include "../interface/screenshot.h" +#include "../interface/Screenshot.h" #include "../interface/viewport.h" #include "../interface/widget.h" #include "../interface/window.h"