From b26a1b4a8868ec52263e970eed5126d0a782e2fe Mon Sep 17 00:00:00 2001 From: mniip Date: Wed, 5 Apr 2023 14:52:20 +0200 Subject: [PATCH] VideoBuffer pointer correctness --- src/graphics/Graphics.cpp | 12 --------- src/graphics/Graphics.h | 7 ------ src/graphics/Renderer.h | 5 ++++ src/graphics/RendererBasic.cpp | 7 ++---- .../elementsearch/ElementSearchActivity.cpp | 2 +- src/gui/game/GameView.cpp | 9 +++---- src/gui/game/GameView.h | 9 ++++--- src/gui/interface/Appearance.cpp | 20 ++++----------- src/gui/interface/Appearance.h | 9 ++++--- src/gui/preview/PreviewView.cpp | 6 ++--- src/gui/preview/PreviewView.h | 7 +++--- src/simulation/SaveRenderer.cpp | 25 ++++--------------- src/simulation/SaveRenderer.h | 6 ++--- 13 files changed, 40 insertions(+), 84 deletions(-) diff --git a/src/graphics/Graphics.cpp b/src/graphics/Graphics.cpp index 7820deacc..03e2b701c 100644 --- a/src/graphics/Graphics.cpp +++ b/src/graphics/Graphics.cpp @@ -29,18 +29,6 @@ VideoBuffer::VideoBuffer(pixel const *data, Vec2 size, size_t rowStride): std::copy_n(data + rowStride * y, size.X, video.RowIterator(Vec2(0, y))); } -VideoBuffer::VideoBuffer(int width, int height): - VideoBuffer(Vec2(width, height)) -{} - -VideoBuffer::VideoBuffer(VideoBuffer * old): - VideoBuffer(*old) -{} - -VideoBuffer::VideoBuffer(pixel const *buffer, int width, int height, int pitch): - VideoBuffer(buffer, Vec2(width, height), pitch == 0 ? width : pitch) -{} - void VideoBuffer::Crop(Rect rect) { rect &= Size().OriginRect(); diff --git a/src/graphics/Graphics.h b/src/graphics/Graphics.h index 9882f4c59..ab2f56071 100644 --- a/src/graphics/Graphics.h +++ b/src/graphics/Graphics.h @@ -48,13 +48,6 @@ public: // Automatically choose a size to fit within the given box, keeping aspect ratio void ResizeToFit(Vec2 bound, bool resample = false); - [[deprecated("Use VideoBuffer(VideoBuffer const &)")]] - VideoBuffer(VideoBuffer * old); - [[deprecated("Use VideoBuffer(pixel const *, Vec2)")]] - VideoBuffer(pixel const *buffer, int width, int height, int pitch = 0); - [[deprecated("Use VideoBuffer(Vec2)")]] - VideoBuffer(int width, int height); - bool WritePNG(const ByteString &path) const; }; diff --git a/src/graphics/Renderer.h b/src/graphics/Renderer.h index 932ad2d86..55df6f507 100644 --- a/src/graphics/Renderer.h +++ b/src/graphics/Renderer.h @@ -48,6 +48,11 @@ class Renderer: public RasterDrawMethods friend struct RasterDrawMethods; public: + Vec2 Size() const + { + return video.Size(); + } + pixel const *Data() const { return video.data(); diff --git a/src/graphics/RendererBasic.cpp b/src/graphics/RendererBasic.cpp index fe3d412c3..5a452d15b 100644 --- a/src/graphics/RendererBasic.cpp +++ b/src/graphics/RendererBasic.cpp @@ -499,11 +499,8 @@ void Renderer::ResetModes() VideoBuffer Renderer::DumpFrame() { - VideoBuffer newBuffer(XRES, YRES); - for(int y = 0; y < YRES; y++) - { - std::copy(vid+(y*WINDOWW), vid+(y*WINDOWW)+XRES, newBuffer.Data()+(y*XRES)); - } + VideoBuffer newBuffer(RES); + newBuffer.BlendImage(video.data(), 0xFF, Size().OriginRect()); return newBuffer; } diff --git a/src/gui/elementsearch/ElementSearchActivity.cpp b/src/gui/elementsearch/ElementSearchActivity.cpp index 34cf4b940..1946ad68a 100644 --- a/src/gui/elementsearch/ElementSearchActivity.cpp +++ b/src/gui/elementsearch/ElementSearchActivity.cpp @@ -157,7 +157,7 @@ void ElementSearchActivity::searchTools(String query) else tempButton = new ToolButton(current+viewPosition, ui::Point(30, 18), tool->Name, tool->Identifier, tool->Description); - tempButton->Appearance.SetTexture(tempTexture.get()); + tempButton->Appearance.SetTexture(std::move(tempTexture)); tempButton->Appearance.BackgroundInactive = tool->Colour.WithAlpha(0xFF); tempButton->SetActionCallback({ [this, tempButton, tool] { if (tempButton->GetSelectionState() >= 0 && tempButton->GetSelectionState() <= 2) diff --git a/src/gui/game/GameView.cpp b/src/gui/game/GameView.cpp index 2e05c9d0d..b4a17811d 100644 --- a/src/gui/game/GameView.cpp +++ b/src/gui/game/GameView.cpp @@ -213,7 +213,7 @@ GameView::GameView(): selectPoint2(0, 0), currentMouse(0, 0), mousePosition(0, 0), - placeSaveThumb(NULL), + placeSaveThumb(nullptr), placeSaveOffset(0, 0) { @@ -344,8 +344,6 @@ GameView::~GameView() } } - - delete placeSaveThumb; } class GameView::OptionListener: public QuickOptionListener @@ -631,7 +629,7 @@ void GameView::NotifyToolListChanged(GameModel * sender) } } }); - tempButton->Appearance.SetTexture(tempTexture.get()); + tempButton->Appearance.SetTexture(std::move(tempTexture)); tempButton->Appearance.BackgroundInactive = toolList[i]->Colour.WithAlpha(0xFF); @@ -1914,7 +1912,6 @@ void GameView::NotifyLogChanged(GameModel * sender, String entry) void GameView::NotifyPlaceSaveChanged(GameModel * sender) { - delete placeSaveThumb; placeSaveOffset = ui::Point(0, 0); if(sender->GetPlaceSave()) { @@ -1924,7 +1921,7 @@ void GameView::NotifyPlaceSaveChanged(GameModel * sender) } else { - placeSaveThumb = NULL; + placeSaveThumb = nullptr; selectMode = SelectNone; } } diff --git a/src/gui/game/GameView.h b/src/gui/game/GameView.h index 5ed87ab95..b94bbafc6 100644 --- a/src/gui/game/GameView.h +++ b/src/gui/game/GameView.h @@ -1,10 +1,11 @@ #pragma once +#include +#include +#include +#include #include "common/String.h" #include "gui/interface/Window.h" #include "simulation/Sample.h" -#include -#include -#include enum DrawMode { @@ -116,7 +117,7 @@ private: ui::Point currentMouse; ui::Point mousePosition; - VideoBuffer * placeSaveThumb; + std::unique_ptr placeSaveThumb; ui::Point placeSaveOffset; SimulationSample sample; diff --git a/src/gui/interface/Appearance.cpp b/src/gui/interface/Appearance.cpp index c39bb8a2f..b17cf5070 100644 --- a/src/gui/interface/Appearance.cpp +++ b/src/gui/interface/Appearance.cpp @@ -5,7 +5,7 @@ namespace ui { Appearance::Appearance(): - texture(NULL), + texture(nullptr), VerticalAlign(AlignMiddle), HorizontalAlign(AlignCentre), @@ -32,23 +32,13 @@ namespace ui icon(NoIcon) {} - VideoBuffer * Appearance::GetTexture() + VideoBuffer const *Appearance::GetTexture() { - return texture; + return texture.get(); } - void Appearance::SetTexture(VideoBuffer * texture) + void Appearance::SetTexture(std::unique_ptr texture) { - delete this->texture; - if(texture) - this->texture = new VideoBuffer(texture); - else - this->texture = NULL; + this->texture = std::move(texture); } - - Appearance::~Appearance() - { - delete texture; - } - } diff --git a/src/gui/interface/Appearance.h b/src/gui/interface/Appearance.h index dfccdf911..a82fd3ce6 100644 --- a/src/gui/interface/Appearance.h +++ b/src/gui/interface/Appearance.h @@ -1,4 +1,5 @@ #pragma once +#include #include "Border.h" #include "Colour.h" #include "graphics/Icons.h" @@ -9,7 +10,8 @@ namespace ui class Appearance { private: - VideoBuffer * texture; + std::shared_ptr texture; + public: enum HorizontalAlignment { @@ -46,10 +48,9 @@ namespace ui Icon icon; - VideoBuffer * GetTexture(); - void SetTexture(VideoBuffer * texture); + VideoBuffer const *GetTexture(); + void SetTexture(std::unique_ptr texture); Appearance(); - ~Appearance(); }; } diff --git a/src/gui/preview/PreviewView.cpp b/src/gui/preview/PreviewView.cpp index a6391f6c3..cece12d2b 100644 --- a/src/gui/preview/PreviewView.cpp +++ b/src/gui/preview/PreviewView.cpp @@ -35,7 +35,7 @@ PreviewView::PreviewView(): ui::Window(ui::Point(-1, -1), ui::Point((XRES/2)+210, (YRES/2)+150)), - savePreview(NULL), + savePreview(nullptr), submitCommentButton(NULL), addCommentBox(NULL), commentWarningLabel(NULL), @@ -418,8 +418,7 @@ void PreviewView::OnKeyPress(int key, int scan, bool repeat, bool shift, bool ct void PreviewView::NotifySaveChanged(PreviewModel * sender) { SaveInfo * save = sender->GetSaveInfo(); - delete savePreview; - savePreview = NULL; + savePreview = nullptr; if(save) { votesUp = save->votesUp; @@ -657,5 +656,4 @@ PreviewView::~PreviewView() RemoveComponent(submitCommentButton); delete submitCommentButton; } - delete savePreview; } diff --git a/src/gui/preview/PreviewView.h b/src/gui/preview/PreviewView.h index 505da6e68..e61596cd9 100644 --- a/src/gui/preview/PreviewView.h +++ b/src/gui/preview/PreviewView.h @@ -1,8 +1,9 @@ #pragma once +#include +#include +#include #include "common/String.h" #include "gui/interface/Window.h" -#include -#include namespace ui { @@ -20,7 +21,7 @@ class PreviewController; class PreviewView: public ui::Window { PreviewController * c; - VideoBuffer * savePreview; + std::unique_ptr savePreview; ui::Button * openButton; ui::Button * browserOpenButton; ui::Button * favButton; diff --git a/src/simulation/SaveRenderer.cpp b/src/simulation/SaveRenderer.cpp index f9084d287..092b5cb4b 100644 --- a/src/simulation/SaveRenderer.cpp +++ b/src/simulation/SaveRenderer.cpp @@ -20,7 +20,7 @@ void SaveRenderer::Flush(int begin, int end) std::fill(ren->graphicscache + begin, ren->graphicscache + end, gcache_item()); } -VideoBuffer * SaveRenderer::Render(GameSave * save, bool decorations, bool fire, Renderer *renderModeSource) +std::unique_ptr SaveRenderer::Render(GameSave * save, bool decorations, bool fire, Renderer *renderModeSource) { std::lock_guard gx(renderMutex); @@ -32,10 +32,7 @@ VideoBuffer * SaveRenderer::Render(GameSave * save, bool decorations, bool fire, ren->SetColourMode(renderModeSource->GetColourMode()); } - int width, height; - VideoBuffer * tempThumb = NULL; - width = save->blockWidth; - height = save->blockHeight; + std::unique_ptr tempThumb; sim->clear_sim(); @@ -43,11 +40,8 @@ VideoBuffer * SaveRenderer::Render(GameSave * save, bool decorations, bool fire, { ren->decorations_enable = true; ren->blackDecorations = !decorations; - pixel * pData = NULL; - pixel * dst; - pixel * src = ren->vid; - ren->ClearAccumulation(); + ren->clearScreen(); if (fire) { @@ -64,17 +58,8 @@ VideoBuffer * SaveRenderer::Render(GameSave * save, bool decorations, bool fire, ren->RenderBegin(); ren->RenderEnd(); - - pData = (pixel *)malloc(PIXELSIZE * ((width*CELL)*(height*CELL))); - dst = pData; - for(int i = 0; i < height*CELL; i++) - { - memcpy(dst, src, (width*CELL)*PIXELSIZE); - dst+=(width*CELL);///PIXELSIZE; - src+=WINDOWW; - } - tempThumb = new VideoBuffer(pData, width*CELL, height*CELL); - free(pData); + tempThumb = std::make_unique(Vec2(save->blockWidth, save->blockHeight) * CELL); + tempThumb->BlendImage(ren->Data(), 0xFF, ren->Size().OriginRect()); } return tempThumb; diff --git a/src/simulation/SaveRenderer.h b/src/simulation/SaveRenderer.h index a65d24e08..42cc9d588 100644 --- a/src/simulation/SaveRenderer.h +++ b/src/simulation/SaveRenderer.h @@ -1,6 +1,7 @@ #pragma once -#include "common/ExplicitSingleton.h" +#include #include +#include "common/ExplicitSingleton.h" class GameSave; class VideoBuffer; @@ -14,8 +15,7 @@ class SaveRenderer: public ExplicitSingleton { std::mutex renderMutex; public: SaveRenderer(); - VideoBuffer * Render(GameSave * save, bool decorations = true, bool fire = true, Renderer *renderModeSource = nullptr); - VideoBuffer * Render(unsigned char * saveData, int saveDataSize, bool decorations = true, bool fire = true); + std::unique_ptr Render(GameSave * save, bool decorations = true, bool fire = true, Renderer *renderModeSource = nullptr); void Flush(int begin, int end); virtual ~SaveRenderer(); };