Skip to content
Permalink
Browse files

Turn RequestBroker::RetrieveThumbnail into a request derived from Dow…

…nload

Also start moving RenderThumbnail out of RequestBroker into its own Task. Add mutex to SaveRenderer to guard Render().
  • Loading branch information...
LBPHacker authored and jacob1 committed Mar 7, 2019
1 parent 53f2018 commit 3a76a3a5144214f01161320f9991e4d638d5b80b
@@ -4,8 +4,6 @@
#include "ImageRequest.h"
#include "common/String.h"

#include <memory>

namespace http
{
class AvatarRequest : public ImageRequest
@@ -940,6 +940,14 @@ void Client::Shutdown()

Client::~Client()
{
if (versionCheckRequest)
{
versionCheckRequest->Cancel();
}
if (alternateVersionCheckRequest)
{
alternateVersionCheckRequest->Cancel();
}
}


@@ -0,0 +1,54 @@
#include "ThumbnailRenderer.h"

#include <cmath>

#include "graphics/Graphics.h"
#include "simulation/SaveRenderer.h"
#include "client/GameSave.h"

ThumbnailRendererTask::ThumbnailRendererTask(GameSave *save, int width, int height, bool autoRescale, bool decorations, bool fire) :
Save(new GameSave(*save)),
Width(width),
Height(height),
Decorations(decorations),
Fire(fire),
AutoRescale(autoRescale)
{
}

ThumbnailRendererTask::~ThumbnailRendererTask()
{
}

bool ThumbnailRendererTask::doWork()
{
thumbnail = std::unique_ptr<VideoBuffer>(SaveRenderer::Ref().Render(Save.get(), Decorations, Fire));
if (thumbnail)
{
if (AutoRescale)
{
int scaleX = (int)std::ceil((float)thumbnail->Width / Width);
int scaleY = (int)std::ceil((float)thumbnail->Height / Height);
int scale = scaleX > scaleY ? scaleX : scaleY;
int newWidth = thumbnail->Width / scale, newHeight = thumbnail->Height / scale;
thumbnail->Resize(newWidth, newHeight, true);
Width = newWidth;
Height = newHeight;
}
else
{
thumbnail->Resize(Width, Height, true);
}
return true;
}
else
{
return false;
}
}

std::unique_ptr<VideoBuffer> ThumbnailRendererTask::GetThumbnail()
{
return std::move(thumbnail);
}

@@ -0,0 +1,28 @@
#ifndef THUMBNAILRENDERER_H
#define THUMBNAILRENDERER_H

#include "tasks/Task.h"

#include <memory>

class GameSave;
class VideoBuffer;
class ThumbnailRendererTask : public Task
{
std::unique_ptr<GameSave> Save;
int Width, Height;
bool Decorations;
bool Fire;
bool AutoRescale;
std::unique_ptr<VideoBuffer> thumbnail;

public:
ThumbnailRendererTask(GameSave *save, int width, int height, bool autoRescale = false, bool decorations = true, bool fire = true);
virtual ~ThumbnailRendererTask();

virtual bool doWork() override;
std::unique_ptr<VideoBuffer> GetThumbnail();
};

#endif // THUMBNAILRENDERER_H

@@ -0,0 +1,15 @@
#include "ThumbnailRequest.h"
#include "Config.h"

namespace http
{
ThumbnailRequest::ThumbnailRequest(int saveID, int saveDate, int width, int height) :
ImageRequest((
saveDate
? ByteString::Build("http://" STATICSERVER "/", saveID, "_", saveDate, "_small.pti")
: ByteString::Build("http://" STATICSERVER "/", saveID, "_small.pti")
), width, height)
{
}
}

@@ -0,0 +1,17 @@
#ifndef THUMBNAILREQUEST2_H
#define THUMBNAILREQUEST2_H

#include "ImageRequest.h"
#include "common/String.h"

namespace http
{
class ThumbnailRequest : public ImageRequest
{
public:
ThumbnailRequest(int saveID, int saveDate, int width, int height);
};
}

#endif // THUMBNAILREQUEST2_H

@@ -6,7 +6,6 @@
#include "Component.h"
#include "graphics/Graphics.h"
#include "gui/interface/Colour.h"
#include "client/requestbroker/RequestListener.h"

#include <memory>

@@ -8,21 +8,23 @@
#include "SaveButton.h"
#include "client/Client.h"
#include "client/SaveInfo.h"
#include "client/ThumbnailRequest.h"
#include "client/ThumbnailRenderer.h"
#include "graphics/Graphics.h"
#include "simulation/SaveRenderer.h"
#include "client/requestbroker/RequestBroker.h"
#include "client/GameSave.h"

namespace ui {

SaveButton::SaveButton(Point position, Point size, SaveInfo * save):
Component(position, size),
file(NULL),
save(save),
thumbnail(NULL),
waitingForThumb(false),
triedThumbnail(false),
isMouseInsideAuthor(false),
isMouseInsideHistory(false),
showVotes(false),
thumbnailRequest(nullptr),
isButtonDown(false),
isMouseInside(false),
selected(false),
@@ -91,12 +93,12 @@ SaveButton::SaveButton(Point position, Point size, SaveFile * file):
Component(position, size),
file(file),
save(NULL),
thumbnail(NULL),
wantsDraw(false),
waitingForThumb(false),
triedThumbnail(false),
isMouseInsideAuthor(false),
isMouseInsideHistory(false),
showVotes(false),
thumbnailRequest(nullptr),
isButtonDown(false),
isMouseInside(false),
selected(false),
@@ -117,50 +119,66 @@ SaveButton::SaveButton(Point position, Point size, SaveFile * file):

SaveButton::~SaveButton()
{
RequestBroker::Ref().DetachRequestListener(this);
if (thumbnailRequest)
{
thumbnailRequest->Cancel();
}

delete thumbnail;
delete actionCallback;
delete save;
delete file;
}

void SaveButton::OnResponseReady(void * imagePtr, int identifier)
{
VideoBuffer * image = (VideoBuffer*)imagePtr;
if (image)
{
delete thumbnail;
thumbnail = image;
waitingForThumb = false;
if (file)
thumbSize = ui::Point(thumbnail->Width, thumbnail->Height);
}
}

void SaveButton::Tick(float dt)
{
if(!thumbnail && !waitingForThumb)
if (!thumbnail)
{
float scaleFactor = (Size.Y-25)/((float)YRES);
ui::Point thumbBoxSize = ui::Point(((float)XRES)*scaleFactor, ((float)YRES)*scaleFactor);
if(save)
if (!triedThumbnail)
{
if(save->GetGameSave())
float scaleFactor = (Size.Y-25)/((float)YRES);
ui::Point thumbBoxSize = ui::Point(((float)XRES)*scaleFactor, ((float)YRES)*scaleFactor);
if (save)
{
if(save->GetGameSave())
{
thumbnailRenderer = std::unique_ptr<ThumbnailRendererTask>(new ThumbnailRendererTask(save->GetGameSave(), thumbBoxSize.X, thumbBoxSize.Y));
thumbnailRenderer->Start();
triedThumbnail = true;
}
else if (save->GetID())
{
thumbnailRequest = new http::ThumbnailRequest(save->GetID(), save->GetVersion(), thumbBoxSize.X, thumbBoxSize.Y);
thumbnailRequest->Start();
triedThumbnail = true;
}
}
else if (file && file->GetGameSave())
{
waitingForThumb = true;
RequestBroker::Ref().RenderThumbnail(save->GetGameSave(), thumbBoxSize.X, thumbBoxSize.Y, this);
thumbnailRenderer = std::unique_ptr<ThumbnailRendererTask>(new ThumbnailRendererTask(file->GetGameSave(), thumbBoxSize.X, thumbBoxSize.Y, true, true, false));
thumbnailRenderer->Start();
triedThumbnail = true;
}
else if(save->GetID())
}

if (thumbnailRequest && thumbnailRequest->CheckDone())
{
thumbnail = thumbnailRequest->Finish();
thumbnailRequest = nullptr;
}

if (thumbnailRenderer)
{
thumbnailRenderer->Poll();
if (thumbnailRenderer->GetDone())
{
waitingForThumb = true;
RequestBroker::Ref().RetrieveThumbnail(save->GetID(), save->GetVersion(), thumbBoxSize.X, thumbBoxSize.Y, this);
thumbnail = thumbnailRenderer->GetThumbnail();
thumbnailRenderer.reset();
}
}
else if(file && file->GetGameSave())

if (thumbnail && file)
{
waitingForThumb = true;
RequestBroker::Ref().RenderThumbnail(file->GetGameSave(), true, false, thumbBoxSize.X, thumbBoxSize.Y, true, this);
thumbSize = ui::Point(thumbnail->Width, thumbnail->Height);
}
}
}
@@ -182,9 +200,9 @@ void SaveButton::Draw(const Point& screenPos)
{
//thumbBoxSize = ui::Point(thumbnail->Width, thumbnail->Height);
if (save && save->id)
g->draw_image(thumbnail, screenPos.X-3+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, 255);
g->draw_image(thumbnail.get(), screenPos.X-3+(Size.X-thumbBoxSize.X)/2, screenPos.Y+(Size.Y-21-thumbBoxSize.Y)/2, 255);
else
g->draw_image(thumbnail, screenPos.X+(Size.X-thumbSize.X)/2, screenPos.Y+(Size.Y-21-thumbSize.Y)/2, 255);
g->draw_image(thumbnail.get(), screenPos.X+(Size.X-thumbSize.X)/2, screenPos.Y+(Size.Y-21-thumbSize.Y)/2, 255);
}
else if (file && !file->GetGameSave())
g->drawtext(screenPos.X+(Size.X-Graphics::textwidth("Error loading save"))/2, screenPos.Y+(Size.Y-28)/2, "Error loading save", 180, 180, 180, 255);
@@ -6,10 +6,16 @@
#include "Component.h"
#include "client/SaveFile.h"
#include "client/SaveInfo.h"
#include "client/requestbroker/RequestListener.h"
#include "graphics/Graphics.h"
#include "gui/interface/Colour.h"

#include <memory>

class ThumbnailRendererTask;
namespace http
{
class ThumbnailRequest;
}
namespace ui
{
class SaveButton;
@@ -23,11 +29,11 @@ class SaveButtonAction
virtual ~SaveButtonAction() {}
};

class SaveButton : public Component, public RequestListener
class SaveButton : public Component
{
SaveFile * file;
SaveInfo * save;
VideoBuffer * thumbnail;
std::unique_ptr<VideoBuffer> thumbnail;
ui::Point thumbSize = ui::Point(0, 0);
String name;
String votesString;
@@ -36,10 +42,12 @@ class SaveButton : public Component, public RequestListener
int voteBarHeightUp;
int voteBarHeightDown;
bool wantsDraw;
bool waitingForThumb;
bool triedThumbnail;
bool isMouseInsideAuthor;
bool isMouseInsideHistory;
bool showVotes;
std::unique_ptr<ThumbnailRendererTask> thumbnailRenderer;
http::ThumbnailRequest *thumbnailRequest;
public:
SaveButton(Point position, Point size, SaveInfo * save);
SaveButton(Point position, Point size, SaveFile * file);
@@ -59,8 +67,6 @@ class SaveButton : public Component, public RequestListener
void Draw(const Point& screenPos) override;
void Tick(float dt) override;

void OnResponseReady(void * imagePtr, int identifier) override;

void SetSelected(bool selected_) { selected = selected_; }
bool GetSelected() { return selected; }
void SetSelectable(bool selectable_) { selectable = selectable_; }
Oops, something went wrong.

0 comments on commit 3a76a3a

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.