Skip to content

Commit

Permalink
Add support for using a different canvas
Browse files Browse the repository at this point in the history
  • Loading branch information
dpjudas committed Jul 7, 2024
1 parent 646f0a3 commit c4453ab
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 11 deletions.
5 changes: 2 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ set(SURREALCOMMON_SOURCES
SurrealEngine/Engine.h
SurrealEngine/GameFolder.cpp
SurrealEngine/GameFolder.h
SurrealEngine/GameWindow.cpp
SurrealEngine/GameWindow.h
SurrealEngine/UE1GameDatabase.h
SurrealEngine/UE1GameDatabase.cpp
SurrealEngine/Utils/CommandLine.cpp
Expand Down Expand Up @@ -324,8 +326,6 @@ set(SURREALCOMMON_SOURCES
SurrealEngine/UI/Launcher/PlayGamePage.h
SurrealEngine/UI/Launcher/SettingsPage.cpp
SurrealEngine/UI/Launcher/SettingsPage.h
SurrealEngine/Window/Window.cpp
SurrealEngine/Window/Window.h
)

set(SURREALCOMMON_WIN32_SOURCES
Expand Down Expand Up @@ -516,7 +516,6 @@ source_group("SurrealEngine\\Editor" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_
source_group("SurrealEngine\\Utils" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/SurrealEngine/Utils/.+")
source_group("SurrealEngine\\GC" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/SurrealEngine/GC/.+")
source_group("SurrealEngine\\VM" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/SurrealEngine/VM/.+")
source_group("SurrealEngine\\Window" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/SurrealEngine/Window/.+")
source_group("Thirdparty" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/Thirdparty/.+")
source_group("Thirdparty\\resample" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/Thirdparty/resample/.+")
source_group("Thirdparty\\dumb" REGULAR_EXPRESSION "${CMAKE_CURRENT_SOURCE_DIR}/Thirdparty/dumb/.+")
Expand Down
1 change: 1 addition & 0 deletions SurrealEngine/GameWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ GameWindow::GameWindow(GameWindowHost* windowHost) : Widget(nullptr, WidgetType:
if (!surface)
throw std::runtime_error("No vulkan surface found");
device = RenderDevice::Create(this, surface);
SetCanvas(std::make_unique<RenderDeviceCanvas>(device.get()));

SetFocus();
}
Expand Down
89 changes: 89 additions & 0 deletions SurrealEngine/RenderDevice/RenderDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,92 @@ std::unique_ptr<RenderDevice> RenderDevice::Create(GameWindow* viewport, std::sh
{
return std::make_unique<VulkanRenderDevice>(viewport, surface);
}

////////////////////////////////////////////////////////////////////////////

RenderDeviceCanvas::RenderDeviceCanvas(RenderDevice* device) : device(device)
{
}

void RenderDeviceCanvas::begin(const Colorf& color)
{
}

void RenderDeviceCanvas::end()
{
}

void RenderDeviceCanvas::begin3d()
{
}

void RenderDeviceCanvas::end3d()
{
}

Point RenderDeviceCanvas::getOrigin()
{
return origin;
}

void RenderDeviceCanvas::setOrigin(const Point& newOrigin)
{
origin = newOrigin;
}

void RenderDeviceCanvas::pushClip(const Rect& box)
{
}

void RenderDeviceCanvas::popClip()
{
}

void RenderDeviceCanvas::fillRect(const Rect& box, const Colorf& color)
{
}

void RenderDeviceCanvas::line(const Point& p0, const Point& p1, const Colorf& color)
{
}

void RenderDeviceCanvas::drawText(const Point& pos, const Colorf& color, const std::string& text)
{
}

Rect RenderDeviceCanvas::measureText(const std::string& text)
{
return Rect();
}

VerticalTextPosition RenderDeviceCanvas::verticalTextAlign()
{
return VerticalTextPosition();
}

void RenderDeviceCanvas::drawText(const std::shared_ptr<Font>& font, const Point& pos, const std::string& text, const Colorf& color)
{
}

void RenderDeviceCanvas::drawTextEllipsis(const std::shared_ptr<Font>& font, const Point& pos, const Rect& clipBox, const std::string& text, const Colorf& color)
{
}

Rect RenderDeviceCanvas::measureText(const std::shared_ptr<Font>& font, const std::string& text)
{
return Rect();
}

FontMetrics RenderDeviceCanvas::getFontMetrics(const std::shared_ptr<Font>& font)
{
return FontMetrics();
}

int RenderDeviceCanvas::getCharacterIndex(const std::shared_ptr<Font>& font, const std::string& text, const Point& hitPoint)
{
return 0;
}

void RenderDeviceCanvas::drawImage(const std::shared_ptr<Image>& image, const Point& pos)
{
}
40 changes: 40 additions & 0 deletions SurrealEngine/RenderDevice/RenderDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#include "UObject/UTexture.h"

#include <zwidget/core/canvas.h>
#include <zwidget/core/rect.h>

class GameWindow;
class UTexture;
class UActor;
Expand Down Expand Up @@ -116,3 +119,40 @@ class RenderDevice
bool PrecacheOnFlip = false;
float Brightness = 0.5f;
};

class RenderDeviceCanvas : public Canvas
{
public:
RenderDeviceCanvas(RenderDevice* device);

void begin(const Colorf& color) override;
void end() override;

void begin3d() override;
void end3d() override;

Point getOrigin() override;
void setOrigin(const Point& origin) override;

void pushClip(const Rect& box) override;
void popClip() override;

void fillRect(const Rect& box, const Colorf& color) override;
void line(const Point& p0, const Point& p1, const Colorf& color) override;

void drawText(const Point& pos, const Colorf& color, const std::string& text) override;
Rect measureText(const std::string& text) override;
VerticalTextPosition verticalTextAlign() override;

void drawText(const std::shared_ptr<Font>& font, const Point& pos, const std::string& text, const Colorf& color) override;
void drawTextEllipsis(const std::shared_ptr<Font>& font, const Point& pos, const Rect& clipBox, const std::string& text, const Colorf& color) override;
Rect measureText(const std::shared_ptr<Font>& font, const std::string& text) override;
FontMetrics getFontMetrics(const std::shared_ptr<Font>& font) override;
int getCharacterIndex(const std::shared_ptr<Font>& font, const std::string& text, const Point& hitPoint) override;

void drawImage(const std::shared_ptr<Image>& image, const Point& pos) override;

private:
RenderDevice* device = nullptr;
Point origin;
};
5 changes: 4 additions & 1 deletion Thirdparty/ZWidget/include/zwidget/core/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ class FontMetrics
class Canvas
{
public:
static std::unique_ptr<Canvas> create(DisplayWindow* window);
static std::unique_ptr<Canvas> create();

virtual ~Canvas() = default;

virtual void attach(DisplayWindow* window) { }
virtual void detach() { }

virtual void begin(const Colorf& color) = 0;
virtual void end() = 0;

Expand Down
1 change: 1 addition & 0 deletions Thirdparty/ZWidget/include/zwidget/core/widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class Widget : DisplayWindowHost

static Size GetScreenSize();

void SetCanvas(std::unique_ptr<Canvas> canvas);
void* GetNativeHandle();
int GetNativePixelWidth();
int GetNativePixelHeight();
Expand Down
13 changes: 7 additions & 6 deletions Thirdparty/ZWidget/src/core/canvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ class CanvasFontGroup
class BitmapCanvas : public Canvas
{
public:
BitmapCanvas(DisplayWindow* window);
~BitmapCanvas();
void attach(DisplayWindow* window) override;
void detach() override;

void begin(const Colorf& color) override;
void end() override;
Expand Down Expand Up @@ -252,15 +252,16 @@ class BitmapCanvas : public Canvas
std::string language;
};

BitmapCanvas::BitmapCanvas(DisplayWindow* window) : window(window)
void BitmapCanvas::attach(DisplayWindow* newWindow)
{
window = newWindow;
uiscale = window->GetDpiScale();
uint32_t white = 0xffffffff;
whiteTexture = createTexture(1, 1, &white);
font = std::make_unique<CanvasFontGroup>("NotoSans", 13.0 * uiscale);
}

BitmapCanvas::~BitmapCanvas()
void BitmapCanvas::detach()
{
}

Expand Down Expand Up @@ -1071,7 +1072,7 @@ void BitmapCanvas::end3d()

/////////////////////////////////////////////////////////////////////////////

std::unique_ptr<Canvas> Canvas::create(DisplayWindow* window)
std::unique_ptr<Canvas> Canvas::create()
{
return std::make_unique<BitmapCanvas>(window);
return std::make_unique<BitmapCanvas>();
}
16 changes: 15 additions & 1 deletion Thirdparty/ZWidget/src/core/widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Widget::Widget(Widget* parent, WidgetType type) : Type(type)
{
Widget* owner = parent ? parent->Window() : nullptr;
DispWindow = DisplayWindow::Create(this, type == WidgetType::Popup, owner ? owner->DispWindow.get() : nullptr);
DispCanvas = Canvas::create(DispWindow.get());
DispCanvas = Canvas::create();
DispCanvas->attach(DispWindow.get());
SetStyleState("root");

SetWindowBackground(GetStyleColor("window-background"));
Expand All @@ -30,6 +31,9 @@ Widget::Widget(Widget* parent, WidgetType type) : Type(type)

Widget::~Widget()
{
if (DispCanvas)
DispCanvas->detach();

while (LastChildObj)
delete LastChildObj;

Expand All @@ -39,6 +43,16 @@ Widget::~Widget()
DetachFromParent();
}

void Widget::SetCanvas(std::unique_ptr<Canvas> canvas)
{
if (DispWindow)
{
DispCanvas->detach();
DispCanvas = std::move(canvas);
DispCanvas->attach(DispWindow.get());
}
}

void Widget::SetParent(Widget* newParent)
{
if (ParentObj != newParent)
Expand Down

0 comments on commit c4453ab

Please sign in to comment.