Refactoring. Add line drawing.

This commit is contained in:
elasota
2019-12-27 18:05:32 -05:00
parent 7f8c46ecb2
commit 8b82ecabef
34 changed files with 676 additions and 557 deletions

View File

@@ -1,6 +1,7 @@
#include "DisplayDeviceManager.h"
#include "HostDisplayDriver.h"
#include "IGpDisplayDriver.h"
#include "PLQDraw.h"
#include "MemoryManager.h"
#include "QDStandardPalette.h"
@@ -15,8 +16,8 @@ namespace PortabilityLayer
void Init() override;
void Shutdown() override;
GDevice **GetMainDevice() override;
GpPixelFormat_t GetPixelFormat() const override;
void SyncPalette(IGpDisplayDriver *displayDriver) override;
void IncrementTickCount(uint32_t count) override;
uint32_t GetTickCount() override;
@@ -24,50 +25,62 @@ namespace PortabilityLayer
static DisplayDeviceManagerImpl *GetInstance();
private:
GDHandle m_mainDevice;
uint32_t m_tickCount;
GpPixelFormat_t m_pixelFormat;
bool m_paletteIsDirty;
PortabilityLayer::RGBAColor *m_palette;
uint8_t m_paletteStorage[256 * sizeof(PortabilityLayer::RGBAColor) + PL_SYSTEM_MEMORY_ALIGNMENT];
static DisplayDeviceManagerImpl ms_instance;
};
DisplayDeviceManagerImpl::DisplayDeviceManagerImpl()
: m_mainDevice(nullptr)
, m_tickCount(1)
: m_tickCount(1)
, m_paletteIsDirty(true)
, m_pixelFormat(GpPixelFormats::k8BitStandard)
{
uint8_t *paletteStorage = m_paletteStorage;
while (reinterpret_cast<intptr_t>(paletteStorage) % PL_SYSTEM_MEMORY_ALIGNMENT != 0)
paletteStorage++;
m_palette = reinterpret_cast<PortabilityLayer::RGBAColor*>(paletteStorage);
for (size_t i = 0; i < 256; i++)
{
PortabilityLayer::RGBAColor &color = m_palette[i];
color.r = color.g = color.b = i;
color.a = 255;
}
}
void DisplayDeviceManagerImpl::Init()
{
m_mainDevice = MemoryManager::GetInstance()->NewHandle<GDevice>();
if (m_mainDevice)
{
GDevice *device = *m_mainDevice;
HostDisplayDriver::GetInstance()->GetDisplayResolution(nullptr, nullptr, &m_pixelFormat);
HostDisplayDriver::GetInstance()->GetDisplayResolution(nullptr, nullptr, &device->pixelFormat);
const PortabilityLayer::RGBAColor *spColors = StandardPalette::GetInstance()->GetColors();
for (size_t i = 0; i < 256; i++)
m_palette[i] = spColors[i];
uint8_t *paletteStorage = device->paletteStorage;
while (reinterpret_cast<intptr_t>(paletteStorage) % PL_SYSTEM_MEMORY_ALIGNMENT != 0)
paletteStorage++;
PortabilityLayer::RGBAColor *paletteData = reinterpret_cast<PortabilityLayer::RGBAColor*>(paletteStorage);
device->paletteDataOffset = static_cast<uint8_t>(paletteStorage - device->paletteStorage);
const PortabilityLayer::RGBAColor *spColors = StandardPalette::GetInstance()->GetColors();
for (size_t i = 0; i < 256; i++)
paletteData[i] = spColors[i];
device->paletteIsDirty = true;
}
m_paletteIsDirty = true;
}
void DisplayDeviceManagerImpl::Shutdown()
{
MemoryManager::GetInstance()->ReleaseHandle(reinterpret_cast<MMHandleBlock*>(m_mainDevice));
}
GDevice **DisplayDeviceManagerImpl::GetMainDevice()
GpPixelFormat_t DisplayDeviceManagerImpl::GetPixelFormat() const
{
return m_mainDevice;
return m_pixelFormat;
}
void DisplayDeviceManagerImpl::SyncPalette(IGpDisplayDriver *displayDriver)
{
if (m_paletteIsDirty)
{
displayDriver->UpdatePalette(m_palette);
m_paletteIsDirty = false;
}
}
void DisplayDeviceManagerImpl::IncrementTickCount(uint32_t count)

View File

