Skip to content

Commit d1da307

Browse files
authored
v2.23
2 parents 410cae6 + 5228ea6 commit d1da307

File tree

2 files changed

+48
-28
lines changed

2 files changed

+48
-28
lines changed

olcPixelGameEngine.h

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,8 @@
316316
+FillTexturedPolygon() - Hijacks DecalStructure for configuration
317317
+olc::vf2d arguments for Sprite::Sample() functions
318318
2.22: = Fix typo on dragged file buffers for unicode builds
319+
2.23: Fixed Emscripten host sizing errors - Thanks Moros
320+
Fixed v2d_generic.clamp() function
319321
320322
!! Apple Platforms will not see these updates immediately - Sorry, I dont have a mac to test... !!
321323
!! Volunteers willing to help appreciated, though PRs are manually integrated with credit !!
@@ -395,7 +397,7 @@ int main()
395397
#include <cstring>
396398
#pragma endregion
397399

398-
#define PGE_VER 222
400+
#define PGE_VER 223
399401

400402
// O------------------------------------------------------------------------------O
401403
// | COMPILER CONFIGURATION ODDITIES |
@@ -683,7 +685,7 @@ namespace olc
683685
v2d_generic min(const v2d_generic& v) const { return v2d_generic(std::min(x, v.x), std::min(y, v.y)); }
684686
v2d_generic cart() { return { std::cos(y) * x, std::sin(y) * x }; }
685687
v2d_generic polar() { return { mag(), std::atan2(y, x) }; }
686-
v2d_generic clamp(const v2d_generic& v1, const v2d_generic& v2) const { return this->max(v1)->min(v2); }
688+
v2d_generic clamp(const v2d_generic& v1, const v2d_generic& v2) const { return this->max(v1).min(v2); }
687689
v2d_generic lerp(const v2d_generic& v1, const double t) { return this->operator*(T(1.0 - t)) + (v1 * T(t)); }
688690
T dot(const v2d_generic& rhs) const { return this->x * rhs.x + this->y * rhs.y; }
689691
T cross(const v2d_generic& rhs) const { return this->x * rhs.y - this->y * rhs.x; }
@@ -1358,8 +1360,9 @@ namespace olc
13581360
#endif
13591361

