Basic screen system

This commit is contained in:
Bram Verhulst
2024-04-02 10:17:20 +02:00
parent 0f9bb76973
commit df9e2f0b64
15 changed files with 404 additions and 410 deletions

View File

@@ -11,34 +11,21 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="26a0623a-44d5-441c-8048-32ff1dab3479" name="Changes" comment=""> <list default="true" id="26a0623a-44d5-441c-8048-32ff1dab3479" name="Changes" comment="">
<change afterPath="$PROJECT_DIR$/.idea/.idea.Motherload/.idea/.name" afterDir="false" /> <change afterPath="$PROJECT_DIR$/Game/Gui/Button.cpp" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/.idea.Motherload/.idea/indexLayout.xml" afterDir="false" /> <change afterPath="$PROJECT_DIR$/Game/Gui/Button.h" afterDir="false" />
<change afterPath="$PROJECT_DIR$/.idea/.idea.Motherload/.idea/projectSettingsUpdater.xml" afterDir="false" /> <change afterPath="$PROJECT_DIR$/Game/Gui/Screen.cpp" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Assets/Player/PlayerWalk.aseprite" afterDir="false" /> <change afterPath="$PROJECT_DIR$/Game/Gui/Screen.h" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Game/Animations/Animation.cpp" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Game/Animations/Animation.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/.idea.Motherload/.idea/copilot/chatSessions/blobs/version" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/.idea.Motherload/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.Motherload/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/.idea.Motherload/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/.idea.Motherload/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Assets/PlayerWalk.aseprite" beforeDir="false" /> <change beforePath="$PROJECT_DIR$/Engine/Collision.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Engine/Collision.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/Camera.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Camera.cpp" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Engine/structs.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Engine/structs.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/Camera.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Camera.h" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Engine/structs.h" beforeDir="false" afterPath="$PROJECT_DIR$/Engine/structs.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/Game.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Game.cpp" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Engine/utils.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Engine/utils.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/Game.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Game.h" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Engine/utils.h" beforeDir="false" afterPath="$PROJECT_DIR$/Engine/utils.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/Game.vcxproj" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Game.vcxproj" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Game/Game.vcxproj" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Game.vcxproj" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/Level.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Level.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/Level.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Level.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/Player.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Player.cpp" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Game/Player.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Player.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/Player.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/Player.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/TextureManager.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/TextureManager.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/TextureManager.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/TextureManager.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/WorldGridManager.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/GridSystem/WorldGridManager.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/WorldGridManager.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/GridSystem/WorldGridManager.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/WorldLevel.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/WorldLevel.cpp" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Game/WorldLevel.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/WorldLevel.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/WorldLevel.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/WorldLevel.h" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Game/WorldLevel.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/WorldLevel.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/WorldTile.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/GridSystem/WorldTile.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/WorldTile.h" beforeDir="false" afterPath="$PROJECT_DIR$/Game/GridSystem/WorldTile.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/main.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/main.cpp" afterDir="false" /> <change beforePath="$PROJECT_DIR$/Game/main.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/main.cpp" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Game/pch.cpp" beforeDir="false" afterPath="$PROJECT_DIR$/Game/pch.cpp" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -72,12 +59,6 @@
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" /> <setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" /> <setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" /> <setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///dummy.cpp" root0="SKIP_HIGHLIGHTING" />
</component> </component>
<component name="MarkdownSettingsMigration"> <component name="MarkdownSettingsMigration">
<option name="stateVersion" value="1" /> <option name="stateVersion" value="1" />
@@ -95,28 +76,28 @@
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent">{ <component name="PropertiesComponent"><![CDATA[{
&quot;keyToString&quot;: { "keyToString": {
&quot;C++ Project.Game.executor&quot;: &quot;Run&quot;, "C++ Project.Game.executor": "Run",
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;, "RunOnceActivity.OpenProjectViewOnStart": "true",
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;, "RunOnceActivity.ShowReadmeOnStart": "true",
&quot;ignore.virus.scanning.warn.message&quot;: &quot;true&quot;, "ignore.virus.scanning.warn.message": "true",
&quot;node.js.detected.package.eslint&quot;: &quot;true&quot;, "node.js.detected.package.eslint": "true",
&quot;node.js.detected.package.tslint&quot;: &quot;true&quot;, "node.js.detected.package.tslint": "true",
&quot;node.js.selected.package.eslint&quot;: &quot;(autodetect)&quot;, "node.js.selected.package.eslint": "(autodetect)",
&quot;node.js.selected.package.tslint&quot;: &quot;(autodetect)&quot;, "node.js.selected.package.tslint": "(autodetect)",
&quot;nodejs_package_manager_path&quot;: &quot;npm&quot;, "nodejs_package_manager_path": "npm",
&quot;settings.editor.selected.configurable&quot;: &quot;CppClangTidyOptionsId&quot;, "settings.editor.selected.configurable": "CppClangTidyOptionsId",
&quot;vue.rearranger.settings.migration&quot;: &quot;true&quot; "vue.rearranger.settings.migration": "true"
}, },
&quot;keyToStringList&quot;: { "keyToStringList": {
&quot;rider.external.source.directories&quot;: [ "rider.external.source.directories": [
&quot;C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\DecompilerCache&quot;, "C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\DecompilerCache",
&quot;C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\SourcesCache&quot;, "C:\\Users\\Bram\\AppData\\Roaming\\JetBrains\\Rider2023.3\\resharper-host\\SourcesCache",
&quot;C:\\Users\\Bram\\AppData\\Local\\Symbols\\src&quot; "C:\\Users\\Bram\\AppData\\Local\\Symbols\\src"
] ]
} }
}</component> }]]></component>
<component name="RunManager"> <component name="RunManager">
<configuration name="Game" type="CppProject" factoryName="C++ Project"> <configuration name="Game" type="CppProject" factoryName="C++ Project">
<configuration_1> <configuration_1>
@@ -191,7 +172,10 @@
<workItem from="1711718669225" duration="530000" /> <workItem from="1711718669225" duration="530000" />
<workItem from="1711734864498" duration="5607000" /> <workItem from="1711734864498" duration="5607000" />
<workItem from="1711819496854" duration="481000" /> <workItem from="1711819496854" duration="481000" />
<workItem from="1711959076645" duration="941000" /> <workItem from="1711959076645" duration="5786000" />
<workItem from="1711987750989" duration="660000" />
<workItem from="1711988744393" duration="1555000" />
<workItem from="1712008689540" duration="3701000" />
</task> </task>
<task id="LOCAL-00001" summary="Rework Tile detection system"> <task id="LOCAL-00001" summary="Rework Tile detection system">
<option name="closed" value="true" /> <option name="closed" value="true" />
@@ -201,7 +185,15 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1711648418492</updated> <updated>1711648418492</updated>
</task> </task>
<option name="localTasksCounter" value="2" /> <task id="LOCAL-00002" summary="Reformat + Basic animation system&#10;General fixes">
<option name="closed" value="true" />
<created>1711960060059</created>
<option name="number" value="00002" />
<option name="presentableId" value="LOCAL-00002" />
<option name="project" value="LOCAL" />
<updated>1711960060059</updated>
</task>
<option name="localTasksCounter" value="3" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
@@ -211,7 +203,8 @@
<component name="VcsManagerConfiguration"> <component name="VcsManagerConfiguration">
<option name="CLEAR_INITIAL_COMMIT_MESSAGE" value="true" /> <option name="CLEAR_INITIAL_COMMIT_MESSAGE" value="true" />
<MESSAGE value="Rework Tile detection system" /> <MESSAGE value="Rework Tile detection system" />
<option name="LAST_COMMIT_MESSAGE" value="Rework Tile detection system" /> <MESSAGE value="Reformat + Basic animation system&#10;General fixes" />
<option name="LAST_COMMIT_MESSAGE" value="Reformat + Basic animation system&#10;General fixes" />
</component> </component>
<component name="XSLT-Support.FileAssociations.UIState"> <component name="XSLT-Support.FileAssociations.UIState">
<expand /> <expand />