@@ -2,9 +2,10 @@
#ifndef __PL_DEVICE_MANAGER_H__
#define __PL_DEVICE_MANAGER_H__
#include <stdint.h>
struct GDevice;
#include <stdint.h>
#include "GpPixelFormat.h"
struct IGpDisplayDriver;
namespace PortabilityLayer
{
@@ -12,9 +13,11 @@ namespace PortabilityLayer
{
public:
virtual void Init() = 0;
virtual void Shutdown() = 0;
virtual void Shutdown() = 0;
virtual GpPixelFormat_t GetPixelFormat() const = 0;
virtual void SyncPalette(IGpDisplayDriver *displayDriver) = 0;
virtual GDevice **GetMainDevice() = 0;
virtual void IncrementTickCount(uint32_t count) = 0;
virtual uint32_t GetTickCount() = 0;

View File

@@ -553,7 +553,7 @@ namespace PortabilityLayer
{
int depth = PortabilityLayer::QDManager::GetInstance()->DepthForPixelFormat(pixelFormat);
if (qdManager->NewGWorld(&m_menuBarGraf, depth, menuRect, nullptr, nullptr, 0) != 0)
if (qdManager->NewGWorld(&m_menuBarGraf, depth, menuRect, nullptr, 0) != 0)
return;
}
@@ -569,11 +569,9 @@ namespace PortabilityLayer
RefreshMenuBarLayout();
CGraf *oldGraf;
GDHandle oldDevice;
GetGWorld(&oldGraf, &oldDevice);
CGraf *oldGraf = GetGraphicsPort();
SetGWorld(m_menuBarGraf, nullptr);
SetGraphicsPort(m_menuBarGraf);
PortabilityLayer::QDState *qdState = qdManager->GetState();
@@ -705,7 +703,7 @@ namespace PortabilityLayer
}
}
SetGWorld(oldGraf, oldDevice);
SetGraphicsPort(oldGraf);
m_menuBarGraf->m_port.SetDirty(QDPortDirtyFlag_Contents);
}
@@ -1019,15 +1017,13 @@ namespace PortabilityLayer
GpPixelFormat_t pixelFormat;
PortabilityLayer::HostDisplayDriver::GetInstance()->GetDisplayResolution(nullptr, nullptr, &pixelFormat);
if (qdManager->NewGWorld(&m_menuGraf, qdManager->DepthForPixelFormat(pixelFormat), menuRect, nullptr, nullptr, 0) != 0)
if (qdManager->NewGWorld(&m_menuGraf, qdManager->DepthForPixelFormat(pixelFormat), menuRect, nullptr, 0) != 0)
return;
}
CGrafPtr oldGraf = nullptr;
GDHandle oldDevice = nullptr;
GetGWorld(&oldGraf, &oldDevice);
CGrafPtr oldGraf = GetGraphicsPort();
SetGWorld(m_menuGraf, nullptr);
SetGraphicsPort(m_menuGraf);
QDState *qdState = qdManager->GetState();
@@ -1077,7 +1073,7 @@ namespace PortabilityLayer
m_menuGraf->m_port.SetDirty(QDPortDirtyFlag_Contents);
SetGWorld(oldGraf, oldDevice);
SetGraphicsPort(oldGraf);
}
MenuManager *MenuManager::GetInstance()

View File

@@ -129,7 +129,7 @@ namespace PortabilityLayer
void *imageData = m_pixMap->GetPixelData();
if (m_pixMap->GetPixelFormat() == GpPixelFormats::k8BitStandard)
if (m_pixMap->GetPixelFormat() == GpPixelFormats::k8BitStandard || m_pixMap->GetPixelFormat() == GpPixelFormats::kBW1)
{
switch (m_blitType)
{
@@ -232,9 +232,9 @@ namespace PortabilityLayer
}
}
OSErr NewGWorld(GWorldPtr *gworld, int depth, const Rect *bounds, CTabHandle colorTable, GDHandle device, int flags)
OSErr NewGWorld(GWorldPtr *gworld, int depth, const Rect *bounds, CTabHandle colorTable, int flags)
{
return PortabilityLayer::QDManager::GetInstance()->NewGWorld(gworld, depth, *bounds, colorTable, device, flags);
return PortabilityLayer::QDManager::GetInstance()->NewGWorld(gworld, depth, *bounds, colorTable, flags);
}
void DisposeGWorld(GWorldPtr gworld)
@@ -288,8 +288,7 @@ void DrawPicture(PicHandle pict, Rect *bounds)
}
PortabilityLayer::QDManager *qdManager = PortabilityLayer::QDManager::GetInstance();
PortabilityLayer::QDPort *port;
qdManager->GetPort(&port, nullptr);
PortabilityLayer::QDPort *port = qdManager->GetPort();
if (!port)
return;
@@ -299,12 +298,15 @@ void DrawPicture(PicHandle pict, Rect *bounds)
long handleSize = GetHandleSize(reinterpret_cast<Handle>(pict));
PortabilityLayer::MemReaderStream stream(picPtr, handleSize);
// Adjust draw origin
const PortabilityLayer::Vec2i drawOrigin = PortabilityLayer::Vec2i(bounds->left - picPtr->picFrame.left, bounds->top - picPtr->picFrame.top);
switch (pixMap->GetPixelFormat())
{
case GpPixelFormats::kBW1:
case GpPixelFormats::k8BitStandard:
{
PortabilityLayer::PixMapBlitEmitter blitEmitter(PortabilityLayer::Vec2i(bounds->left, bounds->top), pixMap);
PortabilityLayer::PixMapBlitEmitter blitEmitter(drawOrigin, pixMap);
PortabilityLayer::QDPictDecoder decoder;
decoder.DecodePict(&stream, &blitEmitter);
@@ -317,18 +319,17 @@ void DrawPicture(PicHandle pict, Rect *bounds)
};
}
void GetGWorld(CGrafPtr *gw, GDHandle *gdHandle)
CGraf *GetGraphicsPort()
{
PortabilityLayer::QDPort *port;
PortabilityLayer::QDManager::GetInstance()->GetPort(&port, gdHandle);
PortabilityLayer::QDPort *port = PortabilityLayer::QDManager::GetInstance()->GetPort();
CGrafPtr grafPtr = reinterpret_cast<CGrafPtr>(port);
CGraf *grafPtr = reinterpret_cast<CGraf *>(port);
assert(&grafPtr->m_port == port);
*gw = grafPtr;
return grafPtr;
}
void SetGWorld(CGrafPtr gw, GDHandle gdHandle)
void SetGraphicsPort(CGrafPtr gw)
{
PortabilityLayer::QDManager::GetInstance()->SetPort(&gw->m_port, gdHandle);
PortabilityLayer::QDManager::GetInstance()->SetPort(&gw->m_port);
}

