Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3D perspective drawing mode #316

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/SSVOpenHexagon/Components/CCustomWall.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class CCustomWall

void update(HexagonGame& mHexagonGame, ssvu::FT mFT);
void draw(HexagonGame& mHexagonGame);
void draw3D(HexagonGame& mHexagonGame, const sf::Color& mColor);

[[gnu::always_inline, nodiscard]] bool isOverlapping(
const sf::Vector2f& mPoint) const noexcept
Expand Down
1 change: 1 addition & 0 deletions include/SSVOpenHexagon/Components/CCustomWallManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ class CCustomWallManager
void cleanup();
void clear();
void draw(HexagonGame& hexagonGame);
void draw3D(HexagonGame& hexagonGame, const sf::Color& mColor);

template <typename F>
void forCustomWalls(F&& f)
Expand Down
28 changes: 28 additions & 0 deletions include/SSVOpenHexagon/Components/CPlayer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class CPlayer
sf::Vector2f startPos;
sf::Vector2f pos;
sf::Vector2f lastPos;
std::array<sf::Vector2f, 3> vertexPositions;
std::vector<sf::Vector2f> pivotVertexes;

float hue;
float angle;
Expand All @@ -39,8 +41,16 @@ class CPlayer
Ticker swapBlinkTimer;
Ticker deadEffectTimer;

float pivotRadius;
static constexpr float pivotBorderThickness{5.f};

void drawCommon(HexagonGame& mHexagonGame);
void drawPivot(HexagonGame& mHexagonGame, const sf::Color& mCapColor);
void drawPivot3D(HexagonGame& mHexagonGame, const sf::Color& mWallColor,
const sf::Color& mCapColor);
void drawDeathEffect(HexagonGame& mHexagonGame);
void drawDeathEffect3D(
HexagonGame& mHexagonGame, const sf::Color& mWallColors);

public:
CPlayer(const sf::Vector2f& mPos, const float swapCooldown) noexcept;
Expand All @@ -53,6 +63,17 @@ class CPlayer

[[nodiscard]] float getPlayerAngle() const noexcept;

[[gnu::always_inline, nodiscard]] const std::vector<sf::Vector2f>&
getPivotVertexes() const noexcept
{
return pivotVertexes;
}

[[gnu::always_inline, nodiscard]] float getPivotRadius() const noexcept
{
return pivotRadius;
}

void setPlayerAngle(const float newAng) noexcept;
void playerSwap(HexagonGame& mHexagonGame, bool mPlaySound);

Expand All @@ -63,6 +84,13 @@ class CPlayer
void updatePosition(const HexagonGame& mHexagonGame, const ssvu::FT mFT);

void draw(HexagonGame& mHexagonGame, const sf::Color& mCapColor);
void draw3D(HexagonGame& mHexagonGame, const sf::Color& mWallColor,
const sf::Color& mCapColor);

void setSides(unsigned int mSideNumber) noexcept
{
pivotVertexes.resize(mSideNumber);
}

[[nodiscard]] bool push(const HexagonGame& mHexagonGame, const CWall& wall,
const sf::Vector2f& mCenterPos, ssvu::FT mFT);
Expand Down
30 changes: 22 additions & 8 deletions include/SSVOpenHexagon/Components/CWall.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
#include "SSVOpenHexagon/Utils/PointInPolygon.hpp"

#include <SSVStart/Utils/Vector2.hpp>
#include <SFML/System/Vector2.hpp>

#include <array>

Expand All @@ -28,26 +27,41 @@ class CWall
float hueMod;
bool killed;

void moveTowardsCenter(HexagonGame& mHexagonGame,
const sf::Vector2f& mCenterPos, const ssvu::FT mFT);
void moveCurve(const sf::Vector2f& mCenterPos, const ssvu::FT mFT);

