VideoBuffer pointer correctness

This commit is contained in:
mniip 2023-04-05 14:52:20 +02:00
parent 50bfa7cd5e
commit b26a1b4a88
13 changed files with 40 additions and 84 deletions

View File

@ -29,18 +29,6 @@ VideoBuffer::VideoBuffer(pixel const *data, Vec2<int> size, size_t rowStride):
std::copy_n(data + rowStride * y, size.X, video.RowIterator(Vec2(0, y))); 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<int> rect) void VideoBuffer::Crop(Rect<int> rect)
{ {
rect &= Size().OriginRect(); rect &= Size().OriginRect();

View File

@ -48,13 +48,6 @@ public:
// Automatically choose a size to fit within the given box, keeping aspect ratio // Automatically choose a size to fit within the given box, keeping aspect ratio
void ResizeToFit(Vec2<int> bound, bool resample = false); void ResizeToFit(Vec2<int> bound, bool resample = false);
[[deprecated("Use VideoBuffer(VideoBuffer const &)")]]
VideoBuffer(VideoBuffer * old);
[[deprecated("Use VideoBuffer(pixel const *, Vec2<int>)")]]
VideoBuffer(pixel const *buffer, int width, int height, int pitch = 0);
[[deprecated("Use VideoBuffer(Vec2<int>)")]]
VideoBuffer(int width, int height);
bool WritePNG(const ByteString &path) const; bool WritePNG(const ByteString &path) const;
}; };

View File