View File

@@ -2,7 +2,7 @@
#include "utils.h" #include "utils.h"
#include "../Game/Player.h" #include "../Game/Player.h"
#include "../Game/WorldTile.h" #include "../Game/GridSystem/WorldTile.h"
namespace Collision namespace Collision
{ {

View File

@@ -65,6 +65,7 @@ Rectf::Rectf(float left, float bottom, float width, float height)
, width { width } , width { width }
, height { height } { , 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 } { // Rectf::Rectf(int left, int bottom, int width, int height) : left { (float)left }, bottom { (float)bottom }, width { (float)width }, height { (float)height } {
// } // }

View File

@@ -38,6 +38,7 @@ struct Rectf
{ {
Rectf( ); Rectf( );
explicit Rectf( float left, float bottom, float width, float height ); 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) //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 }; } Point2f BottomLeft() const { return Point2f{ left, bottom }; }

View File

@@ -6,8 +6,7 @@
#include "utils.h" #include "utils.h"
#pragma region OpenGLDrawFunctionality #pragma region OpenGLDrawFunctionality
void utils::SetColor( const Color4f& color ) void utils::SetColor(const Color4f& color) {
{
glColor4f(color.r, color.g, color.b, color.a); glColor4f(color.r, color.g, color.b, color.a);
} }
@@ -16,8 +15,7 @@ void utils::ClearBackground( const Color4f& color ) {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
} }
void utils::DrawPoint( float x, float y, float pointSize ) void utils::DrawPoint(float x, float y, float pointSize) {
{
glPointSize(pointSize); glPointSize(pointSize);
glBegin(GL_POINTS); glBegin(GL_POINTS);
{ {
@@ -26,26 +24,22 @@ void utils::DrawPoint( float x, float y, float pointSize )
glEnd(); glEnd();
} }
void utils::DrawPoint( const Point2f& p, float pointSize ) void utils::DrawPoint(const Point2f& p, float pointSize) {
{
DrawPoint(p.x, p.y, pointSize); DrawPoint(p.x, p.y, pointSize);
} }
void utils::DrawPoints( Point2f *pVertices, int nrVertices, float pointSize ) void utils::DrawPoints(Point2f* pVertices, int nrVertices, float pointSize) {
{
glPointSize(pointSize); glPointSize(pointSize);
glBegin(GL_POINTS); glBegin(GL_POINTS);
{ {
for ( int idx{ 0 }; idx < nrVertices; ++idx ) for (int idx { 0 }; idx < nrVertices; ++idx) {
{
glVertex2f(pVertices[idx].x, pVertices[idx].y); glVertex2f(pVertices[idx].x, pVertices[idx].y);
} }
} }
glEnd(); glEnd();
} }
void utils::DrawLine( float x1, float y1, float x2, float y2, float lineWidth ) void utils::DrawLine(float x1, float y1, float x2, float y2, float lineWidth) {
{
glLineWidth(lineWidth); glLineWidth(lineWidth);
glBegin(GL_LINES); glBegin(GL_LINES);
{ {
@@ -55,13 +49,11 @@ void utils::DrawLine( float x1, float y1, float x2, float y2, float lineWidth )
glEnd(); glEnd();
} }
void utils::DrawLine( const Point2f& p1, const Point2f& p2, float lineWidth ) void utils::DrawLine(const Point2f& p1, const Point2f& p2, float lineWidth) {
{
DrawLine(p1.x, p1.y, p2.x, p2.y, 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); glLineWidth(lineWidth);
glBegin(GL_LINE_LOOP); glBegin(GL_LINE_LOOP);
{ {
@@ -72,8 +64,7 @@ void utils::DrawTriangle(const Point2f& p1, const Point2f& p2, const Point2f& p3
glEnd(); 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); glBegin(GL_TRIANGLES);
{ {
glVertex2f(p1.x, p1.y); glVertex2f(p1.x, p1.y);
@@ -83,10 +74,8 @@ void utils::FillTriangle(const Point2f& p1, const Point2f& p2, const Point2f& p3
glEnd(); glEnd();
} }
void utils::DrawRect( float left, float bottom, float width, float height, float lineWidth ) void utils::DrawRect(float left, float bottom, float width, float height, float lineWidth) {
{ if (width > 0 && height > 0 && lineWidth > 0) {
if (width > 0 && height > 0 && lineWidth > 0)
{
glLineWidth(lineWidth); glLineWidth(lineWidth);
glBegin(GL_LINE_LOOP); 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 ) void utils::DrawRect(const Point2f& bottomLeft, float width, float height, float lineWidth) {
{
DrawRect(bottomLeft.x, bottomLeft.y, width, height, lineWidth); DrawRect(bottomLeft.x, bottomLeft.y, width, height, lineWidth);
} }
void utils::DrawRect( const Rectf& rect, float lineWidth ) void utils::DrawRect(const Rectf& rect, float lineWidth) {
{
DrawRect(rect.left, rect.bottom, rect.width, rect.height, lineWidth); DrawRect(rect.left, rect.bottom, rect.width, rect.height, lineWidth);
} }
void utils::FillRect( float left, float bottom, float width, float height ) void utils::FillRect(float left, float bottom, float width, float height) {
{ if (width > 0 && height > 0) {
if (width > 0 && height > 0)
{
glBegin(GL_POLYGON); 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 ) void utils::FillRect(const Point2f& bottomLeft, float width, float height) {
{
FillRect(bottomLeft.x, bottomLeft.y, width, height); FillRect(bottomLeft.x, bottomLeft.y, width, height);
} }
void utils::FillRect( const Rectf& rect ) void utils::FillRect(const Rectf& rect) {
{
FillRect(rect.left, rect.bottom, rect.width, rect.height); FillRect(rect.left, rect.bottom, rect.width, rect.height);
} }
void utils::DrawEllipse( float centerX, float centerY, float radX, float radY, float lineWidth ) void utils::DrawEllipse(float centerX, float centerY, float radX, float radY, float lineWidth) {
{ if (radX > 0 && radY > 0 && lineWidth > 0) {
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); glLineWidth(lineWidth);
glBegin(GL_LINE_LOOP); 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)); 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 ) void utils::DrawEllipse(const Point2f& center, float radX, float radY, float lineWidth) {
{
DrawEllipse(center.x, center.y, radX, radY, lineWidth); DrawEllipse(center.x, center.y, radX, radY, lineWidth);
} }
void utils::DrawEllipse( const Ellipsef& ellipse, float lineWidth ) void utils::DrawEllipse(const Ellipsef& ellipse, float lineWidth) {
{
DrawEllipse(ellipse.center.x, ellipse.center.y, ellipse.radiusX, ellipse.radiusY, lineWidth); DrawEllipse(ellipse.center.x, ellipse.center.y, ellipse.radiusX, ellipse.radiusY, lineWidth);
} }
void utils::FillEllipse( float centerX, float centerY, float radX, float radY ) void utils::FillEllipse(float centerX, float centerY, float radX, float radY) {
{ if (radX > 0 && radY > 0) {
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); 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)); glVertex2f(centerX + radX * cos(angle), centerY + radY * sin(angle));
} }
} }
@@ -183,20 +158,16 @@ void utils::FillEllipse( float centerX, float centerY, float radX, float radY )
} }
} }
void utils::FillEllipse( const Ellipsef& ellipse ) void utils::FillEllipse(const Ellipsef& ellipse) {
{
FillEllipse(ellipse.center.x, ellipse.center.y, ellipse.radiusX, ellipse.radiusY); FillEllipse(ellipse.center.x, ellipse.center.y, ellipse.radiusX, ellipse.radiusY);
} }
void utils::FillEllipse( const Point2f& center, float radX, float radY ) void utils::FillEllipse(const Point2f& center, float radX, float radY) {
{
FillEllipse(center.x, center.y, radX, 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 ) void utils::DrawArc(float centerX, float centerY, float radX, float radY, float fromAngle, float tillAngle, float lineWidth) {
{ if (fromAngle > tillAngle) {
if ( fromAngle > tillAngle )
{
return; return;
} }
@@ -205,8 +176,7 @@ void utils::DrawArc( float centerX, float centerY, float radX, float radY, float
glLineWidth(lineWidth); glLineWidth(lineWidth);
glBegin(GL_LINE_STRIP); glBegin(GL_LINE_STRIP);
{ {
for ( float angle = fromAngle; angle < tillAngle; angle += dAngle ) for (float angle = fromAngle; angle < tillAngle; angle += dAngle) {
{
glVertex2f(centerX + radX * cos(angle), centerY + radY * sin(angle)); 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));
@@ -215,15 +185,12 @@ void utils::DrawArc( float centerX, float centerY, float radX, float radY, float
} }
void utils::DrawArc( const Point2f& center, float radX, float radY, float fromAngle, float tillAngle, float 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); 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 ) void utils::FillArc(float centerX, float centerY, float radX, float radY, float fromAngle, float tillAngle) {
{ if (fromAngle > tillAngle) {
if ( fromAngle > tillAngle )
{
return; 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) };
@@ -231,8 +198,7 @@ void utils::FillArc( float centerX, float centerY, float radX, float radY, float
glBegin(GL_POLYGON); glBegin(GL_POLYGON);
{ {
glVertex2f(centerX, centerY); glVertex2f(centerX, centerY);
for ( float angle = fromAngle; angle < tillAngle; angle += dAngle ) for (float angle = fromAngle; angle < tillAngle; angle += dAngle) {
{
glVertex2f(centerX + radX * cos(angle), centerY + radY * sin(angle)); 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));
@@ -240,40 +206,33 @@ void utils::FillArc( float centerX, float centerY, float radX, float radY, float
glEnd(); glEnd();
} }
void utils::FillArc( const Point2f& center, float radX, float radY, float fromAngle, float tillAngle ) void utils::FillArc(const Point2f& center, float radX, float radY, float fromAngle, float tillAngle) {
{
FillArc(center.x, center.y, radX, radY, fromAngle, tillAngle); FillArc(center.x, center.y, radX, radY, fromAngle, tillAngle);
} }
void utils::DrawPolygon( const std::vector<Point2f>& vertices, bool closed, float lineWidth ) void utils::DrawPolygon(const std::vector<Point2f>& vertices, bool closed, float lineWidth) {
{
DrawPolygon(vertices.data(), vertices.size(), closed, lineWidth); DrawPolygon(vertices.data(), vertices.size(), closed, lineWidth);
} }
void utils::DrawPolygon( const Point2f* pVertices, size_t nrVertices, bool closed, float lineWidth ) void utils::DrawPolygon(const Point2f* pVertices, size_t nrVertices, bool closed, float lineWidth) {
{
glLineWidth(lineWidth); glLineWidth(lineWidth);
closed ? glBegin(GL_LINE_LOOP) : glBegin(GL_LINE_STRIP); closed ? glBegin(GL_LINE_LOOP) : glBegin(GL_LINE_STRIP);
{ {
for ( size_t idx{ 0 }; idx < nrVertices; ++idx ) for (size_t idx { 0 }; idx < nrVertices; ++idx) {
{
glVertex2f(pVertices[idx].x, pVertices[idx].y); glVertex2f(pVertices[idx].x, pVertices[idx].y);
} }
} }
glEnd(); glEnd();
} }
void utils::FillPolygon( const std::vector<Point2f>& vertices ) void utils::FillPolygon(const std::vector<Point2f>& vertices) {
{
FillPolygon(vertices.data(), vertices.size()); FillPolygon(vertices.data(), vertices.size());
} }
void utils::FillPolygon( const Point2f *pVertices, size_t nrVertices ) void utils::FillPolygon(const Point2f* pVertices, size_t nrVertices) {
{
glBegin(GL_POLYGON); glBegin(GL_POLYGON);
{ {
for ( size_t idx{ 0 }; idx < nrVertices; ++idx ) for (size_t idx { 0 }; idx < nrVertices; ++idx) {
{
glVertex2f(pVertices[idx].x, pVertices[idx].y); glVertex2f(pVertices[idx].x, pVertices[idx].y);
} }
} }
@@ -282,36 +241,30 @@ void utils::FillPolygon( const Point2f *pVertices, size_t nrVertices )
#pragma endregion OpenGLDrawFunctionality #pragma endregion OpenGLDrawFunctionality
#pragma region CollisionFunctionality #pragma region CollisionFunctionality
float utils::GetDistance(float x1, float y1, float x2, float y2) float utils::GetDistance(float x1, float y1, float x2, float y2) {
{
return ( sqrtf(( x2 - x1 ) * ( x2 - x1 ) + ( y2 - y1 ) * ( y2 - y1 )) ); 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); return GetDistance(p1.x, p1.y, p2.x, p2.y);
} }
bool utils::IsPointInRect( const Point2f& p, const Rectf& r ) bool utils::IsPointInRect(const Point2f& p, const Rectf& r) {
{
return ( p.x >= r.left && return ( p.x >= r.left &&
p.x <= r.left + r.width && p.x <= r.left + r.width &&
p.y >= r.bottom && p.y >= r.bottom &&
p.y <= r.bottom + r.height ); p.y <= r.bottom + r.height );
} }
bool utils::IsPointInCircle( const Point2f& p, const Circlef& c ) 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 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 }; float squaredRadius { c.radius * c.radius };
return ( squaredRadius >= squaredDist ); 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 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; return true;
} }
@@ -324,52 +277,42 @@ bool utils::IsOverlapping( const Point2f& a, const Point2f& b, const Rectf& r )
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 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; return false;
} }
// If one rectangle is under the other // 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 false;
} }
return true; 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? // Is center of circle in the rectangle?
if (IsPointInRect(c.center, r)) if (IsPointInRect(c.center, r)) {
{
return true; return true;
} }
// Check line segments // 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; 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; 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; 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 true;
} }
return false; return false;
} }
bool utils::IsOverlapping( const Circlef& c1, const Circlef& c2 ) bool utils::IsOverlapping(const Circlef& c1, const Circlef& c2) {
{
// squared distance between centers // squared distance between centers
float xDistance { c1.center.x - c2.center.x }; float xDistance { c1.center.x - c2.center.x };
float yDistance { c1.center.y - c2.center.y }; float yDistance { c1.center.y - c2.center.y };
@@ -379,53 +322,42 @@ bool utils::IsOverlapping( const Circlef& c1, const Circlef& c2 )
return ( squaredDistance < squaredTouchingDistance ); return ( squaredDistance < squaredTouchingDistance );
} }
bool utils::IsOverlapping( const Point2f& a, const Point2f& b, const Circlef& c ) bool utils::IsOverlapping(const Point2f& a, const Point2f& b, const Circlef& c) {
{
return utils::DistPointLineSegment(c.center, a, b) <= c.radius; return utils::DistPointLineSegment(c.center, a, b) <= c.radius;
} }
bool utils::IsOverlapping( const std::vector<Point2f>& vertices, const Circlef& c ) bool utils::IsOverlapping(const std::vector<Point2f>& vertices, const Circlef& c) {
{
return IsOverlapping(vertices.data(), vertices.size(), 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 // Verify whether one of vertices is in circle
for ( size_t i{ 0 }; i < nrVertices; ++i ) for (size_t i { 0 }; i < nrVertices; ++i) {
{ if (IsPointInCircle(vertices[i], c)) {
if ( IsPointInCircle( vertices[i], c ) )
{
return true; return true;
} }
} }
// Verify whether one of the polygon edges overlaps with circle // Verify whether one of the polygon edges overlaps with circle
for ( size_t i{ 0 }; i < nrVertices; ++i ) for (size_t i { 0 }; i < nrVertices; ++i) {
{ if (DistPointLineSegment(c.center, vertices[i], vertices[( i + 1 ) % nrVertices]) <= c.radius) {
if ( DistPointLineSegment( c.center, vertices[i], vertices[( i + 1 ) % nrVertices] ) <= c.radius )
{
return true; return true;
} }
} }
// No overlapping with edges, verify whether circle is completely inside the polygon // 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 true;
} }
return false; return false;
} }
bool utils::IsPointInPolygon( const Point2f& p, const std::vector<Point2f>& vertices ) bool utils::IsPointInPolygon(const Point2f& p, const std::vector<Point2f>& vertices) {
{
return IsPointInPolygon(p, vertices.data(), vertices.size()); return IsPointInPolygon(p, vertices.data(), vertices.size());
} }
bool utils::IsPointInPolygon( const Point2f& p, const Point2f* vertices, size_t nrVertices ) bool utils::IsPointInPolygon(const Point2f& p, const Point2f* vertices, size_t nrVertices) {
{ if (nrVertices < 2) {
if ( nrVertices < 2 )
{
return false; return false;
} }
// 1. First do a simple test with axis aligned bounding box around the polygon // 1. First do a simple test with axis aligned bounding box around the polygon
@@ -433,27 +365,21 @@ bool utils::IsPointInPolygon( const Point2f& p, const Point2f* vertices, size_t
float xMax { vertices[0].x }; float xMax { vertices[0].x };
float yMin { vertices[0].y }; float yMin { vertices[0].y };
float yMax { vertices[0].y }; float yMax { vertices[0].y };
for ( size_t idx{ 1 }; idx < nrVertices; ++idx ) for (size_t idx { 1 }; idx < nrVertices; ++idx) {
{ if (xMin > vertices[idx].x) {
if (xMin > vertices[idx].x)
{
xMin = vertices[idx].x; xMin = vertices[idx].x;
} }
if (xMax < vertices[idx].x) if (xMax < vertices[idx].x) {
{
xMax = vertices[idx].x; xMax = vertices[idx].x;
} }
if (yMin > vertices[idx].y) if (yMin > vertices[idx].y) {
{
yMin = vertices[idx].y; yMin = vertices[idx].y;
} }
if (yMax < vertices[idx].y) if (yMax < vertices[idx].y) {
{
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; return false;
} }
@@ -465,28 +391,22 @@ bool utils::IsPointInPolygon( const Point2f& p, const Point2f* vertices, size_t
// Count the number of intersection points // Count the number of intersection points
float lambda1 {}, lambda2 {}; float lambda1 {}, lambda2 {};
for ( size_t i{ 0 }; i < nrVertices; ++i ) for (size_t i { 0 }; i < nrVertices; ++i) {
{ if (IntersectLineSegments(vertices[i], vertices[( i + 1 ) % nrVertices], p, p2, lambda1, lambda2)) {
if ( IntersectLineSegments( vertices[i], vertices[( i + 1 ) % nrVertices], p, p2, lambda1, lambda2 ) ) if (lambda1 > 0 && lambda1 <= 1 && lambda2 > 0 && lambda2 <= 1) {
{
if ( lambda1 > 0 && lambda1 <= 1 && lambda2 > 0 && lambda2 <= 1 )
{
++numberOfIntersectionPoints; ++numberOfIntersectionPoints;
} }
} }
} }
if (numberOfIntersectionPoints % 2 == 0) if (numberOfIntersectionPoints % 2 == 0) {
{
return false; return false;
} }
else else {
{
return true; return true;
} }
} }
bool utils::IntersectLineSegments( const Point2f& p1, const Point2f& p2, const Point2f& q1, const Point2f& q2, float& outLambda1, float& outLambda2, float epsilon ) 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 intersecting { false };
Vector2f p1p2 { p1, p2 }; Vector2f p1p2 { p1, p2 };
@@ -496,8 +416,7 @@ bool utils::IntersectLineSegments( const Point2f& p1, const Point2f& p2, const P
float denom = p1p2.CrossProduct(q1q2); float denom = p1p2.CrossProduct(q1q2);
// Don't divide by zero // Don't divide by zero
if ( std::abs( denom ) > epsilon ) if (std::abs(denom) > epsilon) {
{
intersecting = true; intersecting = true;
Vector2f p1q1 { p1, q1 }; Vector2f p1q1 { p1, q1 };
@@ -515,8 +434,7 @@ bool utils::IntersectLineSegments( const Point2f& p1, const Point2f& p2, const P
// Cross product to determine if segments and the line connecting their start points are parallel, // Cross product to determine if segments and the line connecting their start points are parallel,
// if so, than they are on a line // if so, than they are on a line
// if not, then there is no intersection // if not, then there is no intersection
if (std::abs( p1q1.CrossProduct(q1q2) ) > epsilon) if (std::abs(p1q1.CrossProduct(q1q2)) > epsilon) {
{
return false; return false;
} }
@@ -526,23 +444,19 @@ bool utils::IntersectLineSegments( const Point2f& p1, const Point2f& p2, const P
if (utils::IsPointOnLineSegment(p1, q1, q2) || if (utils::IsPointOnLineSegment(p1, q1, q2) ||
utils::IsPointOnLineSegment(p2, q1, q2) || utils::IsPointOnLineSegment(p2, q1, q2) ||
utils::IsPointOnLineSegment(q1, p1, p2) || utils::IsPointOnLineSegment(q1, p1, p2) ||
utils::IsPointOnLineSegment(q2, p1, p2)) utils::IsPointOnLineSegment(q2, p1, p2)) {
{
intersecting = true; intersecting = true;
} }
} }
return intersecting; return intersecting;
} }
bool utils::Raycast( const std::vector<Point2f>& vertices, const Point2f& rayP1, const Point2f& rayP2, HitInfo& hitInfo ) bool utils::Raycast(const std::vector<Point2f>& vertices, const Point2f& rayP1, const Point2f& rayP2, HitInfo& hitInfo) {
{
return Raycast(vertices.data(), vertices.size(), rayP1, rayP2, 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 ) bool utils::Raycast(const Point2f* vertices, const size_t nrVertices, const Point2f& rayP1, const Point2f& rayP2, HitInfo& hitInfo) {
{ if (nrVertices == 0) {
if ( nrVertices == 0 )
{
return false; return false;
} }
@@ -556,8 +470,7 @@ bool utils::Raycast( const Point2f* vertices, const size_t nrVertices, const Poi
r1.height = std::max(rayP1.y, rayP2.y) - r1.bottom; r1.height = std::max(rayP1.y, rayP2.y) - r1.bottom;
// Line-line intersections. // 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 // Consider line segment between 2 consecutive vertices
// (modulo to allow closed polygon, last - first vertice) // (modulo to allow closed polygon, last - first vertice)
Point2f q1 = vertices[( idx + 0 ) % nrVertices]; Point2f q1 = vertices[( idx + 0 ) % nrVertices];
@@ -569,14 +482,11 @@ bool utils::Raycast( const Point2f* vertices, const size_t nrVertices, const Poi
r2.width = std::max(q1.x, q2.x) - r2.left; r2.width = std::max(q1.x, q2.x) - r2.left;
r2.height = std::max(q1.y, q2.y) - r2.bottom; r2.height = std::max(q1.y, q2.y) - r2.bottom;
if ( IsOverlapping( r1, r2 ) ) if (IsOverlapping(r1, r2)) {
{
float lambda1 {}; float lambda1 {};
float lambda2 {}; float lambda2 {};
if ( IntersectLineSegments( rayP1, rayP2, q1, q2, lambda1, lambda2 ) ) if (IntersectLineSegments(rayP1, rayP2, q1, q2, lambda1, lambda2)) {
{ if (lambda1 > 0 && lambda1 <= 1 && lambda2 > 0 && lambda2 <= 1) {
if ( lambda1 > 0 && lambda1 <= 1 && lambda2 > 0 && lambda2 <= 1 )
{
HitInfo linesHitInfo {}; HitInfo linesHitInfo {};
linesHitInfo.lambda = lambda1; linesHitInfo.lambda = lambda1;
linesHitInfo.intersectPoint = Point2f { rayP1.x + ( ( rayP2.x - rayP1.x ) * lambda1 ), rayP1.y + ( ( rayP2.y - rayP1.y ) * lambda1 ) }; linesHitInfo.intersectPoint = Point2f { rayP1.x + ( ( rayP2.x - rayP1.x ) * lambda1 ), rayP1.y + ( ( rayP2.y - rayP1.y ) * lambda1 ) };
@@ -587,8 +497,7 @@ bool utils::Raycast( const Point2f* vertices, const size_t nrVertices, const Poi
} }
} }
if ( hits.size( ) == 0 ) if (hits.size() == 0) {
{
return false; return false;
} }
@@ -604,26 +513,22 @@ bool utils::Raycast( const Point2f* vertices, const size_t nrVertices, const Poi
return true; return true;
} }
bool utils::IsPointOnLineSegment( const Point2f& p, const Point2f& a, const Point2f& b ) bool utils::IsPointOnLineSegment(const Point2f& p, const Point2f& a, const Point2f& b) {
{
Vector2f ap { a, p }, bp { b, p }; Vector2f ap { a, p }, bp { b, p };
// If not on same line, return false // If not on same line, return false
if ( abs( ap.CrossProduct( bp ) ) > 0.001f ) if (abs(ap.CrossProduct(bp)) > 0.001f) {
{
return false; return false;
} }
// Both vectors must point in opposite directions if p is between a and b // 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 false;
} }
return true; return true;
} }
float utils::DistPointLineSegment( const Point2f& p, const Point2f& a, const Point2f& b ) float utils::DistPointLineSegment(const Point2f& p, const Point2f& a, const Point2f& b) {
{
Vector2f ab { a, b }; Vector2f ab { a, b };
Vector2f ap { a, p }; Vector2f ap { a, p };
Vector2f abNorm { ab.Normalized() }; Vector2f abNorm { ab.Normalized() };
@@ -631,15 +536,13 @@ float utils::DistPointLineSegment( const Point2f& p, const Point2f& a, const Po
// If distToA is negative, then the closest point is A // If distToA is negative, then the closest point is A
// return the distance a, p // return the distance a, p
if ( distToA < 0 ) if (distToA < 0) {
{
return ap.Length(); return ap.Length();
} }
// If distToA is > than dist(a,b) then the closest point is B // If distToA is > than dist(a,b) then the closest point is B
// return the distance b, p // return the distance b, p
float distAB { ab.Length() }; float distAB { ab.Length() };
if ( distToA > distAB ) if (distToA > distAB) {
{
return Vector2f { b, p }.Length(); return Vector2f { b, p }.Length();
} }
@@ -648,8 +551,7 @@ float utils::DistPointLineSegment( const Point2f& p, const Point2f& a, const Po
return Vector2f { p.x - intersection.x, p.y - intersection.y }.Length(); 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 // Parameters
// input: // input:
// r: axis aligned bounding box, start and end points of line segment. // r: axis aligned bounding box, start and end points of line segment.
@@ -675,11 +577,11 @@ bool utils::IntersectRectLine(const Rectf& r, const Point2f& p1, const Point2f&
float y1 { ( r.bottom - p1.y ) / ( p2.y - p1.y ) }; float y1 { ( r.bottom - p1.y ) / ( p2.y - p1.y ) };
float y2 { ( r.bottom + r.height - p1.y ) / ( p2.y - p1.y ) }; float y2 { ( r.bottom + r.height - p1.y ) / ( p2.y - p1.y ) };
using std::max; using std::min; using std::max;
using std::min;
float tMin { max(min(x1, x2), min(y1, y2)) }; float tMin { max(min(x1, x2), min(y1, y2)) };
float tMax { min(max(x1, x2), max(y1, y2)) }; float tMax { min(max(x1, x2), max(y1, y2)) };
if (tMin > tMax) if (tMin > tMax) {
{
return false; return false;
} }
intersectMin = tMin; intersectMin = tMin;
@@ -699,31 +601,40 @@ bool utils::RayVsRect(const Point2f& rayOrigin, const Point2f& rayDir, const Rec
Point2f t_near {}; Point2f t_near {};
Point2f t_far {}; Point2f t_far {};
if(std::isnan(t_far.y) || std::isnan(t_far.x)) return false; if (std::isnan(t_far.y) || std::isnan(t_far.x))
if(std::isnan(t_near.y) || std::isnan(t_near.x)) return false; 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.x > t_far.x)
if (t_near.y > t_far.y) std::swap(t_near.y, t_far.y); 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; 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); t_hit_near = std::max(t_near.x, t_near.y);
float t_hit_far = std::min(t_far.x, t_far.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; contactPoint = rayOrigin + rayDir * t_hit_near;
if (t_near.x > t_near.y) { if (t_near.x > t_near.y) {
if (rayDir.x < 0) { if (rayDir.x < 0) {
contactNormal = Point2f { 1, 0 }; contactNormal = Point2f { 1, 0 };
}else { }
else {
contactNormal = Point2f { -1, 0 }; contactNormal = Point2f { -1, 0 };
} }
} else if(t_near.x < t_near.y) { }
else if (t_near.x < t_near.y) {
if (rayDir.y < 0) { if (rayDir.y < 0) {
contactNormal = Point2f { 0, 1 }; contactNormal = Point2f { 0, 1 };
}else { }
else {
contactNormal = Point2f { 0, -1 }; contactNormal = Point2f { 0, -1 };
} }
} }
@@ -731,7 +642,8 @@ bool utils::RayVsRect(const Point2f& rayOrigin, const Point2f& rayDir, const Rec
} }
bool utils::DynamicRectVsRect(const MovingRectf& in, const Rectf& target, Point2f& contactPoint, Point2f& contactNormal, float& contactTime, float dt) { 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 {};
@@ -762,19 +674,23 @@ bool utils::isKeyDown(int keycode) {
} }
return false; return false;
} }
static bool s_KeyStates[256] = {false}; static bool S_PrevKeyStates[256] = { false };
bool utils::isKeyPressed(int keycode) { bool utils::isKeyPressed(int keycode) {
const Uint8* pStates = SDL_GetKeyboardState(nullptr); const Uint8* pStates = SDL_GetKeyboardState(nullptr);
if (pStates != nullptr) { if (pStates == nullptr) {
if(pStates[keycode] && !s_KeyStates[keycode]) {
s_KeyStates[keycode] = true;
}else {
s_KeyStates[keycode] = false;
}
return s_KeyStates[keycode];
}
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) { bool utils::isKeyUp(int keycode) {
const Uint8* pStates = SDL_GetKeyboardState(nullptr); const Uint8* pStates = SDL_GetKeyboardState(nullptr);
@@ -783,6 +699,12 @@ bool utils::isKeyUp(int keycode) {
} }
return false; 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) { bool utils::isMouseDown(int button) {
const Uint32 pStates = SDL_GetMouseState(nullptr, nullptr); const Uint32 pStates = SDL_GetMouseState(nullptr, nullptr);

View File

@@ -111,4 +111,6 @@ namespace utils
bool isMouseDown(int button); bool isMouseDown(int button);
bool isKeyUp(int keycode); bool isKeyUp(int keycode);
Point2f GetMousePos();
} }

View File

@@ -299,6 +299,8 @@
<SDLCheck>true</SDLCheck> <SDLCheck>true</SDLCheck>
<LinkCompiled>true</LinkCompiled> <LinkCompiled>true</LinkCompiled>
</ClCompile> </ClCompile>
<ClCompile Include="Gui\Button.cpp" />
<ClCompile Include="Gui\Screen.cpp" />
<ClCompile Include="Level.cpp"> <ClCompile Include="Level.cpp">
<RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary> <RuntimeLibrary>MultiThreadedDebugDll</RuntimeLibrary>
<BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks> <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
@@ -458,6 +460,8 @@
<ClInclude Include="Game.h"/> <ClInclude Include="Game.h"/>
<ClInclude Include="GridSystem\WorldGridManager.h"/> <ClInclude Include="GridSystem\WorldGridManager.h"/>
<ClInclude Include="GridSystem\WorldTile.h"/> <ClInclude Include="GridSystem\WorldTile.h"/>
<ClInclude Include="Gui\Button.h" />
<ClInclude Include="Gui\Screen.h" />
<ClInclude Include="Level.h"/> <ClInclude Include="Level.h"/>
<ClInclude Include="pch.h"/> <ClInclude Include="pch.h"/>
<ClInclude Include="Player.h"/> <ClInclude Include="Player.h"/>

1
Game/Gui/Button.cpp Normal file
View File

@@ -0,0 +1 @@
#include "Button.h"

19
Game/Gui/Button.h Normal file
View File

@@ -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 };
};

14
Game/Gui/Screen.cpp Normal file
View File

@@ -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);
}

26
Game/Gui/Screen.h Normal file
View File

@@ -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 };
};

View File

@@ -7,6 +7,7 @@
#include "utils.h" #include "utils.h"
#include "WorldLevel.h" #include "WorldLevel.h"
#include "Animations/Animation.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 }) { 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; m_ContactMap[Collision::CollisionDirection::Top] = nullptr;
@@ -39,7 +40,7 @@ void Player::ProcessImGui() {
ImGui::Begin("Collision Info", nullptr, ImGuiWindowFlags_AlwaysAutoResize); ImGui::Begin("Collision Info", nullptr, ImGuiWindowFlags_AlwaysAutoResize);
ImGui::Text("is Grounded: %s", m_Grounded ? "true" : "false"); ImGui::Text("is Grounded: %s", m_Grounded ? "true" : "false");
ImGui::Text("Did just dig right: %s", m_DidJustDigRight ? "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::Text("Is Key Up H: %s", test ? "true" : "false");
ImGui::Checkbox("Draw Collision Rect", &m_DrawCollisionRect); ImGui::Checkbox("Draw Collision Rect", &m_DrawCollisionRect);

View File

@@ -9,8 +9,10 @@
#include "Collision.h" #include "Collision.h"
#include "colors.h" #include "colors.h"
#include "utils.h" #include "utils.h"
#include "GridSystem/WorldTile.h"
class GroundTileType;
WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera), WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera),
m_gridManager(WorldGridManager()), m_gridManager(WorldGridManager()),
m_player(Player { Point2f { 0, 100 }, TextureManager::GetInstance() }), m_player(Player { Point2f { 0, 100 }, TextureManager::GetInstance() }),
@@ -33,7 +35,7 @@ WorldLevel::WorldLevel(Camera* camera, Rectf viewport): Level(camera),
//AIR //AIR
break; break;
default: default:
std::cout << "??" << std::endl; std::cout << "??" << '\n';
} }
m_gridManager.SetTileAtIndex(x, y, new WorldTile { pos, type, TextureManager::GetInstance() }); 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) { for (size_t x { 0 }; x < WORLD_WIDTH; ++x) {
m_gridManager.GetTileAtIndex(x, 0)->SetTileType(Tiles::AIR); 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() { WorldLevel::~WorldLevel() {
//delete m_pTextTexture; //delete m_pTextTexture;
@@ -117,6 +121,10 @@ void WorldLevel::Draw() const {
m_player.Draw(); m_player.Draw();
m_pCamera->EndRendering(); m_pCamera->EndRendering();
utils::FillRect(utils::GetMousePos(), 10, 10);
m_screen.Draw();
} }
void WorldLevel::MouseMove(const Point2f& mousePos) { void WorldLevel::MouseMove(const Point2f& mousePos) {
m_mousePos = mousePos; m_mousePos = mousePos;

View File

@@ -3,8 +3,8 @@
#include "Level.h" #include "Level.h"
#include "Player.h" #include "Player.h"
#include "utils.h" #include "utils.h"
#include "WorldLevel.h"
#include "GridSystem/WorldGridManager.h" #include "GridSystem/WorldGridManager.h"
#include "Gui/Screen.h"
class WorldLevel : public Level class WorldLevel : public Level
@@ -35,6 +35,8 @@ private:
Rectf m_viewport; Rectf m_viewport;
Screen m_screen;
// ImGui Vars // ImGui Vars
bool m_ShowTextureManagerWindow { false }; bool m_ShowTextureManagerWindow { false };
bool m_ShowCameraWindow { false }; bool m_ShowCameraWindow { false };

View File

@@ -11,7 +11,7 @@ int SDL_main(int argv, char** args) {
StartHeapControl(); 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(); pGame->Run();
delete pGame; delete pGame;