13601362
#if defined(OLC_PLATFORM_X11)
1361-
namespace X11
1362-
{#include <GL/glx.h>}
1363+
namespace X11 {
1364+
#include <GL/glx.h>
1365+
}
13631366
#define CALLSTYLE
13641367
#endif
13651368

@@ -4595,17 +4598,17 @@ namespace olc
45954598
// #include <OpenGL/glu.h>
45964599
//#endif
45974600

4598-
//#if defined(OLC_PLATFORM_EMSCRIPTEN)
4599-
// #include <EGL/egl.h>
4600-
// #include <GLES2/gl2.h>
4601-
// #define GL_GLEXT_PROTOTYPES
4602-
// #include <GLES2/gl2ext.h>
4603-
// #include <emscripten/emscripten.h>
4604-
// #define CALLSTYLE
4605-
// typedef EGLBoolean(locSwapInterval_t)(EGLDisplay display, EGLint interval);
4606-
// #define GL_CLAMP GL_CLAMP_TO_EDGE
4607-
// #define OGL_LOAD(t, n) n;
4608-
//#endif
4601+
#if defined(OLC_PLATFORM_EMSCRIPTEN)
4602+
#include <EGL/egl.h>
4603+
#include <GLES2/gl2.h>
4604+
#define GL_GLEXT_PROTOTYPES
4605+
#include <GLES2/gl2ext.h>
4606+
#include <emscripten/emscripten.h>
4607+
#define CALLSTYLE
4608+
typedef EGLBoolean(locSwapInterval_t)(EGLDisplay display, EGLint interval);
4609+
#define GL_CLAMP GL_CLAMP_TO_EDGE
4610+
#define OGL_LOAD(t, n) n;
4611+
#endif
46094612

46104613
namespace olc
46114614
{
@@ -6319,8 +6322,8 @@ namespace olc
63196322
let isFullscreen = (document.fullscreenElement != null);
63206323

63216324
// get the width of the containing element
6322-
let width = (isFullscreen || !Module.olc_AssumeDefaultShells) ? window.innerWidth : Module.canvas.parentNode.clientWidth;
6323-
let height = (isFullscreen || !Module.olc_AssumeDefaultShells) ? window.innerHeight : Module.canvas.parentNode.clientHeight;
6325+
let width = (isFullscreen) ? window.innerWidth : Module.canvas.parentNode.clientWidth;
6326+
let height = (isFullscreen) ? window.innerHeight : Module.canvas.parentNode.clientHeight;
63246327

63256328
// calculate the expected viewport size
63266329
let viewWidth = width;

utilities/olcUTIL_Geometry2D.h

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -277,43 +277,60 @@ namespace olc::utils::geom2d
277277

278278
// Returns closest point to point
279279
template<typename T1, typename T2>
280-
inline olc::v2d_generic<T2> closest(const olc::v2d_generic<T1>& p1, const olc::v2d_generic<T2>& p2)
280+
inline olc::v2d_generic<T1> closest(const olc::v2d_generic<T1>& p1, const olc::v2d_generic<T2>& p2)
281281
{
282282
return p1;
283283
}
284284

285285
// Returns closest point on line to point
286286
template<typename T1, typename T2>
287-
inline olc::v2d_generic<T2> closest(const line<T1>& l, const olc::v2d_generic<T2>& p)
287+
inline olc::v2d_generic<T1> closest(const line<T1>& l, const olc::v2d_generic<T2>& p)
288288
{
289289
auto d = l.vector();
290-
double u = std::clamp(double(d.dot(p - l.start) / d.mag2()), 0.0, 1.0);
291-
return l.start + d * u;
290+
double u = std::clamp(double(d.dot(p - l.start)) / d.mag2(), 0.0, 1.0);
291+
return l.start + u * d;
292292
}
293293

294294
// Returns closest point on circle to point
295295
template<typename T1, typename T2>
296-
inline olc::v2d_generic<T2> closest(const circle<T1>& c, const olc::v2d_generic<T2>& p)
296+
inline olc::v2d_generic<T1> closest(const circle<T1>& c, const olc::v2d_generic<T2>& p)
297297
{
298-
return c.pos + (p - c.pos).norm() * c.radius;
298+
return c.pos + olc::vd2d(p - c.pos).norm() * c.radius;
299299
}
300300

301301
// Returns closest point on rectangle to point
302302
template<typename T1, typename T2>
303-
inline olc::v2d_generic<T2> closest(const rect<T1>& r, const olc::v2d_generic<T2>& p)
303+
inline olc::v2d_generic<T1> closest(const rect<T1>& r, const olc::v2d_generic<T2>& p)
304304
{
305305
// This could be a "constrain" function hmmmm
306306
// TODO: Not quite what i wanted, should restrain to boundary
307-
return olc::v2d_generic<T2>{ std::clamp(p.x, r.pos.x, r.pos.x + r.size.x), std::clamp(p.y, r.pos.y, r.pos.y + r.size.y) };
307+
return olc::v2d_generic<T1>{ std::clamp(p.x, r.pos.x, r.pos.x + r.size.x), std::clamp(p.y, r.pos.y, r.pos.y + r.size.y) };
308308

309309
}
310310

311311
// Returns closest point on triangle to point
312312
template<typename T1, typename T2>
313-
inline olc::v2d_generic<T2> closest(const triangle<T1>& t, const olc::v2d_generic<T2>& p)
313+
inline olc::v2d_generic<T1> closest(const triangle<T1>& t, const olc::v2d_generic<T2>& p)
314314
{
315-
// TODO:
316-
return olc::v2d_generic<T2>();
315+
olc::utils::geom2d::line<T1> l{t.pos[0], t.pos[1]};
316+
auto p0 = closest(l, p);
317+
auto d0 = (p0 - p).mag2();
318+
319+
l.end = t.pos[2];
320+
auto p1 = closest(l, p);
321+
auto d1 = (p1 - p).mag2();
322+
323+
l.start = t.pos[1];
324+
auto p2 = closest(l, p);
325+
auto d2 = (p2 - p).mag2();
326+
327+
if((d0 <= d1) && (d0 <= d2)) {
328+
return p0;
329+
} else if((d1 <= d0) && (d1 <= d2)) {
330+
return p1;
331+
} else {
332+
return p2;
333+
}
317334
}
318335

319336

0 commit comments

Comments
 (0)