@ -48,6 +48,11 @@ class Renderer: public RasterDrawMethods<Renderer>
friend struct RasterDrawMethods<Renderer>; friend struct RasterDrawMethods<Renderer>;
public: public:
Vec2<int> Size() const
{
return video.Size();
}
pixel const *Data() const pixel const *Data() const
{ {
return video.data(); return video.data();

View File

@ -499,11 +499,8 @@ void Renderer::ResetModes()
VideoBuffer Renderer::DumpFrame() VideoBuffer Renderer::DumpFrame()
{ {
VideoBuffer newBuffer(XRES, YRES); VideoBuffer newBuffer(RES);
for(int y = 0; y < YRES; y++) newBuffer.BlendImage(video.data(), 0xFF, Size().OriginRect());
{
std::copy(vid+(y*WINDOWW), vid+(y*WINDOWW)+XRES, newBuffer.Data()+(y*XRES));
}
return newBuffer; return newBuffer;
} }

View File

@ -157,7 +157,7 @@ void ElementSearchActivity::searchTools(String query)
else else
tempButton = new ToolButton(current+viewPosition, ui::Point(30, 18), tool->Name, tool->Identifier, tool->Description); 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->Appearance.BackgroundInactive = tool->Colour.WithAlpha(0xFF);
tempButton->SetActionCallback({ [this, tempButton, tool] { tempButton->SetActionCallback({ [this, tempButton, tool] {
if (tempButton->GetSelectionState() >= 0 && tempButton->GetSelectionState() <= 2) if (tempButton->GetSelectionState() >= 0 && tempButton->GetSelectionState() <= 2)

View File

@ -213,7 +213,7 @@ GameView::GameView():
selectPoint2(0, 0), selectPoint2(0, 0),
currentMouse(0, 0), currentMouse(0, 0),
mousePosition(0, 0), mousePosition(0, 0),
placeSaveThumb(NULL), placeSaveThumb(nullptr),
placeSaveOffset(0, 0) placeSaveOffset(0, 0)
{ {
@ -344,8 +344,6 @@ GameView::~GameView()
} }
} }
delete placeSaveThumb;
} }
class GameView::OptionListener: public QuickOptionListener 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); tempButton->Appearance.BackgroundInactive = toolList[i]->Colour.WithAlpha(0xFF);
@ -1914,7 +1912,6 @@ void GameView::NotifyLogChanged(GameModel * sender, String entry)
void GameView::NotifyPlaceSaveChanged(GameModel * sender) void GameView::NotifyPlaceSaveChanged(GameModel * sender)
{ {
delete placeSaveThumb;
placeSaveOffset = ui::Point(0, 0); placeSaveOffset = ui::Point(0, 0);
if(sender->GetPlaceSave()) if(sender->GetPlaceSave())
{ {
@ -1924,7 +1921,7 @@ void GameView::NotifyPlaceSaveChanged(GameModel * sender)
} }
else else
{ {
placeSaveThumb = NULL; placeSaveThumb = nullptr;
selectMode = SelectNone; selectMode = SelectNone;
} }
} }

View File

@ -1,10 +1,11 @@
#pragma once #pragma once
#include <ctime>
#include <deque>
#include <memory>
#include <vector>
#include "common/String.h" #include "common/String.h"
#include "gui/interface/Window.h" #include "gui/interface/Window.h"
#include "simulation/Sample.h" #include "simulation/Sample.h"
#include <ctime>
#include <vector>
#include <deque>
enum DrawMode enum DrawMode
{ {
@ -116,7 +117,7 @@ private:
ui::Point currentMouse; ui::Point currentMouse;
ui::Point mousePosition; ui::Point mousePosition;
VideoBuffer * placeSaveThumb; std::unique_ptr<VideoBuffer> placeSaveThumb;
ui::Point placeSaveOffset; ui::Point placeSaveOffset;
SimulationSample sample; SimulationSample sample;

View File

@ -5,7 +5,7 @@
namespace ui namespace ui
{ {
Appearance::Appearance(): Appearance::Appearance():
texture(NULL), texture(nullptr),
VerticalAlign(AlignMiddle), VerticalAlign(AlignMiddle),
HorizontalAlign(AlignCentre), HorizontalAlign(AlignCentre),
@ -32,23 +32,13 @@ namespace ui
icon(NoIcon) 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<VideoBuffer> texture)
{ {
delete this->texture; this->texture = std::move(texture);
if(texture)
this->texture = new VideoBuffer(texture);
else
this->texture = NULL;
} }
Appearance::~Appearance()
{
delete texture;
}
} }

View File

@ -1,4 +1,5 @@
#pragma once #pragma once
#include <memory>
#include "Border.h" #include "Border.h"
#include "Colour.h" #include "Colour.h"
#include "graphics/Icons.h" #include "graphics/Icons.h"
@ -9,7 +10,8 @@ namespace ui
class Appearance class Appearance
{ {
private: private:
VideoBuffer * texture; std::shared_ptr<VideoBuffer> texture;
public: public:
enum HorizontalAlignment enum HorizontalAlignment
{ {
@ -46,10 +48,9 @@ namespace ui
Icon icon; Icon icon;
VideoBuffer * GetTexture(); VideoBuffer const *GetTexture();
void SetTexture(VideoBuffer * texture); void SetTexture(std::unique_ptr<VideoBuffer> texture);
Appearance(); Appearance();
~Appearance();
}; };
} }

View File

@ -35,7 +35,7 @@
PreviewView::PreviewView(): PreviewView::PreviewView():
ui::Window(ui::Point(-1, -1), ui::Point((XRES/2)+210, (YRES/2)+150)), ui::Window(ui::Point(-1, -1), ui::Point((XRES/2)+210, (YRES/2)+150)),
savePreview(NULL), savePreview(nullptr),
submitCommentButton(NULL), submitCommentButton(NULL),
addCommentBox(NULL), addCommentBox(NULL),
commentWarningLabel(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) void PreviewView::NotifySaveChanged(PreviewModel * sender)
{ {
SaveInfo * save = sender->GetSaveInfo(); SaveInfo * save = sender->GetSaveInfo();
delete savePreview; savePreview = nullptr;
savePreview = NULL;
if(save) if(save)
{ {
votesUp = save->votesUp; votesUp = save->votesUp;
@ -657,5 +656,4 @@ PreviewView::~PreviewView()
RemoveComponent(submitCommentButton); RemoveComponent(submitCommentButton);
delete submitCommentButton; delete submitCommentButton;
} }
delete savePreview;
} }

View File

@ -1,8 +1,9 @@
#pragma once #pragma once
#include <memory>
#include <set>
#include <vector>
#include "common/String.h" #include "common/String.h"
#include "gui/interface/Window.h" #include "gui/interface/Window.h"
#include <vector>
#include <set>
namespace ui namespace ui
{ {
@ -20,7 +21,7 @@ class PreviewController;
class PreviewView: public ui::Window class PreviewView: public ui::Window
{ {
PreviewController * c; PreviewController * c;
VideoBuffer * savePreview; std::unique_ptr<VideoBuffer> savePreview;
ui::Button * openButton; ui::Button * openButton;
ui::Button * browserOpenButton; ui::Button * browserOpenButton;
ui::Button * favButton; ui::Button * favButton;

View File

@ -20,7 +20,7 @@ void SaveRenderer::Flush(int begin, int end)
std::fill(ren->graphicscache + begin, ren->graphicscache + end, gcache_item()); std::fill(ren->graphicscache + begin, ren->graphicscache + end, gcache_item());
} }
VideoBuffer * SaveRenderer::Render(GameSave * save, bool decorations, bool fire, Renderer *renderModeSource) std::unique_ptr<VideoBuffer> SaveRenderer::Render(GameSave * save, bool decorations, bool fire, Renderer *renderModeSource)
{ {
std::lock_guard<std::mutex> gx(renderMutex); std::lock_guard<std::mutex> gx(renderMutex);
@ -32,10 +32,7 @@ VideoBuffer * SaveRenderer::Render(GameSave * save, bool decorations, bool fire,
ren->SetColourMode(renderModeSource->GetColourMode()); ren->SetColourMode(renderModeSource->GetColourMode());
} }
int width, height; std::unique_ptr<VideoBuffer> tempThumb;
VideoBuffer * tempThumb = NULL;
width = save->blockWidth;
height = save->blockHeight;
sim->clear_sim(); sim->clear_sim();
@ -43,11 +40,8 @@ VideoBuffer * SaveRenderer::Render(GameSave * save, bool decorations, bool fire,
{ {
ren->decorations_enable = true; ren->decorations_enable = true;
ren->blackDecorations = !decorations; ren->blackDecorations = !decorations;
pixel * pData = NULL;
pixel * dst;
pixel * src = ren->vid;
ren->ClearAccumulation(); ren->ClearAccumulation();
ren->clearScreen();
if (fire) if (fire)
{ {
@ -64,17 +58,8 @@ VideoBuffer * SaveRenderer::Render(GameSave * save, bool decorations, bool fire,
ren->RenderBegin(); ren->RenderBegin();
ren->RenderEnd(); ren->RenderEnd();
tempThumb = std::make_unique<VideoBuffer>(Vec2(save->blockWidth, save->blockHeight) * CELL);
pData = (pixel *)malloc(PIXELSIZE * ((width*CELL)*(height*CELL))); tempThumb->BlendImage(ren->Data(), 0xFF, ren->Size().OriginRect());
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);
} }
return tempThumb; return tempThumb;

View File

@ -1,6 +1,7 @@
#pragma once #pragma once
#include "common/ExplicitSingleton.h" #include <memory>
#include <mutex> #include <mutex>
#include "common/ExplicitSingleton.h"
class GameSave; class GameSave;
class VideoBuffer; class VideoBuffer;
@ -14,8 +15,7 @@ class SaveRenderer: public ExplicitSingleton<SaveRenderer> {
std::mutex renderMutex; std::mutex renderMutex;
public: public:
SaveRenderer(); SaveRenderer();
VideoBuffer * Render(GameSave * save, bool decorations = true, bool fire = true, Renderer *renderModeSource = nullptr); std::unique_ptr<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);
void Flush(int begin, int end); void Flush(int begin, int end);
virtual ~SaveRenderer(); virtual ~SaveRenderer();
}; };