2019-10-23 20:51:11 +00:00
|
|
|
#ifndef __2DDRAWER_H
|
|
|
|
#define __2DDRAWER_H
|
|
|
|
|
2021-01-29 12:20:00 +00:00
|
|
|
#include "buffers.h"
|
2019-10-23 20:51:11 +00:00
|
|
|
#include "tarray.h"
|
|
|
|
#include "vectors.h"
|
|
|
|
#include "textures.h"
|
|
|
|
#include "renderstyle.h"
|
2020-05-25 15:11:32 +00:00
|
|
|
#include "dobject.h"
|
2021-08-14 08:04:45 +00:00
|
|
|
#include "refcounted.h"
|
2019-10-23 20:51:11 +00:00
|
|
|
|
|
|
|
struct DrawParms;
|
2020-05-25 15:11:32 +00:00
|
|
|
struct FColormap;
|
2022-08-04 21:47:01 +00:00
|
|
|
struct IntRect;
|
2019-10-23 20:51:11 +00:00
|
|
|
|
2020-05-25 15:11:32 +00:00
|
|
|
class DShape2DTransform : public DObject
|
|
|
|
{
|
|
|
|
|
|
|
|
DECLARE_CLASS(DShape2DTransform, DObject)
|
|
|
|
public:
|
|
|
|
DShape2DTransform()
|
|
|
|
{
|
|
|
|
transform.Identity();
|
|
|
|
}
|
|
|
|
DMatrix3x3 transform;
|
|
|
|
};
|
|
|
|
|
|
|
|
// intermediate struct for shape drawing
|
|
|
|
|
|
|
|
enum EClearWhich
|
|
|
|
{
|
|
|
|
C_Verts = 1,
|
|
|
|
C_Coords = 2,
|
|
|
|
C_Indices = 4,
|
|
|
|
};
|
|
|
|
|
2021-01-29 12:20:00 +00:00
|
|
|
class F2DVertexBuffer;
|
|
|
|
|
2019-12-31 14:04:28 +00:00
|
|
|
struct F2DPolygons
|
|
|
|
{
|
|
|
|
TArray<FVector4> vertices;
|
|
|
|
TArray<int> indices;
|
|
|
|
|
2020-01-18 21:41:08 +00:00
|
|
|
unsigned AllocVertices(int num)
|
2019-12-31 14:04:28 +00:00
|
|
|
{
|
|
|
|
auto vindex = vertices.Reserve(num);
|
|
|
|
indices.Push(num);
|
|
|
|
return vindex;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2021-02-12 13:44:54 +00:00
|
|
|
class DShape2D;
|
2021-08-14 08:04:45 +00:00
|
|
|
struct DShape2DBufferInfo;
|
2021-01-29 12:20:00 +00:00
|
|
|
|
2022-07-23 10:05:27 +00:00
|
|
|
enum class SpecialDrawCommand {
|
|
|
|
NotSpecial,
|
|
|
|
|
|
|
|
EnableStencil,
|
|
|
|
SetStencil,
|
|
|
|
ClearStencil,
|
|
|
|
};
|
|
|
|
|
2019-10-23 20:51:11 +00:00
|
|
|
class F2DDrawer
|
|
|
|
{
|
|
|
|
public:
|
2022-07-23 10:05:27 +00:00
|
|
|
F2DDrawer() {
|
|
|
|
this->transform.Identity();
|
|
|
|
}
|
2019-10-23 20:51:11 +00:00
|
|
|
|
|
|
|
enum EDrawType : uint8_t
|
|
|
|
{
|
|
|
|
DrawTypeTriangles,
|
|
|
|
DrawTypeLines,
|
|
|
|
DrawTypePoints,
|
2019-11-21 18:30:27 +00:00
|
|
|
DrawTypeRotateSprite,
|
2019-10-23 20:51:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum ETextureFlags : uint8_t
|
|
|
|
{
|
|
|
|
DTF_Wrap = 1,
|
|
|
|
DTF_Scissor = 2,
|
|
|
|
DTF_Burn = 4,
|
2020-09-13 21:14:44 +00:00
|
|
|
DTF_Indexed = 8,
|
2019-10-23 20:51:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// This vertex type is hardware independent and needs conversion when put into a buffer.
|
|
|
|
struct TwoDVertex
|
|
|
|
{
|
|
|
|
float x, y, z;
|
|
|
|
float u, v;
|
|
|
|
PalEntry color0;
|
|
|
|
|
|
|
|
void Set(float xx, float yy, float zz)
|
|
|
|
{
|
|
|
|
x = xx;
|
|
|
|
z = zz;
|
|
|
|
y = yy;
|
|
|
|
u = 0;
|
|
|
|
v = 0;
|
|
|
|
color0 = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Set(double xx, double yy, double zz, double uu, double vv, PalEntry col)
|
|
|
|
{
|
|
|
|
x = (float)xx;
|
|
|
|
z = (float)zz;
|
|
|
|
y = (float)yy;
|
|
|
|
u = (float)uu;
|
|
|
|
v = (float)vv;
|
|
|
|
color0 = col;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
2021-12-30 09:30:21 +00:00
|
|
|
|
2019-10-23 20:51:11 +00:00
|
|
|
struct RenderCommand
|
|
|
|
{
|
2022-07-23 10:05:27 +00:00
|
|
|
SpecialDrawCommand isSpecial;
|
|
|
|
|
|
|
|
bool stencilOn;
|
|
|
|
|
|
|
|
int stencilOffs;
|
|
|
|
int stencilOp;
|
|
|
|
int stencilFlags;
|
|
|
|
|
2019-10-23 20:51:11 +00:00
|
|
|
EDrawType mType;
|
|
|
|
int mVertIndex;
|
|
|
|
int mVertCount;
|
|
|
|
int mIndexIndex;
|
|
|
|
int mIndexCount;
|
|
|
|
|
2020-05-25 21:59:07 +00:00
|
|
|
FGameTexture *mTexture;
|
2020-05-25 15:11:32 +00:00
|
|
|
int mTranslationId;
|
2019-10-23 20:51:11 +00:00
|
|
|
PalEntry mSpecialColormap[2];
|
|
|
|
int mScissor[4];
|
|
|
|
int mDesaturate;
|
2020-06-07 13:02:54 +00:00
|
|
|
FRenderStyle mRenderStyle;
|
2019-10-23 20:51:11 +00:00
|
|
|
PalEntry mColor1; // Overlay color
|
|
|
|
ETexMode mDrawMode;
|
2020-06-07 12:50:12 +00:00
|
|
|
uint8_t mLightLevel;
|
2019-10-23 20:51:11 +00:00
|
|
|
uint8_t mFlags;
|
2021-04-16 19:27:54 +00:00
|
|
|
float mScreenFade;
|
2019-10-23 20:51:11 +00:00
|
|
|
|
2021-01-29 12:20:00 +00:00
|
|
|
bool useTransform;
|
|
|
|
DMatrix3x3 transform;
|
|
|
|
|
2021-08-14 08:04:45 +00:00
|
|
|
RefCountedPtr<DShape2DBufferInfo> shape2DBufInfo;
|
2021-01-29 12:20:00 +00:00
|
|
|
int shape2DBufIndex;
|
|
|
|
int shape2DIndexCount;
|
2021-02-26 18:06:10 +00:00
|
|
|
int shape2DCommandCounter;
|
2021-01-29 12:20:00 +00:00
|
|
|
|
2019-10-23 20:51:11 +00:00
|
|
|
RenderCommand()
|
|
|
|
{
|
2021-11-06 17:26:47 +00:00
|
|
|
memset((void*)this, 0, sizeof(*this));
|
2019-10-23 20:51:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If these fields match, two draw commands can be batched.
|
|
|
|
bool isCompatible(const RenderCommand &other) const
|
|
|
|
{
|
2022-07-23 10:05:27 +00:00
|
|
|
if (
|
|
|
|
isSpecial != SpecialDrawCommand::NotSpecial ||
|
|
|
|
other.isSpecial != SpecialDrawCommand::NotSpecial
|
|
|
|
) return false;
|
2021-08-14 08:04:45 +00:00
|
|
|
if (shape2DBufInfo != nullptr || other.shape2DBufInfo != nullptr) return false;
|
2019-10-23 20:51:11 +00:00
|
|
|
return mTexture == other.mTexture &&
|
2019-12-31 14:04:28 +00:00
|
|
|
mType == other.mType &&
|
2020-05-25 15:11:32 +00:00
|
|
|
mTranslationId == other.mTranslationId &&
|
2019-10-23 20:51:11 +00:00
|
|
|
mSpecialColormap[0].d == other.mSpecialColormap[0].d &&
|
|
|
|
mSpecialColormap[1].d == other.mSpecialColormap[1].d &&
|
|
|
|
!memcmp(mScissor, other.mScissor, sizeof(mScissor)) &&
|
|
|
|
mDesaturate == other.mDesaturate &&
|
|
|
|
mRenderStyle == other.mRenderStyle &&
|
|
|
|
mDrawMode == other.mDrawMode &&
|
|
|
|
mFlags == other.mFlags &&
|
2020-06-07 12:50:12 +00:00
|
|
|
mLightLevel == other.mLightLevel &&
|
2021-01-29 12:20:00 +00:00
|
|
|
mColor1.d == other.mColor1.d &&
|
|
|
|
useTransform == other.useTransform &&
|
2021-04-16 19:27:54 +00:00
|
|
|
mScreenFade == other.mScreenFade &&
|
2021-01-29 12:20:00 +00:00
|
|
|
(
|
|
|
|
!useTransform ||
|
|
|
|
(
|
|
|
|
transform[0] == other.transform[0] &&
|
|
|
|
transform[1] == other.transform[1] &&
|
|
|
|
transform[2] == other.transform[2]
|
|
|
|
)
|
|
|
|
);
|
2019-10-23 20:51:11 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TArray<int> mIndices;
|
|
|
|
TArray<TwoDVertex> mVertices;
|
|
|
|
TArray<RenderCommand> mData;
|
2020-05-25 15:11:32 +00:00
|
|
|
int Width, Height;
|
|
|
|
bool isIn2D;
|
2022-12-05 19:04:31 +00:00
|
|
|
bool locked = false; // prevents clearing of the data so it can be reused multiple times (useful for screen fades)
|
2020-07-01 10:55:32 +00:00
|
|
|
float screenFade = 1.f;
|
2020-10-04 23:42:47 +00:00
|
|
|
DVector2 offset;
|
2022-07-23 10:05:27 +00:00
|
|
|
DMatrix3x3 transform;
|
2020-05-25 15:11:32 +00:00
|
|
|
public:
|
2020-10-24 15:30:47 +00:00
|
|
|
int fullscreenautoaspect = 3;
|
2020-05-25 15:11:32 +00:00
|
|
|
int cliptop = -1, clipleft = -1, clipwidth = -1, clipheight = -1;
|
2021-12-30 09:30:21 +00:00
|
|
|
|
2021-04-16 19:27:54 +00:00
|
|
|
int AddCommand(RenderCommand *data);
|
2019-10-23 20:51:11 +00:00
|
|
|
void AddIndices(int firstvert, int count, ...);
|
2020-05-25 15:11:32 +00:00
|
|
|
private:
|
2019-12-31 14:04:28 +00:00
|
|
|
void AddIndices(int firstvert, TArray<int> &v);
|
2020-05-25 21:59:07 +00:00
|
|
|
bool SetStyle(FGameTexture *tex, DrawParms &parms, PalEntry &color0, RenderCommand &quad);
|
2019-10-23 20:51:11 +00:00
|
|
|
void SetColorOverlay(PalEntry color, float alpha, PalEntry &vertexcolor, PalEntry &overlaycolor);
|
|
|
|
|
|
|
|
public:
|
2020-05-26 21:12:04 +00:00
|
|
|
float GetClassicFlatScalarWidth();
|
|
|
|
float GetClassicFlatScalarHeight();
|
2020-05-25 21:59:07 +00:00
|
|
|
void AddTexture(FGameTexture* img, DrawParms& parms);
|
|
|
|
void AddShape(FGameTexture *img, DShape2D *shape, DrawParms &parms);
|
|
|
|
void AddPoly(FGameTexture *texture, FVector2 *points, int npoints,
|
2020-05-25 15:11:32 +00:00
|
|
|
double originx, double originy, double scalex, double scaley,
|
|
|
|
DAngle rotation, const FColormap &colormap, PalEntry flatcolor, double lightlevel, uint32_t *indices, size_t indexcount);
|
2022-08-04 21:47:01 +00:00
|
|
|
void AddPoly(FGameTexture* img, FVector4 *vt, size_t vtcount, const unsigned int *ind, size_t idxcount, int translation, PalEntry color, FRenderStyle style, const IntRect* clip);
|
2023-05-20 10:40:34 +00:00
|
|
|
void FillPolygon(int* rx1, int* ry1, int* xb1, int32_t npoints, int pic, int palette, int shade, int props, const FVector2& xtex, const FVector2& ytex, const FVector2& otex,
|
2019-12-31 18:50:27 +00:00
|
|
|
int clipx1, int clipy1, int clipx2, int clipy2);
|
2020-10-25 14:06:06 +00:00
|
|
|
void AddFlatFill(int left, int top, int right, int bottom, FGameTexture *src, int local_origin = false, double flatscale = 1.0, PalEntry color = 0xffffffff, ERenderStyle rs = STYLE_Normal);
|
2019-10-23 20:51:11 +00:00
|
|
|
|
2020-05-26 21:12:04 +00:00
|
|
|
void AddColorOnlyQuad(int left, int top, int width, int height, PalEntry color, FRenderStyle *style = nullptr, bool prepend = false);
|
2020-01-05 09:48:44 +00:00
|
|
|
void ClearScreen(PalEntry color = 0xff000000);
|
2020-05-25 15:11:32 +00:00
|
|
|
void AddDim(PalEntry color, float damount, int x1, int y1, int w, int h);
|
|
|
|
void AddClear(int left, int top, int right, int bottom, int palcolor, uint32_t color);
|
2021-12-30 09:30:21 +00:00
|
|
|
|
|
|
|
|
2022-09-02 08:39:01 +00:00
|
|
|
void AddLine(const DVector2& v1, const DVector2& v2, const IntRect* clip, uint32_t color, uint8_t alpha = 255);
|
|
|
|
void AddThickLine(const DVector2& v1, const DVector2& v2, double thickness, uint32_t color, uint8_t alpha = 255);
|
2019-10-23 20:51:11 +00:00
|
|
|
void AddPixel(int x1, int y1, uint32_t color);
|
|
|
|
|
2022-07-23 10:05:27 +00:00
|
|
|
void AddEnableStencil(bool on);
|
|
|
|
void AddSetStencil(int offs, int op, int flags);
|
|
|
|
void AddClearStencil();
|
|
|
|
|
2019-10-23 20:51:11 +00:00
|
|
|
void Clear();
|
2020-07-01 10:55:32 +00:00
|
|
|
void Lock() { locked = true; }
|
|
|
|
void SetScreenFade(float factor) { screenFade = factor; }
|
|
|
|
void Unlock() { locked = false; }
|
2020-05-25 15:11:32 +00:00
|
|
|
int GetWidth() const { return Width; }
|
|
|
|
int GetHeight() const { return Height; }
|
|
|
|
void SetSize(int w, int h) { Width = w; Height = h; }
|
2020-05-26 21:12:04 +00:00
|
|
|
void Begin(int w, int h) { isIn2D = true; Width = w; Height = h; }
|
2020-05-25 15:11:32 +00:00
|
|
|
void End() { isIn2D = false; }
|
|
|
|
bool HasBegun2D() { return isIn2D; }
|
2021-08-14 08:04:45 +00:00
|
|
|
void OnFrameDone();
|
2019-10-23 20:51:11 +00:00
|
|
|
|
2020-05-25 15:11:32 +00:00
|
|
|
void ClearClipRect() { clipleft = cliptop = 0; clipwidth = clipheight = -1; }
|
|
|
|
void SetClipRect(int x, int y, int w, int h);
|
|
|
|
void GetClipRect(int* x, int* y, int* w, int* h);
|
2019-12-30 18:29:32 +00:00
|
|
|
|
2020-10-04 23:42:47 +00:00
|
|
|
DVector2 SetOffset(const DVector2& vec)
|
|
|
|
{
|
|
|
|
auto v = offset;
|
|
|
|
offset = vec;
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2022-07-23 10:05:27 +00:00
|
|
|
void SetTransform(const DShape2DTransform& transform)
|
2020-10-04 23:42:47 +00:00
|
|
|
{
|
2022-07-23 10:05:27 +00:00
|
|
|
this->transform = transform.transform;
|
|
|
|
}
|
|
|
|
void ClearTransform()
|
|
|
|
{
|
|
|
|
this->transform.Identity();
|
2020-10-04 23:42:47 +00:00
|
|
|
}
|
|
|
|
|
2020-09-05 13:59:32 +00:00
|
|
|
int DrawCount() const
|
|
|
|
{
|
|
|
|
return mData.Size();
|
|
|
|
}
|
|
|
|
|
2020-05-25 15:11:32 +00:00
|
|
|
bool mIsFirstPass = true;
|
2019-12-30 18:29:32 +00:00
|
|
|
};
|
2019-10-23 20:51:11 +00:00
|
|
|
|
2022-08-03 11:27:48 +00:00
|
|
|
// DCanvas is already taken so using FCanvas instead.
|
|
|
|
class FCanvas : public DObject
|
|
|
|
{
|
|
|
|
DECLARE_CLASS(FCanvas, DObject)
|
|
|
|
public:
|
|
|
|
F2DDrawer Drawer;
|
|
|
|
FCanvasTexture* Tex = nullptr;
|
|
|
|
};
|
|
|
|
|
2021-10-07 20:41:14 +00:00
|
|
|
struct DShape2DBufferInfo : RefCountedBase
|
2021-08-14 08:04:45 +00:00
|
|
|
{
|
|
|
|
TArray<F2DVertexBuffer> buffers;
|
|
|
|
bool needsVertexUpload = true;
|
|
|
|
int bufIndex = -1;
|
|
|
|
int lastCommand = -1;
|
|
|
|
bool uploadedOnce = false;
|
|
|
|
};
|
|
|
|
|
2021-02-12 13:44:54 +00:00
|
|
|
class DShape2D : public DObject
|
|
|
|
{
|
|
|
|
|
|
|
|
DECLARE_CLASS(DShape2D,DObject)
|
|
|
|
public:
|
|
|
|
DShape2D()
|
2021-08-14 08:04:45 +00:00
|
|
|
: bufferInfo(new DShape2DBufferInfo)
|
2021-02-12 13:44:54 +00:00
|
|
|
{
|
|
|
|
transform.Identity();
|
|
|
|
}
|
|
|
|
|
|
|
|
TArray<int> mIndices;
|
|
|
|
TArray<DVector2> mVertices;
|
|
|
|
TArray<DVector2> mCoords;
|
|
|
|
|
|
|
|
double minx = 0.0;
|
|
|
|
double maxx = 0.0;
|
|
|
|
double miny = 0.0;
|
|
|
|
double maxy = 0.0;
|
|
|
|
|
|
|
|
DMatrix3x3 transform;
|
|
|
|
|
2021-08-14 08:04:45 +00:00
|
|
|
RefCountedPtr<DShape2DBufferInfo> bufferInfo;
|
2021-02-12 13:44:54 +00:00
|
|
|
|
|
|
|
DrawParms* lastParms;
|
|
|
|
|
2021-07-11 07:50:36 +00:00
|
|
|
void OnDestroy() override;
|
2021-02-12 13:44:54 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2021-01-29 12:20:00 +00:00
|
|
|
//===========================================================================
|
|
|
|
//
|
|
|
|
// Vertex buffer for 2D drawer
|
|
|
|
//
|
|
|
|
//===========================================================================
|
|
|
|
|
|
|
|
class F2DVertexBuffer
|
|
|
|
{
|
|
|
|
IVertexBuffer *mVertexBuffer;
|
|
|
|
IIndexBuffer *mIndexBuffer;
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
F2DVertexBuffer();
|
|
|
|
|
|
|
|
~F2DVertexBuffer()
|
|
|
|
{
|
|
|
|
delete mIndexBuffer;
|
|
|
|
delete mVertexBuffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void UploadData(F2DDrawer::TwoDVertex *vertices, int vertcount, int *indices, int indexcount)
|
|
|
|
{
|
2021-10-30 07:34:38 +00:00
|
|
|
mVertexBuffer->SetData(vertcount * sizeof(*vertices), vertices, BufferUsageType::Stream);
|
|
|
|
mIndexBuffer->SetData(indexcount * sizeof(unsigned int), indices, BufferUsageType::Stream);
|
2021-01-29 12:20:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<IVertexBuffer *, IIndexBuffer *> GetBufferObjects() const
|
|
|
|
{
|
|
|
|
return std::make_pair(mVertexBuffer, mIndexBuffer);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-04-08 16:53:47 +00:00
|
|
|
|
2019-10-23 20:51:11 +00:00
|
|
|
#endif
|