From df9e2f0b6417f08fc967bea574ee4282af567d51 Mon Sep 17 00:00:00 2001 From: Bram Verhulst Date: Tue, 2 Apr 2024 10:17:20 +0200 Subject: [PATCH] Basic screen system --- .idea/.idea.Motherload/.idea/workspace.xml | 93 ++- Engine/Collision.cpp | 2 +- Engine/structs.cpp | 1 + Engine/structs.h | 1 + Engine/utils.cpp | 632 +++++++++------------ Engine/utils.h | 2 + Game/Game.vcxproj | 4 + Game/Gui/Button.cpp | 1 + Game/Gui/Button.h | 19 + Game/Gui/Screen.cpp | 14 + Game/Gui/Screen.h | 26 + Game/Player.cpp | 3 +- Game/WorldLevel.cpp | 10 +- Game/WorldLevel.h | 4 +- Game/main.cpp | 2 +- 15 files changed, 404 insertions(+), 410 deletions(-) create mode 100644 Game/Gui/Button.cpp create mode 100644 Game/Gui/Button.h create mode 100644 Game/Gui/Screen.cpp create mode 100644 Game/Gui/Screen.h diff --git a/.idea/.idea.Motherload/.idea/workspace.xml b/.idea/.idea.Motherload/.idea/workspace.xml index 01eeafc..17e454f 100644 --- a/.idea/.idea.Motherload/.idea/workspace.xml +++ b/.idea/.idea.Motherload/.idea/workspace.xml @@ -11,34 +11,21 @@ - - - - - - - + + + + - - - - - + + + + + - - - - - - - - - - - { - "keyToString": { - "C++ Project.Game.executor": "Run", - "RunOnceActivity.OpenProjectViewOnStart": "true", - "RunOnceActivity.ShowReadmeOnStart": "true", - "ignore.virus.scanning.warn.message": "true", - "node.js.detected.package.eslint": "true", - "node.js.detected.package.tslint": "true", - "node.js.selected.package.eslint": "(autodetect)", - "node.js.selected.package.tslint": "(autodetect)", - "nodejs_package_manager_path": "npm", - "settings.editor.selected.configurable": "CppClangTidyOptionsId", - "vue.rearranger.settings.migration": "true" + +}]]> @@ -191,7 +172,10 @@ - + + + + - @@ -211,7 +203,8 @@ diff --git a/Engine/Collision.cpp b/Engine/Collision.cpp index ed77b70..ca78725 100644 --- a/Engine/Collision.cpp +++ b/Engine/Collision.cpp @@ -2,7 +2,7 @@ #include "utils.h" #include "../Game/Player.h" -#include "../Game/WorldTile.h" +#include "../Game/GridSystem/WorldTile.h" namespace Collision { diff --git a/Engine/structs.cpp b/Engine/structs.cpp index 30980e8..8891027 100644 --- a/Engine/structs.cpp +++ b/Engine/structs.cpp @@ -65,6 +65,7 @@ Rectf::Rectf(float left, float bottom, float width, float height) , width { width } , height { height } { } +Rectf::Rectf(Point2f pos, Point2f size): left(pos.x), bottom(pos.y), width(size.x), height(size.y) {} // Rectf::Rectf(int left, int bottom, int width, int height) : left { (float)left }, bottom { (float)bottom }, width { (float)width }, height { (float)height } { // } diff --git a/Engine/structs.h b/Engine/structs.h index a94ab2f..cd60f17 100644 --- a/Engine/structs.h +++ b/Engine/structs.h @@ -38,6 +38,7 @@ struct Rectf { Rectf( ); explicit Rectf( float left, float bottom, float width, float height ); + Rectf(Point2f pos, Point2f size); //explicit Rectf( int left, int bottom, int width, int height ); //Stupid fix for it giving an error (same as Point2f) Point2f BottomLeft() const { return Point2f{ left, bottom }; } diff --git a/Engine/utils.cpp b/Engine/utils.cpp index 91aa400..dcbbd8f 100644 --- a/Engine/utils.cpp +++ b/Engine/utils.cpp @@ -6,62 +6,54 @@ #include "utils.h" #pragma region OpenGLDrawFunctionality -void utils::SetColor( const Color4f& color ) -{ - glColor4f( color.r, color.g, color.b, color.a ); +void utils::SetColor(const Color4f& color) { + glColor4f(color.r, color.g, color.b, color.a); } -void utils::ClearBackground( const Color4f& color ) { +void utils::ClearBackground(const Color4f& color) { glClearColor(color.r, color.g, color.b, color.a); glClear(GL_COLOR_BUFFER_BIT); } -void utils::DrawPoint( float x, float y, float pointSize ) -{ - glPointSize( pointSize ); - glBegin( GL_POINTS ); +void utils::DrawPoint(float x, float y, float pointSize) { + glPointSize(pointSize); + glBegin(GL_POINTS); { - glVertex2f( x, y ); + glVertex2f(x, y); } - glEnd( ); + glEnd(); } -void utils::DrawPoint( const Point2f& p, float pointSize ) -{ - DrawPoint( p.x, p.y, pointSize ); +void utils::DrawPoint(const Point2f& p, float pointSize) { + DrawPoint(p.x, p.y, pointSize); } -void utils::DrawPoints( Point2f *pVertices, int nrVertices, float pointSize ) -{ - glPointSize( pointSize ); - glBegin( GL_POINTS ); +void utils::DrawPoints(Point2f* pVertices, int nrVertices, float pointSize) { + glPointSize(pointSize); + glBegin(GL_POINTS); { - for ( int idx{ 0 }; idx < nrVertices; ++idx ) - { - glVertex2f( pVertices[idx].x, pVertices[idx].y ); + for (int idx { 0 }; idx < nrVertices; ++idx) { + glVertex2f(pVertices[idx].x, pVertices[idx].y); } } - glEnd( ); + glEnd(); } -void utils::DrawLine( float x1, float y1, float x2, float y2, float lineWidth ) -{ - glLineWidth( lineWidth ); - glBegin( GL_LINES ); +void utils::DrawLine(float x1, float y1, float x2, float y2, float lineWidth) { + glLineWidth(lineWidth); + glBegin(GL_LINES); { - glVertex2f( x1, y1 ); - glVertex2f( x2, y2 ); + glVertex2f(x1, y1); + glVertex2f(x2, y2); } - glEnd( ); + glEnd(); } -void utils::DrawLine( const Point2f& p1, const Point2f& p2, float lineWidth ) -{ - DrawLine( p1.x, p1.y, p2.x, p2.y, lineWidth ); +void utils::DrawLine(const Point2f& p1, const Point2f& p2, float lineWidth) { + DrawLine(p1.x, p1.y, p2.x, p2.y, lineWidth); } -void utils::DrawTriangle(const Point2f& p1, const Point2f& p2, const Point2f& p3, float lineWidth) -{ +void utils::DrawTriangle(const Point2f& p1, const Point2f& p2, const Point2f& p3, float lineWidth) { glLineWidth(lineWidth); glBegin(GL_LINE_LOOP); { @@ -72,8 +64,7 @@ void utils::DrawTriangle(const Point2f& p1, const Point2f& p2, const Point2f& p3 glEnd(); } -void utils::FillTriangle(const Point2f& p1, const Point2f& p2, const Point2f& p3) -{ +void utils::FillTriangle(const Point2f& p1, const Point2f& p2, const Point2f& p3) { glBegin(GL_TRIANGLES); { glVertex2f(p1.x, p1.y); @@ -83,10 +74,8 @@ void utils::FillTriangle(const Point2f& p1, const Point2f& p2, const Point2f& p3 glEnd(); } -void utils::DrawRect( float left, float bottom, float width, float height, float lineWidth ) -{ - if (width > 0 && height > 0 && lineWidth > 0) - { +void utils::DrawRect(float left, float bottom, float width, float height, float lineWidth) { + if (width > 0 && height > 0 && lineWidth > 0) { glLineWidth(lineWidth); glBegin(GL_LINE_LOOP); @@ -100,20 +89,16 @@ void utils::DrawRect( float left, float bottom, float width, float height, float } } -void utils::DrawRect( const Point2f& bottomLeft, float width, float height, float lineWidth ) -{ - DrawRect( bottomLeft.x, bottomLeft.y, width, height, lineWidth ); +void utils::DrawRect(const Point2f& bottomLeft, float width, float height, float lineWidth) { + DrawRect(bottomLeft.x, bottomLeft.y, width, height, lineWidth); } -void utils::DrawRect( const Rectf& rect, float lineWidth ) -{ - DrawRect( rect.left, rect.bottom, rect.width, rect.height, lineWidth ); +void utils::DrawRect(const Rectf& rect, float lineWidth) { + DrawRect(rect.left, rect.bottom, rect.width, rect.height, lineWidth); } -void utils::FillRect( float left, float bottom, float width, float height ) -{ - if (width > 0 && height > 0) - { +void utils::FillRect(float left, float bottom, float width, float height) { + if (width > 0 && height > 0) { glBegin(GL_POLYGON); { @@ -126,28 +111,23 @@ void utils::FillRect( float left, float bottom, float width, float height ) } } -void utils::FillRect( const Point2f& bottomLeft, float width, float height ) -{ - FillRect( bottomLeft.x, bottomLeft.y, width, height ); +void utils::FillRect(const Point2f& bottomLeft, float width, float height) { + FillRect(bottomLeft.x, bottomLeft.y, width, height); } -void utils::FillRect( const Rectf& rect ) -{ - FillRect( rect.left, rect.bottom, rect.width, rect.height ); +void utils::FillRect(const Rectf& rect) { + FillRect(rect.left, rect.bottom, rect.width, rect.height); } -void utils::DrawEllipse( float centerX, float centerY, float radX, float radY, float lineWidth ) -{ - if (radX > 0 && radY > 0 && lineWidth > 0) - { +void utils::DrawEllipse(float centerX, float centerY, float radX, float radY, float lineWidth) { + if (radX > 0 && radY > 0 && lineWidth > 0) { - float dAngle{ radX > radY ? float(g_Pi / radX) : float(g_Pi / radY) }; + float dAngle { radX > radY ? float(g_Pi / radX) : float(g_Pi / radY) }; glLineWidth(lineWidth); glBegin(GL_LINE_LOOP); { - for (float angle = 0.0; angle < float(2 * g_Pi); angle += dAngle) - { + for (float angle = 0.0; angle < float(2 * g_Pi); angle += dAngle) { glVertex2f(centerX + radX * cos(angle), centerY + radY * sin(angle)); } } @@ -155,27 +135,22 @@ void utils::DrawEllipse( float centerX, float centerY, float radX, float radY, f } } -void utils::DrawEllipse( const Point2f& center, float radX, float radY, float lineWidth ) -{ - DrawEllipse( center.x, center.y, radX, radY, lineWidth ); +void utils::DrawEllipse(const Point2f& center, float radX, float radY, float lineWidth) { + DrawEllipse(center.x, center.y, radX, radY, lineWidth); } -void utils::DrawEllipse( const Ellipsef& ellipse, float lineWidth ) -{ - DrawEllipse( ellipse.center.x, ellipse.center.y, ellipse.radiusX, ellipse.radiusY, lineWidth ); +void utils::DrawEllipse(const Ellipsef& ellipse, float lineWidth) { + DrawEllipse(ellipse.center.x, ellipse.center.y, ellipse.radiusX, ellipse.radiusY, lineWidth); } -void utils::FillEllipse( float centerX, float centerY, float radX, float radY ) -{ - if (radX > 0 && radY > 0) - { +void utils::FillEllipse(float centerX, float centerY, float radX, float radY) { + if (radX > 0 && radY > 0) { - float dAngle{ radX > radY ? float(g_Pi / radX) : float(g_Pi / radY) }; + float dAngle { radX > radY ? float(g_Pi / radX) : float(g_Pi / radY) }; glBegin(GL_POLYGON); { - for (float angle = 0.0; angle < float(2 * g_Pi); angle += dAngle) - { + for (float angle = 0.0; angle < float(2 * g_Pi); angle += dAngle) { glVertex2f(centerX + radX * cos(angle), centerY + radY * sin(angle)); } } @@ -183,340 +158,283 @@ void utils::FillEllipse( float centerX, float centerY, float radX, float radY ) } } -void utils::FillEllipse( const Ellipsef& ellipse ) -{ - FillEllipse( ellipse.center.x, ellipse.center.y, ellipse.radiusX, ellipse.radiusY ); +void utils::FillEllipse(const Ellipsef& ellipse) { + FillEllipse(ellipse.center.x, ellipse.center.y, ellipse.radiusX, ellipse.radiusY); } -void utils::FillEllipse( const Point2f& center, float radX, float radY ) -{ - FillEllipse( center.x, center.y, radX, radY ); +void utils::FillEllipse(const Point2f& center, float radX, float radY) { + FillEllipse(center.x, center.y, radX, radY); } -void utils::DrawArc( float centerX, float centerY, float radX, float radY, float fromAngle, float tillAngle, float lineWidth ) -{ - if ( fromAngle > tillAngle ) - { +void utils::DrawArc(float centerX, float centerY, float radX, float radY, float fromAngle, float tillAngle, float lineWidth) { + if (fromAngle > tillAngle) { return; } - float dAngle{ radX > radY ? float( g_Pi / radX ) : float( g_Pi / radY ) }; + float dAngle { radX > radY ? float(g_Pi / radX) : float(g_Pi / radY) }; - glLineWidth( lineWidth ); - glBegin( GL_LINE_STRIP ); + glLineWidth(lineWidth); + glBegin(GL_LINE_STRIP); { - for ( float angle = fromAngle; angle < tillAngle; angle += dAngle ) - { - glVertex2f( centerX + radX * cos( angle ), centerY + radY * sin( angle ) ); + for (float angle = fromAngle; angle < tillAngle; angle += dAngle) { + glVertex2f(centerX + radX * cos(angle), centerY + radY * sin(angle)); } - glVertex2f( centerX + radX * cos( tillAngle ), centerY + radY * sin( tillAngle ) ); + glVertex2f(centerX + radX * cos(tillAngle), centerY + radY * sin(tillAngle)); } - glEnd( ); + glEnd(); } -void utils::DrawArc( const Point2f& center, float radX, float radY, float fromAngle, float tillAngle, float lineWidth ) -{ - DrawArc( center.x, center.y, radX, radY, fromAngle, tillAngle, lineWidth ); +void utils::DrawArc(const Point2f& center, float radX, float radY, float fromAngle, float tillAngle, float lineWidth) { + DrawArc(center.x, center.y, radX, radY, fromAngle, tillAngle, lineWidth); } -void utils::FillArc( float centerX, float centerY, float radX, float radY, float fromAngle, float tillAngle ) -{ - if ( fromAngle > tillAngle ) - { +void utils::FillArc(float centerX, float centerY, float radX, float radY, float fromAngle, float tillAngle) { + if (fromAngle > tillAngle) { return; } - float dAngle{ radX > radY ? float( g_Pi / radX ) : float( g_Pi / radY ) }; + float dAngle { radX > radY ? float(g_Pi / radX) : float(g_Pi / radY) }; - glBegin( GL_POLYGON ); + glBegin(GL_POLYGON); { - glVertex2f( centerX, centerY ); - for ( float angle = fromAngle; angle < tillAngle; angle += dAngle ) - { - glVertex2f( centerX + radX * cos( angle ), centerY + radY * sin( angle ) ); + glVertex2f(centerX, centerY); + for (float angle = fromAngle; angle < tillAngle; angle += dAngle) { + glVertex2f(centerX + radX * cos(angle), centerY + radY * sin(angle)); } - glVertex2f( centerX + radX * cos( tillAngle ), centerY + radY * sin( tillAngle ) ); + glVertex2f(centerX + radX * cos(tillAngle), centerY + radY * sin(tillAngle)); } - glEnd( ); + glEnd(); } -void utils::FillArc( const Point2f& center, float radX, float radY, float fromAngle, float tillAngle ) -{ - FillArc( center.x, center.y, radX, radY, fromAngle, tillAngle ); +void utils::FillArc(const Point2f& center, float radX, float radY, float fromAngle, float tillAngle) { + FillArc(center.x, center.y, radX, radY, fromAngle, tillAngle); } -void utils::DrawPolygon( const std::vector& vertices, bool closed, float lineWidth ) -{ - DrawPolygon( vertices.data( ), vertices.size( ), closed, lineWidth ); +void utils::DrawPolygon(const std::vector& vertices, bool closed, float lineWidth) { + DrawPolygon(vertices.data(), vertices.size(), closed, lineWidth); } -void utils::DrawPolygon( const Point2f* pVertices, size_t nrVertices, bool closed, float lineWidth ) -{ - glLineWidth( lineWidth ); - closed ? glBegin( GL_LINE_LOOP ) : glBegin( GL_LINE_STRIP ); +void utils::DrawPolygon(const Point2f* pVertices, size_t nrVertices, bool closed, float lineWidth) { + glLineWidth(lineWidth); + closed ? glBegin(GL_LINE_LOOP) : glBegin(GL_LINE_STRIP); { - for ( size_t idx{ 0 }; idx < nrVertices; ++idx ) - { - glVertex2f( pVertices[idx].x, pVertices[idx].y ); + for (size_t idx { 0 }; idx < nrVertices; ++idx) { + glVertex2f(pVertices[idx].x, pVertices[idx].y); } } - glEnd( ); + glEnd(); } -void utils::FillPolygon( const std::vector& vertices ) -{ - FillPolygon( vertices.data( ), vertices.size( ) ); +void utils::FillPolygon(const std::vector& vertices) { + FillPolygon(vertices.data(), vertices.size()); } -void utils::FillPolygon( const Point2f *pVertices, size_t nrVertices ) -{ - glBegin( GL_POLYGON ); +void utils::FillPolygon(const Point2f* pVertices, size_t nrVertices) { + glBegin(GL_POLYGON); { - for ( size_t idx{ 0 }; idx < nrVertices; ++idx ) - { - glVertex2f( pVertices[idx].x, pVertices[idx].y ); + for (size_t idx { 0 }; idx < nrVertices; ++idx) { + glVertex2f(pVertices[idx].x, pVertices[idx].y); } } - glEnd( ); + glEnd(); } #pragma endregion OpenGLDrawFunctionality #pragma region CollisionFunctionality -float utils::GetDistance(float x1, float y1, float x2, float y2) -{ - return (sqrtf((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1))); +float utils::GetDistance(float x1, float y1, float x2, float y2) { + return ( sqrtf(( x2 - x1 ) * ( x2 - x1 ) + ( y2 - y1 ) * ( y2 - y1 )) ); } -float utils::GetDistance(const Point2f& p1, const Point2f& p2) -{ +float utils::GetDistance(const Point2f& p1, const Point2f& p2) { return GetDistance(p1.x, p1.y, p2.x, p2.y); } -bool utils::IsPointInRect( const Point2f& p, const Rectf& r ) -{ - return ( p.x >= r.left && +bool utils::IsPointInRect(const Point2f& p, const Rectf& r) { + return ( p.x >= r.left && p.x <= r.left + r.width && p.y >= r.bottom && p.y <= r.bottom + r.height ); } -bool utils::IsPointInCircle( const Point2f& p, const Circlef& c ) -{ - float squaredDist{ (p.x - c.center.x) * (p.x - c.center.x) + (p.y - c.center.y) * (p.y - c.center.y) }; - float squaredRadius{ c.radius * c.radius }; +bool utils::IsPointInCircle(const Point2f& p, const Circlef& c) { + float squaredDist { ( p.x - c.center.x ) * ( p.x - c.center.x ) + ( p.y - c.center.y ) * ( p.y - c.center.y ) }; + float squaredRadius { c.radius * c.radius }; return ( squaredRadius >= squaredDist ); } -bool utils::IsOverlapping( const Point2f& a, const Point2f& b, const Rectf& r ) -{ +bool utils::IsOverlapping(const Point2f& a, const Point2f& b, const Rectf& r) { // if one of the line segment end points is in the rect - if ( utils::IsPointInRect( a, r ) || utils::IsPointInRect( b, r ) ) - { + if (utils::IsPointInRect(a, r) || utils::IsPointInRect(b, r)) { return true; } - HitInfo hitInfo{}; - Point2f vertices[]{ Point2f {r.left, r.bottom}, - Point2f{ r.left + r.width, r.bottom }, - Point2f{ r.left + r.width, r.bottom + r.height }, - Point2f{ r.left, r.bottom + r.height } }; + HitInfo hitInfo {}; + Point2f vertices[] { Point2f { r.left, r.bottom }, + Point2f { r.left + r.width, r.bottom }, + Point2f { r.left + r.width, r.bottom + r.height }, + Point2f { r.left, r.bottom + r.height } }; - return Raycast( vertices, 4, a, b, hitInfo ); + return Raycast(vertices, 4, a, b, hitInfo); } -bool utils::IsOverlapping( const Rectf& r1, const Rectf& r2 ) -{ +bool utils::IsOverlapping(const Rectf& r1, const Rectf& r2) { // If one rectangle is on left side of the other - if ( ( r1.left + r1.width ) < r2.left || ( r2.left + r2.width ) < r1.left ) - { + if (( r1.left + r1.width ) < r2.left || ( r2.left + r2.width ) < r1.left) { return false; } // If one rectangle is under the other - if ( r1.bottom > ( r2.bottom + r2.height ) || r2.bottom > ( r1.bottom + r1.height ) ) - { + if (r1.bottom > ( r2.bottom + r2.height ) || r2.bottom > ( r1.bottom + r1.height )) { return false; } return true; } -bool utils::IsOverlapping( const Rectf& r, const Circlef& c ) -{ +bool utils::IsOverlapping(const Rectf& r, const Circlef& c) { // Is center of circle in the rectangle? - if (IsPointInRect(c.center, r)) - { + if (IsPointInRect(c.center, r)) { return true; } // Check line segments - if (utils::DistPointLineSegment(c.center, Point2f{ r.left, r.bottom }, Point2f{ r.left, r.bottom + r.height }) <= c.radius) - { + if (utils::DistPointLineSegment(c.center, Point2f { r.left, r.bottom }, Point2f { r.left, r.bottom + r.height }) <= c.radius) { return true; } - if ( utils::DistPointLineSegment( c.center, Point2f{ r.left, r.bottom }, Point2f{ r.left + r.width, r.bottom } ) <= c.radius ) - { + if (utils::DistPointLineSegment(c.center, Point2f { r.left, r.bottom }, Point2f { r.left + r.width, r.bottom }) <= c.radius) { return true; } - if (utils::DistPointLineSegment(c.center, Point2f{ r.left + r.width, r.bottom + r.height }, Point2f{ r.left, r.bottom + r.height }) <= c.radius) - { + if (utils::DistPointLineSegment(c.center, Point2f { r.left + r.width, r.bottom + r.height }, Point2f { r.left, r.bottom + r.height }) <= c.radius) { return true; } - if (utils::DistPointLineSegment(c.center, Point2f{ r.left + r.width, r.bottom + r.height }, Point2f{ r.left + r.width, r.bottom }) <= c.radius) - { + if (utils::DistPointLineSegment(c.center, Point2f { r.left + r.width, r.bottom + r.height }, Point2f { r.left + r.width, r.bottom }) <= c.radius) { return true; } return false; } -bool utils::IsOverlapping( const Circlef& c1, const Circlef& c2 ) -{ +bool utils::IsOverlapping(const Circlef& c1, const Circlef& c2) { // squared distance between centers - float xDistance{ c1.center.x - c2.center.x }; - float yDistance{ c1.center.y - c2.center.y }; - float squaredDistance{ xDistance * xDistance + yDistance * yDistance }; + float xDistance { c1.center.x - c2.center.x }; + float yDistance { c1.center.y - c2.center.y }; + float squaredDistance { xDistance * xDistance + yDistance * yDistance }; - float squaredTouchingDistance{ (c1.radius + c2.radius) * (c1.radius + c2.radius) }; - return (squaredDistance < squaredTouchingDistance); + float squaredTouchingDistance { ( c1.radius + c2.radius ) * ( c1.radius + c2.radius ) }; + return ( squaredDistance < squaredTouchingDistance ); } -bool utils::IsOverlapping( const Point2f& a, const Point2f& b, const Circlef& c ) -{ - return utils::DistPointLineSegment( c.center, a, b ) <= c.radius; +bool utils::IsOverlapping(const Point2f& a, const Point2f& b, const Circlef& c) { + return utils::DistPointLineSegment(c.center, a, b) <= c.radius; } -bool utils::IsOverlapping( const std::vector& vertices, const Circlef& c ) -{ - return IsOverlapping( vertices.data( ), vertices.size( ), c ); +bool utils::IsOverlapping(const std::vector& vertices, const Circlef& c) { + return IsOverlapping(vertices.data(), vertices.size(), c); } -bool utils::IsOverlapping( const Point2f* vertices, size_t nrVertices, const Circlef& c ) -{ +bool utils::IsOverlapping(const Point2f* vertices, size_t nrVertices, const Circlef& c) { // Verify whether one of vertices is in circle - for ( size_t i{ 0 }; i < nrVertices; ++i ) - { - if ( IsPointInCircle( vertices[i], c ) ) - { + for (size_t i { 0 }; i < nrVertices; ++i) { + if (IsPointInCircle(vertices[i], c)) { return true; } } // Verify whether one of the polygon edges overlaps with circle - for ( size_t i{ 0 }; i < nrVertices; ++i ) - { - if ( DistPointLineSegment( c.center, vertices[i], vertices[( i + 1 ) % nrVertices] ) <= c.radius ) - { + for (size_t i { 0 }; i < nrVertices; ++i) { + if (DistPointLineSegment(c.center, vertices[i], vertices[( i + 1 ) % nrVertices]) <= c.radius) { return true; } } // No overlapping with edges, verify whether circle is completely inside the polygon - if ( IsPointInPolygon( c.center, vertices, nrVertices ) ) - { + if (IsPointInPolygon(c.center, vertices, nrVertices)) { return true; } return false; } -bool utils::IsPointInPolygon( const Point2f& p, const std::vector& vertices ) -{ - return IsPointInPolygon( p, vertices.data( ), vertices.size( ) ); +bool utils::IsPointInPolygon(const Point2f& p, const std::vector& vertices) { + return IsPointInPolygon(p, vertices.data(), vertices.size()); } -bool utils::IsPointInPolygon( const Point2f& p, const Point2f* vertices, size_t nrVertices ) -{ - if ( nrVertices < 2 ) - { +bool utils::IsPointInPolygon(const Point2f& p, const Point2f* vertices, size_t nrVertices) { + if (nrVertices < 2) { return false; } // 1. First do a simple test with axis aligned bounding box around the polygon - float xMin{ vertices[0].x }; - float xMax{ vertices[0].x }; - float yMin{ vertices[0].y }; - float yMax{ vertices[0].y }; - for ( size_t idx{ 1 }; idx < nrVertices; ++idx ) - { - if (xMin > vertices[idx].x) - { + float xMin { vertices[0].x }; + float xMax { vertices[0].x }; + float yMin { vertices[0].y }; + float yMax { vertices[0].y }; + for (size_t idx { 1 }; idx < nrVertices; ++idx) { + if (xMin > vertices[idx].x) { xMin = vertices[idx].x; } - if (xMax < vertices[idx].x) - { + if (xMax < vertices[idx].x) { xMax = vertices[idx].x; } - if (yMin > vertices[idx].y) - { + if (yMin > vertices[idx].y) { yMin = vertices[idx].y; } - if (yMax < vertices[idx].y) - { + if (yMax < vertices[idx].y) { yMax = vertices[idx].y; } } - if (p.x < xMin || p.x > xMax || p.y < yMin || p.y > yMax) - { + if (p.x < xMin || p.x > xMax || p.y < yMin || p.y > yMax) { return false; } // 2. Draw a virtual ray from anywhere outside the polygon to the point // and count how often it hits any side of the polygon. // If the number of hits is even, it's outside of the polygon, if it's odd, it's inside. - int numberOfIntersectionPoints{0}; - Point2f p2{ xMax + 10.0f, p.y }; // Horizontal line from point to point outside polygon (p2) + int numberOfIntersectionPoints { 0 }; + Point2f p2 { xMax + 10.0f, p.y }; // Horizontal line from point to point outside polygon (p2) // Count the number of intersection points - float lambda1{}, lambda2{}; - for ( size_t i{ 0 }; i < nrVertices; ++i ) - { - if ( IntersectLineSegments( vertices[i], vertices[( i + 1 ) % nrVertices], p, p2, lambda1, lambda2 ) ) - { - if ( lambda1 > 0 && lambda1 <= 1 && lambda2 > 0 && lambda2 <= 1 ) - { + float lambda1 {}, lambda2 {}; + for (size_t i { 0 }; i < nrVertices; ++i) { + if (IntersectLineSegments(vertices[i], vertices[( i + 1 ) % nrVertices], p, p2, lambda1, lambda2)) { + if (lambda1 > 0 && lambda1 <= 1 && lambda2 > 0 && lambda2 <= 1) { ++numberOfIntersectionPoints; } } } - if (numberOfIntersectionPoints % 2 == 0) - { + if (numberOfIntersectionPoints % 2 == 0) { return false; } - else - { + else { return true; } } -bool utils::IntersectLineSegments( const Point2f& p1, const Point2f& p2, const Point2f& q1, const Point2f& q2, float& outLambda1, float& outLambda2, float epsilon ) -{ - bool intersecting{ false }; +bool utils::IntersectLineSegments(const Point2f& p1, const Point2f& p2, const Point2f& q1, const Point2f& q2, float& outLambda1, float& outLambda2, float epsilon) { + bool intersecting { false }; - Vector2f p1p2{ p1, p2 }; - Vector2f q1q2{ q1, q2 }; + Vector2f p1p2 { p1, p2 }; + Vector2f q1q2 { q1, q2 }; // Cross product to determine if parallel - float denom = p1p2.CrossProduct( q1q2 ); + float denom = p1p2.CrossProduct(q1q2); // Don't divide by zero - if ( std::abs( denom ) > epsilon ) - { + if (std::abs(denom) > epsilon) { intersecting = true; - Vector2f p1q1{ p1, q1 }; + Vector2f p1q1 { p1, q1 }; - float num1 = p1q1.CrossProduct( q1q2 ); - float num2 = p1q1.CrossProduct( p1p2 ); + float num1 = p1q1.CrossProduct(q1q2); + float num2 = p1q1.CrossProduct(p1p2); outLambda1 = num1 / denom; outLambda2 = num2 / denom; } else // are parallel { // Connect start points - Vector2f p1q1{ p1, q1 }; + Vector2f p1q1 { p1, q1 }; // Cross product to determine if segments and the line connecting their start points are parallel, // if so, than they are on a line // if not, then there is no intersection - if (std::abs( p1q1.CrossProduct(q1q2) ) > epsilon) - { + if (std::abs(p1q1.CrossProduct(q1q2)) > epsilon) { return false; } @@ -526,23 +444,19 @@ bool utils::IntersectLineSegments( const Point2f& p1, const Point2f& p2, const P if (utils::IsPointOnLineSegment(p1, q1, q2) || utils::IsPointOnLineSegment(p2, q1, q2) || utils::IsPointOnLineSegment(q1, p1, p2) || - utils::IsPointOnLineSegment(q2, p1, p2)) - { + utils::IsPointOnLineSegment(q2, p1, p2)) { intersecting = true; } } return intersecting; } -bool utils::Raycast( const std::vector& vertices, const Point2f& rayP1, const Point2f& rayP2, HitInfo& hitInfo ) -{ - return Raycast( vertices.data( ), vertices.size( ), rayP1, rayP2, hitInfo ); +bool utils::Raycast(const std::vector& vertices, const Point2f& rayP1, const Point2f& rayP2, HitInfo& hitInfo) { + return Raycast(vertices.data(), vertices.size(), rayP1, rayP2, hitInfo); } -bool utils::Raycast( const Point2f* vertices, const size_t nrVertices, const Point2f& rayP1, const Point2f& rayP2, HitInfo& hitInfo ) -{ - if ( nrVertices == 0 ) - { +bool utils::Raycast(const Point2f* vertices, const size_t nrVertices, const Point2f& rayP1, const Point2f& rayP2, HitInfo& hitInfo) { + if (nrVertices == 0) { return false; } @@ -550,106 +464,94 @@ bool utils::Raycast( const Point2f* vertices, const size_t nrVertices, const Poi Rectf r1, r2; // r1: minimal AABB rect enclosing the ray - r1.left = std::min( rayP1.x, rayP2.x ); - r1.bottom = std::min( rayP1.y, rayP2.y ); - r1.width = std::max( rayP1.x, rayP2.x ) - r1.left; - r1.height = std::max( rayP1.y, rayP2.y ) - r1.bottom; + r1.left = std::min(rayP1.x, rayP2.x); + r1.bottom = std::min(rayP1.y, rayP2.y); + r1.width = std::max(rayP1.x, rayP2.x) - r1.left; + r1.height = std::max(rayP1.y, rayP2.y) - r1.bottom; // Line-line intersections. - for ( size_t idx{ 0 }; idx <= nrVertices; ++idx ) - { + for (size_t idx { 0 }; idx <= nrVertices; ++idx) { // Consider line segment between 2 consecutive vertices // (modulo to allow closed polygon, last - first vertice) Point2f q1 = vertices[( idx + 0 ) % nrVertices]; Point2f q2 = vertices[( idx + 1 ) % nrVertices]; // r2: minimal AABB rect enclosing the 2 vertices - r2.left = std::min( q1.x, q2.x ); - r2.bottom = std::min( q1.y, q2.y ); - r2.width = std::max( q1.x, q2.x ) - r2.left; - r2.height = std::max( q1.y, q2.y ) - r2.bottom; + r2.left = std::min(q1.x, q2.x); + r2.bottom = std::min(q1.y, q2.y); + r2.width = std::max(q1.x, q2.x) - r2.left; + r2.height = std::max(q1.y, q2.y) - r2.bottom; - if ( IsOverlapping( r1, r2 ) ) - { - float lambda1{}; - float lambda2{}; - if ( IntersectLineSegments( rayP1, rayP2, q1, q2, lambda1, lambda2 ) ) - { - if ( lambda1 > 0 && lambda1 <= 1 && lambda2 > 0 && lambda2 <= 1 ) - { - HitInfo linesHitInfo{}; + if (IsOverlapping(r1, r2)) { + float lambda1 {}; + float lambda2 {}; + if (IntersectLineSegments(rayP1, rayP2, q1, q2, lambda1, lambda2)) { + if (lambda1 > 0 && lambda1 <= 1 && lambda2 > 0 && lambda2 <= 1) { + HitInfo linesHitInfo {}; linesHitInfo.lambda = lambda1; - linesHitInfo.intersectPoint = Point2f{ rayP1.x + ( ( rayP2.x - rayP1.x ) * lambda1 ), rayP1.y + ( ( rayP2.y - rayP1.y ) * lambda1 ) }; - linesHitInfo.normal = Vector2f{ q2.x - q1.x, q2.y - q2.y }.Orthogonal( ).Normalized( ); + linesHitInfo.intersectPoint = Point2f { rayP1.x + ( ( rayP2.x - rayP1.x ) * lambda1 ), rayP1.y + ( ( rayP2.y - rayP1.y ) * lambda1 ) }; + linesHitInfo.normal = Vector2f { q2.x - q1.x, q2.y - q2.y }.Orthogonal().Normalized(); hits.push_back(linesHitInfo); } } } } - if ( hits.size( ) == 0 ) - { + if (hits.size() == 0) { return false; } // Get closest intersection point and copy it into the hitInfo parameter hitInfo = *std::min_element - ( - hits.begin( ), hits.end( ), - []( const HitInfo& first, const HitInfo& last ) + ( + hits.begin(), hits.end(), + [](const HitInfo& first, const HitInfo& last) { - return first.lambda < last.lambda; - } + return first.lambda < last.lambda; + } ); return true; } -bool utils::IsPointOnLineSegment( const Point2f& p, const Point2f& a, const Point2f& b ) -{ - Vector2f ap{ a, p }, bp{ b, p }; +bool utils::IsPointOnLineSegment(const Point2f& p, const Point2f& a, const Point2f& b) { + Vector2f ap { a, p }, bp { b, p }; // If not on same line, return false - if ( abs( ap.CrossProduct( bp ) ) > 0.001f ) - { + if (abs(ap.CrossProduct(bp)) > 0.001f) { return false; } // Both vectors must point in opposite directions if p is between a and b - if ( ap.DotProduct( bp ) > 0 ) - { + if (ap.DotProduct(bp) > 0) { return false; } return true; } -float utils::DistPointLineSegment( const Point2f& p, const Point2f& a, const Point2f& b ) -{ - Vector2f ab{ a, b }; - Vector2f ap{ a, p }; - Vector2f abNorm{ ab.Normalized() }; - float distToA{ abNorm.DotProduct(ap) }; +float utils::DistPointLineSegment(const Point2f& p, const Point2f& a, const Point2f& b) { + Vector2f ab { a, b }; + Vector2f ap { a, p }; + Vector2f abNorm { ab.Normalized() }; + float distToA { abNorm.DotProduct(ap) }; // If distToA is negative, then the closest point is A // return the distance a, p - if ( distToA < 0 ) - { - return ap.Length( ); + if (distToA < 0) { + return ap.Length(); } // If distToA is > than dist(a,b) then the closest point is B // return the distance b, p - float distAB{ ab.Length() }; - if ( distToA > distAB ) - { - return Vector2f{ b, p }.Length( ); + float distAB { ab.Length() }; + if (distToA > distAB) { + return Vector2f { b, p }.Length(); } // Closest point is between A and B, calc intersection point - Vector2f intersection{ abNorm.DotProduct(ap) * abNorm + Vector2f{ a } }; - return Vector2f{ p.x - intersection.x, p.y - intersection.y }.Length( ); + Vector2f intersection { abNorm.DotProduct(ap) * abNorm + Vector2f { a } }; + return Vector2f { p.x - intersection.x, p.y - intersection.y }.Length(); } -bool utils::IntersectRectLine(const Rectf& r, const Point2f& p1, const Point2f& p2, float& intersectMin, float& intersectMax) -{ +bool utils::IntersectRectLine(const Rectf& r, const Point2f& p1, const Point2f& p2, float& intersectMin, float& intersectMax) { // Parameters // input: // r: axis aligned bounding box, start and end points of line segment. @@ -670,16 +572,16 @@ bool utils::IntersectRectLine(const Rectf& r, const Point2f& p1, const Point2f& // 4 floats to convert rect space to line space // x1: value between 0 and 1 where 0 is on p1 and 1 is on p2, <0 and >1 means intersection is not on line segment - float x1{ (r.left - p1.x) / (p2.x - p1.x) }; - float x2{ (r.left + r.width - p1.x) / (p2.x - p1.x) }; - float y1{ (r.bottom - p1.y) / (p2.y - p1.y) }; - float y2{ (r.bottom + r.height - p1.y) / (p2.y - p1.y) }; + float x1 { ( r.left - p1.x ) / ( p2.x - p1.x ) }; + float x2 { ( r.left + r.width - p1.x ) / ( p2.x - p1.x ) }; + float y1 { ( r.bottom - p1.y ) / ( p2.y - p1.y ) }; + float y2 { ( r.bottom + r.height - p1.y ) / ( p2.y - p1.y ) }; - using std::max; using std::min; - float tMin{ max(min(x1,x2),min(y1,y2)) }; - float tMax{ min(max(x1,x2), max(y1,y2)) }; - if (tMin > tMax) - { + using std::max; + using std::min; + float tMin { max(min(x1, x2), min(y1, y2)) }; + float tMax { min(max(x1, x2), max(y1, y2)) }; + if (tMin > tMax) { return false; } intersectMin = tMin; @@ -688,60 +590,70 @@ bool utils::IntersectRectLine(const Rectf& r, const Point2f& p1, const Point2f& } bool utils::IsRectInRect(const Rectf& r1, const Rectf& r2) { // the origin of both rectangles is in bottom left - return (r1.left < r2.left + r2.width && r1.left + r1.width > r2.left && r1.bottom < r2.bottom + r2.height && r1.bottom + r1.height > r2.bottom); + return ( r1.left < r2.left + r2.width && r1.left + r1.width > r2.left && r1.bottom < r2.bottom + r2.height && r1.bottom + r1.height > r2.bottom ); } bool utils::RayVsRect(const Point2f& rayOrigin, const Point2f& rayDir, const Rectf& target, Point2f& contactPoint, Point2f& contactNormal, float& t_hit_near) { - + // Point2f t_near = Point2f{(target.BottomLeft() - rayOrigin).x / rayDir.x, (target.BottomLeft() - rayOrigin).y / rayDir.y}; // Point2f t_far = Point2f{(target.BottomLeft() + Point2f{target.width, target.height} - rayOrigin).x / rayDir.x, (target.BottomLeft() + Point2f{target.width, target.height} - rayOrigin).y / rayDir.y}; - Point2f t_near{}; - Point2f t_far{}; - - if(std::isnan(t_far.y) || std::isnan(t_far.x)) return false; - if(std::isnan(t_near.y) || std::isnan(t_near.x)) return false; - - if (t_near.x > t_far.x) std::swap(t_near.x, t_far.x); - if (t_near.y > t_far.y) std::swap(t_near.y, t_far.y); + Point2f t_near {}; + Point2f t_far {}; - if (t_near.x > t_far.y || t_near.y > t_far.x) return false; + if (std::isnan(t_far.y) || std::isnan(t_far.x)) + return false; + if (std::isnan(t_near.y) || std::isnan(t_near.x)) + return false; + + if (t_near.x > t_far.x) + std::swap(t_near.x, t_far.x); + if (t_near.y > t_far.y) + std::swap(t_near.y, t_far.y); + + if (t_near.x > t_far.y || t_near.y > t_far.x) + return false; t_hit_near = std::max(t_near.x, t_near.y); float t_hit_far = std::min(t_far.x, t_far.y); - if (t_hit_far < 0) return false; + if (t_hit_far < 0) + return false; contactPoint = rayOrigin + rayDir * t_hit_near; - if(t_near.x > t_near.y) { - if(rayDir.x < 0) { - contactNormal = Point2f{1, 0}; - }else { - contactNormal = Point2f{-1, 0}; + if (t_near.x > t_near.y) { + if (rayDir.x < 0) { + contactNormal = Point2f { 1, 0 }; } - } else if(t_near.x < t_near.y) { - if(rayDir.y < 0) { - contactNormal = Point2f{0, 1}; - }else { - contactNormal = Point2f{0, -1}; + else { + contactNormal = Point2f { -1, 0 }; + } + } + else if (t_near.x < t_near.y) { + if (rayDir.y < 0) { + contactNormal = Point2f { 0, 1 }; + } + else { + contactNormal = Point2f { 0, -1 }; } } return true; } bool utils::DynamicRectVsRect(const MovingRectf& in, const Rectf& target, Point2f& contactPoint, Point2f& contactNormal, float& contactTime, float dt) { - if(in.velocity.x == 0 && in.velocity.y == 0) return false; + if (in.velocity.x == 0 && in.velocity.y == 0) + return false; - Rectf expanded_target{}; + Rectf expanded_target {}; expanded_target.left = target.left - in.width / 2; expanded_target.bottom = target.bottom - in.height / 2; expanded_target.width = target.width + in.width; expanded_target.height = target.height + in.height; - - if(RayVsRect(Point2f{in.bottomLeft.x + in.width / 2, in.bottomLeft.y + in.height / 2}, in.velocity * dt, expanded_target, contactPoint, contactNormal, contactTime)) { - if(contactTime <= 1.0f && contactTime >= 0.0f) { + + if (RayVsRect(Point2f { in.bottomLeft.x + in.width / 2, in.bottomLeft.y + in.height / 2 }, in.velocity * dt, expanded_target, contactPoint, contactNormal, contactTime)) { + if (contactTime <= 1.0f && contactTime >= 0.0f) { return true; } } @@ -753,7 +665,7 @@ float utils::DotProduct(const Point2f& a, const Point2f& b) { #pragma endregion CollisionFunctionality int utils::randRange(int min, int max) { - return min + rand() % (( max + 1 ) - min); + return min + rand() % ( ( max + 1 ) - min ); } bool utils::isKeyDown(int keycode) { const Uint8* pStates = SDL_GetKeyboardState(nullptr); @@ -762,27 +674,37 @@ bool utils::isKeyDown(int keycode) { } return false; } -static bool s_KeyStates[256] = {false}; +static bool S_PrevKeyStates[256] = { false }; bool utils::isKeyPressed(int keycode) { const Uint8* pStates = SDL_GetKeyboardState(nullptr); - if (pStates != nullptr) { - if(pStates[keycode] && !s_KeyStates[keycode]) { - s_KeyStates[keycode] = true; - }else { - s_KeyStates[keycode] = false; - } - return s_KeyStates[keycode]; + if (pStates == nullptr) { + return false; } - return false; + bool pressed { false }; + + bool currentPressed = pStates[keycode]; + bool lastPressed = S_PrevKeyStates[keycode]; + + if (!lastPressed && currentPressed) { + pressed = true; + } + S_PrevKeyStates[keycode] = currentPressed; + return pressed; } bool utils::isKeyUp(int keycode) { const Uint8* pStates = SDL_GetKeyboardState(nullptr); - if(pStates[keycode] == 0) { + if (pStates[keycode] == 0) { return true; } return false; } +Point2f utils::GetMousePos() { + int x, y; + SDL_GetMouseState(&x, &y); + //TODO: make the screen size a global or something + return Point2f { float(x), float(500.f - y) }; +} bool utils::isMouseDown(int button) { const Uint32 pStates = SDL_GetMouseState(nullptr, nullptr); diff --git a/Engine/utils.h b/Engine/utils.h index 98c8bb2..9316bb2 100644 --- a/Engine/utils.h +++ b/Engine/utils.h @@ -111,4 +111,6 @@ namespace utils bool isMouseDown(int button); bool isKeyUp(int keycode); + Point2f GetMousePos(); + } \ No newline at end of file diff --git a/Game/Game.vcxproj b/Game/Game.vcxproj index e76bcfa..6b2f98f 100644 --- a/Game/Game.vcxproj +++ b/Game/Game.vcxproj @@ -299,6 +299,8 @@ true true + + MultiThreadedDebugDll EnableFastChecks @@ -458,6 +460,8 @@ + + diff --git a/Game/Gui/Button.cpp b/Game/Gui/Button.cpp new file mode 100644 index 0000000..b079d40 --- /dev/null +++ b/Game/Gui/Button.cpp @@ -0,0 +1 @@ +#include "Button.h" diff --git a/Game/Gui/Button.h b/Game/Gui/Button.h new file mode 100644 index 0000000..240a95e --- /dev/null +++ b/Game/Gui/Button.h @@ -0,0 +1,19 @@ +#pragma once +#include "Texture.h" + +class Button +{ +public: + Button() = default; + Button(const std::string& filePath, Point2f pos, Point2f size); + void Draw() const; + void Update(float elapsedSec); + +private: + Texture* m_Texture{ nullptr }; + Point2f m_Position; + Point2f m_Size; + + bool m_IsHovered{ false }; + bool m_IsPressed{ false }; +}; diff --git a/Game/Gui/Screen.cpp b/Game/Gui/Screen.cpp new file mode 100644 index 0000000..6bbb512 --- /dev/null +++ b/Game/Gui/Screen.cpp @@ -0,0 +1,14 @@ +#include "Screen.h" +Screen::Screen(const std::string& filePath, Point2f pos, Point2f size,TextureManager* manager): m_Position(pos), m_Size(size) +{ + m_Background = manager->GetTexture(filePath); +} +Screen::~Screen() { +} +void Screen::Update(float elapsedSecs) { +} +void Screen::Draw() const { + Rectf dest = Rectf(m_Position, m_Size); + Rectf src = Rectf(0,0, m_Background->GetWidth(), m_Background->GetHeight()); + m_Background->Draw(dest, src, false); +} diff --git a/Game/Gui/Screen.h b/Game/Gui/Screen.h new file mode 100644 index 0000000..7f11fca --- /dev/null +++ b/Game/Gui/Screen.h @@ -0,0 +1,26 @@ +#pragma once +#include "structs.h" +#include "Texture.h" +#include "../TextureManager.h" + +class Screen +{ +public: + Screen() = default; + Screen(const std::string& filePath, Point2f pos, Point2f size, TextureManager* manager); + + ~Screen(); + + void setActive(bool active) { m_Active = active; } + void toggleActive() { m_Active = !m_Active; } + + void Update(float elapsedSecs); + void Draw() const; +private: + Point2f m_Position; + Point2f m_Size; + + Texture* m_Background{ nullptr }; + + bool m_Active{ false }; +}; diff --git a/Game/Player.cpp b/Game/Player.cpp index 9e3f4b5..0735d18 100644 --- a/Game/Player.cpp +++ b/Game/Player.cpp @@ -7,6 +7,7 @@ #include "utils.h" #include "WorldLevel.h" #include "Animations/Animation.h" +#include "GridSystem/WorldTile.h" Player::Player(const Point2f& Position, TextureManager* manager) : m_Position(Position), m_Size(Point2f { 40, 40 }), m_Vel(Point2f { 0, 0 }), m_Acc(Point2f { 0, 0 }) { m_ContactMap[Collision::CollisionDirection::Top] = nullptr; @@ -39,7 +40,7 @@ void Player::ProcessImGui() { ImGui::Begin("Collision Info", nullptr, ImGuiWindowFlags_AlwaysAutoResize); ImGui::Text("is Grounded: %s", m_Grounded ? "true" : "false"); ImGui::Text("Did just dig right: %s", m_DidJustDigRight ? "true" : "false"); - bool test = !utils::isKeyDown(SDL_SCANCODE_H); + bool test = !utils::isKeyPressed(SDL_SCANCODE_H); ImGui::Text("Is Key Up H: %s", test ? "true" : "false"); ImGui::Checkbox("Draw Collision Rect", &m_DrawCollisionRect); diff --git a/Game/WorldLevel.cpp b/Game/WorldLevel.cpp index 5553159..b93d667 100644 --- a/Game/WorldLevel.cpp +++ b/Game/WorldLevel.cpp @@ -9,8 +9,10 @@ #include "Collision.h" #include "colors.h" #include "utils.h" +#include "GridSystem/WorldTile.h" +class GroundTileType; WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera), m_gridManager(WorldGridManager()), m_player(Player { Point2f { 0, 100 }, TextureManager::GetInstance() }), @@ -33,7 +35,7 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera), //AIR break; default: - std::cout << "??" << std::endl; + std::cout << "??" << '\n'; } m_gridManager.SetTileAtIndex(x, y, new WorldTile { pos, type, TextureManager::GetInstance() }); @@ -42,6 +44,8 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera), for (size_t x { 0 }; x < WORLD_WIDTH; ++x) { m_gridManager.GetTileAtIndex(x, 0)->SetTileType(Tiles::AIR); } + Point2f screenCenterPos = Point2f { m_viewport.width / 2 - 492 / 2, m_viewport.height / 2 - 396 / 2 }; + m_screen = Screen{ "gui/fuel/background.png", screenCenterPos, Point2f { 492, 396 }, TextureManager::GetInstance() }; } WorldLevel::~WorldLevel() { //delete m_pTextTexture; @@ -117,6 +121,10 @@ void WorldLevel::Draw() const { m_player.Draw(); m_pCamera->EndRendering(); + + utils::FillRect(utils::GetMousePos(), 10, 10); + + m_screen.Draw(); } void WorldLevel::MouseMove(const Point2f& mousePos) { m_mousePos = mousePos; diff --git a/Game/WorldLevel.h b/Game/WorldLevel.h index 694e4f0..9082302 100644 --- a/Game/WorldLevel.h +++ b/Game/WorldLevel.h @@ -3,8 +3,8 @@ #include "Level.h" #include "Player.h" #include "utils.h" -#include "WorldLevel.h" #include "GridSystem/WorldGridManager.h" +#include "Gui/Screen.h" class WorldLevel : public Level @@ -35,6 +35,8 @@ private: Rectf m_viewport; + Screen m_screen; + // ImGui Vars bool m_ShowTextureManagerWindow { false }; bool m_ShowCameraWindow { false }; diff --git a/Game/main.cpp b/Game/main.cpp index 0d06bc3..dc2efff 100644 --- a/Game/main.cpp +++ b/Game/main.cpp @@ -11,7 +11,7 @@ int SDL_main(int argv, char** args) { StartHeapControl(); - auto pGame { new Game { Window { "Motherload - Verhulst, Bram - 1DAEGD16E", 846.f, 500.f } } }; + auto pGame { new Game { Window { "Motherload - Verhulst, Bram - 1DAEGD16E", 900.f, 500.f } } }; pGame->Run(); delete pGame;