unsigned int getVertexScreenPortion(const sf::Vector2f& mVertex,
const sf::Vector2f& mCenterPos, const unsigned int mSides);
void calcIntersectionPoint(const HexagonGame& mHexagonGame,
sf::Vector2f& mIntersection,
const std::vector<sf::Vector2f>& mPivotVertexes,
const sf::Vector2f& mCenterPos, const sf::Vector2f& wallVertexOne,
const sf::Vector2f& wallVertexTwo);

void draw3DSides(HexagonGame& mHexagonGame, const sf::Vector2f& mOffset3D,
const sf::Color& mColor);
void draw3DSide(HexagonGame& mHexagonGame, const sf::Vector2f& mOffset3D,
const sf::Color& mColor, const sf::Vector2f& mVertexOne,
const sf::Vector2f& mVertexTwo);

public:
CWall(HexagonGame& mHexagonGame, const sf::Vector2f& mCenterPos, int mSide,
float mThickness, float mDistance, const SpeedData& mSpeed,
const SpeedData& mCurve);

void update(const HexagonGame& mHexagonGame, const ssvu::FT mFT);

void moveTowardsCenter(HexagonGame& mHexagonGame,
const sf::Vector2f& mCenterPos, const ssvu::FT mFT);
void update(HexagonGame& mHexagonGame, const sf::Vector2f& mCenterPos,
const ssvu::FT mFT);

[[gnu::always_inline]] void moveVertexAlongCurve(sf::Vector2f& mVertex,
const sf::Vector2f& mCenterPos, const ssvu::FT mFT) const
{
ssvs::rotateRadAround(mVertex, mCenterPos, curve.speed / 60.f * mFT);
}

void moveCurve(const HexagonGame& mHexagonGame,
const sf::Vector2f& mCenterPos, const ssvu::FT mFT);

void draw(HexagonGame& mHexagonGame);
void draw3D(HexagonGame& mHexagonGame, const CPlayer& mPlayer,
const sf::Vector2f& mCenterPos, const sf::Color& mColor);

void setHueMod(float mHueMod) noexcept;

Expand Down
56 changes: 44 additions & 12 deletions include/SSVOpenHexagon/Core/HexagonGame.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,57 @@ class HexagonGame
void updateKeyIcons();

// Draw methods
void draw();
public:
Utils::FastVertexVector<sf::PrimitiveType::Quads> wallQuads;
Utils::FastVertexVector<sf::PrimitiveType::Triangles> playerTris;
Utils::FastVertexVector<sf::PrimitiveType::Quads> capQuads;
Utils::FastVertexVector<sf::PrimitiveType::Triangles> capTris;
Utils::FastVertexVector<sf::PrimitiveType::Quads> wallQuads3D;
Utils::FastVertexVector<sf::PrimitiveType::Triangles> playerTris3D;
sf::Vector2f offset3D;

// Gameplay methods
void incrementDifficulty();
void sideChange(unsigned int mSideNumber);
[[nodiscard]] const sf::Vector2f& get3DOffset() noexcept
{
return offset3D;
}

// Draw methods
private:
static constexpr float piAndHalf{ssvu::piHalf * 3.f};

void drawSetup();
void draw2D();
void drawProjections();
void draw3D();
void drawWrapup();
void draw();
void drawText_TimeAndStatus(const sf::Color& offsetColor);
void drawText_Message(const sf::Color& offsetColor);
void drawText();
void drawKeyIcons();

std::function<void()> drawFunc;
void setDrawFunc()
{
if(!Config::get3D())
{
drawFunc = [this] { draw2D(); };
return;
}

if(levelData->_3DDrawingMode)
{
drawFunc = [this] { draw3D(); };
}
else
{
drawFunc = [this] { drawProjections(); };
}
}

// Gameplay methods
void incrementDifficulty();
void sideChange(unsigned int mSideNumber);

// Data-related methods
void setLevelData(const LevelData& mLevelData, bool mMusicFirstPlay);
void playLevelMusic();
Expand Down Expand Up @@ -322,7 +361,6 @@ class HexagonGame

void invalidateScore(const std::string& mReason);


