Skip to content

Instantly share code, notes, and snippets.

@Koltonix
Created December 6, 2021 17:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Koltonix/a78f1789995deb6ca591b1acaba4ecd1 to your computer and use it in GitHub Desktop.
Save Koltonix/a78f1789995deb6ca591b1acaba4ecd1 to your computer and use it in GitHub Desktop.
#include "Conversions.h"
#include "Geometry.h"
#include "Line.h"
#include "InputSystem.h"
#include <math.h>
#include <SDL.h>
void Line::Start()
{
}
void Line::Update()
{
}
void Line::Render(SDL_Renderer* renderer)
{
SDL_SetRenderDrawColor(renderer, m_lineColour.r, m_lineColour.g, m_lineColour.b, m_lineColour.a);
RenderLine(renderer, m_start, m_end);
SDL_SetRenderDrawColor(renderer, m_ArrowColour.r, m_ArrowColour.g, m_ArrowColour.b, m_ArrowColour.a);
std::vector<Vector2> arrow = ConstructArrow(m_arrowDistance);
RenderLines(renderer, arrow);
arrow = ConstructArrow(m_arrowDistance + .05f);
RenderLines(renderer, arrow);
}
void Line::SetLocked()
{
SetLineAlpha(m_lockedAlpha);
SetArrowAlpha(m_lockedAlpha);
}
void Line::SetUnlocked()
{
SetLineAlpha(m_unlockedAlpha);
SetArrowAlpha(m_unlockedAlpha);
}
void Line::RenderLine(SDL_Renderer* renderer, Vector2 startPos, Vector2 endPos)
{
Vector2 startWorld = ScreenToWorld(startPos);
Vector2 endWorld = ScreenToWorld(endPos);
SDL_RenderDrawLineF(renderer, startWorld.x, startWorld.y, endWorld.x, endWorld.y);
}
void Line::RenderLines(SDL_Renderer* renderer, std::vector<Vector2> points)
{
Vector2 offset = GetPositionOffset();
for (int i = 0; i < points.size() - 1; i++)
{
Vector2 start = ScreenToWorld(points[i]);
Vector2 end = ScreenToWorld(points[i + 1]);
SDL_RenderDrawLineF(renderer, start.x, start.y, end.x, end.y);
}
}
Vector2 Line::ScreenToWorld(Vector2 pos)
{
Vector2 offset = GetPositionOffset();
return Vector2(pos.x + offset.x, offset.y - pos.y);
}
Vector2 Line::GetPointOnLine(float t)
{
return m_start + ((m_end - m_start) * t);
}
std::vector<Vector2> Line::ConstructArrow(float pos)
{
std::vector<Vector2> arrow = std::vector<Vector2>();
Vector2 t = GetPointOnLine(pos);
Vector2 n = (m_end - m_start).Normalise();
Vector2 right = RotateAroundPoint(n, m_arrowAngle) * m_arrowLength + t;
Vector2 left = RotateAroundPoint(n, -m_arrowAngle) * m_arrowLength + t;
return
{
left,
t,
right
};
}
#pragma once
#include "Colour.h"
#include "GameObject.h"
#include <vector>
class Line : public GameObject
{
public:
Line(Vector2 startPos, Vector2 endPos) : m_start(startPos), m_end(endPos) {};
void Start() override;
void Update() override;
void Render(SDL_Renderer* renderer) override;
void SetLineColour(Colour colour) { m_lineColour = colour; }
void SetLineAlpha(float a) { m_lineColour = Colour(m_lineColour.r, m_lineColour.g, m_lineColour.b, a); }
void SetArrowColour(Colour colour) { m_ArrowColour = colour; }
void SetArrowAlpha(float a) { m_ArrowColour = Colour(m_ArrowColour.r, m_ArrowColour.g, m_ArrowColour.b, a); }
void SetLocked();
void SetUnlocked();
Vector2 GetDeleteCentre() { return GetPointOnLine(m_arrowDistance); };
private:
void RenderLine(SDL_Renderer* renderer, Vector2 starPost, Vector2 endPos);
void RenderLines(SDL_Renderer* renderer, std::vector<Vector2> points);
Vector2 ScreenToWorld(Vector2 pos);
Vector2 GetPointOnLine(float t);
std::vector<Vector2> ConstructArrow(float pos);
public:
Vector2 m_start = Vector2();
Vector2 m_end = Vector2();
private:
// Degrees.
float m_arrowAngle = 150.0f;
float m_arrowLength = 5.0f;
// 0.0f-1.0f;
float m_arrowDistance = 0.75f;
Colour m_lineColour = Colour::White();
Colour m_ArrowColour = Colour(192, 192, 192, 255);
float m_lockedAlpha = 32;
float m_unlockedAlpha = 192;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment