Skip to content

Commit 78ab4df

Browse files
committed
preliminary work for unified wrapping code
1 parent 738cb03 commit 78ab4df

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

Source/Renderer/GraphicalPrimitive.cpp

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,48 @@ const GraphicalPrimitive::PrimitiveType TextPrimitive::c_PrimitiveType = Primiti
3030
const GraphicalPrimitive::PrimitiveType BitmapPrimitive::c_PrimitiveType = PrimitiveType::Bitmap;
3131

3232
Vector GraphicalPrimitive::WrapCoordinates(Vector targetPos, const Vector& scenePos) const {
33-
Vector drawPos = scenePos;
33+
return targetPos + scenePos;
34+
}
35+
36+
void GraphicalPrimitive::DrawTiled(BITMAP* drawScreen, const Vector& targetPos) {
37+
DrawLineV(-targetPos + Vector(-30, -30), -targetPos + Vector(30, 30), {53, 0, 0, 255});
38+
DrawLineV(-targetPos + Vector(30, -30), -targetPos + Vector(-30, 30), {53, 0, 0, 255});
3439

40+
Vector tiledTarget{targetPos};
3541
if (g_SceneMan.SceneWrapsX()) {
36-
float sceneWidth = static_cast<float>(g_SceneMan.GetSceneWidth());
37-
if (targetPos.m_X <= sceneWidth && targetPos.m_X > sceneWidth / 2) {
38-
targetPos.m_X -= sceneWidth;
39-
}
42+
tiledTarget.m_X = std::fmodf(targetPos.m_X, g_SceneMan.GetSceneWidth());
4043
}
41-
drawPos.m_X -= targetPos.m_X;
42-
4344
if (g_SceneMan.SceneWrapsY()) {
44-
float sceneHeight = static_cast<float>(g_SceneMan.GetSceneHeight());
45-
if (targetPos.m_Y <= sceneHeight && targetPos.m_Y > sceneHeight / 2) {
46-
targetPos.m_Y -= sceneHeight;
45+
tiledTarget.m_Y = std::fmodf(targetPos.m_Y, g_SceneMan.GetSceneHeight());
46+
}
47+
DrawLineV(-tiledTarget + Vector(-30, -30), -tiledTarget + Vector(30, 30), {53, 0, 0, 255});
48+
DrawLineV(-tiledTarget + Vector(30, -30), -tiledTarget + Vector(-30, 30), {53, 0, 0, 255});
49+
50+
float bitmapWidth = g_SceneMan.GetSceneWidth();
51+
float bitmapHeight = g_SceneMan.GetSceneHeight();
52+
int areaToCoverX = drawScreen->w;
53+
int areaToCoverY = drawScreen->h;
54+
55+
for (int tiledOffsetX = 0; tiledOffsetX < areaToCoverX;) {
56+
float destX = tiledOffsetX - tiledTarget.m_X;
57+
58+
for (int tiledOffsetY = 0; tiledOffsetY < areaToCoverY;) {
59+
float destY = tiledOffsetY - tiledTarget.m_Y;
60+
Draw(drawScreen, Vector(destX, destY));
61+
if (!g_SceneMan.SceneWrapsY()) {
62+
break;
63+
}
64+
tiledOffsetY += bitmapHeight;
65+
}
66+
if (!g_SceneMan.SceneWrapsX()) {
67+
break;
4768
}
69+
tiledOffsetX += bitmapWidth;
4870
}
49-
drawPos.m_Y -= targetPos.m_Y;
50-
return drawPos;
71+
// Draw(drawScreen, targetPos);
5172
}
5273

5374
void LinePrimitive::Draw(BITMAP* drawScreen, const Vector& targetPos) {
54-
DrawLineV(targetPos + Vector(-30, -30), targetPos + Vector(30,30), {53, 0, 0, 255});
55-
DrawLineV(targetPos + Vector(30, -30), targetPos + Vector(-30,30), {53, 0, 0, 255});
5675
Vector drawStart = WrapCoordinates(targetPos, m_StartPos);
5776
Vector drawEnd = WrapCoordinates(targetPos, m_EndPos);
5877
DrawLineEx(drawStart, drawEnd, m_Thickness, {m_Color, 0, 0, 255});

Source/Renderer/GraphicalPrimitive.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <array>
66
#include <string>
77
#include <vector>
8+
#include <algorithm>
89

910
namespace RTE {
1011

@@ -43,6 +44,7 @@ namespace RTE {
4344

4445
Vector m_StartPos; //!< Start position of the primitive.
4546
Vector m_EndPos; //!< End position of the primitive.
47+
float m_DrawRadiusSquared{0.0f};
4648
unsigned char m_Color = 0; //!< Color to draw this primitive with.
4749
int m_Player = -1; //!< Player screen to draw this primitive on.
4850
DrawBlendMode m_BlendMode = DrawBlendMode::NoBlend; //!< The blending mode that will be used when drawing this primitive.
@@ -56,6 +58,8 @@ namespace RTE {
5658
/// @param scenePos Position on scene.
5759
Vector WrapCoordinates(Vector targetPos, const Vector& scenePos) const;
5860

61+
void DrawTiled(BITMAP* drawScreen, const Vector& targetPos);
62+
5963
/// Draws this primitive on provided bitmap.
6064
/// @param drawScreen Bitmap to draw on.
6165
/// @param targetPos Position of graphical primitive.
@@ -87,6 +91,7 @@ namespace RTE {
8791
LinePrimitive(int player, const Vector& startPos, const Vector& endPos, unsigned char color) {
8892
m_StartPos = startPos;
8993
m_EndPos = endPos;
94+
m_DrawRadiusSquared = std::abs((m_StartPos - m_EndPos).GetSqrMagnitude());
9095
m_Color = color;
9196
m_Player = player;
9297
m_Thickness = 1;
@@ -100,6 +105,7 @@ namespace RTE {
100105
LinePrimitive(int player, const Vector& startPos, const Vector& endPos, float thickness, unsigned char color) {
101106
m_StartPos = startPos;
102107
m_EndPos = endPos;
108+
m_DrawRadiusSquared = std::abs((m_StartPos - m_EndPos).GetSqrMagnitude());
103109
m_Color = color;
104110
m_Player = player;
105111
m_Thickness = thickness;
@@ -135,6 +141,7 @@ namespace RTE {
135141
m_StartPos = centerPos;
136142
m_Color = color;
137143
m_Player = player;
144+
m_DrawRadiusSquared = (m_Radius + m_Thickness) * (m_Radius + m_Thickness);
138145
}
139146

140147
private:
@@ -166,6 +173,7 @@ namespace RTE {
166173
m_EndPos = endPos;
167174
m_Color = color;
168175
m_Player = player;
176+
m_DrawRadiusSquared = std::max<float>({(startPos - guideA).GetSqrMagnitude(), (startPos - guideB).GetSqrMagnitude(), (startPos - endPos).GetSqrMagnitude()});
169177
}
170178

171179
private:
@@ -190,6 +198,7 @@ namespace RTE {
190198
m_EndPos = bottomRightPos;
191199
m_Color = color;
192200
m_Player = player;
201+
m_DrawRadiusSquared = (m_EndPos - m_StartPos).GetSqrMagnitude();
193202
}
194203

195204
private:
@@ -214,6 +223,7 @@ namespace RTE {
214223
m_EndPos = bottomRightPos;
215224
m_Color = color;
216225
m_Player = player;
226+
m_DrawRadiusSquared = (m_StartPos - m_EndPos).GetSqrMagnitude();
217227
}
218228

219229
private:
@@ -243,6 +253,7 @@ namespace RTE {
243253
m_EndPos = bottomRightPos;
244254
m_Color = color;
245255
m_Player = player;
256+
m_DrawRadiusSquared = (m_EndPos - m_StartPos).GetSqrMagnitude();
246257
}
247258

248259
private:

0 commit comments

Comments
 (0)