Skip to content

Commit b3bd114

Browse files
committed
Merge branch 'develop' of https://github.com/OneLoneCoder/olcPixelGameEngine into develop
2 parents eaf3174 + f5b6017 commit b3bd114

File tree

1 file changed

+54
-16
lines changed

1 file changed

+54
-16
lines changed

olcPixelGameEngine.h

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@
361361
+SpritePatch
362362
+DecalPatch
363363
+Added the Colour "Orange" cos some internet rando made me laugh about it :D
364+
+Changed ClipLineToScreen to ClipLineToDrawTarget
364365
365366
366367
!! Apple Platforms will not see these updates immediately - Sorry, I dont have a mac to test... !!
@@ -1080,7 +1081,10 @@ namespace olc
10801081
Sprite(const std::string& sImageFile, olc::ResourcePack* pack = nullptr);
10811082
Sprite(int32_t w, int32_t h);
10821083
Sprite(const olc::Sprite&) = delete;
1084+
Sprite(olc::Sprite&&);
10831085
~Sprite();
1086+
Sprite& operator=(const olc::Sprite&) = delete;
1087+
Sprite& operator=(olc::Sprite&&);
10841088

10851089
public:
10861090
olc::rcode LoadFromFile(const std::string& sImageFile, olc::ResourcePack* pack = nullptr);
@@ -1142,6 +1146,8 @@ namespace olc
11421146

11431147
public: // But dont touch
11441148
int32_t id = -1;
1149+
int32_t width = 0;
1150+
int32_t height = 0;
11451151
olc::Sprite* sprite = nullptr;
11461152
olc::vf2d vUVScale = { 1.0f, 1.0f };
11471153
};
@@ -1513,7 +1519,7 @@ namespace olc
15131519
olc::Sprite* GetFontSprite();
15141520

15151521
// Clip a line segment to visible area
1516-
bool ClipLineToScreen(olc::vi2d& in_p1, olc::vi2d& in_p2);
1522+
bool ClipLineToDrawTarget(olc::vi2d& in_p1, olc::vi2d& in_p2);
15171523

15181524

15191525
// Patches
@@ -1993,6 +1999,32 @@ namespace olc
19931999
SetSize(w, h);
19942000
}
19952001

2002+
Sprite::Sprite(olc::Sprite&& spr)
2003+
{
2004+
width = spr.width;
2005+
spr.width = 0;
2006+
2007+
height = spr.height;
2008+
spr.height = 0;
2009+
2010+
pColData = std::move(spr.pColData);
2011+
2012+
modeSample = spr.modeSample;
2013+
}
2014+
2015+
Sprite& Sprite::operator=(olc::Sprite&& spr)
2016+
{
2017+
std::swap(width, spr.width);
2018+
2019+
std::swap(height, spr.height);
2020+
2021+
std::swap(pColData, spr.pColData);
2022+
2023+
std::swap(modeSample, spr.modeSample);
2024+
2025+
return *this;
2026+
}
2027+
19962028
void Sprite::SetSize(int32_t w, int32_t h)
19972029
{
19982030
width = w; height = h;
@@ -2156,6 +2188,8 @@ namespace olc
21562188
{
21572189
id = -1;
21582190
if (spr == nullptr) return;
2191+
width = spr->width;
2192+
height = spr->height;
21592193
sprite = spr;
21602194
id = renderer->CreateTexture(sprite->width, sprite->height, filter, clamp);
21612195
Update();
@@ -2170,14 +2204,17 @@ namespace olc
21702204
void Decal::Update()
21712205
{
21722206
if (sprite == nullptr) return;
2173-
vUVScale = { 1.0f / float(sprite->width), 1.0f / float(sprite->height) };
2207+
width = sprite->width;
2208+
height = sprite->height;
2209+
vUVScale = { 1.0f / float(width), 1.0f / float(height) };
21742210
renderer->ApplyTexture(id);
21752211
renderer->UpdateTexture(id, sprite);
21762212
}
21772213

21782214
void Decal::UpdateSprite()
21792215
{
21802216
if (sprite == nullptr) return;
2217+
sprite->SetSize(width, height);
21812218
renderer->ApplyTexture(id);
21822219
renderer->ReadTexture(id, sprite);
21832220
}
@@ -2207,7 +2244,6 @@ namespace olc
22072244
}
22082245
else
22092246
{
2210-
pSprite.release();
22112247
pSprite = nullptr;
22122248
return olc::rcode::NO_FILE;
22132249
}
@@ -2741,7 +2777,7 @@ namespace olc
27412777
auto rol = [&](void) { pattern = (pattern << 1) | (pattern >> 31); return pattern & 1; };
27422778

27432779
olc::vi2d p1(x1, y1), p2(x2, y2);
2744-
if (!ClipLineToScreen(p1, p2))
2780+
if (!ClipLineToDrawTarget(p1, p2))
27452781
return;
27462782
x1 = p1.x; y1 = p1.y;
27472783
x2 = p2.x; y2 = p2.y;
@@ -2933,15 +2969,17 @@ namespace olc
29332969
return fontRenderable.Sprite();
29342970
}
29352971

