Skip to content

Commit

Permalink
start transform stack stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy S. Postelnek committed Apr 24, 2020
1 parent af2f278 commit b509ead
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
51 changes: 51 additions & 0 deletions include/modules/graphics/graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,25 @@

#include "common/mmath.h"

#if defined (_3DS)
#define RENDERER_NAME "OpenGL ES"
#define RENDERER_VERSION "1.1"
#define RENDERER_VENDOR "Digital Media Professionals Inc."
#define RENDERER_DEVICE "DMP PICA200"
#elif defined (__SWITCH__)
#define RENDERER_NAME "OpenGL"
#define RENDERER_VERSION "4.5"
#define RENDERER_VENDOR "NVIDIA"
#define RENDERER_DEVICE "Tegra X1"
#endif

namespace love
{
class Graphics : public Module
{
public:
static const size_t MAX_USER_STACK_DEPTH = 128;

enum DrawMode
{
DRAW_LINE,
Expand Down Expand Up @@ -52,6 +66,28 @@ namespace love
float y;
};

struct RendererInfo
{
std::string name = RENDERER_NAME;
std::string version = RENDERER_VERSION;
std::string vendor = RENDERER_VENDOR;
std::string device = RENDERER_DEVICE;
};

struct TransformState
{
float offsetX = 0.0f;
float offsetY = 0.0f;

float rotation = 0.0f;

float scalarX = 1.0f;
float scalarY = 1.0f;
};

std::vector<TransformState> transformStack;
void Transform(DrawArgs & args);

static constexpr float MIN_DEPTH = 1.0f/16384.0f;
static inline float CURRENT_DEPTH = 0;

Expand Down Expand Up @@ -86,6 +122,18 @@ namespace love

Rect GetScissor();

void Push();

void Translate(float offsetX, float offsetY);

void Rotate(float rotation);

void Scale(float scalarX, float ScalarY);

void Pop();

RendererInfo GetRendererInfo();

/* Objects */

Image * NewImage(const std::string & path);
Expand Down Expand Up @@ -143,11 +191,14 @@ namespace love
void SetMode();

StrongReference<Font> defaultFont;
RendererInfo rendererInfo;

static inline std::map<std::string, Graphics::DrawMode> m_modes =
{
{ "line", DrawMode::DRAW_LINE },
{ "fill", DrawMode::DRAW_FILL }
};

bool isPushed = false;
};
}
73 changes: 73 additions & 0 deletions source/modules/graphics/graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ Graphics::Graphics()
this->states.reserve(10);
this->states.push_back(DisplayState());

this->transformStack.reserve(16);
this->transformStack.push_back(TransformState());

auto window = Module::GetInstance<Window>(M_WINDOW);

if (window != nullptr)
Expand Down Expand Up @@ -89,6 +92,48 @@ void Graphics::GetDimensions(int * width, int * height)
*height = size.second;
}

void Graphics::Push()
{
if (this->transformStack.size() == MAX_USER_STACK_DEPTH)
throw Exception("Maximum stack depth reached (more pushes than pops?)");

this->transformStack.push_back(transformStack.back());
}

void Graphics::Translate(float offsetX, float offsetY)
{
auto & transform = this->transformStack.back();

transform.offsetX = offsetX;
transform.offsetY = offsetY;
}

void Graphics::Rotate(float rotation)
{
this->transformStack.back().rotation = rotation;
}

void Graphics::Scale(float scalarX, float scalarY)
{
auto & transform = this->transformStack.back();

transform.scalarX = scalarX;
transform.scalarY = scalarY;
}

void Graphics::Pop()
{
if (this->transformStack.size() < 1)
throw Exception("Minimum stack depth reached (more pops than pushes?)");

this->transformStack.pop_back();
}

Graphics::RendererInfo Graphics::GetRendererInfo()
{
return this->rendererInfo;
}

/* Objects */

Image * Graphics::NewImage(const std::string & path)
Expand Down Expand Up @@ -196,6 +241,34 @@ Font * Graphics::GetFont()

/* End Font */

void Graphics::Transform(DrawArgs & args)
{
if (this->transformStack.empty())
return;

auto & transform = this->transformStack.back();

/* Translate */
args.x += transform.offsetX;
args.y += transform.offsetY;

/* Scale */
args.x *= transform.scalarX;
args.y *= transform.scalarY;

args.scalarX += transform.scalarX;
args.scalarY += transform.scalarY;

/* Rotate */
if (transform.rotation != 0)
{
args.x = args.x * cos(transform.rotation) - args.y * sin(transform.rotation);
args.y = args.x * sin(transform.rotation) + args.y * cos(transform.rotation);

args.r += transform.rotation;
}
}

void Graphics::AdjustColor(Color * in)
{
float mul = 255.0f;
Expand Down

0 comments on commit b509ead

Please sign in to comment.