Updated Graphics 2D

pull/113/head
Javidx9 6 years ago committed by GitHub
parent 3c748dde53
commit 87b9db3f7e
  1. 19
      OneLoneCoder_PGE_ExtensionTestGFX2D.cpp
  2. 33
      olcPGEX_Graphics2D.h

@ -46,7 +46,7 @@
Author Author
~~~~~~ ~~~~~~
David Barr, aka javidx9, ©OneLoneCoder 2018 David Barr, aka javidx9, ©OneLoneCoder 2018
*/ */
// Include the olcPixelGameEngine // Include the olcPixelGameEngine
@ -54,6 +54,7 @@
#include "olcPixelGameEngine.h" #include "olcPixelGameEngine.h"
// To use an extension, just include it // To use an extension, just include it
#define OLC_PGE_GRAPHICS2D
#include "olcPGEX_Graphics2D.h" #include "olcPGEX_Graphics2D.h"
class TestExtension : public olc::PixelGameEngine class TestExtension : public olc::PixelGameEngine
@ -70,7 +71,7 @@ public:
for (int i = 0; i < 16; i++) for (int i = 0; i < 16; i++)
listEvents.push_back(""); listEvents.push_back("");
spr = new olc::Sprite("logo_long.png"); spr = new olc::Sprite("new_piskel.png");
return true; return true;
} }
@ -90,8 +91,8 @@ public:
DrawCircle(96, 32, 30); // Circle DrawCircle(96, 32, 30); // Circle
float mx = GetMouseX(); float mx = (float)GetMouseX();
float my = GetMouseY(); float my = (float)GetMouseY();
float px1 = mx - 32, px2 = mx - 96; float px1 = mx - 32, px2 = mx - 96;
float py1 = my - 32, py2 = my - 32; float py1 = my - 32, py2 = my - 32;
@ -101,8 +102,8 @@ public:
py1 = 22.0f * (py1 * pr1) + 32.0f; py1 = 22.0f * (py1 * pr1) + 32.0f;
px2 = 22.0f * (px2 * pr2) + 96.0f; px2 = 22.0f * (px2 * pr2) + 96.0f;
py2 = 22.0f * (py2 * pr2) + 32.0f; py2 = 22.0f * (py2 * pr2) + 32.0f;
FillCircle(px1, py1, 8, olc::CYAN); FillCircle((int32_t)px1, (int32_t)py1, 8, olc::CYAN);
FillCircle(px2, py2, 8, olc::CYAN); FillCircle((int32_t)px2, (int32_t)py2, 8, olc::CYAN);
DrawLine(10, 70, 54, 70); // Lines DrawLine(10, 70, 54, 70); // Lines
DrawLine(54, 70, 70, 54); DrawLine(54, 70, 70, 54);
@ -136,6 +137,8 @@ public:
nLog++; nLog++;
} }
std::string notes = "CDEFGAB";
// Test Text scaling and colours // Test Text scaling and colours
DrawString(0, 360, "Text Scale = 1", olc::WHITE, 1); DrawString(0, 360, "Text Scale = 1", olc::WHITE, 1);
@ -168,6 +171,8 @@ public:
// Use extension to draw sprite with transform applied // Use extension to draw sprite with transform applied
olc::GFX2D::DrawSprite(spr, t1); olc::GFX2D::DrawSprite(spr, t1);
DrawSprite((int32_t)mx, (int32_t)my, spr, 4);
return true; return true;
} }
@ -181,4 +186,4 @@ int main()
demo.Start(); demo.Start();
return 0; return 0;
} }