2936-
bool PixelGameEngine::ClipLineToScreen(olc::vi2d& in_p1, olc::vi2d& in_p2)
2972+
bool PixelGameEngine::ClipLineToDrawTarget(olc::vi2d& in_p1, olc::vi2d& in_p2)
29372973
{
2974+
olc::vi2d vDrawTargetSize{ (int32_t)GetDrawTargetWidth(), (int32_t)GetDrawTargetHeight() };
2975+
29382976
// https://en.wikipedia.org/wiki/Cohen%E2%80%93Sutherland_algorithm
29392977
static constexpr int SEG_I = 0b0000, SEG_L = 0b0001, SEG_R = 0b0010, SEG_B = 0b0100, SEG_T = 0b1000;
2940-
auto Segment = [&vScreenSize = vScreenSize](const olc::vi2d& v)
2978+
auto Segment = [&vDrawTargetSize = vDrawTargetSize](const olc::vi2d& v)
29412979
{
29422980
int i = SEG_I;
2943-
if (v.x < 0) i |= SEG_L; else if (v.x > vScreenSize.x) i |= SEG_R;
2944-
if (v.y < 0) i |= SEG_B; else if (v.y > vScreenSize.y) i |= SEG_T;
2981+
if (v.x < 0) i |= SEG_L; else if (v.x > vDrawTargetSize.x) i |= SEG_R;
2982+
if (v.y < 0) i |= SEG_B; else if (v.y > vDrawTargetSize.y) i |= SEG_T;
29452983
return i;
29462984
};
29472985

@@ -2955,9 +2993,9 @@ namespace olc
29552993
{
29562994
int s3 = s2 > s1 ? s2 : s1;
29572995
olc::vi2d n;
2958-
if (s3 & SEG_T) { n.x = in_p1.x + (in_p2.x - in_p1.x) * (vScreenSize.y - in_p1.y) / (in_p2.y - in_p1.y); n.y = vScreenSize.y; }
2996+
if (s3 & SEG_T) { n.x = in_p1.x + (in_p2.x - in_p1.x) * (vDrawTargetSize.y - in_p1.y) / (in_p2.y - in_p1.y); n.y = vDrawTargetSize.y; }
29592997
else if (s3 & SEG_B) { n.x = in_p1.x + (in_p2.x - in_p1.x) * (0 - in_p1.y) / (in_p2.y - in_p1.y); n.y = 0; }
2960-
else if (s3 & SEG_R) { n.x = vScreenSize.x; n.y = in_p1.y + (in_p2.y - in_p1.y) * (vScreenSize.x - in_p1.x) / (in_p2.x - in_p1.x); }
2998+
else if (s3 & SEG_R) { n.x = vDrawTargetSize.x; n.y = in_p1.y + (in_p2.y - in_p1.y) * (vDrawTargetSize.x - in_p1.x) / (in_p2.x - in_p1.x); }
29612999
else if (s3 & SEG_L) { n.x = 0; n.y = in_p1.y + (in_p2.y - in_p1.y) * (0 - in_p1.x) / (in_p2.x - in_p1.x); }
29623000
if (s3 == s1) { in_p1 = n; s1 = Segment(in_p1); }
29633001
else { in_p2 = n; s2 = Segment(in_p2); }
@@ -3519,8 +3557,8 @@ namespace olc
35193557

35203558
olc::vf2d vScreenSpaceDim =
35213559
{
3522-
vScreenSpacePos.x + (2.0f * (float(decal->sprite->width) * vInvScreenSize.x)) * scale.x,
3523-
vScreenSpacePos.y - (2.0f * (float(decal->sprite->height) * vInvScreenSize.y)) * scale.y
3560+
vScreenSpacePos.x + (2.0f * (float(decal->width) * vInvScreenSize.x)) * scale.x,
3561+
vScreenSpacePos.y - (2.0f * (float(decal->height) * vInvScreenSize.y)) * scale.y
35243562
};
35253563

35263564
DecalInstance di;
@@ -5614,8 +5652,8 @@ namespace olc
56145652
{
56155653
olc::Pixel p = task.vb[n].c;
56165654
glColor4ub(GLubyte(p.r * f[0]), GLubyte(p.g * f[1]), GLubyte(p.b * f[2]), GLubyte(p.a * f[3]));
5617-
glVertex4f(task.vb[n].p[4], task.vb[n].p[5], 0.0f, task.vb[n].p[3]);
5618-
glTexCoord2f(task.vb[n].p[0], task.vb[n].p[1]);
5655+
glTexCoord2f(task.vb[n].p[4], task.vb[n].p[5]);
5656+
glVertex4f(task.vb[n].p[0], task.vb[n].p[1], 0.0f, task.vb[n].p[3]);
56195657
}
56205658
}
56215659

@@ -5680,7 +5718,7 @@ namespace olc
56805718

56815719
void ReadTexture(uint32_t id, olc::Sprite* spr) override
56825720
{
5683-
glReadPixels(0, 0, spr->width, spr->height, GL_RGBA, GL_UNSIGNED_BYTE, spr->GetData());
5721+
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, spr->GetData());
56845722
}
56855723

56865724
void ApplyTexture(uint32_t id) override
@@ -6270,7 +6308,7 @@ namespace olc
62706308

62716309
void ReadTexture(uint32_t id, olc::Sprite* spr) override
62726310
{
6273-
glReadPixels(0, 0, spr->width, spr->height, GL_RGBA, GL_UNSIGNED_BYTE, spr->GetData());
6311+
glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, spr->GetData());
62746312
}
62756313

62766314
void ApplyTexture(uint32_t id) override

0 commit comments

Comments
 (0)