template <typename F>
Utils::LuaMetadataProxy addLuaFn(const std::string& name, F&& f)
{
Expand Down Expand Up @@ -366,12 +404,6 @@ class HexagonGame
}

public:
Utils::FastVertexVector<sf::PrimitiveType::Quads> wallQuads;
Utils::FastVertexVector<sf::PrimitiveType::Triangles> playerTris;
Utils::FastVertexVector<sf::PrimitiveType::Triangles> capTris;
Utils::FastVertexVector<sf::PrimitiveType::Quads> wallQuads3D;
Utils::FastVertexVector<sf::PrimitiveType::Triangles> playerTris3D;

MenuGame* mgPtr;

HexagonGame(Steam::steam_manager& mSteamManager,
Expand Down
1 change: 1 addition & 0 deletions include/SSVOpenHexagon/Data/LevelData.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class LevelData
packPath + ssvuj::getExtr<std::string>(root, "luaFile", "nullLuaPath")};
std::vector<float> difficultyMults{
ssvuj::getExtr<std::vector<float>>(root, "difficultyMults", {})};
bool _3DDrawingMode{ssvuj::getExtr<bool>(root, "3DMode", false)};

LevelData(const ssvuj::Obj& mRoot, const ssvufs::Path& mPackPath,
const std::string& mPackId)
Expand Down
20 changes: 18 additions & 2 deletions include/SSVOpenHexagon/Utils/Utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,35 @@ inline void uppercasify(std::string& s)
return s;
}

inline float getFontHeight(sf::Text& font)
[[nodiscard, gnu::pure]] inline float getFontHeight(sf::Text& font)
{
font.setString("A");
return ssvs::getGlobalHeight(font);
}

inline float getFontHeight(sf::Text& font, const unsigned int charSize)
[[nodiscard, gnu::pure]] inline float getFontHeight(
sf::Text& font, const unsigned int charSize)
{
font.setCharacterSize(charSize);
font.setString("A");
return ssvs::getGlobalHeight(font);
}

bool getLinesIntersection(sf::Vector2f& mIntersection, const sf::Vector2f& l1p1,
const sf::Vector2f& l1p2, const sf::Vector2f& l2p1,
const sf::Vector2f& l2p2);

[[nodiscard, gnu::pure]] inline float fastSqrt(const float& n)
{
static union
{
int i;
float f;
} u;
u.i = 0x5F375A86 - (*(int*)&n >> 1);
return (int(3) - n * u.f * u.f) * n * u.f * 0.5f;
}

[[nodiscard, gnu::pure]] inline float getSaturated(float mValue)
{
return std::max(0.f, std::min(1.f, mValue));
Expand Down
27 changes: 27 additions & 0 deletions src/SSVOpenHexagon/Components/CCustomWall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,33 @@ void CCustomWall::draw(HexagonGame& mHexagonGame)
vertexPositions[3], vertexColors[3]);
}

void CCustomWall::draw3D(HexagonGame& mHexagonGame, const sf::Color& mColor)
{
draw(mHexagonGame);

const sf::Vector2f& offset3D{mHexagonGame.get3DOffset()};
mHexagonGame.wallQuads3D.reserve_more(4 * 4);

for(unsigned int i{0}, j{3}; i < 4u; j = i++)
{
mHexagonGame.wallQuads3D.unsafe_emplace_back(
vertexPositions[i], mColor);

mHexagonGame.wallQuads3D.unsafe_emplace_back(
vertexPositions[j], mColor);

mHexagonGame.wallQuads3D.unsafe_emplace_back(
sf::Vector2f{vertexPositions[j].x + offset3D.x,
vertexPositions[j].y + offset3D.y},
mColor);

mHexagonGame.wallQuads3D.unsafe_emplace_back(
sf::Vector2f{vertexPositions[i].x + offset3D.x,
vertexPositions[i].y + offset3D.y},
mColor);
}
}

void CCustomWall::update(HexagonGame& mHexagonGame, ssvu::FT mFT)
{
(void)mHexagonGame;
Expand Down
Loading