View File

@@ -25,7 +25,7 @@ enum QDFlags
useTempMem = 1,
};
OSErr NewGWorld(GWorldPtr *gworld, int depth, const Rect *bounds, CTabHandle colorTable, GDHandle device, int flags);
OSErr NewGWorld(GWorldPtr *gworld, int depth, const Rect *bounds, CTabHandle colorTable, int flags);
void DisposeGWorld(GWorldPtr gworld);
PixMapHandle GetGWorldPixMap(GWorldPtr gworld);
@@ -37,8 +37,8 @@ void OffsetRect(Rect *rect, int right, int down);
void DrawPicture(PicHandle pict, Rect *bounds);
void GetGWorld(CGrafPtr *gw, GDHandle *gdHandle);
void SetGWorld(CGrafPtr gw, GDHandle gdHandle);
CGrafPtr GetGraphicsPort();
void SetGraphicsPort(CGrafPtr gw);
#endif

View File

@@ -4,12 +4,14 @@
#include "DisplayDeviceManager.h"
#include "FontFamily.h"
#include "FontManager.h"
#include "LinePlotter.h"
#include "MMHandleBlock.h"
#include "MemoryManager.h"
#include "HostFontHandler.h"
#include "PLPasStr.h"
#include "RenderedFont.h"
#include "RenderedGlyphMetrics.h"
#include "Rect2i.h"
#include "ResourceManager.h"
#include "ResTypeID.h"
#include "RGBAColor.h"
@@ -18,10 +20,21 @@
#include "QDGraf.h"
#include "QDPixMap.h"
#include "QDUtils.h"
#include "Vec2i.h"
#include <algorithm>
#include <assert.h>
enum PaintColorResolution
{
PaintColorResolution_Fore,
PaintColorResolution_Back,
PaintColorResolution_Pen,
};
static void PaintRectWithPCR(const Rect &rect, PaintColorResolution pcr);
void GetPort(GrafPtr *graf)
{
PL_NotYetImplemented();
@@ -29,11 +42,7 @@ void GetPort(GrafPtr *graf)
void SetPort(GrafPtr graf)
{
PortabilityLayer::QDManager *qd = PortabilityLayer::QDManager::GetInstance();
GDHandle device;
qd->GetPort(nullptr, &device);
qd->SetPort(graf, device);
PortabilityLayer::QDManager::GetInstance()->SetPort(graf);
}
void BeginUpdate(WindowPtr graf)
@@ -83,18 +92,10 @@ void SetRect(Rect *rect, short left, short top, short right, short bottom)
rect->right = right;
}
GDHandle GetMainDevice()
{
return PortabilityLayer::DisplayDeviceManager::GetInstance()->GetMainDevice();
}
void SetPortWindowPort(WindowPtr window)
{
PortabilityLayer::WindowManager *wm = PortabilityLayer::WindowManager::GetInstance();
GDevice **device = wm->GetWindowDevice(window);
PortabilityLayer::QDManager::GetInstance()->SetPort(&window->m_graf.m_port, device);
PortabilityLayer::QDManager::GetInstance()->SetPort(&window->m_graf.m_port);
}
void SetPortDialogPort(Dialog *dialog)
@@ -131,9 +132,136 @@ void MoveTo(int x, int y)
penPos.v = y;
}
static void PlotLine(PortabilityLayer::QDState *qdState, PortabilityLayer::QDPort *qdPort, const PortabilityLayer::Vec2i &pointA, const PortabilityLayer::Vec2i &pointB)
{
const Rect lineRect = Rect::Create(
std::min(pointA.m_y, pointB.m_y),
std::min(pointA.m_x, pointB.m_x),
std::max(pointA.m_y, pointB.m_y) + 1,
std::max(pointA.m_x, pointB.m_x) + 1);
// If the points are a straight line, paint as a rect
if (pointA.m_y == pointB.m_y || pointA.m_x == pointB.m_x)
{
PaintRectWithPCR(lineRect, PaintColorResolution_Fore);
return;
}
GpPixelFormat_t pixelFormat = qdPort->GetPixelFormat();
Rect constrainedRect = qdPort->GetRect();
if (qdState->m_clipRegion)
{
const Region &region = **qdState->m_clipRegion;
if (region.size > sizeof(Region))
PL_NotYetImplemented();
constrainedRect = constrainedRect.Intersect(region.rect);
}
constrainedRect = constrainedRect.Intersect(lineRect);
if (!constrainedRect.IsValid())
return;
PortabilityLayer::Vec2i upperPoint = pointA;
PortabilityLayer::Vec2i lowerPoint = pointB;
if (pointA.m_y > pointB.m_y)
std::swap(upperPoint, lowerPoint);
PortabilityLayer::PixMapImpl *pixMap = static_cast<PortabilityLayer::PixMapImpl*>(*qdPort->GetPixMap());
const size_t pitch = pixMap->GetPitch();
uint8_t *pixData = static_cast<uint8_t*>(pixMap->GetPixelData());
PortabilityLayer::LinePlotter plotter;
plotter.Reset(upperPoint, lowerPoint);
PortabilityLayer::Vec2i currentPoint = pointA;
while (currentPoint.m_x < constrainedRect.left || currentPoint.m_y < constrainedRect.top || currentPoint.m_x >= constrainedRect.right)
{
PortabilityLayer::PlotDirection plotDir = plotter.PlotNext();
if (plotDir == PortabilityLayer::PlotDirection_Exhausted)
return;
currentPoint = plotter.GetPoint();
}
assert(currentPoint.m_y < constrainedRect.bottom);
size_t plotIndex = static_cast<size_t>(currentPoint.m_y) * pitch + static_cast<size_t>(currentPoint.m_x);
const size_t plotLimit = pixMap->GetPitch() * (pixMap->m_rect.bottom - pixMap->m_rect.top);
switch (pixelFormat)
{
case GpPixelFormats::k8BitStandard:
{
const size_t pixelSize = 1;
const uint8_t color = qdState->ResolveForeColor8(nullptr, 0);
while (currentPoint.m_x >= constrainedRect.left && currentPoint.m_x < constrainedRect.right && currentPoint.m_y < constrainedRect.bottom)
{
assert(plotIndex < plotLimit);
pixData[plotIndex] = color;
PortabilityLayer::PlotDirection plotDir = plotter.PlotNext();
if (plotDir == PortabilityLayer::PlotDirection_Exhausted)
return;
switch (plotDir)
{
default:
case PortabilityLayer::PlotDirection_Exhausted:
return;
case PortabilityLayer::PlotDirection_NegX_NegY:
case PortabilityLayer::PlotDirection_0X_NegY:
case PortabilityLayer::PlotDirection_PosX_NegY:
// These should never happen, the point order is swapped so that Y is always 0 or positive
assert(false);
return;
case PortabilityLayer::PlotDirection_NegX_PosY:
currentPoint.m_x--;
currentPoint.m_y++;
plotIndex = plotIndex + pitch - pixelSize;
break;
case PortabilityLayer::PlotDirection_0X_PosY:
currentPoint.m_y++;
plotIndex = plotIndex + pitch;
break;
case PortabilityLayer::PlotDirection_PosX_PosY:
currentPoint.m_x++;
currentPoint.m_y++;
plotIndex = plotIndex + pitch + pixelSize;
break;
case PortabilityLayer::PlotDirection_NegX_0Y:
currentPoint.m_x--;
plotIndex = plotIndex - pixelSize;
break;
case PortabilityLayer::PlotDirection_PosX_0Y:
currentPoint.m_x++;
plotIndex = plotIndex + pixelSize;
break;
}
}
}
break;
default:
PL_NotYetImplemented();
return;
}
}
void LineTo(int x, int y)
{
PL_NotYetImplemented_TODO("Polys");
PortabilityLayer::QDManager *qdManager = PortabilityLayer::QDManager::GetInstance();
PortabilityLayer::QDState *qdState = qdManager->GetState();
PlotLine(qdState, qdManager->GetPort(), PortabilityLayer::Vec2i(qdState->m_penPos.h, qdState->m_penPos.v), PortabilityLayer::Vec2i(x, y));
}
void SetOrigin(int x, int y)
@@ -216,9 +344,7 @@ void GetForeColor(RGBColor *color)
void Index2Color(int index, RGBColor *color)
{
PortabilityLayer::QDPort *port;
PortabilityLayer::QDManager::GetInstance()->GetPort(&port, nullptr);
PortabilityLayer::QDPort *port = PortabilityLayer::QDManager::GetInstance()->GetPort();
GpPixelFormat_t pf = port->GetPixelFormat();
if (pf == GpPixelFormats::k8BitCustom)
@@ -315,8 +441,7 @@ void DrawString(const PLPasStr &str)
{
PortabilityLayer::QDManager *qdManager = PortabilityLayer::QDManager::GetInstance();
PortabilityLayer::QDPort *port = nullptr;
qdManager->GetPort(&port, nullptr);
PortabilityLayer::QDPort *port = qdManager->GetPort();
PortabilityLayer::QDState *qdState = qdManager->GetState();
@@ -368,17 +493,16 @@ void DrawString(const PLPasStr &str)
DrawGlyph(qdState, pixMap, rect, penPos, rfont, chars[i]);
}
void PaintRect(const Rect *rect)
void PaintRectWithPCR(const Rect &rect, PaintColorResolution pcr)
{
if (!rect->IsValid())
if (!rect.IsValid())
return;
PortabilityLayer::QDPort *qdPort;
PortabilityLayer::QDManager::GetInstance()->GetPort(&qdPort, nullptr);
PortabilityLayer::QDPort *qdPort = PortabilityLayer::QDManager::GetInstance()->GetPort();
GpPixelFormat_t pixelFormat = qdPort->GetPixelFormat();
Rect constrainedRect = *rect;
Rect constrainedRect = rect;
PortabilityLayer::QDState *qdState = qdPort->GetState();
@@ -408,7 +532,19 @@ void PaintRect(const Rect *rect)
{
case GpPixelFormats::k8BitStandard:
{
const uint8_t color = qdState->ResolveForeColor8(nullptr, 0);
uint8_t color = 0;
switch (pcr)
{
case PaintColorResolution_Fore:
color = qdState->ResolveForeColor8(nullptr, 0);
break;
case PaintColorResolution_Back:
color = qdState->ResolveBackColor8(nullptr, 0);
break;
default:
assert(false);
break;
}
size_t scanlineIndex = 0;
for (size_t ln = 0; ln < numLines; ln++)
@@ -425,6 +561,12 @@ void PaintRect(const Rect *rect)
}
}
void PaintRect(const Rect *rect)
{
PaintRectWithPCR(*rect, PaintColorResolution_Fore);
}
void PaintOval(const Rect *rect)
{
PL_NotYetImplemented_TODO("Ovals");
@@ -462,14 +604,16 @@ void FrameRoundRect(const Rect *rect, int w, int h)
PL_NotYetImplemented_TODO("Ovals");
}
void PenMode(CopyBitsMode copyBitsMode)
void PenInvertMode(bool invertMode)
{
PL_NotYetImplemented();
PortabilityLayer::QDState *qdState = PortabilityLayer::QDManager::GetInstance()->GetState();
qdState->m_penInvert = invertMode;
}
void PenMode(PenModeID penMode)
void PenMask(bool maskMode)
{
PL_NotYetImplemented_TODO("Polys");
PortabilityLayer::QDState *qdState = PortabilityLayer::QDManager::GetInstance()->GetState();
qdState->m_penMask = maskMode;
}
void PenPat(const Pattern *pattern)
@@ -484,7 +628,9 @@ void PenSize(int w, int h)
void PenNormal()
{
PL_NotYetImplemented_TODO("Polys");
PortabilityLayer::QDState *qdState = PortabilityLayer::QDManager::GetInstance()->GetState();
qdState->m_penInvert = false;
qdState->m_penMask = false;
}
void EraseRect(const Rect *rect)
@@ -507,7 +653,18 @@ void InsetRect(Rect *rect, int x, int y)
void Line(int x, int y)
{
PL_NotYetImplemented_TODO("Polys");
PortabilityLayer::QDManager *qdManager = PortabilityLayer::QDManager::GetInstance();
PortabilityLayer::QDState *qdState = qdManager->GetState();
const PortabilityLayer::Vec2i oldPos = PortabilityLayer::Vec2i(qdState->m_penPos.h, qdState->m_penPos.v);
qdState->m_penPos.h += x;
qdState->m_penPos.v += y;
const PortabilityLayer::Vec2i newPos = PortabilityLayer::Vec2i(qdState->m_penPos.h, qdState->m_penPos.v);
PlotLine(qdState, qdManager->GetPort(), oldPos, newPos);
}
Pattern *GetQDGlobalsGray(Pattern *pattern)
@@ -555,6 +712,7 @@ static void CopyBitsComplete(const BitMap *srcBitmap, const BitMap *maskBitmap,
const GpPixelFormat_t pixelFormat = srcBitmap->m_pixelFormat;
const size_t srcPitch = srcBitmap->m_pitch;
const size_t destPitch = destBitmap->m_pitch;
const size_t maskPitch = (maskBitmap != nullptr) ? maskBitmap->m_pitch : 0;
if (srcRectBase->right - srcRectBase->left != destRectBase->right - destRectBase->left ||
srcRectBase->bottom - srcRectBase->top != destRectBase->bottom - destRectBase->top)
@@ -566,7 +724,8 @@ static void CopyBitsComplete(const BitMap *srcBitmap, const BitMap *maskBitmap,
if (maskBitmap)
{
assert(maskRectBase);
assert(maskRectBase->right - maskRectBase->left == destRectBase->right - destRectBase->left);
assert(maskRectBase->right - maskRectBase->left == srcRectBase->right - srcRectBase->left);
assert(maskBitmap->m_pixelFormat == GpPixelFormats::kBW1 || maskBitmap->m_pixelFormat == GpPixelFormats::k8BitStandard);
}
assert((maskBitmap == nullptr) == (maskRectBase == nullptr));
@@ -589,6 +748,15 @@ static void CopyBitsComplete(const BitMap *srcBitmap, const BitMap *maskBitmap,
const int32_t srcTop = srcRectBase->top + topNudge;
const int32_t srcBottom = srcRectBase->bottom + bottomNudge;
maskRect = Rect::Create(0, 0, 0, 0);
if (maskRectBase)
{
maskRect.left = maskRectBase->left + leftNudge;
maskRect.right = maskRectBase->right + rightNudge;
maskRect.top = maskRectBase->top + topNudge;
maskRect.bottom = maskRectBase->bottom + bottomNudge;
}
if (srcTop >= srcBottom)
return;
@@ -639,12 +807,24 @@ static void CopyBitsComplete(const BitMap *srcBitmap, const BitMap *maskBitmap,
constrainedSrcRect.top += constrainedDestRect.top - destRect.top;
constrainedSrcRect.bottom += constrainedDestRect.bottom - destRect.bottom;
Rect constrainedMaskRect = maskRect;
if (maskRectBase != nullptr)
{
constrainedMaskRect.left += constrainedDestRect.left - destRect.left;
constrainedMaskRect.right += constrainedDestRect.right - destRect.right;
constrainedMaskRect.top += constrainedDestRect.top - destRect.top;
constrainedMaskRect.bottom += constrainedDestRect.bottom - destRect.bottom;
}
const size_t srcFirstCol = constrainedSrcRect.left - srcBitmap->m_rect.left;
const size_t srcFirstRow = constrainedSrcRect.top - srcBitmap->m_rect.top;
const size_t destFirstCol = constrainedDestRect.left - destBitmap->m_rect.left;
const size_t destFirstRow = constrainedDestRect.top - destBitmap->m_rect.top;
const size_t maskFirstCol = maskBitmap ? constrainedMaskRect.left - maskBitmap->m_rect.left : 0;
const size_t maskFirstRow = maskBitmap ? constrainedMaskRect.top - maskBitmap->m_rect.top : 0;
if (mask && mask->size != sizeof(Region))
{
PL_NotYetImplemented();
@@ -672,22 +852,65 @@ static void CopyBitsComplete(const BitMap *srcBitmap, const BitMap *maskBitmap,
const uint8_t *srcBytes = static_cast<const uint8_t*>(srcBitmap->m_data);
uint8_t *destBytes = static_cast<uint8_t*>(destBitmap->m_data);
const uint8_t *maskBytes = maskBitmap ? static_cast<uint8_t*>(maskBitmap->m_data) : nullptr;
const size_t firstSrcByte = srcFirstRow * srcPitch + srcFirstCol * pixelSizeBytes;
const size_t firstDestByte = destFirstRow * destPitch + destFirstCol * pixelSizeBytes;
const size_t firstMaskRowByte = maskBitmap ? maskFirstRow * maskPitch : 0;
const size_t numCopiedRows = srcRect.bottom - srcRect.top;
const size_t numCopiedCols = srcRect.right - srcRect.left;
const size_t numCopiedBytesPerScanline = numCopiedCols * pixelSizeBytes;
for (size_t i = 0; i < numCopiedRows; i++)
memcpy(destBytes + firstDestByte + i * destPitch, srcBytes + firstSrcByte + i * srcPitch, numCopiedBytesPerScanline);
if (maskBitmap)
{
for (size_t i = 0; i < numCopiedRows; i++)
{
uint8_t *destRow = destBytes + firstDestByte + i * destPitch;
const uint8_t *srcRow = srcBytes + firstSrcByte + i * srcPitch;
const uint8_t *rowMaskBytes = maskBytes + firstMaskRowByte + i * maskPitch;
size_t span = 0;
for (size_t col = 0; col < numCopiedCols; col++)
{
const size_t maskBitOffset = maskFirstCol + col;
//const bool maskBit = ((maskBytes[maskBitOffset / 8] & (0x80 >> (maskBitOffset & 7))) != 0);
const bool maskBit = (rowMaskBytes[maskBitOffset] != 0);
if (maskBit)
span += pixelSizeBytes;
else
{
if (span != 0)
memcpy(destRow + col * pixelSizeBytes - span, srcRow + col * pixelSizeBytes - span, span);
span = 0;
}
}
if (span != 0)
memcpy(destRow + numCopiedCols * pixelSizeBytes - span, srcRow + numCopiedCols * pixelSizeBytes - span, span);
}
}
else
{
for (size_t i = 0; i < numCopiedRows; i++)
memcpy(destBytes + firstDestByte + i * destPitch, srcBytes + firstSrcByte + i * srcPitch, numCopiedBytesPerScanline);
}
}
}
void CopyBits(const BitMap *srcBitmap, BitMap *destBitmap, const Rect *srcRectBase, const Rect *destRectBase, CopyBitsMode copyMode, RgnHandle maskRegion)
{
CopyBitsComplete(srcBitmap, nullptr, destBitmap, srcRectBase, nullptr, destRectBase, maskRegion);
const BitMap *maskBitmap = nullptr;
const Rect *maskRect = nullptr;
if (copyMode == transparent && srcBitmap->m_pixelFormat == GpPixelFormats::k8BitStandard)
{
maskBitmap = srcBitmap;
maskRect = srcRectBase;
}
CopyBitsComplete(srcBitmap, maskBitmap, destBitmap, srcRectBase, maskRect, destRectBase, maskRegion);
}
void CopyMask(const BitMap *srcBitmap, const BitMap *maskBitmap, BitMap *destBitmap, const Rect *srcRectBase, const Rect *maskRectBase, const Rect *destRectBase)

View File

@@ -56,8 +56,9 @@ enum CopyBitsMode
enum PenModeID
{
patOr = transparent + 1,
patXor,
PenMode_Solid,
PenMode_Pattern,
PenMode_PatternInvert,
};
struct CIcon
@@ -81,9 +82,6 @@ struct RGBColor
unsigned short blue;
};
typedef GDevice *GDPtr;
typedef GDPtr *GDHandle;
typedef CIcon *CIconPtr;
typedef CIconPtr *CIconHandle;
@@ -109,8 +107,6 @@ void DisposeCIcon(CIconHandle icon);
void SetRect(Rect *rect, short left, short top, short right, short bottom);
GDHandle GetMainDevice();
void TextSize(int sz);
void TextFace(int face);
void TextFont(int fontID);
@@ -132,8 +128,8 @@ void ClipRect(const Rect *rect); // Sets the clipping area
void FrameRect(const Rect *rect);
void FrameOval(const Rect *rect);
void FrameRoundRect(const Rect *rect, int w, int h);
void PenMode(CopyBitsMode copyBitsMode);
void PenMode(PenModeID mode);
void PenInvertMode(bool invertMode);
void PenMask(bool maskMode);
void PenPat(const Pattern *pattern);
void PenSize(int w, int h);
void PenNormal();

View File

@@ -16,9 +16,9 @@ namespace PortabilityLayer
QDManagerImpl();
void Init() override;
void GetPort(QDPort **port, GDevice ***gdHandle) override;
void SetPort(QDPort *gw, GDevice **gdHandle) override;
int NewGWorld(CGraf **gw, int depth, const Rect &bounds, ColorTable **colorTable, GDevice **device, int flags) override;
QDPort *GetPort() const override;
void SetPort(QDPort *gw) override;
int NewGWorld(CGraf **gw, int depth, const Rect &bounds, ColorTable **colorTable, int flags) override;
void DisposeGWorld(CGraf *gw) override;
QDState *GetState() override;
@@ -28,14 +28,12 @@ namespace PortabilityLayer
private:
QDPort *m_port;
GDHandle m_gdHandle;
static QDManagerImpl ms_instance;
};
QDManagerImpl::QDManagerImpl()
: m_port(nullptr)
, m_gdHandle(nullptr)
{
}
@@ -43,21 +41,17 @@ namespace PortabilityLayer
{
}
void QDManagerImpl::GetPort(QDPort **port, GDevice ***gdHandle)
QDPort *QDManagerImpl::GetPort() const
{
if (port)
*port = m_port;
if (gdHandle)
*gdHandle = m_gdHandle;
return m_port;
}
void QDManagerImpl::SetPort(QDPort *gw, GDevice **gdHandle)
void QDManagerImpl::SetPort(QDPort *gw)
{
m_port = gw;
m_gdHandle = gdHandle;
}
int QDManagerImpl::NewGWorld(CGraf **gw, int depth, const Rect &bounds, ColorTable **colorTable, GDevice **device, int flags)
int QDManagerImpl::NewGWorld(CGraf **gw, int depth, const Rect &bounds, ColorTable **colorTable, int flags)
{
GpPixelFormat_t pixelFormat;

View File

@@ -4,7 +4,6 @@
struct ColorTable;
struct CGraf;
struct GDevice;
struct Rect;
namespace PortabilityLayer
@@ -16,9 +15,9 @@ namespace PortabilityLayer
{
public:
virtual void Init() = 0;
virtual void GetPort(QDPort **gw, GDevice ***gdHandle) = 0;
virtual void SetPort(QDPort *gw, GDevice **gdHandle) = 0;
virtual int NewGWorld(CGraf **gw, int depth, const Rect &bounds, ColorTable **colorTable, GDevice **device, int flags) = 0;
virtual QDPort *GetPort() const = 0;
virtual void SetPort(QDPort *gw) = 0;
virtual int NewGWorld(CGraf **gw, int depth, const Rect &bounds, ColorTable **colorTable, int flags) = 0;
virtual void DisposeGWorld(CGraf *gw) = 0;
virtual QDState *GetState() = 0;

View File

@@ -18,6 +18,8 @@ namespace PortabilityLayer
, m_isForeResolved8(false)
, m_isBackResolved8(false)
, m_clipRegion(nullptr)
, m_penInvert(false)
, m_penMask(false)
{
m_backUnresolvedColor.r = m_backUnresolvedColor.g = m_backUnresolvedColor.b = m_backUnresolvedColor.a = 255;
m_foreUnresolvedColor.r = m_foreUnresolvedColor.g = m_foreUnresolvedColor.b = 0;

View File

@@ -16,6 +16,8 @@ namespace PortabilityLayer
int m_textSize;
Region **m_clipRegion;
Point m_penPos;
bool m_penInvert;
bool m_penMask;
void SetForeColor(const RGBAColor &color);
void SetBackColor(const RGBAColor &color);

View File

@@ -6,6 +6,19 @@ namespace PortabilityLayer
{
struct RGBAColor
{
uint8_t r, g, b, a;
};
uint8_t r, g, b, a;
static RGBAColor Create(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
};
inline RGBAColor RGBAColor::Create(uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
RGBAColor color;
color.r = r;
color.g = g;
color.b = b;
color.a = a;
return color;
}
}

View File

@@ -143,12 +143,12 @@ namespace PortabilityLayer
m_bottomRight.m_x = i;
}
bool Rect2i::IsValid() const
inline bool Rect2i::IsValid() const
{
return m_bottomRight.m_x >= m_topLeft.m_x && m_bottomRight.m_y >= m_topLeft.m_y;
}
Rect2i Rect2i::Intersect(const Rect2i &other) const
inline Rect2i Rect2i::Intersect(const Rect2i &other) const
{
const int32_t top = std::max(m_topLeft.m_y, other.m_topLeft.m_y);
const int32_t left = std::max(m_topLeft.m_x, other.m_topLeft.m_x);
@@ -158,7 +158,7 @@ namespace PortabilityLayer
return Rect2i(top, left, bottom, right);
}
Rect Rect2i::ToShortRect() const
inline Rect Rect2i::ToShortRect() const
{
return Rect::Create(static_cast<int16_t>(m_topLeft.m_y), static_cast<int16_t>(m_topLeft.m_x), static_cast<int16_t>(m_bottomRight.m_y), static_cast<int16_t>(m_bottomRight.m_x));
}

View File

@@ -26,7 +26,7 @@ namespace PortabilityLayer
WindowImpl();
~WindowImpl();
bool Init(const WindowDef &windowDef, GDevice **device);
bool Init(const WindowDef &windowDef);
bool Resize(int width, int height);
WindowImpl *GetWindowAbove() const;
@@ -38,12 +38,9 @@ namespace PortabilityLayer
bool IsVisible() const;
void SetVisible(bool visible);
GDevice **GetDevice() const;
private:
WindowImpl *m_windowAbove;
WindowImpl *m_windowBelow;
GDevice **m_device;
bool m_visible;
};
@@ -59,7 +56,6 @@ namespace PortabilityLayer
void PutWindowBehind(Window *window, Window *otherWindow) override;
void ShowWindow(Window *window) override;
void HideWindow(Window *window) override;
GDevice **GetWindowDevice(Window *window) override;
void FindWindow(const Point &point, Window **outWindow, short *outRegion) const override;
void RenderFrame(IGpDisplayDriver *displayDriver) override;
@@ -82,7 +78,6 @@ namespace PortabilityLayer
WindowImpl::WindowImpl()
: m_windowAbove(nullptr)
, m_windowBelow(nullptr)
, m_device(nullptr)
, m_visible(true)
{
}
@@ -92,7 +87,7 @@ namespace PortabilityLayer
PL_NotYetImplemented();
}
bool WindowImpl::Init(const WindowDef &windowDef, GDevice **device)
bool WindowImpl::Init(const WindowDef &windowDef)
{
const Rect bounds = windowDef.m_initialRect;
@@ -101,10 +96,10 @@ namespace PortabilityLayer
const Rect adjustedBounds = Rect::Create(0, 0, bounds.bottom - bounds.top, bounds.right - bounds.left);
if (int errorCode = m_graf.Init(adjustedBounds, (*device)->pixelFormat))
return false;
GpPixelFormat_t pixelFormat = PortabilityLayer::DisplayDeviceManager::GetInstance()->GetPixelFormat();
m_device = device;
if (int errorCode = m_graf.Init(adjustedBounds, pixelFormat))
return false;
return true;
}
@@ -148,11 +143,6 @@ namespace PortabilityLayer
m_visible = visible;
}
GDevice **WindowImpl::GetDevice() const
{
return m_device;
}
WindowManagerImpl::WindowManagerImpl()
: m_windowStackTop(nullptr)
, m_windowStackBottom(nullptr)
@@ -165,14 +155,12 @@ namespace PortabilityLayer
if (!windowMem)
return nullptr;
GDevice **device = DisplayDeviceManager::GetInstance()->GetMainDevice();
Rect portRect = windowDef.m_initialRect;
if (!portRect.IsValid())
return nullptr;
WindowImpl *window = new (windowMem) WindowImpl();
if (!window->Init(windowDef, device))
if (!window->Init(windowDef))
{
window->~WindowImpl();
MemoryManager::GetInstance()->Release(windowMem);
@@ -243,11 +231,6 @@ namespace PortabilityLayer
}
}
GDevice **WindowManagerImpl::GetWindowDevice(Window *window)
{
return static_cast<WindowImpl*>(window)->GetDevice();
}
void WindowManagerImpl::FindWindow(const Point &point, Window **outWindow, short *outRegion) const
{
// outRegion = One of:
@@ -304,24 +287,15 @@ namespace PortabilityLayer
void WindowManagerImpl::RenderFrame(IGpDisplayDriver *displayDriver)
{
GDevice **mainDeviceHdl = PortabilityLayer::DisplayDeviceManager::GetInstance()->GetMainDevice();
PortabilityLayer::DisplayDeviceManager *dd = PortabilityLayer::DisplayDeviceManager::GetInstance();
if (mainDeviceHdl)
dd->SyncPalette(displayDriver);
WindowImpl *window = m_windowStackBottom;
while (window)
{
GDevice *mainDevice = *mainDeviceHdl;
if (mainDevice->paletteIsDirty)
{
displayDriver->UpdatePalette(mainDevice->paletteStorage + mainDevice->paletteDataOffset);
mainDevice->paletteIsDirty = false;
}
WindowImpl *window = m_windowStackBottom;
while (window)
{
RenderWindow(window, displayDriver);
window = window->GetWindowAbove();
}
RenderWindow(window, displayDriver);
window = window->GetWindowAbove();
}
}

View File

@@ -21,7 +21,6 @@ namespace PortabilityLayer
virtual void PutWindowBehind(Window *window, Window *otherWindow) = 0;
virtual void ShowWindow(Window *window) = 0;
virtual void HideWindow(Window *window) = 0;
virtual GDevice **GetWindowDevice(Window *window) = 0;
virtual void FindWindow(const Point &point, Window **outWindow, short *outRegion) const = 0;
virtual void RenderFrame(IGpDisplayDriver *displayDriver) = 0;