-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathExtensions.cpp
More file actions
50 lines (40 loc) · 1.7 KB
/
Extensions.cpp
File metadata and controls
50 lines (40 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "package.h"
//---------------------------------------------------------------------------------
// Written by Terence J. Grant - tjgrant [at] tatewake [dot] com
// Find the full tutorial at: http://gamedev.tutsplus.com/series/
//----------------------------------------------------------------------------------
float Extensions::toAngle(const tVector2f& vector)
{
return (float)atan2f(vector.y, vector.x);
}
tVector2f Extensions::scaleTo(const tVector2f& vector, float length)
{
return vector * (length / vector.length());
}
tPoint2f Extensions::toPoint(const tVector2f& vector)
{
return tPoint2f((int)vector.x, (int)vector.y);
}
float Extensions::nextFloat(float minValue, float maxValue)
{
return (float)tMath::random() * (maxValue - minValue) + minValue;
}
tVector2f Extensions::nextVector2(float minLength, float maxLength)
{
float theta = tMath::random() * 2.0f * tMath::PI;
float length = nextFloat(minLength, maxLength);
return tVector2f(length * (float)cosf(theta), length * (float)sinf(theta));
}
tColor4f Extensions::colorLerp(const tColor4f& a, const tColor4f& b, float alpha)
{
return tColor4f(tMath::mix(a.r, b.r, alpha),
tMath::mix(a.g, b.g, alpha),
tMath::mix(a.b, b.b, alpha),
tMath::mix(a.a, b.a, alpha));
}
void Extensions::drawLine(tSpriteBatch* spriteBatch, const tVector2f& start, const tVector2f& end, const tColor4f& color, float thickness)
{
tVector2f delta = end - start;
spriteBatch->draw(0, Art::getInstance()->getPixel(), tPoint2f((int32_t)start.x, (int32_t)start.y), tOptional<tRectf>(), color,
toAngle(delta), tPoint2f(0, 0), tVector2f(delta.length(), thickness));
}