@ -3,7 +3,7 @@
+-------------------------------------------------------------+ +-------------------------------------------------------------+
| OneLoneCoder Pixel Game Engine Extension | | OneLoneCoder Pixel Game Engine Extension |
| Advanced 2D Rendering - v0.3 | | Advanced 2D Rendering - v0.4 |
+-------------------------------------------------------------+ +-------------------------------------------------------------+
What is this? What is this?
@ -15,7 +15,7 @@
License (OLC-3) License (OLC-3)
~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~
Copyright 2018 OneLoneCoder.com Copyright 2018 - 2019 OneLoneCoder.com
Redistribution and use in source and binary forms, with or without Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions modification, are permitted provided that the following conditions
@ -56,7 +56,7 @@
Author Author
~~~~~~ ~~~~~~
David Barr, aka javidx9, ©OneLoneCoder 2018 David Barr, aka javidx9, ©OneLoneCoder 2019
*/ */
/* /*
@ -99,6 +99,8 @@ namespace olc
inline void Scale(float sx, float sy); inline void Scale(float sx, float sy);
// Append a shear operation (sx, sy) to this transform // Append a shear operation (sx, sy) to this transform
inline void Shear(float sx, float sy); inline void Shear(float sx, float sy);
inline void Perspective(float ox, float oy);
// Calculate the Forward Transformation of the coordinate (in_x, in_y) -> (out_x, out_y) // Calculate the Forward Transformation of the coordinate (in_x, in_y) -> (out_x, out_y)
inline void Forward(float in_x, float in_y, float &out_x, float &out_y); inline void Forward(float in_x, float in_y, float &out_x, float &out_y);
// Calculate the Inverse Transformation of the coordinate (in_x, in_y) -> (out_x, out_y) // Calculate the Inverse Transformation of the coordinate (in_x, in_y) -> (out_x, out_y)
@ -121,7 +123,8 @@ namespace olc
} }
#ifdef OLC_PGE_GRAPHICS2D
#undef OLC_PGE_GRAPHICS2D
namespace olc namespace olc
{ {
@ -250,16 +253,37 @@ namespace olc
Multiply(); Multiply();
} }
void olc::GFX2D::Transform2D::Perspective(float ox, float oy)
{
// Construct Translate Matrix
matrix[2][0][0] = 1.0f; matrix[2][1][0] = 0.0f; matrix[2][2][0] = 0.0f;
matrix[2][0][1] = 0.0f; matrix[2][1][1] = 1.0f; matrix[2][2][1] = 0.0f;
matrix[2][0][2] = ox; matrix[2][1][2] = oy; matrix[2][2][2] = 1.0f;
Multiply();
}
void olc::GFX2D::Transform2D::Forward(float in_x, float in_y, float &out_x, float &out_y) void olc::GFX2D::Transform2D::Forward(float in_x, float in_y, float &out_x, float &out_y)
{ {
out_x = in_x * matrix[nSourceMatrix][0][0] + in_y * matrix[nSourceMatrix][1][0] + matrix[nSourceMatrix][2][0]; out_x = in_x * matrix[nSourceMatrix][0][0] + in_y * matrix[nSourceMatrix][1][0] + matrix[nSourceMatrix][2][0];
out_y = in_x * matrix[nSourceMatrix][0][1] + in_y * matrix[nSourceMatrix][1][1] + matrix[nSourceMatrix][2][1]; out_y = in_x * matrix[nSourceMatrix][0][1] + in_y * matrix[nSourceMatrix][1][1] + matrix[nSourceMatrix][2][1];
float out_z = in_x * matrix[nSourceMatrix][0][2] + in_y * matrix[nSourceMatrix][1][2] + matrix[nSourceMatrix][2][2];
if (out_z != 0)
{
out_x /= out_z;
out_y /= out_z;
}
} }
void olc::GFX2D::Transform2D::Backward(float in_x, float in_y, float &out_x, float &out_y) void olc::GFX2D::Transform2D::Backward(float in_x, float in_y, float &out_x, float &out_y)
{ {
out_x = in_x * matrix[3][0][0] + in_y * matrix[3][1][0] + matrix[3][2][0]; out_x = in_x * matrix[3][0][0] + in_y * matrix[3][1][0] + matrix[3][2][0];
out_y = in_x * matrix[3][0][1] + in_y * matrix[3][1][1] + matrix[3][2][1]; out_y = in_x * matrix[3][0][1] + in_y * matrix[3][1][1] + matrix[3][2][1];
float out_z = in_x * matrix[3][0][2] + in_y * matrix[3][1][2] + matrix[3][2][2];
if (out_z != 0)
{
out_x /= out_z;
out_y /= out_z;
}
} }
void olc::GFX2D::Transform2D::Invert() void olc::GFX2D::Transform2D::Invert()
@ -285,4 +309,5 @@ namespace olc
} }
} }
#endif
#endif #endif
Loading…
Cancel
Save